gl_rasterizer.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. // Fragment lighting
  62. res.lighting.enable = !regs.lighting.disable;
  63. res.lighting.src_num = regs.lighting.src_num + 1;
  64. for (unsigned light_index = 0; light_index < res.lighting.src_num; ++light_index) {
  65. unsigned num = regs.lighting.light_enable.GetNum(light_index);
  66. const auto& light = regs.lighting.light[num];
  67. res.lighting.light[light_index].num = num;
  68. res.lighting.light[light_index].directional = light.directional != 0;
  69. res.lighting.light[light_index].two_sided_diffuse = light.two_sided_diffuse != 0;
  70. res.lighting.light[light_index].dist_atten_enable = regs.lighting.IsDistAttenEnabled(num);
  71. res.lighting.light[light_index].dist_atten_bias = Pica::float20::FromRawFloat20(light.dist_atten_bias).ToFloat32();
  72. res.lighting.light[light_index].dist_atten_scale = Pica::float20::FromRawFloat20(light.dist_atten_scale).ToFloat32();
  73. }
  74. res.lighting.lut_d0.enable = regs.lighting.lut_enable_d0 == 0;
  75. res.lighting.lut_d0.abs_input = regs.lighting.abs_lut_input.d0 == 0;
  76. res.lighting.lut_d0.type = regs.lighting.lut_input.d0.Value();
  77. res.lighting.lut_d0.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d0);
  78. res.lighting.lut_d1.enable = regs.lighting.lut_enable_d1 == 0;
  79. res.lighting.lut_d1.abs_input = regs.lighting.abs_lut_input.d1 == 0;
  80. res.lighting.lut_d1.type = regs.lighting.lut_input.d1.Value();
  81. res.lighting.lut_d1.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d1);
  82. res.lighting.lut_fr.enable = regs.lighting.lut_enable_fr == 0;
  83. res.lighting.lut_fr.abs_input = regs.lighting.abs_lut_input.fr == 0;
  84. res.lighting.lut_fr.type = regs.lighting.lut_input.fr.Value();
  85. res.lighting.lut_fr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.fr);
  86. res.lighting.lut_rr.enable = regs.lighting.lut_enable_rr == 0;
  87. res.lighting.lut_rr.abs_input = regs.lighting.abs_lut_input.rr == 0;
  88. res.lighting.lut_rr.type = regs.lighting.lut_input.rr.Value();
  89. res.lighting.lut_rr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rr);
  90. res.lighting.lut_rg.enable = regs.lighting.lut_enable_rg == 0;
  91. res.lighting.lut_rg.abs_input = regs.lighting.abs_lut_input.rg == 0;
  92. res.lighting.lut_rg.type = regs.lighting.lut_input.rg.Value();
  93. res.lighting.lut_rg.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rg);
  94. res.lighting.lut_rb.enable = regs.lighting.lut_enable_rb == 0;
  95. res.lighting.lut_rb.abs_input = regs.lighting.abs_lut_input.rb == 0;
  96. res.lighting.lut_rb.type = regs.lighting.lut_input.rb.Value();
  97. res.lighting.lut_rb.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rb);
  98. res.lighting.config = regs.lighting.config;
  99. res.lighting.fresnel_selector = regs.lighting.fresnel_selector;
  100. res.lighting.bump_mode = regs.lighting.bump_mode;
  101. res.lighting.bump_selector = regs.lighting.bump_selector;
  102. res.lighting.bump_renorm = regs.lighting.bump_renorm == 0;
  103. res.lighting.clamp_highlights = regs.lighting.clamp_highlights != 0;
  104. return res;
  105. }
  106. bool TevStageUpdatesCombinerBufferColor(unsigned stage_index) const {
  107. return (stage_index < 4) && (combiner_buffer_input & (1 << stage_index));
  108. }
  109. bool TevStageUpdatesCombinerBufferAlpha(unsigned stage_index) const {
  110. return (stage_index < 4) && ((combiner_buffer_input >> 4) & (1 << stage_index));
  111. }
  112. bool operator ==(const PicaShaderConfig& o) const {
  113. return std::memcmp(this, &o, sizeof(PicaShaderConfig)) == 0;
  114. };
  115. Pica::Regs::CompareFunc alpha_test_func = Pica::Regs::CompareFunc::Never;
  116. std::array<Pica::Regs::TevStageConfig, 6> tev_stages = {};
  117. u8 combiner_buffer_input = 0;
  118. struct {
  119. struct {
  120. unsigned num = 0;
  121. bool directional = false;
  122. bool two_sided_diffuse = false;
  123. bool dist_atten_enable = false;
  124. GLfloat dist_atten_scale = 0.0f;
  125. GLfloat dist_atten_bias = 0.0f;
  126. } light[8];
  127. bool enable = false;
  128. unsigned src_num = 0;
  129. Pica::Regs::LightingBumpMode bump_mode = Pica::Regs::LightingBumpMode::None;
  130. unsigned bump_selector = 0;
  131. bool bump_renorm = false;
  132. bool clamp_highlights = false;
  133. Pica::Regs::LightingConfig config = Pica::Regs::LightingConfig::Config0;
  134. Pica::Regs::LightingFresnelSelector fresnel_selector = Pica::Regs::LightingFresnelSelector::None;
  135. struct {
  136. bool enable = false;
  137. bool abs_input = false;
  138. Pica::Regs::LightingLutInput type = Pica::Regs::LightingLutInput::NH;
  139. float scale = 1.0f;
  140. } lut_d0, lut_d1, lut_fr, lut_rr, lut_rg, lut_rb;
  141. } lighting;
  142. };
  143. namespace std {
  144. template <>
  145. struct hash<PicaShaderConfig> {
  146. size_t operator()(const PicaShaderConfig& k) const {
  147. return Common::ComputeHash64(&k, sizeof(PicaShaderConfig));
  148. }
  149. };
  150. } // namespace std
  151. class RasterizerOpenGL : public VideoCore::RasterizerInterface {
  152. public:
  153. RasterizerOpenGL();
  154. ~RasterizerOpenGL() override;
  155. void InitObjects() override;
  156. void Reset() override;
  157. void AddTriangle(const Pica::Shader::OutputVertex& v0,
  158. const Pica::Shader::OutputVertex& v1,
  159. const Pica::Shader::OutputVertex& v2) override;
  160. void DrawTriangles() override;
  161. void FlushFramebuffer() override;
  162. void NotifyPicaRegisterChanged(u32 id) override;
  163. void FlushRegion(PAddr addr, u32 size) override;
  164. void InvalidateRegion(PAddr addr, u32 size) override;
  165. /// OpenGL shader generated for a given Pica register state
  166. struct PicaShader {
  167. /// OpenGL shader resource
  168. OGLShader shader;
  169. };
  170. private:
  171. /// Structure used for storing information about color textures
  172. struct TextureInfo {
  173. OGLTexture texture;
  174. GLsizei width;
  175. GLsizei height;
  176. Pica::Regs::ColorFormat format;
  177. GLenum gl_format;
  178. GLenum gl_type;
  179. };
  180. /// Structure used for storing information about depth textures
  181. struct DepthTextureInfo {
  182. OGLTexture texture;
  183. GLsizei width;
  184. GLsizei height;
  185. Pica::Regs::DepthFormat format;
  186. GLenum gl_format;
  187. GLenum gl_type;
  188. };
  189. struct SamplerInfo {
  190. using TextureConfig = Pica::Regs::TextureConfig;
  191. OGLSampler sampler;
  192. /// Creates the sampler object, initializing its state so that it's in sync with the SamplerInfo struct.
  193. void Create();
  194. /// Syncs the sampler object with the config, updating any necessary state.
  195. void SyncWithConfig(const TextureConfig& config);
  196. private:
  197. TextureConfig::TextureFilter mag_filter;
  198. TextureConfig::TextureFilter min_filter;
  199. TextureConfig::WrapMode wrap_s;
  200. TextureConfig::WrapMode wrap_t;
  201. u32 border_color;
  202. };
  203. /// Structure that the hardware rendered vertices are composed of
  204. struct HardwareVertex {
  205. HardwareVertex(const Pica::Shader::OutputVertex& v) {
  206. position[0] = v.pos.x.ToFloat32();
  207. position[1] = v.pos.y.ToFloat32();
  208. position[2] = v.pos.z.ToFloat32();
  209. position[3] = v.pos.w.ToFloat32();
  210. color[0] = v.color.x.ToFloat32();
  211. color[1] = v.color.y.ToFloat32();
  212. color[2] = v.color.z.ToFloat32();
  213. color[3] = v.color.w.ToFloat32();
  214. tex_coord0[0] = v.tc0.x.ToFloat32();
  215. tex_coord0[1] = v.tc0.y.ToFloat32();
  216. tex_coord1[0] = v.tc1.x.ToFloat32();
  217. tex_coord1[1] = v.tc1.y.ToFloat32();
  218. tex_coord2[0] = v.tc2.x.ToFloat32();
  219. tex_coord2[1] = v.tc2.y.ToFloat32();
  220. normquat[0] = v.quat.x.ToFloat32();
  221. normquat[1] = v.quat.y.ToFloat32();
  222. normquat[2] = v.quat.z.ToFloat32();
  223. normquat[3] = v.quat.w.ToFloat32();
  224. view[0] = v.view.x.ToFloat32();
  225. view[1] = v.view.y.ToFloat32();
  226. view[2] = v.view.z.ToFloat32();
  227. }
  228. GLfloat position[4];
  229. GLfloat color[4];
  230. GLfloat tex_coord0[2];
  231. GLfloat tex_coord1[2];
  232. GLfloat tex_coord2[2];
  233. GLfloat normquat[4];
  234. GLfloat view[3];
  235. };
  236. struct LightSrc {
  237. std::array<GLfloat, 3> specular_0;
  238. INSERT_PADDING_WORDS(1);
  239. std::array<GLfloat, 3> specular_1;
  240. INSERT_PADDING_WORDS(1);
  241. std::array<GLfloat, 3> diffuse;
  242. INSERT_PADDING_WORDS(1);
  243. std::array<GLfloat, 3> ambient;
  244. INSERT_PADDING_WORDS(1);
  245. std::array<GLfloat, 3> position;
  246. INSERT_PADDING_WORDS(1);
  247. };
  248. /// Uniform structure for the Uniform Buffer Object, all members must be 16-byte aligned
  249. struct UniformData {
  250. // A vec4 color for each of the six tev stages
  251. std::array<GLfloat, 4> const_color[6];
  252. std::array<GLfloat, 4> tev_combiner_buffer_color;
  253. GLint alphatest_ref;
  254. GLfloat depth_offset;
  255. INSERT_PADDING_WORDS(2);
  256. std::array<GLfloat, 3> lighting_global_ambient;
  257. INSERT_PADDING_WORDS(1);
  258. LightSrc light_src[8];
  259. };
  260. static_assert(sizeof(UniformData) == 0x310, "The size of the UniformData structure has changed, update the structure in the shader");
  261. static_assert(sizeof(UniformData) < 16384, "UniformData structure must be less than 16kb as per the OpenGL spec");
  262. /// Reconfigure the OpenGL color texture to use the given format and dimensions
  263. void ReconfigureColorTexture(TextureInfo& texture, Pica::Regs::ColorFormat format, u32 width, u32 height);
  264. /// Reconfigure the OpenGL depth texture to use the given format and dimensions
  265. void ReconfigureDepthTexture(DepthTextureInfo& texture, Pica::Regs::DepthFormat format, u32 width, u32 height);
  266. /// Sets the OpenGL shader in accordance with the current PICA register state
  267. void SetShader();
  268. /// Syncs the state and contents of the OpenGL framebuffer to match the current PICA framebuffer
  269. void SyncFramebuffer();
  270. /// Syncs the cull mode to match the PICA register
  271. void SyncCullMode();
  272. /// Syncs the depth scale and offset to match the PICA registers
  273. void SyncDepthModifiers();
  274. /// Syncs the blend enabled status to match the PICA register
  275. void SyncBlendEnabled();
  276. /// Syncs the blend functions to match the PICA register
  277. void SyncBlendFuncs();
  278. /// Syncs the blend color to match the PICA register
  279. void SyncBlendColor();
  280. /// Syncs the alpha test states to match the PICA register
  281. void SyncAlphaTest();
  282. /// Syncs the logic op states to match the PICA register
  283. void SyncLogicOp();
  284. /// Syncs the stencil test states to match the PICA register
  285. void SyncStencilTest();
  286. /// Syncs the depth test states to match the PICA register
  287. void SyncDepthTest();
  288. /// Syncs the TEV constant color to match the PICA register
  289. void SyncTevConstColor(int tev_index, const Pica::Regs::TevStageConfig& tev_stage);
  290. /// Syncs the TEV combiner color buffer to match the PICA register
  291. void SyncCombinerColor();
  292. /// Syncs the lighting global ambient color to match the PICA register
  293. void SyncGlobalAmbient();
  294. /// Syncs the lighting lookup tables
  295. void SyncLightingLUT(unsigned index);
  296. /// Syncs the specified light's diffuse color to match the PICA register
  297. void SyncLightDiffuse(int light_index);
  298. /// Syncs the specified light's ambient color to match the PICA register
  299. void SyncLightAmbient(int light_index);
  300. /// Syncs the specified light's position to match the PICA register
  301. void SyncLightPosition(int light_index);
  302. /// Syncs the specified light's specular 0 color to match the PICA register
  303. void SyncLightSpecular0(int light_index);
  304. /// Syncs the specified light's specular 1 color to match the PICA register
  305. void SyncLightSpecular1(int light_index);
  306. /// Syncs the remaining OpenGL drawing state to match the current PICA state
  307. void SyncDrawState();
  308. /// Copies the 3DS color framebuffer into the OpenGL color framebuffer texture
  309. void ReloadColorBuffer();
  310. /// Copies the 3DS depth framebuffer into the OpenGL depth framebuffer texture
  311. void ReloadDepthBuffer();
  312. /**
  313. * Save the current OpenGL color framebuffer to the current PICA framebuffer in 3DS memory
  314. * Loads the OpenGL framebuffer textures into temporary buffers
  315. * Then copies into the 3DS framebuffer using proper Morton order
  316. */
  317. void CommitColorBuffer();
  318. /**
  319. * Save the current OpenGL depth framebuffer to the current PICA framebuffer in 3DS memory
  320. * Loads the OpenGL framebuffer textures into temporary buffers
  321. * Then copies into the 3DS framebuffer using proper Morton order
  322. */
  323. void CommitDepthBuffer();
  324. RasterizerCacheOpenGL res_cache;
  325. std::vector<HardwareVertex> vertex_batch;
  326. OpenGLState state;
  327. PAddr cached_fb_color_addr;
  328. PAddr cached_fb_depth_addr;
  329. // Hardware rasterizer
  330. std::array<SamplerInfo, 3> texture_samplers;
  331. TextureInfo fb_color_texture;
  332. DepthTextureInfo fb_depth_texture;
  333. std::unordered_map<PicaShaderConfig, std::unique_ptr<PicaShader>> shader_cache;
  334. const PicaShader* current_shader = nullptr;
  335. struct {
  336. UniformData data;
  337. bool lut_dirty[6];
  338. bool dirty;
  339. } uniform_block_data;
  340. OGLVertexArray vertex_array;
  341. OGLBuffer vertex_buffer;
  342. OGLBuffer uniform_buffer;
  343. OGLFramebuffer framebuffer;
  344. std::array<OGLTexture, 6> lighting_lut;
  345. std::array<std::array<std::array<GLfloat, 4>, 256>, 6> lighting_lut_data;
  346. };