gl_state.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <glad/glad.h>
  7. #include "common/assert.h"
  8. #include "common/logging/log.h"
  9. #include "common/microprofile.h"
  10. #include "video_core/renderer_opengl/gl_state.h"
  11. MICROPROFILE_DEFINE(OpenGL_State, "OpenGL", "State Change", MP_RGB(192, 128, 128));
  12. namespace OpenGL {
  13. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  14. OpenGLState OpenGLState::cur_state;
  15. namespace {
  16. template <typename T>
  17. bool UpdateValue(T& current_value, const T new_value) {
  18. const bool changed = current_value != new_value;
  19. current_value = new_value;
  20. return changed;
  21. }
  22. template <typename T1, typename T2>
  23. bool UpdateTie(T1 current_value, const T2 new_value) {
  24. const bool changed = current_value != new_value;
  25. current_value = new_value;
  26. return changed;
  27. }
  28. template <typename T>
  29. std::optional<std::pair<GLuint, GLsizei>> UpdateArray(T& current_values, const T& new_values) {
  30. std::optional<std::size_t> first;
  31. std::size_t last;
  32. for (std::size_t i = 0; i < std::size(current_values); ++i) {
  33. if (!UpdateValue(current_values[i], new_values[i])) {
  34. continue;
  35. }
  36. if (!first) {
  37. first = i;
  38. }
  39. last = i;
  40. }
  41. if (!first) {
  42. return std::nullopt;
  43. }
  44. return std::make_pair(static_cast<GLuint>(*first), static_cast<GLsizei>(last - *first + 1));
  45. }
  46. void Enable(GLenum cap, bool enable) {
  47. if (enable) {
  48. glEnable(cap);
  49. } else {
  50. glDisable(cap);
  51. }
  52. }
  53. void Enable(GLenum cap, GLuint index, bool enable) {
  54. if (enable) {
  55. glEnablei(cap, index);
  56. } else {
  57. glDisablei(cap, index);
  58. }
  59. }
  60. void Enable(GLenum cap, bool& current_value, bool new_value) {
  61. if (UpdateValue(current_value, new_value)) {
  62. Enable(cap, new_value);
  63. }
  64. }
  65. void Enable(GLenum cap, GLuint index, bool& current_value, bool new_value) {
  66. if (UpdateValue(current_value, new_value)) {
  67. Enable(cap, index, new_value);
  68. }
  69. }
  70. } // Anonymous namespace
  71. OpenGLState::OpenGLState() = default;
  72. void OpenGLState::SetDefaultViewports() {
  73. viewports.fill(Viewport{});
  74. depth_clamp.far_plane = false;
  75. depth_clamp.near_plane = false;
  76. }
  77. void OpenGLState::ApplyFramebufferState() {
  78. if (UpdateValue(cur_state.draw.read_framebuffer, draw.read_framebuffer)) {
  79. glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
  80. }
  81. if (UpdateValue(cur_state.draw.draw_framebuffer, draw.draw_framebuffer)) {
  82. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, draw.draw_framebuffer);
  83. }
  84. }
  85. void OpenGLState::ApplyVertexArrayState() {
  86. if (UpdateValue(cur_state.draw.vertex_array, draw.vertex_array)) {
  87. glBindVertexArray(draw.vertex_array);
  88. }
  89. }
  90. void OpenGLState::ApplyShaderProgram() {
  91. if (UpdateValue(cur_state.draw.shader_program, draw.shader_program)) {
  92. glUseProgram(draw.shader_program);
  93. }
  94. }
  95. void OpenGLState::ApplyProgramPipeline() {
  96. if (UpdateValue(cur_state.draw.program_pipeline, draw.program_pipeline)) {
  97. glBindProgramPipeline(draw.program_pipeline);
  98. }
  99. }
  100. void OpenGLState::ApplyClipDistances() {
  101. for (std::size_t i = 0; i < clip_distance.size(); ++i) {
  102. Enable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i), cur_state.clip_distance[i],
  103. clip_distance[i]);
  104. }
  105. }
  106. void OpenGLState::ApplyPointSize() {
  107. if (UpdateValue(cur_state.point.size, point.size)) {
  108. glPointSize(point.size);
  109. }
  110. }
  111. void OpenGLState::ApplyFragmentColorClamp() {
  112. if (UpdateValue(cur_state.fragment_color_clamp.enabled, fragment_color_clamp.enabled)) {
  113. glClampColor(GL_CLAMP_FRAGMENT_COLOR_ARB,
  114. fragment_color_clamp.enabled ? GL_TRUE : GL_FALSE);
  115. }
  116. }
  117. void OpenGLState::ApplyMultisample() {
  118. Enable(GL_SAMPLE_ALPHA_TO_COVERAGE, cur_state.multisample_control.alpha_to_coverage,
  119. multisample_control.alpha_to_coverage);
  120. Enable(GL_SAMPLE_ALPHA_TO_ONE, cur_state.multisample_control.alpha_to_one,
  121. multisample_control.alpha_to_one);
  122. }
  123. void OpenGLState::ApplyDepthClamp() {
  124. if (depth_clamp.far_plane == cur_state.depth_clamp.far_plane &&
  125. depth_clamp.near_plane == cur_state.depth_clamp.near_plane) {
  126. return;
  127. }
  128. cur_state.depth_clamp = depth_clamp;
  129. UNIMPLEMENTED_IF_MSG(depth_clamp.far_plane != depth_clamp.near_plane,
  130. "Unimplemented Depth Clamp Separation!");
  131. Enable(GL_DEPTH_CLAMP, depth_clamp.far_plane || depth_clamp.near_plane);
  132. }
  133. void OpenGLState::ApplySRgb() {
  134. if (cur_state.framebuffer_srgb.enabled == framebuffer_srgb.enabled)
  135. return;
  136. cur_state.framebuffer_srgb.enabled = framebuffer_srgb.enabled;
  137. if (framebuffer_srgb.enabled) {
  138. glEnable(GL_FRAMEBUFFER_SRGB);
  139. } else {
  140. glDisable(GL_FRAMEBUFFER_SRGB);
  141. }
  142. }
  143. void OpenGLState::ApplyCulling() {
  144. Enable(GL_CULL_FACE, cur_state.cull.enabled, cull.enabled);
  145. if (UpdateValue(cur_state.cull.mode, cull.mode)) {
  146. glCullFace(cull.mode);
  147. }
  148. if (UpdateValue(cur_state.cull.front_face, cull.front_face)) {
  149. glFrontFace(cull.front_face);
  150. }
  151. }
  152. void OpenGLState::ApplyRasterizerDiscard() {
  153. Enable(GL_RASTERIZER_DISCARD, cur_state.rasterizer_discard, rasterizer_discard);
  154. }
  155. void OpenGLState::ApplyColorMask() {
  156. if (!dirty.color_mask) {
  157. return;
  158. }
  159. dirty.color_mask = false;
  160. for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
  161. const auto& updated = color_mask[i];
  162. auto& current = cur_state.color_mask[i];
  163. if (updated.red_enabled != current.red_enabled ||
  164. updated.green_enabled != current.green_enabled ||
  165. updated.blue_enabled != current.blue_enabled ||
  166. updated.alpha_enabled != current.alpha_enabled) {
  167. current = updated;
  168. glColorMaski(static_cast<GLuint>(i), updated.red_enabled, updated.green_enabled,
  169. updated.blue_enabled, updated.alpha_enabled);
  170. }
  171. }
  172. }
  173. void OpenGLState::ApplyDepth() {
  174. Enable(GL_DEPTH_TEST, cur_state.depth.test_enabled, depth.test_enabled);
  175. if (cur_state.depth.test_func != depth.test_func) {
  176. cur_state.depth.test_func = depth.test_func;
  177. glDepthFunc(depth.test_func);
  178. }
  179. if (cur_state.depth.write_mask != depth.write_mask) {
  180. cur_state.depth.write_mask = depth.write_mask;
  181. glDepthMask(depth.write_mask);
  182. }
  183. }
  184. void OpenGLState::ApplyPrimitiveRestart() {
  185. Enable(GL_PRIMITIVE_RESTART, cur_state.primitive_restart.enabled, primitive_restart.enabled);
  186. if (cur_state.primitive_restart.index != primitive_restart.index) {
  187. cur_state.primitive_restart.index = primitive_restart.index;
  188. glPrimitiveRestartIndex(primitive_restart.index);
  189. }
  190. }
  191. void OpenGLState::ApplyStencilTest() {
  192. if (!dirty.stencil_state) {
  193. return;
  194. }
  195. dirty.stencil_state = false;
  196. Enable(GL_STENCIL_TEST, cur_state.stencil.test_enabled, stencil.test_enabled);
  197. const auto ConfigStencil = [](GLenum face, const auto& config, auto& current) {
  198. if (current.test_func != config.test_func || current.test_ref != config.test_ref ||
  199. current.test_mask != config.test_mask) {
  200. current.test_func = config.test_func;
  201. current.test_ref = config.test_ref;
  202. current.test_mask = config.test_mask;
  203. glStencilFuncSeparate(face, config.test_func, config.test_ref, config.test_mask);
  204. }
  205. if (current.action_depth_fail != config.action_depth_fail ||
  206. current.action_depth_pass != config.action_depth_pass ||
  207. current.action_stencil_fail != config.action_stencil_fail) {
  208. current.action_depth_fail = config.action_depth_fail;
  209. current.action_depth_pass = config.action_depth_pass;
  210. current.action_stencil_fail = config.action_stencil_fail;
  211. glStencilOpSeparate(face, config.action_stencil_fail, config.action_depth_fail,
  212. config.action_depth_pass);
  213. }
  214. if (current.write_mask != config.write_mask) {
  215. current.write_mask = config.write_mask;
  216. glStencilMaskSeparate(face, config.write_mask);
  217. }
  218. };
  219. ConfigStencil(GL_FRONT, stencil.front, cur_state.stencil.front);
  220. ConfigStencil(GL_BACK, stencil.back, cur_state.stencil.back);
  221. }
  222. void OpenGLState::ApplyViewport() {
  223. for (GLuint i = 0; i < static_cast<GLuint>(Maxwell::NumViewports); ++i) {
  224. const auto& updated = viewports[i];
  225. auto& current = cur_state.viewports[i];
  226. if (current.x != updated.x || current.y != updated.y || current.width != updated.width ||
  227. current.height != updated.height) {
  228. current.x = updated.x;
  229. current.y = updated.y;
  230. current.width = updated.width;
  231. current.height = updated.height;
  232. glViewportIndexedf(i, static_cast<GLfloat>(updated.x), static_cast<GLfloat>(updated.y),
  233. static_cast<GLfloat>(updated.width),
  234. static_cast<GLfloat>(updated.height));
  235. }
  236. if (current.depth_range_near != updated.depth_range_near ||
  237. current.depth_range_far != updated.depth_range_far) {
  238. current.depth_range_near = updated.depth_range_near;
  239. current.depth_range_far = updated.depth_range_far;
  240. glDepthRangeIndexed(i, updated.depth_range_near, updated.depth_range_far);
  241. }
  242. Enable(GL_SCISSOR_TEST, i, current.scissor.enabled, updated.scissor.enabled);
  243. if (current.scissor.x != updated.scissor.x || current.scissor.y != updated.scissor.y ||
  244. current.scissor.width != updated.scissor.width ||
  245. current.scissor.height != updated.scissor.height) {
  246. current.scissor.x = updated.scissor.x;
  247. current.scissor.y = updated.scissor.y;
  248. current.scissor.width = updated.scissor.width;
  249. current.scissor.height = updated.scissor.height;
  250. glScissorIndexed(i, updated.scissor.x, updated.scissor.y, updated.scissor.width,
  251. updated.scissor.height);
  252. }
  253. }
  254. }
  255. void OpenGLState::ApplyGlobalBlending() {
  256. const Blend& updated = blend[0];
  257. Blend& current = cur_state.blend[0];
  258. Enable(GL_BLEND, current.enabled, updated.enabled);
  259. if (current.src_rgb_func != updated.src_rgb_func ||
  260. current.dst_rgb_func != updated.dst_rgb_func || current.src_a_func != updated.src_a_func ||
  261. current.dst_a_func != updated.dst_a_func) {
  262. current.src_rgb_func = updated.src_rgb_func;
  263. current.dst_rgb_func = updated.dst_rgb_func;
  264. current.src_a_func = updated.src_a_func;
  265. current.dst_a_func = updated.dst_a_func;
  266. glBlendFuncSeparate(updated.src_rgb_func, updated.dst_rgb_func, updated.src_a_func,
  267. updated.dst_a_func);
  268. }
  269. if (current.rgb_equation != updated.rgb_equation || current.a_equation != updated.a_equation) {
  270. current.rgb_equation = updated.rgb_equation;
  271. current.a_equation = updated.a_equation;
  272. glBlendEquationSeparate(updated.rgb_equation, updated.a_equation);
  273. }
  274. }
  275. void OpenGLState::ApplyTargetBlending(std::size_t target, bool force) {
  276. const Blend& updated = blend[target];
  277. Blend& current = cur_state.blend[target];
  278. if (current.enabled != updated.enabled || force) {
  279. current.enabled = updated.enabled;
  280. Enable(GL_BLEND, static_cast<GLuint>(target), updated.enabled);
  281. }
  282. if (UpdateTie(std::tie(current.src_rgb_func, current.dst_rgb_func, current.src_a_func,
  283. current.dst_a_func),
  284. std::tie(updated.src_rgb_func, updated.dst_rgb_func, updated.src_a_func,
  285. updated.dst_a_func))) {
  286. glBlendFuncSeparatei(static_cast<GLuint>(target), updated.src_rgb_func,
  287. updated.dst_rgb_func, updated.src_a_func, updated.dst_a_func);
  288. }
  289. if (UpdateTie(std::tie(current.rgb_equation, current.a_equation),
  290. std::tie(updated.rgb_equation, updated.a_equation))) {
  291. glBlendEquationSeparatei(static_cast<GLuint>(target), updated.rgb_equation,
  292. updated.a_equation);
  293. }
  294. }
  295. void OpenGLState::ApplyBlending() {
  296. if (!dirty.blend_state) {
  297. return;
  298. }
  299. dirty.blend_state = false;
  300. if (independant_blend.enabled) {
  301. const bool force = independant_blend.enabled != cur_state.independant_blend.enabled;
  302. for (std::size_t target = 0; target < Maxwell::NumRenderTargets; ++target) {
  303. ApplyTargetBlending(target, force);
  304. }
  305. } else {
  306. ApplyGlobalBlending();
  307. }
  308. cur_state.independant_blend.enabled = independant_blend.enabled;
  309. if (UpdateTie(
  310. std::tie(cur_state.blend_color.red, cur_state.blend_color.green,
  311. cur_state.blend_color.blue, cur_state.blend_color.alpha),
  312. std::tie(blend_color.red, blend_color.green, blend_color.blue, blend_color.alpha))) {
  313. glBlendColor(blend_color.red, blend_color.green, blend_color.blue, blend_color.alpha);
  314. }
  315. }
  316. void OpenGLState::ApplyLogicOp() {
  317. Enable(GL_COLOR_LOGIC_OP, cur_state.logic_op.enabled, logic_op.enabled);
  318. if (UpdateValue(cur_state.logic_op.operation, logic_op.operation)) {
  319. glLogicOp(logic_op.operation);
  320. }
  321. }
  322. void OpenGLState::ApplyPolygonOffset() {
  323. if (!dirty.polygon_offset) {
  324. return;
  325. }
  326. dirty.polygon_offset = false;
  327. Enable(GL_POLYGON_OFFSET_FILL, cur_state.polygon_offset.fill_enable,
  328. polygon_offset.fill_enable);
  329. Enable(GL_POLYGON_OFFSET_LINE, cur_state.polygon_offset.line_enable,
  330. polygon_offset.line_enable);
  331. Enable(GL_POLYGON_OFFSET_POINT, cur_state.polygon_offset.point_enable,
  332. polygon_offset.point_enable);
  333. if (UpdateTie(std::tie(cur_state.polygon_offset.factor, cur_state.polygon_offset.units,
  334. cur_state.polygon_offset.clamp),
  335. std::tie(polygon_offset.factor, polygon_offset.units, polygon_offset.clamp))) {
  336. if (GLAD_GL_EXT_polygon_offset_clamp && polygon_offset.clamp != 0) {
  337. glPolygonOffsetClamp(polygon_offset.factor, polygon_offset.units, polygon_offset.clamp);
  338. } else {
  339. UNIMPLEMENTED_IF_MSG(polygon_offset.clamp != 0,
  340. "Unimplemented Depth polygon offset clamp.");
  341. glPolygonOffset(polygon_offset.factor, polygon_offset.units);
  342. }
  343. }
  344. }
  345. void OpenGLState::ApplyAlphaTest() {
  346. Enable(GL_ALPHA_TEST, cur_state.alpha_test.enabled, alpha_test.enabled);
  347. if (UpdateTie(std::tie(cur_state.alpha_test.func, cur_state.alpha_test.ref),
  348. std::tie(alpha_test.func, alpha_test.ref))) {
  349. glAlphaFunc(alpha_test.func, alpha_test.ref);
  350. }
  351. }
  352. void OpenGLState::ApplyClipControl() {
  353. if (UpdateTie(std::tie(cur_state.clip_control.origin, cur_state.clip_control.depth_mode),
  354. std::tie(clip_control.origin, clip_control.depth_mode))) {
  355. glClipControl(clip_control.origin, clip_control.depth_mode);
  356. }
  357. }
  358. void OpenGLState::ApplyTextures() {
  359. const std::size_t size = std::size(textures);
  360. for (std::size_t i = 0; i < size; ++i) {
  361. if (UpdateValue(cur_state.textures[i], textures[i])) {
  362. // BindTextureUnit doesn't support binding null textures, skip those binds.
  363. // TODO(Rodrigo): Stop using null textures
  364. if (textures[i] != 0) {
  365. glBindTextureUnit(static_cast<GLuint>(i), textures[i]);
  366. }
  367. }
  368. }
  369. }
  370. void OpenGLState::ApplySamplers() {
  371. const std::size_t size = std::size(samplers);
  372. for (std::size_t i = 0; i < size; ++i) {
  373. if (UpdateValue(cur_state.samplers[i], samplers[i])) {
  374. glBindSampler(static_cast<GLuint>(i), samplers[i]);
  375. }
  376. }
  377. }
  378. void OpenGLState::ApplyImages() {
  379. if (const auto update = UpdateArray(cur_state.images, images)) {
  380. glBindImageTextures(update->first, update->second, images.data() + update->first);
  381. }
  382. }
  383. void OpenGLState::Apply() {
  384. MICROPROFILE_SCOPE(OpenGL_State);
  385. ApplyFramebufferState();
  386. ApplyVertexArrayState();
  387. ApplyShaderProgram();
  388. ApplyProgramPipeline();
  389. ApplyClipDistances();
  390. ApplyPointSize();
  391. ApplyFragmentColorClamp();
  392. ApplyMultisample();
  393. ApplyRasterizerDiscard();
  394. ApplyColorMask();
  395. ApplyDepthClamp();
  396. ApplyViewport();
  397. ApplyStencilTest();
  398. ApplySRgb();
  399. ApplyCulling();
  400. ApplyDepth();
  401. ApplyPrimitiveRestart();
  402. ApplyBlending();
  403. ApplyLogicOp();
  404. ApplyTextures();
  405. ApplySamplers();
  406. ApplyImages();
  407. ApplyPolygonOffset();
  408. ApplyAlphaTest();
  409. ApplyClipControl();
  410. }
  411. void OpenGLState::EmulateViewportWithScissor() {
  412. auto& current = viewports[0];
  413. if (current.scissor.enabled) {
  414. const GLint left = std::max(current.x, current.scissor.x);
  415. const GLint right =
  416. std::max(current.x + current.width, current.scissor.x + current.scissor.width);
  417. const GLint bottom = std::max(current.y, current.scissor.y);
  418. const GLint top =
  419. std::max(current.y + current.height, current.scissor.y + current.scissor.height);
  420. current.scissor.x = std::max(left, 0);
  421. current.scissor.y = std::max(bottom, 0);
  422. current.scissor.width = std::max(right - left, 0);
  423. current.scissor.height = std::max(top - bottom, 0);
  424. } else {
  425. current.scissor.enabled = true;
  426. current.scissor.x = current.x;
  427. current.scissor.y = current.y;
  428. current.scissor.width = current.width;
  429. current.scissor.height = current.height;
  430. }
  431. }
  432. OpenGLState& OpenGLState::UnbindTexture(GLuint handle) {
  433. for (auto& texture : textures) {
  434. if (texture == handle) {
  435. texture = 0;
  436. }
  437. }
  438. return *this;
  439. }
  440. OpenGLState& OpenGLState::ResetSampler(GLuint handle) {
  441. for (auto& sampler : samplers) {
  442. if (sampler == handle) {
  443. sampler = 0;
  444. }
  445. }
  446. return *this;
  447. }
  448. OpenGLState& OpenGLState::ResetProgram(GLuint handle) {
  449. if (draw.shader_program == handle) {
  450. draw.shader_program = 0;
  451. }
  452. return *this;
  453. }
  454. OpenGLState& OpenGLState::ResetPipeline(GLuint handle) {
  455. if (draw.program_pipeline == handle) {
  456. draw.program_pipeline = 0;
  457. }
  458. return *this;
  459. }
  460. OpenGLState& OpenGLState::ResetVertexArray(GLuint handle) {
  461. if (draw.vertex_array == handle) {
  462. draw.vertex_array = 0;
  463. }
  464. return *this;
  465. }
  466. OpenGLState& OpenGLState::ResetFramebuffer(GLuint handle) {
  467. if (draw.read_framebuffer == handle) {
  468. draw.read_framebuffer = 0;
  469. }
  470. if (draw.draw_framebuffer == handle) {
  471. draw.draw_framebuffer = 0;
  472. }
  473. return *this;
  474. }
  475. } // namespace OpenGL