gl_rasterizer.cpp 68 KB

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