gl_state.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 = 0xFF;
  25. stencil.write_mask = 0xFF;
  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.rgb_equation = GL_FUNC_ADD;
  31. blend.a_equation = GL_FUNC_ADD;
  32. blend.src_rgb_func = GL_ONE;
  33. blend.dst_rgb_func = GL_ZERO;
  34. blend.src_a_func = GL_ONE;
  35. blend.dst_a_func = GL_ZERO;
  36. blend.color.red = 0.0f;
  37. blend.color.green = 0.0f;
  38. blend.color.blue = 0.0f;
  39. blend.color.alpha = 0.0f;
  40. logic_op = GL_COPY;
  41. for (auto& texture_unit : texture_units) {
  42. texture_unit.texture_2d = 0;
  43. texture_unit.sampler = 0;
  44. }
  45. lighting_lut.texture_buffer = 0;
  46. fog_lut.texture_buffer = 0;
  47. proctex_lut.texture_buffer = 0;
  48. proctex_diff_lut.texture_buffer = 0;
  49. proctex_color_map.texture_buffer = 0;
  50. proctex_alpha_map.texture_buffer = 0;
  51. proctex_noise_lut.texture_buffer = 0;
  52. draw.read_framebuffer = 0;
  53. draw.draw_framebuffer = 0;
  54. draw.vertex_array = 0;
  55. draw.vertex_buffer = 0;
  56. draw.uniform_buffer = 0;
  57. draw.shader_program = 0;
  58. clip_distance = {};
  59. }
  60. void OpenGLState::Apply() const {
  61. // Culling
  62. if (cull.enabled != cur_state.cull.enabled) {
  63. if (cull.enabled) {
  64. glEnable(GL_CULL_FACE);
  65. } else {
  66. glDisable(GL_CULL_FACE);
  67. }
  68. }
  69. if (cull.mode != cur_state.cull.mode) {
  70. glCullFace(cull.mode);
  71. }
  72. if (cull.front_face != cur_state.cull.front_face) {
  73. glFrontFace(cull.front_face);
  74. }
  75. // Depth test
  76. if (depth.test_enabled != cur_state.depth.test_enabled) {
  77. if (depth.test_enabled) {
  78. glEnable(GL_DEPTH_TEST);
  79. } else {
  80. glDisable(GL_DEPTH_TEST);
  81. }
  82. }
  83. if (depth.test_func != cur_state.depth.test_func) {
  84. glDepthFunc(depth.test_func);
  85. }
  86. // Depth mask
  87. if (depth.write_mask != cur_state.depth.write_mask) {
  88. glDepthMask(depth.write_mask);
  89. }
  90. // Color mask
  91. if (color_mask.red_enabled != cur_state.color_mask.red_enabled ||
  92. color_mask.green_enabled != cur_state.color_mask.green_enabled ||
  93. color_mask.blue_enabled != cur_state.color_mask.blue_enabled ||
  94. color_mask.alpha_enabled != cur_state.color_mask.alpha_enabled) {
  95. glColorMask(color_mask.red_enabled, color_mask.green_enabled, color_mask.blue_enabled,
  96. color_mask.alpha_enabled);
  97. }
  98. // Stencil test
  99. if (stencil.test_enabled != cur_state.stencil.test_enabled) {
  100. if (stencil.test_enabled) {
  101. glEnable(GL_STENCIL_TEST);
  102. } else {
  103. glDisable(GL_STENCIL_TEST);
  104. }
  105. }
  106. if (stencil.test_func != cur_state.stencil.test_func ||
  107. stencil.test_ref != cur_state.stencil.test_ref ||
  108. stencil.test_mask != cur_state.stencil.test_mask) {
  109. glStencilFunc(stencil.test_func, stencil.test_ref, stencil.test_mask);
  110. }
  111. if (stencil.action_depth_fail != cur_state.stencil.action_depth_fail ||
  112. stencil.action_depth_pass != cur_state.stencil.action_depth_pass ||
  113. stencil.action_stencil_fail != cur_state.stencil.action_stencil_fail) {
  114. glStencilOp(stencil.action_stencil_fail, stencil.action_depth_fail,
  115. stencil.action_depth_pass);
  116. }
  117. // Stencil mask
  118. if (stencil.write_mask != cur_state.stencil.write_mask) {
  119. glStencilMask(stencil.write_mask);
  120. }
  121. // Blending
  122. if (blend.enabled != cur_state.blend.enabled) {
  123. if (blend.enabled) {
  124. glEnable(GL_BLEND);
  125. cur_state.logic_op = GL_COPY;
  126. glLogicOp(cur_state.logic_op);
  127. glDisable(GL_COLOR_LOGIC_OP);
  128. } else {
  129. glDisable(GL_BLEND);
  130. glEnable(GL_COLOR_LOGIC_OP);
  131. }
  132. }
  133. if (blend.color.red != cur_state.blend.color.red ||
  134. blend.color.green != cur_state.blend.color.green ||
  135. blend.color.blue != cur_state.blend.color.blue ||
  136. blend.color.alpha != cur_state.blend.color.alpha) {
  137. glBlendColor(blend.color.red, blend.color.green, blend.color.blue, blend.color.alpha);
  138. }
  139. if (blend.src_rgb_func != cur_state.blend.src_rgb_func ||
  140. blend.dst_rgb_func != cur_state.blend.dst_rgb_func ||
  141. blend.src_a_func != cur_state.blend.src_a_func ||
  142. blend.dst_a_func != cur_state.blend.dst_a_func) {
  143. glBlendFuncSeparate(blend.src_rgb_func, blend.dst_rgb_func, blend.src_a_func,
  144. blend.dst_a_func);
  145. }
  146. if (blend.rgb_equation != cur_state.blend.rgb_equation ||
  147. blend.a_equation != cur_state.blend.a_equation) {
  148. glBlendEquationSeparate(blend.rgb_equation, blend.a_equation);
  149. }
  150. if (logic_op != cur_state.logic_op) {
  151. glLogicOp(logic_op);
  152. }
  153. // Textures
  154. for (unsigned i = 0; i < ARRAY_SIZE(texture_units); ++i) {
  155. if (texture_units[i].texture_2d != cur_state.texture_units[i].texture_2d) {
  156. glActiveTexture(TextureUnits::PicaTexture(i).Enum());
  157. glBindTexture(GL_TEXTURE_2D, texture_units[i].texture_2d);
  158. }
  159. if (texture_units[i].sampler != cur_state.texture_units[i].sampler) {
  160. glBindSampler(i, texture_units[i].sampler);
  161. }
  162. }
  163. // Lighting LUTs
  164. if (lighting_lut.texture_buffer != cur_state.lighting_lut.texture_buffer) {
  165. glActiveTexture(TextureUnits::LightingLUT.Enum());
  166. glBindTexture(GL_TEXTURE_BUFFER, cur_state.lighting_lut.texture_buffer);
  167. }
  168. // Fog LUT
  169. if (fog_lut.texture_buffer != cur_state.fog_lut.texture_buffer) {
  170. glActiveTexture(TextureUnits::FogLUT.Enum());
  171. glBindTexture(GL_TEXTURE_BUFFER, fog_lut.texture_buffer);
  172. }
  173. // ProcTex Noise LUT
  174. if (proctex_noise_lut.texture_buffer != cur_state.proctex_noise_lut.texture_buffer) {
  175. glActiveTexture(TextureUnits::ProcTexNoiseLUT.Enum());
  176. glBindTexture(GL_TEXTURE_BUFFER, proctex_noise_lut.texture_buffer);
  177. }
  178. // ProcTex Color Map
  179. if (proctex_color_map.texture_buffer != cur_state.proctex_color_map.texture_buffer) {
  180. glActiveTexture(TextureUnits::ProcTexColorMap.Enum());
  181. glBindTexture(GL_TEXTURE_BUFFER, proctex_color_map.texture_buffer);
  182. }
  183. // ProcTex Alpha Map
  184. if (proctex_alpha_map.texture_buffer != cur_state.proctex_alpha_map.texture_buffer) {
  185. glActiveTexture(TextureUnits::ProcTexAlphaMap.Enum());
  186. glBindTexture(GL_TEXTURE_BUFFER, proctex_alpha_map.texture_buffer);
  187. }
  188. // ProcTex LUT
  189. if (proctex_lut.texture_buffer != cur_state.proctex_lut.texture_buffer) {
  190. glActiveTexture(TextureUnits::ProcTexLUT.Enum());
  191. glBindTexture(GL_TEXTURE_BUFFER, proctex_lut.texture_buffer);
  192. }
  193. // ProcTex Diff LUT
  194. if (proctex_diff_lut.texture_buffer != cur_state.proctex_diff_lut.texture_buffer) {
  195. glActiveTexture(TextureUnits::ProcTexDiffLUT.Enum());
  196. glBindTexture(GL_TEXTURE_BUFFER, proctex_diff_lut.texture_buffer);
  197. }
  198. // Framebuffer
  199. if (draw.read_framebuffer != cur_state.draw.read_framebuffer) {
  200. glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
  201. }
  202. if (draw.draw_framebuffer != cur_state.draw.draw_framebuffer) {
  203. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, draw.draw_framebuffer);
  204. }
  205. // Vertex array
  206. if (draw.vertex_array != cur_state.draw.vertex_array) {
  207. glBindVertexArray(draw.vertex_array);
  208. }
  209. // Vertex buffer
  210. if (draw.vertex_buffer != cur_state.draw.vertex_buffer) {
  211. glBindBuffer(GL_ARRAY_BUFFER, draw.vertex_buffer);
  212. }
  213. // Uniform buffer
  214. if (draw.uniform_buffer != cur_state.draw.uniform_buffer) {
  215. glBindBuffer(GL_UNIFORM_BUFFER, draw.uniform_buffer);
  216. }
  217. // Shader program
  218. if (draw.shader_program != cur_state.draw.shader_program) {
  219. glUseProgram(draw.shader_program);
  220. }
  221. // Clip distance
  222. for (size_t i = 0; i < clip_distance.size(); ++i) {
  223. if (clip_distance[i] != cur_state.clip_distance[i]) {
  224. if (clip_distance[i]) {
  225. glEnable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i));
  226. } else {
  227. glDisable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i));
  228. }
  229. }
  230. }
  231. cur_state = *this;
  232. }
  233. void OpenGLState::ResetTexture(GLuint handle) {
  234. for (auto& unit : cur_state.texture_units) {
  235. if (unit.texture_2d == handle) {
  236. unit.texture_2d = 0;
  237. }
  238. }
  239. if (cur_state.lighting_lut.texture_buffer == handle)
  240. cur_state.lighting_lut.texture_buffer = 0;
  241. if (cur_state.fog_lut.texture_buffer == handle)
  242. cur_state.fog_lut.texture_buffer = 0;
  243. if (cur_state.proctex_noise_lut.texture_buffer == handle)
  244. cur_state.proctex_noise_lut.texture_buffer = 0;
  245. if (cur_state.proctex_color_map.texture_buffer == handle)
  246. cur_state.proctex_color_map.texture_buffer = 0;
  247. if (cur_state.proctex_alpha_map.texture_buffer == handle)
  248. cur_state.proctex_alpha_map.texture_buffer = 0;
  249. if (cur_state.proctex_lut.texture_buffer == handle)
  250. cur_state.proctex_lut.texture_buffer = 0;
  251. if (cur_state.proctex_diff_lut.texture_buffer == handle)
  252. cur_state.proctex_diff_lut.texture_buffer = 0;
  253. }
  254. void OpenGLState::ResetSampler(GLuint handle) {
  255. for (auto& unit : cur_state.texture_units) {
  256. if (unit.sampler == handle) {
  257. unit.sampler = 0;
  258. }
  259. }
  260. }
  261. void OpenGLState::ResetProgram(GLuint handle) {
  262. if (cur_state.draw.shader_program == handle) {
  263. cur_state.draw.shader_program = 0;
  264. }
  265. }
  266. void OpenGLState::ResetBuffer(GLuint handle) {
  267. if (cur_state.draw.vertex_buffer == handle) {
  268. cur_state.draw.vertex_buffer = 0;
  269. }
  270. if (cur_state.draw.uniform_buffer == handle) {
  271. cur_state.draw.uniform_buffer = 0;
  272. }
  273. }
  274. void OpenGLState::ResetVertexArray(GLuint handle) {
  275. if (cur_state.draw.vertex_array == handle) {
  276. cur_state.draw.vertex_array = 0;
  277. }
  278. }
  279. void OpenGLState::ResetFramebuffer(GLuint handle) {
  280. if (cur_state.draw.read_framebuffer == handle) {
  281. cur_state.draw.read_framebuffer = 0;
  282. }
  283. if (cur_state.draw.draw_framebuffer == handle) {
  284. cur_state.draw.draw_framebuffer = 0;
  285. }
  286. }