gl_rasterizer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_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.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. ScreenInfo& info);
  49. ~RasterizerOpenGL() override;
  50. void Draw(bool is_indexed, bool is_instanced) override;
  51. void Clear() override;
  52. void DispatchCompute(GPUVAddr code_addr) override;
  53. void ResetCounter(VideoCore::QueryType type) override;
  54. void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
  55. void FlushAll() override;
  56. void FlushRegion(CacheAddr addr, u64 size) override;
  57. void InvalidateRegion(CacheAddr addr, u64 size) override;
  58. void FlushAndInvalidateRegion(CacheAddr addr, u64 size) override;
  59. void FlushCommands() override;
  60. void TickFrame() override;
  61. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src,
  62. const Tegra::Engines::Fermi2D::Regs::Surface& dst,
  63. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  64. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  65. u32 pixel_stride) override;
  66. void LoadDiskResources(const std::atomic_bool& stop_loading,
  67. const VideoCore::DiskResourceLoadCallback& callback) override;
  68. /// Returns true when there are commands queued to the OpenGL server.
  69. bool AnyCommandQueued() const {
  70. return num_queued_commands > 0;
  71. }
  72. private:
  73. /// Configures the color and depth framebuffer states.
  74. void ConfigureFramebuffers();
  75. void ConfigureClearFramebuffer(OpenGLState& current_state, bool using_color_fb,
  76. bool using_depth_fb, bool using_stencil_fb);
  77. /// Configures the current constbuffers to use for the draw command.
  78. void SetupDrawConstBuffers(std::size_t stage_index, const Shader& shader);
  79. /// Configures the current constbuffers to use for the kernel invocation.
  80. void SetupComputeConstBuffers(const Shader& kernel);
  81. /// Configures a constant buffer.
  82. void SetupConstBuffer(u32 binding, const Tegra::Engines::ConstBufferInfo& buffer,
  83. const GLShader::ConstBufferEntry& entry);
  84. /// Configures the current global memory entries to use for the draw command.
  85. void SetupDrawGlobalMemory(std::size_t stage_index, const Shader& shader);
  86. /// Configures the current global memory entries to use for the kernel invocation.
  87. void SetupComputeGlobalMemory(const Shader& kernel);
  88. /// Configures a constant buffer.
  89. void SetupGlobalMemory(u32 binding, const GLShader::GlobalMemoryEntry& entry, GPUVAddr gpu_addr,
  90. std::size_t size);
  91. /// Configures the current textures to use for the draw command.
  92. void SetupDrawTextures(std::size_t stage_index, const Shader& shader);
  93. /// Configures the textures used in a compute shader.
  94. void SetupComputeTextures(const Shader& kernel);
  95. /// Configures a texture.
  96. void SetupTexture(u32 binding, const Tegra::Texture::FullTextureInfo& texture,
  97. const GLShader::SamplerEntry& entry);
  98. /// Configures images in a graphics shader.
  99. void SetupDrawImages(std::size_t stage_index, const Shader& shader);
  100. /// Configures images in a compute shader.
  101. void SetupComputeImages(const Shader& shader);
  102. /// Configures an image.
  103. void SetupImage(u32 binding, const Tegra::Texture::TICEntry& tic,
  104. const GLShader::ImageEntry& entry);
  105. /// Syncs the viewport and depth range to match the guest state
  106. void SyncViewport();
  107. /// Syncs the depth clamp state
  108. void SyncDepthClamp();
  109. /// Syncs the clip enabled status to match the guest state
  110. void SyncClipEnabled(
  111. const std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances>& clip_mask);
  112. /// Syncs the clip coefficients to match the guest state
  113. void SyncClipCoef();
  114. /// Syncs the cull mode to match the guest state
  115. void SyncCullMode();
  116. /// Syncs the primitve restart to match the guest state
  117. void SyncPrimitiveRestart();
  118. /// Syncs the depth test state to match the guest state
  119. void SyncDepthTestState();
  120. /// Syncs the stencil test state to match the guest state
  121. void SyncStencilTestState();
  122. /// Syncs the blend state to match the guest state
  123. void SyncBlendState();
  124. /// Syncs the LogicOp state to match the guest state
  125. void SyncLogicOpState();
  126. /// Syncs the the color clamp state
  127. void SyncFragmentColorClampState();
  128. /// Syncs the alpha coverage and alpha to one
  129. void SyncMultiSampleState();
  130. /// Syncs the scissor test state to match the guest state
  131. void SyncScissorTest();
  132. /// Syncs the transform feedback state to match the guest state
  133. void SyncTransformFeedback();
  134. /// Syncs the point state to match the guest state
  135. void SyncPointState();
  136. /// Syncs the rasterizer enable state to match the guest state
  137. void SyncRasterizeEnable();
  138. /// Syncs Color Mask
  139. void SyncColorMask();
  140. /// Syncs the polygon offsets
  141. void SyncPolygonOffset();
  142. /// Syncs the alpha test state to match the guest state
  143. void SyncAlphaTest();
  144. /// Syncs the framebuffer sRGB state to match the guest state
  145. void SyncFramebufferSRGB();
  146. /// Check for extension that are not strictly required but are needed for correct emulation
  147. void CheckExtensions();
  148. std::size_t CalculateVertexArraysSize() const;
  149. std::size_t CalculateIndexBufferSize() const;
  150. /// Updates the current vertex format
  151. void SetupVertexFormat();
  152. void SetupVertexBuffer();
  153. void SetupVertexInstances();
  154. GLintptr SetupIndexBuffer();
  155. void SetupShaders(GLenum primitive_mode);
  156. const Device device;
  157. OpenGLState state;
  158. TextureCacheOpenGL texture_cache;
  159. ShaderCacheOpenGL shader_cache;
  160. SamplerCacheOpenGL sampler_cache;
  161. FramebufferCacheOpenGL framebuffer_cache;
  162. QueryCache query_cache;
  163. Core::System& system;
  164. ScreenInfo& screen_info;
  165. std::unique_ptr<GLShader::ProgramManager> shader_program_manager;
  166. static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;
  167. OGLBufferCache buffer_cache;
  168. VertexArrayPushBuffer vertex_array_pushbuffer;
  169. BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER};
  170. BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER};
  171. /// Number of commands queued to the OpenGL driver. Reseted on flush.
  172. std::size_t num_queued_commands = 0;
  173. };
  174. } // namespace OpenGL