gl_rasterizer.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <atomic>
  7. #include <cstddef>
  8. #include <memory>
  9. #include <optional>
  10. #include <tuple>
  11. #include <utility>
  12. #include <boost/container/static_vector.hpp>
  13. #include <glad/glad.h>
  14. #include "common/common_types.h"
  15. #include "video_core/engines/const_buffer_info.h"
  16. #include "video_core/engines/maxwell_3d.h"
  17. #include "video_core/rasterizer_accelerated.h"
  18. #include "video_core/rasterizer_interface.h"
  19. #include "video_core/renderer_opengl/gl_buffer_cache.h"
  20. #include "video_core/renderer_opengl/gl_device.h"
  21. #include "video_core/renderer_opengl/gl_fence_manager.h"
  22. #include "video_core/renderer_opengl/gl_query_cache.h"
  23. #include "video_core/renderer_opengl/gl_resource_manager.h"
  24. #include "video_core/renderer_opengl/gl_shader_cache.h"
  25. #include "video_core/renderer_opengl/gl_shader_decompiler.h"
  26. #include "video_core/renderer_opengl/gl_shader_manager.h"
  27. #include "video_core/renderer_opengl/gl_state_tracker.h"
  28. #include "video_core/renderer_opengl/gl_texture_cache.h"
  29. #include "video_core/shader/async_shaders.h"
  30. #include "video_core/textures/texture.h"
  31. namespace Core::Memory {
  32. class Memory;
  33. }
  34. namespace Core::Frontend {
  35. class EmuWindow;
  36. }
  37. namespace Tegra {
  38. class MemoryManager;
  39. }
  40. namespace OpenGL {
  41. struct ScreenInfo;
  42. struct ShaderEntries;
  43. struct BindlessSSBO {
  44. GLuint64EXT address;
  45. GLsizei length;
  46. GLsizei padding;
  47. };
  48. static_assert(sizeof(BindlessSSBO) * CHAR_BIT == 128);
  49. class RasterizerOpenGL : public VideoCore::RasterizerAccelerated {
  50. public:
  51. explicit RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  52. Core::Memory::Memory& cpu_memory_, const Device& device_,
  53. ScreenInfo& screen_info_, ProgramManager& program_manager_,
  54. StateTracker& state_tracker_);
  55. ~RasterizerOpenGL() override;
  56. void Draw(bool is_indexed, bool is_instanced) override;
  57. void Clear() override;
  58. void DispatchCompute(GPUVAddr code_addr) override;
  59. void ResetCounter(VideoCore::QueryType type) override;
  60. void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
  61. void BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr, u32 size) override;
  62. void DisableGraphicsUniformBuffer(size_t stage, u32 index) override;
  63. void FlushAll() override;
  64. void FlushRegion(VAddr addr, u64 size) override;
  65. bool MustFlushRegion(VAddr addr, u64 size) override;
  66. void InvalidateRegion(VAddr addr, u64 size) override;
  67. void OnCPUWrite(VAddr addr, u64 size) override;
  68. void SyncGuestHost() override;
  69. void UnmapMemory(VAddr addr, u64 size) override;
  70. void ModifyGPUMemory(GPUVAddr addr, u64 size) override;
  71. void SignalSemaphore(GPUVAddr addr, u32 value) override;
  72. void SignalSyncPoint(u32 value) override;
  73. void SignalReference() override;
  74. void ReleaseFences() override;
  75. void FlushAndInvalidateRegion(VAddr addr, u64 size) override;
  76. void WaitForIdle() override;
  77. void FragmentBarrier() override;
  78. void TiledCacheBarrier() override;
  79. void FlushCommands() override;
  80. void TickFrame() override;
  81. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  82. const Tegra::Engines::Fermi2D::Surface& dst,
  83. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  84. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  85. u32 pixel_stride) override;
  86. void LoadDiskResources(u64 title_id, std::stop_token stop_loading,
  87. const VideoCore::DiskResourceLoadCallback& callback) override;
  88. /// Returns true when there are commands queued to the OpenGL server.
  89. bool AnyCommandQueued() const {
  90. return num_queued_commands > 0;
  91. }
  92. VideoCommon::Shader::AsyncShaders& GetAsyncShaders() {
  93. return async_shaders;
  94. }
  95. const VideoCommon::Shader::AsyncShaders& GetAsyncShaders() const {
  96. return async_shaders;
  97. }
  98. private:
  99. static constexpr size_t MAX_TEXTURES = 192;
  100. static constexpr size_t MAX_IMAGES = 48;
  101. static constexpr size_t MAX_IMAGE_VIEWS = MAX_TEXTURES + MAX_IMAGES;
  102. void BindComputeTextures(Shader* kernel);
  103. void BindTextures(const ShaderEntries& entries, GLuint base_texture, GLuint base_image,
  104. size_t& image_view_index, size_t& texture_index, size_t& image_index);
  105. /// Configures the current textures to use for the draw command.
  106. void SetupDrawTextures(const Shader* shader, size_t stage_index);
  107. /// Configures the textures used in a compute shader.
  108. void SetupComputeTextures(const Shader* kernel);
  109. /// Configures images in a graphics shader.
  110. void SetupDrawImages(const Shader* shader, size_t stage_index);
  111. /// Configures images in a compute shader.
  112. void SetupComputeImages(const Shader* shader);
  113. /// Syncs state to match guest's
  114. void SyncState();
  115. /// Syncs the viewport and depth range to match the guest state
  116. void SyncViewport();
  117. /// Syncs the depth clamp state
  118. void SyncDepthClamp();
  119. /// Syncs the clip enabled status to match the guest state
  120. void SyncClipEnabled(u32 clip_mask);
  121. /// Syncs the clip coefficients to match the guest state
  122. void SyncClipCoef();
  123. /// Syncs the cull mode to match the guest state
  124. void SyncCullMode();
  125. /// Syncs the primitve restart to match the guest state
  126. void SyncPrimitiveRestart();
  127. /// Syncs the depth test state to match the guest state
  128. void SyncDepthTestState();
  129. /// Syncs the stencil test state to match the guest state
  130. void SyncStencilTestState();
  131. /// Syncs the blend state to match the guest state
  132. void SyncBlendState();
  133. /// Syncs the LogicOp state to match the guest state
  134. void SyncLogicOpState();
  135. /// Syncs the the color clamp state
  136. void SyncFragmentColorClampState();
  137. /// Syncs the alpha coverage and alpha to one
  138. void SyncMultiSampleState();
  139. /// Syncs the scissor test state to match the guest state
  140. void SyncScissorTest();
  141. /// Syncs the point state to match the guest state
  142. void SyncPointState();
  143. /// Syncs the line state to match the guest state
  144. void SyncLineState();
  145. /// Syncs the rasterizer enable state to match the guest state
  146. void SyncRasterizeEnable();
  147. /// Syncs polygon modes to match the guest state
  148. void SyncPolygonModes();
  149. /// Syncs Color Mask
  150. void SyncColorMask();
  151. /// Syncs the polygon offsets
  152. void SyncPolygonOffset();
  153. /// Syncs the alpha test state to match the guest state
  154. void SyncAlphaTest();
  155. /// Syncs the framebuffer sRGB state to match the guest state
  156. void SyncFramebufferSRGB();
  157. /// Syncs vertex formats to match the guest state
  158. void SyncVertexFormats();
  159. /// Syncs vertex instances to match the guest state
  160. void SyncVertexInstances();
  161. /// Syncs transform feedback state to match guest state
  162. /// @note Only valid on assembly shaders
  163. void SyncTransformFeedback();
  164. /// Begin a transform feedback
  165. void BeginTransformFeedback(GLenum primitive_mode);
  166. /// End a transform feedback
  167. void EndTransformFeedback();
  168. void SetupShaders(bool is_indexed);
  169. Tegra::GPU& gpu;
  170. Tegra::Engines::Maxwell3D& maxwell3d;
  171. Tegra::Engines::KeplerCompute& kepler_compute;
  172. Tegra::MemoryManager& gpu_memory;
  173. const Device& device;
  174. ScreenInfo& screen_info;
  175. ProgramManager& program_manager;
  176. StateTracker& state_tracker;
  177. TextureCacheRuntime texture_cache_runtime;
  178. TextureCache texture_cache;
  179. BufferCacheRuntime buffer_cache_runtime;
  180. BufferCache buffer_cache;
  181. ShaderCacheOpenGL shader_cache;
  182. QueryCache query_cache;
  183. FenceManagerOpenGL fence_manager;
  184. VideoCommon::Shader::AsyncShaders async_shaders;
  185. boost::container::static_vector<u32, MAX_IMAGE_VIEWS> image_view_indices;
  186. std::array<ImageViewId, MAX_IMAGE_VIEWS> image_view_ids;
  187. boost::container::static_vector<GLuint, MAX_TEXTURES> sampler_handles;
  188. std::array<GLuint, MAX_TEXTURES> texture_handles{};
  189. std::array<GLuint, MAX_IMAGES> image_handles{};
  190. /// Number of commands queued to the OpenGL driver. Resetted on flush.
  191. std::size_t num_queued_commands = 0;
  192. u32 last_clip_distance_mask = 0;
  193. };
  194. } // namespace OpenGL