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