gl_rasterizer.cpp 37 KB

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