gl_rasterizer.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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(VideoCore::QueryType type) override;
  70. void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) 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 OnCPUWrite(VAddr addr, u64 size) override;
  82. void InvalidateGPUCache() override;
  83. void UnmapMemory(VAddr addr, u64 size) override;
  84. void ModifyGPUMemory(size_t as_id, GPUVAddr addr, u64 size) override;
  85. void SignalFence(std::function<void()>&& func) override;
  86. void SyncOperation(std::function<void()>&& func) override;
  87. void SignalSyncPoint(u32 value) override;
  88. void SignalReference() override;
  89. void ReleaseFences() override;
  90. void FlushAndInvalidateRegion(
  91. VAddr addr, u64 size, VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  92. void WaitForIdle() override;
  93. void FragmentBarrier() override;
  94. void TiledCacheBarrier() override;
  95. void FlushCommands() override;
  96. void TickFrame() override;
  97. bool AccelerateConditionalRendering() override;
  98. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  99. const Tegra::Engines::Fermi2D::Surface& dst,
  100. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  101. Tegra::Engines::AccelerateDMAInterface& AccessAccelerateDMA() override;
  102. void AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
  103. std::span<const u8> memory) override;
  104. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  105. u32 pixel_stride) 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. private:
  116. static constexpr size_t MAX_TEXTURES = 192;
  117. static constexpr size_t MAX_IMAGES = 48;
  118. static constexpr size_t MAX_IMAGE_VIEWS = MAX_TEXTURES + MAX_IMAGES;
  119. template <typename Func>
  120. void PrepareDraw(bool is_indexed, Func&&);
  121. /// Syncs state to match guest's
  122. void SyncState();
  123. /// Syncs the viewport and depth range to match the guest state
  124. void SyncViewport();
  125. /// Syncs the depth clamp state
  126. void SyncDepthClamp();
  127. /// Syncs the clip enabled status to match the guest state
  128. void SyncClipEnabled(u32 clip_mask);
  129. /// Syncs the clip coefficients to match the guest state
  130. void SyncClipCoef();
  131. /// Syncs the cull mode to match the guest state
  132. void SyncCullMode();
  133. /// Syncs the primitive restart to match the guest state
  134. void SyncPrimitiveRestart();
  135. /// Syncs the depth test state to match the guest state
  136. void SyncDepthTestState();
  137. /// Syncs the stencil test state to match the guest state
  138. void SyncStencilTestState();
  139. /// Syncs the blend state to match the guest state
  140. void SyncBlendState();
  141. /// Syncs the LogicOp state to match the guest state
  142. void SyncLogicOpState();
  143. /// Syncs the the color clamp state
  144. void SyncFragmentColorClampState();
  145. /// Syncs the alpha coverage and alpha to one
  146. void SyncMultiSampleState();
  147. /// Syncs the scissor test state to match the guest state
  148. void SyncScissorTest();
  149. /// Syncs the point state to match the guest state
  150. void SyncPointState();
  151. /// Syncs the line state to match the guest state
  152. void SyncLineState();
  153. /// Syncs the rasterizer enable state to match the guest state
  154. void SyncRasterizeEnable();
  155. /// Syncs polygon modes to match the guest state
  156. void SyncPolygonModes();
  157. /// Syncs Color Mask
  158. void SyncColorMask();
  159. /// Syncs the polygon offsets
  160. void SyncPolygonOffset();
  161. /// Syncs the alpha test state to match the guest state
  162. void SyncAlphaTest();
  163. /// Syncs the framebuffer sRGB state to match the guest state
  164. void SyncFramebufferSRGB();
  165. /// Syncs vertex formats to match the guest state
  166. void SyncVertexFormats();
  167. /// Syncs vertex instances to match the guest state
  168. void SyncVertexInstances();
  169. /// Begin a transform feedback
  170. void BeginTransformFeedback(GraphicsPipeline* pipeline, GLenum primitive_mode);
  171. /// End a transform feedback
  172. void EndTransformFeedback();
  173. Tegra::GPU& gpu;
  174. const Device& device;
  175. ScreenInfo& screen_info;
  176. ProgramManager& program_manager;
  177. StateTracker& state_tracker;
  178. TextureCacheRuntime texture_cache_runtime;
  179. TextureCache texture_cache;
  180. BufferCacheRuntime buffer_cache_runtime;
  181. BufferCache buffer_cache;
  182. ShaderCache shader_cache;
  183. QueryCache query_cache;
  184. AccelerateDMA accelerate_dma;
  185. FenceManagerOpenGL fence_manager;
  186. BlitImageHelper blit_image;
  187. boost::container::static_vector<u32, MAX_IMAGE_VIEWS> image_view_indices;
  188. std::array<ImageViewId, MAX_IMAGE_VIEWS> image_view_ids;
  189. boost::container::static_vector<GLuint, MAX_TEXTURES> sampler_handles;
  190. std::array<GLuint, MAX_TEXTURES> texture_handles{};
  191. std::array<GLuint, MAX_IMAGES> image_handles{};
  192. /// Number of commands queued to the OpenGL driver. Reset on flush.
  193. size_t num_queued_commands = 0;
  194. bool has_written_global_memory = false;
  195. u32 last_clip_distance_mask = 0;
  196. };
  197. } // namespace OpenGL