gl_rasterizer.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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::WrapMode wrap_u;
  81. Tegra::Texture::WrapMode wrap_v;
  82. Tegra::Texture::WrapMode wrap_p;
  83. bool uses_depth_compare;
  84. Tegra::Texture::DepthCompareFunc depth_compare_func;
  85. GLvec4 border_color;
  86. };
  87. /**
  88. * Configures the color and depth framebuffer states.
  89. * @param use_color_fb If true, configure color framebuffers.
  90. * @param using_depth_fb If true, configure the depth/stencil framebuffer.
  91. * @param preserve_contents If true, tries to preserve data from a previously used framebuffer.
  92. * @param single_color_target Specifies if a single color buffer target should be used.
  93. */
  94. void ConfigureFramebuffers(bool use_color_fb = true, bool using_depth_fb = true,
  95. bool preserve_contents = true,
  96. boost::optional<std::size_t> single_color_target = {});
  97. /*
  98. * Configures the current constbuffers to use for the draw command.
  99. * @param stage The shader stage to configure buffers for.
  100. * @param shader The shader object that contains the specified stage.
  101. * @param current_bindpoint The offset at which to start counting new buffer bindpoints.
  102. * @returns The next available bindpoint for use in the next shader stage.
  103. */
  104. u32 SetupConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, Shader& shader,
  105. GLenum primitive_mode, u32 current_bindpoint);
  106. /*
  107. * Configures the current textures to use for the draw command.
  108. * @param stage The shader stage to configure textures for.
  109. * @param shader The shader object that contains the specified stage.
  110. * @param current_unit The offset at which to start counting unused texture units.
  111. * @returns The next available bindpoint for use in the next shader stage.
  112. */
  113. u32 SetupTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, Shader& shader,
  114. GLenum primitive_mode, u32 current_unit);
  115. /// Syncs the viewport to match the guest state
  116. void SyncViewport();
  117. /// Syncs the clip enabled status to match the guest state
  118. void SyncClipEnabled();
  119. /// Syncs the clip coefficients to match the guest state
  120. void SyncClipCoef();
  121. /// Syncs the cull mode to match the guest state
  122. void SyncCullMode();
  123. /// Syncs the primitve restart to match the guest state
  124. void SyncPrimitiveRestart();
  125. /// Syncs the depth range to match the guest state
  126. void SyncDepthRange();
  127. /// Syncs the depth test state to match the guest state
  128. void SyncDepthTestState();
  129. /// Syncs the stencil test state to match the guest state
  130. void SyncStencilTestState();
  131. /// Syncs the blend state to match the guest state
  132. void SyncBlendState();
  133. /// Syncs the LogicOp state to match the guest state
  134. void SyncLogicOpState();
  135. /// Syncs the scissor test state to match the guest state
  136. void SyncScissorTest();
  137. /// Syncs the transform feedback state to match the guest state
  138. void SyncTransformFeedback();
  139. /// Syncs the point state to match the guest state
  140. void SyncPointState();
  141. /// Check asserts for alpha testing.
  142. void CheckAlphaTests();
  143. bool has_ARB_direct_state_access = false;
  144. bool has_ARB_multi_bind = false;
  145. bool has_ARB_separate_shader_objects = false;
  146. bool has_ARB_vertex_attrib_binding = false;
  147. OpenGLState state;
  148. RasterizerCacheOpenGL res_cache;
  149. ShaderCacheOpenGL shader_cache;
  150. Core::Frontend::EmuWindow& emu_window;
  151. ScreenInfo& screen_info;
  152. std::unique_ptr<GLShader::ProgramManager> shader_program_manager;
  153. std::map<std::array<Tegra::Engines::Maxwell3D::Regs::VertexAttribute,
  154. Tegra::Engines::Maxwell3D::Regs::NumVertexAttributes>,
  155. OGLVertexArray>
  156. vertex_array_cache;
  157. std::array<SamplerInfo, Tegra::Engines::Maxwell3D::Regs::NumTextureSamplers> texture_samplers;
  158. static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;
  159. OGLBufferCache buffer_cache;
  160. OGLFramebuffer framebuffer;
  161. PrimitiveAssembler primitive_assembler{buffer_cache};
  162. GLint uniform_buffer_alignment;
  163. std::size_t CalculateVertexArraysSize() const;
  164. std::size_t CalculateIndexBufferSize() const;
  165. void SetupVertexArrays();
  166. DrawParameters SetupDraw();
  167. void SetupShaders(GLenum primitive_mode);
  168. enum class AccelDraw { Disabled, Arrays, Indexed };
  169. AccelDraw accelerate_draw = AccelDraw::Disabled;
  170. using CachedPageMap = boost::icl::interval_map<u64, int>;
  171. CachedPageMap cached_pages;
  172. };
  173. } // namespace OpenGL