gl_rasterizer.h 17 KB

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