gl_rasterizer.cpp 68 KB

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