gl_rasterizer.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_stream_buffer.h"
  29. #include "video_core/renderer_opengl/gl_texture_cache.h"
  30. #include "video_core/shader/async_shaders.h"
  31. #include "video_core/textures/texture.h"
  32. namespace Core::Memory {
  33. class Memory;
  34. }
  35. namespace Core::Frontend {
  36. class EmuWindow;
  37. }
  38. namespace Tegra {
  39. class MemoryManager;
  40. }
  41. namespace OpenGL {
  42. struct ScreenInfo;
  43. struct ShaderEntries;
  44. struct BindlessSSBO {
  45. GLuint64EXT address;
  46. GLsizei length;
  47. GLsizei padding;
  48. };
  49. static_assert(sizeof(BindlessSSBO) * CHAR_BIT == 128);
  50. class RasterizerOpenGL : public VideoCore::RasterizerAccelerated {
  51. public:
  52. explicit RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  53. Core::Memory::Memory& cpu_memory_, const Device& device_,
  54. ScreenInfo& screen_info_, ProgramManager& program_manager_,
  55. StateTracker& state_tracker_);
  56. ~RasterizerOpenGL() override;
  57. void Draw(bool is_indexed, bool is_instanced) override;
  58. void Clear() override;
  59. void DispatchCompute(GPUVAddr code_addr) override;
  60. void ResetCounter(VideoCore::QueryType type) override;
  61. void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
  62. void FlushAll() override;
  63. void FlushRegion(VAddr addr, u64 size) override;
  64. bool MustFlushRegion(VAddr addr, u64 size) override;
  65. void InvalidateRegion(VAddr addr, u64 size) override;
  66. void OnCPUWrite(VAddr addr, u64 size) override;
  67. void SyncGuestHost() override;
  68. void UnmapMemory(VAddr addr, u64 size) override;
  69. void SignalSemaphore(GPUVAddr addr, u32 value) override;
  70. void SignalSyncPoint(u32 value) override;
  71. void ReleaseFences() override;
  72. void FlushAndInvalidateRegion(VAddr addr, u64 size) override;
  73. void WaitForIdle() override;
  74. void FragmentBarrier() override;
  75. void TiledCacheBarrier() override;
  76. void FlushCommands() override;
  77. void TickFrame() override;
  78. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  79. const Tegra::Engines::Fermi2D::Surface& dst,
  80. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  81. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  82. u32 pixel_stride) override;
  83. void LoadDiskResources(u64 title_id, const std::atomic_bool& stop_loading,
  84. const VideoCore::DiskResourceLoadCallback& callback) override;
  85. /// Returns true when there are commands queued to the OpenGL server.
  86. bool AnyCommandQueued() const {
  87. return num_queued_commands > 0;
  88. }
  89. VideoCommon::Shader::AsyncShaders& GetAsyncShaders() {
  90. return async_shaders;
  91. }
  92. const VideoCommon::Shader::AsyncShaders& GetAsyncShaders() const {
  93. return async_shaders;
  94. }
  95. private:
  96. static constexpr size_t MAX_TEXTURES = 192;
  97. static constexpr size_t MAX_IMAGES = 48;
  98. static constexpr size_t MAX_IMAGE_VIEWS = MAX_TEXTURES + MAX_IMAGES;
  99. void BindComputeTextures(Shader* kernel);
  100. void BindTextures(const ShaderEntries& entries, GLuint base_texture, GLuint base_image,
  101. size_t& image_view_index, size_t& texture_index, size_t& image_index);
  102. /// Configures the current constbuffers to use for the draw command.
  103. void SetupDrawConstBuffers(std::size_t stage_index, Shader* shader);
  104. /// Configures the current constbuffers to use for the kernel invocation.
  105. void SetupComputeConstBuffers(Shader* kernel);
  106. /// Configures a constant buffer.
  107. void SetupConstBuffer(GLenum stage, u32 binding, const Tegra::Engines::ConstBufferInfo& buffer,
  108. const ConstBufferEntry& entry, bool use_unified,
  109. std::size_t unified_offset);
  110. /// Configures the current global memory entries to use for the draw command.
  111. void SetupDrawGlobalMemory(std::size_t stage_index, Shader* shader);
  112. /// Configures the current global memory entries to use for the kernel invocation.
  113. void SetupComputeGlobalMemory(Shader* kernel);
  114. /// Configures a global memory buffer.
  115. void SetupGlobalMemory(u32 binding, const GlobalMemoryEntry& entry, GPUVAddr gpu_addr,
  116. size_t size, BindlessSSBO* ssbo);
  117. /// Configures the current textures to use for the draw command.
  118. void SetupDrawTextures(const Shader* shader, size_t stage_index);
  119. /// Configures the textures used in a compute shader.
  120. void SetupComputeTextures(const Shader* kernel);
  121. /// Configures images in a graphics shader.
  122. void SetupDrawImages(const Shader* shader, size_t stage_index);
  123. /// Configures images in a compute shader.
  124. void SetupComputeImages(const Shader* shader);
  125. /// Syncs the viewport and depth range to match the guest state
  126. void SyncViewport();
  127. /// Syncs the depth clamp state
  128. void SyncDepthClamp();
  129. /// Syncs the clip enabled status to match the guest state
  130. void SyncClipEnabled(u32 clip_mask);
  131. /// Syncs the clip coefficients to match the guest state
  132. void SyncClipCoef();
  133. /// Syncs the cull mode to match the guest state
  134. void SyncCullMode();
  135. /// Syncs the primitve restart to match the guest state
  136. void SyncPrimitiveRestart();
  137. /// Syncs the depth test state to match the guest state
  138. void SyncDepthTestState();
  139. /// Syncs the stencil test state to match the guest state
  140. void SyncStencilTestState();
  141. /// Syncs the blend state to match the guest state
  142. void SyncBlendState();
  143. /// Syncs the LogicOp state to match the guest state
  144. void SyncLogicOpState();
  145. /// Syncs the the color clamp state
  146. void SyncFragmentColorClampState();
  147. /// Syncs the alpha coverage and alpha to one
  148. void SyncMultiSampleState();
  149. /// Syncs the scissor test state to match the guest state
  150. void SyncScissorTest();
  151. /// Syncs the point state to match the guest state
  152. void SyncPointState();
  153. /// Syncs the line state to match the guest state
  154. void SyncLineState();
  155. /// Syncs the rasterizer enable state to match the guest state
  156. void SyncRasterizeEnable();
  157. /// Syncs polygon modes to match the guest state
  158. void SyncPolygonModes();
  159. /// Syncs Color Mask
  160. void SyncColorMask();
  161. /// Syncs the polygon offsets
  162. void SyncPolygonOffset();
  163. /// Syncs the alpha test state to match the guest state
  164. void SyncAlphaTest();
  165. /// Syncs the framebuffer sRGB state to match the guest state
  166. void SyncFramebufferSRGB();
  167. /// Syncs transform feedback state to match guest state
  168. /// @note Only valid on assembly shaders
  169. void SyncTransformFeedback();
  170. /// Begin a transform feedback
  171. void BeginTransformFeedback(GLenum primitive_mode);
  172. /// End a transform feedback
  173. void EndTransformFeedback();
  174. std::size_t CalculateVertexArraysSize() const;
  175. std::size_t CalculateIndexBufferSize() const;
  176. /// Updates the current vertex format
  177. void SetupVertexFormat();
  178. void SetupVertexBuffer();
  179. void SetupVertexInstances();
  180. GLintptr SetupIndexBuffer();
  181. void SetupShaders();
  182. Tegra::GPU& gpu;
  183. Tegra::Engines::Maxwell3D& maxwell3d;
  184. Tegra::Engines::KeplerCompute& kepler_compute;
  185. Tegra::MemoryManager& gpu_memory;
  186. const Device& device;
  187. ScreenInfo& screen_info;
  188. ProgramManager& program_manager;
  189. StateTracker& state_tracker;
  190. OGLStreamBuffer stream_buffer;
  191. TextureCacheRuntime texture_cache_runtime;
  192. TextureCache texture_cache;
  193. ShaderCacheOpenGL shader_cache;
  194. QueryCache query_cache;
  195. OGLBufferCache buffer_cache;
  196. FenceManagerOpenGL fence_manager;
  197. VideoCommon::Shader::AsyncShaders async_shaders;
  198. boost::container::static_vector<u32, MAX_IMAGE_VIEWS> image_view_indices;
  199. std::array<ImageViewId, MAX_IMAGE_VIEWS> image_view_ids;
  200. boost::container::static_vector<GLuint, MAX_TEXTURES> sampler_handles;
  201. std::array<GLuint, MAX_TEXTURES> texture_handles;
  202. std::array<GLuint, MAX_IMAGES> image_handles;
  203. std::array<OGLBuffer, Tegra::Engines::Maxwell3D::Regs::NumTransformFeedbackBuffers>
  204. transform_feedback_buffers;
  205. std::bitset<Tegra::Engines::Maxwell3D::Regs::NumTransformFeedbackBuffers>
  206. enabled_transform_feedback_buffers;
  207. static constexpr std::size_t NUM_CONSTANT_BUFFERS =
  208. Tegra::Engines::Maxwell3D::Regs::MaxConstBuffers *
  209. Tegra::Engines::Maxwell3D::Regs::MaxShaderProgram;
  210. std::array<GLuint, NUM_CONSTANT_BUFFERS> staging_cbufs{};
  211. std::size_t current_cbuf = 0;
  212. OGLBuffer unified_uniform_buffer;
  213. /// Number of commands queued to the OpenGL driver. Resetted on flush.
  214. std::size_t num_queued_commands = 0;
  215. u32 last_clip_distance_mask = 0;
  216. };
  217. } // namespace OpenGL