gl_rasterizer.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <map>
  9. #include <memory>
  10. #include <optional>
  11. #include <tuple>
  12. #include <utility>
  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_framebuffer_cache.h"
  23. #include "video_core/renderer_opengl/gl_query_cache.h"
  24. #include "video_core/renderer_opengl/gl_resource_manager.h"
  25. #include "video_core/renderer_opengl/gl_sampler_cache.h"
  26. #include "video_core/renderer_opengl/gl_shader_cache.h"
  27. #include "video_core/renderer_opengl/gl_shader_decompiler.h"
  28. #include "video_core/renderer_opengl/gl_shader_manager.h"
  29. #include "video_core/renderer_opengl/gl_state_tracker.h"
  30. #include "video_core/renderer_opengl/gl_texture_cache.h"
  31. #include "video_core/renderer_opengl/utils.h"
  32. #include "video_core/textures/texture.h"
  33. namespace Core {
  34. class System;
  35. }
  36. namespace Core::Frontend {
  37. class EmuWindow;
  38. }
  39. namespace Tegra {
  40. class MemoryManager;
  41. }
  42. namespace OpenGL {
  43. struct ScreenInfo;
  44. struct DrawParameters;
  45. class RasterizerOpenGL : public VideoCore::RasterizerAccelerated {
  46. public:
  47. explicit RasterizerOpenGL(Core::System& system, Core::Frontend::EmuWindow& emu_window,
  48. const Device& device, ScreenInfo& info,
  49. ProgramManager& program_manager, StateTracker& state_tracker);
  50. ~RasterizerOpenGL() override;
  51. void Draw(bool is_indexed, bool is_instanced) override;
  52. void Clear() override;
  53. void DispatchCompute(GPUVAddr code_addr) override;
  54. void ResetCounter(VideoCore::QueryType type) override;
  55. void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
  56. void FlushAll() override;
  57. void FlushRegion(VAddr addr, u64 size) override;
  58. bool MustFlushRegion(VAddr addr, u64 size) override;
  59. void InvalidateRegion(VAddr addr, u64 size) override;
  60. void OnCPUWrite(VAddr addr, u64 size) override;
  61. void SyncGuestHost() override;
  62. void SignalSemaphore(GPUVAddr addr, u32 value) override;
  63. void SignalSyncPoint(u32 value) override;
  64. void ReleaseFences() override;
  65. void FlushAndInvalidateRegion(VAddr addr, u64 size) override;
  66. void WaitForIdle() override;
  67. void FlushCommands() override;
  68. void TickFrame() override;
  69. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src,
  70. const Tegra::Engines::Fermi2D::Regs::Surface& dst,
  71. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  72. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  73. u32 pixel_stride) override;
  74. void LoadDiskResources(const std::atomic_bool& stop_loading,
  75. const VideoCore::DiskResourceLoadCallback& callback) override;
  76. void SetupDirtyFlags() override;
  77. /// Returns true when there are commands queued to the OpenGL server.
  78. bool AnyCommandQueued() const {
  79. return num_queued_commands > 0;
  80. }
  81. private:
  82. /// Configures the color and depth framebuffer states.
  83. void ConfigureFramebuffers();
  84. /// Configures the color and depth framebuffer for clearing.
  85. void ConfigureClearFramebuffer(bool using_color, bool using_depth_stencil);
  86. /// Configures the current constbuffers to use for the draw command.
  87. void SetupDrawConstBuffers(std::size_t stage_index, Shader* shader);
  88. /// Configures the current constbuffers to use for the kernel invocation.
  89. void SetupComputeConstBuffers(Shader* kernel);
  90. /// Configures a constant buffer.
  91. void SetupConstBuffer(GLenum stage, u32 binding, const Tegra::Engines::ConstBufferInfo& buffer,
  92. const ConstBufferEntry& entry, bool use_unified,
  93. std::size_t unified_offset);
  94. /// Configures the current global memory entries to use for the draw command.
  95. void SetupDrawGlobalMemory(std::size_t stage_index, Shader* shader);
  96. /// Configures the current global memory entries to use for the kernel invocation.
  97. void SetupComputeGlobalMemory(Shader* kernel);
  98. /// Configures a constant buffer.
  99. void SetupGlobalMemory(u32 binding, const GlobalMemoryEntry& entry, GPUVAddr gpu_addr,
  100. std::size_t size);
  101. /// Configures the current textures to use for the draw command.
  102. void SetupDrawTextures(std::size_t stage_index, Shader* shader);
  103. /// Configures the textures used in a compute shader.
  104. void SetupComputeTextures(Shader* kernel);
  105. /// Configures a texture.
  106. void SetupTexture(u32 binding, const Tegra::Texture::FullTextureInfo& texture,
  107. const SamplerEntry& entry);
  108. /// Configures images in a graphics shader.
  109. void SetupDrawImages(std::size_t stage_index, Shader* shader);
  110. /// Configures images in a compute shader.
  111. void SetupComputeImages(Shader* shader);
  112. /// Configures an image.
  113. void SetupImage(u32 binding, const Tegra::Texture::TICEntry& tic, const ImageEntry& entry);
  114. /// Syncs the viewport and depth range to match the guest state
  115. void SyncViewport();
  116. /// Syncs the depth clamp state
  117. void SyncDepthClamp();
  118. /// Syncs the clip enabled status to match the guest state
  119. void SyncClipEnabled(u32 clip_mask);
  120. /// Syncs the clip coefficients to match the guest state
  121. void SyncClipCoef();
  122. /// Syncs the cull mode to match the guest state
  123. void SyncCullMode();
  124. /// Syncs the primitve restart to match the guest state
  125. void SyncPrimitiveRestart();
  126. /// Syncs the depth test state to match the guest state
  127. void SyncDepthTestState();
  128. /// Syncs the stencil test state to match the guest state
  129. void SyncStencilTestState();
  130. /// Syncs the blend state to match the guest state
  131. void SyncBlendState();
  132. /// Syncs the LogicOp state to match the guest state
  133. void SyncLogicOpState();
  134. /// Syncs the the color clamp state
  135. void SyncFragmentColorClampState();
  136. /// Syncs the alpha coverage and alpha to one
  137. void SyncMultiSampleState();
  138. /// Syncs the scissor test state to match the guest state
  139. void SyncScissorTest();
  140. /// Syncs the point state to match the guest state
  141. void SyncPointState();
  142. /// Syncs the line state to match the guest state
  143. void SyncLineState();
  144. /// Syncs the rasterizer enable state to match the guest state
  145. void SyncRasterizeEnable();
  146. /// Syncs polygon modes to match the guest state
  147. void SyncPolygonModes();
  148. /// Syncs Color Mask
  149. void SyncColorMask();
  150. /// Syncs the polygon offsets
  151. void SyncPolygonOffset();
  152. /// Syncs the alpha test state to match the guest state
  153. void SyncAlphaTest();
  154. /// Syncs the framebuffer sRGB state to match the guest state
  155. void SyncFramebufferSRGB();
  156. /// Syncs transform feedback state to match guest state
  157. /// @note Only valid on assembly shaders
  158. void SyncTransformFeedback();
  159. /// Begin a transform feedback
  160. void BeginTransformFeedback(GLenum primitive_mode);
  161. /// End a transform feedback
  162. void EndTransformFeedback();
  163. /// Check for extension that are not strictly required but are needed for correct emulation
  164. void CheckExtensions();
  165. std::size_t CalculateVertexArraysSize() const;
  166. std::size_t CalculateIndexBufferSize() const;
  167. /// Updates the current vertex format
  168. void SetupVertexFormat();
  169. void SetupVertexBuffer();
  170. void SetupVertexInstances();
  171. GLintptr SetupIndexBuffer();
  172. void SetupShaders(GLenum primitive_mode);
  173. const Device& device;
  174. TextureCacheOpenGL texture_cache;
  175. ShaderCacheOpenGL shader_cache;
  176. SamplerCacheOpenGL sampler_cache;
  177. FramebufferCacheOpenGL framebuffer_cache;
  178. QueryCache query_cache;
  179. OGLBufferCache buffer_cache;
  180. FenceManagerOpenGL fence_manager;
  181. Core::System& system;
  182. ScreenInfo& screen_info;
  183. ProgramManager& program_manager;
  184. StateTracker& state_tracker;
  185. static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;
  186. GLint vertex_binding = 0;
  187. std::array<OGLBuffer, Tegra::Engines::Maxwell3D::Regs::NumTransformFeedbackBuffers>
  188. transform_feedback_buffers;
  189. std::bitset<Tegra::Engines::Maxwell3D::Regs::NumTransformFeedbackBuffers>
  190. enabled_transform_feedback_buffers;
  191. static constexpr std::size_t NUM_CONSTANT_BUFFERS =
  192. Tegra::Engines::Maxwell3D::Regs::MaxConstBuffers *
  193. Tegra::Engines::Maxwell3D::Regs::MaxShaderProgram;
  194. std::array<GLuint, NUM_CONSTANT_BUFFERS> staging_cbufs{};
  195. std::size_t current_cbuf = 0;
  196. OGLBuffer unified_uniform_buffer;
  197. /// Number of commands queued to the OpenGL driver. Reseted on flush.
  198. std::size_t num_queued_commands = 0;
  199. u32 last_clip_distance_mask = 0;
  200. };
  201. } // namespace OpenGL