gl_rasterizer.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <string>
  6. #include <tuple>
  7. #include <utility>
  8. #include <glad/glad.h>
  9. #include "common/assert.h"
  10. #include "common/color.h"
  11. #include "common/logging/log.h"
  12. #include "common/math_util.h"
  13. #include "common/microprofile.h"
  14. #include "common/vector_math.h"
  15. #include "core/hw/gpu.h"
  16. #include "video_core/pica_state.h"
  17. #include "video_core/regs_framebuffer.h"
  18. #include "video_core/regs_rasterizer.h"
  19. #include "video_core/regs_texturing.h"
  20. #include "video_core/renderer_opengl/gl_rasterizer.h"
  21. #include "video_core/renderer_opengl/gl_shader_gen.h"
  22. #include "video_core/renderer_opengl/pica_to_gl.h"
  23. #include "video_core/renderer_opengl/renderer_opengl.h"
  24. MICROPROFILE_DEFINE(OpenGL_Drawing, "OpenGL", "Drawing", MP_RGB(128, 128, 192));
  25. MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(100, 100, 255));
  26. MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Mgmt", MP_RGB(100, 255, 100));
  27. RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
  28. // Create sampler objects
  29. for (size_t i = 0; i < texture_samplers.size(); ++i) {
  30. texture_samplers[i].Create();
  31. state.texture_units[i].sampler = texture_samplers[i].sampler.handle;
  32. }
  33. // Generate VBO, VAO and UBO
  34. vertex_buffer.Create();
  35. vertex_array.Create();
  36. uniform_buffer.Create();
  37. state.draw.vertex_array = vertex_array.handle;
  38. state.draw.vertex_buffer = vertex_buffer.handle;
  39. state.draw.uniform_buffer = uniform_buffer.handle;
  40. state.Apply();
  41. // Bind the UBO to binding point 0
  42. glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer.handle);
  43. uniform_block_data.dirty = true;
  44. for (unsigned index = 0; index < lighting_luts.size(); index++) {
  45. uniform_block_data.lut_dirty[index] = true;
  46. }
  47. uniform_block_data.fog_lut_dirty = true;
  48. uniform_block_data.proctex_noise_lut_dirty = true;
  49. uniform_block_data.proctex_color_map_dirty = true;
  50. uniform_block_data.proctex_alpha_map_dirty = true;
  51. uniform_block_data.proctex_lut_dirty = true;
  52. uniform_block_data.proctex_diff_lut_dirty = true;
  53. // Set vertex attributes
  54. glVertexAttribPointer(GLShader::ATTRIBUTE_POSITION, 4, GL_FLOAT, GL_FALSE,
  55. sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, position));
  56. glEnableVertexAttribArray(GLShader::ATTRIBUTE_POSITION);
  57. glVertexAttribPointer(GLShader::ATTRIBUTE_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex),
  58. (GLvoid*)offsetof(HardwareVertex, color));
  59. glEnableVertexAttribArray(GLShader::ATTRIBUTE_COLOR);
  60. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD0, 2, GL_FLOAT, GL_FALSE,
  61. sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord0));
  62. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD1, 2, GL_FLOAT, GL_FALSE,
  63. sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord1));
  64. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD2, 2, GL_FLOAT, GL_FALSE,
  65. sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord2));
  66. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD0);
  67. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD1);
  68. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD2);
  69. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD0_W, 1, GL_FLOAT, GL_FALSE,
  70. sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord0_w));
  71. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD0_W);
  72. glVertexAttribPointer(GLShader::ATTRIBUTE_NORMQUAT, 4, GL_FLOAT, GL_FALSE,
  73. sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, normquat));
  74. glEnableVertexAttribArray(GLShader::ATTRIBUTE_NORMQUAT);
  75. glVertexAttribPointer(GLShader::ATTRIBUTE_VIEW, 3, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex),
  76. (GLvoid*)offsetof(HardwareVertex, view));
  77. glEnableVertexAttribArray(GLShader::ATTRIBUTE_VIEW);
  78. // Create render framebuffer
  79. framebuffer.Create();
  80. // Allocate and bind lighting lut textures
  81. for (size_t i = 0; i < lighting_luts.size(); ++i) {
  82. lighting_luts[i].Create();
  83. state.lighting_luts[i].texture_1d = lighting_luts[i].handle;
  84. }
  85. state.Apply();
  86. for (size_t i = 0; i < lighting_luts.size(); ++i) {
  87. glActiveTexture(static_cast<GLenum>(GL_TEXTURE3 + i));
  88. glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr);
  89. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  90. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  91. }
  92. // Setup the LUT for the fog
  93. {
  94. fog_lut.Create();
  95. state.fog_lut.texture_1d = fog_lut.handle;
  96. }
  97. state.Apply();
  98. glActiveTexture(GL_TEXTURE9);
  99. glTexImage1D(GL_TEXTURE_1D, 0, GL_R32UI, 128, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, nullptr);
  100. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  101. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  102. // Setup the noise LUT for proctex
  103. proctex_noise_lut.Create();
  104. state.proctex_noise_lut.texture_1d = proctex_noise_lut.handle;
  105. state.Apply();
  106. glActiveTexture(GL_TEXTURE10);
  107. glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr);
  108. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  109. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  110. // Setup the color map for proctex
  111. proctex_color_map.Create();
  112. state.proctex_color_map.texture_1d = proctex_color_map.handle;
  113. state.Apply();
  114. glActiveTexture(GL_TEXTURE11);
  115. glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr);
  116. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  117. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  118. // Setup the alpha map for proctex
  119. proctex_alpha_map.Create();
  120. state.proctex_alpha_map.texture_1d = proctex_alpha_map.handle;
  121. state.Apply();
  122. glActiveTexture(GL_TEXTURE12);
  123. glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr);
  124. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  125. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  126. // Setup the LUT for proctex
  127. proctex_lut.Create();
  128. state.proctex_lut.texture_1d = proctex_lut.handle;
  129. state.Apply();
  130. glActiveTexture(GL_TEXTURE13);
  131. glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr);
  132. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  133. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  134. // Setup the difference LUT for proctex
  135. proctex_diff_lut.Create();
  136. state.proctex_diff_lut.texture_1d = proctex_diff_lut.handle;
  137. state.Apply();
  138. glActiveTexture(GL_TEXTURE14);
  139. glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr);
  140. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  141. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  142. // Sync fixed function OpenGL state
  143. SyncCullMode();
  144. SyncBlendEnabled();
  145. SyncBlendFuncs();
  146. SyncBlendColor();
  147. SyncLogicOp();
  148. SyncStencilTest();
  149. SyncDepthTest();
  150. SyncColorWriteMask();
  151. SyncStencilWriteMask();
  152. SyncDepthWriteMask();
  153. }
  154. RasterizerOpenGL::~RasterizerOpenGL() {}
  155. /**
  156. * This is a helper function to resolve an issue with opposite quaternions being interpolated by
  157. * OpenGL. See below for a detailed description of this issue (yuriks):
  158. *
  159. * For any rotation, there are two quaternions Q, and -Q, that represent the same rotation. If you
  160. * interpolate two quaternions that are opposite, instead of going from one rotation to another
  161. * using the shortest path, you'll go around the longest path. You can test if two quaternions are
  162. * opposite by checking if Dot(Q1, W2) < 0. In that case, you can flip either of them, therefore
  163. * making Dot(-Q1, W2) positive.
  164. *
  165. * NOTE: This solution corrects this issue per-vertex before passing the quaternions to OpenGL. This
  166. * should be correct for nearly all cases, however a more correct implementation (but less trivial
  167. * and perhaps unnecessary) would be to handle this per-fragment, by interpolating the quaternions
  168. * manually using two Lerps, and doing this correction before each Lerp.
  169. */
  170. static bool AreQuaternionsOpposite(Math::Vec4<Pica::float24> qa, Math::Vec4<Pica::float24> qb) {
  171. Math::Vec4f a{qa.x.ToFloat32(), qa.y.ToFloat32(), qa.z.ToFloat32(), qa.w.ToFloat32()};
  172. Math::Vec4f b{qb.x.ToFloat32(), qb.y.ToFloat32(), qb.z.ToFloat32(), qb.w.ToFloat32()};
  173. return (Math::Dot(a, b) < 0.f);
  174. }
  175. void RasterizerOpenGL::AddTriangle(const Pica::Shader::OutputVertex& v0,
  176. const Pica::Shader::OutputVertex& v1,
  177. const Pica::Shader::OutputVertex& v2) {
  178. vertex_batch.emplace_back(v0, false);
  179. vertex_batch.emplace_back(v1, AreQuaternionsOpposite(v0.quat, v1.quat));
  180. vertex_batch.emplace_back(v2, AreQuaternionsOpposite(v0.quat, v2.quat));
  181. }
  182. void RasterizerOpenGL::DrawTriangles() {
  183. if (vertex_batch.empty())
  184. return;
  185. MICROPROFILE_SCOPE(OpenGL_Drawing);
  186. const auto& regs = Pica::g_state.regs;
  187. // Sync and bind the framebuffer surfaces
  188. CachedSurface* color_surface;
  189. CachedSurface* depth_surface;
  190. MathUtil::Rectangle<int> rect;
  191. std::tie(color_surface, depth_surface, rect) =
  192. res_cache.GetFramebufferSurfaces(regs.framebuffer.framebuffer);
  193. state.draw.draw_framebuffer = framebuffer.handle;
  194. state.Apply();
  195. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  196. color_surface != nullptr ? color_surface->texture.handle : 0, 0);
  197. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
  198. depth_surface != nullptr ? depth_surface->texture.handle : 0, 0);
  199. bool has_stencil =
  200. regs.framebuffer.framebuffer.depth_format == Pica::FramebufferRegs::DepthFormat::D24S8;
  201. glFramebufferTexture2D(
  202. GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
  203. (has_stencil && depth_surface != nullptr) ? depth_surface->texture.handle : 0, 0);
  204. // Sync the viewport
  205. // These registers hold half-width and half-height, so must be multiplied by 2
  206. GLsizei viewport_width =
  207. (GLsizei)Pica::float24::FromRaw(regs.rasterizer.viewport_size_x).ToFloat32() * 2;
  208. GLsizei viewport_height =
  209. (GLsizei)Pica::float24::FromRaw(regs.rasterizer.viewport_size_y).ToFloat32() * 2;
  210. glViewport(
  211. (GLint)(rect.left + regs.rasterizer.viewport_corner.x * color_surface->res_scale_width),
  212. (GLint)(rect.bottom + regs.rasterizer.viewport_corner.y * color_surface->res_scale_height),
  213. (GLsizei)(viewport_width * color_surface->res_scale_width),
  214. (GLsizei)(viewport_height * color_surface->res_scale_height));
  215. if (uniform_block_data.data.framebuffer_scale[0] != color_surface->res_scale_width ||
  216. uniform_block_data.data.framebuffer_scale[1] != color_surface->res_scale_height) {
  217. uniform_block_data.data.framebuffer_scale[0] = color_surface->res_scale_width;
  218. uniform_block_data.data.framebuffer_scale[1] = color_surface->res_scale_height;
  219. uniform_block_data.dirty = true;
  220. }
  221. // Scissor checks are window-, not viewport-relative, which means that if the cached texture
  222. // sub-rect changes, the scissor bounds also need to be updated.
  223. GLint scissor_x1 = static_cast<GLint>(
  224. rect.left + regs.rasterizer.scissor_test.x1 * color_surface->res_scale_width);
  225. GLint scissor_y1 = static_cast<GLint>(
  226. rect.bottom + regs.rasterizer.scissor_test.y1 * color_surface->res_scale_height);
  227. // x2, y2 have +1 added to cover the entire pixel area, otherwise you might get cracks when
  228. // scaling or doing multisampling.
  229. GLint scissor_x2 = static_cast<GLint>(
  230. rect.left + (regs.rasterizer.scissor_test.x2 + 1) * color_surface->res_scale_width);
  231. GLint scissor_y2 = static_cast<GLint>(
  232. rect.bottom + (regs.rasterizer.scissor_test.y2 + 1) * color_surface->res_scale_height);
  233. if (uniform_block_data.data.scissor_x1 != scissor_x1 ||
  234. uniform_block_data.data.scissor_x2 != scissor_x2 ||
  235. uniform_block_data.data.scissor_y1 != scissor_y1 ||
  236. uniform_block_data.data.scissor_y2 != scissor_y2) {
  237. uniform_block_data.data.scissor_x1 = scissor_x1;
  238. uniform_block_data.data.scissor_x2 = scissor_x2;
  239. uniform_block_data.data.scissor_y1 = scissor_y1;
  240. uniform_block_data.data.scissor_y2 = scissor_y2;
  241. uniform_block_data.dirty = true;
  242. }
  243. // Sync and bind the texture surfaces
  244. const auto pica_textures = regs.texturing.GetTextures();
  245. for (unsigned texture_index = 0; texture_index < pica_textures.size(); ++texture_index) {
  246. const auto& texture = pica_textures[texture_index];
  247. if (texture.enabled) {
  248. texture_samplers[texture_index].SyncWithConfig(texture.config);
  249. CachedSurface* surface = res_cache.GetTextureSurface(texture);
  250. if (surface != nullptr) {
  251. state.texture_units[texture_index].texture_2d = surface->texture.handle;
  252. } else {
  253. // Can occur when texture addr is null or its memory is unmapped/invalid
  254. state.texture_units[texture_index].texture_2d = 0;
  255. }
  256. } else {
  257. state.texture_units[texture_index].texture_2d = 0;
  258. }
  259. }
  260. // Sync and bind the shader
  261. if (shader_dirty) {
  262. SetShader();
  263. shader_dirty = false;
  264. }
  265. // Sync the lighting luts
  266. for (unsigned index = 0; index < lighting_luts.size(); index++) {
  267. if (uniform_block_data.lut_dirty[index]) {
  268. SyncLightingLUT(index);
  269. uniform_block_data.lut_dirty[index] = false;
  270. }
  271. }
  272. // Sync the fog lut
  273. if (uniform_block_data.fog_lut_dirty) {
  274. SyncFogLUT();
  275. uniform_block_data.fog_lut_dirty = false;
  276. }
  277. // Sync the proctex noise lut
  278. if (uniform_block_data.proctex_noise_lut_dirty) {
  279. SyncProcTexNoiseLUT();
  280. uniform_block_data.proctex_noise_lut_dirty = false;
  281. }
  282. // Sync the proctex color map
  283. if (uniform_block_data.proctex_color_map_dirty) {
  284. SyncProcTexColorMap();
  285. uniform_block_data.proctex_color_map_dirty = false;
  286. }
  287. // Sync the proctex alpha map
  288. if (uniform_block_data.proctex_alpha_map_dirty) {
  289. SyncProcTexAlphaMap();
  290. uniform_block_data.proctex_alpha_map_dirty = false;
  291. }
  292. // Sync the proctex lut
  293. if (uniform_block_data.proctex_lut_dirty) {
  294. SyncProcTexLUT();
  295. uniform_block_data.proctex_lut_dirty = false;
  296. }
  297. // Sync the proctex difference lut
  298. if (uniform_block_data.proctex_diff_lut_dirty) {
  299. SyncProcTexDiffLUT();
  300. uniform_block_data.proctex_diff_lut_dirty = false;
  301. }
  302. // Sync the uniform data
  303. if (uniform_block_data.dirty) {
  304. glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformData), &uniform_block_data.data,
  305. GL_STATIC_DRAW);
  306. uniform_block_data.dirty = false;
  307. }
  308. state.Apply();
  309. // Draw the vertex batch
  310. glBufferData(GL_ARRAY_BUFFER, vertex_batch.size() * sizeof(HardwareVertex), vertex_batch.data(),
  311. GL_STREAM_DRAW);
  312. glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertex_batch.size());
  313. // Mark framebuffer surfaces as dirty
  314. // TODO: Restrict invalidation area to the viewport
  315. if (color_surface != nullptr) {
  316. color_surface->dirty = true;
  317. res_cache.FlushRegion(color_surface->addr, color_surface->size, color_surface, true);
  318. }
  319. if (depth_surface != nullptr) {
  320. depth_surface->dirty = true;
  321. res_cache.FlushRegion(depth_surface->addr, depth_surface->size, depth_surface, true);
  322. }
  323. vertex_batch.clear();
  324. // Unbind textures for potential future use as framebuffer attachments
  325. for (unsigned texture_index = 0; texture_index < pica_textures.size(); ++texture_index) {
  326. state.texture_units[texture_index].texture_2d = 0;
  327. }
  328. state.Apply();
  329. }
  330. void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
  331. const auto& regs = Pica::g_state.regs;
  332. switch (id) {
  333. // Culling
  334. case PICA_REG_INDEX(rasterizer.cull_mode):
  335. SyncCullMode();
  336. break;
  337. // Depth modifiers
  338. case PICA_REG_INDEX(rasterizer.viewport_depth_range):
  339. SyncDepthScale();
  340. break;
  341. case PICA_REG_INDEX(rasterizer.viewport_depth_near_plane):
  342. SyncDepthOffset();
  343. break;
  344. // Depth buffering
  345. case PICA_REG_INDEX(rasterizer.depthmap_enable):
  346. shader_dirty = true;
  347. break;
  348. // Blending
  349. case PICA_REG_INDEX(framebuffer.output_merger.alphablend_enable):
  350. SyncBlendEnabled();
  351. break;
  352. case PICA_REG_INDEX(framebuffer.output_merger.alpha_blending):
  353. SyncBlendFuncs();
  354. break;
  355. case PICA_REG_INDEX(framebuffer.output_merger.blend_const):
  356. SyncBlendColor();
  357. break;
  358. // Fog state
  359. case PICA_REG_INDEX(texturing.fog_color):
  360. SyncFogColor();
  361. break;
  362. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[0], 0xe8):
  363. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[1], 0xe9):
  364. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[2], 0xea):
  365. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[3], 0xeb):
  366. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[4], 0xec):
  367. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[5], 0xed):
  368. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[6], 0xee):
  369. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[7], 0xef):
  370. uniform_block_data.fog_lut_dirty = true;
  371. break;
  372. // ProcTex state
  373. case PICA_REG_INDEX(texturing.proctex):
  374. case PICA_REG_INDEX(texturing.proctex_lut):
  375. case PICA_REG_INDEX(texturing.proctex_lut_offset):
  376. shader_dirty = true;
  377. break;
  378. case PICA_REG_INDEX(texturing.proctex_noise_u):
  379. case PICA_REG_INDEX(texturing.proctex_noise_v):
  380. case PICA_REG_INDEX(texturing.proctex_noise_frequency):
  381. SyncProcTexNoise();
  382. break;
  383. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[0], 0xb0):
  384. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[1], 0xb1):
  385. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[2], 0xb2):
  386. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[3], 0xb3):
  387. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[4], 0xb4):
  388. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[5], 0xb5):
  389. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[6], 0xb6):
  390. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[7], 0xb7):
  391. using Pica::TexturingRegs;
  392. switch (regs.texturing.proctex_lut_config.ref_table.Value()) {
  393. case TexturingRegs::ProcTexLutTable::Noise:
  394. uniform_block_data.proctex_noise_lut_dirty = true;
  395. break;
  396. case TexturingRegs::ProcTexLutTable::ColorMap:
  397. uniform_block_data.proctex_color_map_dirty = true;
  398. break;
  399. case TexturingRegs::ProcTexLutTable::AlphaMap:
  400. uniform_block_data.proctex_alpha_map_dirty = true;
  401. break;
  402. case TexturingRegs::ProcTexLutTable::Color:
  403. uniform_block_data.proctex_lut_dirty = true;
  404. break;
  405. case TexturingRegs::ProcTexLutTable::ColorDiff:
  406. uniform_block_data.proctex_diff_lut_dirty = true;
  407. break;
  408. }
  409. break;
  410. // Alpha test
  411. case PICA_REG_INDEX(framebuffer.output_merger.alpha_test):
  412. SyncAlphaTest();
  413. shader_dirty = true;
  414. break;
  415. // Sync GL stencil test + stencil write mask
  416. // (Pica stencil test function register also contains a stencil write mask)
  417. case PICA_REG_INDEX(framebuffer.output_merger.stencil_test.raw_func):
  418. SyncStencilTest();
  419. SyncStencilWriteMask();
  420. break;
  421. case PICA_REG_INDEX(framebuffer.output_merger.stencil_test.raw_op):
  422. case PICA_REG_INDEX(framebuffer.framebuffer.depth_format):
  423. SyncStencilTest();
  424. break;
  425. // Sync GL depth test + depth and color write mask
  426. // (Pica depth test function register also contains a depth and color write mask)
  427. case PICA_REG_INDEX(framebuffer.output_merger.depth_test_enable):
  428. SyncDepthTest();
  429. SyncDepthWriteMask();
  430. SyncColorWriteMask();
  431. break;
  432. // Sync GL depth and stencil write mask
  433. // (This is a dedicated combined depth / stencil write-enable register)
  434. case PICA_REG_INDEX(framebuffer.framebuffer.allow_depth_stencil_write):
  435. SyncDepthWriteMask();
  436. SyncStencilWriteMask();
  437. break;
  438. // Sync GL color write mask
  439. // (This is a dedicated color write-enable register)
  440. case PICA_REG_INDEX(framebuffer.framebuffer.allow_color_write):
  441. SyncColorWriteMask();
  442. break;
  443. // Scissor test
  444. case PICA_REG_INDEX(rasterizer.scissor_test.mode):
  445. shader_dirty = true;
  446. break;
  447. // Logic op
  448. case PICA_REG_INDEX(framebuffer.output_merger.logic_op):
  449. SyncLogicOp();
  450. break;
  451. case PICA_REG_INDEX(texturing.main_config):
  452. shader_dirty = true;
  453. break;
  454. // Texture 0 type
  455. case PICA_REG_INDEX(texturing.texture0.type):
  456. shader_dirty = true;
  457. break;
  458. // TEV stages
  459. // (This also syncs fog_mode and fog_flip which are part of tev_combiner_buffer_input)
  460. case PICA_REG_INDEX(texturing.tev_stage0.color_source1):
  461. case PICA_REG_INDEX(texturing.tev_stage0.color_modifier1):
  462. case PICA_REG_INDEX(texturing.tev_stage0.color_op):
  463. case PICA_REG_INDEX(texturing.tev_stage0.color_scale):
  464. case PICA_REG_INDEX(texturing.tev_stage1.color_source1):
  465. case PICA_REG_INDEX(texturing.tev_stage1.color_modifier1):
  466. case PICA_REG_INDEX(texturing.tev_stage1.color_op):
  467. case PICA_REG_INDEX(texturing.tev_stage1.color_scale):
  468. case PICA_REG_INDEX(texturing.tev_stage2.color_source1):
  469. case PICA_REG_INDEX(texturing.tev_stage2.color_modifier1):
  470. case PICA_REG_INDEX(texturing.tev_stage2.color_op):
  471. case PICA_REG_INDEX(texturing.tev_stage2.color_scale):
  472. case PICA_REG_INDEX(texturing.tev_stage3.color_source1):
  473. case PICA_REG_INDEX(texturing.tev_stage3.color_modifier1):
  474. case PICA_REG_INDEX(texturing.tev_stage3.color_op):
  475. case PICA_REG_INDEX(texturing.tev_stage3.color_scale):
  476. case PICA_REG_INDEX(texturing.tev_stage4.color_source1):
  477. case PICA_REG_INDEX(texturing.tev_stage4.color_modifier1):
  478. case PICA_REG_INDEX(texturing.tev_stage4.color_op):
  479. case PICA_REG_INDEX(texturing.tev_stage4.color_scale):
  480. case PICA_REG_INDEX(texturing.tev_stage5.color_source1):
  481. case PICA_REG_INDEX(texturing.tev_stage5.color_modifier1):
  482. case PICA_REG_INDEX(texturing.tev_stage5.color_op):
  483. case PICA_REG_INDEX(texturing.tev_stage5.color_scale):
  484. case PICA_REG_INDEX(texturing.tev_combiner_buffer_input):
  485. shader_dirty = true;
  486. break;
  487. case PICA_REG_INDEX(texturing.tev_stage0.const_r):
  488. SyncTevConstColor(0, regs.texturing.tev_stage0);
  489. break;
  490. case PICA_REG_INDEX(texturing.tev_stage1.const_r):
  491. SyncTevConstColor(1, regs.texturing.tev_stage1);
  492. break;
  493. case PICA_REG_INDEX(texturing.tev_stage2.const_r):
  494. SyncTevConstColor(2, regs.texturing.tev_stage2);
  495. break;
  496. case PICA_REG_INDEX(texturing.tev_stage3.const_r):
  497. SyncTevConstColor(3, regs.texturing.tev_stage3);
  498. break;
  499. case PICA_REG_INDEX(texturing.tev_stage4.const_r):
  500. SyncTevConstColor(4, regs.texturing.tev_stage4);
  501. break;
  502. case PICA_REG_INDEX(texturing.tev_stage5.const_r):
  503. SyncTevConstColor(5, regs.texturing.tev_stage5);
  504. break;
  505. // TEV combiner buffer color
  506. case PICA_REG_INDEX(texturing.tev_combiner_buffer_color):
  507. SyncCombinerColor();
  508. break;
  509. // Fragment lighting switches
  510. case PICA_REG_INDEX(lighting.disable):
  511. case PICA_REG_INDEX(lighting.max_light_index):
  512. case PICA_REG_INDEX(lighting.config0):
  513. case PICA_REG_INDEX(lighting.config1):
  514. case PICA_REG_INDEX(lighting.abs_lut_input):
  515. case PICA_REG_INDEX(lighting.lut_input):
  516. case PICA_REG_INDEX(lighting.lut_scale):
  517. case PICA_REG_INDEX(lighting.light_enable):
  518. break;
  519. // Fragment lighting specular 0 color
  520. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].specular_0, 0x140 + 0 * 0x10):
  521. SyncLightSpecular0(0);
  522. break;
  523. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].specular_0, 0x140 + 1 * 0x10):
  524. SyncLightSpecular0(1);
  525. break;
  526. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].specular_0, 0x140 + 2 * 0x10):
  527. SyncLightSpecular0(2);
  528. break;
  529. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].specular_0, 0x140 + 3 * 0x10):
  530. SyncLightSpecular0(3);
  531. break;
  532. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].specular_0, 0x140 + 4 * 0x10):
  533. SyncLightSpecular0(4);
  534. break;
  535. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].specular_0, 0x140 + 5 * 0x10):
  536. SyncLightSpecular0(5);
  537. break;
  538. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].specular_0, 0x140 + 6 * 0x10):
  539. SyncLightSpecular0(6);
  540. break;
  541. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].specular_0, 0x140 + 7 * 0x10):
  542. SyncLightSpecular0(7);
  543. break;
  544. // Fragment lighting specular 1 color
  545. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].specular_1, 0x141 + 0 * 0x10):
  546. SyncLightSpecular1(0);
  547. break;
  548. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].specular_1, 0x141 + 1 * 0x10):
  549. SyncLightSpecular1(1);
  550. break;
  551. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].specular_1, 0x141 + 2 * 0x10):
  552. SyncLightSpecular1(2);
  553. break;
  554. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].specular_1, 0x141 + 3 * 0x10):
  555. SyncLightSpecular1(3);
  556. break;
  557. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].specular_1, 0x141 + 4 * 0x10):
  558. SyncLightSpecular1(4);
  559. break;
  560. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].specular_1, 0x141 + 5 * 0x10):
  561. SyncLightSpecular1(5);
  562. break;
  563. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].specular_1, 0x141 + 6 * 0x10):
  564. SyncLightSpecular1(6);
  565. break;
  566. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].specular_1, 0x141 + 7 * 0x10):
  567. SyncLightSpecular1(7);
  568. break;
  569. // Fragment lighting diffuse color
  570. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].diffuse, 0x142 + 0 * 0x10):
  571. SyncLightDiffuse(0);
  572. break;
  573. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].diffuse, 0x142 + 1 * 0x10):
  574. SyncLightDiffuse(1);
  575. break;
  576. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].diffuse, 0x142 + 2 * 0x10):
  577. SyncLightDiffuse(2);
  578. break;
  579. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].diffuse, 0x142 + 3 * 0x10):
  580. SyncLightDiffuse(3);
  581. break;
  582. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].diffuse, 0x142 + 4 * 0x10):
  583. SyncLightDiffuse(4);
  584. break;
  585. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].diffuse, 0x142 + 5 * 0x10):
  586. SyncLightDiffuse(5);
  587. break;
  588. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].diffuse, 0x142 + 6 * 0x10):
  589. SyncLightDiffuse(6);
  590. break;
  591. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].diffuse, 0x142 + 7 * 0x10):
  592. SyncLightDiffuse(7);
  593. break;
  594. // Fragment lighting ambient color
  595. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].ambient, 0x143 + 0 * 0x10):
  596. SyncLightAmbient(0);
  597. break;
  598. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].ambient, 0x143 + 1 * 0x10):
  599. SyncLightAmbient(1);
  600. break;
  601. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].ambient, 0x143 + 2 * 0x10):
  602. SyncLightAmbient(2);
  603. break;
  604. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].ambient, 0x143 + 3 * 0x10):
  605. SyncLightAmbient(3);
  606. break;
  607. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].ambient, 0x143 + 4 * 0x10):
  608. SyncLightAmbient(4);
  609. break;
  610. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].ambient, 0x143 + 5 * 0x10):
  611. SyncLightAmbient(5);
  612. break;
  613. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].ambient, 0x143 + 6 * 0x10):
  614. SyncLightAmbient(6);
  615. break;
  616. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].ambient, 0x143 + 7 * 0x10):
  617. SyncLightAmbient(7);
  618. break;
  619. // Fragment lighting position
  620. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].x, 0x144 + 0 * 0x10):
  621. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].z, 0x145 + 0 * 0x10):
  622. SyncLightPosition(0);
  623. break;
  624. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].x, 0x144 + 1 * 0x10):
  625. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].z, 0x145 + 1 * 0x10):
  626. SyncLightPosition(1);
  627. break;
  628. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].x, 0x144 + 2 * 0x10):
  629. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].z, 0x145 + 2 * 0x10):
  630. SyncLightPosition(2);
  631. break;
  632. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].x, 0x144 + 3 * 0x10):
  633. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].z, 0x145 + 3 * 0x10):
  634. SyncLightPosition(3);
  635. break;
  636. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].x, 0x144 + 4 * 0x10):
  637. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].z, 0x145 + 4 * 0x10):
  638. SyncLightPosition(4);
  639. break;
  640. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].x, 0x144 + 5 * 0x10):
  641. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].z, 0x145 + 5 * 0x10):
  642. SyncLightPosition(5);
  643. break;
  644. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].x, 0x144 + 6 * 0x10):
  645. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].z, 0x145 + 6 * 0x10):
  646. SyncLightPosition(6);
  647. break;
  648. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].x, 0x144 + 7 * 0x10):
  649. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].z, 0x145 + 7 * 0x10):
  650. SyncLightPosition(7);
  651. break;
  652. // Fragment lighting light source config
  653. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].config, 0x149 + 0 * 0x10):
  654. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].config, 0x149 + 1 * 0x10):
  655. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].config, 0x149 + 2 * 0x10):
  656. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].config, 0x149 + 3 * 0x10):
  657. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].config, 0x149 + 4 * 0x10):
  658. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].config, 0x149 + 5 * 0x10):
  659. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].config, 0x149 + 6 * 0x10):
  660. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].config, 0x149 + 7 * 0x10):
  661. shader_dirty = true;
  662. break;
  663. // Fragment lighting distance attenuation bias
  664. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].dist_atten_bias, 0x014A + 0 * 0x10):
  665. SyncLightDistanceAttenuationBias(0);
  666. break;
  667. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].dist_atten_bias, 0x014A + 1 * 0x10):
  668. SyncLightDistanceAttenuationBias(1);
  669. break;
  670. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].dist_atten_bias, 0x014A + 2 * 0x10):
  671. SyncLightDistanceAttenuationBias(2);
  672. break;
  673. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].dist_atten_bias, 0x014A + 3 * 0x10):
  674. SyncLightDistanceAttenuationBias(3);
  675. break;
  676. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].dist_atten_bias, 0x014A + 4 * 0x10):
  677. SyncLightDistanceAttenuationBias(4);
  678. break;
  679. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].dist_atten_bias, 0x014A + 5 * 0x10):
  680. SyncLightDistanceAttenuationBias(5);
  681. break;
  682. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].dist_atten_bias, 0x014A + 6 * 0x10):
  683. SyncLightDistanceAttenuationBias(6);
  684. break;
  685. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].dist_atten_bias, 0x014A + 7 * 0x10):
  686. SyncLightDistanceAttenuationBias(7);
  687. break;
  688. // Fragment lighting distance attenuation scale
  689. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].dist_atten_scale, 0x014B + 0 * 0x10):
  690. SyncLightDistanceAttenuationScale(0);
  691. break;
  692. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].dist_atten_scale, 0x014B + 1 * 0x10):
  693. SyncLightDistanceAttenuationScale(1);
  694. break;
  695. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].dist_atten_scale, 0x014B + 2 * 0x10):
  696. SyncLightDistanceAttenuationScale(2);
  697. break;
  698. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].dist_atten_scale, 0x014B + 3 * 0x10):
  699. SyncLightDistanceAttenuationScale(3);
  700. break;
  701. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].dist_atten_scale, 0x014B + 4 * 0x10):
  702. SyncLightDistanceAttenuationScale(4);
  703. break;
  704. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].dist_atten_scale, 0x014B + 5 * 0x10):
  705. SyncLightDistanceAttenuationScale(5);
  706. break;
  707. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].dist_atten_scale, 0x014B + 6 * 0x10):
  708. SyncLightDistanceAttenuationScale(6);
  709. break;
  710. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].dist_atten_scale, 0x014B + 7 * 0x10):
  711. SyncLightDistanceAttenuationScale(7);
  712. break;
  713. // Fragment lighting global ambient color (emission + ambient * ambient)
  714. case PICA_REG_INDEX_WORKAROUND(lighting.global_ambient, 0x1c0):
  715. SyncGlobalAmbient();
  716. break;
  717. // Fragment lighting lookup tables
  718. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[0], 0x1c8):
  719. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[1], 0x1c9):
  720. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[2], 0x1ca):
  721. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[3], 0x1cb):
  722. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[4], 0x1cc):
  723. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[5], 0x1cd):
  724. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[6], 0x1ce):
  725. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[7], 0x1cf): {
  726. auto& lut_config = regs.lighting.lut_config;
  727. uniform_block_data.lut_dirty[lut_config.type / 4] = true;
  728. break;
  729. }
  730. }
  731. }
  732. void RasterizerOpenGL::FlushAll() {
  733. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  734. res_cache.FlushAll();
  735. }
  736. void RasterizerOpenGL::FlushRegion(PAddr addr, u32 size) {
  737. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  738. res_cache.FlushRegion(addr, size, nullptr, false);
  739. }
  740. void RasterizerOpenGL::FlushAndInvalidateRegion(PAddr addr, u32 size) {
  741. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  742. res_cache.FlushRegion(addr, size, nullptr, true);
  743. }
  744. bool RasterizerOpenGL::AccelerateDisplayTransfer(const GPU::Regs::DisplayTransferConfig& config) {
  745. MICROPROFILE_SCOPE(OpenGL_Blits);
  746. CachedSurface src_params;
  747. src_params.addr = config.GetPhysicalInputAddress();
  748. // It's important to use the correct source input width to properly skip over parts of the input
  749. // image which will be cropped from the output but still affect the stride of the input image.
  750. src_params.width = config.input_width;
  751. // Using the output's height is fine because we don't read or skip over the remaining part of
  752. // the image, and it allows for smaller texture cache lookup rectangles.
  753. src_params.height = config.output_height;
  754. src_params.is_tiled = !config.input_linear;
  755. src_params.pixel_format = CachedSurface::PixelFormatFromGPUPixelFormat(config.input_format);
  756. CachedSurface dst_params;
  757. dst_params.addr = config.GetPhysicalOutputAddress();
  758. dst_params.width =
  759. config.scaling != config.NoScale ? config.output_width / 2 : config.output_width.Value();
  760. dst_params.height =
  761. config.scaling == config.ScaleXY ? config.output_height / 2 : config.output_height.Value();
  762. dst_params.is_tiled = config.input_linear != config.dont_swizzle;
  763. dst_params.pixel_format = CachedSurface::PixelFormatFromGPUPixelFormat(config.output_format);
  764. MathUtil::Rectangle<int> src_rect;
  765. CachedSurface* src_surface = res_cache.GetSurfaceRect(src_params, false, true, src_rect);
  766. if (src_surface == nullptr) {
  767. return false;
  768. }
  769. // Adjust the source rectangle to take into account parts of the input lines being cropped
  770. if (config.input_width > config.output_width) {
  771. src_rect.right -= static_cast<int>((config.input_width - config.output_width) *
  772. src_surface->res_scale_width);
  773. }
  774. // Require destination surface to have same resolution scale as source to preserve scaling
  775. dst_params.res_scale_width = src_surface->res_scale_width;
  776. dst_params.res_scale_height = src_surface->res_scale_height;
  777. MathUtil::Rectangle<int> dst_rect;
  778. CachedSurface* dst_surface = res_cache.GetSurfaceRect(dst_params, true, false, dst_rect);
  779. if (dst_surface == nullptr) {
  780. return false;
  781. }
  782. // Don't accelerate if the src and dst surfaces are the same
  783. if (src_surface == dst_surface) {
  784. return false;
  785. }
  786. if (config.flip_vertically) {
  787. std::swap(dst_rect.top, dst_rect.bottom);
  788. }
  789. if (!res_cache.TryBlitSurfaces(src_surface, src_rect, dst_surface, dst_rect)) {
  790. return false;
  791. }
  792. u32 dst_size = dst_params.width * dst_params.height *
  793. CachedSurface::GetFormatBpp(dst_params.pixel_format) / 8;
  794. dst_surface->dirty = true;
  795. res_cache.FlushRegion(config.GetPhysicalOutputAddress(), dst_size, dst_surface, true);
  796. return true;
  797. }
  798. bool RasterizerOpenGL::AccelerateTextureCopy(const GPU::Regs::DisplayTransferConfig& config) {
  799. // TODO(tfarley): Try to hardware accelerate this
  800. return false;
  801. }
  802. bool RasterizerOpenGL::AccelerateFill(const GPU::Regs::MemoryFillConfig& config) {
  803. MICROPROFILE_SCOPE(OpenGL_Blits);
  804. using PixelFormat = CachedSurface::PixelFormat;
  805. using SurfaceType = CachedSurface::SurfaceType;
  806. CachedSurface* dst_surface = res_cache.TryGetFillSurface(config);
  807. if (dst_surface == nullptr) {
  808. return false;
  809. }
  810. OpenGLState cur_state = OpenGLState::GetCurState();
  811. SurfaceType dst_type = CachedSurface::GetFormatType(dst_surface->pixel_format);
  812. GLuint old_fb = cur_state.draw.draw_framebuffer;
  813. cur_state.draw.draw_framebuffer = framebuffer.handle;
  814. // TODO: When scissor test is implemented, need to disable scissor test in cur_state here so
  815. // Clear call isn't affected
  816. cur_state.Apply();
  817. if (dst_type == SurfaceType::Color || dst_type == SurfaceType::Texture) {
  818. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  819. dst_surface->texture.handle, 0);
  820. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0,
  821. 0);
  822. GLfloat color_values[4] = {0.0f, 0.0f, 0.0f, 0.0f};
  823. // TODO: Handle additional pixel format and fill value size combinations to accelerate more
  824. // cases
  825. // For instance, checking if fill value's bytes/bits repeat to allow filling
  826. // I8/A8/I4/A4/...
  827. // Currently only handles formats that are multiples of the fill value size
  828. if (config.fill_24bit) {
  829. switch (dst_surface->pixel_format) {
  830. case PixelFormat::RGB8:
  831. color_values[0] = config.value_24bit_r / 255.0f;
  832. color_values[1] = config.value_24bit_g / 255.0f;
  833. color_values[2] = config.value_24bit_b / 255.0f;
  834. break;
  835. default:
  836. return false;
  837. }
  838. } else if (config.fill_32bit) {
  839. u32 value = config.value_32bit;
  840. switch (dst_surface->pixel_format) {
  841. case PixelFormat::RGBA8:
  842. color_values[0] = (value >> 24) / 255.0f;
  843. color_values[1] = ((value >> 16) & 0xFF) / 255.0f;
  844. color_values[2] = ((value >> 8) & 0xFF) / 255.0f;
  845. color_values[3] = (value & 0xFF) / 255.0f;
  846. break;
  847. default:
  848. return false;
  849. }
  850. } else {
  851. u16 value_16bit = config.value_16bit.Value();
  852. Math::Vec4<u8> color;
  853. switch (dst_surface->pixel_format) {
  854. case PixelFormat::RGBA8:
  855. color_values[0] = (value_16bit >> 8) / 255.0f;
  856. color_values[1] = (value_16bit & 0xFF) / 255.0f;
  857. color_values[2] = color_values[0];
  858. color_values[3] = color_values[1];
  859. break;
  860. case PixelFormat::RGB5A1:
  861. color = Color::DecodeRGB5A1((const u8*)&value_16bit);
  862. color_values[0] = color[0] / 31.0f;
  863. color_values[1] = color[1] / 31.0f;
  864. color_values[2] = color[2] / 31.0f;
  865. color_values[3] = color[3];
  866. break;
  867. case PixelFormat::RGB565:
  868. color = Color::DecodeRGB565((const u8*)&value_16bit);
  869. color_values[0] = color[0] / 31.0f;
  870. color_values[1] = color[1] / 63.0f;
  871. color_values[2] = color[2] / 31.0f;
  872. break;
  873. case PixelFormat::RGBA4:
  874. color = Color::DecodeRGBA4((const u8*)&value_16bit);
  875. color_values[0] = color[0] / 15.0f;
  876. color_values[1] = color[1] / 15.0f;
  877. color_values[2] = color[2] / 15.0f;
  878. color_values[3] = color[3] / 15.0f;
  879. break;
  880. case PixelFormat::IA8:
  881. case PixelFormat::RG8:
  882. color_values[0] = (value_16bit >> 8) / 255.0f;
  883. color_values[1] = (value_16bit & 0xFF) / 255.0f;
  884. break;
  885. default:
  886. return false;
  887. }
  888. }
  889. cur_state.color_mask.red_enabled = GL_TRUE;
  890. cur_state.color_mask.green_enabled = GL_TRUE;
  891. cur_state.color_mask.blue_enabled = GL_TRUE;
  892. cur_state.color_mask.alpha_enabled = GL_TRUE;
  893. cur_state.Apply();
  894. glClearBufferfv(GL_COLOR, 0, color_values);
  895. } else if (dst_type == SurfaceType::Depth) {
  896. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
  897. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
  898. dst_surface->texture.handle, 0);
  899. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  900. GLfloat value_float;
  901. if (dst_surface->pixel_format == CachedSurface::PixelFormat::D16) {
  902. value_float = config.value_32bit / 65535.0f; // 2^16 - 1
  903. } else if (dst_surface->pixel_format == CachedSurface::PixelFormat::D24) {
  904. value_float = config.value_32bit / 16777215.0f; // 2^24 - 1
  905. }
  906. cur_state.depth.write_mask = GL_TRUE;
  907. cur_state.Apply();
  908. glClearBufferfv(GL_DEPTH, 0, &value_float);
  909. } else if (dst_type == SurfaceType::DepthStencil) {
  910. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
  911. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
  912. dst_surface->texture.handle, 0);
  913. GLfloat value_float = (config.value_32bit & 0xFFFFFF) / 16777215.0f; // 2^24 - 1
  914. GLint value_int = (config.value_32bit >> 24);
  915. cur_state.depth.write_mask = GL_TRUE;
  916. cur_state.stencil.write_mask = 0xFF;
  917. cur_state.Apply();
  918. glClearBufferfi(GL_DEPTH_STENCIL, 0, value_float, value_int);
  919. }
  920. cur_state.draw.draw_framebuffer = old_fb;
  921. // TODO: Return scissor test to previous value when scissor test is implemented
  922. cur_state.Apply();
  923. dst_surface->dirty = true;
  924. res_cache.FlushRegion(dst_surface->addr, dst_surface->size, dst_surface, true);
  925. return true;
  926. }
  927. bool RasterizerOpenGL::AccelerateDisplay(const GPU::Regs::FramebufferConfig& config,
  928. PAddr framebuffer_addr, u32 pixel_stride,
  929. ScreenInfo& screen_info) {
  930. if (framebuffer_addr == 0) {
  931. return false;
  932. }
  933. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  934. CachedSurface src_params;
  935. src_params.addr = framebuffer_addr;
  936. src_params.width = config.width;
  937. src_params.height = config.height;
  938. src_params.pixel_stride = pixel_stride;
  939. src_params.is_tiled = false;
  940. src_params.pixel_format = CachedSurface::PixelFormatFromGPUPixelFormat(config.color_format);
  941. MathUtil::Rectangle<int> src_rect;
  942. CachedSurface* src_surface = res_cache.GetSurfaceRect(src_params, false, true, src_rect);
  943. if (src_surface == nullptr) {
  944. return false;
  945. }
  946. u32 scaled_width = src_surface->GetScaledWidth();
  947. u32 scaled_height = src_surface->GetScaledHeight();
  948. screen_info.display_texcoords = MathUtil::Rectangle<float>(
  949. (float)src_rect.top / (float)scaled_height, (float)src_rect.left / (float)scaled_width,
  950. (float)src_rect.bottom / (float)scaled_height, (float)src_rect.right / (float)scaled_width);
  951. screen_info.display_texture = src_surface->texture.handle;
  952. return true;
  953. }
  954. void RasterizerOpenGL::SamplerInfo::Create() {
  955. sampler.Create();
  956. mag_filter = min_filter = TextureConfig::Linear;
  957. wrap_s = wrap_t = TextureConfig::Repeat;
  958. border_color = 0;
  959. glSamplerParameteri(sampler.handle, GL_TEXTURE_MIN_FILTER,
  960. GL_LINEAR); // default is GL_LINEAR_MIPMAP_LINEAR
  961. // Other attributes have correct defaults
  962. }
  963. void RasterizerOpenGL::SamplerInfo::SyncWithConfig(
  964. const Pica::TexturingRegs::TextureConfig& config) {
  965. GLuint s = sampler.handle;
  966. if (mag_filter != config.mag_filter) {
  967. mag_filter = config.mag_filter;
  968. glSamplerParameteri(s, GL_TEXTURE_MAG_FILTER, PicaToGL::TextureFilterMode(mag_filter));
  969. }
  970. if (min_filter != config.min_filter) {
  971. min_filter = config.min_filter;
  972. glSamplerParameteri(s, GL_TEXTURE_MIN_FILTER, PicaToGL::TextureFilterMode(min_filter));
  973. }
  974. if (wrap_s != config.wrap_s) {
  975. wrap_s = config.wrap_s;
  976. glSamplerParameteri(s, GL_TEXTURE_WRAP_S, PicaToGL::WrapMode(wrap_s));
  977. }
  978. if (wrap_t != config.wrap_t) {
  979. wrap_t = config.wrap_t;
  980. glSamplerParameteri(s, GL_TEXTURE_WRAP_T, PicaToGL::WrapMode(wrap_t));
  981. }
  982. if (wrap_s == TextureConfig::ClampToBorder || wrap_t == TextureConfig::ClampToBorder) {
  983. if (border_color != config.border_color.raw) {
  984. border_color = config.border_color.raw;
  985. auto gl_color = PicaToGL::ColorRGBA8(border_color);
  986. glSamplerParameterfv(s, GL_TEXTURE_BORDER_COLOR, gl_color.data());
  987. }
  988. }
  989. }
  990. void RasterizerOpenGL::SetShader() {
  991. auto config = GLShader::PicaShaderConfig::BuildFromRegs(Pica::g_state.regs);
  992. std::unique_ptr<PicaShader> shader = std::make_unique<PicaShader>();
  993. // Find (or generate) the GLSL shader for the current TEV state
  994. auto cached_shader = shader_cache.find(config);
  995. if (cached_shader != shader_cache.end()) {
  996. current_shader = cached_shader->second.get();
  997. state.draw.shader_program = current_shader->shader.handle;
  998. state.Apply();
  999. } else {
  1000. LOG_DEBUG(Render_OpenGL, "Creating new shader");
  1001. shader->shader.Create(GLShader::GenerateVertexShader().c_str(),
  1002. GLShader::GenerateFragmentShader(config).c_str());
  1003. state.draw.shader_program = shader->shader.handle;
  1004. state.Apply();
  1005. // Set the texture samplers to correspond to different texture units
  1006. GLuint uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[0]");
  1007. if (uniform_tex != -1) {
  1008. glUniform1i(uniform_tex, 0);
  1009. }
  1010. uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[1]");
  1011. if (uniform_tex != -1) {
  1012. glUniform1i(uniform_tex, 1);
  1013. }
  1014. uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[2]");
  1015. if (uniform_tex != -1) {
  1016. glUniform1i(uniform_tex, 2);
  1017. }
  1018. // Set the texture samplers to correspond to different lookup table texture units
  1019. GLuint uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[0]");
  1020. if (uniform_lut != -1) {
  1021. glUniform1i(uniform_lut, 3);
  1022. }
  1023. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[1]");
  1024. if (uniform_lut != -1) {
  1025. glUniform1i(uniform_lut, 4);
  1026. }
  1027. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[2]");
  1028. if (uniform_lut != -1) {
  1029. glUniform1i(uniform_lut, 5);
  1030. }
  1031. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[3]");
  1032. if (uniform_lut != -1) {
  1033. glUniform1i(uniform_lut, 6);
  1034. }
  1035. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[4]");
  1036. if (uniform_lut != -1) {
  1037. glUniform1i(uniform_lut, 7);
  1038. }
  1039. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[5]");
  1040. if (uniform_lut != -1) {
  1041. glUniform1i(uniform_lut, 8);
  1042. }
  1043. GLuint uniform_fog_lut = glGetUniformLocation(shader->shader.handle, "fog_lut");
  1044. if (uniform_fog_lut != -1) {
  1045. glUniform1i(uniform_fog_lut, 9);
  1046. }
  1047. GLuint uniform_proctex_noise_lut =
  1048. glGetUniformLocation(shader->shader.handle, "proctex_noise_lut");
  1049. if (uniform_proctex_noise_lut != -1) {
  1050. glUniform1i(uniform_proctex_noise_lut, 10);
  1051. }
  1052. GLuint uniform_proctex_color_map =
  1053. glGetUniformLocation(shader->shader.handle, "proctex_color_map");
  1054. if (uniform_proctex_color_map != -1) {
  1055. glUniform1i(uniform_proctex_color_map, 11);
  1056. }
  1057. GLuint uniform_proctex_alpha_map =
  1058. glGetUniformLocation(shader->shader.handle, "proctex_alpha_map");
  1059. if (uniform_proctex_alpha_map != -1) {
  1060. glUniform1i(uniform_proctex_alpha_map, 12);
  1061. }
  1062. GLuint uniform_proctex_lut = glGetUniformLocation(shader->shader.handle, "proctex_lut");
  1063. if (uniform_proctex_lut != -1) {
  1064. glUniform1i(uniform_proctex_lut, 13);
  1065. }
  1066. GLuint uniform_proctex_diff_lut =
  1067. glGetUniformLocation(shader->shader.handle, "proctex_diff_lut");
  1068. if (uniform_proctex_diff_lut != -1) {
  1069. glUniform1i(uniform_proctex_diff_lut, 14);
  1070. }
  1071. current_shader = shader_cache.emplace(config, std::move(shader)).first->second.get();
  1072. GLuint block_index = glGetUniformBlockIndex(current_shader->shader.handle, "shader_data");
  1073. if (block_index != GL_INVALID_INDEX) {
  1074. GLint block_size;
  1075. glGetActiveUniformBlockiv(current_shader->shader.handle, block_index,
  1076. GL_UNIFORM_BLOCK_DATA_SIZE, &block_size);
  1077. ASSERT_MSG(block_size == sizeof(UniformData),
  1078. "Uniform block size did not match! Got %d, expected %zu",
  1079. static_cast<int>(block_size), sizeof(UniformData));
  1080. glUniformBlockBinding(current_shader->shader.handle, block_index, 0);
  1081. // Update uniforms
  1082. SyncDepthScale();
  1083. SyncDepthOffset();
  1084. SyncAlphaTest();
  1085. SyncCombinerColor();
  1086. auto& tev_stages = Pica::g_state.regs.texturing.GetTevStages();
  1087. for (int index = 0; index < tev_stages.size(); ++index)
  1088. SyncTevConstColor(index, tev_stages[index]);
  1089. SyncGlobalAmbient();
  1090. for (int light_index = 0; light_index < 8; light_index++) {
  1091. SyncLightSpecular0(light_index);
  1092. SyncLightSpecular1(light_index);
  1093. SyncLightDiffuse(light_index);
  1094. SyncLightAmbient(light_index);
  1095. SyncLightPosition(light_index);
  1096. SyncLightDistanceAttenuationBias(light_index);
  1097. SyncLightDistanceAttenuationScale(light_index);
  1098. }
  1099. SyncFogColor();
  1100. SyncProcTexNoise();
  1101. }
  1102. }
  1103. }
  1104. void RasterizerOpenGL::SyncCullMode() {
  1105. const auto& regs = Pica::g_state.regs;
  1106. switch (regs.rasterizer.cull_mode) {
  1107. case Pica::RasterizerRegs::CullMode::KeepAll:
  1108. state.cull.enabled = false;
  1109. break;
  1110. case Pica::RasterizerRegs::CullMode::KeepClockWise:
  1111. state.cull.enabled = true;
  1112. state.cull.front_face = GL_CW;
  1113. break;
  1114. case Pica::RasterizerRegs::CullMode::KeepCounterClockWise:
  1115. state.cull.enabled = true;
  1116. state.cull.front_face = GL_CCW;
  1117. break;
  1118. default:
  1119. LOG_CRITICAL(Render_OpenGL, "Unknown cull mode %d", regs.rasterizer.cull_mode.Value());
  1120. UNIMPLEMENTED();
  1121. break;
  1122. }
  1123. }
  1124. void RasterizerOpenGL::SyncDepthScale() {
  1125. float depth_scale =
  1126. Pica::float24::FromRaw(Pica::g_state.regs.rasterizer.viewport_depth_range).ToFloat32();
  1127. if (depth_scale != uniform_block_data.data.depth_scale) {
  1128. uniform_block_data.data.depth_scale = depth_scale;
  1129. uniform_block_data.dirty = true;
  1130. }
  1131. }
  1132. void RasterizerOpenGL::SyncDepthOffset() {
  1133. float depth_offset =
  1134. Pica::float24::FromRaw(Pica::g_state.regs.rasterizer.viewport_depth_near_plane).ToFloat32();
  1135. if (depth_offset != uniform_block_data.data.depth_offset) {
  1136. uniform_block_data.data.depth_offset = depth_offset;
  1137. uniform_block_data.dirty = true;
  1138. }
  1139. }
  1140. void RasterizerOpenGL::SyncBlendEnabled() {
  1141. state.blend.enabled = (Pica::g_state.regs.framebuffer.output_merger.alphablend_enable == 1);
  1142. }
  1143. void RasterizerOpenGL::SyncBlendFuncs() {
  1144. const auto& regs = Pica::g_state.regs;
  1145. state.blend.rgb_equation =
  1146. PicaToGL::BlendEquation(regs.framebuffer.output_merger.alpha_blending.blend_equation_rgb);
  1147. state.blend.a_equation =
  1148. PicaToGL::BlendEquation(regs.framebuffer.output_merger.alpha_blending.blend_equation_a);
  1149. state.blend.src_rgb_func =
  1150. PicaToGL::BlendFunc(regs.framebuffer.output_merger.alpha_blending.factor_source_rgb);
  1151. state.blend.dst_rgb_func =
  1152. PicaToGL::BlendFunc(regs.framebuffer.output_merger.alpha_blending.factor_dest_rgb);
  1153. state.blend.src_a_func =
  1154. PicaToGL::BlendFunc(regs.framebuffer.output_merger.alpha_blending.factor_source_a);
  1155. state.blend.dst_a_func =
  1156. PicaToGL::BlendFunc(regs.framebuffer.output_merger.alpha_blending.factor_dest_a);
  1157. }
  1158. void RasterizerOpenGL::SyncBlendColor() {
  1159. auto blend_color =
  1160. PicaToGL::ColorRGBA8(Pica::g_state.regs.framebuffer.output_merger.blend_const.raw);
  1161. state.blend.color.red = blend_color[0];
  1162. state.blend.color.green = blend_color[1];
  1163. state.blend.color.blue = blend_color[2];
  1164. state.blend.color.alpha = blend_color[3];
  1165. }
  1166. void RasterizerOpenGL::SyncFogColor() {
  1167. const auto& regs = Pica::g_state.regs;
  1168. uniform_block_data.data.fog_color = {
  1169. regs.texturing.fog_color.r.Value() / 255.0f, regs.texturing.fog_color.g.Value() / 255.0f,
  1170. regs.texturing.fog_color.b.Value() / 255.0f,
  1171. };
  1172. uniform_block_data.dirty = true;
  1173. }
  1174. void RasterizerOpenGL::SyncFogLUT() {
  1175. std::array<GLuint, 128> new_data;
  1176. std::transform(Pica::g_state.fog.lut.begin(), Pica::g_state.fog.lut.end(), new_data.begin(),
  1177. [](const auto& entry) { return entry.raw; });
  1178. if (new_data != fog_lut_data) {
  1179. fog_lut_data = new_data;
  1180. glActiveTexture(GL_TEXTURE9);
  1181. glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 128, GL_RED_INTEGER, GL_UNSIGNED_INT,
  1182. fog_lut_data.data());
  1183. }
  1184. }
  1185. void RasterizerOpenGL::SyncProcTexNoise() {
  1186. const auto& regs = Pica::g_state.regs.texturing;
  1187. uniform_block_data.data.proctex_noise_f = {
  1188. Pica::float16::FromRaw(regs.proctex_noise_frequency.u).ToFloat32(),
  1189. Pica::float16::FromRaw(regs.proctex_noise_frequency.v).ToFloat32(),
  1190. };
  1191. uniform_block_data.data.proctex_noise_a = {
  1192. regs.proctex_noise_u.amplitude / 4095.0f, regs.proctex_noise_v.amplitude / 4095.0f,
  1193. };
  1194. uniform_block_data.data.proctex_noise_p = {
  1195. Pica::float16::FromRaw(regs.proctex_noise_u.phase).ToFloat32(),
  1196. Pica::float16::FromRaw(regs.proctex_noise_v.phase).ToFloat32(),
  1197. };
  1198. uniform_block_data.dirty = true;
  1199. }
  1200. // helper function for SyncProcTexNoiseLUT/ColorMap/AlphaMap
  1201. static void SyncProcTexValueLUT(const std::array<Pica::State::ProcTex::ValueEntry, 128>& lut,
  1202. std::array<GLvec2, 128>& lut_data, GLenum texture) {
  1203. std::array<GLvec2, 128> new_data;
  1204. std::transform(lut.begin(), lut.end(), new_data.begin(), [](const auto& entry) {
  1205. return GLvec2{entry.ToFloat(), entry.DiffToFloat()};
  1206. });
  1207. if (new_data != lut_data) {
  1208. lut_data = new_data;
  1209. glActiveTexture(texture);
  1210. glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 128, GL_RG, GL_FLOAT, lut_data.data());
  1211. }
  1212. }
  1213. void RasterizerOpenGL::SyncProcTexNoiseLUT() {
  1214. SyncProcTexValueLUT(Pica::g_state.proctex.noise_table, proctex_noise_lut_data, GL_TEXTURE10);
  1215. }
  1216. void RasterizerOpenGL::SyncProcTexColorMap() {
  1217. SyncProcTexValueLUT(Pica::g_state.proctex.color_map_table, proctex_color_map_data,
  1218. GL_TEXTURE11);
  1219. }
  1220. void RasterizerOpenGL::SyncProcTexAlphaMap() {
  1221. SyncProcTexValueLUT(Pica::g_state.proctex.alpha_map_table, proctex_alpha_map_data,
  1222. GL_TEXTURE12);
  1223. }
  1224. void RasterizerOpenGL::SyncProcTexLUT() {
  1225. std::array<GLvec4, 256> new_data;
  1226. std::transform(Pica::g_state.proctex.color_table.begin(),
  1227. Pica::g_state.proctex.color_table.end(), new_data.begin(),
  1228. [](const auto& entry) {
  1229. auto rgba = entry.ToVector() / 255.0f;
  1230. return GLvec4{rgba.r(), rgba.g(), rgba.b(), rgba.a()};
  1231. });
  1232. if (new_data != proctex_lut_data) {
  1233. proctex_lut_data = new_data;
  1234. glActiveTexture(GL_TEXTURE13);
  1235. glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT, proctex_lut_data.data());
  1236. }
  1237. }
  1238. void RasterizerOpenGL::SyncProcTexDiffLUT() {
  1239. std::array<GLvec4, 256> new_data;
  1240. std::transform(Pica::g_state.proctex.color_diff_table.begin(),
  1241. Pica::g_state.proctex.color_diff_table.end(), new_data.begin(),
  1242. [](const auto& entry) {
  1243. auto rgba = entry.ToVector() / 255.0f;
  1244. return GLvec4{rgba.r(), rgba.g(), rgba.b(), rgba.a()};
  1245. });
  1246. if (new_data != proctex_diff_lut_data) {
  1247. proctex_diff_lut_data = new_data;
  1248. glActiveTexture(GL_TEXTURE14);
  1249. glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT, proctex_diff_lut_data.data());
  1250. }
  1251. }
  1252. void RasterizerOpenGL::SyncAlphaTest() {
  1253. const auto& regs = Pica::g_state.regs;
  1254. if (regs.framebuffer.output_merger.alpha_test.ref != uniform_block_data.data.alphatest_ref) {
  1255. uniform_block_data.data.alphatest_ref = regs.framebuffer.output_merger.alpha_test.ref;
  1256. uniform_block_data.dirty = true;
  1257. }
  1258. }
  1259. void RasterizerOpenGL::SyncLogicOp() {
  1260. state.logic_op = PicaToGL::LogicOp(Pica::g_state.regs.framebuffer.output_merger.logic_op);
  1261. }
  1262. void RasterizerOpenGL::SyncColorWriteMask() {
  1263. const auto& regs = Pica::g_state.regs;
  1264. auto IsColorWriteEnabled = [&](u32 value) {
  1265. return (regs.framebuffer.framebuffer.allow_color_write != 0 && value != 0) ? GL_TRUE
  1266. : GL_FALSE;
  1267. };
  1268. state.color_mask.red_enabled = IsColorWriteEnabled(regs.framebuffer.output_merger.red_enable);
  1269. state.color_mask.green_enabled =
  1270. IsColorWriteEnabled(regs.framebuffer.output_merger.green_enable);
  1271. state.color_mask.blue_enabled = IsColorWriteEnabled(regs.framebuffer.output_merger.blue_enable);
  1272. state.color_mask.alpha_enabled =
  1273. IsColorWriteEnabled(regs.framebuffer.output_merger.alpha_enable);
  1274. }
  1275. void RasterizerOpenGL::SyncStencilWriteMask() {
  1276. const auto& regs = Pica::g_state.regs;
  1277. state.stencil.write_mask =
  1278. (regs.framebuffer.framebuffer.allow_depth_stencil_write != 0)
  1279. ? static_cast<GLuint>(regs.framebuffer.output_merger.stencil_test.write_mask)
  1280. : 0;
  1281. }
  1282. void RasterizerOpenGL::SyncDepthWriteMask() {
  1283. const auto& regs = Pica::g_state.regs;
  1284. state.depth.write_mask = (regs.framebuffer.framebuffer.allow_depth_stencil_write != 0 &&
  1285. regs.framebuffer.output_merger.depth_write_enable)
  1286. ? GL_TRUE
  1287. : GL_FALSE;
  1288. }
  1289. void RasterizerOpenGL::SyncStencilTest() {
  1290. const auto& regs = Pica::g_state.regs;
  1291. state.stencil.test_enabled =
  1292. regs.framebuffer.output_merger.stencil_test.enable &&
  1293. regs.framebuffer.framebuffer.depth_format == Pica::FramebufferRegs::DepthFormat::D24S8;
  1294. state.stencil.test_func =
  1295. PicaToGL::CompareFunc(regs.framebuffer.output_merger.stencil_test.func);
  1296. state.stencil.test_ref = regs.framebuffer.output_merger.stencil_test.reference_value;
  1297. state.stencil.test_mask = regs.framebuffer.output_merger.stencil_test.input_mask;
  1298. state.stencil.action_stencil_fail =
  1299. PicaToGL::StencilOp(regs.framebuffer.output_merger.stencil_test.action_stencil_fail);
  1300. state.stencil.action_depth_fail =
  1301. PicaToGL::StencilOp(regs.framebuffer.output_merger.stencil_test.action_depth_fail);
  1302. state.stencil.action_depth_pass =
  1303. PicaToGL::StencilOp(regs.framebuffer.output_merger.stencil_test.action_depth_pass);
  1304. }
  1305. void RasterizerOpenGL::SyncDepthTest() {
  1306. const auto& regs = Pica::g_state.regs;
  1307. state.depth.test_enabled = regs.framebuffer.output_merger.depth_test_enable == 1 ||
  1308. regs.framebuffer.output_merger.depth_write_enable == 1;
  1309. state.depth.test_func =
  1310. regs.framebuffer.output_merger.depth_test_enable == 1
  1311. ? PicaToGL::CompareFunc(regs.framebuffer.output_merger.depth_test_func)
  1312. : GL_ALWAYS;
  1313. }
  1314. void RasterizerOpenGL::SyncCombinerColor() {
  1315. auto combiner_color =
  1316. PicaToGL::ColorRGBA8(Pica::g_state.regs.texturing.tev_combiner_buffer_color.raw);
  1317. if (combiner_color != uniform_block_data.data.tev_combiner_buffer_color) {
  1318. uniform_block_data.data.tev_combiner_buffer_color = combiner_color;
  1319. uniform_block_data.dirty = true;
  1320. }
  1321. }
  1322. void RasterizerOpenGL::SyncTevConstColor(int stage_index,
  1323. const Pica::TexturingRegs::TevStageConfig& tev_stage) {
  1324. auto const_color = PicaToGL::ColorRGBA8(tev_stage.const_color);
  1325. if (const_color != uniform_block_data.data.const_color[stage_index]) {
  1326. uniform_block_data.data.const_color[stage_index] = const_color;
  1327. uniform_block_data.dirty = true;
  1328. }
  1329. }
  1330. void RasterizerOpenGL::SyncGlobalAmbient() {
  1331. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.global_ambient);
  1332. if (color != uniform_block_data.data.lighting_global_ambient) {
  1333. uniform_block_data.data.lighting_global_ambient = color;
  1334. uniform_block_data.dirty = true;
  1335. }
  1336. }
  1337. void RasterizerOpenGL::SyncLightingLUT(unsigned lut_index) {
  1338. std::array<GLvec4, 256> new_data;
  1339. for (unsigned offset = 0; offset < new_data.size(); ++offset) {
  1340. new_data[offset][0] = Pica::g_state.lighting.luts[(lut_index * 4) + 0][offset].ToFloat();
  1341. new_data[offset][1] = Pica::g_state.lighting.luts[(lut_index * 4) + 1][offset].ToFloat();
  1342. new_data[offset][2] = Pica::g_state.lighting.luts[(lut_index * 4) + 2][offset].ToFloat();
  1343. new_data[offset][3] = Pica::g_state.lighting.luts[(lut_index * 4) + 3][offset].ToFloat();
  1344. }
  1345. if (new_data != lighting_lut_data[lut_index]) {
  1346. lighting_lut_data[lut_index] = new_data;
  1347. glActiveTexture(GL_TEXTURE3 + lut_index);
  1348. glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT,
  1349. lighting_lut_data[lut_index].data());
  1350. }
  1351. }
  1352. void RasterizerOpenGL::SyncLightSpecular0(int light_index) {
  1353. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].specular_0);
  1354. if (color != uniform_block_data.data.light_src[light_index].specular_0) {
  1355. uniform_block_data.data.light_src[light_index].specular_0 = color;
  1356. uniform_block_data.dirty = true;
  1357. }
  1358. }
  1359. void RasterizerOpenGL::SyncLightSpecular1(int light_index) {
  1360. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].specular_1);
  1361. if (color != uniform_block_data.data.light_src[light_index].specular_1) {
  1362. uniform_block_data.data.light_src[light_index].specular_1 = color;
  1363. uniform_block_data.dirty = true;
  1364. }
  1365. }
  1366. void RasterizerOpenGL::SyncLightDiffuse(int light_index) {
  1367. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].diffuse);
  1368. if (color != uniform_block_data.data.light_src[light_index].diffuse) {
  1369. uniform_block_data.data.light_src[light_index].diffuse = color;
  1370. uniform_block_data.dirty = true;
  1371. }
  1372. }
  1373. void RasterizerOpenGL::SyncLightAmbient(int light_index) {
  1374. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].ambient);
  1375. if (color != uniform_block_data.data.light_src[light_index].ambient) {
  1376. uniform_block_data.data.light_src[light_index].ambient = color;
  1377. uniform_block_data.dirty = true;
  1378. }
  1379. }
  1380. void RasterizerOpenGL::SyncLightPosition(int light_index) {
  1381. GLvec3 position = {
  1382. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].x).ToFloat32(),
  1383. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].y).ToFloat32(),
  1384. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].z).ToFloat32()};
  1385. if (position != uniform_block_data.data.light_src[light_index].position) {
  1386. uniform_block_data.data.light_src[light_index].position = position;
  1387. uniform_block_data.dirty = true;
  1388. }
  1389. }
  1390. void RasterizerOpenGL::SyncLightDistanceAttenuationBias(int light_index) {
  1391. GLfloat dist_atten_bias =
  1392. Pica::float20::FromRaw(Pica::g_state.regs.lighting.light[light_index].dist_atten_bias)
  1393. .ToFloat32();
  1394. if (dist_atten_bias != uniform_block_data.data.light_src[light_index].dist_atten_bias) {
  1395. uniform_block_data.data.light_src[light_index].dist_atten_bias = dist_atten_bias;
  1396. uniform_block_data.dirty = true;
  1397. }
  1398. }
  1399. void RasterizerOpenGL::SyncLightDistanceAttenuationScale(int light_index) {
  1400. GLfloat dist_atten_scale =
  1401. Pica::float20::FromRaw(Pica::g_state.regs.lighting.light[light_index].dist_atten_scale)
  1402. .ToFloat32();
  1403. if (dist_atten_scale != uniform_block_data.data.light_src[light_index].dist_atten_scale) {
  1404. uniform_block_data.data.light_src[light_index].dist_atten_scale = dist_atten_scale;
  1405. uniform_block_data.dirty = true;
  1406. }
  1407. }