gl_rasterizer.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_interface.h"
  13. #include "video_core/renderer_opengl/blit_image.h"
  14. #include "video_core/renderer_opengl/gl_blit_screen.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 FramebufferTextureInfo;
  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::RasterizerInterface,
  57. protected VideoCommon::ChannelSetupCaches<VideoCommon::ChannelInfo> {
  58. public:
  59. explicit RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  60. Tegra::MaxwellDeviceMemoryManager& device_memory_,
  61. const Device& device_, 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,
  71. VideoCommon::QueryPropertiesFlags flags, u32 payload, u32 subreport) override;
  72. void BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr, u32 size) override;
  73. void DisableGraphicsUniformBuffer(size_t stage, u32 index) override;
  74. void FlushAll() override;
  75. void FlushRegion(DAddr addr, u64 size,
  76. VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  77. bool MustFlushRegion(DAddr addr, u64 size,
  78. VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  79. VideoCore::RasterizerDownloadArea GetFlushArea(PAddr addr, u64 size) override;
  80. void InvalidateRegion(DAddr addr, u64 size,
  81. VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  82. void OnCacheInvalidation(PAddr addr, u64 size) override;
  83. bool OnCPUWrite(PAddr addr, u64 size) override;
  84. void InvalidateGPUCache() override;
  85. void UnmapMemory(DAddr addr, u64 size) override;
  86. void ModifyGPUMemory(size_t as_id, GPUVAddr addr, u64 size) override;
  87. void SignalFence(std::function<void()>&& func) override;
  88. void SyncOperation(std::function<void()>&& func) override;
  89. void SignalSyncPoint(u32 value) override;
  90. void SignalReference() override;
  91. void ReleaseFences(bool force = true) override;
  92. void FlushAndInvalidateRegion(
  93. DAddr addr, u64 size, VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  94. void WaitForIdle() override;
  95. void FragmentBarrier() override;
  96. void TiledCacheBarrier() override;
  97. void FlushCommands() override;
  98. void TickFrame() override;
  99. bool AccelerateConditionalRendering() override;
  100. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  101. const Tegra::Engines::Fermi2D::Surface& dst,
  102. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  103. Tegra::Engines::AccelerateDMAInterface& AccessAccelerateDMA() override;
  104. void AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
  105. std::span<const u8> memory) override;
  106. void LoadDiskResources(u64 title_id, std::stop_token stop_loading,
  107. const VideoCore::DiskResourceLoadCallback& callback) override;
  108. /// Returns true when there are commands queued to the OpenGL server.
  109. bool AnyCommandQueued() const {
  110. return num_queued_commands > 0;
  111. }
  112. void InitializeChannel(Tegra::Control::ChannelState& channel) override;
  113. void BindChannel(Tegra::Control::ChannelState& channel) override;
  114. void ReleaseChannel(s32 channel_id) override;
  115. void RegisterTransformFeedback(GPUVAddr tfb_object_addr) override;
  116. bool HasDrawTransformFeedback() override {
  117. return true;
  118. }
  119. std::optional<FramebufferTextureInfo> AccelerateDisplay(const Tegra::FramebufferConfig& config,
  120. VAddr framebuffer_addr,
  121. u32 pixel_stride);
  122. private:
  123. static constexpr size_t MAX_TEXTURES = 192;
  124. static constexpr size_t MAX_IMAGES = 48;
  125. static constexpr size_t MAX_IMAGE_VIEWS = MAX_TEXTURES + MAX_IMAGES;
  126. template <typename Func>
  127. void PrepareDraw(bool is_indexed, Func&&);
  128. /// Syncs state to match guest's
  129. void SyncState();
  130. /// Syncs the viewport and depth range to match the guest state
  131. void SyncViewport();
  132. /// Syncs the depth clamp state
  133. void SyncDepthClamp();
  134. /// Syncs the clip enabled status to match the guest state
  135. void SyncClipEnabled(u32 clip_mask);
  136. /// Syncs the clip coefficients to match the guest state
  137. void SyncClipCoef();
  138. /// Syncs the cull mode to match the guest state
  139. void SyncCullMode();
  140. /// Syncs the primitive restart to match the guest state
  141. void SyncPrimitiveRestart();
  142. /// Syncs the depth test state to match the guest state
  143. void SyncDepthTestState();
  144. /// Syncs the stencil test state to match the guest state
  145. void SyncStencilTestState();
  146. /// Syncs the blend state to match the guest state
  147. void SyncBlendState();
  148. /// Syncs the LogicOp state to match the guest state
  149. void SyncLogicOpState();
  150. /// Syncs the the color clamp state
  151. void SyncFragmentColorClampState();
  152. /// Syncs the alpha coverage and alpha to one
  153. void SyncMultiSampleState();
  154. /// Syncs the scissor test state to match the guest state
  155. void SyncScissorTest();
  156. /// Syncs the point state to match the guest state
  157. void SyncPointState();
  158. /// Syncs the line state to match the guest state
  159. void SyncLineState();
  160. /// Syncs the rasterizer enable state to match the guest state
  161. void SyncRasterizeEnable();
  162. /// Syncs polygon modes to match the guest state
  163. void SyncPolygonModes();
  164. /// Syncs Color Mask
  165. void SyncColorMask();
  166. /// Syncs the polygon offsets
  167. void SyncPolygonOffset();
  168. /// Syncs the alpha test state to match the guest state
  169. void SyncAlphaTest();
  170. /// Syncs the framebuffer sRGB state to match the guest state
  171. void SyncFramebufferSRGB();
  172. /// Syncs vertex formats to match the guest state
  173. void SyncVertexFormats();
  174. /// Syncs vertex instances to match the guest state
  175. void SyncVertexInstances();
  176. /// Begin a transform feedback
  177. void BeginTransformFeedback(GraphicsPipeline* pipeline, GLenum primitive_mode);
  178. /// End a transform feedback
  179. void EndTransformFeedback();
  180. void QueryFallback(GPUVAddr gpu_addr, VideoCommon::QueryType type,
  181. VideoCommon::QueryPropertiesFlags flags, u32 payload, u32 subreport);
  182. Tegra::GPU& gpu;
  183. Tegra::MaxwellDeviceMemoryManager& device_memory;
  184. const Device& device;
  185. ProgramManager& program_manager;
  186. StateTracker& state_tracker;
  187. StagingBufferPool staging_buffer_pool;
  188. TextureCacheRuntime texture_cache_runtime;
  189. TextureCache texture_cache;
  190. BufferCacheRuntime buffer_cache_runtime;
  191. BufferCache buffer_cache;
  192. ShaderCache shader_cache;
  193. QueryCache query_cache;
  194. AccelerateDMA accelerate_dma;
  195. FenceManagerOpenGL fence_manager;
  196. BlitImageHelper blit_image;
  197. boost::container::static_vector<u32, MAX_IMAGE_VIEWS> image_view_indices;
  198. std::array<ImageViewId, MAX_IMAGE_VIEWS> image_view_ids;
  199. boost::container::static_vector<GLuint, MAX_TEXTURES> sampler_handles;
  200. std::array<GLuint, MAX_TEXTURES> texture_handles{};
  201. std::array<GLuint, MAX_IMAGES> image_handles{};
  202. /// Number of commands queued to the OpenGL driver. Reset on flush.
  203. size_t num_queued_commands = 0;
  204. bool has_written_global_memory = false;
  205. u32 last_clip_distance_mask = 0;
  206. };
  207. } // namespace OpenGL