gl_rasterizer.cpp 68 KB

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