gl_state.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 "video_core/renderer_opengl/gl_state.h"
  9. namespace OpenGL {
  10. OpenGLState OpenGLState::cur_state;
  11. OpenGLState::OpenGLState() {
  12. // These all match default OpenGL values
  13. cull.enabled = false;
  14. cull.mode = GL_BACK;
  15. cull.front_face = GL_CCW;
  16. depth.test_enabled = false;
  17. depth.test_func = GL_LESS;
  18. depth.write_mask = GL_TRUE;
  19. color_mask.red_enabled = GL_TRUE;
  20. color_mask.green_enabled = GL_TRUE;
  21. color_mask.blue_enabled = GL_TRUE;
  22. color_mask.alpha_enabled = GL_TRUE;
  23. stencil.test_enabled = false;
  24. auto reset_stencil = [](auto& config) {
  25. config.test_func = GL_ALWAYS;
  26. config.test_ref = 0;
  27. config.test_mask = 0xFFFFFFFF;
  28. config.write_mask = 0xFFFFFFFF;
  29. config.action_depth_fail = GL_KEEP;
  30. config.action_depth_pass = GL_KEEP;
  31. config.action_stencil_fail = GL_KEEP;
  32. };
  33. reset_stencil(stencil.front);
  34. reset_stencil(stencil.back);
  35. blend.enabled = true;
  36. blend.rgb_equation = GL_FUNC_ADD;
  37. blend.a_equation = GL_FUNC_ADD;
  38. blend.src_rgb_func = GL_ONE;
  39. blend.dst_rgb_func = GL_ZERO;
  40. blend.src_a_func = GL_ONE;
  41. blend.dst_a_func = GL_ZERO;
  42. blend.color.red = 0.0f;
  43. blend.color.green = 0.0f;
  44. blend.color.blue = 0.0f;
  45. blend.color.alpha = 0.0f;
  46. logic_op.enabled = false;
  47. logic_op.operation = GL_COPY;
  48. for (auto& texture_unit : texture_units) {
  49. texture_unit.Reset();
  50. }
  51. draw.read_framebuffer = 0;
  52. draw.draw_framebuffer = 0;
  53. draw.vertex_array = 0;
  54. draw.vertex_buffer = 0;
  55. draw.uniform_buffer = 0;
  56. draw.shader_program = 0;
  57. draw.program_pipeline = 0;
  58. scissor.enabled = false;
  59. scissor.x = 0;
  60. scissor.y = 0;
  61. scissor.width = 0;
  62. scissor.height = 0;
  63. viewport.x = 0;
  64. viewport.y = 0;
  65. viewport.width = 0;
  66. viewport.height = 0;
  67. clip_distance = {};
  68. }
  69. void OpenGLState::Apply() const {
  70. // Culling
  71. if (cull.enabled != cur_state.cull.enabled) {
  72. if (cull.enabled) {
  73. glEnable(GL_CULL_FACE);
  74. } else {
  75. glDisable(GL_CULL_FACE);
  76. }
  77. }
  78. if (cull.mode != cur_state.cull.mode) {
  79. glCullFace(cull.mode);
  80. }
  81. if (cull.front_face != cur_state.cull.front_face) {
  82. glFrontFace(cull.front_face);
  83. }
  84. // Depth test
  85. if (depth.test_enabled != cur_state.depth.test_enabled) {
  86. if (depth.test_enabled) {
  87. glEnable(GL_DEPTH_TEST);
  88. } else {
  89. glDisable(GL_DEPTH_TEST);
  90. }
  91. }
  92. if (depth.test_func != cur_state.depth.test_func) {
  93. glDepthFunc(depth.test_func);
  94. }
  95. // Depth mask
  96. if (depth.write_mask != cur_state.depth.write_mask) {
  97. glDepthMask(depth.write_mask);
  98. }
  99. // Color mask
  100. if (color_mask.red_enabled != cur_state.color_mask.red_enabled ||
  101. color_mask.green_enabled != cur_state.color_mask.green_enabled ||
  102. color_mask.blue_enabled != cur_state.color_mask.blue_enabled ||
  103. color_mask.alpha_enabled != cur_state.color_mask.alpha_enabled) {
  104. glColorMask(color_mask.red_enabled, color_mask.green_enabled, color_mask.blue_enabled,
  105. color_mask.alpha_enabled);
  106. }
  107. // Stencil test
  108. if (stencil.test_enabled != cur_state.stencil.test_enabled) {
  109. if (stencil.test_enabled) {
  110. glEnable(GL_STENCIL_TEST);
  111. } else {
  112. glDisable(GL_STENCIL_TEST);
  113. }
  114. }
  115. auto config_stencil = [](GLenum face, const auto& config, const auto& prev_config) {
  116. if (config.test_func != prev_config.test_func || config.test_ref != prev_config.test_ref ||
  117. config.test_mask != prev_config.test_mask) {
  118. glStencilFuncSeparate(face, config.test_func, config.test_ref, config.test_mask);
  119. }
  120. if (config.action_depth_fail != prev_config.action_depth_fail ||
  121. config.action_depth_pass != prev_config.action_depth_pass ||
  122. config.action_stencil_fail != prev_config.action_stencil_fail) {
  123. glStencilOpSeparate(face, config.action_stencil_fail, config.action_depth_fail,
  124. config.action_depth_pass);
  125. }
  126. if (config.write_mask != prev_config.write_mask) {
  127. glStencilMaskSeparate(face, config.write_mask);
  128. }
  129. };
  130. config_stencil(GL_FRONT, stencil.front, cur_state.stencil.front);
  131. config_stencil(GL_BACK, stencil.back, cur_state.stencil.back);
  132. // Blending
  133. if (blend.enabled != cur_state.blend.enabled) {
  134. if (blend.enabled) {
  135. ASSERT(!logic_op.enabled);
  136. glEnable(GL_BLEND);
  137. } else {
  138. glDisable(GL_BLEND);
  139. }
  140. }
  141. if (blend.color.red != cur_state.blend.color.red ||
  142. blend.color.green != cur_state.blend.color.green ||
  143. blend.color.blue != cur_state.blend.color.blue ||
  144. blend.color.alpha != cur_state.blend.color.alpha) {
  145. glBlendColor(blend.color.red, blend.color.green, blend.color.blue, blend.color.alpha);
  146. }
  147. if (blend.src_rgb_func != cur_state.blend.src_rgb_func ||
  148. blend.dst_rgb_func != cur_state.blend.dst_rgb_func ||
  149. blend.src_a_func != cur_state.blend.src_a_func ||
  150. blend.dst_a_func != cur_state.blend.dst_a_func) {
  151. glBlendFuncSeparate(blend.src_rgb_func, blend.dst_rgb_func, blend.src_a_func,
  152. blend.dst_a_func);
  153. }
  154. if (blend.rgb_equation != cur_state.blend.rgb_equation ||
  155. blend.a_equation != cur_state.blend.a_equation) {
  156. glBlendEquationSeparate(blend.rgb_equation, blend.a_equation);
  157. }
  158. // Logic Operation
  159. if (logic_op.enabled != cur_state.logic_op.enabled) {
  160. if (logic_op.enabled) {
  161. ASSERT(!blend.enabled);
  162. glEnable(GL_COLOR_LOGIC_OP);
  163. } else {
  164. glDisable(GL_COLOR_LOGIC_OP);
  165. }
  166. }
  167. if (logic_op.operation != cur_state.logic_op.operation) {
  168. glLogicOp(logic_op.operation);
  169. }
  170. // Textures
  171. for (std::size_t i = 0; i < std::size(texture_units); ++i) {
  172. const auto& texture_unit = texture_units[i];
  173. const auto& cur_state_texture_unit = cur_state.texture_units[i];
  174. if (texture_unit.texture != cur_state_texture_unit.texture) {
  175. glActiveTexture(TextureUnits::MaxwellTexture(static_cast<int>(i)).Enum());
  176. glBindTexture(texture_unit.target, texture_unit.texture);
  177. }
  178. // Update the texture swizzle
  179. if (texture_unit.swizzle.r != cur_state_texture_unit.swizzle.r ||
  180. texture_unit.swizzle.g != cur_state_texture_unit.swizzle.g ||
  181. texture_unit.swizzle.b != cur_state_texture_unit.swizzle.b ||
  182. texture_unit.swizzle.a != cur_state_texture_unit.swizzle.a) {
  183. std::array<GLint, 4> mask = {texture_unit.swizzle.r, texture_unit.swizzle.g,
  184. texture_unit.swizzle.b, texture_unit.swizzle.a};
  185. glTexParameteriv(texture_unit.target, GL_TEXTURE_SWIZZLE_RGBA, mask.data());
  186. }
  187. }
  188. // Samplers
  189. {
  190. bool has_delta{};
  191. std::size_t first{}, last{};
  192. std::array<GLuint, Tegra::Engines::Maxwell3D::Regs::NumTextureSamplers> samplers;
  193. for (std::size_t i = 0; i < std::size(samplers); ++i) {
  194. samplers[i] = texture_units[i].sampler;
  195. if (samplers[i] != cur_state.texture_units[i].sampler) {
  196. if (!has_delta) {
  197. first = i;
  198. has_delta = true;
  199. }
  200. last = i;
  201. }
  202. }
  203. if (has_delta) {
  204. glBindSamplers(static_cast<GLuint>(first), static_cast<GLsizei>(last - first + 1),
  205. samplers.data());
  206. }
  207. }
  208. // Framebuffer
  209. if (draw.read_framebuffer != cur_state.draw.read_framebuffer) {
  210. glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
  211. }
  212. if (draw.draw_framebuffer != cur_state.draw.draw_framebuffer) {
  213. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, draw.draw_framebuffer);
  214. }
  215. // Vertex array
  216. if (draw.vertex_array != cur_state.draw.vertex_array) {
  217. glBindVertexArray(draw.vertex_array);
  218. }
  219. // Vertex buffer
  220. if (draw.vertex_buffer != cur_state.draw.vertex_buffer) {
  221. glBindBuffer(GL_ARRAY_BUFFER, draw.vertex_buffer);
  222. }
  223. // Uniform buffer
  224. if (draw.uniform_buffer != cur_state.draw.uniform_buffer) {
  225. glBindBuffer(GL_UNIFORM_BUFFER, draw.uniform_buffer);
  226. }
  227. // Shader program
  228. if (draw.shader_program != cur_state.draw.shader_program) {
  229. glUseProgram(draw.shader_program);
  230. }
  231. // Program pipeline
  232. if (draw.program_pipeline != cur_state.draw.program_pipeline) {
  233. glBindProgramPipeline(draw.program_pipeline);
  234. }
  235. // Scissor test
  236. if (scissor.enabled != cur_state.scissor.enabled) {
  237. if (scissor.enabled) {
  238. glEnable(GL_SCISSOR_TEST);
  239. } else {
  240. glDisable(GL_SCISSOR_TEST);
  241. }
  242. }
  243. if (scissor.x != cur_state.scissor.x || scissor.y != cur_state.scissor.y ||
  244. scissor.width != cur_state.scissor.width || scissor.height != cur_state.scissor.height) {
  245. glScissor(scissor.x, scissor.y, scissor.width, scissor.height);
  246. }
  247. if (viewport.x != cur_state.viewport.x || viewport.y != cur_state.viewport.y ||
  248. viewport.width != cur_state.viewport.width ||
  249. viewport.height != cur_state.viewport.height) {
  250. glViewport(viewport.x, viewport.y, viewport.width, viewport.height);
  251. }
  252. // Clip distance
  253. for (std::size_t i = 0; i < clip_distance.size(); ++i) {
  254. if (clip_distance[i] != cur_state.clip_distance[i]) {
  255. if (clip_distance[i]) {
  256. glEnable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i));
  257. } else {
  258. glDisable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i));
  259. }
  260. }
  261. }
  262. cur_state = *this;
  263. }
  264. OpenGLState& OpenGLState::UnbindTexture(GLuint handle) {
  265. for (auto& unit : texture_units) {
  266. if (unit.texture == handle) {
  267. unit.Unbind();
  268. }
  269. }
  270. return *this;
  271. }
  272. OpenGLState& OpenGLState::ResetSampler(GLuint handle) {
  273. for (auto& unit : texture_units) {
  274. if (unit.sampler == handle) {
  275. unit.sampler = 0;
  276. }
  277. }
  278. return *this;
  279. }
  280. OpenGLState& OpenGLState::ResetProgram(GLuint handle) {
  281. if (draw.shader_program == handle) {
  282. draw.shader_program = 0;
  283. }
  284. return *this;
  285. }
  286. OpenGLState& OpenGLState::ResetPipeline(GLuint handle) {
  287. if (draw.program_pipeline == handle) {
  288. draw.program_pipeline = 0;
  289. }
  290. return *this;
  291. }
  292. OpenGLState& OpenGLState::ResetBuffer(GLuint handle) {
  293. if (draw.vertex_buffer == handle) {
  294. draw.vertex_buffer = 0;
  295. }
  296. if (draw.uniform_buffer == handle) {
  297. draw.uniform_buffer = 0;
  298. }
  299. return *this;
  300. }
  301. OpenGLState& OpenGLState::ResetVertexArray(GLuint handle) {
  302. if (draw.vertex_array == handle) {
  303. draw.vertex_array = 0;
  304. }
  305. return *this;
  306. }
  307. OpenGLState& OpenGLState::ResetFramebuffer(GLuint handle) {
  308. if (draw.read_framebuffer == handle) {
  309. draw.read_framebuffer = 0;
  310. }
  311. if (draw.draw_framebuffer == handle) {
  312. draw.draw_framebuffer = 0;
  313. }
  314. return *this;
  315. }
  316. } // namespace OpenGL