gl_rasterizer.h 7.4 KB

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