gl_rasterizer.cpp 55 KB

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