gl_rasterizer.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <cstddef>
  7. #include <map>
  8. #include <memory>
  9. #include <tuple>
  10. #include <utility>
  11. #include <vector>
  12. #include <boost/icl/interval_map.hpp>
  13. #include <boost/optional.hpp>
  14. #include <boost/range/iterator_range.hpp>
  15. #include <glad/glad.h>
  16. #include "common/common_types.h"
  17. #include "video_core/engines/maxwell_3d.h"
  18. #include "video_core/memory_manager.h"
  19. #include "video_core/rasterizer_cache.h"
  20. #include "video_core/rasterizer_interface.h"
  21. #include "video_core/renderer_opengl/gl_buffer_cache.h"
  22. #include "video_core/renderer_opengl/gl_primitive_assembler.h"
  23. #include "video_core/renderer_opengl/gl_rasterizer_cache.h"
  24. #include "video_core/renderer_opengl/gl_resource_manager.h"
  25. #include "video_core/renderer_opengl/gl_shader_cache.h"
  26. #include "video_core/renderer_opengl/gl_shader_gen.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_stream_buffer.h"
  30. namespace Core::Frontend {
  31. class EmuWindow;
  32. }
  33. namespace OpenGL {
  34. struct ScreenInfo;
  35. struct DrawParameters;
  36. class RasterizerOpenGL : public VideoCore::RasterizerInterface {
  37. public:
  38. explicit RasterizerOpenGL(Core::Frontend::EmuWindow& renderer, ScreenInfo& info);
  39. ~RasterizerOpenGL() override;
  40. void DrawArrays() override;
  41. void Clear() override;
  42. void FlushAll() override;
  43. void FlushRegion(VAddr addr, u64 size) override;
  44. void InvalidateRegion(VAddr addr, u64 size) override;
  45. void FlushAndInvalidateRegion(VAddr addr, u64 size) override;
  46. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src,
  47. const Tegra::Engines::Fermi2D::Regs::Surface& dst) override;
  48. bool AccelerateFill(const void* config) override;
  49. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  50. u32 pixel_stride) override;
  51. bool AccelerateDrawBatch(bool is_indexed) override;
  52. void UpdatePagesCachedCount(Tegra::GPUVAddr addr, u64 size, int delta) override;
  53. /// OpenGL shader generated for a given Maxwell register state
  54. struct MaxwellShader {
  55. /// OpenGL shader resource
  56. OGLProgram shader;
  57. };
  58. struct VertexShader {
  59. OGLShader shader;
  60. };
  61. struct FragmentShader {
  62. OGLShader shader;
  63. };
  64. /// Maximum supported size that a constbuffer can have in bytes.
  65. static constexpr std::size_t MaxConstbufferSize = 0x10000;
  66. static_assert(MaxConstbufferSize % sizeof(GLvec4) == 0,
  67. "The maximum size of a constbuffer must be a multiple of the size of GLvec4");
  68. private:
  69. class SamplerInfo {
  70. public:
  71. OGLSampler sampler;
  72. /// Creates the sampler object, initializing its state so that it's in sync with the
  73. /// SamplerInfo struct.
  74. void Create();
  75. /// Syncs the sampler object with the config, updating any necessary state.
  76. void SyncWithConfig(const Tegra::Texture::TSCEntry& config);
  77. private:
  78. Tegra::Texture::TextureFilter mag_filter;
  79. Tegra::Texture::TextureFilter min_filter;
  80. Tegra::Texture::TextureMipmapFilter mip_filter;
  81. Tegra::Texture::WrapMode wrap_u;
  82. Tegra::Texture::WrapMode wrap_v;
  83. Tegra::Texture::WrapMode wrap_p;
  84. bool uses_depth_compare;
  85. Tegra::Texture::DepthCompareFunc depth_compare_func;
  86. GLvec4 border_color;
  87. };
  88. /**
  89. * Configures the color and depth framebuffer states.
  90. * @param use_color_fb If true, configure color framebuffers.
  91. * @param using_depth_fb If true, configure the depth/stencil framebuffer.
  92. * @param preserve_contents If true, tries to preserve data from a previously used framebuffer.
  93. * @param single_color_target Specifies if a single color buffer target should be used.
  94. */
  95. void ConfigureFramebuffers(bool use_color_fb = true, bool using_depth_fb = true,
  96. bool preserve_contents = true,
  97. boost::optional<std::size_t> single_color_target = {});
  98. /*
  99. * Configures the current constbuffers to use for the draw command.
  100. * @param stage The shader stage to configure buffers for.
  101. * @param shader The shader object that contains the specified stage.
  102. * @param current_bindpoint The offset at which to start counting new buffer bindpoints.
  103. * @returns The next available bindpoint for use in the next shader stage.
  104. */
  105. u32 SetupConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, Shader& shader,
  106. GLenum primitive_mode, u32 current_bindpoint);
  107. /*
  108. * Configures the current textures to use for the draw command.
  109. * @param stage The shader stage to configure textures for.
  110. * @param shader The shader object that contains the specified stage.
  111. * @param current_unit The offset at which to start counting unused texture units.
  112. * @returns The next available bindpoint for use in the next shader stage.
  113. */
  114. u32 SetupTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, Shader& shader,
  115. GLenum primitive_mode, u32 current_unit);
  116. /// Syncs the viewport to match the guest state
  117. void SyncViewport();
  118. /// Syncs the clip enabled status to match the guest state
  119. void SyncClipEnabled();
  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 range to match the guest state
  127. void SyncDepthRange();
  128. /// Syncs the depth test state to match the guest state
  129. void SyncDepthTestState();
  130. /// Syncs the stencil test state to match the guest state
  131. void SyncStencilTestState();
  132. /// Syncs the blend state to match the guest state
  133. void SyncBlendState();
  134. /// Syncs the LogicOp state to match the guest state
  135. void SyncLogicOpState();
  136. /// Syncs the scissor test state to match the guest state
  137. void SyncScissorTest();
  138. /// Syncs the transform feedback state to match the guest state
  139. void SyncTransformFeedback();
  140. /// Syncs the point state to match the guest state
  141. void SyncPointState();
  142. /// Check asserts for alpha testing.
  143. void CheckAlphaTests();
  144. bool has_ARB_direct_state_access = false;
  145. bool has_ARB_multi_bind = false;
  146. bool has_ARB_separate_shader_objects = false;
  147. bool has_ARB_vertex_attrib_binding = false;
  148. OpenGLState state;
  149. RasterizerCacheOpenGL res_cache;
  150. ShaderCacheOpenGL shader_cache;
  151. Core::Frontend::EmuWindow& emu_window;
  152. ScreenInfo& screen_info;
  153. std::unique_ptr<GLShader::ProgramManager> shader_program_manager;
  154. std::map<std::array<Tegra::Engines::Maxwell3D::Regs::VertexAttribute,
  155. Tegra::Engines::Maxwell3D::Regs::NumVertexAttributes>,
  156. OGLVertexArray>
  157. vertex_array_cache;
  158. std::array<SamplerInfo, Tegra::Engines::Maxwell3D::Regs::NumTextureSamplers> texture_samplers;
  159. static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;
  160. OGLBufferCache buffer_cache;
  161. OGLFramebuffer framebuffer;
  162. PrimitiveAssembler primitive_assembler{buffer_cache};
  163. GLint uniform_buffer_alignment;
  164. std::size_t CalculateVertexArraysSize() const;
  165. std::size_t CalculateIndexBufferSize() const;
  166. void SetupVertexArrays();
  167. DrawParameters SetupDraw();
  168. void SetupShaders(GLenum primitive_mode);
  169. enum class AccelDraw { Disabled, Arrays, Indexed };
  170. AccelDraw accelerate_draw = AccelDraw::Disabled;
  171. using CachedPageMap = boost::icl::interval_map<u64, int>;
  172. CachedPageMap cached_pages;
  173. };
  174. } // namespace OpenGL