gl_rasterizer.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include <memory>
  6. #include <glad/glad.h>
  7. #include "common/color.h"
  8. #include "common/file_util.h"
  9. #include "common/math_util.h"
  10. #include "common/microprofile.h"
  11. #include "core/memory.h"
  12. #include "core/settings.h"
  13. #include "core/hw/gpu.h"
  14. #include "video_core/pica.h"
  15. #include "video_core/pica_state.h"
  16. #include "video_core/utils.h"
  17. #include "video_core/renderer_opengl/gl_rasterizer.h"
  18. #include "video_core/renderer_opengl/gl_shader_gen.h"
  19. #include "video_core/renderer_opengl/gl_shader_util.h"
  20. #include "video_core/renderer_opengl/pica_to_gl.h"
  21. static bool IsPassThroughTevStage(const Pica::Regs::TevStageConfig& stage) {
  22. return (stage.color_op == Pica::Regs::TevStageConfig::Operation::Replace &&
  23. stage.alpha_op == Pica::Regs::TevStageConfig::Operation::Replace &&
  24. stage.color_source1 == Pica::Regs::TevStageConfig::Source::Previous &&
  25. stage.alpha_source1 == Pica::Regs::TevStageConfig::Source::Previous &&
  26. stage.color_modifier1 == Pica::Regs::TevStageConfig::ColorModifier::SourceColor &&
  27. stage.alpha_modifier1 == Pica::Regs::TevStageConfig::AlphaModifier::SourceAlpha &&
  28. stage.GetColorMultiplier() == 1 &&
  29. stage.GetAlphaMultiplier() == 1);
  30. }
  31. RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
  32. // Create sampler objects
  33. for (size_t i = 0; i < texture_samplers.size(); ++i) {
  34. texture_samplers[i].Create();
  35. state.texture_units[i].sampler = texture_samplers[i].sampler.handle;
  36. }
  37. // Generate VBO, VAO and UBO
  38. vertex_buffer.Create();
  39. vertex_array.Create();
  40. uniform_buffer.Create();
  41. state.draw.vertex_array = vertex_array.handle;
  42. state.draw.vertex_buffer = vertex_buffer.handle;
  43. state.draw.uniform_buffer = uniform_buffer.handle;
  44. state.Apply();
  45. // Bind the UBO to binding point 0
  46. glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer.handle);
  47. uniform_block_data.dirty = true;
  48. for (unsigned index = 0; index < lighting_luts.size(); index++) {
  49. uniform_block_data.lut_dirty[index] = true;
  50. }
  51. // Set vertex attributes
  52. glVertexAttribPointer(GLShader::ATTRIBUTE_POSITION, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, position));
  53. glEnableVertexAttribArray(GLShader::ATTRIBUTE_POSITION);
  54. glVertexAttribPointer(GLShader::ATTRIBUTE_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, color));
  55. glEnableVertexAttribArray(GLShader::ATTRIBUTE_COLOR);
  56. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord0));
  57. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD1, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord1));
  58. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD2, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord2));
  59. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD0);
  60. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD1);
  61. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD2);
  62. glVertexAttribPointer(GLShader::ATTRIBUTE_NORMQUAT, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, normquat));
  63. glEnableVertexAttribArray(GLShader::ATTRIBUTE_NORMQUAT);
  64. glVertexAttribPointer(GLShader::ATTRIBUTE_VIEW, 3, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, view));
  65. glEnableVertexAttribArray(GLShader::ATTRIBUTE_VIEW);
  66. // Create render framebuffer
  67. framebuffer.Create();
  68. // Allocate and bind lighting lut textures
  69. for (size_t i = 0; i < lighting_luts.size(); ++i) {
  70. lighting_luts[i].Create();
  71. state.lighting_luts[i].texture_1d = lighting_luts[i].handle;
  72. }
  73. state.Apply();
  74. for (size_t i = 0; i < lighting_luts.size(); ++i) {
  75. glActiveTexture(GL_TEXTURE3 + i);
  76. glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr);
  77. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  78. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  79. }
  80. // Sync fixed function OpenGL state
  81. SyncCullMode();
  82. SyncDepthModifiers();
  83. SyncBlendEnabled();
  84. SyncBlendFuncs();
  85. SyncBlendColor();
  86. SyncLogicOp();
  87. SyncStencilTest();
  88. SyncDepthTest();
  89. SyncColorWriteMask();
  90. SyncStencilWriteMask();
  91. SyncDepthWriteMask();
  92. }
  93. RasterizerOpenGL::~RasterizerOpenGL() {
  94. }
  95. /**
  96. * This is a helper function to resolve an issue with opposite quaternions being interpolated by
  97. * OpenGL. See below for a detailed description of this issue (yuriks):
  98. *
  99. * For any rotation, there are two quaternions Q, and -Q, that represent the same rotation. If you
  100. * interpolate two quaternions that are opposite, instead of going from one rotation to another
  101. * using the shortest path, you'll go around the longest path. You can test if two quaternions are
  102. * opposite by checking if Dot(Q1, W2) < 0. In that case, you can flip either of them, therefore
  103. * making Dot(-Q1, W2) positive.
  104. *
  105. * NOTE: This solution corrects this issue per-vertex before passing the quaternions to OpenGL. This
  106. * should be correct for nearly all cases, however a more correct implementation (but less trivial
  107. * and perhaps unnecessary) would be to handle this per-fragment, by interpolating the quaternions
  108. * manually using two Lerps, and doing this correction before each Lerp.
  109. */
  110. static bool AreQuaternionsOpposite(Math::Vec4<Pica::float24> qa, Math::Vec4<Pica::float24> qb) {
  111. Math::Vec4f a{ qa.x.ToFloat32(), qa.y.ToFloat32(), qa.z.ToFloat32(), qa.w.ToFloat32() };
  112. Math::Vec4f b{ qb.x.ToFloat32(), qb.y.ToFloat32(), qb.z.ToFloat32(), qb.w.ToFloat32() };
  113. return (Math::Dot(a, b) < 0.f);
  114. }
  115. void RasterizerOpenGL::AddTriangle(const Pica::Shader::OutputVertex& v0,
  116. const Pica::Shader::OutputVertex& v1,
  117. const Pica::Shader::OutputVertex& v2) {
  118. vertex_batch.emplace_back(v0, false);
  119. vertex_batch.emplace_back(v1, AreQuaternionsOpposite(v0.quat, v1.quat));
  120. vertex_batch.emplace_back(v2, AreQuaternionsOpposite(v0.quat, v2.quat));
  121. }
  122. void RasterizerOpenGL::DrawTriangles() {
  123. if (vertex_batch.empty())
  124. return;
  125. const auto& regs = Pica::g_state.regs;
  126. // Sync and bind the framebuffer surfaces
  127. CachedSurface* color_surface;
  128. CachedSurface* depth_surface;
  129. MathUtil::Rectangle<int> rect;
  130. std::tie(color_surface, depth_surface, rect) = res_cache.GetFramebufferSurfaces(regs.framebuffer);
  131. state.draw.draw_framebuffer = framebuffer.handle;
  132. state.Apply();
  133. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color_surface != nullptr ? color_surface->texture.handle : 0, 0);
  134. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_surface != nullptr ? depth_surface->texture.handle : 0, 0);
  135. bool has_stencil = regs.framebuffer.depth_format == Pica::Regs::DepthFormat::D24S8;
  136. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, (has_stencil && depth_surface != nullptr) ? depth_surface->texture.handle : 0, 0);
  137. if (OpenGLState::CheckFBStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  138. return;
  139. }
  140. // Sync the viewport
  141. // These registers hold half-width and half-height, so must be multiplied by 2
  142. GLsizei viewport_width = (GLsizei)Pica::float24::FromRaw(regs.viewport_size_x).ToFloat32() * 2;
  143. GLsizei viewport_height = (GLsizei)Pica::float24::FromRaw(regs.viewport_size_y).ToFloat32() * 2;
  144. glViewport((GLint)(rect.left + regs.viewport_corner.x * color_surface->res_scale_width),
  145. (GLint)(rect.bottom + regs.viewport_corner.y * color_surface->res_scale_height),
  146. (GLsizei)(viewport_width * color_surface->res_scale_width), (GLsizei)(viewport_height * color_surface->res_scale_height));
  147. // Sync and bind the texture surfaces
  148. const auto pica_textures = regs.GetTextures();
  149. for (unsigned texture_index = 0; texture_index < pica_textures.size(); ++texture_index) {
  150. const auto& texture = pica_textures[texture_index];
  151. if (texture.enabled) {
  152. texture_samplers[texture_index].SyncWithConfig(texture.config);
  153. CachedSurface* surface = res_cache.GetTextureSurface(texture);
  154. if (surface != nullptr) {
  155. state.texture_units[texture_index].texture_2d = surface->texture.handle;
  156. } else {
  157. // Can occur when texture addr is null or its memory is unmapped/invalid
  158. state.texture_units[texture_index].texture_2d = 0;
  159. }
  160. } else {
  161. state.texture_units[texture_index].texture_2d = 0;
  162. }
  163. }
  164. // Sync and bind the shader
  165. if (shader_dirty) {
  166. SetShader();
  167. shader_dirty = false;
  168. }
  169. // Sync the lighting luts
  170. for (unsigned index = 0; index < lighting_luts.size(); index++) {
  171. if (uniform_block_data.lut_dirty[index]) {
  172. SyncLightingLUT(index);
  173. uniform_block_data.lut_dirty[index] = false;
  174. }
  175. }
  176. // Sync the uniform data
  177. if (uniform_block_data.dirty) {
  178. glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformData), &uniform_block_data.data, GL_STATIC_DRAW);
  179. uniform_block_data.dirty = false;
  180. }
  181. state.Apply();
  182. // Draw the vertex batch
  183. glBufferData(GL_ARRAY_BUFFER, vertex_batch.size() * sizeof(HardwareVertex), vertex_batch.data(), GL_STREAM_DRAW);
  184. glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertex_batch.size());
  185. // Mark framebuffer surfaces as dirty
  186. // TODO: Restrict invalidation area to the viewport
  187. if (color_surface != nullptr) {
  188. color_surface->dirty = true;
  189. res_cache.FlushRegion(color_surface->addr, color_surface->size, color_surface, true);
  190. }
  191. if (depth_surface != nullptr) {
  192. depth_surface->dirty = true;
  193. res_cache.FlushRegion(depth_surface->addr, depth_surface->size, depth_surface, true);
  194. }
  195. vertex_batch.clear();
  196. // Unbind textures for potential future use as framebuffer attachments
  197. for (unsigned texture_index = 0; texture_index < pica_textures.size(); ++texture_index) {
  198. state.texture_units[texture_index].texture_2d = 0;
  199. }
  200. state.Apply();
  201. }
  202. void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
  203. const auto& regs = Pica::g_state.regs;
  204. switch(id) {
  205. // Culling
  206. case PICA_REG_INDEX(cull_mode):
  207. SyncCullMode();
  208. break;
  209. // Depth modifiers
  210. case PICA_REG_INDEX(viewport_depth_range):
  211. case PICA_REG_INDEX(viewport_depth_far_plane):
  212. SyncDepthModifiers();
  213. break;
  214. // Blending
  215. case PICA_REG_INDEX(output_merger.alphablend_enable):
  216. SyncBlendEnabled();
  217. break;
  218. case PICA_REG_INDEX(output_merger.alpha_blending):
  219. SyncBlendFuncs();
  220. break;
  221. case PICA_REG_INDEX(output_merger.blend_const):
  222. SyncBlendColor();
  223. break;
  224. // Alpha test
  225. case PICA_REG_INDEX(output_merger.alpha_test):
  226. SyncAlphaTest();
  227. shader_dirty = true;
  228. break;
  229. // Sync GL stencil test + stencil write mask
  230. // (Pica stencil test function register also contains a stencil write mask)
  231. case PICA_REG_INDEX(output_merger.stencil_test.raw_func):
  232. SyncStencilTest();
  233. SyncStencilWriteMask();
  234. break;
  235. case PICA_REG_INDEX(output_merger.stencil_test.raw_op):
  236. case PICA_REG_INDEX(framebuffer.depth_format):
  237. SyncStencilTest();
  238. break;
  239. // Sync GL depth test + depth and color write mask
  240. // (Pica depth test function register also contains a depth and color write mask)
  241. case PICA_REG_INDEX(output_merger.depth_test_enable):
  242. SyncDepthTest();
  243. SyncDepthWriteMask();
  244. SyncColorWriteMask();
  245. break;
  246. // Sync GL depth and stencil write mask
  247. // (This is a dedicated combined depth / stencil write-enable register)
  248. case PICA_REG_INDEX(framebuffer.allow_depth_stencil_write):
  249. SyncDepthWriteMask();
  250. SyncStencilWriteMask();
  251. break;
  252. // Sync GL color write mask
  253. // (This is a dedicated color write-enable register)
  254. case PICA_REG_INDEX(framebuffer.allow_color_write):
  255. SyncColorWriteMask();
  256. break;
  257. // Logic op
  258. case PICA_REG_INDEX(output_merger.logic_op):
  259. SyncLogicOp();
  260. break;
  261. // TEV stages
  262. case PICA_REG_INDEX(tev_stage0.color_source1):
  263. case PICA_REG_INDEX(tev_stage0.color_modifier1):
  264. case PICA_REG_INDEX(tev_stage0.color_op):
  265. case PICA_REG_INDEX(tev_stage0.color_scale):
  266. case PICA_REG_INDEX(tev_stage1.color_source1):
  267. case PICA_REG_INDEX(tev_stage1.color_modifier1):
  268. case PICA_REG_INDEX(tev_stage1.color_op):
  269. case PICA_REG_INDEX(tev_stage1.color_scale):
  270. case PICA_REG_INDEX(tev_stage2.color_source1):
  271. case PICA_REG_INDEX(tev_stage2.color_modifier1):
  272. case PICA_REG_INDEX(tev_stage2.color_op):
  273. case PICA_REG_INDEX(tev_stage2.color_scale):
  274. case PICA_REG_INDEX(tev_stage3.color_source1):
  275. case PICA_REG_INDEX(tev_stage3.color_modifier1):
  276. case PICA_REG_INDEX(tev_stage3.color_op):
  277. case PICA_REG_INDEX(tev_stage3.color_scale):
  278. case PICA_REG_INDEX(tev_stage4.color_source1):
  279. case PICA_REG_INDEX(tev_stage4.color_modifier1):
  280. case PICA_REG_INDEX(tev_stage4.color_op):
  281. case PICA_REG_INDEX(tev_stage4.color_scale):
  282. case PICA_REG_INDEX(tev_stage5.color_source1):
  283. case PICA_REG_INDEX(tev_stage5.color_modifier1):
  284. case PICA_REG_INDEX(tev_stage5.color_op):
  285. case PICA_REG_INDEX(tev_stage5.color_scale):
  286. case PICA_REG_INDEX(tev_combiner_buffer_input):
  287. shader_dirty = true;
  288. break;
  289. case PICA_REG_INDEX(tev_stage0.const_r):
  290. SyncTevConstColor(0, regs.tev_stage0);
  291. break;
  292. case PICA_REG_INDEX(tev_stage1.const_r):
  293. SyncTevConstColor(1, regs.tev_stage1);
  294. break;
  295. case PICA_REG_INDEX(tev_stage2.const_r):
  296. SyncTevConstColor(2, regs.tev_stage2);
  297. break;
  298. case PICA_REG_INDEX(tev_stage3.const_r):
  299. SyncTevConstColor(3, regs.tev_stage3);
  300. break;
  301. case PICA_REG_INDEX(tev_stage4.const_r):
  302. SyncTevConstColor(4, regs.tev_stage4);
  303. break;
  304. case PICA_REG_INDEX(tev_stage5.const_r):
  305. SyncTevConstColor(5, regs.tev_stage5);
  306. break;
  307. // TEV combiner buffer color
  308. case PICA_REG_INDEX(tev_combiner_buffer_color):
  309. SyncCombinerColor();
  310. break;
  311. // Fragment lighting specular 0 color
  312. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].specular_0, 0x140 + 0 * 0x10):
  313. SyncLightSpecular0(0);
  314. break;
  315. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].specular_0, 0x140 + 1 * 0x10):
  316. SyncLightSpecular0(1);
  317. break;
  318. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].specular_0, 0x140 + 2 * 0x10):
  319. SyncLightSpecular0(2);
  320. break;
  321. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].specular_0, 0x140 + 3 * 0x10):
  322. SyncLightSpecular0(3);
  323. break;
  324. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].specular_0, 0x140 + 4 * 0x10):
  325. SyncLightSpecular0(4);
  326. break;
  327. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].specular_0, 0x140 + 5 * 0x10):
  328. SyncLightSpecular0(5);
  329. break;
  330. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].specular_0, 0x140 + 6 * 0x10):
  331. SyncLightSpecular0(6);
  332. break;
  333. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].specular_0, 0x140 + 7 * 0x10):
  334. SyncLightSpecular0(7);
  335. break;
  336. // Fragment lighting specular 1 color
  337. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].specular_1, 0x141 + 0 * 0x10):
  338. SyncLightSpecular1(0);
  339. break;
  340. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].specular_1, 0x141 + 1 * 0x10):
  341. SyncLightSpecular1(1);
  342. break;
  343. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].specular_1, 0x141 + 2 * 0x10):
  344. SyncLightSpecular1(2);
  345. break;
  346. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].specular_1, 0x141 + 3 * 0x10):
  347. SyncLightSpecular1(3);
  348. break;
  349. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].specular_1, 0x141 + 4 * 0x10):
  350. SyncLightSpecular1(4);
  351. break;
  352. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].specular_1, 0x141 + 5 * 0x10):
  353. SyncLightSpecular1(5);
  354. break;
  355. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].specular_1, 0x141 + 6 * 0x10):
  356. SyncLightSpecular1(6);
  357. break;
  358. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].specular_1, 0x141 + 7 * 0x10):
  359. SyncLightSpecular1(7);
  360. break;
  361. // Fragment lighting diffuse color
  362. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].diffuse, 0x142 + 0 * 0x10):
  363. SyncLightDiffuse(0);
  364. break;
  365. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].diffuse, 0x142 + 1 * 0x10):
  366. SyncLightDiffuse(1);
  367. break;
  368. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].diffuse, 0x142 + 2 * 0x10):
  369. SyncLightDiffuse(2);
  370. break;
  371. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].diffuse, 0x142 + 3 * 0x10):
  372. SyncLightDiffuse(3);
  373. break;
  374. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].diffuse, 0x142 + 4 * 0x10):
  375. SyncLightDiffuse(4);
  376. break;
  377. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].diffuse, 0x142 + 5 * 0x10):
  378. SyncLightDiffuse(5);
  379. break;
  380. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].diffuse, 0x142 + 6 * 0x10):
  381. SyncLightDiffuse(6);
  382. break;
  383. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].diffuse, 0x142 + 7 * 0x10):
  384. SyncLightDiffuse(7);
  385. break;
  386. // Fragment lighting ambient color
  387. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].ambient, 0x143 + 0 * 0x10):
  388. SyncLightAmbient(0);
  389. break;
  390. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].ambient, 0x143 + 1 * 0x10):
  391. SyncLightAmbient(1);
  392. break;
  393. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].ambient, 0x143 + 2 * 0x10):
  394. SyncLightAmbient(2);
  395. break;
  396. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].ambient, 0x143 + 3 * 0x10):
  397. SyncLightAmbient(3);
  398. break;
  399. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].ambient, 0x143 + 4 * 0x10):
  400. SyncLightAmbient(4);
  401. break;
  402. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].ambient, 0x143 + 5 * 0x10):
  403. SyncLightAmbient(5);
  404. break;
  405. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].ambient, 0x143 + 6 * 0x10):
  406. SyncLightAmbient(6);
  407. break;
  408. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].ambient, 0x143 + 7 * 0x10):
  409. SyncLightAmbient(7);
  410. break;
  411. // Fragment lighting position
  412. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].x, 0x144 + 0 * 0x10):
  413. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].z, 0x145 + 0 * 0x10):
  414. SyncLightPosition(0);
  415. break;
  416. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].x, 0x144 + 1 * 0x10):
  417. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].z, 0x145 + 1 * 0x10):
  418. SyncLightPosition(1);
  419. break;
  420. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].x, 0x144 + 2 * 0x10):
  421. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].z, 0x145 + 2 * 0x10):
  422. SyncLightPosition(2);
  423. break;
  424. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].x, 0x144 + 3 * 0x10):
  425. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].z, 0x145 + 3 * 0x10):
  426. SyncLightPosition(3);
  427. break;
  428. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].x, 0x144 + 4 * 0x10):
  429. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].z, 0x145 + 4 * 0x10):
  430. SyncLightPosition(4);
  431. break;
  432. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].x, 0x144 + 5 * 0x10):
  433. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].z, 0x145 + 5 * 0x10):
  434. SyncLightPosition(5);
  435. break;
  436. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].x, 0x144 + 6 * 0x10):
  437. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].z, 0x145 + 6 * 0x10):
  438. SyncLightPosition(6);
  439. break;
  440. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].x, 0x144 + 7 * 0x10):
  441. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].z, 0x145 + 7 * 0x10):
  442. SyncLightPosition(7);
  443. break;
  444. // Fragment lighting global ambient color (emission + ambient * ambient)
  445. case PICA_REG_INDEX_WORKAROUND(lighting.global_ambient, 0x1c0):
  446. SyncGlobalAmbient();
  447. break;
  448. // Fragment lighting lookup tables
  449. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[0], 0x1c8):
  450. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[1], 0x1c9):
  451. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[2], 0x1ca):
  452. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[3], 0x1cb):
  453. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[4], 0x1cc):
  454. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[5], 0x1cd):
  455. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[6], 0x1ce):
  456. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[7], 0x1cf):
  457. {
  458. auto& lut_config = regs.lighting.lut_config;
  459. uniform_block_data.lut_dirty[lut_config.type / 4] = true;
  460. break;
  461. }
  462. }
  463. }
  464. void RasterizerOpenGL::FlushAll() {
  465. res_cache.FlushAll();
  466. }
  467. void RasterizerOpenGL::FlushRegion(PAddr addr, u32 size) {
  468. res_cache.FlushRegion(addr, size, nullptr, false);
  469. }
  470. void RasterizerOpenGL::FlushAndInvalidateRegion(PAddr addr, u32 size) {
  471. res_cache.FlushRegion(addr, size, nullptr, true);
  472. }
  473. bool RasterizerOpenGL::AccelerateDisplayTransfer(const GPU::Regs::DisplayTransferConfig& config) {
  474. using PixelFormat = CachedSurface::PixelFormat;
  475. using SurfaceType = CachedSurface::SurfaceType;
  476. if (config.is_texture_copy) {
  477. // TODO(tfarley): Try to hardware accelerate this
  478. return false;
  479. }
  480. CachedSurface src_params;
  481. src_params.addr = config.GetPhysicalInputAddress();
  482. src_params.width = config.output_width;
  483. src_params.height = config.output_height;
  484. src_params.is_tiled = !config.input_linear;
  485. src_params.pixel_format = CachedSurface::PixelFormatFromGPUPixelFormat(config.input_format);
  486. CachedSurface dst_params;
  487. dst_params.addr = config.GetPhysicalOutputAddress();
  488. dst_params.width = config.scaling != config.NoScale ? config.output_width / 2 : config.output_width.Value();
  489. dst_params.height = config.scaling == config.ScaleXY ? config.output_height / 2 : config.output_height.Value();
  490. dst_params.is_tiled = config.input_linear != config.dont_swizzle;
  491. dst_params.pixel_format = CachedSurface::PixelFormatFromGPUPixelFormat(config.output_format);
  492. MathUtil::Rectangle<int> src_rect;
  493. CachedSurface* src_surface = res_cache.GetSurfaceRect(src_params, false, true, src_rect);
  494. if (src_surface == nullptr) {
  495. return false;
  496. }
  497. // Require destination surface to have same resolution scale as source to preserve scaling
  498. dst_params.res_scale_width = src_surface->res_scale_width;
  499. dst_params.res_scale_height = src_surface->res_scale_height;
  500. MathUtil::Rectangle<int> dst_rect;
  501. CachedSurface* dst_surface = res_cache.GetSurfaceRect(dst_params, true, false, dst_rect);
  502. if (dst_surface == nullptr) {
  503. return false;
  504. }
  505. // Don't accelerate if the src and dst surfaces are the same
  506. if (src_surface == dst_surface) {
  507. return false;
  508. }
  509. if (config.flip_vertically) {
  510. std::swap(dst_rect.top, dst_rect.bottom);
  511. }
  512. if (!res_cache.TryBlitSurfaces(src_surface, src_rect, dst_surface, dst_rect)) {
  513. return false;
  514. }
  515. u32 dst_size = dst_params.width * dst_params.height * CachedSurface::GetFormatBpp(dst_params.pixel_format) / 8;
  516. dst_surface->dirty = true;
  517. res_cache.FlushRegion(config.GetPhysicalOutputAddress(), dst_size, dst_surface, true);
  518. return true;
  519. }
  520. bool RasterizerOpenGL::AccelerateFill(const GPU::Regs::MemoryFillConfig& config) {
  521. using PixelFormat = CachedSurface::PixelFormat;
  522. using SurfaceType = CachedSurface::SurfaceType;
  523. CachedSurface* dst_surface = res_cache.TryGetFillSurface(config);
  524. if (dst_surface == nullptr) {
  525. return false;
  526. }
  527. OpenGLState cur_state = OpenGLState::GetCurState();
  528. SurfaceType dst_type = CachedSurface::GetFormatType(dst_surface->pixel_format);
  529. GLuint old_fb = cur_state.draw.draw_framebuffer;
  530. cur_state.draw.draw_framebuffer = framebuffer.handle;
  531. // TODO: When scissor test is implemented, need to disable scissor test in cur_state here so Clear call isn't affected
  532. cur_state.Apply();
  533. if (dst_type == SurfaceType::Color || dst_type == SurfaceType::Texture) {
  534. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dst_surface->texture.handle, 0);
  535. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  536. if (OpenGLState::CheckFBStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  537. return false;
  538. }
  539. GLfloat color_values[4] = {0.0f, 0.0f, 0.0f, 0.0f};
  540. // TODO: Handle additional pixel format and fill value size combinations to accelerate more cases
  541. // For instance, checking if fill value's bytes/bits repeat to allow filling I8/A8/I4/A4/...
  542. // Currently only handles formats that are multiples of the fill value size
  543. if (config.fill_24bit) {
  544. switch (dst_surface->pixel_format) {
  545. case PixelFormat::RGB8:
  546. color_values[0] = config.value_24bit_r / 255.0f;
  547. color_values[1] = config.value_24bit_g / 255.0f;
  548. color_values[2] = config.value_24bit_b / 255.0f;
  549. break;
  550. default:
  551. return false;
  552. }
  553. } else if (config.fill_32bit) {
  554. u32 value = config.value_32bit;
  555. switch (dst_surface->pixel_format) {
  556. case PixelFormat::RGBA8:
  557. color_values[0] = (value >> 24) / 255.0f;
  558. color_values[1] = ((value >> 16) & 0xFF) / 255.0f;
  559. color_values[2] = ((value >> 8) & 0xFF) / 255.0f;
  560. color_values[3] = (value & 0xFF) / 255.0f;
  561. break;
  562. default:
  563. return false;
  564. }
  565. } else {
  566. u16 value_16bit = config.value_16bit.Value();
  567. Math::Vec4<u8> color;
  568. switch (dst_surface->pixel_format) {
  569. case PixelFormat::RGBA8:
  570. color_values[0] = (value_16bit >> 8) / 255.0f;
  571. color_values[1] = (value_16bit & 0xFF) / 255.0f;
  572. color_values[2] = color_values[0];
  573. color_values[3] = color_values[1];
  574. break;
  575. case PixelFormat::RGB5A1:
  576. color = Color::DecodeRGB5A1((const u8*)&value_16bit);
  577. color_values[0] = color[0] / 31.0f;
  578. color_values[1] = color[1] / 31.0f;
  579. color_values[2] = color[2] / 31.0f;
  580. color_values[3] = color[3];
  581. break;
  582. case PixelFormat::RGB565:
  583. color = Color::DecodeRGB565((const u8*)&value_16bit);
  584. color_values[0] = color[0] / 31.0f;
  585. color_values[1] = color[1] / 63.0f;
  586. color_values[2] = color[2] / 31.0f;
  587. break;
  588. case PixelFormat::RGBA4:
  589. color = Color::DecodeRGBA4((const u8*)&value_16bit);
  590. color_values[0] = color[0] / 15.0f;
  591. color_values[1] = color[1] / 15.0f;
  592. color_values[2] = color[2] / 15.0f;
  593. color_values[3] = color[3] / 15.0f;
  594. break;
  595. case PixelFormat::IA8:
  596. case PixelFormat::RG8:
  597. color_values[0] = (value_16bit >> 8) / 255.0f;
  598. color_values[1] = (value_16bit & 0xFF) / 255.0f;
  599. break;
  600. default:
  601. return false;
  602. }
  603. }
  604. cur_state.color_mask.red_enabled = true;
  605. cur_state.color_mask.green_enabled = true;
  606. cur_state.color_mask.blue_enabled = true;
  607. cur_state.color_mask.alpha_enabled = true;
  608. cur_state.Apply();
  609. glClearBufferfv(GL_COLOR, 0, color_values);
  610. } else if (dst_type == SurfaceType::Depth) {
  611. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
  612. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, dst_surface->texture.handle, 0);
  613. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  614. if (OpenGLState::CheckFBStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  615. return false;
  616. }
  617. GLfloat value_float;
  618. if (dst_surface->pixel_format == CachedSurface::PixelFormat::D16) {
  619. value_float = config.value_32bit / 65535.0f; // 2^16 - 1
  620. } else if (dst_surface->pixel_format == CachedSurface::PixelFormat::D24) {
  621. value_float = config.value_32bit / 16777215.0f; // 2^24 - 1
  622. }
  623. cur_state.depth.write_mask = true;
  624. cur_state.Apply();
  625. glClearBufferfv(GL_DEPTH, 0, &value_float);
  626. } else if (dst_type == SurfaceType::DepthStencil) {
  627. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
  628. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, dst_surface->texture.handle, 0);
  629. if (OpenGLState::CheckFBStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  630. return false;
  631. }
  632. GLfloat value_float = (config.value_32bit & 0xFFFFFF) / 16777215.0f; // 2^24 - 1
  633. GLint value_int = (config.value_32bit >> 24);
  634. cur_state.depth.write_mask = true;
  635. cur_state.stencil.write_mask = true;
  636. cur_state.Apply();
  637. glClearBufferfi(GL_DEPTH_STENCIL, 0, value_float, value_int);
  638. }
  639. cur_state.draw.draw_framebuffer = old_fb;
  640. // TODO: Return scissor test to previous value when scissor test is implemented
  641. cur_state.Apply();
  642. dst_surface->dirty = true;
  643. res_cache.FlushRegion(dst_surface->addr, dst_surface->size, dst_surface, true);
  644. return true;
  645. }
  646. bool RasterizerOpenGL::AccelerateDisplay(const GPU::Regs::FramebufferConfig& config, PAddr framebuffer_addr, u32 pixel_stride, ScreenInfo& screen_info) {
  647. if (framebuffer_addr == 0) {
  648. return false;
  649. }
  650. CachedSurface src_params;
  651. src_params.addr = framebuffer_addr;
  652. src_params.width = config.width;
  653. src_params.height = config.height;
  654. src_params.stride = pixel_stride;
  655. src_params.is_tiled = false;
  656. src_params.pixel_format = CachedSurface::PixelFormatFromGPUPixelFormat(config.color_format);
  657. MathUtil::Rectangle<int> src_rect;
  658. CachedSurface* src_surface = res_cache.GetSurfaceRect(src_params, false, true, src_rect);
  659. if (src_surface == nullptr) {
  660. return false;
  661. }
  662. u32 scaled_width = src_surface->GetScaledWidth();
  663. u32 scaled_height = src_surface->GetScaledHeight();
  664. screen_info.display_texcoords = MathUtil::Rectangle<float>((float)src_rect.top / (float)scaled_height,
  665. (float)src_rect.left / (float)scaled_width,
  666. (float)src_rect.bottom / (float)scaled_height,
  667. (float)src_rect.right / (float)scaled_width);
  668. screen_info.display_texture = src_surface->texture.handle;
  669. return true;
  670. }
  671. void RasterizerOpenGL::SamplerInfo::Create() {
  672. sampler.Create();
  673. mag_filter = min_filter = TextureConfig::Linear;
  674. wrap_s = wrap_t = TextureConfig::Repeat;
  675. border_color = 0;
  676. glSamplerParameteri(sampler.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // default is GL_LINEAR_MIPMAP_LINEAR
  677. // Other attributes have correct defaults
  678. }
  679. void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Pica::Regs::TextureConfig& config) {
  680. GLuint s = sampler.handle;
  681. if (mag_filter != config.mag_filter) {
  682. mag_filter = config.mag_filter;
  683. glSamplerParameteri(s, GL_TEXTURE_MAG_FILTER, PicaToGL::TextureFilterMode(mag_filter));
  684. }
  685. if (min_filter != config.min_filter) {
  686. min_filter = config.min_filter;
  687. glSamplerParameteri(s, GL_TEXTURE_MIN_FILTER, PicaToGL::TextureFilterMode(min_filter));
  688. }
  689. if (wrap_s != config.wrap_s) {
  690. wrap_s = config.wrap_s;
  691. glSamplerParameteri(s, GL_TEXTURE_WRAP_S, PicaToGL::WrapMode(wrap_s));
  692. }
  693. if (wrap_t != config.wrap_t) {
  694. wrap_t = config.wrap_t;
  695. glSamplerParameteri(s, GL_TEXTURE_WRAP_T, PicaToGL::WrapMode(wrap_t));
  696. }
  697. if (wrap_s == TextureConfig::ClampToBorder || wrap_t == TextureConfig::ClampToBorder) {
  698. if (border_color != config.border_color.raw) {
  699. border_color = config.border_color.raw;
  700. auto gl_color = PicaToGL::ColorRGBA8(border_color);
  701. glSamplerParameterfv(s, GL_TEXTURE_BORDER_COLOR, gl_color.data());
  702. }
  703. }
  704. }
  705. void RasterizerOpenGL::SetShader() {
  706. PicaShaderConfig config = PicaShaderConfig::CurrentConfig();
  707. std::unique_ptr<PicaShader> shader = std::make_unique<PicaShader>();
  708. // Find (or generate) the GLSL shader for the current TEV state
  709. auto cached_shader = shader_cache.find(config);
  710. if (cached_shader != shader_cache.end()) {
  711. current_shader = cached_shader->second.get();
  712. state.draw.shader_program = current_shader->shader.handle;
  713. state.Apply();
  714. } else {
  715. LOG_DEBUG(Render_OpenGL, "Creating new shader");
  716. shader->shader.Create(GLShader::GenerateVertexShader().c_str(), GLShader::GenerateFragmentShader(config).c_str());
  717. state.draw.shader_program = shader->shader.handle;
  718. state.Apply();
  719. // Set the texture samplers to correspond to different texture units
  720. GLuint uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[0]");
  721. if (uniform_tex != -1) { glUniform1i(uniform_tex, 0); }
  722. uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[1]");
  723. if (uniform_tex != -1) { glUniform1i(uniform_tex, 1); }
  724. uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[2]");
  725. if (uniform_tex != -1) { glUniform1i(uniform_tex, 2); }
  726. // Set the texture samplers to correspond to different lookup table texture units
  727. GLuint uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[0]");
  728. if (uniform_lut != -1) { glUniform1i(uniform_lut, 3); }
  729. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[1]");
  730. if (uniform_lut != -1) { glUniform1i(uniform_lut, 4); }
  731. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[2]");
  732. if (uniform_lut != -1) { glUniform1i(uniform_lut, 5); }
  733. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[3]");
  734. if (uniform_lut != -1) { glUniform1i(uniform_lut, 6); }
  735. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[4]");
  736. if (uniform_lut != -1) { glUniform1i(uniform_lut, 7); }
  737. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[5]");
  738. if (uniform_lut != -1) { glUniform1i(uniform_lut, 8); }
  739. current_shader = shader_cache.emplace(config, std::move(shader)).first->second.get();
  740. unsigned int block_index = glGetUniformBlockIndex(current_shader->shader.handle, "shader_data");
  741. glUniformBlockBinding(current_shader->shader.handle, block_index, 0);
  742. // Update uniforms
  743. SyncAlphaTest();
  744. SyncCombinerColor();
  745. auto& tev_stages = Pica::g_state.regs.GetTevStages();
  746. for (int index = 0; index < tev_stages.size(); ++index)
  747. SyncTevConstColor(index, tev_stages[index]);
  748. SyncGlobalAmbient();
  749. for (int light_index = 0; light_index < 8; light_index++) {
  750. SyncLightSpecular0(light_index);
  751. SyncLightSpecular1(light_index);
  752. SyncLightDiffuse(light_index);
  753. SyncLightAmbient(light_index);
  754. SyncLightPosition(light_index);
  755. }
  756. }
  757. }
  758. void RasterizerOpenGL::SyncCullMode() {
  759. const auto& regs = Pica::g_state.regs;
  760. switch (regs.cull_mode) {
  761. case Pica::Regs::CullMode::KeepAll:
  762. state.cull.enabled = false;
  763. break;
  764. case Pica::Regs::CullMode::KeepClockWise:
  765. state.cull.enabled = true;
  766. state.cull.front_face = GL_CW;
  767. break;
  768. case Pica::Regs::CullMode::KeepCounterClockWise:
  769. state.cull.enabled = true;
  770. state.cull.front_face = GL_CCW;
  771. break;
  772. default:
  773. LOG_CRITICAL(Render_OpenGL, "Unknown cull mode %d", regs.cull_mode.Value());
  774. UNIMPLEMENTED();
  775. break;
  776. }
  777. }
  778. void RasterizerOpenGL::SyncDepthModifiers() {
  779. float depth_scale = -Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_range).ToFloat32();
  780. float depth_offset = Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_far_plane).ToFloat32() / 2.0f;
  781. // TODO: Implement scale modifier
  782. uniform_block_data.data.depth_offset = depth_offset;
  783. uniform_block_data.dirty = true;
  784. }
  785. void RasterizerOpenGL::SyncBlendEnabled() {
  786. state.blend.enabled = (Pica::g_state.regs.output_merger.alphablend_enable == 1);
  787. }
  788. void RasterizerOpenGL::SyncBlendFuncs() {
  789. const auto& regs = Pica::g_state.regs;
  790. state.blend.src_rgb_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_source_rgb);
  791. state.blend.dst_rgb_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_dest_rgb);
  792. state.blend.src_a_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_source_a);
  793. state.blend.dst_a_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_dest_a);
  794. }
  795. void RasterizerOpenGL::SyncBlendColor() {
  796. auto blend_color = PicaToGL::ColorRGBA8(Pica::g_state.regs.output_merger.blend_const.raw);
  797. state.blend.color.red = blend_color[0];
  798. state.blend.color.green = blend_color[1];
  799. state.blend.color.blue = blend_color[2];
  800. state.blend.color.alpha = blend_color[3];
  801. }
  802. void RasterizerOpenGL::SyncAlphaTest() {
  803. const auto& regs = Pica::g_state.regs;
  804. if (regs.output_merger.alpha_test.ref != uniform_block_data.data.alphatest_ref) {
  805. uniform_block_data.data.alphatest_ref = regs.output_merger.alpha_test.ref;
  806. uniform_block_data.dirty = true;
  807. }
  808. }
  809. void RasterizerOpenGL::SyncLogicOp() {
  810. state.logic_op = PicaToGL::LogicOp(Pica::g_state.regs.output_merger.logic_op);
  811. }
  812. void RasterizerOpenGL::SyncColorWriteMask() {
  813. const auto& regs = Pica::g_state.regs;
  814. auto IsColorWriteEnabled = [&](u32 value) {
  815. return (regs.framebuffer.allow_color_write != 0 && value != 0) ? GL_TRUE : GL_FALSE;
  816. };
  817. state.color_mask.red_enabled = IsColorWriteEnabled(regs.output_merger.red_enable);
  818. state.color_mask.green_enabled = IsColorWriteEnabled(regs.output_merger.green_enable);
  819. state.color_mask.blue_enabled = IsColorWriteEnabled(regs.output_merger.blue_enable);
  820. state.color_mask.alpha_enabled = IsColorWriteEnabled(regs.output_merger.alpha_enable);
  821. }
  822. void RasterizerOpenGL::SyncStencilWriteMask() {
  823. const auto& regs = Pica::g_state.regs;
  824. state.stencil.write_mask = (regs.framebuffer.allow_depth_stencil_write != 0)
  825. ? static_cast<GLuint>(regs.output_merger.stencil_test.write_mask)
  826. : 0;
  827. }
  828. void RasterizerOpenGL::SyncDepthWriteMask() {
  829. const auto& regs = Pica::g_state.regs;
  830. state.depth.write_mask = (regs.framebuffer.allow_depth_stencil_write != 0 && regs.output_merger.depth_write_enable)
  831. ? GL_TRUE
  832. : GL_FALSE;
  833. }
  834. void RasterizerOpenGL::SyncStencilTest() {
  835. const auto& regs = Pica::g_state.regs;
  836. state.stencil.test_enabled = regs.output_merger.stencil_test.enable && regs.framebuffer.depth_format == Pica::Regs::DepthFormat::D24S8;
  837. state.stencil.test_func = PicaToGL::CompareFunc(regs.output_merger.stencil_test.func);
  838. state.stencil.test_ref = regs.output_merger.stencil_test.reference_value;
  839. state.stencil.test_mask = regs.output_merger.stencil_test.input_mask;
  840. state.stencil.action_stencil_fail = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_stencil_fail);
  841. state.stencil.action_depth_fail = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_depth_fail);
  842. state.stencil.action_depth_pass = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_depth_pass);
  843. }
  844. void RasterizerOpenGL::SyncDepthTest() {
  845. const auto& regs = Pica::g_state.regs;
  846. state.depth.test_enabled = regs.output_merger.depth_test_enable == 1 ||
  847. regs.output_merger.depth_write_enable == 1;
  848. state.depth.test_func = regs.output_merger.depth_test_enable == 1 ?
  849. PicaToGL::CompareFunc(regs.output_merger.depth_test_func) : GL_ALWAYS;
  850. }
  851. void RasterizerOpenGL::SyncCombinerColor() {
  852. auto combiner_color = PicaToGL::ColorRGBA8(Pica::g_state.regs.tev_combiner_buffer_color.raw);
  853. if (combiner_color != uniform_block_data.data.tev_combiner_buffer_color) {
  854. uniform_block_data.data.tev_combiner_buffer_color = combiner_color;
  855. uniform_block_data.dirty = true;
  856. }
  857. }
  858. void RasterizerOpenGL::SyncTevConstColor(int stage_index, const Pica::Regs::TevStageConfig& tev_stage) {
  859. auto const_color = PicaToGL::ColorRGBA8(tev_stage.const_color);
  860. if (const_color != uniform_block_data.data.const_color[stage_index]) {
  861. uniform_block_data.data.const_color[stage_index] = const_color;
  862. uniform_block_data.dirty = true;
  863. }
  864. }
  865. void RasterizerOpenGL::SyncGlobalAmbient() {
  866. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.global_ambient);
  867. if (color != uniform_block_data.data.lighting_global_ambient) {
  868. uniform_block_data.data.lighting_global_ambient = color;
  869. uniform_block_data.dirty = true;
  870. }
  871. }
  872. void RasterizerOpenGL::SyncLightingLUT(unsigned lut_index) {
  873. std::array<GLvec4, 256> new_data;
  874. for (unsigned offset = 0; offset < new_data.size(); ++offset) {
  875. new_data[offset][0] = Pica::g_state.lighting.luts[(lut_index * 4) + 0][offset].ToFloat();
  876. new_data[offset][1] = Pica::g_state.lighting.luts[(lut_index * 4) + 1][offset].ToFloat();
  877. new_data[offset][2] = Pica::g_state.lighting.luts[(lut_index * 4) + 2][offset].ToFloat();
  878. new_data[offset][3] = Pica::g_state.lighting.luts[(lut_index * 4) + 3][offset].ToFloat();
  879. }
  880. if (new_data != lighting_lut_data[lut_index]) {
  881. lighting_lut_data[lut_index] = new_data;
  882. glActiveTexture(GL_TEXTURE3 + lut_index);
  883. glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT, lighting_lut_data[lut_index].data());
  884. }
  885. }
  886. void RasterizerOpenGL::SyncLightSpecular0(int light_index) {
  887. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].specular_0);
  888. if (color != uniform_block_data.data.light_src[light_index].specular_0) {
  889. uniform_block_data.data.light_src[light_index].specular_0 = color;
  890. uniform_block_data.dirty = true;
  891. }
  892. }
  893. void RasterizerOpenGL::SyncLightSpecular1(int light_index) {
  894. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].specular_1);
  895. if (color != uniform_block_data.data.light_src[light_index].specular_1) {
  896. uniform_block_data.data.light_src[light_index].specular_1 = color;
  897. uniform_block_data.dirty = true;
  898. }
  899. }
  900. void RasterizerOpenGL::SyncLightDiffuse(int light_index) {
  901. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].diffuse);
  902. if (color != uniform_block_data.data.light_src[light_index].diffuse) {
  903. uniform_block_data.data.light_src[light_index].diffuse = color;
  904. uniform_block_data.dirty = true;
  905. }
  906. }
  907. void RasterizerOpenGL::SyncLightAmbient(int light_index) {
  908. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].ambient);
  909. if (color != uniform_block_data.data.light_src[light_index].ambient) {
  910. uniform_block_data.data.light_src[light_index].ambient = color;
  911. uniform_block_data.dirty = true;
  912. }
  913. }
  914. void RasterizerOpenGL::SyncLightPosition(int light_index) {
  915. GLvec3 position = {
  916. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].x).ToFloat32(),
  917. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].y).ToFloat32(),
  918. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].z).ToFloat32() };
  919. if (position != uniform_block_data.data.light_src[light_index].position) {
  920. uniform_block_data.data.light_src[light_index].position = position;
  921. uniform_block_data.dirty = true;
  922. }
  923. }