gl_rasterizer.h 9.1 KB

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