gl_rasterizer.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <boost/icl/interval_map.hpp>
  14. #include <glad/glad.h>
  15. #include "common/common_types.h"
  16. #include "video_core/engines/const_buffer_info.h"
  17. #include "video_core/engines/maxwell_3d.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_resource_manager.h"
  24. #include "video_core/renderer_opengl/gl_sampler_cache.h"
  25. #include "video_core/renderer_opengl/gl_shader_cache.h"
  26. #include "video_core/renderer_opengl/gl_shader_decompiler.h"
  27. #include "video_core/renderer_opengl/gl_shader_manager.h"
  28. #include "video_core/renderer_opengl/gl_state.h"
  29. #include "video_core/renderer_opengl/gl_texture_cache.h"
  30. #include "video_core/renderer_opengl/utils.h"
  31. #include "video_core/textures/texture.h"
  32. namespace Core {
  33. class System;
  34. }
  35. namespace Core::Frontend {
  36. class EmuWindow;
  37. }
  38. namespace Tegra {
  39. class MemoryManager;
  40. }
  41. namespace OpenGL {
  42. struct ScreenInfo;
  43. struct DrawParameters;
  44. class RasterizerOpenGL : public VideoCore::RasterizerInterface {
  45. public:
  46. explicit RasterizerOpenGL(Core::System& system, Core::Frontend::EmuWindow& emu_window,
  47. ScreenInfo& info);
  48. ~RasterizerOpenGL() override;
  49. void DrawArrays() override;
  50. void Clear() override;
  51. void DispatchCompute(GPUVAddr code_addr) override;
  52. void FlushAll() override;
  53. void FlushRegion(CacheAddr addr, u64 size) override;
  54. void InvalidateRegion(CacheAddr addr, u64 size) override;
  55. void FlushAndInvalidateRegion(CacheAddr addr, u64 size) override;
  56. void FlushCommands() override;
  57. void TickFrame() override;
  58. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src,
  59. const Tegra::Engines::Fermi2D::Regs::Surface& dst,
  60. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  61. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  62. u32 pixel_stride) override;
  63. bool AccelerateDrawBatch(bool is_indexed) override;
  64. void UpdatePagesCachedCount(VAddr addr, u64 size, int delta) override;
  65. void LoadDiskResources(const std::atomic_bool& stop_loading,
  66. const VideoCore::DiskResourceLoadCallback& callback) override;
  67. private:
  68. struct FramebufferConfigState {
  69. bool using_color_fb{};
  70. bool using_depth_fb{};
  71. bool preserve_contents{};
  72. std::optional<std::size_t> single_color_target;
  73. bool operator==(const FramebufferConfigState& rhs) const {
  74. return std::tie(using_color_fb, using_depth_fb, preserve_contents,
  75. single_color_target) == std::tie(rhs.using_color_fb, rhs.using_depth_fb,
  76. rhs.preserve_contents,
  77. rhs.single_color_target);
  78. }
  79. bool operator!=(const FramebufferConfigState& rhs) const {
  80. return !operator==(rhs);
  81. }
  82. };
  83. /**
  84. * Configures the color and depth framebuffer states.
  85. *
  86. * @param current_state The current OpenGL state.
  87. * @param using_color_fb If true, configure color framebuffers.
  88. * @param using_depth_fb If true, configure the depth/stencil framebuffer.
  89. * @param preserve_contents If true, tries to preserve data from a previously used
  90. * framebuffer.
  91. * @param single_color_target Specifies if a single color buffer target should be used.
  92. *
  93. * @returns If depth (first) or stencil (second) are being stored in the bound zeta texture
  94. * (requires using_depth_fb to be true)
  95. */
  96. std::pair<bool, bool> ConfigureFramebuffers(
  97. OpenGLState& current_state, bool using_color_fb = true, bool using_depth_fb = true,
  98. bool preserve_contents = true, std::optional<std::size_t> single_color_target = {});
  99. void ConfigureClearFramebuffer(OpenGLState& current_state, bool using_color_fb,
  100. bool using_depth_fb, bool using_stencil_fb);
  101. /// Configures the current constbuffers to use for the draw command.
  102. void SetupDrawConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  103. const Shader& shader);
  104. /// Configures the current constbuffers to use for the kernel invocation.
  105. void SetupComputeConstBuffers(const Shader& kernel);
  106. /// Configures a constant buffer.
  107. void SetupConstBuffer(const Tegra::Engines::ConstBufferInfo& buffer,
  108. const GLShader::ConstBufferEntry& entry);
  109. /// Configures the current global memory entries to use for the draw command.
  110. void SetupDrawGlobalMemory(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  111. const Shader& shader);
  112. /// Configures the current global memory entries to use for the kernel invocation.
  113. void SetupComputeGlobalMemory(const Shader& kernel);
  114. /// Configures a constant buffer.
  115. void SetupGlobalMemory(const GLShader::GlobalMemoryEntry& entry, GPUVAddr gpu_addr,
  116. std::size_t size);
  117. /// Configures the current textures to use for the draw command. Returns shaders texture buffer
  118. /// usage.
  119. TextureBufferUsage SetupDrawTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  120. const Shader& shader, BaseBindings base_bindings);
  121. /// Configures the textures used in a compute shader. Returns texture buffer usage.
  122. TextureBufferUsage SetupComputeTextures(const Shader& kernel);
  123. /// Configures a texture. Returns true when the texture is a texture buffer.
  124. bool SetupTexture(u32 binding, const Tegra::Texture::FullTextureInfo& texture,
  125. const GLShader::SamplerEntry& entry);
  126. /// Configures images in a compute shader.
  127. void SetupComputeImages(const Shader& shader);
  128. /// Configures an image.
  129. void SetupImage(u32 binding, const Tegra::Texture::TICEntry& tic,
  130. const GLShader::ImageEntry& entry);
  131. /// Syncs the viewport and depth range to match the guest state
  132. void SyncViewport(OpenGLState& current_state);
  133. /// Syncs the clip enabled status to match the guest state
  134. void SyncClipEnabled(
  135. const std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances>& clip_mask);
  136. /// Syncs the clip coefficients to match the guest state
  137. void SyncClipCoef();
  138. /// Syncs the cull mode to match the guest state
  139. void SyncCullMode();
  140. /// Syncs the primitve restart to match the guest state
  141. void SyncPrimitiveRestart();
  142. /// Syncs the depth test state to match the guest state
  143. void SyncDepthTestState();
  144. /// Syncs the stencil test state to match the guest state
  145. void SyncStencilTestState();
  146. /// Syncs the blend state to match the guest state
  147. void SyncBlendState();
  148. /// Syncs the LogicOp state to match the guest state
  149. void SyncLogicOpState();
  150. /// Syncs the the color clamp state
  151. void SyncFragmentColorClampState();
  152. /// Syncs the alpha coverage and alpha to one
  153. void SyncMultiSampleState();
  154. /// Syncs the scissor test state to match the guest state
  155. void SyncScissorTest(OpenGLState& current_state);
  156. /// Syncs the transform feedback state to match the guest state
  157. void SyncTransformFeedback();
  158. /// Syncs the point state to match the guest state
  159. void SyncPointState();
  160. /// Syncs Color Mask
  161. void SyncColorMask();
  162. /// Syncs the polygon offsets
  163. void SyncPolygonOffset();
  164. /// Syncs the alpha test state to match the guest state
  165. void SyncAlphaTest();
  166. /// Check for extension that are not strictly required
  167. /// but are needed for correct emulation
  168. void CheckExtensions();
  169. const Device device;
  170. OpenGLState state;
  171. TextureCacheOpenGL texture_cache;
  172. ShaderCacheOpenGL shader_cache;
  173. SamplerCacheOpenGL sampler_cache;
  174. FramebufferCacheOpenGL framebuffer_cache;
  175. Core::System& system;
  176. ScreenInfo& screen_info;
  177. std::unique_ptr<GLShader::ProgramManager> shader_program_manager;
  178. std::map<std::array<Tegra::Engines::Maxwell3D::Regs::VertexAttribute,
  179. Tegra::Engines::Maxwell3D::Regs::NumVertexAttributes>,
  180. OGLVertexArray>
  181. vertex_array_cache;
  182. FramebufferConfigState current_framebuffer_config_state;
  183. std::pair<bool, bool> current_depth_stencil_usage{};
  184. static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;
  185. OGLBufferCache buffer_cache;
  186. VertexArrayPushBuffer vertex_array_pushbuffer;
  187. BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER};
  188. BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER};
  189. std::size_t CalculateVertexArraysSize() const;
  190. std::size_t CalculateIndexBufferSize() const;
  191. /// Updates and returns a vertex array object representing current vertex format
  192. GLuint SetupVertexFormat();
  193. void SetupVertexBuffer(GLuint vao);
  194. void SetupVertexInstances(GLuint vao);
  195. GLintptr SetupIndexBuffer();
  196. DrawParameters SetupDraw(GLintptr index_buffer_offset);
  197. void SetupShaders(GLenum primitive_mode);
  198. enum class AccelDraw { Disabled, Arrays, Indexed };
  199. AccelDraw accelerate_draw = AccelDraw::Disabled;
  200. OGLFramebuffer clear_framebuffer;
  201. using CachedPageMap = boost::icl::interval_map<u64, int>;
  202. CachedPageMap cached_pages;
  203. };
  204. } // namespace OpenGL