gl_rasterizer.cpp 36 KB

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