gl_rasterizer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 <cstddef>
  6. #include <cstring>
  7. #include <memory>
  8. #include <vector>
  9. #include <unordered_map>
  10. #include "common/common_types.h"
  11. #include "common/hash.h"
  12. #include "video_core/pica.h"
  13. #include "video_core/rasterizer_interface.h"
  14. #include "video_core/renderer_opengl/gl_rasterizer_cache.h"
  15. #include "video_core/renderer_opengl/gl_state.h"
  16. #include "video_core/shader/shader_interpreter.h"
  17. /**
  18. * This struct contains all state used to generate the GLSL shader program that emulates the current
  19. * Pica register configuration. This struct is used as a cache key for generated GLSL shader
  20. * programs. The functions in gl_shader_gen.cpp should retrieve state from this struct only, not by
  21. * directly accessing Pica registers. This should reduce the risk of bugs in shader generation where
  22. * Pica state is not being captured in the shader cache key, thereby resulting in (what should be)
  23. * two separate shaders sharing the same key.
  24. */
  25. struct PicaShaderConfig {
  26. /// Construct a PicaShaderConfig with the current Pica register configuration.
  27. static PicaShaderConfig CurrentConfig() {
  28. PicaShaderConfig res;
  29. const auto& regs = Pica::g_state.regs;
  30. res.alpha_test_func = regs.output_merger.alpha_test.enable ?
  31. regs.output_merger.alpha_test.func.Value() : Pica::Regs::CompareFunc::Always;
  32. // Copy relevant TevStageConfig fields only. We're doing this manually (instead of calling
  33. // the GetTevStages() function) because BitField explicitly disables copies.
  34. res.tev_stages[0].sources_raw = regs.tev_stage0.sources_raw;
  35. res.tev_stages[1].sources_raw = regs.tev_stage1.sources_raw;
  36. res.tev_stages[2].sources_raw = regs.tev_stage2.sources_raw;
  37. res.tev_stages[3].sources_raw = regs.tev_stage3.sources_raw;
  38. res.tev_stages[4].sources_raw = regs.tev_stage4.sources_raw;
  39. res.tev_stages[5].sources_raw = regs.tev_stage5.sources_raw;
  40. res.tev_stages[0].modifiers_raw = regs.tev_stage0.modifiers_raw;
  41. res.tev_stages[1].modifiers_raw = regs.tev_stage1.modifiers_raw;
  42. res.tev_stages[2].modifiers_raw = regs.tev_stage2.modifiers_raw;
  43. res.tev_stages[3].modifiers_raw = regs.tev_stage3.modifiers_raw;
  44. res.tev_stages[4].modifiers_raw = regs.tev_stage4.modifiers_raw;
  45. res.tev_stages[5].modifiers_raw = regs.tev_stage5.modifiers_raw;
  46. res.tev_stages[0].ops_raw = regs.tev_stage0.ops_raw;
  47. res.tev_stages[1].ops_raw = regs.tev_stage1.ops_raw;
  48. res.tev_stages[2].ops_raw = regs.tev_stage2.ops_raw;
  49. res.tev_stages[3].ops_raw = regs.tev_stage3.ops_raw;
  50. res.tev_stages[4].ops_raw = regs.tev_stage4.ops_raw;
  51. res.tev_stages[5].ops_raw = regs.tev_stage5.ops_raw;
  52. res.tev_stages[0].scales_raw = regs.tev_stage0.scales_raw;
  53. res.tev_stages[1].scales_raw = regs.tev_stage1.scales_raw;
  54. res.tev_stages[2].scales_raw = regs.tev_stage2.scales_raw;
  55. res.tev_stages[3].scales_raw = regs.tev_stage3.scales_raw;
  56. res.tev_stages[4].scales_raw = regs.tev_stage4.scales_raw;
  57. res.tev_stages[5].scales_raw = regs.tev_stage5.scales_raw;
  58. res.combiner_buffer_input =
  59. regs.tev_combiner_buffer_input.update_mask_rgb.Value() |
  60. regs.tev_combiner_buffer_input.update_mask_a.Value() << 4;
  61. return res;
  62. }
  63. bool TevStageUpdatesCombinerBufferColor(unsigned stage_index) const {
  64. return (stage_index < 4) && (combiner_buffer_input & (1 << stage_index));
  65. }
  66. bool TevStageUpdatesCombinerBufferAlpha(unsigned stage_index) const {
  67. return (stage_index < 4) && ((combiner_buffer_input >> 4) & (1 << stage_index));
  68. }
  69. bool operator ==(const PicaShaderConfig& o) const {
  70. return std::memcmp(this, &o, sizeof(PicaShaderConfig)) == 0;
  71. };
  72. Pica::Regs::CompareFunc alpha_test_func;
  73. std::array<Pica::Regs::TevStageConfig, 6> tev_stages = {};
  74. u8 combiner_buffer_input;
  75. };
  76. namespace std {
  77. template <>
  78. struct hash<PicaShaderConfig> {
  79. size_t operator()(const PicaShaderConfig& k) const {
  80. return Common::ComputeHash64(&k, sizeof(PicaShaderConfig));
  81. }
  82. };
  83. } // namespace std
  84. class RasterizerOpenGL : public VideoCore::RasterizerInterface {
  85. public:
  86. RasterizerOpenGL();
  87. ~RasterizerOpenGL() override;
  88. void InitObjects() override;
  89. void Reset() override;
  90. void AddTriangle(const Pica::Shader::OutputVertex& v0,
  91. const Pica::Shader::OutputVertex& v1,
  92. const Pica::Shader::OutputVertex& v2) override;
  93. void DrawTriangles() override;
  94. void FlushFramebuffer() override;
  95. void NotifyPicaRegisterChanged(u32 id) override;
  96. void FlushRegion(PAddr addr, u32 size) override;
  97. void InvalidateRegion(PAddr addr, u32 size) override;
  98. /// OpenGL shader generated for a given Pica register state
  99. struct PicaShader {
  100. /// OpenGL shader resource
  101. OGLShader shader;
  102. };
  103. private:
  104. /// Structure used for storing information about color textures
  105. struct TextureInfo {
  106. OGLTexture texture;
  107. GLsizei width;
  108. GLsizei height;
  109. Pica::Regs::ColorFormat format;
  110. GLenum gl_format;
  111. GLenum gl_type;
  112. };
  113. /// Structure used for storing information about depth textures
  114. struct DepthTextureInfo {
  115. OGLTexture texture;
  116. GLsizei width;
  117. GLsizei height;
  118. Pica::Regs::DepthFormat format;
  119. GLenum gl_format;
  120. GLenum gl_type;
  121. };
  122. struct SamplerInfo {
  123. using TextureConfig = Pica::Regs::TextureConfig;
  124. OGLSampler sampler;
  125. /// Creates the sampler object, initializing its state so that it's in sync with the SamplerInfo struct.
  126. void Create();
  127. /// Syncs the sampler object with the config, updating any necessary state.
  128. void SyncWithConfig(const TextureConfig& config);
  129. private:
  130. TextureConfig::TextureFilter mag_filter;
  131. TextureConfig::TextureFilter min_filter;
  132. TextureConfig::WrapMode wrap_s;
  133. TextureConfig::WrapMode wrap_t;
  134. u32 border_color;
  135. };
  136. /// Structure that the hardware rendered vertices are composed of
  137. struct HardwareVertex {
  138. HardwareVertex(const Pica::Shader::OutputVertex& v) {
  139. position[0] = v.pos.x.ToFloat32();
  140. position[1] = v.pos.y.ToFloat32();
  141. position[2] = v.pos.z.ToFloat32();
  142. position[3] = v.pos.w.ToFloat32();
  143. color[0] = v.color.x.ToFloat32();
  144. color[1] = v.color.y.ToFloat32();
  145. color[2] = v.color.z.ToFloat32();
  146. color[3] = v.color.w.ToFloat32();
  147. tex_coord0[0] = v.tc0.x.ToFloat32();
  148. tex_coord0[1] = v.tc0.y.ToFloat32();
  149. tex_coord1[0] = v.tc1.x.ToFloat32();
  150. tex_coord1[1] = v.tc1.y.ToFloat32();
  151. tex_coord2[0] = v.tc2.x.ToFloat32();
  152. tex_coord2[1] = v.tc2.y.ToFloat32();
  153. }
  154. GLfloat position[4];
  155. GLfloat color[4];
  156. GLfloat tex_coord0[2];
  157. GLfloat tex_coord1[2];
  158. GLfloat tex_coord2[2];
  159. };
  160. /// Uniform structure for the Uniform Buffer Object, all members must be 16-byte aligned
  161. struct UniformData {
  162. // A vec4 color for each of the six tev stages
  163. std::array<GLfloat, 4> const_color[6];
  164. std::array<GLfloat, 4> tev_combiner_buffer_color;
  165. GLint alphatest_ref;
  166. GLfloat depth_offset;
  167. INSERT_PADDING_BYTES(8);
  168. };
  169. static_assert(sizeof(UniformData) == 0x80, "The size of the UniformData structure has changed, update the structure in the shader");
  170. static_assert(sizeof(UniformData) < 16000, "UniformData structure must be less than 16kb as per the OpenGL spec");
  171. /// Reconfigure the OpenGL color texture to use the given format and dimensions
  172. void ReconfigureColorTexture(TextureInfo& texture, Pica::Regs::ColorFormat format, u32 width, u32 height);
  173. /// Reconfigure the OpenGL depth texture to use the given format and dimensions
  174. void ReconfigureDepthTexture(DepthTextureInfo& texture, Pica::Regs::DepthFormat format, u32 width, u32 height);
  175. /// Sets the OpenGL shader in accordance with the current PICA register state
  176. void SetShader();
  177. /// Syncs the state and contents of the OpenGL framebuffer to match the current PICA framebuffer
  178. void SyncFramebuffer();
  179. /// Syncs the cull mode to match the PICA register
  180. void SyncCullMode();
  181. /// Syncs the depth scale and offset to match the PICA registers
  182. void SyncDepthModifiers();
  183. /// Syncs the blend enabled status to match the PICA register
  184. void SyncBlendEnabled();
  185. /// Syncs the blend functions to match the PICA register
  186. void SyncBlendFuncs();
  187. /// Syncs the blend color to match the PICA register
  188. void SyncBlendColor();
  189. /// Syncs the alpha test states to match the PICA register
  190. void SyncAlphaTest();
  191. /// Syncs the logic op states to match the PICA register
  192. void SyncLogicOp();
  193. /// Syncs the stencil test states to match the PICA register
  194. void SyncStencilTest();
  195. /// Syncs the depth test states to match the PICA register
  196. void SyncDepthTest();
  197. /// Syncs the TEV constant color to match the PICA register
  198. void SyncTevConstColor(int tev_index, const Pica::Regs::TevStageConfig& tev_stage);
  199. /// Syncs the TEV combiner color buffer to match the PICA register
  200. void SyncCombinerColor();
  201. /// Syncs the remaining OpenGL drawing state to match the current PICA state
  202. void SyncDrawState();
  203. /// Copies the 3DS color framebuffer into the OpenGL color framebuffer texture
  204. void ReloadColorBuffer();
  205. /// Copies the 3DS depth framebuffer into the OpenGL depth framebuffer texture
  206. void ReloadDepthBuffer();
  207. /**
  208. * Save the current OpenGL color framebuffer to the current PICA framebuffer in 3DS memory
  209. * Loads the OpenGL framebuffer textures into temporary buffers
  210. * Then copies into the 3DS framebuffer using proper Morton order
  211. */
  212. void CommitColorBuffer();
  213. /**
  214. * Save the current OpenGL depth framebuffer to the current PICA framebuffer in 3DS memory
  215. * Loads the OpenGL framebuffer textures into temporary buffers
  216. * Then copies into the 3DS framebuffer using proper Morton order
  217. */
  218. void CommitDepthBuffer();
  219. RasterizerCacheOpenGL res_cache;
  220. std::vector<HardwareVertex> vertex_batch;
  221. OpenGLState state;
  222. PAddr last_fb_color_addr;
  223. PAddr last_fb_depth_addr;
  224. // Hardware rasterizer
  225. std::array<SamplerInfo, 3> texture_samplers;
  226. TextureInfo fb_color_texture;
  227. DepthTextureInfo fb_depth_texture;
  228. std::unordered_map<PicaShaderConfig, std::unique_ptr<PicaShader>> shader_cache;
  229. const PicaShader* current_shader = nullptr;
  230. struct {
  231. UniformData data;
  232. bool dirty;
  233. } uniform_block_data;
  234. OGLVertexArray vertex_array;
  235. OGLBuffer vertex_buffer;
  236. OGLBuffer uniform_buffer;
  237. OGLFramebuffer framebuffer;
  238. };