gl_rasterizer.h 8.0 KB

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