gl_state.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <iterator>
  5. #include <glad/glad.h>
  6. #include "common/assert.h"
  7. #include "common/logging/log.h"
  8. #include "common/microprofile.h"
  9. #include "video_core/renderer_opengl/gl_state.h"
  10. MICROPROFILE_DEFINE(OpenGL_State, "OpenGL", "State Change", MP_RGB(192, 128, 128));
  11. namespace OpenGL {
  12. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  13. OpenGLState OpenGLState::cur_state;
  14. bool OpenGLState::s_rgb_used;
  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. void Enable(GLenum cap, GLuint index, bool& current_value, bool new_value) {
  65. if (UpdateValue(current_value, new_value))
  66. Enable(cap, index, new_value);
  67. }
  68. } // namespace
  69. OpenGLState::OpenGLState() {
  70. // These all match default OpenGL values
  71. framebuffer_srgb.enabled = false;
  72. multisample_control.alpha_to_coverage = false;
  73. multisample_control.alpha_to_one = false;
  74. cull.enabled = false;
  75. cull.mode = GL_BACK;
  76. cull.front_face = GL_CCW;
  77. depth.test_enabled = false;
  78. depth.test_func = GL_LESS;
  79. depth.write_mask = GL_TRUE;
  80. primitive_restart.enabled = false;
  81. primitive_restart.index = 0;
  82. for (auto& item : color_mask) {
  83. item.red_enabled = GL_TRUE;
  84. item.green_enabled = GL_TRUE;
  85. item.blue_enabled = GL_TRUE;
  86. item.alpha_enabled = GL_TRUE;
  87. }
  88. const auto ResetStencil = [](auto& config) {
  89. config.test_func = GL_ALWAYS;
  90. config.test_ref = 0;
  91. config.test_mask = 0xFFFFFFFF;
  92. config.write_mask = 0xFFFFFFFF;
  93. config.action_depth_fail = GL_KEEP;
  94. config.action_depth_pass = GL_KEEP;
  95. config.action_stencil_fail = GL_KEEP;
  96. };
  97. stencil.test_enabled = false;
  98. ResetStencil(stencil.front);
  99. ResetStencil(stencil.back);
  100. for (auto& item : viewports) {
  101. item.x = 0;
  102. item.y = 0;
  103. item.width = 0;
  104. item.height = 0;
  105. item.depth_range_near = 0.0f;
  106. item.depth_range_far = 1.0f;
  107. item.scissor.enabled = false;
  108. item.scissor.x = 0;
  109. item.scissor.y = 0;
  110. item.scissor.width = 0;
  111. item.scissor.height = 0;
  112. }
  113. for (auto& item : blend) {
  114. item.enabled = true;
  115. item.rgb_equation = GL_FUNC_ADD;
  116. item.a_equation = GL_FUNC_ADD;
  117. item.src_rgb_func = GL_ONE;
  118. item.dst_rgb_func = GL_ZERO;
  119. item.src_a_func = GL_ONE;
  120. item.dst_a_func = GL_ZERO;
  121. }
  122. independant_blend.enabled = false;
  123. blend_color.red = 0.0f;
  124. blend_color.green = 0.0f;
  125. blend_color.blue = 0.0f;
  126. blend_color.alpha = 0.0f;
  127. logic_op.enabled = false;
  128. logic_op.operation = GL_COPY;
  129. draw.read_framebuffer = 0;
  130. draw.draw_framebuffer = 0;
  131. draw.vertex_array = 0;
  132. draw.shader_program = 0;
  133. draw.program_pipeline = 0;
  134. clip_distance = {};
  135. point.size = 1;
  136. fragment_color_clamp.enabled = false;
  137. depth_clamp.far_plane = false;
  138. depth_clamp.near_plane = false;
  139. polygon_offset.fill_enable = false;
  140. polygon_offset.line_enable = false;
  141. polygon_offset.point_enable = false;
  142. polygon_offset.factor = 0.0f;
  143. polygon_offset.units = 0.0f;
  144. polygon_offset.clamp = 0.0f;
  145. alpha_test.enabled = false;
  146. alpha_test.func = GL_ALWAYS;
  147. alpha_test.ref = 0.0f;
  148. }
  149. void OpenGLState::SetDefaultViewports() {
  150. for (auto& item : viewports) {
  151. item.x = 0;
  152. item.y = 0;
  153. item.width = 0;
  154. item.height = 0;
  155. item.depth_range_near = 0.0f;
  156. item.depth_range_far = 1.0f;
  157. item.scissor.enabled = false;
  158. item.scissor.x = 0;
  159. item.scissor.y = 0;
  160. item.scissor.width = 0;
  161. item.scissor.height = 0;
  162. }
  163. depth_clamp.far_plane = false;
  164. depth_clamp.near_plane = false;
  165. }
  166. void OpenGLState::ApplyDefaultState() {
  167. glEnable(GL_BLEND);
  168. glDisable(GL_FRAMEBUFFER_SRGB);
  169. glDisable(GL_CULL_FACE);
  170. glDisable(GL_DEPTH_TEST);
  171. glDisable(GL_PRIMITIVE_RESTART);
  172. glDisable(GL_STENCIL_TEST);
  173. glDisable(GL_COLOR_LOGIC_OP);
  174. glDisable(GL_SCISSOR_TEST);
  175. }
  176. void OpenGLState::ApplyFramebufferState() const {
  177. if (UpdateValue(cur_state.draw.read_framebuffer, draw.read_framebuffer)) {
  178. glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
  179. }
  180. if (UpdateValue(cur_state.draw.draw_framebuffer, draw.draw_framebuffer)) {
  181. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, draw.draw_framebuffer);
  182. }
  183. }
  184. void OpenGLState::ApplyVertexArrayState() const {
  185. if (UpdateValue(cur_state.draw.vertex_array, draw.vertex_array)) {
  186. glBindVertexArray(draw.vertex_array);
  187. }
  188. }
  189. void OpenGLState::ApplyShaderProgram() const {
  190. if (UpdateValue(cur_state.draw.shader_program, draw.shader_program)) {
  191. glUseProgram(draw.shader_program);
  192. }
  193. }
  194. void OpenGLState::ApplyProgramPipeline() const {
  195. if (UpdateValue(cur_state.draw.program_pipeline, draw.program_pipeline)) {
  196. glBindProgramPipeline(draw.program_pipeline);
  197. }
  198. }
  199. void OpenGLState::ApplyClipDistances() const {
  200. for (std::size_t i = 0; i < clip_distance.size(); ++i) {
  201. Enable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i), cur_state.clip_distance[i],
  202. clip_distance[i]);
  203. }
  204. }
  205. void OpenGLState::ApplyPointSize() const {
  206. if (UpdateValue(cur_state.point.size, point.size)) {
  207. glPointSize(point.size);
  208. }
  209. }
  210. void OpenGLState::ApplyFragmentColorClamp() const {
  211. if (UpdateValue(cur_state.fragment_color_clamp.enabled, fragment_color_clamp.enabled)) {
  212. glClampColor(GL_CLAMP_FRAGMENT_COLOR_ARB,
  213. fragment_color_clamp.enabled ? GL_TRUE : GL_FALSE);
  214. }
  215. }
  216. void OpenGLState::ApplyMultisample() const {
  217. Enable(GL_SAMPLE_ALPHA_TO_COVERAGE, cur_state.multisample_control.alpha_to_coverage,
  218. multisample_control.alpha_to_coverage);
  219. Enable(GL_SAMPLE_ALPHA_TO_ONE, cur_state.multisample_control.alpha_to_one,
  220. multisample_control.alpha_to_one);
  221. }
  222. void OpenGLState::ApplyDepthClamp() const {
  223. if (depth_clamp.far_plane == cur_state.depth_clamp.far_plane &&
  224. depth_clamp.near_plane == cur_state.depth_clamp.near_plane) {
  225. return;
  226. }
  227. cur_state.depth_clamp = depth_clamp;
  228. UNIMPLEMENTED_IF_MSG(depth_clamp.far_plane != depth_clamp.near_plane,
  229. "Unimplemented Depth Clamp Separation!");
  230. Enable(GL_DEPTH_CLAMP, depth_clamp.far_plane || depth_clamp.near_plane);
  231. }
  232. void OpenGLState::ApplySRgb() const {
  233. if (cur_state.framebuffer_srgb.enabled == framebuffer_srgb.enabled)
  234. return;
  235. cur_state.framebuffer_srgb.enabled = framebuffer_srgb.enabled;
  236. if (framebuffer_srgb.enabled) {
  237. // Track if sRGB is used
  238. s_rgb_used = true;
  239. glEnable(GL_FRAMEBUFFER_SRGB);
  240. } else {
  241. glDisable(GL_FRAMEBUFFER_SRGB);
  242. }
  243. }
  244. void OpenGLState::ApplyCulling() const {
  245. Enable(GL_CULL_FACE, cur_state.cull.enabled, cull.enabled);
  246. if (UpdateValue(cur_state.cull.mode, cull.mode)) {
  247. glCullFace(cull.mode);
  248. }
  249. if (UpdateValue(cur_state.cull.front_face, cull.front_face)) {
  250. glFrontFace(cull.front_face);
  251. }
  252. }
  253. void OpenGLState::ApplyColorMask() const {
  254. for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
  255. const auto& updated = color_mask[i];
  256. auto& current = cur_state.color_mask[i];
  257. if (updated.red_enabled != current.red_enabled ||
  258. updated.green_enabled != current.green_enabled ||
  259. updated.blue_enabled != current.blue_enabled ||
  260. updated.alpha_enabled != current.alpha_enabled) {
  261. current = updated;
  262. glColorMaski(static_cast<GLuint>(i), updated.red_enabled, updated.green_enabled,
  263. updated.blue_enabled, updated.alpha_enabled);
  264. }
  265. }
  266. }
  267. void OpenGLState::ApplyDepth() const {
  268. Enable(GL_DEPTH_TEST, cur_state.depth.test_enabled, depth.test_enabled);
  269. if (cur_state.depth.test_func != depth.test_func) {
  270. cur_state.depth.test_func = depth.test_func;
  271. glDepthFunc(depth.test_func);
  272. }
  273. if (cur_state.depth.write_mask != depth.write_mask) {
  274. cur_state.depth.write_mask = depth.write_mask;
  275. glDepthMask(depth.write_mask);
  276. }
  277. }
  278. void OpenGLState::ApplyPrimitiveRestart() const {
  279. Enable(GL_PRIMITIVE_RESTART, cur_state.primitive_restart.enabled, primitive_restart.enabled);
  280. if (cur_state.primitive_restart.index != primitive_restart.index) {
  281. cur_state.primitive_restart.index = primitive_restart.index;
  282. glPrimitiveRestartIndex(primitive_restart.index);
  283. }
  284. }
  285. void OpenGLState::ApplyStencilTest() const {
  286. Enable(GL_STENCIL_TEST, cur_state.stencil.test_enabled, stencil.test_enabled);
  287. const auto ConfigStencil = [](GLenum face, const auto& config, auto& current) {
  288. if (current.test_func != config.test_func || current.test_ref != config.test_ref ||
  289. current.test_mask != config.test_mask) {
  290. current.test_func = config.test_func;
  291. current.test_ref = config.test_ref;
  292. current.test_mask = config.test_mask;
  293. glStencilFuncSeparate(face, config.test_func, config.test_ref, config.test_mask);
  294. }
  295. if (current.action_depth_fail != config.action_depth_fail ||
  296. current.action_depth_pass != config.action_depth_pass ||
  297. current.action_stencil_fail != config.action_stencil_fail) {
  298. current.action_depth_fail = config.action_depth_fail;
  299. current.action_depth_pass = config.action_depth_pass;
  300. current.action_stencil_fail = config.action_stencil_fail;
  301. glStencilOpSeparate(face, config.action_stencil_fail, config.action_depth_fail,
  302. config.action_depth_pass);
  303. }
  304. if (current.write_mask != config.write_mask) {
  305. current.write_mask = config.write_mask;
  306. glStencilMaskSeparate(face, config.write_mask);
  307. }
  308. };
  309. ConfigStencil(GL_FRONT, stencil.front, cur_state.stencil.front);
  310. ConfigStencil(GL_BACK, stencil.back, cur_state.stencil.back);
  311. }
  312. void OpenGLState::ApplyViewport() const {
  313. for (GLuint i = 0; i < static_cast<GLuint>(Maxwell::NumViewports); ++i) {
  314. const auto& updated = viewports[i];
  315. auto& current = cur_state.viewports[i];
  316. if (current.x != updated.x || current.y != updated.y || current.width != updated.width ||
  317. current.height != updated.height) {
  318. current.x = updated.x;
  319. current.y = updated.y;
  320. current.width = updated.width;
  321. current.height = updated.height;
  322. glViewportIndexedf(i, static_cast<GLfloat>(updated.x), static_cast<GLfloat>(updated.y),
  323. static_cast<GLfloat>(updated.width),
  324. static_cast<GLfloat>(updated.height));
  325. }
  326. if (current.depth_range_near != updated.depth_range_near ||
  327. current.depth_range_far != updated.depth_range_far) {
  328. current.depth_range_near = updated.depth_range_near;
  329. current.depth_range_far = updated.depth_range_far;
  330. glDepthRangeIndexed(i, updated.depth_range_near, updated.depth_range_far);
  331. }
  332. Enable(GL_SCISSOR_TEST, i, current.scissor.enabled, updated.scissor.enabled);
  333. if (current.scissor.x != updated.scissor.x || current.scissor.y != updated.scissor.y ||
  334. current.scissor.width != updated.scissor.width ||
  335. current.scissor.height != updated.scissor.height) {
  336. current.scissor.x = updated.scissor.x;
  337. current.scissor.y = updated.scissor.y;
  338. current.scissor.width = updated.scissor.width;
  339. current.scissor.height = updated.scissor.height;
  340. glScissorIndexed(i, updated.scissor.x, updated.scissor.y, updated.scissor.width,
  341. updated.scissor.height);
  342. }
  343. }
  344. }
  345. void OpenGLState::ApplyGlobalBlending() const {
  346. const Blend& updated = blend[0];
  347. Blend& current = cur_state.blend[0];
  348. Enable(GL_BLEND, current.enabled, updated.enabled);
  349. if (current.src_rgb_func != updated.src_rgb_func ||
  350. current.dst_rgb_func != updated.dst_rgb_func || current.src_a_func != updated.src_a_func ||
  351. current.dst_a_func != updated.dst_a_func) {
  352. current.src_rgb_func = updated.src_rgb_func;
  353. current.dst_rgb_func = updated.dst_rgb_func;
  354. current.src_a_func = updated.src_a_func;
  355. current.dst_a_func = updated.dst_a_func;
  356. glBlendFuncSeparate(updated.src_rgb_func, updated.dst_rgb_func, updated.src_a_func,
  357. updated.dst_a_func);
  358. }
  359. if (current.rgb_equation != updated.rgb_equation || current.a_equation != updated.a_equation) {
  360. current.rgb_equation = updated.rgb_equation;
  361. current.a_equation = updated.a_equation;
  362. glBlendEquationSeparate(updated.rgb_equation, updated.a_equation);
  363. }
  364. }
  365. void OpenGLState::ApplyTargetBlending(std::size_t target, bool force) const {
  366. const Blend& updated = blend[target];
  367. Blend& current = cur_state.blend[target];
  368. if (current.enabled != updated.enabled || force) {
  369. current.enabled = updated.enabled;
  370. Enable(GL_BLEND, static_cast<GLuint>(target), updated.enabled);
  371. }
  372. if (UpdateTie(std::tie(current.src_rgb_func, current.dst_rgb_func, current.src_a_func,
  373. current.dst_a_func),
  374. std::tie(updated.src_rgb_func, updated.dst_rgb_func, updated.src_a_func,
  375. updated.dst_a_func))) {
  376. glBlendFuncSeparatei(static_cast<GLuint>(target), updated.src_rgb_func,
  377. updated.dst_rgb_func, updated.src_a_func, updated.dst_a_func);
  378. }
  379. if (UpdateTie(std::tie(current.rgb_equation, current.a_equation),
  380. std::tie(updated.rgb_equation, updated.a_equation))) {
  381. glBlendEquationSeparatei(static_cast<GLuint>(target), updated.rgb_equation,
  382. updated.a_equation);
  383. }
  384. }
  385. void OpenGLState::ApplyBlending() const {
  386. if (independant_blend.enabled) {
  387. const bool force = independant_blend.enabled != cur_state.independant_blend.enabled;
  388. for (std::size_t target = 0; target < Maxwell::NumRenderTargets; ++target) {
  389. ApplyTargetBlending(target, force);
  390. }
  391. } else {
  392. ApplyGlobalBlending();
  393. }
  394. cur_state.independant_blend.enabled = independant_blend.enabled;
  395. if (UpdateTie(
  396. std::tie(cur_state.blend_color.red, cur_state.blend_color.green,
  397. cur_state.blend_color.blue, cur_state.blend_color.alpha),
  398. std::tie(blend_color.red, blend_color.green, blend_color.blue, blend_color.alpha))) {
  399. glBlendColor(blend_color.red, blend_color.green, blend_color.blue, blend_color.alpha);
  400. }
  401. }
  402. void OpenGLState::ApplyLogicOp() const {
  403. Enable(GL_COLOR_LOGIC_OP, cur_state.logic_op.enabled, logic_op.enabled);
  404. if (UpdateValue(cur_state.logic_op.operation, logic_op.operation)) {
  405. glLogicOp(logic_op.operation);
  406. }
  407. }
  408. void OpenGLState::ApplyPolygonOffset() const {
  409. Enable(GL_POLYGON_OFFSET_FILL, cur_state.polygon_offset.fill_enable,
  410. polygon_offset.fill_enable);
  411. Enable(GL_POLYGON_OFFSET_LINE, cur_state.polygon_offset.line_enable,
  412. polygon_offset.line_enable);
  413. Enable(GL_POLYGON_OFFSET_POINT, cur_state.polygon_offset.point_enable,
  414. polygon_offset.point_enable);
  415. if (UpdateTie(std::tie(cur_state.polygon_offset.factor, cur_state.polygon_offset.units,
  416. cur_state.polygon_offset.clamp),
  417. std::tie(polygon_offset.factor, polygon_offset.units, polygon_offset.clamp))) {
  418. if (GLAD_GL_EXT_polygon_offset_clamp && polygon_offset.clamp != 0) {
  419. glPolygonOffsetClamp(polygon_offset.factor, polygon_offset.units, polygon_offset.clamp);
  420. } else {
  421. UNIMPLEMENTED_IF_MSG(polygon_offset.clamp != 0,
  422. "Unimplemented Depth polygon offset clamp.");
  423. glPolygonOffset(polygon_offset.factor, polygon_offset.units);
  424. }
  425. }
  426. }
  427. void OpenGLState::ApplyAlphaTest() const {
  428. Enable(GL_ALPHA_TEST, cur_state.alpha_test.enabled, alpha_test.enabled);
  429. if (UpdateTie(std::tie(cur_state.alpha_test.func, cur_state.alpha_test.ref),
  430. std::tie(alpha_test.func, alpha_test.ref))) {
  431. glAlphaFunc(alpha_test.func, alpha_test.ref);
  432. }
  433. }
  434. void OpenGLState::ApplyTextures() const {
  435. if (const auto update = UpdateArray(cur_state.textures, textures)) {
  436. glBindTextures(update->first, update->second, textures.data() + update->first);
  437. }
  438. }
  439. void OpenGLState::ApplySamplers() const {
  440. if (const auto update = UpdateArray(cur_state.samplers, samplers)) {
  441. glBindSamplers(update->first, update->second, samplers.data() + update->first);
  442. }
  443. }
  444. void OpenGLState::ApplyImages() const {
  445. if (const auto update = UpdateArray(cur_state.images, images)) {
  446. glBindImageTextures(update->first, update->second, images.data() + update->first);
  447. }
  448. }
  449. void OpenGLState::Apply() {
  450. MICROPROFILE_SCOPE(OpenGL_State);
  451. ApplyFramebufferState();
  452. ApplyVertexArrayState();
  453. ApplyShaderProgram();
  454. ApplyProgramPipeline();
  455. ApplyClipDistances();
  456. ApplyPointSize();
  457. ApplyFragmentColorClamp();
  458. ApplyMultisample();
  459. if (dirty.color_mask) {
  460. ApplyColorMask();
  461. dirty.color_mask = false;
  462. }
  463. ApplyDepthClamp();
  464. ApplyViewport();
  465. if (dirty.stencil_state) {
  466. ApplyStencilTest();
  467. dirty.stencil_state = false;
  468. }
  469. ApplySRgb();
  470. ApplyCulling();
  471. ApplyDepth();
  472. ApplyPrimitiveRestart();
  473. if (dirty.blend_state) {
  474. ApplyBlending();
  475. dirty.blend_state = false;
  476. }
  477. ApplyLogicOp();
  478. ApplyTextures();
  479. ApplySamplers();
  480. ApplyImages();
  481. if (dirty.polygon_offset) {
  482. ApplyPolygonOffset();
  483. dirty.polygon_offset = false;
  484. }
  485. ApplyAlphaTest();
  486. }
  487. void OpenGLState::EmulateViewportWithScissor() {
  488. auto& current = viewports[0];
  489. if (current.scissor.enabled) {
  490. const GLint left = std::max(current.x, current.scissor.x);
  491. const GLint right =
  492. std::max(current.x + current.width, current.scissor.x + current.scissor.width);
  493. const GLint bottom = std::max(current.y, current.scissor.y);
  494. const GLint top =
  495. std::max(current.y + current.height, current.scissor.y + current.scissor.height);
  496. current.scissor.x = std::max(left, 0);
  497. current.scissor.y = std::max(bottom, 0);
  498. current.scissor.width = std::max(right - left, 0);
  499. current.scissor.height = std::max(top - bottom, 0);
  500. } else {
  501. current.scissor.enabled = true;
  502. current.scissor.x = current.x;
  503. current.scissor.y = current.y;
  504. current.scissor.width = current.width;
  505. current.scissor.height = current.height;
  506. }
  507. }
  508. OpenGLState& OpenGLState::UnbindTexture(GLuint handle) {
  509. for (auto& texture : textures) {
  510. if (texture == handle) {
  511. texture = 0;
  512. }
  513. }
  514. return *this;
  515. }
  516. OpenGLState& OpenGLState::ResetSampler(GLuint handle) {
  517. for (auto& sampler : samplers) {
  518. if (sampler == handle) {
  519. sampler = 0;
  520. }
  521. }
  522. return *this;
  523. }
  524. OpenGLState& OpenGLState::ResetProgram(GLuint handle) {
  525. if (draw.shader_program == handle) {
  526. draw.shader_program = 0;
  527. }
  528. return *this;
  529. }
  530. OpenGLState& OpenGLState::ResetPipeline(GLuint handle) {
  531. if (draw.program_pipeline == handle) {
  532. draw.program_pipeline = 0;
  533. }
  534. return *this;
  535. }
  536. OpenGLState& OpenGLState::ResetVertexArray(GLuint handle) {
  537. if (draw.vertex_array == handle) {
  538. draw.vertex_array = 0;
  539. }
  540. return *this;
  541. }
  542. OpenGLState& OpenGLState::ResetFramebuffer(GLuint handle) {
  543. if (draw.read_framebuffer == handle) {
  544. draw.read_framebuffer = 0;
  545. }
  546. if (draw.draw_framebuffer == handle) {
  547. draw.draw_framebuffer = 0;
  548. }
  549. return *this;
  550. }
  551. } // namespace OpenGL