gl_state.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 = true;
  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. draw.program_pipeline = 0;
  59. scissor.enabled = false;
  60. scissor.x = 0;
  61. scissor.y = 0;
  62. scissor.width = 0;
  63. scissor.height = 0;
  64. viewport.x = 0;
  65. viewport.y = 0;
  66. viewport.width = 0;
  67. viewport.height = 0;
  68. clip_distance = {};
  69. }
  70. void OpenGLState::Apply() const {
  71. // Culling
  72. if (cull.enabled != cur_state.cull.enabled) {
  73. if (cull.enabled) {
  74. glEnable(GL_CULL_FACE);
  75. } else {
  76. glDisable(GL_CULL_FACE);
  77. }
  78. }
  79. if (cull.mode != cur_state.cull.mode) {
  80. glCullFace(cull.mode);
  81. }
  82. if (cull.front_face != cur_state.cull.front_face) {
  83. glFrontFace(cull.front_face);
  84. }
  85. // Depth test
  86. if (depth.test_enabled != cur_state.depth.test_enabled) {
  87. if (depth.test_enabled) {
  88. glEnable(GL_DEPTH_TEST);
  89. } else {
  90. glDisable(GL_DEPTH_TEST);
  91. }
  92. }
  93. if (depth.test_func != cur_state.depth.test_func) {
  94. glDepthFunc(depth.test_func);
  95. }
  96. // Depth mask
  97. if (depth.write_mask != cur_state.depth.write_mask) {
  98. glDepthMask(depth.write_mask);
  99. }
  100. // Color mask
  101. if (color_mask.red_enabled != cur_state.color_mask.red_enabled ||
  102. color_mask.green_enabled != cur_state.color_mask.green_enabled ||
  103. color_mask.blue_enabled != cur_state.color_mask.blue_enabled ||
  104. color_mask.alpha_enabled != cur_state.color_mask.alpha_enabled) {
  105. glColorMask(color_mask.red_enabled, color_mask.green_enabled, color_mask.blue_enabled,
  106. color_mask.alpha_enabled);
  107. }
  108. // Stencil test
  109. if (stencil.test_enabled != cur_state.stencil.test_enabled) {
  110. if (stencil.test_enabled) {
  111. glEnable(GL_STENCIL_TEST);
  112. } else {
  113. glDisable(GL_STENCIL_TEST);
  114. }
  115. }
  116. if (stencil.test_func != cur_state.stencil.test_func ||
  117. stencil.test_ref != cur_state.stencil.test_ref ||
  118. stencil.test_mask != cur_state.stencil.test_mask) {
  119. glStencilFunc(stencil.test_func, stencil.test_ref, stencil.test_mask);
  120. }
  121. if (stencil.action_depth_fail != cur_state.stencil.action_depth_fail ||
  122. stencil.action_depth_pass != cur_state.stencil.action_depth_pass ||
  123. stencil.action_stencil_fail != cur_state.stencil.action_stencil_fail) {
  124. glStencilOp(stencil.action_stencil_fail, stencil.action_depth_fail,
  125. stencil.action_depth_pass);
  126. }
  127. // Stencil mask
  128. if (stencil.write_mask != cur_state.stencil.write_mask) {
  129. glStencilMask(stencil.write_mask);
  130. }
  131. // Blending
  132. if (blend.enabled != cur_state.blend.enabled) {
  133. if (blend.enabled) {
  134. glEnable(GL_BLEND);
  135. glDisable(GL_COLOR_LOGIC_OP);
  136. } else {
  137. glDisable(GL_BLEND);
  138. glEnable(GL_COLOR_LOGIC_OP);
  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. if (logic_op != cur_state.logic_op) {
  159. glLogicOp(logic_op);
  160. }
  161. // Textures
  162. for (unsigned i = 0; i < ARRAY_SIZE(texture_units); ++i) {
  163. if (texture_units[i].texture_2d != cur_state.texture_units[i].texture_2d) {
  164. glActiveTexture(TextureUnits::PicaTexture(i).Enum());
  165. glBindTexture(GL_TEXTURE_2D, texture_units[i].texture_2d);
  166. }
  167. if (texture_units[i].sampler != cur_state.texture_units[i].sampler) {
  168. glBindSampler(i, texture_units[i].sampler);
  169. }
  170. }
  171. // Lighting LUTs
  172. if (lighting_lut.texture_buffer != cur_state.lighting_lut.texture_buffer) {
  173. glActiveTexture(TextureUnits::LightingLUT.Enum());
  174. glBindTexture(GL_TEXTURE_BUFFER, lighting_lut.texture_buffer);
  175. }
  176. // Fog LUT
  177. if (fog_lut.texture_buffer != cur_state.fog_lut.texture_buffer) {
  178. glActiveTexture(TextureUnits::FogLUT.Enum());
  179. glBindTexture(GL_TEXTURE_BUFFER, fog_lut.texture_buffer);
  180. }
  181. // ProcTex Noise LUT
  182. if (proctex_noise_lut.texture_buffer != cur_state.proctex_noise_lut.texture_buffer) {
  183. glActiveTexture(TextureUnits::ProcTexNoiseLUT.Enum());
  184. glBindTexture(GL_TEXTURE_BUFFER, proctex_noise_lut.texture_buffer);
  185. }
  186. // ProcTex Color Map
  187. if (proctex_color_map.texture_buffer != cur_state.proctex_color_map.texture_buffer) {
  188. glActiveTexture(TextureUnits::ProcTexColorMap.Enum());
  189. glBindTexture(GL_TEXTURE_BUFFER, proctex_color_map.texture_buffer);
  190. }
  191. // ProcTex Alpha Map
  192. if (proctex_alpha_map.texture_buffer != cur_state.proctex_alpha_map.texture_buffer) {
  193. glActiveTexture(TextureUnits::ProcTexAlphaMap.Enum());
  194. glBindTexture(GL_TEXTURE_BUFFER, proctex_alpha_map.texture_buffer);
  195. }
  196. // ProcTex LUT
  197. if (proctex_lut.texture_buffer != cur_state.proctex_lut.texture_buffer) {
  198. glActiveTexture(TextureUnits::ProcTexLUT.Enum());
  199. glBindTexture(GL_TEXTURE_BUFFER, proctex_lut.texture_buffer);
  200. }
  201. // ProcTex Diff LUT
  202. if (proctex_diff_lut.texture_buffer != cur_state.proctex_diff_lut.texture_buffer) {
  203. glActiveTexture(TextureUnits::ProcTexDiffLUT.Enum());
  204. glBindTexture(GL_TEXTURE_BUFFER, proctex_diff_lut.texture_buffer);
  205. }
  206. // Framebuffer
  207. if (draw.read_framebuffer != cur_state.draw.read_framebuffer) {
  208. glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
  209. }
  210. if (draw.draw_framebuffer != cur_state.draw.draw_framebuffer) {
  211. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, draw.draw_framebuffer);
  212. }
  213. // Vertex array
  214. if (draw.vertex_array != cur_state.draw.vertex_array) {
  215. glBindVertexArray(draw.vertex_array);
  216. }
  217. // Vertex buffer
  218. if (draw.vertex_buffer != cur_state.draw.vertex_buffer) {
  219. glBindBuffer(GL_ARRAY_BUFFER, draw.vertex_buffer);
  220. }
  221. // Uniform buffer
  222. if (draw.uniform_buffer != cur_state.draw.uniform_buffer) {
  223. glBindBuffer(GL_UNIFORM_BUFFER, draw.uniform_buffer);
  224. }
  225. // Shader program
  226. if (draw.shader_program != cur_state.draw.shader_program) {
  227. glUseProgram(draw.shader_program);
  228. }
  229. // Program pipeline
  230. if (draw.program_pipeline != cur_state.draw.program_pipeline) {
  231. glBindProgramPipeline(draw.program_pipeline);
  232. }
  233. // Scissor test
  234. if (scissor.enabled != cur_state.scissor.enabled) {
  235. if (scissor.enabled) {
  236. glEnable(GL_SCISSOR_TEST);
  237. } else {
  238. glDisable(GL_SCISSOR_TEST);
  239. }
  240. }
  241. if (scissor.x != cur_state.scissor.x || scissor.y != cur_state.scissor.y ||
  242. scissor.width != cur_state.scissor.width || scissor.height != cur_state.scissor.height) {
  243. glScissor(scissor.x, scissor.y, scissor.width, scissor.height);
  244. }
  245. if (viewport.x != cur_state.viewport.x || viewport.y != cur_state.viewport.y ||
  246. viewport.width != cur_state.viewport.width ||
  247. viewport.height != cur_state.viewport.height) {
  248. glViewport(viewport.x, viewport.y, viewport.width, viewport.height);
  249. }
  250. // Clip distance
  251. for (size_t i = 0; i < clip_distance.size(); ++i) {
  252. if (clip_distance[i] != cur_state.clip_distance[i]) {
  253. if (clip_distance[i]) {
  254. glEnable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i));
  255. } else {
  256. glDisable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i));
  257. }
  258. }
  259. }
  260. cur_state = *this;
  261. }
  262. OpenGLState& OpenGLState::ResetTexture(GLuint handle) {
  263. for (auto& unit : texture_units) {
  264. if (unit.texture_2d == handle) {
  265. unit.texture_2d = 0;
  266. }
  267. }
  268. if (lighting_lut.texture_buffer == handle)
  269. lighting_lut.texture_buffer = 0;
  270. if (fog_lut.texture_buffer == handle)
  271. fog_lut.texture_buffer = 0;
  272. if (proctex_noise_lut.texture_buffer == handle)
  273. proctex_noise_lut.texture_buffer = 0;
  274. if (proctex_color_map.texture_buffer == handle)
  275. proctex_color_map.texture_buffer = 0;
  276. if (proctex_alpha_map.texture_buffer == handle)
  277. proctex_alpha_map.texture_buffer = 0;
  278. if (proctex_lut.texture_buffer == handle)
  279. proctex_lut.texture_buffer = 0;
  280. if (proctex_diff_lut.texture_buffer == handle)
  281. proctex_diff_lut.texture_buffer = 0;
  282. return *this;
  283. }
  284. OpenGLState& OpenGLState::ResetSampler(GLuint handle) {
  285. for (auto& unit : texture_units) {
  286. if (unit.sampler == handle) {
  287. unit.sampler = 0;
  288. }
  289. }
  290. return *this;
  291. }
  292. OpenGLState& OpenGLState::ResetProgram(GLuint handle) {
  293. if (draw.shader_program == handle) {
  294. draw.shader_program = 0;
  295. }
  296. return *this;
  297. }
  298. OpenGLState& OpenGLState::ResetPipeline(GLuint handle) {
  299. if (draw.program_pipeline == handle) {
  300. draw.program_pipeline = 0;
  301. }
  302. return *this;
  303. }
  304. OpenGLState& OpenGLState::ResetBuffer(GLuint handle) {
  305. if (draw.vertex_buffer == handle) {
  306. draw.vertex_buffer = 0;
  307. }
  308. if (draw.uniform_buffer == handle) {
  309. draw.uniform_buffer = 0;
  310. }
  311. return *this;
  312. }
  313. OpenGLState& OpenGLState::ResetVertexArray(GLuint handle) {
  314. if (draw.vertex_array == handle) {
  315. draw.vertex_array = 0;
  316. }
  317. return *this;
  318. }
  319. OpenGLState& OpenGLState::ResetFramebuffer(GLuint handle) {
  320. if (draw.read_framebuffer == handle) {
  321. draw.read_framebuffer = 0;
  322. }
  323. if (draw.draw_framebuffer == handle) {
  324. draw.draw_framebuffer = 0;
  325. }
  326. return *this;
  327. }