gl_state.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <glad/glad.h>
  5. #include "common/common_funcs.h"
  6. #include "common/logging/log.h"
  7. #include "video_core/renderer_opengl/gl_state.h"
  8. OpenGLState OpenGLState::cur_state;
  9. OpenGLState::OpenGLState() {
  10. // These all match default OpenGL values
  11. cull.enabled = false;
  12. cull.mode = GL_BACK;
  13. cull.front_face = GL_CCW;
  14. depth.test_enabled = false;
  15. depth.test_func = GL_LESS;
  16. depth.write_mask = GL_TRUE;
  17. color_mask.red_enabled = GL_TRUE;
  18. color_mask.green_enabled = GL_TRUE;
  19. color_mask.blue_enabled = GL_TRUE;
  20. color_mask.alpha_enabled = GL_TRUE;
  21. stencil.test_enabled = false;
  22. stencil.test_func = GL_ALWAYS;
  23. stencil.test_ref = 0;
  24. stencil.test_mask = -1;
  25. stencil.write_mask = -1;
  26. stencil.action_depth_fail = GL_KEEP;
  27. stencil.action_depth_pass = GL_KEEP;
  28. stencil.action_stencil_fail = GL_KEEP;
  29. blend.enabled = false;
  30. blend.src_rgb_func = GL_ONE;
  31. blend.dst_rgb_func = GL_ZERO;
  32. blend.src_a_func = GL_ONE;
  33. blend.dst_a_func = GL_ZERO;
  34. blend.color.red = 0.0f;
  35. blend.color.green = 0.0f;
  36. blend.color.blue = 0.0f;
  37. blend.color.alpha = 0.0f;
  38. logic_op = GL_COPY;
  39. for (auto& texture_unit : texture_units) {
  40. texture_unit.texture_2d = 0;
  41. texture_unit.sampler = 0;
  42. }
  43. for (auto& lut : lighting_luts) {
  44. lut.texture_1d = 0;
  45. }
  46. draw.read_framebuffer = 0;
  47. draw.draw_framebuffer = 0;
  48. draw.vertex_array = 0;
  49. draw.vertex_buffer = 0;
  50. draw.uniform_buffer = 0;
  51. draw.shader_program = 0;
  52. }
  53. void OpenGLState::Apply() const {
  54. // Culling
  55. if (cull.enabled != cur_state.cull.enabled) {
  56. if (cull.enabled) {
  57. glEnable(GL_CULL_FACE);
  58. } else {
  59. glDisable(GL_CULL_FACE);
  60. }
  61. }
  62. if (cull.mode != cur_state.cull.mode) {
  63. glCullFace(cull.mode);
  64. }
  65. if (cull.front_face != cur_state.cull.front_face) {
  66. glFrontFace(cull.front_face);
  67. }
  68. // Depth test
  69. if (depth.test_enabled != cur_state.depth.test_enabled) {
  70. if (depth.test_enabled) {
  71. glEnable(GL_DEPTH_TEST);
  72. } else {
  73. glDisable(GL_DEPTH_TEST);
  74. }
  75. }
  76. if (depth.test_func != cur_state.depth.test_func) {
  77. glDepthFunc(depth.test_func);
  78. }
  79. // Depth mask
  80. if (depth.write_mask != cur_state.depth.write_mask) {
  81. glDepthMask(depth.write_mask);
  82. }
  83. // Color mask
  84. if (color_mask.red_enabled != cur_state.color_mask.red_enabled ||
  85. color_mask.green_enabled != cur_state.color_mask.green_enabled ||
  86. color_mask.blue_enabled != cur_state.color_mask.blue_enabled ||
  87. color_mask.alpha_enabled != cur_state.color_mask.alpha_enabled) {
  88. glColorMask(color_mask.red_enabled, color_mask.green_enabled,
  89. color_mask.blue_enabled, color_mask.alpha_enabled);
  90. }
  91. // Stencil test
  92. if (stencil.test_enabled != cur_state.stencil.test_enabled) {
  93. if (stencil.test_enabled) {
  94. glEnable(GL_STENCIL_TEST);
  95. } else {
  96. glDisable(GL_STENCIL_TEST);
  97. }
  98. }
  99. if (stencil.test_func != cur_state.stencil.test_func ||
  100. stencil.test_ref != cur_state.stencil.test_ref ||
  101. stencil.test_mask != cur_state.stencil.test_mask) {
  102. glStencilFunc(stencil.test_func, stencil.test_ref, stencil.test_mask);
  103. }
  104. if (stencil.action_depth_fail != cur_state.stencil.action_depth_fail ||
  105. stencil.action_depth_pass != cur_state.stencil.action_depth_pass ||
  106. stencil.action_stencil_fail != cur_state.stencil.action_stencil_fail) {
  107. glStencilOp(stencil.action_stencil_fail, stencil.action_depth_fail, stencil.action_depth_pass);
  108. }
  109. // Stencil mask
  110. if (stencil.write_mask != cur_state.stencil.write_mask) {
  111. glStencilMask(stencil.write_mask);
  112. }
  113. // Blending
  114. if (blend.enabled != cur_state.blend.enabled) {
  115. if (blend.enabled) {
  116. glEnable(GL_BLEND);
  117. cur_state.logic_op = GL_COPY;
  118. glLogicOp(cur_state.logic_op);
  119. glDisable(GL_COLOR_LOGIC_OP);
  120. } else {
  121. glDisable(GL_BLEND);
  122. glEnable(GL_COLOR_LOGIC_OP);
  123. }
  124. }
  125. if (blend.color.red != cur_state.blend.color.red ||
  126. blend.color.green != cur_state.blend.color.green ||
  127. blend.color.blue != cur_state.blend.color.blue ||
  128. blend.color.alpha != cur_state.blend.color.alpha) {
  129. glBlendColor(blend.color.red, blend.color.green,
  130. blend.color.blue, blend.color.alpha);
  131. }
  132. if (blend.src_rgb_func != cur_state.blend.src_rgb_func ||
  133. blend.dst_rgb_func != cur_state.blend.dst_rgb_func ||
  134. blend.src_a_func != cur_state.blend.src_a_func ||
  135. blend.dst_a_func != cur_state.blend.dst_a_func) {
  136. glBlendFuncSeparate(blend.src_rgb_func, blend.dst_rgb_func,
  137. blend.src_a_func, blend.dst_a_func);
  138. }
  139. if (logic_op != cur_state.logic_op) {
  140. glLogicOp(logic_op);
  141. }
  142. // Textures
  143. for (unsigned i = 0; i < ARRAY_SIZE(texture_units); ++i) {
  144. if (texture_units[i].texture_2d != cur_state.texture_units[i].texture_2d) {
  145. glActiveTexture(GL_TEXTURE0 + i);
  146. glBindTexture(GL_TEXTURE_2D, texture_units[i].texture_2d);
  147. }
  148. if (texture_units[i].sampler != cur_state.texture_units[i].sampler) {
  149. glBindSampler(i, texture_units[i].sampler);
  150. }
  151. }
  152. // Lighting LUTs
  153. for (unsigned i = 0; i < ARRAY_SIZE(lighting_luts); ++i) {
  154. if (lighting_luts[i].texture_1d != cur_state.lighting_luts[i].texture_1d) {
  155. glActiveTexture(GL_TEXTURE3 + i);
  156. glBindTexture(GL_TEXTURE_1D, lighting_luts[i].texture_1d);
  157. }
  158. }
  159. // Framebuffer
  160. if (draw.read_framebuffer != cur_state.draw.read_framebuffer) {
  161. glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
  162. }
  163. if (draw.draw_framebuffer != cur_state.draw.draw_framebuffer) {
  164. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, draw.draw_framebuffer);
  165. }
  166. // Vertex array
  167. if (draw.vertex_array != cur_state.draw.vertex_array) {
  168. glBindVertexArray(draw.vertex_array);
  169. }
  170. // Vertex buffer
  171. if (draw.vertex_buffer != cur_state.draw.vertex_buffer) {
  172. glBindBuffer(GL_ARRAY_BUFFER, draw.vertex_buffer);
  173. }
  174. // Uniform buffer
  175. if (draw.uniform_buffer != cur_state.draw.uniform_buffer) {
  176. glBindBuffer(GL_UNIFORM_BUFFER, draw.uniform_buffer);
  177. }
  178. // Shader program
  179. if (draw.shader_program != cur_state.draw.shader_program) {
  180. glUseProgram(draw.shader_program);
  181. }
  182. cur_state = *this;
  183. }
  184. GLenum OpenGLState::CheckFBStatus(GLenum target) {
  185. GLenum fb_status = glCheckFramebufferStatus(target);
  186. if (fb_status != GL_FRAMEBUFFER_COMPLETE) {
  187. const char* fb_description = (target == GL_READ_FRAMEBUFFER ? "READ" : (target == GL_DRAW_FRAMEBUFFER ? "DRAW" : "UNK"));
  188. LOG_CRITICAL(Render_OpenGL, "OpenGL %s framebuffer check failed, status %X", fb_description, fb_status);
  189. }
  190. return fb_status;
  191. }
  192. void OpenGLState::ResetTexture(GLuint handle) {
  193. for (auto& unit : cur_state.texture_units) {
  194. if (unit.texture_2d == handle) {
  195. unit.texture_2d = 0;
  196. }
  197. }
  198. }
  199. void OpenGLState::ResetSampler(GLuint handle) {
  200. for (auto& unit : cur_state.texture_units) {
  201. if (unit.sampler == handle) {
  202. unit.sampler = 0;
  203. }
  204. }
  205. }
  206. void OpenGLState::ResetProgram(GLuint handle) {
  207. if (cur_state.draw.shader_program == handle) {
  208. cur_state.draw.shader_program = 0;
  209. }
  210. }
  211. void OpenGLState::ResetBuffer(GLuint handle) {
  212. if (cur_state.draw.vertex_buffer == handle) {
  213. cur_state.draw.vertex_buffer = 0;
  214. }
  215. if (cur_state.draw.uniform_buffer == handle) {
  216. cur_state.draw.uniform_buffer = 0;
  217. }
  218. }
  219. void OpenGLState::ResetVertexArray(GLuint handle) {
  220. if (cur_state.draw.vertex_array == handle) {
  221. cur_state.draw.vertex_array = 0;
  222. }
  223. }
  224. void OpenGLState::ResetFramebuffer(GLuint handle) {
  225. if (cur_state.draw.read_framebuffer == handle) {
  226. cur_state.draw.read_framebuffer = 0;
  227. }
  228. if (cur_state.draw.draw_framebuffer == handle) {
  229. cur_state.draw.draw_framebuffer = 0;
  230. }
  231. }