gl_rasterizer.h 8.6 KB

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