gl_rasterizer.cpp 69 KB

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