gl_rasterizer.cpp 37 KB

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