gl_rasterizer.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include <memory>
  6. #include "common/color.h"
  7. #include "common/math_util.h"
  8. #include "common/profiler.h"
  9. #include "core/hw/gpu.h"
  10. #include "core/memory.h"
  11. #include "core/settings.h"
  12. #include "video_core/pica.h"
  13. #include "video_core/utils.h"
  14. #include "video_core/renderer_opengl/gl_rasterizer.h"
  15. #include "video_core/renderer_opengl/gl_shaders.h"
  16. #include "video_core/renderer_opengl/gl_shader_util.h"
  17. #include "video_core/renderer_opengl/pica_to_gl.h"
  18. #include "generated/gl_3_2_core.h"
  19. static bool IsPassThroughTevStage(const Pica::Regs::TevStageConfig& stage) {
  20. return (stage.color_op == Pica::Regs::TevStageConfig::Operation::Replace &&
  21. stage.alpha_op == Pica::Regs::TevStageConfig::Operation::Replace &&
  22. stage.color_source1 == Pica::Regs::TevStageConfig::Source::Previous &&
  23. stage.alpha_source1 == Pica::Regs::TevStageConfig::Source::Previous &&
  24. stage.color_modifier1 == Pica::Regs::TevStageConfig::ColorModifier::SourceColor &&
  25. stage.alpha_modifier1 == Pica::Regs::TevStageConfig::AlphaModifier::SourceAlpha &&
  26. stage.GetColorMultiplier() == 1 &&
  27. stage.GetAlphaMultiplier() == 1);
  28. }
  29. RasterizerOpenGL::RasterizerOpenGL() : last_fb_color_addr(0), last_fb_depth_addr(0) { }
  30. RasterizerOpenGL::~RasterizerOpenGL() { }
  31. void RasterizerOpenGL::InitObjects() {
  32. // Create the hardware shader program and get attrib/uniform locations
  33. shader.Create(GLShaders::g_vertex_shader_hw, GLShaders::g_fragment_shader_hw);
  34. attrib_position = glGetAttribLocation(shader.handle, "vert_position");
  35. attrib_color = glGetAttribLocation(shader.handle, "vert_color");
  36. attrib_texcoords = glGetAttribLocation(shader.handle, "vert_texcoords");
  37. uniform_alphatest_enabled = glGetUniformLocation(shader.handle, "alphatest_enabled");
  38. uniform_alphatest_func = glGetUniformLocation(shader.handle, "alphatest_func");
  39. uniform_alphatest_ref = glGetUniformLocation(shader.handle, "alphatest_ref");
  40. uniform_tex = glGetUniformLocation(shader.handle, "tex");
  41. uniform_tev_combiner_buffer_color = glGetUniformLocation(shader.handle, "tev_combiner_buffer_color");
  42. const auto tev_stages = Pica::g_state.regs.GetTevStages();
  43. for (unsigned tev_stage_index = 0; tev_stage_index < tev_stages.size(); ++tev_stage_index) {
  44. auto& uniform_tev_cfg = uniform_tev_cfgs[tev_stage_index];
  45. std::string tev_ref_str = "tev_cfgs[" + std::to_string(tev_stage_index) + "]";
  46. uniform_tev_cfg.enabled = glGetUniformLocation(shader.handle, (tev_ref_str + ".enabled").c_str());
  47. uniform_tev_cfg.color_sources = glGetUniformLocation(shader.handle, (tev_ref_str + ".color_sources").c_str());
  48. uniform_tev_cfg.alpha_sources = glGetUniformLocation(shader.handle, (tev_ref_str + ".alpha_sources").c_str());
  49. uniform_tev_cfg.color_modifiers = glGetUniformLocation(shader.handle, (tev_ref_str + ".color_modifiers").c_str());
  50. uniform_tev_cfg.alpha_modifiers = glGetUniformLocation(shader.handle, (tev_ref_str + ".alpha_modifiers").c_str());
  51. uniform_tev_cfg.color_alpha_op = glGetUniformLocation(shader.handle, (tev_ref_str + ".color_alpha_op").c_str());
  52. uniform_tev_cfg.color_alpha_multiplier = glGetUniformLocation(shader.handle, (tev_ref_str + ".color_alpha_multiplier").c_str());
  53. uniform_tev_cfg.const_color = glGetUniformLocation(shader.handle, (tev_ref_str + ".const_color").c_str());
  54. uniform_tev_cfg.updates_combiner_buffer_color_alpha = glGetUniformLocation(shader.handle, (tev_ref_str + ".updates_combiner_buffer_color_alpha").c_str());
  55. }
  56. // Generate VBO and VAO
  57. vertex_buffer.Create();
  58. vertex_array.Create();
  59. // Update OpenGL state
  60. state.draw.vertex_array = vertex_array.handle;
  61. state.draw.vertex_buffer = vertex_buffer.handle;
  62. state.draw.shader_program = shader.handle;
  63. state.Apply();
  64. // Set the texture samplers to correspond to different texture units
  65. glUniform1i(uniform_tex, 0);
  66. glUniform1i(uniform_tex + 1, 1);
  67. glUniform1i(uniform_tex + 2, 2);
  68. // Set vertex attributes
  69. glVertexAttribPointer(attrib_position, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, position));
  70. glVertexAttribPointer(attrib_color, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, color));
  71. glVertexAttribPointer(attrib_texcoords, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord0));
  72. glVertexAttribPointer(attrib_texcoords + 1, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord1));
  73. glVertexAttribPointer(attrib_texcoords + 2, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord2));
  74. glEnableVertexAttribArray(attrib_position);
  75. glEnableVertexAttribArray(attrib_color);
  76. glEnableVertexAttribArray(attrib_texcoords);
  77. glEnableVertexAttribArray(attrib_texcoords + 1);
  78. glEnableVertexAttribArray(attrib_texcoords + 2);
  79. // Create textures for OGL framebuffer that will be rendered to, initially 1x1 to succeed in framebuffer creation
  80. fb_color_texture.texture.Create();
  81. ReconfigureColorTexture(fb_color_texture, Pica::Regs::ColorFormat::RGBA8, 1, 1);
  82. state.texture_units[0].texture_2d = fb_color_texture.texture.handle;
  83. state.Apply();
  84. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  85. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  86. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  87. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  88. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  89. state.texture_units[0].texture_2d = 0;
  90. state.Apply();
  91. fb_depth_texture.texture.Create();
  92. ReconfigureDepthTexture(fb_depth_texture, Pica::Regs::DepthFormat::D16, 1, 1);
  93. state.texture_units[0].texture_2d = fb_depth_texture.texture.handle;
  94. state.Apply();
  95. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  96. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  97. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  98. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  99. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  100. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  101. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  102. state.texture_units[0].texture_2d = 0;
  103. state.Apply();
  104. // Configure OpenGL framebuffer
  105. framebuffer.Create();
  106. state.draw.framebuffer = framebuffer.handle;
  107. state.Apply();
  108. glActiveTexture(GL_TEXTURE0);
  109. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_color_texture.texture.handle, 0);
  110. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, fb_depth_texture.texture.handle, 0);
  111. ASSERT_MSG(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE,
  112. "OpenGL rasterizer framebuffer setup failed, status %X", glCheckFramebufferStatus(GL_FRAMEBUFFER));
  113. }
  114. void RasterizerOpenGL::Reset() {
  115. const auto& regs = Pica::g_state.regs;
  116. SyncCullMode();
  117. SyncBlendEnabled();
  118. SyncBlendFuncs();
  119. SyncBlendColor();
  120. SyncAlphaTest();
  121. SyncLogicOp();
  122. SyncStencilTest();
  123. SyncDepthTest();
  124. // TEV stage 0
  125. SyncTevSources(0, regs.tev_stage0);
  126. SyncTevModifiers(0, regs.tev_stage0);
  127. SyncTevOps(0, regs.tev_stage0);
  128. SyncTevColor(0, regs.tev_stage0);
  129. SyncTevMultipliers(0, regs.tev_stage0);
  130. // TEV stage 1
  131. SyncTevSources(1, regs.tev_stage1);
  132. SyncTevModifiers(1, regs.tev_stage1);
  133. SyncTevOps(1, regs.tev_stage1);
  134. SyncTevColor(1, regs.tev_stage1);
  135. SyncTevMultipliers(1, regs.tev_stage1);
  136. // TEV stage 2
  137. SyncTevSources(2, regs.tev_stage2);
  138. SyncTevModifiers(2, regs.tev_stage2);
  139. SyncTevOps(2, regs.tev_stage2);
  140. SyncTevColor(2, regs.tev_stage2);
  141. SyncTevMultipliers(2, regs.tev_stage2);
  142. // TEV stage 3
  143. SyncTevSources(3, regs.tev_stage3);
  144. SyncTevModifiers(3, regs.tev_stage3);
  145. SyncTevOps(3, regs.tev_stage3);
  146. SyncTevColor(3, regs.tev_stage3);
  147. SyncTevMultipliers(3, regs.tev_stage3);
  148. // TEV stage 4
  149. SyncTevSources(4, regs.tev_stage4);
  150. SyncTevModifiers(4, regs.tev_stage4);
  151. SyncTevOps(4, regs.tev_stage4);
  152. SyncTevColor(4, regs.tev_stage4);
  153. SyncTevMultipliers(4, regs.tev_stage4);
  154. // TEV stage 5
  155. SyncTevSources(5, regs.tev_stage5);
  156. SyncTevModifiers(5, regs.tev_stage5);
  157. SyncTevOps(5, regs.tev_stage5);
  158. SyncTevColor(5, regs.tev_stage5);
  159. SyncTevMultipliers(5, regs.tev_stage5);
  160. SyncCombinerColor();
  161. SyncCombinerWriteFlags();
  162. res_cache.FullFlush();
  163. }
  164. void RasterizerOpenGL::AddTriangle(const Pica::Shader::OutputVertex& v0,
  165. const Pica::Shader::OutputVertex& v1,
  166. const Pica::Shader::OutputVertex& v2) {
  167. vertex_batch.push_back(HardwareVertex(v0));
  168. vertex_batch.push_back(HardwareVertex(v1));
  169. vertex_batch.push_back(HardwareVertex(v2));
  170. }
  171. void RasterizerOpenGL::DrawTriangles() {
  172. SyncFramebuffer();
  173. SyncDrawState();
  174. glBufferData(GL_ARRAY_BUFFER, vertex_batch.size() * sizeof(HardwareVertex), vertex_batch.data(), GL_STREAM_DRAW);
  175. glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertex_batch.size());
  176. vertex_batch.clear();
  177. // Flush the resource cache at the current depth and color framebuffer addresses for render-to-texture
  178. const auto& regs = Pica::g_state.regs;
  179. PAddr cur_fb_color_addr = regs.framebuffer.GetColorBufferPhysicalAddress();
  180. u32 cur_fb_color_size = Pica::Regs::BytesPerColorPixel(regs.framebuffer.color_format)
  181. * regs.framebuffer.GetWidth() * regs.framebuffer.GetHeight();
  182. PAddr cur_fb_depth_addr = regs.framebuffer.GetDepthBufferPhysicalAddress();
  183. u32 cur_fb_depth_size = Pica::Regs::BytesPerDepthPixel(regs.framebuffer.depth_format)
  184. * regs.framebuffer.GetWidth() * regs.framebuffer.GetHeight();
  185. res_cache.NotifyFlush(cur_fb_color_addr, cur_fb_color_size);
  186. res_cache.NotifyFlush(cur_fb_depth_addr, cur_fb_depth_size);
  187. }
  188. void RasterizerOpenGL::CommitFramebuffer() {
  189. CommitColorBuffer();
  190. CommitDepthBuffer();
  191. }
  192. void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
  193. const auto& regs = Pica::g_state.regs;
  194. if (!Settings::values.use_hw_renderer)
  195. return;
  196. switch(id) {
  197. // Culling
  198. case PICA_REG_INDEX(cull_mode):
  199. SyncCullMode();
  200. break;
  201. // Blending
  202. case PICA_REG_INDEX(output_merger.alphablend_enable):
  203. SyncBlendEnabled();
  204. break;
  205. case PICA_REG_INDEX(output_merger.alpha_blending):
  206. SyncBlendFuncs();
  207. break;
  208. case PICA_REG_INDEX(output_merger.blend_const):
  209. SyncBlendColor();
  210. break;
  211. // Alpha test
  212. case PICA_REG_INDEX(output_merger.alpha_test):
  213. SyncAlphaTest();
  214. break;
  215. // Stencil test
  216. case PICA_REG_INDEX(output_merger.stencil_test.raw_func):
  217. case PICA_REG_INDEX(output_merger.stencil_test.raw_op):
  218. SyncStencilTest();
  219. break;
  220. // Depth test
  221. case PICA_REG_INDEX(output_merger.depth_test_enable):
  222. SyncDepthTest();
  223. break;
  224. // Logic op
  225. case PICA_REG_INDEX(output_merger.logic_op):
  226. SyncLogicOp();
  227. break;
  228. // TEV stage 0
  229. case PICA_REG_INDEX(tev_stage0.color_source1):
  230. SyncTevSources(0, regs.tev_stage0);
  231. break;
  232. case PICA_REG_INDEX(tev_stage0.color_modifier1):
  233. SyncTevModifiers(0, regs.tev_stage0);
  234. break;
  235. case PICA_REG_INDEX(tev_stage0.color_op):
  236. SyncTevOps(0, regs.tev_stage0);
  237. break;
  238. case PICA_REG_INDEX(tev_stage0.const_r):
  239. SyncTevColor(0, regs.tev_stage0);
  240. break;
  241. case PICA_REG_INDEX(tev_stage0.color_scale):
  242. SyncTevMultipliers(0, regs.tev_stage0);
  243. break;
  244. // TEV stage 1
  245. case PICA_REG_INDEX(tev_stage1.color_source1):
  246. SyncTevSources(1, regs.tev_stage1);
  247. break;
  248. case PICA_REG_INDEX(tev_stage1.color_modifier1):
  249. SyncTevModifiers(1, regs.tev_stage1);
  250. break;
  251. case PICA_REG_INDEX(tev_stage1.color_op):
  252. SyncTevOps(1, regs.tev_stage1);
  253. break;
  254. case PICA_REG_INDEX(tev_stage1.const_r):
  255. SyncTevColor(1, regs.tev_stage1);
  256. break;
  257. case PICA_REG_INDEX(tev_stage1.color_scale):
  258. SyncTevMultipliers(1, regs.tev_stage1);
  259. break;
  260. // TEV stage 2
  261. case PICA_REG_INDEX(tev_stage2.color_source1):
  262. SyncTevSources(2, regs.tev_stage2);
  263. break;
  264. case PICA_REG_INDEX(tev_stage2.color_modifier1):
  265. SyncTevModifiers(2, regs.tev_stage2);
  266. break;
  267. case PICA_REG_INDEX(tev_stage2.color_op):
  268. SyncTevOps(2, regs.tev_stage2);
  269. break;
  270. case PICA_REG_INDEX(tev_stage2.const_r):
  271. SyncTevColor(2, regs.tev_stage2);
  272. break;
  273. case PICA_REG_INDEX(tev_stage2.color_scale):
  274. SyncTevMultipliers(2, regs.tev_stage2);
  275. break;
  276. // TEV stage 3
  277. case PICA_REG_INDEX(tev_stage3.color_source1):
  278. SyncTevSources(3, regs.tev_stage3);
  279. break;
  280. case PICA_REG_INDEX(tev_stage3.color_modifier1):
  281. SyncTevModifiers(3, regs.tev_stage3);
  282. break;
  283. case PICA_REG_INDEX(tev_stage3.color_op):
  284. SyncTevOps(3, regs.tev_stage3);
  285. break;
  286. case PICA_REG_INDEX(tev_stage3.const_r):
  287. SyncTevColor(3, regs.tev_stage3);
  288. break;
  289. case PICA_REG_INDEX(tev_stage3.color_scale):
  290. SyncTevMultipliers(3, regs.tev_stage3);
  291. break;
  292. // TEV stage 4
  293. case PICA_REG_INDEX(tev_stage4.color_source1):
  294. SyncTevSources(4, regs.tev_stage4);
  295. break;
  296. case PICA_REG_INDEX(tev_stage4.color_modifier1):
  297. SyncTevModifiers(4, regs.tev_stage4);
  298. break;
  299. case PICA_REG_INDEX(tev_stage4.color_op):
  300. SyncTevOps(4, regs.tev_stage4);
  301. break;
  302. case PICA_REG_INDEX(tev_stage4.const_r):
  303. SyncTevColor(4, regs.tev_stage4);
  304. break;
  305. case PICA_REG_INDEX(tev_stage4.color_scale):
  306. SyncTevMultipliers(4, regs.tev_stage4);
  307. break;
  308. // TEV stage 5
  309. case PICA_REG_INDEX(tev_stage5.color_source1):
  310. SyncTevSources(5, regs.tev_stage5);
  311. break;
  312. case PICA_REG_INDEX(tev_stage5.color_modifier1):
  313. SyncTevModifiers(5, regs.tev_stage5);
  314. break;
  315. case PICA_REG_INDEX(tev_stage5.color_op):
  316. SyncTevOps(5, regs.tev_stage5);
  317. break;
  318. case PICA_REG_INDEX(tev_stage5.const_r):
  319. SyncTevColor(5, regs.tev_stage5);
  320. break;
  321. case PICA_REG_INDEX(tev_stage5.color_scale):
  322. SyncTevMultipliers(5, regs.tev_stage5);
  323. break;
  324. // TEV combiner buffer color
  325. case PICA_REG_INDEX(tev_combiner_buffer_color):
  326. SyncCombinerColor();
  327. break;
  328. // TEV combiner buffer write flags
  329. case PICA_REG_INDEX(tev_combiner_buffer_input):
  330. SyncCombinerWriteFlags();
  331. break;
  332. }
  333. }
  334. void RasterizerOpenGL::NotifyPreRead(PAddr addr, u32 size) {
  335. const auto& regs = Pica::g_state.regs;
  336. if (!Settings::values.use_hw_renderer)
  337. return;
  338. PAddr cur_fb_color_addr = regs.framebuffer.GetColorBufferPhysicalAddress();
  339. u32 cur_fb_color_size = Pica::Regs::BytesPerColorPixel(regs.framebuffer.color_format)
  340. * regs.framebuffer.GetWidth() * regs.framebuffer.GetHeight();
  341. PAddr cur_fb_depth_addr = regs.framebuffer.GetDepthBufferPhysicalAddress();
  342. u32 cur_fb_depth_size = Pica::Regs::BytesPerDepthPixel(regs.framebuffer.depth_format)
  343. * regs.framebuffer.GetWidth() * regs.framebuffer.GetHeight();
  344. // If source memory region overlaps 3DS framebuffers, commit them before the copy happens
  345. if (MathUtil::IntervalsIntersect(addr, size, cur_fb_color_addr, cur_fb_color_size))
  346. CommitColorBuffer();
  347. if (MathUtil::IntervalsIntersect(addr, size, cur_fb_depth_addr, cur_fb_depth_size))
  348. CommitDepthBuffer();
  349. }
  350. void RasterizerOpenGL::NotifyFlush(PAddr addr, u32 size) {
  351. const auto& regs = Pica::g_state.regs;
  352. if (!Settings::values.use_hw_renderer)
  353. return;
  354. PAddr cur_fb_color_addr = regs.framebuffer.GetColorBufferPhysicalAddress();
  355. u32 cur_fb_color_size = Pica::Regs::BytesPerColorPixel(regs.framebuffer.color_format)
  356. * regs.framebuffer.GetWidth() * regs.framebuffer.GetHeight();
  357. PAddr cur_fb_depth_addr = regs.framebuffer.GetDepthBufferPhysicalAddress();
  358. u32 cur_fb_depth_size = Pica::Regs::BytesPerDepthPixel(regs.framebuffer.depth_format)
  359. * regs.framebuffer.GetWidth() * regs.framebuffer.GetHeight();
  360. // If modified memory region overlaps 3DS framebuffers, reload their contents into OpenGL
  361. if (MathUtil::IntervalsIntersect(addr, size, cur_fb_color_addr, cur_fb_color_size))
  362. ReloadColorBuffer();
  363. if (MathUtil::IntervalsIntersect(addr, size, cur_fb_depth_addr, cur_fb_depth_size))
  364. ReloadDepthBuffer();
  365. // Notify cache of flush in case the region touches a cached resource
  366. res_cache.NotifyFlush(addr, size);
  367. }
  368. void RasterizerOpenGL::ReconfigureColorTexture(TextureInfo& texture, Pica::Regs::ColorFormat format, u32 width, u32 height) {
  369. GLint internal_format;
  370. texture.format = format;
  371. texture.width = width;
  372. texture.height = height;
  373. switch (format) {
  374. case Pica::Regs::ColorFormat::RGBA8:
  375. internal_format = GL_RGBA;
  376. texture.gl_format = GL_RGBA;
  377. texture.gl_type = GL_UNSIGNED_INT_8_8_8_8;
  378. break;
  379. case Pica::Regs::ColorFormat::RGB8:
  380. // This pixel format uses BGR since GL_UNSIGNED_BYTE specifies byte-order, unlike every
  381. // specific OpenGL type used in this function using native-endian (that is, little-endian
  382. // mostly everywhere) for words or half-words.
  383. // TODO: check how those behave on big-endian processors.
  384. internal_format = GL_RGB;
  385. texture.gl_format = GL_BGR;
  386. texture.gl_type = GL_UNSIGNED_BYTE;
  387. break;
  388. case Pica::Regs::ColorFormat::RGB5A1:
  389. internal_format = GL_RGBA;
  390. texture.gl_format = GL_RGBA;
  391. texture.gl_type = GL_UNSIGNED_SHORT_5_5_5_1;
  392. break;
  393. case Pica::Regs::ColorFormat::RGB565:
  394. internal_format = GL_RGB;
  395. texture.gl_format = GL_RGB;
  396. texture.gl_type = GL_UNSIGNED_SHORT_5_6_5;
  397. break;
  398. case Pica::Regs::ColorFormat::RGBA4:
  399. internal_format = GL_RGBA;
  400. texture.gl_format = GL_RGBA;
  401. texture.gl_type = GL_UNSIGNED_SHORT_4_4_4_4;
  402. break;
  403. default:
  404. LOG_CRITICAL(Render_OpenGL, "Unknown framebuffer texture color format %x", format);
  405. UNIMPLEMENTED();
  406. break;
  407. }
  408. state.texture_units[0].texture_2d = texture.texture.handle;
  409. state.Apply();
  410. glActiveTexture(GL_TEXTURE0);
  411. glTexImage2D(GL_TEXTURE_2D, 0, internal_format, texture.width, texture.height, 0,
  412. texture.gl_format, texture.gl_type, nullptr);
  413. state.texture_units[0].texture_2d = 0;
  414. state.Apply();
  415. }
  416. void RasterizerOpenGL::ReconfigureDepthTexture(DepthTextureInfo& texture, Pica::Regs::DepthFormat format, u32 width, u32 height) {
  417. GLint internal_format;
  418. texture.format = format;
  419. texture.width = width;
  420. texture.height = height;
  421. switch (format) {
  422. case Pica::Regs::DepthFormat::D16:
  423. internal_format = GL_DEPTH_COMPONENT16;
  424. texture.gl_format = GL_DEPTH_COMPONENT;
  425. texture.gl_type = GL_UNSIGNED_SHORT;
  426. break;
  427. case Pica::Regs::DepthFormat::D24:
  428. internal_format = GL_DEPTH_COMPONENT24;
  429. texture.gl_format = GL_DEPTH_COMPONENT;
  430. texture.gl_type = GL_UNSIGNED_INT;
  431. break;
  432. case Pica::Regs::DepthFormat::D24S8:
  433. internal_format = GL_DEPTH24_STENCIL8;
  434. texture.gl_format = GL_DEPTH_STENCIL;
  435. texture.gl_type = GL_UNSIGNED_INT_24_8;
  436. break;
  437. default:
  438. LOG_CRITICAL(Render_OpenGL, "Unknown framebuffer texture depth format %x", format);
  439. UNIMPLEMENTED();
  440. break;
  441. }
  442. state.texture_units[0].texture_2d = texture.texture.handle;
  443. state.Apply();
  444. glActiveTexture(GL_TEXTURE0);
  445. glTexImage2D(GL_TEXTURE_2D, 0, internal_format, texture.width, texture.height, 0,
  446. texture.gl_format, texture.gl_type, nullptr);
  447. state.texture_units[0].texture_2d = 0;
  448. state.Apply();
  449. }
  450. void RasterizerOpenGL::SyncFramebuffer() {
  451. const auto& regs = Pica::g_state.regs;
  452. PAddr cur_fb_color_addr = regs.framebuffer.GetColorBufferPhysicalAddress();
  453. Pica::Regs::ColorFormat new_fb_color_format = regs.framebuffer.color_format;
  454. PAddr cur_fb_depth_addr = regs.framebuffer.GetDepthBufferPhysicalAddress();
  455. Pica::Regs::DepthFormat new_fb_depth_format = regs.framebuffer.depth_format;
  456. bool fb_size_changed = fb_color_texture.width != regs.framebuffer.GetWidth() ||
  457. fb_color_texture.height != regs.framebuffer.GetHeight();
  458. bool color_fb_prop_changed = fb_color_texture.format != new_fb_color_format ||
  459. fb_size_changed;
  460. bool depth_fb_prop_changed = fb_depth_texture.format != new_fb_depth_format ||
  461. fb_size_changed;
  462. bool color_fb_modified = last_fb_color_addr != cur_fb_color_addr ||
  463. color_fb_prop_changed;
  464. bool depth_fb_modified = last_fb_depth_addr != cur_fb_depth_addr ||
  465. depth_fb_prop_changed;
  466. // Commit if framebuffer modified in any way
  467. if (color_fb_modified)
  468. CommitColorBuffer();
  469. if (depth_fb_modified)
  470. CommitDepthBuffer();
  471. // Reconfigure framebuffer textures if any property has changed
  472. if (color_fb_prop_changed) {
  473. ReconfigureColorTexture(fb_color_texture, new_fb_color_format,
  474. regs.framebuffer.GetWidth(), regs.framebuffer.GetHeight());
  475. }
  476. if (depth_fb_prop_changed) {
  477. ReconfigureDepthTexture(fb_depth_texture, new_fb_depth_format,
  478. regs.framebuffer.GetWidth(), regs.framebuffer.GetHeight());
  479. // Only attach depth buffer as stencil if it supports stencil
  480. switch (new_fb_depth_format) {
  481. case Pica::Regs::DepthFormat::D16:
  482. case Pica::Regs::DepthFormat::D24:
  483. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  484. break;
  485. case Pica::Regs::DepthFormat::D24S8:
  486. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, fb_depth_texture.texture.handle, 0);
  487. break;
  488. default:
  489. LOG_CRITICAL(Render_OpenGL, "Unknown framebuffer depth format %x", new_fb_depth_format);
  490. UNIMPLEMENTED();
  491. break;
  492. }
  493. }
  494. // Load buffer data again if fb modified in any way
  495. if (color_fb_modified) {
  496. last_fb_color_addr = cur_fb_color_addr;
  497. ReloadColorBuffer();
  498. }
  499. if (depth_fb_modified) {
  500. last_fb_depth_addr = cur_fb_depth_addr;
  501. ReloadDepthBuffer();
  502. }
  503. }
  504. void RasterizerOpenGL::SyncCullMode() {
  505. const auto& regs = Pica::g_state.regs;
  506. switch (regs.cull_mode) {
  507. case Pica::Regs::CullMode::KeepAll:
  508. state.cull.enabled = false;
  509. break;
  510. case Pica::Regs::CullMode::KeepClockWise:
  511. state.cull.enabled = true;
  512. state.cull.mode = GL_BACK;
  513. break;
  514. case Pica::Regs::CullMode::KeepCounterClockWise:
  515. state.cull.enabled = true;
  516. state.cull.mode = GL_FRONT;
  517. break;
  518. default:
  519. LOG_CRITICAL(Render_OpenGL, "Unknown cull mode %d", regs.cull_mode.Value());
  520. UNIMPLEMENTED();
  521. break;
  522. }
  523. }
  524. void RasterizerOpenGL::SyncBlendEnabled() {
  525. state.blend.enabled = (Pica::g_state.regs.output_merger.alphablend_enable == 1);
  526. }
  527. void RasterizerOpenGL::SyncBlendFuncs() {
  528. const auto& regs = Pica::g_state.regs;
  529. state.blend.src_rgb_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_source_rgb);
  530. state.blend.dst_rgb_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_dest_rgb);
  531. state.blend.src_a_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_source_a);
  532. state.blend.dst_a_func = PicaToGL::BlendFunc(regs.output_merger.alpha_blending.factor_dest_a);
  533. }
  534. void RasterizerOpenGL::SyncBlendColor() {
  535. auto blend_color = PicaToGL::ColorRGBA8((u8*)&Pica::g_state.regs.output_merger.blend_const.r);
  536. state.blend.color.red = blend_color[0];
  537. state.blend.color.green = blend_color[1];
  538. state.blend.color.blue = blend_color[2];
  539. state.blend.color.alpha = blend_color[3];
  540. }
  541. void RasterizerOpenGL::SyncAlphaTest() {
  542. const auto& regs = Pica::g_state.regs;
  543. glUniform1i(uniform_alphatest_enabled, regs.output_merger.alpha_test.enable);
  544. glUniform1i(uniform_alphatest_func, (GLint)regs.output_merger.alpha_test.func.Value());
  545. glUniform1f(uniform_alphatest_ref, regs.output_merger.alpha_test.ref / 255.0f);
  546. }
  547. void RasterizerOpenGL::SyncLogicOp() {
  548. state.logic_op = PicaToGL::LogicOp(Pica::g_state.regs.output_merger.logic_op);
  549. }
  550. void RasterizerOpenGL::SyncStencilTest() {
  551. const auto& regs = Pica::g_state.regs;
  552. state.stencil.test_enabled = regs.output_merger.stencil_test.enable && regs.framebuffer.depth_format == Pica::Regs::DepthFormat::D24S8;
  553. state.stencil.test_func = PicaToGL::CompareFunc(regs.output_merger.stencil_test.func);
  554. state.stencil.test_ref = regs.output_merger.stencil_test.reference_value;
  555. state.stencil.test_mask = regs.output_merger.stencil_test.input_mask;
  556. state.stencil.write_mask = regs.output_merger.stencil_test.write_mask;
  557. state.stencil.action_stencil_fail = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_stencil_fail);
  558. state.stencil.action_depth_fail = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_depth_fail);
  559. state.stencil.action_depth_pass = PicaToGL::StencilOp(regs.output_merger.stencil_test.action_depth_pass);
  560. }
  561. void RasterizerOpenGL::SyncDepthTest() {
  562. const auto& regs = Pica::g_state.regs;
  563. state.depth.test_enabled = (regs.output_merger.depth_test_enable == 1);
  564. state.depth.test_func = PicaToGL::CompareFunc(regs.output_merger.depth_test_func);
  565. state.color_mask.red_enabled = regs.output_merger.red_enable;
  566. state.color_mask.green_enabled = regs.output_merger.green_enable;
  567. state.color_mask.blue_enabled = regs.output_merger.blue_enable;
  568. state.color_mask.alpha_enabled = regs.output_merger.alpha_enable;
  569. state.depth.write_mask = regs.output_merger.depth_write_enable ? GL_TRUE : GL_FALSE;
  570. }
  571. void RasterizerOpenGL::SyncTevSources(unsigned stage_index, const Pica::Regs::TevStageConfig& config) {
  572. GLint color_srcs[3] = { (GLint)config.color_source1.Value(),
  573. (GLint)config.color_source2.Value(),
  574. (GLint)config.color_source3.Value() };
  575. GLint alpha_srcs[3] = { (GLint)config.alpha_source1.Value(),
  576. (GLint)config.alpha_source2.Value(),
  577. (GLint)config.alpha_source3.Value() };
  578. glUniform3iv(uniform_tev_cfgs[stage_index].color_sources, 1, color_srcs);
  579. glUniform3iv(uniform_tev_cfgs[stage_index].alpha_sources, 1, alpha_srcs);
  580. }
  581. void RasterizerOpenGL::SyncTevModifiers(unsigned stage_index, const Pica::Regs::TevStageConfig& config) {
  582. GLint color_mods[3] = { (GLint)config.color_modifier1.Value(),
  583. (GLint)config.color_modifier2.Value(),
  584. (GLint)config.color_modifier3.Value() };
  585. GLint alpha_mods[3] = { (GLint)config.alpha_modifier1.Value(),
  586. (GLint)config.alpha_modifier2.Value(),
  587. (GLint)config.alpha_modifier3.Value() };
  588. glUniform3iv(uniform_tev_cfgs[stage_index].color_modifiers, 1, color_mods);
  589. glUniform3iv(uniform_tev_cfgs[stage_index].alpha_modifiers, 1, alpha_mods);
  590. }
  591. void RasterizerOpenGL::SyncTevOps(unsigned stage_index, const Pica::Regs::TevStageConfig& config) {
  592. glUniform2i(uniform_tev_cfgs[stage_index].color_alpha_op, (GLint)config.color_op.Value(), (GLint)config.alpha_op.Value());
  593. }
  594. void RasterizerOpenGL::SyncTevColor(unsigned stage_index, const Pica::Regs::TevStageConfig& config) {
  595. auto const_color = PicaToGL::ColorRGBA8((u8*)&config.const_r);
  596. glUniform4fv(uniform_tev_cfgs[stage_index].const_color, 1, const_color.data());
  597. }
  598. void RasterizerOpenGL::SyncTevMultipliers(unsigned stage_index, const Pica::Regs::TevStageConfig& config) {
  599. glUniform2i(uniform_tev_cfgs[stage_index].color_alpha_multiplier, config.GetColorMultiplier(), config.GetAlphaMultiplier());
  600. }
  601. void RasterizerOpenGL::SyncCombinerColor() {
  602. auto combiner_color = PicaToGL::ColorRGBA8((u8*)&Pica::g_state.regs.tev_combiner_buffer_color.r);
  603. glUniform4fv(uniform_tev_combiner_buffer_color, 1, combiner_color.data());
  604. }
  605. void RasterizerOpenGL::SyncCombinerWriteFlags() {
  606. const auto& regs = Pica::g_state.regs;
  607. const auto tev_stages = regs.GetTevStages();
  608. for (unsigned tev_stage_index = 0; tev_stage_index < tev_stages.size(); ++tev_stage_index) {
  609. glUniform2i(uniform_tev_cfgs[tev_stage_index].updates_combiner_buffer_color_alpha,
  610. regs.tev_combiner_buffer_input.TevStageUpdatesCombinerBufferColor(tev_stage_index),
  611. regs.tev_combiner_buffer_input.TevStageUpdatesCombinerBufferAlpha(tev_stage_index));
  612. }
  613. }
  614. void RasterizerOpenGL::SyncDrawState() {
  615. const auto& regs = Pica::g_state.regs;
  616. // Sync the viewport
  617. GLsizei viewport_width = (GLsizei)Pica::float24::FromRawFloat24(regs.viewport_size_x).ToFloat32() * 2;
  618. GLsizei viewport_height = (GLsizei)Pica::float24::FromRawFloat24(regs.viewport_size_y).ToFloat32() * 2;
  619. // OpenGL uses different y coordinates, so negate corner offset and flip origin
  620. // TODO: Ensure viewport_corner.x should not be negated or origin flipped
  621. // TODO: Use floating-point viewports for accuracy if supported
  622. glViewport((GLsizei)static_cast<float>(regs.viewport_corner.x),
  623. -(GLsizei)static_cast<float>(regs.viewport_corner.y)
  624. + regs.framebuffer.GetHeight() - viewport_height,
  625. viewport_width, viewport_height);
  626. // Sync bound texture(s), upload if not cached
  627. const auto pica_textures = regs.GetTextures();
  628. for (unsigned texture_index = 0; texture_index < pica_textures.size(); ++texture_index) {
  629. const auto& texture = pica_textures[texture_index];
  630. if (texture.enabled) {
  631. res_cache.LoadAndBindTexture(state, texture_index, texture);
  632. } else {
  633. state.texture_units[texture_index].texture_2d = 0;
  634. }
  635. }
  636. // Skip processing TEV stages that simply pass the previous stage results through
  637. const auto tev_stages = regs.GetTevStages();
  638. for (unsigned tev_stage_index = 0; tev_stage_index < tev_stages.size(); ++tev_stage_index) {
  639. glUniform1i(uniform_tev_cfgs[tev_stage_index].enabled, !IsPassThroughTevStage(tev_stages[tev_stage_index]));
  640. }
  641. state.Apply();
  642. }
  643. void RasterizerOpenGL::ReloadColorBuffer() {
  644. u8* color_buffer = Memory::GetPhysicalPointer(Pica::g_state.regs.framebuffer.GetColorBufferPhysicalAddress());
  645. if (color_buffer == nullptr)
  646. return;
  647. u32 bytes_per_pixel = Pica::Regs::BytesPerColorPixel(fb_color_texture.format);
  648. std::unique_ptr<u8[]> temp_fb_color_buffer(new u8[fb_color_texture.width * fb_color_texture.height * bytes_per_pixel]);
  649. // Directly copy pixels. Internal OpenGL color formats are consistent so no conversion is necessary.
  650. for (int y = 0; y < fb_color_texture.height; ++y) {
  651. for (int x = 0; x < fb_color_texture.width; ++x) {
  652. const u32 coarse_y = y & ~7;
  653. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_color_texture.width * bytes_per_pixel;
  654. u32 gl_pixel_index = (x + y * fb_color_texture.width) * bytes_per_pixel;
  655. u8* pixel = color_buffer + dst_offset;
  656. memcpy(&temp_fb_color_buffer[gl_pixel_index], pixel, bytes_per_pixel);
  657. }
  658. }
  659. state.texture_units[0].texture_2d = fb_color_texture.texture.handle;
  660. state.Apply();
  661. glActiveTexture(GL_TEXTURE0);
  662. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, fb_color_texture.width, fb_color_texture.height,
  663. fb_color_texture.gl_format, fb_color_texture.gl_type, temp_fb_color_buffer.get());
  664. state.texture_units[0].texture_2d = 0;
  665. state.Apply();
  666. }
  667. void RasterizerOpenGL::ReloadDepthBuffer() {
  668. PAddr depth_buffer_addr = Pica::g_state.regs.framebuffer.GetDepthBufferPhysicalAddress();
  669. if (depth_buffer_addr == 0)
  670. return;
  671. // TODO: Appears to work, but double-check endianness of depth values and order of depth-stencil
  672. u8* depth_buffer = Memory::GetPhysicalPointer(depth_buffer_addr);
  673. if (depth_buffer == nullptr)
  674. return;
  675. u32 bytes_per_pixel = Pica::Regs::BytesPerDepthPixel(fb_depth_texture.format);
  676. // OpenGL needs 4 bpp alignment for D24
  677. u32 gl_bpp = bytes_per_pixel == 3 ? 4 : bytes_per_pixel;
  678. std::unique_ptr<u8[]> temp_fb_depth_buffer(new u8[fb_depth_texture.width * fb_depth_texture.height * gl_bpp]);
  679. u8* temp_fb_depth_data = bytes_per_pixel == 3 ? (temp_fb_depth_buffer.get() + 1) : temp_fb_depth_buffer.get();
  680. if (fb_depth_texture.format == Pica::Regs::DepthFormat::D24S8) {
  681. for (int y = 0; y < fb_depth_texture.height; ++y) {
  682. for (int x = 0; x < fb_depth_texture.width; ++x) {
  683. const u32 coarse_y = y & ~7;
  684. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
  685. u32 gl_pixel_index = (x + y * fb_depth_texture.width);
  686. u8* pixel = depth_buffer + dst_offset;
  687. u32 depth_stencil = *(u32*)pixel;
  688. ((u32*)temp_fb_depth_data)[gl_pixel_index] = (depth_stencil << 8) | (depth_stencil >> 24);
  689. }
  690. }
  691. } else {
  692. for (int y = 0; y < fb_depth_texture.height; ++y) {
  693. for (int x = 0; x < fb_depth_texture.width; ++x) {
  694. const u32 coarse_y = y & ~7;
  695. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
  696. u32 gl_pixel_index = (x + y * fb_depth_texture.width) * gl_bpp;
  697. u8* pixel = depth_buffer + dst_offset;
  698. memcpy(&temp_fb_depth_data[gl_pixel_index], pixel, bytes_per_pixel);
  699. }
  700. }
  701. }
  702. state.texture_units[0].texture_2d = fb_depth_texture.texture.handle;
  703. state.Apply();
  704. glActiveTexture(GL_TEXTURE0);
  705. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, fb_depth_texture.width, fb_depth_texture.height,
  706. fb_depth_texture.gl_format, fb_depth_texture.gl_type, temp_fb_depth_buffer.get());
  707. state.texture_units[0].texture_2d = 0;
  708. state.Apply();
  709. }
  710. Common::Profiling::TimingCategory buffer_commit_category("Framebuffer Commit");
  711. void RasterizerOpenGL::CommitColorBuffer() {
  712. if (last_fb_color_addr != 0) {
  713. u8* color_buffer = Memory::GetPhysicalPointer(last_fb_color_addr);
  714. if (color_buffer != nullptr) {
  715. Common::Profiling::ScopeTimer timer(buffer_commit_category);
  716. u32 bytes_per_pixel = Pica::Regs::BytesPerColorPixel(fb_color_texture.format);
  717. std::unique_ptr<u8[]> temp_gl_color_buffer(new u8[fb_color_texture.width * fb_color_texture.height * bytes_per_pixel]);
  718. state.texture_units[0].texture_2d = fb_color_texture.texture.handle;
  719. state.Apply();
  720. glActiveTexture(GL_TEXTURE0);
  721. glGetTexImage(GL_TEXTURE_2D, 0, fb_color_texture.gl_format, fb_color_texture.gl_type, temp_gl_color_buffer.get());
  722. state.texture_units[0].texture_2d = 0;
  723. state.Apply();
  724. // Directly copy pixels. Internal OpenGL color formats are consistent so no conversion is necessary.
  725. for (int y = 0; y < fb_color_texture.height; ++y) {
  726. for (int x = 0; x < fb_color_texture.width; ++x) {
  727. const u32 coarse_y = y & ~7;
  728. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_color_texture.width * bytes_per_pixel;
  729. u32 gl_pixel_index = x * bytes_per_pixel + y * fb_color_texture.width * bytes_per_pixel;
  730. u8* pixel = color_buffer + dst_offset;
  731. memcpy(pixel, &temp_gl_color_buffer[gl_pixel_index], bytes_per_pixel);
  732. }
  733. }
  734. }
  735. }
  736. }
  737. void RasterizerOpenGL::CommitDepthBuffer() {
  738. if (last_fb_depth_addr != 0) {
  739. // TODO: Output seems correct visually, but doesn't quite match sw renderer output. One of them is wrong.
  740. u8* depth_buffer = Memory::GetPhysicalPointer(last_fb_depth_addr);
  741. if (depth_buffer != nullptr) {
  742. Common::Profiling::ScopeTimer timer(buffer_commit_category);
  743. u32 bytes_per_pixel = Pica::Regs::BytesPerDepthPixel(fb_depth_texture.format);
  744. // OpenGL needs 4 bpp alignment for D24
  745. u32 gl_bpp = bytes_per_pixel == 3 ? 4 : bytes_per_pixel;
  746. std::unique_ptr<u8[]> temp_gl_depth_buffer(new u8[fb_depth_texture.width * fb_depth_texture.height * gl_bpp]);
  747. state.texture_units[0].texture_2d = fb_depth_texture.texture.handle;
  748. state.Apply();
  749. glActiveTexture(GL_TEXTURE0);
  750. glGetTexImage(GL_TEXTURE_2D, 0, fb_depth_texture.gl_format, fb_depth_texture.gl_type, temp_gl_depth_buffer.get());
  751. state.texture_units[0].texture_2d = 0;
  752. state.Apply();
  753. u8* temp_gl_depth_data = bytes_per_pixel == 3 ? (temp_gl_depth_buffer.get() + 1) : temp_gl_depth_buffer.get();
  754. if (fb_depth_texture.format == Pica::Regs::DepthFormat::D24S8) {
  755. for (int y = 0; y < fb_depth_texture.height; ++y) {
  756. for (int x = 0; x < fb_depth_texture.width; ++x) {
  757. const u32 coarse_y = y & ~7;
  758. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
  759. u32 gl_pixel_index = (x + y * fb_depth_texture.width);
  760. u8* pixel = depth_buffer + dst_offset;
  761. u32 depth_stencil = ((u32*)temp_gl_depth_data)[gl_pixel_index];
  762. *(u32*)pixel = (depth_stencil >> 8) | (depth_stencil << 24);
  763. }
  764. }
  765. } else {
  766. for (int y = 0; y < fb_depth_texture.height; ++y) {
  767. for (int x = 0; x < fb_depth_texture.width; ++x) {
  768. const u32 coarse_y = y & ~7;
  769. u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
  770. u32 gl_pixel_index = (x + y * fb_depth_texture.width) * gl_bpp;
  771. u8* pixel = depth_buffer + dst_offset;
  772. memcpy(pixel, &temp_gl_depth_data[gl_pixel_index], bytes_per_pixel);
  773. }
  774. }
  775. }
  776. }
  777. }
  778. }