gl_rasterizer.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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/make_unique.h"
  10. #include "common/math_util.h"
  11. #include "common/microprofile.h"
  12. #include "common/profiler.h"
  13. #include "core/memory.h"
  14. #include "core/settings.h"
  15. #include "core/hw/gpu.h"
  16. #include "video_core/pica.h"
  17. #include "video_core/pica_state.h"
  18. #include "video_core/utils.h"
  19. #include "video_core/renderer_opengl/gl_rasterizer.h"
  20. #include "video_core/renderer_opengl/gl_shader_gen.h"
  21. #include "video_core/renderer_opengl/gl_shader_util.h"
  22. #include "video_core/renderer_opengl/pica_to_gl.h"
  23. static bool IsPassThroughTevStage(const Pica::Regs::TevStageConfig& stage) {
  24. return (stage.color_op == Pica::Regs::TevStageConfig::Operation::Replace &&
  25. stage.alpha_op == Pica::Regs::TevStageConfig::Operation::Replace &&
  26. stage.color_source1 == Pica::Regs::TevStageConfig::Source::Previous &&
  27. stage.alpha_source1 == Pica::Regs::TevStageConfig::Source::Previous &&
  28. stage.color_modifier1 == Pica::Regs::TevStageConfig::ColorModifier::SourceColor &&
  29. stage.alpha_modifier1 == Pica::Regs::TevStageConfig::AlphaModifier::SourceAlpha &&
  30. stage.GetColorMultiplier() == 1 &&
  31. stage.GetAlphaMultiplier() == 1);
  32. }
  33. RasterizerOpenGL::RasterizerOpenGL() : cached_fb_color_addr(0), cached_fb_depth_addr(0) { }
  34. RasterizerOpenGL::~RasterizerOpenGL() { }
  35. void RasterizerOpenGL::InitObjects() {
  36. // Create sampler objects
  37. for (size_t i = 0; i < texture_samplers.size(); ++i) {
  38. texture_samplers[i].Create();
  39. state.texture_units[i].sampler = texture_samplers[i].sampler.handle;
  40. }
  41. // Generate VBO, VAO and UBO
  42. vertex_buffer.Create();
  43. vertex_array.Create();
  44. uniform_buffer.Create();
  45. state.draw.vertex_array = vertex_array.handle;
  46. state.draw.vertex_buffer = vertex_buffer.handle;
  47. state.draw.uniform_buffer = uniform_buffer.handle;
  48. state.Apply();
  49. // Bind the UBO to binding point 0
  50. glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer.handle);
  51. uniform_block_data.dirty = true;
  52. // Set vertex attributes
  53. glVertexAttribPointer(GLShader::ATTRIBUTE_POSITION, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, position));
  54. glEnableVertexAttribArray(GLShader::ATTRIBUTE_POSITION);
  55. glVertexAttribPointer(GLShader::ATTRIBUTE_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, color));
  56. glEnableVertexAttribArray(GLShader::ATTRIBUTE_COLOR);
  57. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord0));
  58. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD1, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord1));
  59. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD2, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord2));
  60. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD0);
  61. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD1);
  62. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD2);
  63. glVertexAttribPointer(GLShader::ATTRIBUTE_NORMQUAT, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, normquat));
  64. glEnableVertexAttribArray(GLShader::ATTRIBUTE_NORMQUAT);
  65. glVertexAttribPointer(GLShader::ATTRIBUTE_VIEW, 3, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, view));
  66. glEnableVertexAttribArray(GLShader::ATTRIBUTE_VIEW);
  67. SetShader();
  68. // Create textures for OGL framebuffer that will be rendered to, initially 1x1 to succeed in framebuffer creation
  69. fb_color_texture.texture.Create();
  70. ReconfigureColorTexture(fb_color_texture, Pica::Regs::ColorFormat::RGBA8, 1, 1);
  71. state.texture_units[0].texture_2d = fb_color_texture.texture.handle;
  72. state.Apply();
  73. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  74. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  75. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  76. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  77. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  78. state.texture_units[0].texture_2d = 0;
  79. state.Apply();
  80. fb_depth_texture.texture.Create();
  81. ReconfigureDepthTexture(fb_depth_texture, Pica::Regs::DepthFormat::D16, 1, 1);
  82. state.texture_units[0].texture_2d = fb_depth_texture.texture.handle;
  83. state.Apply();
  84. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  85. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  86. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  87. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  88. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  89. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  90. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  91. state.texture_units[0].texture_2d = 0;
  92. state.Apply();
  93. // Configure OpenGL framebuffer
  94. framebuffer.Create();
  95. state.draw.framebuffer = framebuffer.handle;
  96. state.Apply();
  97. glActiveTexture(GL_TEXTURE0);
  98. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_color_texture.texture.handle, 0);
  99. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, fb_depth_texture.texture.handle, 0);
  100. for (size_t i = 0; i < lighting_lut.size(); ++i) {
  101. lighting_lut[i].Create();
  102. state.lighting_lut[i].texture_1d = lighting_lut[i].handle;
  103. glActiveTexture(GL_TEXTURE3 + i);
  104. glBindTexture(GL_TEXTURE_1D, state.lighting_lut[i].texture_1d);
  105. glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr);
  106. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  107. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  108. }
  109. state.Apply();
  110. ASSERT_MSG(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE,
  111. "OpenGL rasterizer framebuffer setup failed, status %X", glCheckFramebufferStatus(GL_FRAMEBUFFER));
  112. }
  113. void RasterizerOpenGL::Reset() {
  114. SyncCullMode();
  115. SyncDepthModifiers();
  116. SyncBlendEnabled();
  117. SyncBlendFuncs();
  118. SyncBlendColor();
  119. SyncLogicOp();
  120. SyncStencilTest();
  121. SyncDepthTest();
  122. SetShader();
  123. res_cache.InvalidateAll();
  124. }
  125. /**
  126. * This is a helper function to resolve an issue with opposite quaternions being interpolated by
  127. * OpenGL. See below for a detailed description of this issue (yuriks):
  128. *
  129. * For any rotation, there are two quaternions Q, and -Q, that represent the same rotation. If you
  130. * interpolate two quaternions that are opposite, instead of going from one rotation to another
  131. * using the shortest path, you'll go around the longest path. You can test if two quaternions are
  132. * opposite by checking if Dot(Q1, W2) < 0. In that case, you can flip either of them, therefore
  133. * making Dot(-Q1, W2) positive.
  134. *
  135. * NOTE: This solution corrects this issue per-vertex before passing the quaternions to OpenGL. This
  136. * should be correct for nearly all cases, however a more correct implementation (but less trivial
  137. * and perhaps unnecessary) would be to handle this per-fragment, by interpolating the quaternions
  138. * manually using two Lerps, and doing this correction before each Lerp.
  139. */
  140. static bool AreQuaternionsOpposite(Math::Vec4<Pica::float24> qa, Math::Vec4<Pica::float24> qb) {
  141. Math::Vec4f a{ qa.x.ToFloat32(), qa.y.ToFloat32(), qa.z.ToFloat32(), qa.w.ToFloat32() };
  142. Math::Vec4f b{ qb.x.ToFloat32(), qb.y.ToFloat32(), qb.z.ToFloat32(), qb.w.ToFloat32() };
  143. return (Math::Dot(a, b) < 0.f);
  144. }
  145. void RasterizerOpenGL::AddTriangle(const Pica::Shader::OutputVertex& v0,
  146. const Pica::Shader::OutputVertex& v1,
  147. const Pica::Shader::OutputVertex& v2) {
  148. vertex_batch.emplace_back(v0, false);
  149. vertex_batch.emplace_back(v1, AreQuaternionsOpposite(v0.quat, v1.quat));
  150. vertex_batch.emplace_back(v2, AreQuaternionsOpposite(v0.quat, v2.quat));
  151. }
  152. void RasterizerOpenGL::DrawTriangles() {
  153. if (vertex_batch.empty())
  154. return;
  155. SyncFramebuffer();
  156. SyncDrawState();
  157. if (state.draw.shader_dirty) {
  158. SetShader();
  159. state.draw.shader_dirty = false;
  160. }
  161. for (unsigned index = 0; index < lighting_lut.size(); index++) {
  162. if (uniform_block_data.lut_dirty[index]) {
  163. SyncLightingLUT(index);
  164. uniform_block_data.lut_dirty[index] = false;
  165. }
  166. }
  167. if (uniform_block_data.dirty) {
  168. glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformData), &uniform_block_data.data, GL_STATIC_DRAW);
  169. uniform_block_data.dirty = false;
  170. }
  171. glBufferData(GL_ARRAY_BUFFER, vertex_batch.size() * sizeof(HardwareVertex), vertex_batch.data(), GL_STREAM_DRAW);
  172. glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertex_batch.size());
  173. vertex_batch.clear();
  174. // Flush the resource cache at the current depth and color framebuffer addresses for render-to-texture
  175. const auto& regs = Pica::g_state.regs;
  176. u32 cached_fb_color_size = Pica::Regs::BytesPerColorPixel(fb_color_texture.format)
  177. * fb_color_texture.width * fb_color_texture.height;
  178. u32 cached_fb_depth_size = Pica::Regs::BytesPerDepthPixel(fb_depth_texture.format)
  179. * fb_depth_texture.width * fb_depth_texture.height;
  180. res_cache.InvalidateInRange(cached_fb_color_addr, cached_fb_color_size, true);
  181. res_cache.InvalidateInRange(cached_fb_depth_addr, cached_fb_depth_size, true);
  182. }
  183. void RasterizerOpenGL::FlushFramebuffer() {
  184. CommitColorBuffer();
  185. CommitDepthBuffer();
  186. }
  187. void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
  188. const auto& regs = Pica::g_state.regs;
  189. switch(id) {
  190. // Culling
  191. case PICA_REG_INDEX(cull_mode):
  192. SyncCullMode();
  193. break;
  194. // Depth modifiers
  195. case PICA_REG_INDEX(viewport_depth_range):
  196. case PICA_REG_INDEX(viewport_depth_far_plane):
  197. SyncDepthModifiers();
  198. break;
  199. // Blending
  200. case PICA_REG_INDEX(output_merger.alphablend_enable):
  201. SyncBlendEnabled();
  202. break;
  203. case PICA_REG_INDEX(output_merger.alpha_blending):
  204. SyncBlendFuncs();
  205. break;
  206. case PICA_REG_INDEX(output_merger.blend_const):
  207. SyncBlendColor();
  208. break;
  209. // Alpha test
  210. case PICA_REG_INDEX(output_merger.alpha_test):
  211. SyncAlphaTest();
  212. state.draw.shader_dirty = true;
  213. break;
  214. // Stencil test
  215. case PICA_REG_INDEX(output_merger.stencil_test.raw_func):
  216. case PICA_REG_INDEX(output_merger.stencil_test.raw_op):
  217. SyncStencilTest();
  218. break;
  219. // Depth test
  220. case PICA_REG_INDEX(output_merger.depth_test_enable):
  221. SyncDepthTest();
  222. break;
  223. // Logic op
  224. case PICA_REG_INDEX(output_merger.logic_op):
  225. SyncLogicOp();
  226. break;
  227. // TEV stages
  228. case PICA_REG_INDEX(tev_stage0.color_source1):
  229. case PICA_REG_INDEX(tev_stage0.color_modifier1):
  230. case PICA_REG_INDEX(tev_stage0.color_op):
  231. case PICA_REG_INDEX(tev_stage0.color_scale):
  232. case PICA_REG_INDEX(tev_stage1.color_source1):
  233. case PICA_REG_INDEX(tev_stage1.color_modifier1):
  234. case PICA_REG_INDEX(tev_stage1.color_op):
  235. case PICA_REG_INDEX(tev_stage1.color_scale):
  236. case PICA_REG_INDEX(tev_stage2.color_source1):
  237. case PICA_REG_INDEX(tev_stage2.color_modifier1):
  238. case PICA_REG_INDEX(tev_stage2.color_op):
  239. case PICA_REG_INDEX(tev_stage2.color_scale):
  240. case PICA_REG_INDEX(tev_stage3.color_source1):
  241. case PICA_REG_INDEX(tev_stage3.color_modifier1):
  242. case PICA_REG_INDEX(tev_stage3.color_op):
  243. case PICA_REG_INDEX(tev_stage3.color_scale):
  244. case PICA_REG_INDEX(tev_stage4.color_source1):
  245. case PICA_REG_INDEX(tev_stage4.color_modifier1):
  246. case PICA_REG_INDEX(tev_stage4.color_op):
  247. case PICA_REG_INDEX(tev_stage4.color_scale):
  248. case PICA_REG_INDEX(tev_stage5.color_source1):
  249. case PICA_REG_INDEX(tev_stage5.color_modifier1):
  250. case PICA_REG_INDEX(tev_stage5.color_op):
  251. case PICA_REG_INDEX(tev_stage5.color_scale):
  252. case PICA_REG_INDEX(tev_combiner_buffer_input):
  253. state.draw.shader_dirty = true;
  254. break;
  255. case PICA_REG_INDEX(tev_stage0.const_r):
  256. SyncTevConstColor(0, regs.tev_stage0);
  257. break;
  258. case PICA_REG_INDEX(tev_stage1.const_r):
  259. SyncTevConstColor(1, regs.tev_stage1);
  260. break;
  261. case PICA_REG_INDEX(tev_stage2.const_r):
  262. SyncTevConstColor(2, regs.tev_stage2);
  263. break;
  264. case PICA_REG_INDEX(tev_stage3.const_r):
  265. SyncTevConstColor(3, regs.tev_stage3);
  266. break;
  267. case PICA_REG_INDEX(tev_stage4.const_r):
  268. SyncTevConstColor(4, regs.tev_stage4);
  269. break;
  270. case PICA_REG_INDEX(tev_stage5.const_r):
  271. SyncTevConstColor(5, regs.tev_stage5);
  272. break;
  273. // TEV combiner buffer color
  274. case PICA_REG_INDEX(tev_combiner_buffer_color):
  275. SyncCombinerColor();
  276. break;
  277. // Fragment lighting specular 0 color
  278. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].specular_0, 0x140 + 0 * 0x10):
  279. SyncLightSpecular0(0);
  280. break;
  281. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].specular_0, 0x140 + 1 * 0x10):
  282. SyncLightSpecular0(1);
  283. break;
  284. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].specular_0, 0x140 + 2 * 0x10):
  285. SyncLightSpecular0(2);
  286. break;
  287. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].specular_0, 0x140 + 3 * 0x10):
  288. SyncLightSpecular0(3);
  289. break;
  290. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].specular_0, 0x140 + 4 * 0x10):
  291. SyncLightSpecular0(4);
  292. break;
  293. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].specular_0, 0x140 + 5 * 0x10):
  294. SyncLightSpecular0(5);
  295. break;
  296. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].specular_0, 0x140 + 6 * 0x10):
  297. SyncLightSpecular0(6);
  298. break;
  299. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].specular_0, 0x140 + 7 * 0x10):
  300. SyncLightSpecular0(7);
  301. break;
  302. // Fragment lighting specular 1 color
  303. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].specular_1, 0x141 + 0 * 0x10):
  304. SyncLightSpecular1(0);
  305. break;
  306. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].specular_1, 0x141 + 1 * 0x10):
  307. SyncLightSpecular1(1);
  308. break;
  309. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].specular_1, 0x141 + 2 * 0x10):
  310. SyncLightSpecular1(2);
  311. break;
  312. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].specular_1, 0x141 + 3 * 0x10):
  313. SyncLightSpecular1(3);
  314. break;
  315. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].specular_1, 0x141 + 4 * 0x10):
  316. SyncLightSpecular1(4);
  317. break;
  318. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].specular_1, 0x141 + 5 * 0x10):
  319. SyncLightSpecular1(5);
  320. break;
  321. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].specular_1, 0x141 + 6 * 0x10):
  322. SyncLightSpecular1(6);
  323. break;
  324. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].specular_1, 0x141 + 7 * 0x10):
  325. SyncLightSpecular1(7);
  326. break;
  327. // Fragment lighting diffuse color
  328. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].diffuse, 0x142 + 0 * 0x10):
  329. SyncLightDiffuse(0);
  330. break;
  331. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].diffuse, 0x142 + 1 * 0x10):
  332. SyncLightDiffuse(1);
  333. break;
  334. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].diffuse, 0x142 + 2 * 0x10):
  335. SyncLightDiffuse(2);
  336. break;
  337. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].diffuse, 0x142 + 3 * 0x10):
  338. SyncLightDiffuse(3);
  339. break;
  340. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].diffuse, 0x142 + 4 * 0x10):
  341. SyncLightDiffuse(4);
  342. break;
  343. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].diffuse, 0x142 + 5 * 0x10):
  344. SyncLightDiffuse(5);
  345. break;
  346. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].diffuse, 0x142 + 6 * 0x10):
  347. SyncLightDiffuse(6);
  348. break;
  349. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].diffuse, 0x142 + 7 * 0x10):
  350. SyncLightDiffuse(7);
  351. break;
  352. // Fragment lighting ambient color
  353. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].ambient, 0x143 + 0 * 0x10):
  354. SyncLightAmbient(0);
  355. break;
  356. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].ambient, 0x143 + 1 * 0x10):
  357. SyncLightAmbient(1);
  358. break;
  359. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].ambient, 0x143 + 2 * 0x10):
  360. SyncLightAmbient(2);
  361. break;
  362. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].ambient, 0x143 + 3 * 0x10):
  363. SyncLightAmbient(3);
  364. break;
  365. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].ambient, 0x143 + 4 * 0x10):
  366. SyncLightAmbient(4);
  367. break;
  368. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].ambient, 0x143 + 5 * 0x10):
  369. SyncLightAmbient(5);
  370. break;
  371. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].ambient, 0x143 + 6 * 0x10):
  372. SyncLightAmbient(6);
  373. break;
  374. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].ambient, 0x143 + 7 * 0x10):
  375. SyncLightAmbient(7);
  376. break;
  377. // Fragment lighting position
  378. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].x, 0x144 + 0 * 0x10):
  379. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].z, 0x145 + 0 * 0x10):
  380. SyncLightPosition(0);
  381. break;
  382. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].x, 0x144 + 1 * 0x10):
  383. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].z, 0x145 + 1 * 0x10):
  384. SyncLightPosition(1);
  385. break;
  386. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].x, 0x144 + 2 * 0x10):
  387. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].z, 0x145 + 2 * 0x10):
  388. SyncLightPosition(2);
  389. break;
  390. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].x, 0x144 + 3 * 0x10):
  391. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].z, 0x145 + 3 * 0x10):
  392. SyncLightPosition(3);
  393. break;
  394. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].x, 0x144 + 4 * 0x10):
  395. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].z, 0x145 + 4 * 0x10):
  396. SyncLightPosition(4);
  397. break;
  398. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].x, 0x144 + 5 * 0x10):
  399. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].z, 0x145 + 5 * 0x10):
  400. SyncLightPosition(5);
  401. break;
  402. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].x, 0x144 + 6 * 0x10):
  403. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].z, 0x145 + 6 * 0x10):
  404. SyncLightPosition(6);
  405. break;
  406. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].x, 0x144 + 7 * 0x10):
  407. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].z, 0x145 + 7 * 0x10):
  408. SyncLightPosition(7);
  409. break;
  410. // Fragment lighting global ambient color (emission + ambient * ambient)
  411. case PICA_REG_INDEX_WORKAROUND(lighting.global_ambient, 0x1c0):
  412. SyncGlobalAmbient();
  413. break;
  414. // Fragment lighting lookup tables
  415. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[0], 0x1c8):
  416. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[1], 0x1c9):
  417. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[2], 0x1ca):
  418. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[3], 0x1cb):
  419. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[4], 0x1cc):
  420. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[5], 0x1cd):
  421. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[6], 0x1ce):
  422. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[7], 0x1cf):
  423. {
  424. auto& lut_config = regs.lighting.lut_config;
  425. uniform_block_data.lut_dirty[lut_config.type / 4] = true;
  426. break;
  427. }
  428. }
  429. }
  430. void RasterizerOpenGL::FlushRegion(PAddr addr, u32 size) {
  431. const auto& regs = Pica::g_state.regs;
  432. u32 cached_fb_color_size = Pica::Regs::BytesPerColorPixel(fb_color_texture.format)
  433. * fb_color_texture.width * fb_color_texture.height;
  434. u32 cached_fb_depth_size = Pica::Regs::BytesPerDepthPixel(fb_depth_texture.format)
  435. * fb_depth_texture.width * fb_depth_texture.height;
  436. // If source memory region overlaps 3DS framebuffers, commit them before the copy happens
  437. if (MathUtil::IntervalsIntersect(addr, size, cached_fb_color_addr, cached_fb_color_size))
  438. CommitColorBuffer();
  439. if (MathUtil::IntervalsIntersect(addr, size, cached_fb_depth_addr, cached_fb_depth_size))
  440. CommitDepthBuffer();
  441. }
  442. void RasterizerOpenGL::InvalidateRegion(PAddr addr, u32 size) {
  443. const auto& regs = Pica::g_state.regs;
  444. u32 cached_fb_color_size = Pica::Regs::BytesPerColorPixel(fb_color_texture.format)
  445. * fb_color_texture.width * fb_color_texture.height;
  446. u32 cached_fb_depth_size = Pica::Regs::BytesPerDepthPixel(fb_depth_texture.format)
  447. * fb_depth_texture.width * fb_depth_texture.height;
  448. // If modified memory region overlaps 3DS framebuffers, reload their contents into OpenGL
  449. if (MathUtil::IntervalsIntersect(addr, size, cached_fb_color_addr, cached_fb_color_size))
  450. ReloadColorBuffer();
  451. if (MathUtil::IntervalsIntersect(addr, size, cached_fb_depth_addr, cached_fb_depth_size))
  452. ReloadDepthBuffer();
  453. // Notify cache of flush in case the region touches a cached resource
  454. res_cache.InvalidateInRange(addr, size);
  455. }
  456. void RasterizerOpenGL::SamplerInfo::Create() {
  457. sampler.Create();
  458. mag_filter = min_filter = TextureConfig::Linear;
  459. wrap_s = wrap_t = TextureConfig::Repeat;
  460. border_color = 0;
  461. glSamplerParameteri(sampler.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // default is GL_LINEAR_MIPMAP_LINEAR
  462. // Other attributes have correct defaults
  463. }
  464. void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Pica::Regs::TextureConfig& config) {
  465. GLuint s = sampler.handle;
  466. if (mag_filter != config.mag_filter) {
  467. mag_filter = config.mag_filter;
  468. glSamplerParameteri(s, GL_TEXTURE_MAG_FILTER, PicaToGL::TextureFilterMode(mag_filter));
  469. }
  470. if (min_filter != config.min_filter) {
  471. min_filter = config.min_filter;
  472. glSamplerParameteri(s, GL_TEXTURE_MIN_FILTER, PicaToGL::TextureFilterMode(min_filter));
  473. }
  474. if (wrap_s != config.wrap_s) {
  475. wrap_s = config.wrap_s;
  476. glSamplerParameteri(s, GL_TEXTURE_WRAP_S, PicaToGL::WrapMode(wrap_s));
  477. }
  478. if (wrap_t != config.wrap_t) {
  479. wrap_t = config.wrap_t;
  480. glSamplerParameteri(s, GL_TEXTURE_WRAP_T, PicaToGL::WrapMode(wrap_t));
  481. }
  482. if (wrap_s == TextureConfig::ClampToBorder || wrap_t == TextureConfig::ClampToBorder) {
  483. if (border_color != config.border_color.raw) {
  484. auto gl_color = PicaToGL::ColorRGBA8(border_color);
  485. glSamplerParameterfv(s, GL_TEXTURE_BORDER_COLOR, gl_color.data());
  486. }
  487. }
  488. }
  489. void RasterizerOpenGL::ReconfigureColorTexture(TextureInfo& texture, Pica::Regs::ColorFormat format, u32 width, u32 height) {
  490. GLint internal_format;
  491. texture.format = format;
  492. texture.width = width;
  493. texture.height = height;
  494. switch (format) {
  495. case Pica::Regs::ColorFormat::RGBA8:
  496. internal_format = GL_RGBA;
  497. texture.gl_format = GL_RGBA;
  498. texture.gl_type = GL_UNSIGNED_INT_8_8_8_8;
  499. break;
  500. case Pica::Regs::ColorFormat::RGB8:
  501. // This pixel format uses BGR since GL_UNSIGNED_BYTE specifies byte-order, unlike every
  502. // specific OpenGL type used in this function using native-endian (that is, little-endian
  503. // mostly everywhere) for words or half-words.
  504. // TODO: check how those behave on big-endian processors.
  505. internal_format = GL_RGB;
  506. texture.gl_format = GL_BGR;
  507. texture.gl_type = GL_UNSIGNED_BYTE;
  508. break;
  509. case Pica::Regs::ColorFormat::RGB5A1:
  510. internal_format = GL_RGBA;
  511. texture.gl_format = GL_RGBA;
  512. texture.gl_type = GL_UNSIGNED_SHORT_5_5_5_1;
  513. break;
  514. case Pica::Regs::ColorFormat::RGB565:
  515. internal_format = GL_RGB;
  516. texture.gl_format = GL_RGB;
  517. texture.gl_type = GL_UNSIGNED_SHORT_5_6_5;
  518. break;
  519. case Pica::Regs::ColorFormat::RGBA4:
  520. internal_format = GL_RGBA;
  521. texture.gl_format = GL_RGBA;
  522. texture.gl_type = GL_UNSIGNED_SHORT_4_4_4_4;
  523. break;
  524. default:
  525. LOG_CRITICAL(Render_OpenGL, "Unknown framebuffer texture color format %x", format);
  526. UNIMPLEMENTED();
  527. break;
  528. }
  529. state.texture_units[0].texture_2d = texture.texture.handle;
  530. state.Apply();
  531. glActiveTexture(GL_TEXTURE0);
  532. glTexImage2D(GL_TEXTURE_2D, 0, internal_format, texture.width, texture.height, 0,
  533. texture.gl_format, texture.gl_type, nullptr);
  534. state.texture_units[0].texture_2d = 0;
  535. state.Apply();
  536. }
  537. void RasterizerOpenGL::ReconfigureDepthTexture(DepthTextureInfo& texture, Pica::Regs::DepthFormat format, u32 width, u32 height) {
  538. GLint internal_format;
  539. texture.format = format;
  540. texture.width = width;
  541. texture.height = height;
  542. switch (format) {
  543. case Pica::Regs::DepthFormat::D16:
  544. internal_format = GL_DEPTH_COMPONENT16;
  545. texture.gl_format = GL_DEPTH_COMPONENT;
  546. texture.gl_type = GL_UNSIGNED_SHORT;
  547. break;
  548. case Pica::Regs::DepthFormat::D24:
  549. internal_format = GL_DEPTH_COMPONENT24;
  550. texture.gl_format = GL_DEPTH_COMPONENT;
  551. texture.gl_type = GL_UNSIGNED_INT;
  552. break;
  553. case Pica::Regs::DepthFormat::D24S8:
  554. internal_format = GL_DEPTH24_STENCIL8;
  555. texture.gl_format = GL_DEPTH_STENCIL;
  556. texture.gl_type = GL_UNSIGNED_INT_24_8;
  557. break;
  558. default:
  559. LOG_CRITICAL(Render_OpenGL, "Unknown framebuffer texture depth format %x", format);
  560. UNIMPLEMENTED();
  561. break;
  562. }
  563. state.texture_units[0].texture_2d = texture.texture.handle;
  564. state.Apply();
  565. glActiveTexture(GL_TEXTURE0);
  566. glTexImage2D(GL_TEXTURE_2D, 0, internal_format, texture.width, texture.height, 0,
  567. texture.gl_format, texture.gl_type, nullptr);
  568. state.texture_units[0].texture_2d = 0;
  569. state.Apply();
  570. }
  571. void RasterizerOpenGL::SetShader() {
  572. PicaShaderConfig config = PicaShaderConfig::CurrentConfig();
  573. std::unique_ptr<PicaShader> shader = Common::make_unique<PicaShader>();
  574. // Find (or generate) the GLSL shader for the current TEV state
  575. auto cached_shader = shader_cache.find(config);
  576. if (cached_shader != shader_cache.end()) {
  577. current_shader = cached_shader->second.get();
  578. state.draw.shader_program = current_shader->shader.handle;
  579. state.Apply();
  580. } else {
  581. LOG_DEBUG(Render_OpenGL, "Creating new shader");
  582. shader->shader.Create(GLShader::GenerateVertexShader().c_str(), GLShader::GenerateFragmentShader(config).c_str());
  583. state.draw.shader_program = shader->shader.handle;
  584. state.Apply();
  585. // Set the texture samplers to correspond to different texture units
  586. GLuint uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[0]");
  587. if (uniform_tex != -1) { glUniform1i(uniform_tex, 0); }
  588. uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[1]");
  589. if (uniform_tex != -1) { glUniform1i(uniform_tex, 1); }
  590. uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[2]");
  591. if (uniform_tex != -1) { glUniform1i(uniform_tex, 2); }
  592. // Set the texture samplers to correspond to different lookup table texture units
  593. GLuint uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[0]");
  594. if (uniform_lut != -1) { glUniform1i(uniform_lut, 3); }
  595. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[1]");
  596. if (uniform_lut != -1) { glUniform1i(uniform_lut, 4); }
  597. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[2]");
  598. if (uniform_lut != -1) { glUniform1i(uniform_lut, 5); }
  599. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[3]");
  600. if (uniform_lut != -1) { glUniform1i(uniform_lut, 6); }
  601. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[4]");
  602. if (uniform_lut != -1) { glUniform1i(uniform_lut, 7); }
  603. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[5]");
  604. if (uniform_lut != -1) { glUniform1i(uniform_lut, 8); }
  605. current_shader = shader_cache.emplace(config, std::move(shader)).first->second.get();
  606. unsigned int block_index = glGetUniformBlockIndex(current_shader->shader.handle, "shader_data");
  607. glUniformBlockBinding(current_shader->shader.handle, block_index, 0);
  608. // Update uniforms
  609. SyncAlphaTest();
  610. SyncCombinerColor();
  611. auto& tev_stages = Pica::g_state.regs.GetTevStages();
  612. for (int index = 0; index < tev_stages.size(); ++index)
  613. SyncTevConstColor(index, tev_stages[index]);
  614. SyncGlobalAmbient();
  615. for (int light_index = 0; light_index < 8; light_index++) {
  616. SyncLightDiffuse(light_index);
  617. SyncLightAmbient(light_index);
  618. SyncLightPosition(light_index);
  619. }
  620. }
  621. }
  622. void RasterizerOpenGL::SyncFramebuffer() {
  623. const auto& regs = Pica::g_state.regs;
  624. PAddr new_fb_color_addr = regs.framebuffer.GetColorBufferPhysicalAddress();
  625. Pica::Regs::ColorFormat new_fb_color_format = regs.framebuffer.color_format;
  626. PAddr new_fb_depth_addr = regs.framebuffer.GetDepthBufferPhysicalAddress();
  627. Pica::Regs::DepthFormat new_fb_depth_format = regs.framebuffer.depth_format;
  628. bool fb_size_changed = fb_color_texture.width != static_cast<GLsizei>(regs.framebuffer.GetWidth()) ||
  629. fb_color_texture.height != static_cast<GLsizei>(regs.framebuffer.GetHeight());
  630. bool color_fb_prop_changed = fb_color_texture.format != new_fb_color_format ||
  631. fb_size_changed;
  632. bool depth_fb_prop_changed = fb_depth_texture.format != new_fb_depth_format ||
  633. fb_size_changed;
  634. bool color_fb_modified = cached_fb_color_addr != new_fb_color_addr ||
  635. color_fb_prop_changed;
  636. bool depth_fb_modified = cached_fb_depth_addr != new_fb_depth_addr ||
  637. depth_fb_prop_changed;
  638. // Commit if framebuffer modified in any way
  639. if (color_fb_modified)
  640. CommitColorBuffer();
  641. if (depth_fb_modified)
  642. CommitDepthBuffer();
  643. // Reconfigure framebuffer textures if any property has changed
  644. if (color_fb_prop_changed) {
  645. ReconfigureColorTexture(fb_color_texture, new_fb_color_format,
  646. regs.framebuffer.GetWidth(), regs.framebuffer.GetHeight());
  647. }
  648. if (depth_fb_prop_changed) {
  649. ReconfigureDepthTexture(fb_depth_texture, new_fb_depth_format,
  650. regs.framebuffer.GetWidth(), regs.framebuffer.GetHeight());
  651. // Only attach depth buffer as stencil if it supports stencil
  652. switch (new_fb_depth_format) {
  653. case Pica::Regs::DepthFormat::D16:
  654. case Pica::Regs::DepthFormat::D24:
  655. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  656. break;
  657. case Pica::Regs::DepthFormat::D24S8:
  658. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, fb_depth_texture.texture.handle, 0);
  659. break;
  660. default:
  661. LOG_CRITICAL(Render_OpenGL, "Unknown framebuffer depth format %x", new_fb_depth_format);
  662. UNIMPLEMENTED();
  663. break;
  664. }
  665. }
  666. // Load buffer data again if fb modified in any way
  667. if (color_fb_modified) {
  668. cached_fb_color_addr = new_fb_color_addr;
  669. ReloadColorBuffer();
  670. }
  671. if (depth_fb_modified) {
  672. cached_fb_depth_addr = new_fb_depth_addr;
  673. ReloadDepthBuffer();
  674. }
  675. ASSERT_MSG(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE,
  676. "OpenGL rasterizer framebuffer setup failed, status %X", glCheckFramebufferStatus(GL_FRAMEBUFFER));
  677. }
  678. void RasterizerOpenGL::SyncCullMode() {
  679. const auto& regs = Pica::g_state.regs;
  680. switch (regs.cull_mode) {
  681. case Pica::Regs::CullMode::KeepAll:
  682. state.cull.enabled = false;
  683. break;
  684. case Pica::Regs::CullMode::KeepClockWise:
  685. state.cull.enabled = true;
  686. state.cull.front_face = GL_CW;
  687. break;
  688. case Pica::Regs::CullMode::KeepCounterClockWise:
  689. state.cull.enabled = true;
  690. state.cull.front_face = GL_CCW;
  691. break;
  692. default:
  693. LOG_CRITICAL(Render_OpenGL, "Unknown cull mode %d", regs.cull_mode.Value());
  694. UNIMPLEMENTED();
  695. break;
  696. }
  697. }
  698. void RasterizerOpenGL::SyncDepthModifiers() {
  699. float depth_scale = -Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_range).ToFloat32();
  700. float depth_offset = Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_far_plane).ToFloat32() / 2.0f;
  701. // TODO: Implement scale modifier
  702. uniform_block_data.data.depth_offset = depth_offset;
  703. uniform_block_data.dirty = true;
  704. }
  705. void RasterizerOpenGL::SyncBlendEnabled() {
  706. state.blend.enabled = (Pica::g_state.regs.output_merger.alphablend_enable == 1);
  707. }
  708. void RasterizerOpenGL::SyncBlendFuncs() {
  709. const auto& regs = Pica::g_state.regs;
  710. state.blend.src_rgb_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_source_rgb);
  711. state.blend.dst_rgb_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_dest_rgb);
  712. state.blend.src_a_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_source_a);
  713. state.blend.dst_a_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_dest_a);
  714. }
  715. void RasterizerOpenGL::SyncBlendColor() {
  716. auto blend_color = PicaToGL::ColorRGBA8(Pica::g_state.regs.output_merger.blend_const.raw);
  717. state.blend.color.red = blend_color[0];
  718. state.blend.color.green = blend_color[1];
  719. state.blend.color.blue = blend_color[2];
  720. state.blend.color.alpha = blend_color[3];
  721. }
  722. void RasterizerOpenGL::SyncAlphaTest() {
  723. const auto& regs = Pica::g_state.regs;
  724. if (regs.output_merger.alpha_test.ref != uniform_block_data.data.alphatest_ref) {
  725. uniform_block_data.data.alphatest_ref = regs.output_merger.alpha_test.ref;
  726. uniform_block_data.dirty = true;
  727. }
  728. }
  729. void RasterizerOpenGL::SyncLogicOp() {
  730. state.logic_op = PicaToGL::LogicOp(Pica::g_state.regs.output_merger.logic_op);
  731. }
  732. void RasterizerOpenGL::SyncStencilTest() {
  733. const auto& regs = Pica::g_state.regs;
  734. state.stencil.test_enabled = regs.output_merger.stencil_test.enable && regs.framebuffer.depth_format == Pica::Regs::DepthFormat::D24S8;
  735. state.stencil.test_func = PicaToGL::CompareFunc(regs.output_merger.stencil_test.func);
  736. state.stencil.test_ref = regs.output_merger.stencil_test.reference_value;
  737. state.stencil.test_mask = regs.output_merger.stencil_test.input_mask;
  738. state.stencil.write_mask = regs.output_merger.stencil_test.write_mask;
  739. state.stencil.action_stencil_fail = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_stencil_fail);
  740. state.stencil.action_depth_fail = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_depth_fail);
  741. state.stencil.action_depth_pass = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_depth_pass);
  742. }
  743. void RasterizerOpenGL::SyncDepthTest() {
  744. const auto& regs = Pica::g_state.regs;
  745. state.depth.test_enabled = regs.output_merger.depth_test_enable == 1 ||
  746. regs.output_merger.depth_write_enable == 1;
  747. state.depth.test_func = regs.output_merger.depth_test_enable == 1 ?
  748. PicaToGL::CompareFunc(regs.output_merger.depth_test_func) : GL_ALWAYS;
  749. state.color_mask.red_enabled = regs.output_merger.red_enable;
  750. state.color_mask.green_enabled = regs.output_merger.green_enable;
  751. state.color_mask.blue_enabled = regs.output_merger.blue_enable;
  752. state.color_mask.alpha_enabled = regs.output_merger.alpha_enable;
  753. state.depth.write_mask = regs.output_merger.depth_write_enable ? GL_TRUE : GL_FALSE;
  754. }
  755. void RasterizerOpenGL::SyncCombinerColor() {
  756. auto combiner_color = PicaToGL::ColorRGBA8(Pica::g_state.regs.tev_combiner_buffer_color.raw);
  757. if (combiner_color != uniform_block_data.data.tev_combiner_buffer_color) {
  758. uniform_block_data.data.tev_combiner_buffer_color = combiner_color;
  759. uniform_block_data.dirty = true;
  760. }
  761. }
  762. void RasterizerOpenGL::SyncTevConstColor(int stage_index, const Pica::Regs::TevStageConfig& tev_stage) {
  763. auto const_color = PicaToGL::ColorRGBA8(tev_stage.const_color);
  764. if (const_color != uniform_block_data.data.const_color[stage_index]) {
  765. uniform_block_data.data.const_color[stage_index] = const_color;
  766. uniform_block_data.dirty = true;
  767. }
  768. }
  769. void RasterizerOpenGL::SyncGlobalAmbient() {
  770. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.global_ambient);
  771. if (color != uniform_block_data.data.lighting_global_ambient) {
  772. uniform_block_data.data.lighting_global_ambient = color;
  773. uniform_block_data.dirty = true;
  774. }
  775. }
  776. void RasterizerOpenGL::SyncLightingLUT(unsigned lut_index) {
  777. std::array<GLvec4, 256> new_data;
  778. for (unsigned offset = 0; offset < new_data.size(); ++offset) {
  779. new_data[offset][0] = Pica::g_state.lighting.luts[(lut_index * 4) + 0][offset].ToFloat();
  780. new_data[offset][1] = Pica::g_state.lighting.luts[(lut_index * 4) + 1][offset].ToFloat();
  781. new_data[offset][2] = Pica::g_state.lighting.luts[(lut_index * 4) + 2][offset].ToFloat();
  782. new_data[offset][3] = Pica::g_state.lighting.luts[(lut_index * 4) + 3][offset].ToFloat();
  783. }
  784. if (new_data != lighting_lut_data[lut_index]) {
  785. lighting_lut_data[lut_index] = new_data;
  786. glActiveTexture(GL_TEXTURE3 + lut_index);
  787. glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT, lighting_lut_data[lut_index].data());
  788. }
  789. }
  790. void RasterizerOpenGL::SyncLightSpecular0(int light_index) {
  791. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].specular_0);
  792. if (color != uniform_block_data.data.light_src[light_index].specular_0) {
  793. uniform_block_data.data.light_src[light_index].specular_0 = color;
  794. uniform_block_data.dirty = true;
  795. }
  796. }
  797. void RasterizerOpenGL::SyncLightSpecular1(int light_index) {
  798. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].specular_1);
  799. if (color != uniform_block_data.data.light_src[light_index].specular_1) {
  800. uniform_block_data.data.light_src[light_index].specular_1 = color;
  801. uniform_block_data.dirty = true;
  802. }
  803. }
  804. void RasterizerOpenGL::SyncLightDiffuse(int light_index) {
  805. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].diffuse);
  806. if (color != uniform_block_data.data.light_src[light_index].diffuse) {
  807. uniform_block_data.data.light_src[light_index].diffuse = color;
  808. uniform_block_data.dirty = true;
  809. }
  810. }
  811. void RasterizerOpenGL::SyncLightAmbient(int light_index) {
  812. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].ambient);
  813. if (color != uniform_block_data.data.light_src[light_index].ambient) {
  814. uniform_block_data.data.light_src[light_index].ambient = color;
  815. uniform_block_data.dirty = true;
  816. }
  817. }
  818. void RasterizerOpenGL::SyncLightPosition(int light_index) {
  819. GLvec3 position = {
  820. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].x).ToFloat32(),
  821. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].y).ToFloat32(),
  822. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].z).ToFloat32() };
  823. if (position != uniform_block_data.data.light_src[light_index].position) {
  824. uniform_block_data.data.light_src[light_index].position = position;
  825. uniform_block_data.dirty = true;
  826. }
  827. }
  828. void RasterizerOpenGL::SyncDrawState() {
  829. const auto& regs = Pica::g_state.regs;
  830. // Sync the viewport
  831. GLsizei viewport_width = (GLsizei)Pica::float24::FromRaw(regs.viewport_size_x).ToFloat32() * 2;
  832. GLsizei viewport_height = (GLsizei)Pica::float24::FromRaw(regs.viewport_size_y).ToFloat32() * 2;
  833. // OpenGL uses different y coordinates, so negate corner offset and flip origin
  834. // TODO: Ensure viewport_corner.x should not be negated or origin flipped
  835. // TODO: Use floating-point viewports for accuracy if supported
  836. glViewport((GLsizei)regs.viewport_corner.x,
  837. (GLsizei)regs.viewport_corner.y,
  838. viewport_width, viewport_height);
  839. // Sync bound texture(s), upload if not cached
  840. const auto pica_textures = regs.GetTextures();
  841. for (unsigned texture_index = 0; texture_index < pica_textures.size(); ++texture_index) {
  842. const auto& texture = pica_textures[texture_index];
  843. if (texture.enabled) {
  844. texture_samplers[texture_index].SyncWithConfig(texture.config);
  845. res_cache.LoadAndBindTexture(state, texture_index, texture);
  846. } else {
  847. state.texture_units[texture_index].texture_2d = 0;
  848. }
  849. }
  850. state.draw.uniform_buffer = uniform_buffer.handle;
  851. state.Apply();
  852. }
  853. MICROPROFILE_DEFINE(OpenGL_FramebufferReload, "OpenGL", "FB Reload", MP_RGB(70, 70, 200));
  854. void RasterizerOpenGL::ReloadColorBuffer() {
  855. u8* color_buffer = Memory::GetPhysicalPointer(cached_fb_color_addr);
  856. if (color_buffer == nullptr)
  857. return;
  858. MICROPROFILE_SCOPE(OpenGL_FramebufferReload);
  859. u32 bytes_per_pixel = Pica::Regs::BytesPerColorPixel(fb_color_texture.format);
  860. std::unique_ptr<u8[]> temp_fb_color_buffer(new u8[fb_color_texture.width * fb_color_texture.height * bytes_per_pixel]);
  861. // Directly copy pixels. Internal OpenGL color formats are consistent so no conversion is necessary.
  862. for (int y = 0; y < fb_color_texture.height; ++y) {
  863. for (int x = 0; x < fb_color_texture.width; ++x) {
  864. const u32 coarse_y = y & ~7;
  865. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_color_texture.width * bytes_per_pixel;
  866. u32 gl_pixel_index = (x + (fb_color_texture.height - 1 - y) * fb_color_texture.width) * bytes_per_pixel;
  867. u8* pixel = color_buffer + dst_offset;
  868. memcpy(&temp_fb_color_buffer[gl_pixel_index], pixel, bytes_per_pixel);
  869. }
  870. }
  871. state.texture_units[0].texture_2d = fb_color_texture.texture.handle;
  872. state.Apply();
  873. glActiveTexture(GL_TEXTURE0);
  874. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, fb_color_texture.width, fb_color_texture.height,
  875. fb_color_texture.gl_format, fb_color_texture.gl_type, temp_fb_color_buffer.get());
  876. state.texture_units[0].texture_2d = 0;
  877. state.Apply();
  878. }
  879. void RasterizerOpenGL::ReloadDepthBuffer() {
  880. if (cached_fb_depth_addr == 0)
  881. return;
  882. // TODO: Appears to work, but double-check endianness of depth values and order of depth-stencil
  883. u8* depth_buffer = Memory::GetPhysicalPointer(cached_fb_depth_addr);
  884. if (depth_buffer == nullptr)
  885. return;
  886. MICROPROFILE_SCOPE(OpenGL_FramebufferReload);
  887. u32 bytes_per_pixel = Pica::Regs::BytesPerDepthPixel(fb_depth_texture.format);
  888. // OpenGL needs 4 bpp alignment for D24
  889. u32 gl_bpp = bytes_per_pixel == 3 ? 4 : bytes_per_pixel;
  890. std::unique_ptr<u8[]> temp_fb_depth_buffer(new u8[fb_depth_texture.width * fb_depth_texture.height * gl_bpp]);
  891. u8* temp_fb_depth_data = bytes_per_pixel == 3 ? (temp_fb_depth_buffer.get() + 1) : temp_fb_depth_buffer.get();
  892. if (fb_depth_texture.format == Pica::Regs::DepthFormat::D24S8) {
  893. for (int y = 0; y < fb_depth_texture.height; ++y) {
  894. for (int x = 0; x < fb_depth_texture.width; ++x) {
  895. const u32 coarse_y = y & ~7;
  896. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
  897. u32 gl_pixel_index = (x + (fb_depth_texture.height - 1 - y) * fb_depth_texture.width);
  898. u8* pixel = depth_buffer + dst_offset;
  899. u32 depth_stencil = *(u32*)pixel;
  900. ((u32*)temp_fb_depth_data)[gl_pixel_index] = (depth_stencil << 8) | (depth_stencil >> 24);
  901. }
  902. }
  903. } else {
  904. for (int y = 0; y < fb_depth_texture.height; ++y) {
  905. for (int x = 0; x < fb_depth_texture.width; ++x) {
  906. const u32 coarse_y = y & ~7;
  907. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
  908. u32 gl_pixel_index = (x + (fb_depth_texture.height - 1 - y) * fb_depth_texture.width) * gl_bpp;
  909. u8* pixel = depth_buffer + dst_offset;
  910. memcpy(&temp_fb_depth_data[gl_pixel_index], pixel, bytes_per_pixel);
  911. }
  912. }
  913. }
  914. state.texture_units[0].texture_2d = fb_depth_texture.texture.handle;
  915. state.Apply();
  916. glActiveTexture(GL_TEXTURE0);
  917. if (fb_depth_texture.format == Pica::Regs::DepthFormat::D24S8) {
  918. // TODO(Subv): There is a bug with Intel Windows drivers that makes glTexSubImage2D not change the stencil buffer.
  919. // The bug has been reported to Intel (https://communities.intel.com/message/324464)
  920. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, fb_depth_texture.width, fb_depth_texture.height, 0,
  921. GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, temp_fb_depth_buffer.get());
  922. } else {
  923. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, fb_depth_texture.width, fb_depth_texture.height,
  924. fb_depth_texture.gl_format, fb_depth_texture.gl_type, temp_fb_depth_buffer.get());
  925. }
  926. state.texture_units[0].texture_2d = 0;
  927. state.Apply();
  928. }
  929. Common::Profiling::TimingCategory buffer_commit_category("Framebuffer Commit");
  930. MICROPROFILE_DEFINE(OpenGL_FramebufferCommit, "OpenGL", "FB Commit", MP_RGB(70, 70, 200));
  931. void RasterizerOpenGL::CommitColorBuffer() {
  932. if (cached_fb_color_addr != 0) {
  933. u8* color_buffer = Memory::GetPhysicalPointer(cached_fb_color_addr);
  934. if (color_buffer != nullptr) {
  935. Common::Profiling::ScopeTimer timer(buffer_commit_category);
  936. MICROPROFILE_SCOPE(OpenGL_FramebufferCommit);
  937. u32 bytes_per_pixel = Pica::Regs::BytesPerColorPixel(fb_color_texture.format);
  938. std::unique_ptr<u8[]> temp_gl_color_buffer(new u8[fb_color_texture.width * fb_color_texture.height * bytes_per_pixel]);
  939. state.texture_units[0].texture_2d = fb_color_texture.texture.handle;
  940. state.Apply();
  941. glActiveTexture(GL_TEXTURE0);
  942. glGetTexImage(GL_TEXTURE_2D, 0, fb_color_texture.gl_format, fb_color_texture.gl_type, temp_gl_color_buffer.get());
  943. state.texture_units[0].texture_2d = 0;
  944. state.Apply();
  945. // Directly copy pixels. Internal OpenGL color formats are consistent so no conversion is necessary.
  946. for (int y = 0; y < fb_color_texture.height; ++y) {
  947. for (int x = 0; x < fb_color_texture.width; ++x) {
  948. const u32 coarse_y = y & ~7;
  949. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_color_texture.width * bytes_per_pixel;
  950. u32 gl_pixel_index = x * bytes_per_pixel + (fb_color_texture.height - 1 - y) * fb_color_texture.width * bytes_per_pixel;
  951. u8* pixel = color_buffer + dst_offset;
  952. memcpy(pixel, &temp_gl_color_buffer[gl_pixel_index], bytes_per_pixel);
  953. }
  954. }
  955. }
  956. }
  957. }
  958. void RasterizerOpenGL::CommitDepthBuffer() {
  959. if (cached_fb_depth_addr != 0) {
  960. // TODO: Output seems correct visually, but doesn't quite match sw renderer output. One of them is wrong.
  961. u8* depth_buffer = Memory::GetPhysicalPointer(cached_fb_depth_addr);
  962. if (depth_buffer != nullptr) {
  963. Common::Profiling::ScopeTimer timer(buffer_commit_category);
  964. MICROPROFILE_SCOPE(OpenGL_FramebufferCommit);
  965. u32 bytes_per_pixel = Pica::Regs::BytesPerDepthPixel(fb_depth_texture.format);
  966. // OpenGL needs 4 bpp alignment for D24
  967. u32 gl_bpp = bytes_per_pixel == 3 ? 4 : bytes_per_pixel;
  968. std::unique_ptr<u8[]> temp_gl_depth_buffer(new u8[fb_depth_texture.width * fb_depth_texture.height * gl_bpp]);
  969. state.texture_units[0].texture_2d = fb_depth_texture.texture.handle;
  970. state.Apply();
  971. glActiveTexture(GL_TEXTURE0);
  972. glGetTexImage(GL_TEXTURE_2D, 0, fb_depth_texture.gl_format, fb_depth_texture.gl_type, temp_gl_depth_buffer.get());
  973. state.texture_units[0].texture_2d = 0;
  974. state.Apply();
  975. u8* temp_gl_depth_data = bytes_per_pixel == 3 ? (temp_gl_depth_buffer.get() + 1) : temp_gl_depth_buffer.get();
  976. if (fb_depth_texture.format == Pica::Regs::DepthFormat::D24S8) {
  977. for (int y = 0; y < fb_depth_texture.height; ++y) {
  978. for (int x = 0; x < fb_depth_texture.width; ++x) {
  979. const u32 coarse_y = y & ~7;
  980. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
  981. u32 gl_pixel_index = (x + (fb_depth_texture.height - 1 - y) * fb_depth_texture.width);
  982. u8* pixel = depth_buffer + dst_offset;
  983. u32 depth_stencil = ((u32*)temp_gl_depth_data)[gl_pixel_index];
  984. *(u32*)pixel = (depth_stencil >> 8) | (depth_stencil << 24);
  985. }
  986. }
  987. } else {
  988. for (int y = 0; y < fb_depth_texture.height; ++y) {
  989. for (int x = 0; x < fb_depth_texture.width; ++x) {
  990. const u32 coarse_y = y & ~7;
  991. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
  992. u32 gl_pixel_index = (x + (fb_depth_texture.height - 1 - y) * fb_depth_texture.width) * gl_bpp;
  993. u8* pixel = depth_buffer + dst_offset;
  994. memcpy(pixel, &temp_gl_depth_data[gl_pixel_index], bytes_per_pixel);
  995. }
  996. }
  997. }
  998. }
  999. }
  1000. }