gl_rasterizer.cpp 44 KB

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