gl_rasterizer.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 "core/hw/gpu.h"
  17. #include "video_core/pica.h"
  18. #include "video_core/pica_state.h"
  19. #include "video_core/pica_types.h"
  20. #include "video_core/rasterizer_interface.h"
  21. #include "video_core/renderer_opengl/gl_rasterizer_cache.h"
  22. #include "video_core/renderer_opengl/gl_resource_manager.h"
  23. #include "video_core/renderer_opengl/gl_state.h"
  24. #include "video_core/renderer_opengl/pica_to_gl.h"
  25. #include "video_core/shader/shader.h"
  26. struct ScreenInfo;
  27. /**
  28. * This struct contains all state used to generate the GLSL shader program that emulates the current
  29. * Pica register configuration. This struct is used as a cache key for generated GLSL shader
  30. * programs. The functions in gl_shader_gen.cpp should retrieve state from this struct only, not by
  31. * directly accessing Pica registers. This should reduce the risk of bugs in shader generation where
  32. * Pica state is not being captured in the shader cache key, thereby resulting in (what should be)
  33. * two separate shaders sharing the same key.
  34. *
  35. * We use a union because "implicitly-defined copy/move constructor for a union X copies the object
  36. * representation of X." and "implicitly-defined copy assignment operator for a union X copies the
  37. * object representation (3.9) of X." = Bytewise copy instead of memberwise copy. This is important
  38. * because the padding bytes are included in the hash and comparison between objects.
  39. */
  40. union PicaShaderConfig {
  41. /// Construct a PicaShaderConfig with the current Pica register configuration.
  42. static PicaShaderConfig CurrentConfig() {
  43. PicaShaderConfig res;
  44. auto& state = res.state;
  45. std::memset(&state, 0, sizeof(PicaShaderConfig::State));
  46. const auto& regs = Pica::g_state.regs;
  47. state.scissor_test_mode = regs.rasterizer.scissor_test.mode;
  48. state.depthmap_enable = regs.rasterizer.depthmap_enable;
  49. state.alpha_test_func = regs.output_merger.alpha_test.enable
  50. ? regs.output_merger.alpha_test.func.Value()
  51. : Pica::Regs::CompareFunc::Always;
  52. state.texture0_type = regs.texturing.texture0.type;
  53. // Copy relevant tev stages fields.
  54. // We don't sync const_color here because of the high variance, it is a
  55. // shader uniform instead.
  56. const auto& tev_stages = regs.texturing.GetTevStages();
  57. DEBUG_ASSERT(state.tev_stages.size() == tev_stages.size());
  58. for (size_t i = 0; i < tev_stages.size(); i++) {
  59. const auto& tev_stage = tev_stages[i];
  60. state.tev_stages[i].sources_raw = tev_stage.sources_raw;
  61. state.tev_stages[i].modifiers_raw = tev_stage.modifiers_raw;
  62. state.tev_stages[i].ops_raw = tev_stage.ops_raw;
  63. state.tev_stages[i].scales_raw = tev_stage.scales_raw;
  64. }
  65. state.fog_mode = regs.texturing.fog_mode;
  66. state.fog_flip = regs.texturing.fog_flip != 0;
  67. state.combiner_buffer_input =
  68. regs.texturing.tev_combiner_buffer_input.update_mask_rgb.Value() |
  69. regs.texturing.tev_combiner_buffer_input.update_mask_a.Value() << 4;
  70. // Fragment lighting
  71. state.lighting.enable = !regs.lighting.disable;
  72. state.lighting.src_num = regs.lighting.max_light_index + 1;
  73. for (unsigned light_index = 0; light_index < state.lighting.src_num; ++light_index) {
  74. unsigned num = regs.lighting.light_enable.GetNum(light_index);
  75. const auto& light = regs.lighting.light[num];
  76. state.lighting.light[light_index].num = num;
  77. state.lighting.light[light_index].directional = light.config.directional != 0;
  78. state.lighting.light[light_index].two_sided_diffuse =
  79. light.config.two_sided_diffuse != 0;
  80. state.lighting.light[light_index].dist_atten_enable =
  81. !regs.lighting.IsDistAttenDisabled(num);
  82. }
  83. state.lighting.lut_d0.enable = regs.lighting.config1.disable_lut_d0 == 0;
  84. state.lighting.lut_d0.abs_input = regs.lighting.abs_lut_input.disable_d0 == 0;
  85. state.lighting.lut_d0.type = regs.lighting.lut_input.d0.Value();
  86. state.lighting.lut_d0.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d0);
  87. state.lighting.lut_d1.enable = regs.lighting.config1.disable_lut_d1 == 0;
  88. state.lighting.lut_d1.abs_input = regs.lighting.abs_lut_input.disable_d1 == 0;
  89. state.lighting.lut_d1.type = regs.lighting.lut_input.d1.Value();
  90. state.lighting.lut_d1.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d1);
  91. state.lighting.lut_fr.enable = regs.lighting.config1.disable_lut_fr == 0;
  92. state.lighting.lut_fr.abs_input = regs.lighting.abs_lut_input.disable_fr == 0;
  93. state.lighting.lut_fr.type = regs.lighting.lut_input.fr.Value();
  94. state.lighting.lut_fr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.fr);
  95. state.lighting.lut_rr.enable = regs.lighting.config1.disable_lut_rr == 0;
  96. state.lighting.lut_rr.abs_input = regs.lighting.abs_lut_input.disable_rr == 0;
  97. state.lighting.lut_rr.type = regs.lighting.lut_input.rr.Value();
  98. state.lighting.lut_rr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rr);
  99. state.lighting.lut_rg.enable = regs.lighting.config1.disable_lut_rg == 0;
  100. state.lighting.lut_rg.abs_input = regs.lighting.abs_lut_input.disable_rg == 0;
  101. state.lighting.lut_rg.type = regs.lighting.lut_input.rg.Value();
  102. state.lighting.lut_rg.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rg);
  103. state.lighting.lut_rb.enable = regs.lighting.config1.disable_lut_rb == 0;
  104. state.lighting.lut_rb.abs_input = regs.lighting.abs_lut_input.disable_rb == 0;
  105. state.lighting.lut_rb.type = regs.lighting.lut_input.rb.Value();
  106. state.lighting.lut_rb.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rb);
  107. state.lighting.config = regs.lighting.config0.config;
  108. state.lighting.fresnel_selector = regs.lighting.config0.fresnel_selector;
  109. state.lighting.bump_mode = regs.lighting.config0.bump_mode;
  110. state.lighting.bump_selector = regs.lighting.config0.bump_selector;
  111. state.lighting.bump_renorm = regs.lighting.config0.disable_bump_renorm == 0;
  112. state.lighting.clamp_highlights = regs.lighting.config0.clamp_highlights != 0;
  113. return res;
  114. }
  115. bool TevStageUpdatesCombinerBufferColor(unsigned stage_index) const {
  116. return (stage_index < 4) && (state.combiner_buffer_input & (1 << stage_index));
  117. }
  118. bool TevStageUpdatesCombinerBufferAlpha(unsigned stage_index) const {
  119. return (stage_index < 4) && ((state.combiner_buffer_input >> 4) & (1 << stage_index));
  120. }
  121. bool operator==(const PicaShaderConfig& o) const {
  122. return std::memcmp(&state, &o.state, sizeof(PicaShaderConfig::State)) == 0;
  123. };
  124. // NOTE: MSVC15 (Update 2) doesn't think `delete`'d constructors and operators are TC.
  125. // This makes BitField not TC when used in a union or struct so we have to resort
  126. // to this ugly hack.
  127. // Once that bug is fixed we can use Pica::Regs::TevStageConfig here.
  128. // Doesn't include const_color because we don't sync it, see comment in CurrentConfig()
  129. struct TevStageConfigRaw {
  130. u32 sources_raw;
  131. u32 modifiers_raw;
  132. u32 ops_raw;
  133. u32 scales_raw;
  134. explicit operator Pica::TexturingRegs::TevStageConfig() const noexcept {
  135. Pica::TexturingRegs::TevStageConfig stage;
  136. stage.sources_raw = sources_raw;
  137. stage.modifiers_raw = modifiers_raw;
  138. stage.ops_raw = ops_raw;
  139. stage.const_color = 0;
  140. stage.scales_raw = scales_raw;
  141. return stage;
  142. }
  143. };
  144. struct State {
  145. Pica::Regs::CompareFunc alpha_test_func;
  146. Pica::RasterizerRegs::ScissorMode scissor_test_mode;
  147. Pica::TexturingRegs::TextureConfig::TextureType texture0_type;
  148. std::array<TevStageConfigRaw, 6> tev_stages;
  149. u8 combiner_buffer_input;
  150. Pica::RasterizerRegs::DepthBuffering depthmap_enable;
  151. Pica::TexturingRegs::FogMode fog_mode;
  152. bool fog_flip;
  153. struct {
  154. struct {
  155. unsigned num;
  156. bool directional;
  157. bool two_sided_diffuse;
  158. bool dist_atten_enable;
  159. } light[8];
  160. bool enable;
  161. unsigned src_num;
  162. Pica::Regs::LightingBumpMode bump_mode;
  163. unsigned bump_selector;
  164. bool bump_renorm;
  165. bool clamp_highlights;
  166. Pica::Regs::LightingConfig config;
  167. Pica::Regs::LightingFresnelSelector fresnel_selector;
  168. struct {
  169. bool enable;
  170. bool abs_input;
  171. Pica::Regs::LightingLutInput type;
  172. float scale;
  173. } lut_d0, lut_d1, lut_fr, lut_rr, lut_rg, lut_rb;
  174. } lighting;
  175. } state;
  176. };
  177. #if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER)
  178. static_assert(std::is_trivially_copyable<PicaShaderConfig::State>::value,
  179. "PicaShaderConfig::State must be trivially copyable");
  180. #endif
  181. namespace std {
  182. template <>
  183. struct hash<PicaShaderConfig> {
  184. size_t operator()(const PicaShaderConfig& k) const {
  185. return Common::ComputeHash64(&k.state, sizeof(PicaShaderConfig::State));
  186. }
  187. };
  188. } // namespace std
  189. class RasterizerOpenGL : public VideoCore::RasterizerInterface {
  190. public:
  191. RasterizerOpenGL();
  192. ~RasterizerOpenGL() override;
  193. void AddTriangle(const Pica::Shader::OutputVertex& v0, const Pica::Shader::OutputVertex& v1,
  194. const Pica::Shader::OutputVertex& v2) override;
  195. void DrawTriangles() override;
  196. void NotifyPicaRegisterChanged(u32 id) override;
  197. void FlushAll() override;
  198. void FlushRegion(PAddr addr, u32 size) override;
  199. void FlushAndInvalidateRegion(PAddr addr, u32 size) override;
  200. bool AccelerateDisplayTransfer(const GPU::Regs::DisplayTransferConfig& config) override;
  201. bool AccelerateTextureCopy(const GPU::Regs::DisplayTransferConfig& config) override;
  202. bool AccelerateFill(const GPU::Regs::MemoryFillConfig& config) override;
  203. bool AccelerateDisplay(const GPU::Regs::FramebufferConfig& config, PAddr framebuffer_addr,
  204. u32 pixel_stride, ScreenInfo& screen_info) override;
  205. /// OpenGL shader generated for a given Pica register state
  206. struct PicaShader {
  207. /// OpenGL shader resource
  208. OGLShader shader;
  209. };
  210. private:
  211. struct SamplerInfo {
  212. using TextureConfig = Pica::TexturingRegs::TextureConfig;
  213. OGLSampler sampler;
  214. /// Creates the sampler object, initializing its state so that it's in sync with the
  215. /// SamplerInfo struct.
  216. void Create();
  217. /// Syncs the sampler object with the config, updating any necessary state.
  218. void SyncWithConfig(const TextureConfig& config);
  219. private:
  220. TextureConfig::TextureFilter mag_filter;
  221. TextureConfig::TextureFilter min_filter;
  222. TextureConfig::WrapMode wrap_s;
  223. TextureConfig::WrapMode wrap_t;
  224. u32 border_color;
  225. };
  226. /// Structure that the hardware rendered vertices are composed of
  227. struct HardwareVertex {
  228. HardwareVertex(const Pica::Shader::OutputVertex& v, bool flip_quaternion) {
  229. position[0] = v.pos.x.ToFloat32();
  230. position[1] = v.pos.y.ToFloat32();
  231. position[2] = v.pos.z.ToFloat32();
  232. position[3] = v.pos.w.ToFloat32();
  233. color[0] = v.color.x.ToFloat32();
  234. color[1] = v.color.y.ToFloat32();
  235. color[2] = v.color.z.ToFloat32();
  236. color[3] = v.color.w.ToFloat32();
  237. tex_coord0[0] = v.tc0.x.ToFloat32();
  238. tex_coord0[1] = v.tc0.y.ToFloat32();
  239. tex_coord1[0] = v.tc1.x.ToFloat32();
  240. tex_coord1[1] = v.tc1.y.ToFloat32();
  241. tex_coord2[0] = v.tc2.x.ToFloat32();
  242. tex_coord2[1] = v.tc2.y.ToFloat32();
  243. tex_coord0_w = v.tc0_w.ToFloat32();
  244. normquat[0] = v.quat.x.ToFloat32();
  245. normquat[1] = v.quat.y.ToFloat32();
  246. normquat[2] = v.quat.z.ToFloat32();
  247. normquat[3] = v.quat.w.ToFloat32();
  248. view[0] = v.view.x.ToFloat32();
  249. view[1] = v.view.y.ToFloat32();
  250. view[2] = v.view.z.ToFloat32();
  251. if (flip_quaternion) {
  252. for (float& x : normquat) {
  253. x = -x;
  254. }
  255. }
  256. }
  257. GLfloat position[4];
  258. GLfloat color[4];
  259. GLfloat tex_coord0[2];
  260. GLfloat tex_coord1[2];
  261. GLfloat tex_coord2[2];
  262. GLfloat tex_coord0_w;
  263. GLfloat normquat[4];
  264. GLfloat view[3];
  265. };
  266. struct LightSrc {
  267. alignas(16) GLvec3 specular_0;
  268. alignas(16) GLvec3 specular_1;
  269. alignas(16) GLvec3 diffuse;
  270. alignas(16) GLvec3 ambient;
  271. alignas(16) GLvec3 position;
  272. GLfloat dist_atten_bias;
  273. GLfloat dist_atten_scale;
  274. };
  275. /// Uniform structure for the Uniform Buffer Object, all vectors must be 16-byte aligned
  276. // NOTE: Always keep a vec4 at the end. The GL spec is not clear wether the alignment at
  277. // the end of a uniform block is included in UNIFORM_BLOCK_DATA_SIZE or not.
  278. // Not following that rule will cause problems on some AMD drivers.
  279. struct UniformData {
  280. alignas(8) GLvec2 framebuffer_scale;
  281. GLint alphatest_ref;
  282. GLfloat depth_scale;
  283. GLfloat depth_offset;
  284. GLint scissor_x1;
  285. GLint scissor_y1;
  286. GLint scissor_x2;
  287. GLint scissor_y2;
  288. alignas(16) GLvec3 fog_color;
  289. alignas(16) GLvec3 lighting_global_ambient;
  290. LightSrc light_src[8];
  291. alignas(16) GLvec4 const_color[6]; // A vec4 color for each of the six tev stages
  292. alignas(16) GLvec4 tev_combiner_buffer_color;
  293. };
  294. static_assert(
  295. sizeof(UniformData) == 0x3C0,
  296. "The size of the UniformData structure has changed, update the structure in the shader");
  297. static_assert(sizeof(UniformData) < 16384,
  298. "UniformData structure must be less than 16kb as per the OpenGL spec");
  299. /// Sets the OpenGL shader in accordance with the current PICA register state
  300. void SetShader();
  301. /// Syncs the cull mode to match the PICA register
  302. void SyncCullMode();
  303. /// Syncs the depth scale to match the PICA register
  304. void SyncDepthScale();
  305. /// Syncs the depth offset to match the PICA register
  306. void SyncDepthOffset();
  307. /// Syncs the blend enabled status to match the PICA register
  308. void SyncBlendEnabled();
  309. /// Syncs the blend functions to match the PICA register
  310. void SyncBlendFuncs();
  311. /// Syncs the blend color to match the PICA register
  312. void SyncBlendColor();
  313. /// Syncs the fog states to match the PICA register
  314. void SyncFogColor();
  315. void SyncFogLUT();
  316. /// Syncs the alpha test states to match the PICA register
  317. void SyncAlphaTest();
  318. /// Syncs the logic op states to match the PICA register
  319. void SyncLogicOp();
  320. /// Syncs the color write mask to match the PICA register state
  321. void SyncColorWriteMask();
  322. /// Syncs the stencil write mask to match the PICA register state
  323. void SyncStencilWriteMask();
  324. /// Syncs the depth write mask to match the PICA register state
  325. void SyncDepthWriteMask();
  326. /// Syncs the stencil test states to match the PICA register
  327. void SyncStencilTest();
  328. /// Syncs the depth test states to match the PICA register
  329. void SyncDepthTest();
  330. /// Syncs the TEV combiner color buffer to match the PICA register
  331. void SyncCombinerColor();
  332. /// Syncs the TEV constant color to match the PICA register
  333. void SyncTevConstColor(int tev_index, const Pica::TexturingRegs::TevStageConfig& tev_stage);
  334. /// Syncs the lighting global ambient color to match the PICA register
  335. void SyncGlobalAmbient();
  336. /// Syncs the lighting lookup tables
  337. void SyncLightingLUT(unsigned index);
  338. /// Syncs the specified light's specular 0 color to match the PICA register
  339. void SyncLightSpecular0(int light_index);
  340. /// Syncs the specified light's specular 1 color to match the PICA register
  341. void SyncLightSpecular1(int light_index);
  342. /// Syncs the specified light's diffuse color to match the PICA register
  343. void SyncLightDiffuse(int light_index);
  344. /// Syncs the specified light's ambient color to match the PICA register
  345. void SyncLightAmbient(int light_index);
  346. /// Syncs the specified light's position to match the PICA register
  347. void SyncLightPosition(int light_index);
  348. /// Syncs the specified light's distance attenuation bias to match the PICA register
  349. void SyncLightDistanceAttenuationBias(int light_index);
  350. /// Syncs the specified light's distance attenuation scale to match the PICA register
  351. void SyncLightDistanceAttenuationScale(int light_index);
  352. OpenGLState state;
  353. RasterizerCacheOpenGL res_cache;
  354. std::vector<HardwareVertex> vertex_batch;
  355. std::unordered_map<PicaShaderConfig, std::unique_ptr<PicaShader>> shader_cache;
  356. const PicaShader* current_shader = nullptr;
  357. bool shader_dirty;
  358. struct {
  359. UniformData data;
  360. bool lut_dirty[6];
  361. bool fog_lut_dirty;
  362. bool dirty;
  363. } uniform_block_data = {};
  364. std::array<SamplerInfo, 3> texture_samplers;
  365. OGLVertexArray vertex_array;
  366. OGLBuffer vertex_buffer;
  367. OGLBuffer uniform_buffer;
  368. OGLFramebuffer framebuffer;
  369. std::array<OGLTexture, 6> lighting_luts;
  370. std::array<std::array<GLvec4, 256>, 6> lighting_lut_data{};
  371. OGLTexture fog_lut;
  372. std::array<GLuint, 128> fog_lut_data{};
  373. };