gl_rasterizer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <cstring>
  8. #include <memory>
  9. #include <unordered_map>
  10. #include <vector>
  11. #include <glad/glad.h>
  12. #include "common/bit_field.h"
  13. #include "common/common_types.h"
  14. #include "common/hash.h"
  15. #include "common/vector_math.h"
  16. #include "video_core/rasterizer_interface.h"
  17. #include "video_core/renderer_opengl/gl_rasterizer_cache.h"
  18. #include "video_core/renderer_opengl/gl_resource_manager.h"
  19. #include "video_core/renderer_opengl/gl_shader_gen.h"
  20. #include "video_core/renderer_opengl/gl_state.h"
  21. #include "video_core/renderer_opengl/gl_stream_buffer.h"
  22. struct ScreenInfo;
  23. class RasterizerOpenGL : public VideoCore::RasterizerInterface {
  24. public:
  25. RasterizerOpenGL();
  26. ~RasterizerOpenGL() override;
  27. void DrawArrays() override;
  28. void NotifyMaxwellRegisterChanged(u32 id) override;
  29. void FlushAll() override;
  30. void FlushRegion(VAddr addr, u64 size) override;
  31. void InvalidateRegion(VAddr addr, u64 size) override;
  32. void FlushAndInvalidateRegion(VAddr addr, u64 size) override;
  33. bool AccelerateDisplayTransfer(const void* config) override;
  34. bool AccelerateTextureCopy(const void* config) override;
  35. bool AccelerateFill(const void* config) override;
  36. bool AccelerateDisplay(const Tegra::FramebufferConfig& framebuffer, VAddr framebuffer_addr,
  37. u32 pixel_stride, ScreenInfo& screen_info) override;
  38. bool AccelerateDrawBatch(bool is_indexed) override;
  39. /// OpenGL shader generated for a given Maxwell register state
  40. struct MaxwellShader {
  41. /// OpenGL shader resource
  42. OGLProgram shader;
  43. };
  44. struct VertexShader {
  45. OGLShader shader;
  46. };
  47. struct FragmentShader {
  48. OGLShader shader;
  49. };
  50. /// Uniform structure for the Uniform Buffer Object, all vectors must be 16-byte aligned
  51. // NOTE: Always keep a vec4 at the end. The GL spec is not clear wether the alignment at
  52. // the end of a uniform block is included in UNIFORM_BLOCK_DATA_SIZE or not.
  53. // Not following that rule will cause problems on some AMD drivers.
  54. struct UniformData {};
  55. // static_assert(
  56. // sizeof(UniformData) == 0x460,
  57. // "The size of the UniformData structure has changed, update the structure in the shader");
  58. static_assert(sizeof(UniformData) < 16384,
  59. "UniformData structure must be less than 16kb as per the OpenGL spec");
  60. struct VSUniformData {};
  61. // static_assert(
  62. // sizeof(VSUniformData) == 1856,
  63. // "The size of the VSUniformData structure has changed, update the structure in the
  64. // shader");
  65. static_assert(sizeof(VSUniformData) < 16384,
  66. "VSUniformData structure must be less than 16kb as per the OpenGL spec");
  67. struct FSUniformData {};
  68. // static_assert(
  69. // sizeof(FSUniformData) == 1856,
  70. // "The size of the FSUniformData structure has changed, update the structure in the
  71. // shader");
  72. static_assert(sizeof(FSUniformData) < 16384,
  73. "FSUniformData structure must be less than 16kb as per the OpenGL spec");
  74. private:
  75. class SamplerInfo {
  76. public:
  77. OGLSampler sampler;
  78. /// Creates the sampler object, initializing its state so that it's in sync with the
  79. /// SamplerInfo struct.
  80. void Create();
  81. /// Syncs the sampler object with the config, updating any necessary state.
  82. void SyncWithConfig(const Tegra::Texture::TSCEntry& config);
  83. private:
  84. Tegra::Texture::TextureFilter mag_filter;
  85. Tegra::Texture::TextureFilter min_filter;
  86. Tegra::Texture::WrapMode wrap_u;
  87. Tegra::Texture::WrapMode wrap_v;
  88. u32 border_color_r;
  89. u32 border_color_g;
  90. u32 border_color_b;
  91. u32 border_color_a;
  92. };
  93. /// Binds the framebuffer color and depth surface
  94. void BindFramebufferSurfaces(const Surface& color_surface, const Surface& depth_surface,
  95. bool has_stencil);
  96. /// Binds the required textures to OpenGL before drawing a batch.
  97. void BindTextures();
  98. /// Syncs the viewport to match the guest state
  99. void SyncViewport(const MathUtil::Rectangle<u32>& surfaces_rect, u16 res_scale);
  100. /// Syncs the clip enabled status to match the guest state
  101. void SyncClipEnabled();
  102. /// Syncs the clip coefficients to match the guest state
  103. void SyncClipCoef();
  104. /// Sets the OpenGL shader in accordance with the current guest state
  105. void SetShader();
  106. /// Syncs the cull mode to match the guest state
  107. void SyncCullMode();
  108. /// Syncs the depth scale to match the guest state
  109. void SyncDepthScale();
  110. /// Syncs the depth offset to match the guest state
  111. void SyncDepthOffset();
  112. /// Syncs the blend enabled status to match the guest state
  113. void SyncBlendEnabled();
  114. /// Syncs the blend functions to match the guest state
  115. void SyncBlendFuncs();
  116. /// Syncs the blend color to match the guest state
  117. void SyncBlendColor();
  118. bool has_ARB_buffer_storage;
  119. bool has_ARB_direct_state_access;
  120. bool has_ARB_separate_shader_objects;
  121. bool has_ARB_vertex_attrib_binding;
  122. OpenGLState state;
  123. RasterizerCacheOpenGL res_cache;
  124. /// Shader used for test renderering - to be removed once we have emulated shaders
  125. MaxwellShader test_shader{};
  126. const MaxwellShader* current_shader{};
  127. bool shader_dirty{};
  128. struct {
  129. UniformData data;
  130. bool dirty;
  131. } uniform_block_data = {};
  132. OGLPipeline pipeline;
  133. OGLVertexArray sw_vao;
  134. OGLVertexArray hw_vao;
  135. std::array<bool, 16> hw_vao_enabled_attributes;
  136. std::array<SamplerInfo, 32> texture_samplers;
  137. static constexpr size_t VERTEX_BUFFER_SIZE = 128 * 1024 * 1024;
  138. std::unique_ptr<OGLStreamBuffer> vertex_buffer;
  139. OGLBuffer uniform_buffer;
  140. OGLFramebuffer framebuffer;
  141. static constexpr size_t STREAM_BUFFER_SIZE = 4 * 1024 * 1024;
  142. std::unique_ptr<OGLStreamBuffer> stream_buffer;
  143. GLsizeiptr vs_input_size;
  144. void AnalyzeVertexArray(bool is_indexed);
  145. void SetupVertexArray(u8* array_ptr, GLintptr buffer_offset);
  146. OGLBuffer vs_uniform_buffer;
  147. std::unordered_map<GLShader::MaxwellVSConfig, VertexShader*> vs_shader_map;
  148. std::unordered_map<std::string, VertexShader> vs_shader_cache;
  149. OGLShader vs_default_shader;
  150. void SetupVertexShader(VSUniformData* ub_ptr, GLintptr buffer_offset);
  151. OGLBuffer fs_uniform_buffer;
  152. std::unordered_map<GLShader::MaxwellFSConfig, FragmentShader*> fs_shader_map;
  153. std::unordered_map<std::string, FragmentShader> fs_shader_cache;
  154. OGLShader fs_default_shader;
  155. void SetupFragmentShader(FSUniformData* ub_ptr, GLintptr buffer_offset);
  156. enum class AccelDraw { Disabled, Arrays, Indexed };
  157. AccelDraw accelerate_draw;
  158. };