gl_rasterizer.cpp 38 KB

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