gl_state.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. Enable(GL_PROGRAM_POINT_SIZE, cur_state.point.program_control, point.program_control);
  108. Enable(GL_POINT_SPRITE, cur_state.point.sprite, point.sprite);
  109. if (UpdateValue(cur_state.point.size, point.size)) {
  110. glPointSize(point.size);
  111. }
  112. }
  113. void OpenGLState::ApplyFragmentColorClamp() {
  114. if (UpdateValue(cur_state.fragment_color_clamp.enabled, fragment_color_clamp.enabled)) {
  115. glClampColor(GL_CLAMP_FRAGMENT_COLOR_ARB,
  116. fragment_color_clamp.enabled ? GL_TRUE : GL_FALSE);
  117. }
  118. }
  119. void OpenGLState::ApplyMultisample() {
  120. Enable(GL_SAMPLE_ALPHA_TO_COVERAGE, cur_state.multisample_control.alpha_to_coverage,
  121. multisample_control.alpha_to_coverage);
  122. Enable(GL_SAMPLE_ALPHA_TO_ONE, cur_state.multisample_control.alpha_to_one,
  123. multisample_control.alpha_to_one);
  124. }
  125. void OpenGLState::ApplyDepthClamp() {
  126. if (depth_clamp.far_plane == cur_state.depth_clamp.far_plane &&
  127. depth_clamp.near_plane == cur_state.depth_clamp.near_plane) {
  128. return;
  129. }
  130. cur_state.depth_clamp = depth_clamp;
  131. UNIMPLEMENTED_IF_MSG(depth_clamp.far_plane != depth_clamp.near_plane,
  132. "Unimplemented Depth Clamp Separation!");
  133. Enable(GL_DEPTH_CLAMP, depth_clamp.far_plane || depth_clamp.near_plane);
  134. }
  135. void OpenGLState::ApplySRgb() {
  136. if (cur_state.framebuffer_srgb.enabled == framebuffer_srgb.enabled)
  137. return;
  138. cur_state.framebuffer_srgb.enabled = framebuffer_srgb.enabled;
  139. if (framebuffer_srgb.enabled) {
  140. glEnable(GL_FRAMEBUFFER_SRGB);
  141. } else {
  142. glDisable(GL_FRAMEBUFFER_SRGB);
  143. }
  144. }
  145. void OpenGLState::ApplyCulling() {
  146. Enable(GL_CULL_FACE, cur_state.cull.enabled, cull.enabled);
  147. if (UpdateValue(cur_state.cull.mode, cull.mode)) {
  148. glCullFace(cull.mode);
  149. }
  150. if (UpdateValue(cur_state.cull.front_face, cull.front_face)) {
  151. glFrontFace(cull.front_face);
  152. }
  153. }
  154. void OpenGLState::ApplyRasterizerDiscard() {
  155. Enable(GL_RASTERIZER_DISCARD, cur_state.rasterizer_discard, rasterizer_discard);
  156. }
  157. void OpenGLState::ApplyColorMask() {
  158. if (!dirty.color_mask) {
  159. return;
  160. }
  161. dirty.color_mask = false;
  162. for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
  163. const auto& updated = color_mask[i];
  164. auto& current = cur_state.color_mask[i];
  165. if (updated.red_enabled != current.red_enabled ||
  166. updated.green_enabled != current.green_enabled ||
  167. updated.blue_enabled != current.blue_enabled ||
  168. updated.alpha_enabled != current.alpha_enabled) {
  169. current = updated;
  170. glColorMaski(static_cast<GLuint>(i), updated.red_enabled, updated.green_enabled,
  171. updated.blue_enabled, updated.alpha_enabled);
  172. }
  173. }
  174. }
  175. void OpenGLState::ApplyDepth() {
  176. Enable(GL_DEPTH_TEST, cur_state.depth.test_enabled, depth.test_enabled);
  177. if (cur_state.depth.test_func != depth.test_func) {
  178. cur_state.depth.test_func = depth.test_func;
  179. glDepthFunc(depth.test_func);
  180. }
  181. if (cur_state.depth.write_mask != depth.write_mask) {
  182. cur_state.depth.write_mask = depth.write_mask;
  183. glDepthMask(depth.write_mask);
  184. }
  185. }
  186. void OpenGLState::ApplyPrimitiveRestart() {
  187. Enable(GL_PRIMITIVE_RESTART, cur_state.primitive_restart.enabled, primitive_restart.enabled);
  188. if (cur_state.primitive_restart.index != primitive_restart.index) {
  189. cur_state.primitive_restart.index = primitive_restart.index;
  190. glPrimitiveRestartIndex(primitive_restart.index);
  191. }
  192. }
  193. void OpenGLState::ApplyStencilTest() {
  194. if (!dirty.stencil_state) {
  195. return;
  196. }
  197. dirty.stencil_state = false;
  198. Enable(GL_STENCIL_TEST, cur_state.stencil.test_enabled, stencil.test_enabled);
  199. const auto ConfigStencil = [](GLenum face, const auto& config, auto& current) {
  200. if (current.test_func != config.test_func || current.test_ref != config.test_ref ||
  201. current.test_mask != config.test_mask) {
  202. current.test_func = config.test_func;
  203. current.test_ref = config.test_ref;
  204. current.test_mask = config.test_mask;
  205. glStencilFuncSeparate(face, config.test_func, config.test_ref, config.test_mask);
  206. }
  207. if (current.action_depth_fail != config.action_depth_fail ||
  208. current.action_depth_pass != config.action_depth_pass ||
  209. current.action_stencil_fail != config.action_stencil_fail) {
  210. current.action_depth_fail = config.action_depth_fail;
  211. current.action_depth_pass = config.action_depth_pass;
  212. current.action_stencil_fail = config.action_stencil_fail;
  213. glStencilOpSeparate(face, config.action_stencil_fail, config.action_depth_fail,
  214. config.action_depth_pass);
  215. }
  216. if (current.write_mask != config.write_mask) {
  217. current.write_mask = config.write_mask;
  218. glStencilMaskSeparate(face, config.write_mask);
  219. }
  220. };
  221. ConfigStencil(GL_FRONT, stencil.front, cur_state.stencil.front);
  222. ConfigStencil(GL_BACK, stencil.back, cur_state.stencil.back);
  223. }
  224. void OpenGLState::ApplyViewport() {
  225. for (GLuint i = 0; i < static_cast<GLuint>(Maxwell::NumViewports); ++i) {
  226. const auto& updated = viewports[i];
  227. auto& current = cur_state.viewports[i];
  228. if (current.x != updated.x || current.y != updated.y || current.width != updated.width ||
  229. current.height != updated.height) {
  230. current.x = updated.x;
  231. current.y = updated.y;
  232. current.width = updated.width;
  233. current.height = updated.height;
  234. glViewportIndexedf(i, static_cast<GLfloat>(updated.x), static_cast<GLfloat>(updated.y),
  235. static_cast<GLfloat>(updated.width),
  236. static_cast<GLfloat>(updated.height));
  237. }
  238. if (current.depth_range_near != updated.depth_range_near ||
  239. current.depth_range_far != updated.depth_range_far) {
  240. current.depth_range_near = updated.depth_range_near;
  241. current.depth_range_far = updated.depth_range_far;
  242. glDepthRangeIndexed(i, updated.depth_range_near, updated.depth_range_far);
  243. }
  244. Enable(GL_SCISSOR_TEST, i, current.scissor.enabled, updated.scissor.enabled);
  245. if (current.scissor.x != updated.scissor.x || current.scissor.y != updated.scissor.y ||
  246. current.scissor.width != updated.scissor.width ||
  247. current.scissor.height != updated.scissor.height) {
  248. current.scissor.x = updated.scissor.x;
  249. current.scissor.y = updated.scissor.y;
  250. current.scissor.width = updated.scissor.width;
  251. current.scissor.height = updated.scissor.height;
  252. glScissorIndexed(i, updated.scissor.x, updated.scissor.y, updated.scissor.width,
  253. updated.scissor.height);
  254. }
  255. }
  256. }
  257. void OpenGLState::ApplyGlobalBlending() {
  258. const Blend& updated = blend[0];
  259. Blend& current = cur_state.blend[0];
  260. Enable(GL_BLEND, current.enabled, updated.enabled);
  261. if (current.src_rgb_func != updated.src_rgb_func ||
  262. current.dst_rgb_func != updated.dst_rgb_func || current.src_a_func != updated.src_a_func ||
  263. current.dst_a_func != updated.dst_a_func) {
  264. current.src_rgb_func = updated.src_rgb_func;
  265. current.dst_rgb_func = updated.dst_rgb_func;
  266. current.src_a_func = updated.src_a_func;
  267. current.dst_a_func = updated.dst_a_func;
  268. glBlendFuncSeparate(updated.src_rgb_func, updated.dst_rgb_func, updated.src_a_func,
  269. updated.dst_a_func);
  270. }
  271. if (current.rgb_equation != updated.rgb_equation || current.a_equation != updated.a_equation) {
  272. current.rgb_equation = updated.rgb_equation;
  273. current.a_equation = updated.a_equation;
  274. glBlendEquationSeparate(updated.rgb_equation, updated.a_equation);
  275. }
  276. }
  277. void OpenGLState::ApplyTargetBlending(std::size_t target, bool force) {
  278. const Blend& updated = blend[target];
  279. Blend& current = cur_state.blend[target];
  280. if (current.enabled != updated.enabled || force) {
  281. current.enabled = updated.enabled;
  282. Enable(GL_BLEND, static_cast<GLuint>(target), updated.enabled);
  283. }
  284. if (UpdateTie(std::tie(current.src_rgb_func, current.dst_rgb_func, current.src_a_func,
  285. current.dst_a_func),
  286. std::tie(updated.src_rgb_func, updated.dst_rgb_func, updated.src_a_func,
  287. updated.dst_a_func))) {
  288. glBlendFuncSeparatei(static_cast<GLuint>(target), updated.src_rgb_func,
  289. updated.dst_rgb_func, updated.src_a_func, updated.dst_a_func);
  290. }
  291. if (UpdateTie(std::tie(current.rgb_equation, current.a_equation),
  292. std::tie(updated.rgb_equation, updated.a_equation))) {
  293. glBlendEquationSeparatei(static_cast<GLuint>(target), updated.rgb_equation,
  294. updated.a_equation);
  295. }
  296. }
  297. void OpenGLState::ApplyBlending() {
  298. if (!dirty.blend_state) {
  299. return;
  300. }
  301. dirty.blend_state = false;
  302. if (independant_blend.enabled) {
  303. const bool force = independant_blend.enabled != cur_state.independant_blend.enabled;
  304. for (std::size_t target = 0; target < Maxwell::NumRenderTargets; ++target) {
  305. ApplyTargetBlending(target, force);
  306. }
  307. } else {
  308. ApplyGlobalBlending();
  309. }
  310. cur_state.independant_blend.enabled = independant_blend.enabled;
  311. if (UpdateTie(
  312. std::tie(cur_state.blend_color.red, cur_state.blend_color.green,
  313. cur_state.blend_color.blue, cur_state.blend_color.alpha),
  314. std::tie(blend_color.red, blend_color.green, blend_color.blue, blend_color.alpha))) {
  315. glBlendColor(blend_color.red, blend_color.green, blend_color.blue, blend_color.alpha);
  316. }
  317. }
  318. void OpenGLState::ApplyLogicOp() {
  319. Enable(GL_COLOR_LOGIC_OP, cur_state.logic_op.enabled, logic_op.enabled);
  320. if (UpdateValue(cur_state.logic_op.operation, logic_op.operation)) {
  321. glLogicOp(logic_op.operation);
  322. }
  323. }
  324. void OpenGLState::ApplyPolygonOffset() {
  325. if (!dirty.polygon_offset) {
  326. return;
  327. }
  328. dirty.polygon_offset = false;
  329. Enable(GL_POLYGON_OFFSET_FILL, cur_state.polygon_offset.fill_enable,
  330. polygon_offset.fill_enable);
  331. Enable(GL_POLYGON_OFFSET_LINE, cur_state.polygon_offset.line_enable,
  332. polygon_offset.line_enable);
  333. Enable(GL_POLYGON_OFFSET_POINT, cur_state.polygon_offset.point_enable,
  334. polygon_offset.point_enable);
  335. if (UpdateTie(std::tie(cur_state.polygon_offset.factor, cur_state.polygon_offset.units,
  336. cur_state.polygon_offset.clamp),
  337. std::tie(polygon_offset.factor, polygon_offset.units, polygon_offset.clamp))) {
  338. if (GLAD_GL_EXT_polygon_offset_clamp && polygon_offset.clamp != 0) {
  339. glPolygonOffsetClamp(polygon_offset.factor, polygon_offset.units, polygon_offset.clamp);
  340. } else {
  341. UNIMPLEMENTED_IF_MSG(polygon_offset.clamp != 0,
  342. "Unimplemented Depth polygon offset clamp.");
  343. glPolygonOffset(polygon_offset.factor, polygon_offset.units);
  344. }
  345. }
  346. }
  347. void OpenGLState::ApplyAlphaTest() {
  348. Enable(GL_ALPHA_TEST, cur_state.alpha_test.enabled, alpha_test.enabled);
  349. if (UpdateTie(std::tie(cur_state.alpha_test.func, cur_state.alpha_test.ref),
  350. std::tie(alpha_test.func, alpha_test.ref))) {
  351. glAlphaFunc(alpha_test.func, alpha_test.ref);
  352. }
  353. }
  354. void OpenGLState::ApplyClipControl() {
  355. if (UpdateTie(std::tie(cur_state.clip_control.origin, cur_state.clip_control.depth_mode),
  356. std::tie(clip_control.origin, clip_control.depth_mode))) {
  357. glClipControl(clip_control.origin, clip_control.depth_mode);
  358. }
  359. }
  360. void OpenGLState::ApplyTextures() {
  361. const std::size_t size = std::size(textures);
  362. for (std::size_t i = 0; i < size; ++i) {
  363. if (UpdateValue(cur_state.textures[i], textures[i])) {
  364. // BindTextureUnit doesn't support binding null textures, skip those binds.
  365. // TODO(Rodrigo): Stop using null textures
  366. if (textures[i] != 0) {
  367. glBindTextureUnit(static_cast<GLuint>(i), textures[i]);
  368. }
  369. }
  370. }
  371. }
  372. void OpenGLState::ApplySamplers() {
  373. const std::size_t size = std::size(samplers);
  374. for (std::size_t i = 0; i < size; ++i) {
  375. if (UpdateValue(cur_state.samplers[i], samplers[i])) {
  376. glBindSampler(static_cast<GLuint>(i), samplers[i]);
  377. }
  378. }
  379. }
  380. void OpenGLState::ApplyImages() {
  381. if (const auto update = UpdateArray(cur_state.images, images)) {
  382. glBindImageTextures(update->first, update->second, images.data() + update->first);
  383. }
  384. }
  385. void OpenGLState::Apply() {
  386. MICROPROFILE_SCOPE(OpenGL_State);
  387. ApplyFramebufferState();
  388. ApplyVertexArrayState();
  389. ApplyShaderProgram();
  390. ApplyProgramPipeline();
  391. ApplyClipDistances();
  392. ApplyPointSize();
  393. ApplyFragmentColorClamp();
  394. ApplyMultisample();
  395. ApplyRasterizerDiscard();
  396. ApplyColorMask();
  397. ApplyDepthClamp();
  398. ApplyViewport();
  399. ApplyStencilTest();
  400. ApplySRgb();
  401. ApplyCulling();
  402. ApplyDepth();
  403. ApplyPrimitiveRestart();
  404. ApplyBlending();
  405. ApplyLogicOp();
  406. ApplyTextures();
  407. ApplySamplers();
  408. ApplyImages();
  409. ApplyPolygonOffset();
  410. ApplyAlphaTest();
  411. ApplyClipControl();
  412. }
  413. void OpenGLState::EmulateViewportWithScissor() {
  414. auto& current = viewports[0];
  415. if (current.scissor.enabled) {
  416. const GLint left = std::max(current.x, current.scissor.x);
  417. const GLint right =
  418. std::max(current.x + current.width, current.scissor.x + current.scissor.width);
  419. const GLint bottom = std::max(current.y, current.scissor.y);
  420. const GLint top =
  421. std::max(current.y + current.height, current.scissor.y + current.scissor.height);
  422. current.scissor.x = std::max(left, 0);
  423. current.scissor.y = std::max(bottom, 0);
  424. current.scissor.width = std::max(right - left, 0);
  425. current.scissor.height = std::max(top - bottom, 0);
  426. } else {
  427. current.scissor.enabled = true;
  428. current.scissor.x = current.x;
  429. current.scissor.y = current.y;
  430. current.scissor.width = current.width;
  431. current.scissor.height = current.height;
  432. }
  433. }
  434. OpenGLState& OpenGLState::UnbindTexture(GLuint handle) {
  435. for (auto& texture : textures) {
  436. if (texture == handle) {
  437. texture = 0;
  438. }
  439. }
  440. return *this;
  441. }
  442. OpenGLState& OpenGLState::ResetSampler(GLuint handle) {
  443. for (auto& sampler : samplers) {
  444. if (sampler == handle) {
  445. sampler = 0;
  446. }
  447. }
  448. return *this;
  449. }
  450. OpenGLState& OpenGLState::ResetProgram(GLuint handle) {
  451. if (draw.shader_program == handle) {
  452. draw.shader_program = 0;
  453. }
  454. return *this;
  455. }
  456. OpenGLState& OpenGLState::ResetPipeline(GLuint handle) {
  457. if (draw.program_pipeline == handle) {
  458. draw.program_pipeline = 0;
  459. }
  460. return *this;
  461. }
  462. OpenGLState& OpenGLState::ResetVertexArray(GLuint handle) {
  463. if (draw.vertex_array == handle) {
  464. draw.vertex_array = 0;
  465. }
  466. return *this;
  467. }
  468. OpenGLState& OpenGLState::ResetFramebuffer(GLuint handle) {
  469. if (draw.read_framebuffer == handle) {
  470. draw.read_framebuffer = 0;
  471. }
  472. if (draw.draw_framebuffer == handle) {
  473. draw.draw_framebuffer = 0;
  474. }
  475. return *this;
  476. }
  477. } // namespace OpenGL