gl_rasterizer.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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/vector_math.h"
  14. #include "core/hw/gpu.h"
  15. #include "video_core/pica.h"
  16. #include "video_core/pica_state.h"
  17. #include "video_core/renderer_opengl/gl_rasterizer.h"
  18. #include "video_core/renderer_opengl/gl_shader_gen.h"
  19. #include "video_core/renderer_opengl/gl_shader_util.h"
  20. #include "video_core/renderer_opengl/pica_to_gl.h"
  21. #include "video_core/renderer_opengl/renderer_opengl.h"
  22. static bool IsPassThroughTevStage(const Pica::Regs::TevStageConfig& stage) {
  23. return (stage.color_op == Pica::Regs::TevStageConfig::Operation::Replace &&
  24. stage.alpha_op == Pica::Regs::TevStageConfig::Operation::Replace &&
  25. stage.color_source1 == Pica::Regs::TevStageConfig::Source::Previous &&
  26. stage.alpha_source1 == Pica::Regs::TevStageConfig::Source::Previous &&
  27. stage.color_modifier1 == Pica::Regs::TevStageConfig::ColorModifier::SourceColor &&
  28. stage.alpha_modifier1 == Pica::Regs::TevStageConfig::AlphaModifier::SourceAlpha &&
  29. stage.GetColorMultiplier() == 1 &&
  30. stage.GetAlphaMultiplier() == 1);
  31. }
  32. RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
  33. // Create sampler objects
  34. for (size_t i = 0; i < texture_samplers.size(); ++i) {
  35. texture_samplers[i].Create();
  36. state.texture_units[i].sampler = texture_samplers[i].sampler.handle;
  37. }
  38. // Generate VBO, VAO and UBO
  39. vertex_buffer.Create();
  40. vertex_array.Create();
  41. uniform_buffer.Create();
  42. state.draw.vertex_array = vertex_array.handle;
  43. state.draw.vertex_buffer = vertex_buffer.handle;
  44. state.draw.uniform_buffer = uniform_buffer.handle;
  45. state.Apply();
  46. // Bind the UBO to binding point 0
  47. glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer.handle);
  48. uniform_block_data.dirty = true;
  49. for (unsigned index = 0; index < lighting_luts.size(); index++) {
  50. uniform_block_data.lut_dirty[index] = true;
  51. }
  52. uniform_block_data.fog_lut_dirty = true;
  53. // Set vertex attributes
  54. glVertexAttribPointer(GLShader::ATTRIBUTE_POSITION, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, position));
  55. glEnableVertexAttribArray(GLShader::ATTRIBUTE_POSITION);
  56. glVertexAttribPointer(GLShader::ATTRIBUTE_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, color));
  57. glEnableVertexAttribArray(GLShader::ATTRIBUTE_COLOR);
  58. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord0));
  59. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD1, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord1));
  60. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD2, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord2));
  61. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD0);
  62. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD1);
  63. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD2);
  64. glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD0_W, 1, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord0_w));
  65. glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD0_W);
  66. glVertexAttribPointer(GLShader::ATTRIBUTE_NORMQUAT, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, normquat));
  67. glEnableVertexAttribArray(GLShader::ATTRIBUTE_NORMQUAT);
  68. glVertexAttribPointer(GLShader::ATTRIBUTE_VIEW, 3, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, view));
  69. glEnableVertexAttribArray(GLShader::ATTRIBUTE_VIEW);
  70. // Create render framebuffer
  71. framebuffer.Create();
  72. // Allocate and bind lighting lut textures
  73. for (size_t i = 0; i < lighting_luts.size(); ++i) {
  74. lighting_luts[i].Create();
  75. state.lighting_luts[i].texture_1d = lighting_luts[i].handle;
  76. }
  77. state.Apply();
  78. for (size_t i = 0; i < lighting_luts.size(); ++i) {
  79. glActiveTexture(static_cast<GLenum>(GL_TEXTURE3 + i));
  80. glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr);
  81. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  82. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  83. }
  84. // Setup the LUT for the fog
  85. {
  86. fog_lut.Create();
  87. state.fog_lut.texture_1d = fog_lut.handle;
  88. }
  89. state.Apply();
  90. glActiveTexture(GL_TEXTURE9);
  91. glTexImage1D(GL_TEXTURE_1D, 0, GL_R32UI, 128, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, nullptr);
  92. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  93. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  94. // Sync fixed function OpenGL state
  95. SyncCullMode();
  96. SyncBlendEnabled();
  97. SyncBlendFuncs();
  98. SyncBlendColor();
  99. SyncLogicOp();
  100. SyncStencilTest();
  101. SyncDepthTest();
  102. SyncColorWriteMask();
  103. SyncStencilWriteMask();
  104. SyncDepthWriteMask();
  105. }
  106. RasterizerOpenGL::~RasterizerOpenGL() {
  107. }
  108. /**
  109. * This is a helper function to resolve an issue with opposite quaternions being interpolated by
  110. * OpenGL. See below for a detailed description of this issue (yuriks):
  111. *
  112. * For any rotation, there are two quaternions Q, and -Q, that represent the same rotation. If you
  113. * interpolate two quaternions that are opposite, instead of going from one rotation to another
  114. * using the shortest path, you'll go around the longest path. You can test if two quaternions are
  115. * opposite by checking if Dot(Q1, W2) < 0. In that case, you can flip either of them, therefore
  116. * making Dot(-Q1, W2) positive.
  117. *
  118. * NOTE: This solution corrects this issue per-vertex before passing the quaternions to OpenGL. This
  119. * should be correct for nearly all cases, however a more correct implementation (but less trivial
  120. * and perhaps unnecessary) would be to handle this per-fragment, by interpolating the quaternions
  121. * manually using two Lerps, and doing this correction before each Lerp.
  122. */
  123. static bool AreQuaternionsOpposite(Math::Vec4<Pica::float24> qa, Math::Vec4<Pica::float24> qb) {
  124. Math::Vec4f a{ qa.x.ToFloat32(), qa.y.ToFloat32(), qa.z.ToFloat32(), qa.w.ToFloat32() };
  125. Math::Vec4f b{ qb.x.ToFloat32(), qb.y.ToFloat32(), qb.z.ToFloat32(), qb.w.ToFloat32() };
  126. return (Math::Dot(a, b) < 0.f);
  127. }
  128. void RasterizerOpenGL::AddTriangle(const Pica::Shader::OutputVertex& v0,
  129. const Pica::Shader::OutputVertex& v1,
  130. const Pica::Shader::OutputVertex& v2) {
  131. vertex_batch.emplace_back(v0, false);
  132. vertex_batch.emplace_back(v1, AreQuaternionsOpposite(v0.quat, v1.quat));
  133. vertex_batch.emplace_back(v2, AreQuaternionsOpposite(v0.quat, v2.quat));
  134. }
  135. void RasterizerOpenGL::DrawTriangles() {
  136. if (vertex_batch.empty())
  137. return;
  138. const auto& regs = Pica::g_state.regs;
  139. // Sync and bind the framebuffer surfaces
  140. CachedSurface* color_surface;
  141. CachedSurface* depth_surface;
  142. MathUtil::Rectangle<int> rect;
  143. std::tie(color_surface, depth_surface, rect) = res_cache.GetFramebufferSurfaces(regs.framebuffer);
  144. state.draw.draw_framebuffer = framebuffer.handle;
  145. state.Apply();
  146. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color_surface != nullptr ? color_surface->texture.handle : 0, 0);
  147. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_surface != nullptr ? depth_surface->texture.handle : 0, 0);
  148. bool has_stencil = regs.framebuffer.depth_format == Pica::Regs::DepthFormat::D24S8;
  149. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, (has_stencil && depth_surface != nullptr) ? depth_surface->texture.handle : 0, 0);
  150. if (OpenGLState::CheckFBStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  151. return;
  152. }
  153. // Sync the viewport
  154. // These registers hold half-width and half-height, so must be multiplied by 2
  155. GLsizei viewport_width = (GLsizei)Pica::float24::FromRaw(regs.viewport_size_x).ToFloat32() * 2;
  156. GLsizei viewport_height = (GLsizei)Pica::float24::FromRaw(regs.viewport_size_y).ToFloat32() * 2;
  157. glViewport((GLint)(rect.left + regs.viewport_corner.x * color_surface->res_scale_width),
  158. (GLint)(rect.bottom + regs.viewport_corner.y * color_surface->res_scale_height),
  159. (GLsizei)(viewport_width * color_surface->res_scale_width), (GLsizei)(viewport_height * color_surface->res_scale_height));
  160. // Sync and bind the texture surfaces
  161. const auto pica_textures = regs.GetTextures();
  162. for (unsigned texture_index = 0; texture_index < pica_textures.size(); ++texture_index) {
  163. const auto& texture = pica_textures[texture_index];
  164. if (texture.enabled) {
  165. texture_samplers[texture_index].SyncWithConfig(texture.config);
  166. CachedSurface* surface = res_cache.GetTextureSurface(texture);
  167. if (surface != nullptr) {
  168. state.texture_units[texture_index].texture_2d = surface->texture.handle;
  169. } else {
  170. // Can occur when texture addr is null or its memory is unmapped/invalid
  171. state.texture_units[texture_index].texture_2d = 0;
  172. }
  173. } else {
  174. state.texture_units[texture_index].texture_2d = 0;
  175. }
  176. }
  177. // Sync and bind the shader
  178. if (shader_dirty) {
  179. SetShader();
  180. shader_dirty = false;
  181. }
  182. // Sync the lighting luts
  183. for (unsigned index = 0; index < lighting_luts.size(); index++) {
  184. if (uniform_block_data.lut_dirty[index]) {
  185. SyncLightingLUT(index);
  186. uniform_block_data.lut_dirty[index] = false;
  187. }
  188. }
  189. // Sync the fog lut
  190. if (uniform_block_data.fog_lut_dirty) {
  191. SyncFogLUT();
  192. uniform_block_data.fog_lut_dirty = false;
  193. }
  194. // Sync the uniform data
  195. if (uniform_block_data.dirty) {
  196. glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformData), &uniform_block_data.data, GL_STATIC_DRAW);
  197. uniform_block_data.dirty = false;
  198. }
  199. state.Apply();
  200. // Draw the vertex batch
  201. glBufferData(GL_ARRAY_BUFFER, vertex_batch.size() * sizeof(HardwareVertex), vertex_batch.data(), GL_STREAM_DRAW);
  202. glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertex_batch.size());
  203. // Mark framebuffer surfaces as dirty
  204. // TODO: Restrict invalidation area to the viewport
  205. if (color_surface != nullptr) {
  206. color_surface->dirty = true;
  207. res_cache.FlushRegion(color_surface->addr, color_surface->size, color_surface, true);
  208. }
  209. if (depth_surface != nullptr) {
  210. depth_surface->dirty = true;
  211. res_cache.FlushRegion(depth_surface->addr, depth_surface->size, depth_surface, true);
  212. }
  213. vertex_batch.clear();
  214. // Unbind textures for potential future use as framebuffer attachments
  215. for (unsigned texture_index = 0; texture_index < pica_textures.size(); ++texture_index) {
  216. state.texture_units[texture_index].texture_2d = 0;
  217. }
  218. state.Apply();
  219. }
  220. void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
  221. const auto& regs = Pica::g_state.regs;
  222. switch(id) {
  223. // Culling
  224. case PICA_REG_INDEX(cull_mode):
  225. SyncCullMode();
  226. break;
  227. // Depth modifiers
  228. case PICA_REG_INDEX(viewport_depth_range):
  229. SyncDepthScale();
  230. break;
  231. case PICA_REG_INDEX(viewport_depth_near_plane):
  232. SyncDepthOffset();
  233. break;
  234. // Depth buffering
  235. case PICA_REG_INDEX(depthmap_enable):
  236. shader_dirty = true;
  237. break;
  238. // Blending
  239. case PICA_REG_INDEX(output_merger.alphablend_enable):
  240. SyncBlendEnabled();
  241. break;
  242. case PICA_REG_INDEX(output_merger.alpha_blending):
  243. SyncBlendFuncs();
  244. break;
  245. case PICA_REG_INDEX(output_merger.blend_const):
  246. SyncBlendColor();
  247. break;
  248. // Fog state
  249. case PICA_REG_INDEX(fog_color):
  250. SyncFogColor();
  251. break;
  252. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[0], 0xe8):
  253. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[1], 0xe9):
  254. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[2], 0xea):
  255. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[3], 0xeb):
  256. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[4], 0xec):
  257. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[5], 0xed):
  258. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[6], 0xee):
  259. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[7], 0xef):
  260. uniform_block_data.fog_lut_dirty = true;
  261. break;
  262. // Alpha test
  263. case PICA_REG_INDEX(output_merger.alpha_test):
  264. SyncAlphaTest();
  265. shader_dirty = true;
  266. break;
  267. // Sync GL stencil test + stencil write mask
  268. // (Pica stencil test function register also contains a stencil write mask)
  269. case PICA_REG_INDEX(output_merger.stencil_test.raw_func):
  270. SyncStencilTest();
  271. SyncStencilWriteMask();
  272. break;
  273. case PICA_REG_INDEX(output_merger.stencil_test.raw_op):
  274. case PICA_REG_INDEX(framebuffer.depth_format):
  275. SyncStencilTest();
  276. break;
  277. // Sync GL depth test + depth and color write mask
  278. // (Pica depth test function register also contains a depth and color write mask)
  279. case PICA_REG_INDEX(output_merger.depth_test_enable):
  280. SyncDepthTest();
  281. SyncDepthWriteMask();
  282. SyncColorWriteMask();
  283. break;
  284. // Sync GL depth and stencil write mask
  285. // (This is a dedicated combined depth / stencil write-enable register)
  286. case PICA_REG_INDEX(framebuffer.allow_depth_stencil_write):
  287. SyncDepthWriteMask();
  288. SyncStencilWriteMask();
  289. break;
  290. // Sync GL color write mask
  291. // (This is a dedicated color write-enable register)
  292. case PICA_REG_INDEX(framebuffer.allow_color_write):
  293. SyncColorWriteMask();
  294. break;
  295. // Logic op
  296. case PICA_REG_INDEX(output_merger.logic_op):
  297. SyncLogicOp();
  298. break;
  299. // Texture 0 type
  300. case PICA_REG_INDEX(texture0.type):
  301. shader_dirty = true;
  302. break;
  303. // TEV stages
  304. // (This also syncs fog_mode and fog_flip which are part of tev_combiner_buffer_input)
  305. case PICA_REG_INDEX(tev_stage0.color_source1):
  306. case PICA_REG_INDEX(tev_stage0.color_modifier1):
  307. case PICA_REG_INDEX(tev_stage0.color_op):
  308. case PICA_REG_INDEX(tev_stage0.color_scale):
  309. case PICA_REG_INDEX(tev_stage1.color_source1):
  310. case PICA_REG_INDEX(tev_stage1.color_modifier1):
  311. case PICA_REG_INDEX(tev_stage1.color_op):
  312. case PICA_REG_INDEX(tev_stage1.color_scale):
  313. case PICA_REG_INDEX(tev_stage2.color_source1):
  314. case PICA_REG_INDEX(tev_stage2.color_modifier1):
  315. case PICA_REG_INDEX(tev_stage2.color_op):
  316. case PICA_REG_INDEX(tev_stage2.color_scale):
  317. case PICA_REG_INDEX(tev_stage3.color_source1):
  318. case PICA_REG_INDEX(tev_stage3.color_modifier1):
  319. case PICA_REG_INDEX(tev_stage3.color_op):
  320. case PICA_REG_INDEX(tev_stage3.color_scale):
  321. case PICA_REG_INDEX(tev_stage4.color_source1):
  322. case PICA_REG_INDEX(tev_stage4.color_modifier1):
  323. case PICA_REG_INDEX(tev_stage4.color_op):
  324. case PICA_REG_INDEX(tev_stage4.color_scale):
  325. case PICA_REG_INDEX(tev_stage5.color_source1):
  326. case PICA_REG_INDEX(tev_stage5.color_modifier1):
  327. case PICA_REG_INDEX(tev_stage5.color_op):
  328. case PICA_REG_INDEX(tev_stage5.color_scale):
  329. case PICA_REG_INDEX(tev_combiner_buffer_input):
  330. shader_dirty = true;
  331. break;
  332. case PICA_REG_INDEX(tev_stage0.const_r):
  333. SyncTevConstColor(0, regs.tev_stage0);
  334. break;
  335. case PICA_REG_INDEX(tev_stage1.const_r):
  336. SyncTevConstColor(1, regs.tev_stage1);
  337. break;
  338. case PICA_REG_INDEX(tev_stage2.const_r):
  339. SyncTevConstColor(2, regs.tev_stage2);
  340. break;
  341. case PICA_REG_INDEX(tev_stage3.const_r):
  342. SyncTevConstColor(3, regs.tev_stage3);
  343. break;
  344. case PICA_REG_INDEX(tev_stage4.const_r):
  345. SyncTevConstColor(4, regs.tev_stage4);
  346. break;
  347. case PICA_REG_INDEX(tev_stage5.const_r):
  348. SyncTevConstColor(5, regs.tev_stage5);
  349. break;
  350. // TEV combiner buffer color
  351. case PICA_REG_INDEX(tev_combiner_buffer_color):
  352. SyncCombinerColor();
  353. break;
  354. // Fragment lighting switches
  355. case PICA_REG_INDEX(lighting.disable):
  356. case PICA_REG_INDEX(lighting.num_lights):
  357. case PICA_REG_INDEX(lighting.config0):
  358. case PICA_REG_INDEX(lighting.config1):
  359. case PICA_REG_INDEX(lighting.abs_lut_input):
  360. case PICA_REG_INDEX(lighting.lut_input):
  361. case PICA_REG_INDEX(lighting.lut_scale):
  362. case PICA_REG_INDEX(lighting.light_enable):
  363. break;
  364. // Fragment lighting specular 0 color
  365. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].specular_0, 0x140 + 0 * 0x10):
  366. SyncLightSpecular0(0);
  367. break;
  368. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].specular_0, 0x140 + 1 * 0x10):
  369. SyncLightSpecular0(1);
  370. break;
  371. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].specular_0, 0x140 + 2 * 0x10):
  372. SyncLightSpecular0(2);
  373. break;
  374. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].specular_0, 0x140 + 3 * 0x10):
  375. SyncLightSpecular0(3);
  376. break;
  377. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].specular_0, 0x140 + 4 * 0x10):
  378. SyncLightSpecular0(4);
  379. break;
  380. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].specular_0, 0x140 + 5 * 0x10):
  381. SyncLightSpecular0(5);
  382. break;
  383. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].specular_0, 0x140 + 6 * 0x10):
  384. SyncLightSpecular0(6);
  385. break;
  386. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].specular_0, 0x140 + 7 * 0x10):
  387. SyncLightSpecular0(7);
  388. break;
  389. // Fragment lighting specular 1 color
  390. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].specular_1, 0x141 + 0 * 0x10):
  391. SyncLightSpecular1(0);
  392. break;
  393. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].specular_1, 0x141 + 1 * 0x10):
  394. SyncLightSpecular1(1);
  395. break;
  396. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].specular_1, 0x141 + 2 * 0x10):
  397. SyncLightSpecular1(2);
  398. break;
  399. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].specular_1, 0x141 + 3 * 0x10):
  400. SyncLightSpecular1(3);
  401. break;
  402. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].specular_1, 0x141 + 4 * 0x10):
  403. SyncLightSpecular1(4);
  404. break;
  405. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].specular_1, 0x141 + 5 * 0x10):
  406. SyncLightSpecular1(5);
  407. break;
  408. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].specular_1, 0x141 + 6 * 0x10):
  409. SyncLightSpecular1(6);
  410. break;
  411. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].specular_1, 0x141 + 7 * 0x10):
  412. SyncLightSpecular1(7);
  413. break;
  414. // Fragment lighting diffuse color
  415. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].diffuse, 0x142 + 0 * 0x10):
  416. SyncLightDiffuse(0);
  417. break;
  418. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].diffuse, 0x142 + 1 * 0x10):
  419. SyncLightDiffuse(1);
  420. break;
  421. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].diffuse, 0x142 + 2 * 0x10):
  422. SyncLightDiffuse(2);
  423. break;
  424. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].diffuse, 0x142 + 3 * 0x10):
  425. SyncLightDiffuse(3);
  426. break;
  427. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].diffuse, 0x142 + 4 * 0x10):
  428. SyncLightDiffuse(4);
  429. break;
  430. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].diffuse, 0x142 + 5 * 0x10):
  431. SyncLightDiffuse(5);
  432. break;
  433. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].diffuse, 0x142 + 6 * 0x10):
  434. SyncLightDiffuse(6);
  435. break;
  436. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].diffuse, 0x142 + 7 * 0x10):
  437. SyncLightDiffuse(7);
  438. break;
  439. // Fragment lighting ambient color
  440. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].ambient, 0x143 + 0 * 0x10):
  441. SyncLightAmbient(0);
  442. break;
  443. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].ambient, 0x143 + 1 * 0x10):
  444. SyncLightAmbient(1);
  445. break;
  446. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].ambient, 0x143 + 2 * 0x10):
  447. SyncLightAmbient(2);
  448. break;
  449. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].ambient, 0x143 + 3 * 0x10):
  450. SyncLightAmbient(3);
  451. break;
  452. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].ambient, 0x143 + 4 * 0x10):
  453. SyncLightAmbient(4);
  454. break;
  455. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].ambient, 0x143 + 5 * 0x10):
  456. SyncLightAmbient(5);
  457. break;
  458. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].ambient, 0x143 + 6 * 0x10):
  459. SyncLightAmbient(6);
  460. break;
  461. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].ambient, 0x143 + 7 * 0x10):
  462. SyncLightAmbient(7);
  463. break;
  464. // Fragment lighting position
  465. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].x, 0x144 + 0 * 0x10):
  466. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].z, 0x145 + 0 * 0x10):
  467. SyncLightPosition(0);
  468. break;
  469. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].x, 0x144 + 1 * 0x10):
  470. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].z, 0x145 + 1 * 0x10):
  471. SyncLightPosition(1);
  472. break;
  473. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].x, 0x144 + 2 * 0x10):
  474. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].z, 0x145 + 2 * 0x10):
  475. SyncLightPosition(2);
  476. break;
  477. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].x, 0x144 + 3 * 0x10):
  478. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].z, 0x145 + 3 * 0x10):
  479. SyncLightPosition(3);
  480. break;
  481. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].x, 0x144 + 4 * 0x10):
  482. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].z, 0x145 + 4 * 0x10):
  483. SyncLightPosition(4);
  484. break;
  485. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].x, 0x144 + 5 * 0x10):
  486. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].z, 0x145 + 5 * 0x10):
  487. SyncLightPosition(5);
  488. break;
  489. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].x, 0x144 + 6 * 0x10):
  490. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].z, 0x145 + 6 * 0x10):
  491. SyncLightPosition(6);
  492. break;
  493. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].x, 0x144 + 7 * 0x10):
  494. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].z, 0x145 + 7 * 0x10):
  495. SyncLightPosition(7);
  496. break;
  497. // Fragment lighting light source config
  498. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].config, 0x149 + 0 * 0x10):
  499. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].config, 0x149 + 1 * 0x10):
  500. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].config, 0x149 + 2 * 0x10):
  501. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].config, 0x149 + 3 * 0x10):
  502. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].config, 0x149 + 4 * 0x10):
  503. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].config, 0x149 + 5 * 0x10):
  504. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].config, 0x149 + 6 * 0x10):
  505. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].config, 0x149 + 7 * 0x10):
  506. shader_dirty = true;
  507. break;
  508. // Fragment lighting distance attenuation bias
  509. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].dist_atten_bias, 0x014A + 0 * 0x10):
  510. SyncLightDistanceAttenuationBias(0);
  511. break;
  512. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].dist_atten_bias, 0x014A + 1 * 0x10):
  513. SyncLightDistanceAttenuationBias(1);
  514. break;
  515. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].dist_atten_bias, 0x014A + 2 * 0x10):
  516. SyncLightDistanceAttenuationBias(2);
  517. break;
  518. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].dist_atten_bias, 0x014A + 3 * 0x10):
  519. SyncLightDistanceAttenuationBias(3);
  520. break;
  521. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].dist_atten_bias, 0x014A + 4 * 0x10):
  522. SyncLightDistanceAttenuationBias(4);
  523. break;
  524. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].dist_atten_bias, 0x014A + 5 * 0x10):
  525. SyncLightDistanceAttenuationBias(5);
  526. break;
  527. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].dist_atten_bias, 0x014A + 6 * 0x10):
  528. SyncLightDistanceAttenuationBias(6);
  529. break;
  530. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].dist_atten_bias, 0x014A + 7 * 0x10):
  531. SyncLightDistanceAttenuationBias(7);
  532. break;
  533. // Fragment lighting distance attenuation scale
  534. case PICA_REG_INDEX_WORKAROUND(lighting.light[0].dist_atten_scale, 0x014B + 0 * 0x10):
  535. SyncLightDistanceAttenuationScale(0);
  536. break;
  537. case PICA_REG_INDEX_WORKAROUND(lighting.light[1].dist_atten_scale, 0x014B + 1 * 0x10):
  538. SyncLightDistanceAttenuationScale(1);
  539. break;
  540. case PICA_REG_INDEX_WORKAROUND(lighting.light[2].dist_atten_scale, 0x014B + 2 * 0x10):
  541. SyncLightDistanceAttenuationScale(2);
  542. break;
  543. case PICA_REG_INDEX_WORKAROUND(lighting.light[3].dist_atten_scale, 0x014B + 3 * 0x10):
  544. SyncLightDistanceAttenuationScale(3);
  545. break;
  546. case PICA_REG_INDEX_WORKAROUND(lighting.light[4].dist_atten_scale, 0x014B + 4 * 0x10):
  547. SyncLightDistanceAttenuationScale(4);
  548. break;
  549. case PICA_REG_INDEX_WORKAROUND(lighting.light[5].dist_atten_scale, 0x014B + 5 * 0x10):
  550. SyncLightDistanceAttenuationScale(5);
  551. break;
  552. case PICA_REG_INDEX_WORKAROUND(lighting.light[6].dist_atten_scale, 0x014B + 6 * 0x10):
  553. SyncLightDistanceAttenuationScale(6);
  554. break;
  555. case PICA_REG_INDEX_WORKAROUND(lighting.light[7].dist_atten_scale, 0x014B + 7 * 0x10):
  556. SyncLightDistanceAttenuationScale(7);
  557. break;
  558. // Fragment lighting global ambient color (emission + ambient * ambient)
  559. case PICA_REG_INDEX_WORKAROUND(lighting.global_ambient, 0x1c0):
  560. SyncGlobalAmbient();
  561. break;
  562. // Fragment lighting lookup tables
  563. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[0], 0x1c8):
  564. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[1], 0x1c9):
  565. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[2], 0x1ca):
  566. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[3], 0x1cb):
  567. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[4], 0x1cc):
  568. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[5], 0x1cd):
  569. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[6], 0x1ce):
  570. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[7], 0x1cf):
  571. {
  572. auto& lut_config = regs.lighting.lut_config;
  573. uniform_block_data.lut_dirty[lut_config.type / 4] = true;
  574. break;
  575. }
  576. }
  577. }
  578. void RasterizerOpenGL::FlushAll() {
  579. res_cache.FlushAll();
  580. }
  581. void RasterizerOpenGL::FlushRegion(PAddr addr, u32 size) {
  582. res_cache.FlushRegion(addr, size, nullptr, false);
  583. }
  584. void RasterizerOpenGL::FlushAndInvalidateRegion(PAddr addr, u32 size) {
  585. res_cache.FlushRegion(addr, size, nullptr, true);
  586. }
  587. bool RasterizerOpenGL::AccelerateDisplayTransfer(const GPU::Regs::DisplayTransferConfig& config) {
  588. using PixelFormat = CachedSurface::PixelFormat;
  589. using SurfaceType = CachedSurface::SurfaceType;
  590. if (config.is_texture_copy) {
  591. // TODO(tfarley): Try to hardware accelerate this
  592. return false;
  593. }
  594. CachedSurface src_params;
  595. src_params.addr = config.GetPhysicalInputAddress();
  596. src_params.width = config.output_width;
  597. src_params.height = config.output_height;
  598. src_params.is_tiled = !config.input_linear;
  599. src_params.pixel_format = CachedSurface::PixelFormatFromGPUPixelFormat(config.input_format);
  600. CachedSurface dst_params;
  601. dst_params.addr = config.GetPhysicalOutputAddress();
  602. dst_params.width = config.scaling != config.NoScale ? config.output_width / 2 : config.output_width.Value();
  603. dst_params.height = config.scaling == config.ScaleXY ? config.output_height / 2 : config.output_height.Value();
  604. dst_params.is_tiled = config.input_linear != config.dont_swizzle;
  605. dst_params.pixel_format = CachedSurface::PixelFormatFromGPUPixelFormat(config.output_format);
  606. MathUtil::Rectangle<int> src_rect;
  607. CachedSurface* src_surface = res_cache.GetSurfaceRect(src_params, false, true, src_rect);
  608. if (src_surface == nullptr) {
  609. return false;
  610. }
  611. // Require destination surface to have same resolution scale as source to preserve scaling
  612. dst_params.res_scale_width = src_surface->res_scale_width;
  613. dst_params.res_scale_height = src_surface->res_scale_height;
  614. MathUtil::Rectangle<int> dst_rect;
  615. CachedSurface* dst_surface = res_cache.GetSurfaceRect(dst_params, true, false, dst_rect);
  616. if (dst_surface == nullptr) {
  617. return false;
  618. }
  619. // Don't accelerate if the src and dst surfaces are the same
  620. if (src_surface == dst_surface) {
  621. return false;
  622. }
  623. if (config.flip_vertically) {
  624. std::swap(dst_rect.top, dst_rect.bottom);
  625. }
  626. if (!res_cache.TryBlitSurfaces(src_surface, src_rect, dst_surface, dst_rect)) {
  627. return false;
  628. }
  629. u32 dst_size = dst_params.width * dst_params.height * CachedSurface::GetFormatBpp(dst_params.pixel_format) / 8;
  630. dst_surface->dirty = true;
  631. res_cache.FlushRegion(config.GetPhysicalOutputAddress(), dst_size, dst_surface, true);
  632. return true;
  633. }
  634. bool RasterizerOpenGL::AccelerateFill(const GPU::Regs::MemoryFillConfig& config) {
  635. using PixelFormat = CachedSurface::PixelFormat;
  636. using SurfaceType = CachedSurface::SurfaceType;
  637. CachedSurface* dst_surface = res_cache.TryGetFillSurface(config);
  638. if (dst_surface == nullptr) {
  639. return false;
  640. }
  641. OpenGLState cur_state = OpenGLState::GetCurState();
  642. SurfaceType dst_type = CachedSurface::GetFormatType(dst_surface->pixel_format);
  643. GLuint old_fb = cur_state.draw.draw_framebuffer;
  644. cur_state.draw.draw_framebuffer = framebuffer.handle;
  645. // TODO: When scissor test is implemented, need to disable scissor test in cur_state here so Clear call isn't affected
  646. cur_state.Apply();
  647. if (dst_type == SurfaceType::Color || dst_type == SurfaceType::Texture) {
  648. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dst_surface->texture.handle, 0);
  649. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  650. if (OpenGLState::CheckFBStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  651. return false;
  652. }
  653. GLfloat color_values[4] = {0.0f, 0.0f, 0.0f, 0.0f};
  654. // TODO: Handle additional pixel format and fill value size combinations to accelerate more cases
  655. // For instance, checking if fill value's bytes/bits repeat to allow filling I8/A8/I4/A4/...
  656. // Currently only handles formats that are multiples of the fill value size
  657. if (config.fill_24bit) {
  658. switch (dst_surface->pixel_format) {
  659. case PixelFormat::RGB8:
  660. color_values[0] = config.value_24bit_r / 255.0f;
  661. color_values[1] = config.value_24bit_g / 255.0f;
  662. color_values[2] = config.value_24bit_b / 255.0f;
  663. break;
  664. default:
  665. return false;
  666. }
  667. } else if (config.fill_32bit) {
  668. u32 value = config.value_32bit;
  669. switch (dst_surface->pixel_format) {
  670. case PixelFormat::RGBA8:
  671. color_values[0] = (value >> 24) / 255.0f;
  672. color_values[1] = ((value >> 16) & 0xFF) / 255.0f;
  673. color_values[2] = ((value >> 8) & 0xFF) / 255.0f;
  674. color_values[3] = (value & 0xFF) / 255.0f;
  675. break;
  676. default:
  677. return false;
  678. }
  679. } else {
  680. u16 value_16bit = config.value_16bit.Value();
  681. Math::Vec4<u8> color;
  682. switch (dst_surface->pixel_format) {
  683. case PixelFormat::RGBA8:
  684. color_values[0] = (value_16bit >> 8) / 255.0f;
  685. color_values[1] = (value_16bit & 0xFF) / 255.0f;
  686. color_values[2] = color_values[0];
  687. color_values[3] = color_values[1];
  688. break;
  689. case PixelFormat::RGB5A1:
  690. color = Color::DecodeRGB5A1((const u8*)&value_16bit);
  691. color_values[0] = color[0] / 31.0f;
  692. color_values[1] = color[1] / 31.0f;
  693. color_values[2] = color[2] / 31.0f;
  694. color_values[3] = color[3];
  695. break;
  696. case PixelFormat::RGB565:
  697. color = Color::DecodeRGB565((const u8*)&value_16bit);
  698. color_values[0] = color[0] / 31.0f;
  699. color_values[1] = color[1] / 63.0f;
  700. color_values[2] = color[2] / 31.0f;
  701. break;
  702. case PixelFormat::RGBA4:
  703. color = Color::DecodeRGBA4((const u8*)&value_16bit);
  704. color_values[0] = color[0] / 15.0f;
  705. color_values[1] = color[1] / 15.0f;
  706. color_values[2] = color[2] / 15.0f;
  707. color_values[3] = color[3] / 15.0f;
  708. break;
  709. case PixelFormat::IA8:
  710. case PixelFormat::RG8:
  711. color_values[0] = (value_16bit >> 8) / 255.0f;
  712. color_values[1] = (value_16bit & 0xFF) / 255.0f;
  713. break;
  714. default:
  715. return false;
  716. }
  717. }
  718. cur_state.color_mask.red_enabled = true;
  719. cur_state.color_mask.green_enabled = true;
  720. cur_state.color_mask.blue_enabled = true;
  721. cur_state.color_mask.alpha_enabled = true;
  722. cur_state.Apply();
  723. glClearBufferfv(GL_COLOR, 0, color_values);
  724. } else if (dst_type == SurfaceType::Depth) {
  725. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
  726. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, dst_surface->texture.handle, 0);
  727. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  728. if (OpenGLState::CheckFBStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  729. return false;
  730. }
  731. GLfloat value_float;
  732. if (dst_surface->pixel_format == CachedSurface::PixelFormat::D16) {
  733. value_float = config.value_32bit / 65535.0f; // 2^16 - 1
  734. } else if (dst_surface->pixel_format == CachedSurface::PixelFormat::D24) {
  735. value_float = config.value_32bit / 16777215.0f; // 2^24 - 1
  736. }
  737. cur_state.depth.write_mask = true;
  738. cur_state.Apply();
  739. glClearBufferfv(GL_DEPTH, 0, &value_float);
  740. } else if (dst_type == SurfaceType::DepthStencil) {
  741. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
  742. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, dst_surface->texture.handle, 0);
  743. if (OpenGLState::CheckFBStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  744. return false;
  745. }
  746. GLfloat value_float = (config.value_32bit & 0xFFFFFF) / 16777215.0f; // 2^24 - 1
  747. GLint value_int = (config.value_32bit >> 24);
  748. cur_state.depth.write_mask = true;
  749. cur_state.stencil.write_mask = true;
  750. cur_state.Apply();
  751. glClearBufferfi(GL_DEPTH_STENCIL, 0, value_float, value_int);
  752. }
  753. cur_state.draw.draw_framebuffer = old_fb;
  754. // TODO: Return scissor test to previous value when scissor test is implemented
  755. cur_state.Apply();
  756. dst_surface->dirty = true;
  757. res_cache.FlushRegion(dst_surface->addr, dst_surface->size, dst_surface, true);
  758. return true;
  759. }
  760. bool RasterizerOpenGL::AccelerateDisplay(const GPU::Regs::FramebufferConfig& config, PAddr framebuffer_addr, u32 pixel_stride, ScreenInfo& screen_info) {
  761. if (framebuffer_addr == 0) {
  762. return false;
  763. }
  764. CachedSurface src_params;
  765. src_params.addr = framebuffer_addr;
  766. src_params.width = config.width;
  767. src_params.height = config.height;
  768. src_params.stride = pixel_stride;
  769. src_params.is_tiled = false;
  770. src_params.pixel_format = CachedSurface::PixelFormatFromGPUPixelFormat(config.color_format);
  771. MathUtil::Rectangle<int> src_rect;
  772. CachedSurface* src_surface = res_cache.GetSurfaceRect(src_params, false, true, src_rect);
  773. if (src_surface == nullptr) {
  774. return false;
  775. }
  776. u32 scaled_width = src_surface->GetScaledWidth();
  777. u32 scaled_height = src_surface->GetScaledHeight();
  778. screen_info.display_texcoords = MathUtil::Rectangle<float>((float)src_rect.top / (float)scaled_height,
  779. (float)src_rect.left / (float)scaled_width,
  780. (float)src_rect.bottom / (float)scaled_height,
  781. (float)src_rect.right / (float)scaled_width);
  782. screen_info.display_texture = src_surface->texture.handle;
  783. return true;
  784. }
  785. void RasterizerOpenGL::SamplerInfo::Create() {
  786. sampler.Create();
  787. mag_filter = min_filter = TextureConfig::Linear;
  788. wrap_s = wrap_t = TextureConfig::Repeat;
  789. border_color = 0;
  790. glSamplerParameteri(sampler.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // default is GL_LINEAR_MIPMAP_LINEAR
  791. // Other attributes have correct defaults
  792. }
  793. void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Pica::Regs::TextureConfig& config) {
  794. GLuint s = sampler.handle;
  795. if (mag_filter != config.mag_filter) {
  796. mag_filter = config.mag_filter;
  797. glSamplerParameteri(s, GL_TEXTURE_MAG_FILTER, PicaToGL::TextureFilterMode(mag_filter));
  798. }
  799. if (min_filter != config.min_filter) {
  800. min_filter = config.min_filter;
  801. glSamplerParameteri(s, GL_TEXTURE_MIN_FILTER, PicaToGL::TextureFilterMode(min_filter));
  802. }
  803. if (wrap_s != config.wrap_s) {
  804. wrap_s = config.wrap_s;
  805. glSamplerParameteri(s, GL_TEXTURE_WRAP_S, PicaToGL::WrapMode(wrap_s));
  806. }
  807. if (wrap_t != config.wrap_t) {
  808. wrap_t = config.wrap_t;
  809. glSamplerParameteri(s, GL_TEXTURE_WRAP_T, PicaToGL::WrapMode(wrap_t));
  810. }
  811. if (wrap_s == TextureConfig::ClampToBorder || wrap_t == TextureConfig::ClampToBorder) {
  812. if (border_color != config.border_color.raw) {
  813. border_color = config.border_color.raw;
  814. auto gl_color = PicaToGL::ColorRGBA8(border_color);
  815. glSamplerParameterfv(s, GL_TEXTURE_BORDER_COLOR, gl_color.data());
  816. }
  817. }
  818. }
  819. void RasterizerOpenGL::SetShader() {
  820. PicaShaderConfig config = PicaShaderConfig::CurrentConfig();
  821. std::unique_ptr<PicaShader> shader = std::make_unique<PicaShader>();
  822. // Find (or generate) the GLSL shader for the current TEV state
  823. auto cached_shader = shader_cache.find(config);
  824. if (cached_shader != shader_cache.end()) {
  825. current_shader = cached_shader->second.get();
  826. state.draw.shader_program = current_shader->shader.handle;
  827. state.Apply();
  828. } else {
  829. LOG_DEBUG(Render_OpenGL, "Creating new shader");
  830. shader->shader.Create(GLShader::GenerateVertexShader().c_str(), GLShader::GenerateFragmentShader(config).c_str());
  831. state.draw.shader_program = shader->shader.handle;
  832. state.Apply();
  833. // Set the texture samplers to correspond to different texture units
  834. GLuint uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[0]");
  835. if (uniform_tex != -1) { glUniform1i(uniform_tex, 0); }
  836. uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[1]");
  837. if (uniform_tex != -1) { glUniform1i(uniform_tex, 1); }
  838. uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[2]");
  839. if (uniform_tex != -1) { glUniform1i(uniform_tex, 2); }
  840. // Set the texture samplers to correspond to different lookup table texture units
  841. GLuint uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[0]");
  842. if (uniform_lut != -1) { glUniform1i(uniform_lut, 3); }
  843. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[1]");
  844. if (uniform_lut != -1) { glUniform1i(uniform_lut, 4); }
  845. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[2]");
  846. if (uniform_lut != -1) { glUniform1i(uniform_lut, 5); }
  847. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[3]");
  848. if (uniform_lut != -1) { glUniform1i(uniform_lut, 6); }
  849. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[4]");
  850. if (uniform_lut != -1) { glUniform1i(uniform_lut, 7); }
  851. uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[5]");
  852. if (uniform_lut != -1) { glUniform1i(uniform_lut, 8); }
  853. GLuint uniform_fog_lut = glGetUniformLocation(shader->shader.handle, "fog_lut");
  854. if (uniform_fog_lut != -1) { glUniform1i(uniform_fog_lut, 9); }
  855. current_shader = shader_cache.emplace(config, std::move(shader)).first->second.get();
  856. unsigned int block_index = glGetUniformBlockIndex(current_shader->shader.handle, "shader_data");
  857. GLint block_size;
  858. glGetActiveUniformBlockiv(current_shader->shader.handle, block_index, GL_UNIFORM_BLOCK_DATA_SIZE, &block_size);
  859. ASSERT_MSG(block_size == sizeof(UniformData), "Uniform block size did not match!");
  860. glUniformBlockBinding(current_shader->shader.handle, block_index, 0);
  861. // Update uniforms
  862. SyncDepthScale();
  863. SyncDepthOffset();
  864. SyncAlphaTest();
  865. SyncCombinerColor();
  866. auto& tev_stages = Pica::g_state.regs.GetTevStages();
  867. for (int index = 0; index < tev_stages.size(); ++index)
  868. SyncTevConstColor(index, tev_stages[index]);
  869. SyncGlobalAmbient();
  870. for (int light_index = 0; light_index < 8; light_index++) {
  871. SyncLightSpecular0(light_index);
  872. SyncLightSpecular1(light_index);
  873. SyncLightDiffuse(light_index);
  874. SyncLightAmbient(light_index);
  875. SyncLightPosition(light_index);
  876. SyncLightDistanceAttenuationBias(light_index);
  877. SyncLightDistanceAttenuationScale(light_index);
  878. }
  879. SyncFogColor();
  880. }
  881. }
  882. void RasterizerOpenGL::SyncCullMode() {
  883. const auto& regs = Pica::g_state.regs;
  884. switch (regs.cull_mode) {
  885. case Pica::Regs::CullMode::KeepAll:
  886. state.cull.enabled = false;
  887. break;
  888. case Pica::Regs::CullMode::KeepClockWise:
  889. state.cull.enabled = true;
  890. state.cull.front_face = GL_CW;
  891. break;
  892. case Pica::Regs::CullMode::KeepCounterClockWise:
  893. state.cull.enabled = true;
  894. state.cull.front_face = GL_CCW;
  895. break;
  896. default:
  897. LOG_CRITICAL(Render_OpenGL, "Unknown cull mode %d", regs.cull_mode.Value());
  898. UNIMPLEMENTED();
  899. break;
  900. }
  901. }
  902. void RasterizerOpenGL::SyncDepthScale() {
  903. float depth_scale = Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_range).ToFloat32();
  904. if (depth_scale != uniform_block_data.data.depth_scale) {
  905. uniform_block_data.data.depth_scale = depth_scale;
  906. uniform_block_data.dirty = true;
  907. }
  908. }
  909. void RasterizerOpenGL::SyncDepthOffset() {
  910. float depth_offset = Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_near_plane).ToFloat32();
  911. if (depth_offset != uniform_block_data.data.depth_offset) {
  912. uniform_block_data.data.depth_offset = depth_offset;
  913. uniform_block_data.dirty = true;
  914. }
  915. }
  916. void RasterizerOpenGL::SyncBlendEnabled() {
  917. state.blend.enabled = (Pica::g_state.regs.output_merger.alphablend_enable == 1);
  918. }
  919. void RasterizerOpenGL::SyncBlendFuncs() {
  920. const auto& regs = Pica::g_state.regs;
  921. state.blend.rgb_equation = PicaToGL::BlendEquation(regs.output_merger.alpha_blending.blend_equation_rgb);
  922. state.blend.a_equation = PicaToGL::BlendEquation(regs.output_merger.alpha_blending.blend_equation_a);
  923. state.blend.src_rgb_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_source_rgb);
  924. state.blend.dst_rgb_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_dest_rgb);
  925. state.blend.src_a_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_source_a);
  926. state.blend.dst_a_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_dest_a);
  927. }
  928. void RasterizerOpenGL::SyncBlendColor() {
  929. auto blend_color = PicaToGL::ColorRGBA8(Pica::g_state.regs.output_merger.blend_const.raw);
  930. state.blend.color.red = blend_color[0];
  931. state.blend.color.green = blend_color[1];
  932. state.blend.color.blue = blend_color[2];
  933. state.blend.color.alpha = blend_color[3];
  934. }
  935. void RasterizerOpenGL::SyncFogColor() {
  936. const auto& regs = Pica::g_state.regs;
  937. uniform_block_data.data.fog_color = {
  938. regs.fog_color.r.Value() / 255.0f,
  939. regs.fog_color.g.Value() / 255.0f,
  940. regs.fog_color.b.Value() / 255.0f
  941. };
  942. uniform_block_data.dirty = true;
  943. }
  944. void RasterizerOpenGL::SyncFogLUT() {
  945. std::array<GLuint, 128> new_data;
  946. std::transform(Pica::g_state.fog.lut.begin(), Pica::g_state.fog.lut.end(), new_data.begin(), [](const auto& entry) {
  947. return entry.raw;
  948. });
  949. if (new_data != fog_lut_data) {
  950. fog_lut_data = new_data;
  951. glActiveTexture(GL_TEXTURE9);
  952. glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 128, GL_RED_INTEGER, GL_UNSIGNED_INT, fog_lut_data.data());
  953. }
  954. }
  955. void RasterizerOpenGL::SyncAlphaTest() {
  956. const auto& regs = Pica::g_state.regs;
  957. if (regs.output_merger.alpha_test.ref != uniform_block_data.data.alphatest_ref) {
  958. uniform_block_data.data.alphatest_ref = regs.output_merger.alpha_test.ref;
  959. uniform_block_data.dirty = true;
  960. }
  961. }
  962. void RasterizerOpenGL::SyncLogicOp() {
  963. state.logic_op = PicaToGL::LogicOp(Pica::g_state.regs.output_merger.logic_op);
  964. }
  965. void RasterizerOpenGL::SyncColorWriteMask() {
  966. const auto& regs = Pica::g_state.regs;
  967. auto IsColorWriteEnabled = [&](u32 value) {
  968. return (regs.framebuffer.allow_color_write != 0 && value != 0) ? GL_TRUE : GL_FALSE;
  969. };
  970. state.color_mask.red_enabled = IsColorWriteEnabled(regs.output_merger.red_enable);
  971. state.color_mask.green_enabled = IsColorWriteEnabled(regs.output_merger.green_enable);
  972. state.color_mask.blue_enabled = IsColorWriteEnabled(regs.output_merger.blue_enable);
  973. state.color_mask.alpha_enabled = IsColorWriteEnabled(regs.output_merger.alpha_enable);
  974. }
  975. void RasterizerOpenGL::SyncStencilWriteMask() {
  976. const auto& regs = Pica::g_state.regs;
  977. state.stencil.write_mask = (regs.framebuffer.allow_depth_stencil_write != 0)
  978. ? static_cast<GLuint>(regs.output_merger.stencil_test.write_mask)
  979. : 0;
  980. }
  981. void RasterizerOpenGL::SyncDepthWriteMask() {
  982. const auto& regs = Pica::g_state.regs;
  983. state.depth.write_mask = (regs.framebuffer.allow_depth_stencil_write != 0 && regs.output_merger.depth_write_enable)
  984. ? GL_TRUE
  985. : GL_FALSE;
  986. }
  987. void RasterizerOpenGL::SyncStencilTest() {
  988. const auto& regs = Pica::g_state.regs;
  989. state.stencil.test_enabled = regs.output_merger.stencil_test.enable && regs.framebuffer.depth_format == Pica::Regs::DepthFormat::D24S8;
  990. state.stencil.test_func = PicaToGL::CompareFunc(regs.output_merger.stencil_test.func);
  991. state.stencil.test_ref = regs.output_merger.stencil_test.reference_value;
  992. state.stencil.test_mask = regs.output_merger.stencil_test.input_mask;
  993. state.stencil.action_stencil_fail = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_stencil_fail);
  994. state.stencil.action_depth_fail = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_depth_fail);
  995. state.stencil.action_depth_pass = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_depth_pass);
  996. }
  997. void RasterizerOpenGL::SyncDepthTest() {
  998. const auto& regs = Pica::g_state.regs;
  999. state.depth.test_enabled = regs.output_merger.depth_test_enable == 1 ||
  1000. regs.output_merger.depth_write_enable == 1;
  1001. state.depth.test_func = regs.output_merger.depth_test_enable == 1 ?
  1002. PicaToGL::CompareFunc(regs.output_merger.depth_test_func) : GL_ALWAYS;
  1003. }
  1004. void RasterizerOpenGL::SyncCombinerColor() {
  1005. auto combiner_color = PicaToGL::ColorRGBA8(Pica::g_state.regs.tev_combiner_buffer_color.raw);
  1006. if (combiner_color != uniform_block_data.data.tev_combiner_buffer_color) {
  1007. uniform_block_data.data.tev_combiner_buffer_color = combiner_color;
  1008. uniform_block_data.dirty = true;
  1009. }
  1010. }
  1011. void RasterizerOpenGL::SyncTevConstColor(int stage_index, const Pica::Regs::TevStageConfig& tev_stage) {
  1012. auto const_color = PicaToGL::ColorRGBA8(tev_stage.const_color);
  1013. if (const_color != uniform_block_data.data.const_color[stage_index]) {
  1014. uniform_block_data.data.const_color[stage_index] = const_color;
  1015. uniform_block_data.dirty = true;
  1016. }
  1017. }
  1018. void RasterizerOpenGL::SyncGlobalAmbient() {
  1019. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.global_ambient);
  1020. if (color != uniform_block_data.data.lighting_global_ambient) {
  1021. uniform_block_data.data.lighting_global_ambient = color;
  1022. uniform_block_data.dirty = true;
  1023. }
  1024. }
  1025. void RasterizerOpenGL::SyncLightingLUT(unsigned lut_index) {
  1026. std::array<GLvec4, 256> new_data;
  1027. for (unsigned offset = 0; offset < new_data.size(); ++offset) {
  1028. new_data[offset][0] = Pica::g_state.lighting.luts[(lut_index * 4) + 0][offset].ToFloat();
  1029. new_data[offset][1] = Pica::g_state.lighting.luts[(lut_index * 4) + 1][offset].ToFloat();
  1030. new_data[offset][2] = Pica::g_state.lighting.luts[(lut_index * 4) + 2][offset].ToFloat();
  1031. new_data[offset][3] = Pica::g_state.lighting.luts[(lut_index * 4) + 3][offset].ToFloat();
  1032. }
  1033. if (new_data != lighting_lut_data[lut_index]) {
  1034. lighting_lut_data[lut_index] = new_data;
  1035. glActiveTexture(GL_TEXTURE3 + lut_index);
  1036. glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT, lighting_lut_data[lut_index].data());
  1037. }
  1038. }
  1039. void RasterizerOpenGL::SyncLightSpecular0(int light_index) {
  1040. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].specular_0);
  1041. if (color != uniform_block_data.data.light_src[light_index].specular_0) {
  1042. uniform_block_data.data.light_src[light_index].specular_0 = color;
  1043. uniform_block_data.dirty = true;
  1044. }
  1045. }
  1046. void RasterizerOpenGL::SyncLightSpecular1(int light_index) {
  1047. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].specular_1);
  1048. if (color != uniform_block_data.data.light_src[light_index].specular_1) {
  1049. uniform_block_data.data.light_src[light_index].specular_1 = color;
  1050. uniform_block_data.dirty = true;
  1051. }
  1052. }
  1053. void RasterizerOpenGL::SyncLightDiffuse(int light_index) {
  1054. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].diffuse);
  1055. if (color != uniform_block_data.data.light_src[light_index].diffuse) {
  1056. uniform_block_data.data.light_src[light_index].diffuse = color;
  1057. uniform_block_data.dirty = true;
  1058. }
  1059. }
  1060. void RasterizerOpenGL::SyncLightAmbient(int light_index) {
  1061. auto color = PicaToGL::LightColor(Pica::g_state.regs.lighting.light[light_index].ambient);
  1062. if (color != uniform_block_data.data.light_src[light_index].ambient) {
  1063. uniform_block_data.data.light_src[light_index].ambient = color;
  1064. uniform_block_data.dirty = true;
  1065. }
  1066. }
  1067. void RasterizerOpenGL::SyncLightPosition(int light_index) {
  1068. GLvec3 position = {
  1069. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].x).ToFloat32(),
  1070. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].y).ToFloat32(),
  1071. Pica::float16::FromRaw(Pica::g_state.regs.lighting.light[light_index].z).ToFloat32() };
  1072. if (position != uniform_block_data.data.light_src[light_index].position) {
  1073. uniform_block_data.data.light_src[light_index].position = position;
  1074. uniform_block_data.dirty = true;
  1075. }
  1076. }
  1077. void RasterizerOpenGL::SyncLightDistanceAttenuationBias(int light_index) {
  1078. GLfloat dist_atten_bias = Pica::float20::FromRaw(Pica::g_state.regs.lighting.light[light_index].dist_atten_bias).ToFloat32();
  1079. if (dist_atten_bias != uniform_block_data.data.light_src[light_index].dist_atten_bias) {
  1080. uniform_block_data.data.light_src[light_index].dist_atten_bias = dist_atten_bias;
  1081. uniform_block_data.dirty = true;
  1082. }
  1083. }
  1084. void RasterizerOpenGL::SyncLightDistanceAttenuationScale(int light_index) {
  1085. GLfloat dist_atten_scale = Pica::float20::FromRaw(Pica::g_state.regs.lighting.light[light_index].dist_atten_scale).ToFloat32();
  1086. if (dist_atten_scale != uniform_block_data.data.light_src[light_index].dist_atten_scale) {
  1087. uniform_block_data.data.light_src[light_index].dist_atten_scale = dist_atten_scale;
  1088. uniform_block_data.dirty = true;
  1089. }
  1090. }