gl_rasterizer.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-FileCopyrightText: 2015 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <cstddef>
  6. #include <optional>
  7. #include <boost/container/static_vector.hpp>
  8. #include <glad/glad.h>
  9. #include "common/common_types.h"
  10. #include "video_core/control/channel_state_cache.h"
  11. #include "video_core/engines/maxwell_dma.h"
  12. #include "video_core/rasterizer_accelerated.h"
  13. #include "video_core/rasterizer_interface.h"
  14. #include "video_core/renderer_opengl/gl_buffer_cache.h"
  15. #include "video_core/renderer_opengl/gl_device.h"
  16. #include "video_core/renderer_opengl/gl_fence_manager.h"
  17. #include "video_core/renderer_opengl/gl_query_cache.h"
  18. #include "video_core/renderer_opengl/gl_shader_cache.h"
  19. #include "video_core/renderer_opengl/gl_texture_cache.h"
  20. namespace Core::Memory {
  21. class Memory;
  22. }
  23. namespace Core::Frontend {
  24. class EmuWindow;
  25. }
  26. namespace Tegra {
  27. class MemoryManager;
  28. }
  29. namespace OpenGL {
  30. struct ScreenInfo;
  31. struct ShaderEntries;
  32. struct BindlessSSBO {
  33. GLuint64EXT address;
  34. GLsizei length;
  35. GLsizei padding;
  36. };
  37. static_assert(sizeof(BindlessSSBO) * CHAR_BIT == 128);
  38. class AccelerateDMA : public Tegra::Engines::AccelerateDMAInterface {
  39. public:
  40. explicit AccelerateDMA(BufferCache& buffer_cache);
  41. bool BufferCopy(GPUVAddr src_address, GPUVAddr dest_address, u64 amount) override;
  42. bool BufferClear(GPUVAddr src_address, u64 amount, u32 value) override;
  43. private:
  44. BufferCache& buffer_cache;
  45. };
  46. class RasterizerOpenGL : public VideoCore::RasterizerAccelerated,
  47. protected VideoCommon::ChannelSetupCaches<VideoCommon::ChannelInfo> {
  48. public:
  49. explicit RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  50. Core::Memory::Memory& cpu_memory_, const Device& device_,
  51. ScreenInfo& screen_info_, ProgramManager& program_manager_,
  52. StateTracker& state_tracker_);
  53. ~RasterizerOpenGL() override;
  54. void Draw(bool is_indexed, u32 instance_count) override;
  55. void DrawIndirect() override;
  56. void Clear(u32 layer_count) override;
  57. void DispatchCompute() override;
  58. void ResetCounter(VideoCore::QueryType type) override;
  59. void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
  60. void BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr, u32 size) override;
  61. void DisableGraphicsUniformBuffer(size_t stage, u32 index) override;
  62. void FlushAll() override;
  63. void FlushRegion(VAddr addr, u64 size,
  64. VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  65. bool MustFlushRegion(VAddr addr, u64 size,
  66. VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  67. void InvalidateRegion(VAddr addr, u64 size,
  68. VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  69. void OnCPUWrite(VAddr addr, u64 size) override;
  70. void InvalidateGPUCache() override;
  71. void UnmapMemory(VAddr addr, u64 size) override;
  72. void ModifyGPUMemory(size_t as_id, GPUVAddr addr, u64 size) override;
  73. void SignalFence(std::function<void()>&& func) override;
  74. void SyncOperation(std::function<void()>&& func) override;
  75. void SignalSyncPoint(u32 value) override;
  76. void SignalReference() override;
  77. void ReleaseFences() override;
  78. void FlushAndInvalidateRegion(
  79. VAddr addr, u64 size,
  80. VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  81. void WaitForIdle() override;
  82. void FragmentBarrier() override;
  83. void TiledCacheBarrier() override;
  84. void FlushCommands() override;
  85. void TickFrame() override;
  86. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  87. const Tegra::Engines::Fermi2D::Surface& dst,
  88. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  89. Tegra::Engines::AccelerateDMAInterface& AccessAccelerateDMA() override;
  90. void AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
  91. std::span<const u8> memory) override;
  92. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  93. u32 pixel_stride) override;
  94. void LoadDiskResources(u64 title_id, std::stop_token stop_loading,
  95. const VideoCore::DiskResourceLoadCallback& callback) override;
  96. /// Returns true when there are commands queued to the OpenGL server.
  97. bool AnyCommandQueued() const {
  98. return num_queued_commands > 0;
  99. }
  100. void InitializeChannel(Tegra::Control::ChannelState& channel) override;
  101. void BindChannel(Tegra::Control::ChannelState& channel) override;
  102. void ReleaseChannel(s32 channel_id) override;
  103. private:
  104. static constexpr size_t MAX_TEXTURES = 192;
  105. static constexpr size_t MAX_IMAGES = 48;
  106. static constexpr size_t MAX_IMAGE_VIEWS = MAX_TEXTURES + MAX_IMAGES;
  107. template <typename Func>
  108. void PrepareDraw(bool is_indexed, Func&&);
  109. /// Syncs state to match guest's
  110. void SyncState();
  111. /// Syncs the viewport and depth range to match the guest state
  112. void SyncViewport();
  113. /// Syncs the depth clamp state
  114. void SyncDepthClamp();
  115. /// Syncs the clip enabled status to match the guest state
  116. void SyncClipEnabled(u32 clip_mask);
  117. /// Syncs the clip coefficients to match the guest state
  118. void SyncClipCoef();
  119. /// Syncs the cull mode to match the guest state
  120. void SyncCullMode();
  121. /// Syncs the primitve restart to match the guest state
  122. void SyncPrimitiveRestart();
  123. /// Syncs the depth test state to match the guest state
  124. void SyncDepthTestState();
  125. /// Syncs the stencil test state to match the guest state
  126. void SyncStencilTestState();
  127. /// Syncs the blend state to match the guest state
  128. void SyncBlendState();
  129. /// Syncs the LogicOp state to match the guest state
  130. void SyncLogicOpState();
  131. /// Syncs the the color clamp state
  132. void SyncFragmentColorClampState();
  133. /// Syncs the alpha coverage and alpha to one
  134. void SyncMultiSampleState();
  135. /// Syncs the scissor test state to match the guest state
  136. void SyncScissorTest();
  137. /// Syncs the point state to match the guest state
  138. void SyncPointState();
  139. /// Syncs the line state to match the guest state
  140. void SyncLineState();
  141. /// Syncs the rasterizer enable state to match the guest state
  142. void SyncRasterizeEnable();
  143. /// Syncs polygon modes to match the guest state
  144. void SyncPolygonModes();
  145. /// Syncs Color Mask
  146. void SyncColorMask();
  147. /// Syncs the polygon offsets
  148. void SyncPolygonOffset();
  149. /// Syncs the alpha test state to match the guest state
  150. void SyncAlphaTest();
  151. /// Syncs the framebuffer sRGB state to match the guest state
  152. void SyncFramebufferSRGB();
  153. /// Syncs vertex formats to match the guest state
  154. void SyncVertexFormats();
  155. /// Syncs vertex instances to match the guest state
  156. void SyncVertexInstances();
  157. /// Begin a transform feedback
  158. void BeginTransformFeedback(GraphicsPipeline* pipeline, GLenum primitive_mode);
  159. /// End a transform feedback
  160. void EndTransformFeedback();
  161. Tegra::GPU& gpu;
  162. const Device& device;
  163. ScreenInfo& screen_info;
  164. ProgramManager& program_manager;
  165. StateTracker& state_tracker;
  166. TextureCacheRuntime texture_cache_runtime;
  167. TextureCache texture_cache;
  168. BufferCacheRuntime buffer_cache_runtime;
  169. BufferCache buffer_cache;
  170. ShaderCache shader_cache;
  171. QueryCache query_cache;
  172. AccelerateDMA accelerate_dma;
  173. FenceManagerOpenGL fence_manager;
  174. boost::container::static_vector<u32, MAX_IMAGE_VIEWS> image_view_indices;
  175. std::array<ImageViewId, MAX_IMAGE_VIEWS> image_view_ids;
  176. boost::container::static_vector<GLuint, MAX_TEXTURES> sampler_handles;
  177. std::array<GLuint, MAX_TEXTURES> texture_handles{};
  178. std::array<GLuint, MAX_IMAGES> image_handles{};
  179. /// Number of commands queued to the OpenGL driver. Resetted on flush.
  180. size_t num_queued_commands = 0;
  181. bool has_written_global_memory = false;
  182. u32 last_clip_distance_mask = 0;
  183. };
  184. } // namespace OpenGL