gl_rasterizer.h 18 KB

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