gl_state.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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/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. texture_unit.swizzle.r = GL_RED;
  45. texture_unit.swizzle.g = GL_GREEN;
  46. texture_unit.swizzle.b = GL_BLUE;
  47. texture_unit.swizzle.a = GL_ALPHA;
  48. }
  49. lighting_lut.texture_buffer = 0;
  50. fog_lut.texture_buffer = 0;
  51. proctex_lut.texture_buffer = 0;
  52. proctex_diff_lut.texture_buffer = 0;
  53. proctex_color_map.texture_buffer = 0;
  54. proctex_alpha_map.texture_buffer = 0;
  55. proctex_noise_lut.texture_buffer = 0;
  56. draw.read_framebuffer = 0;
  57. draw.draw_framebuffer = 0;
  58. draw.vertex_array = 0;
  59. draw.vertex_buffer = 0;
  60. draw.uniform_buffer = 0;
  61. draw.shader_program = 0;
  62. draw.program_pipeline = 0;
  63. scissor.enabled = false;
  64. scissor.x = 0;
  65. scissor.y = 0;
  66. scissor.width = 0;
  67. scissor.height = 0;
  68. viewport.x = 0;
  69. viewport.y = 0;
  70. viewport.width = 0;
  71. viewport.height = 0;
  72. clip_distance = {};
  73. }
  74. void OpenGLState::Apply() const {
  75. // Culling
  76. if (cull.enabled != cur_state.cull.enabled) {
  77. if (cull.enabled) {
  78. glEnable(GL_CULL_FACE);
  79. } else {
  80. glDisable(GL_CULL_FACE);
  81. }
  82. }
  83. if (cull.mode != cur_state.cull.mode) {
  84. glCullFace(cull.mode);
  85. }
  86. if (cull.front_face != cur_state.cull.front_face) {
  87. glFrontFace(cull.front_face);
  88. }
  89. // Depth test
  90. if (depth.test_enabled != cur_state.depth.test_enabled) {
  91. if (depth.test_enabled) {
  92. glEnable(GL_DEPTH_TEST);
  93. } else {
  94. glDisable(GL_DEPTH_TEST);
  95. }
  96. }
  97. if (depth.test_func != cur_state.depth.test_func) {
  98. glDepthFunc(depth.test_func);
  99. }
  100. // Depth mask
  101. if (depth.write_mask != cur_state.depth.write_mask) {
  102. glDepthMask(depth.write_mask);
  103. }
  104. // Color mask
  105. if (color_mask.red_enabled != cur_state.color_mask.red_enabled ||
  106. color_mask.green_enabled != cur_state.color_mask.green_enabled ||
  107. color_mask.blue_enabled != cur_state.color_mask.blue_enabled ||
  108. color_mask.alpha_enabled != cur_state.color_mask.alpha_enabled) {
  109. glColorMask(color_mask.red_enabled, color_mask.green_enabled, color_mask.blue_enabled,
  110. color_mask.alpha_enabled);
  111. }
  112. // Stencil test
  113. if (stencil.test_enabled != cur_state.stencil.test_enabled) {
  114. if (stencil.test_enabled) {
  115. glEnable(GL_STENCIL_TEST);
  116. } else {
  117. glDisable(GL_STENCIL_TEST);
  118. }
  119. }
  120. if (stencil.test_func != cur_state.stencil.test_func ||
  121. stencil.test_ref != cur_state.stencil.test_ref ||
  122. stencil.test_mask != cur_state.stencil.test_mask) {
  123. glStencilFunc(stencil.test_func, stencil.test_ref, stencil.test_mask);
  124. }
  125. if (stencil.action_depth_fail != cur_state.stencil.action_depth_fail ||
  126. stencil.action_depth_pass != cur_state.stencil.action_depth_pass ||
  127. stencil.action_stencil_fail != cur_state.stencil.action_stencil_fail) {
  128. glStencilOp(stencil.action_stencil_fail, stencil.action_depth_fail,
  129. stencil.action_depth_pass);
  130. }
  131. // Stencil mask
  132. if (stencil.write_mask != cur_state.stencil.write_mask) {
  133. glStencilMask(stencil.write_mask);
  134. }
  135. // Blending
  136. if (blend.enabled != cur_state.blend.enabled) {
  137. if (blend.enabled) {
  138. glEnable(GL_BLEND);
  139. glDisable(GL_COLOR_LOGIC_OP);
  140. } else {
  141. glDisable(GL_BLEND);
  142. glEnable(GL_COLOR_LOGIC_OP);
  143. }
  144. }
  145. if (blend.color.red != cur_state.blend.color.red ||
  146. blend.color.green != cur_state.blend.color.green ||
  147. blend.color.blue != cur_state.blend.color.blue ||
  148. blend.color.alpha != cur_state.blend.color.alpha) {
  149. glBlendColor(blend.color.red, blend.color.green, blend.color.blue, blend.color.alpha);
  150. }
  151. if (blend.src_rgb_func != cur_state.blend.src_rgb_func ||
  152. blend.dst_rgb_func != cur_state.blend.dst_rgb_func ||
  153. blend.src_a_func != cur_state.blend.src_a_func ||
  154. blend.dst_a_func != cur_state.blend.dst_a_func) {
  155. glBlendFuncSeparate(blend.src_rgb_func, blend.dst_rgb_func, blend.src_a_func,
  156. blend.dst_a_func);
  157. }
  158. if (blend.rgb_equation != cur_state.blend.rgb_equation ||
  159. blend.a_equation != cur_state.blend.a_equation) {
  160. glBlendEquationSeparate(blend.rgb_equation, blend.a_equation);
  161. }
  162. if (logic_op != cur_state.logic_op) {
  163. glLogicOp(logic_op);
  164. }
  165. // Textures
  166. for (size_t i = 0; i < std::size(texture_units); ++i) {
  167. if (texture_units[i].texture_2d != cur_state.texture_units[i].texture_2d) {
  168. glActiveTexture(TextureUnits::MaxwellTexture(i).Enum());
  169. glBindTexture(GL_TEXTURE_2D, texture_units[i].texture_2d);
  170. }
  171. if (texture_units[i].sampler != cur_state.texture_units[i].sampler) {
  172. glBindSampler(i, texture_units[i].sampler);
  173. }
  174. // Update the texture swizzle
  175. if (texture_units[i].swizzle.r != cur_state.texture_units[i].swizzle.r ||
  176. texture_units[i].swizzle.g != cur_state.texture_units[i].swizzle.g ||
  177. texture_units[i].swizzle.b != cur_state.texture_units[i].swizzle.b ||
  178. texture_units[i].swizzle.a != cur_state.texture_units[i].swizzle.a) {
  179. std::array<GLint, 4> mask = {texture_units[i].swizzle.r, texture_units[i].swizzle.g,
  180. texture_units[i].swizzle.b, texture_units[i].swizzle.a};
  181. glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, mask.data());
  182. }
  183. }
  184. // Constbuffers
  185. for (u32 stage = 0; stage < draw.const_buffers.size(); ++stage) {
  186. for (u32 buffer_id = 0; buffer_id < draw.const_buffers[stage].size(); ++buffer_id) {
  187. auto& current = cur_state.draw.const_buffers[stage][buffer_id];
  188. auto& new_state = draw.const_buffers[stage][buffer_id];
  189. if (current.enabled != new_state.enabled || current.bindpoint != new_state.bindpoint ||
  190. current.ssbo != new_state.ssbo) {
  191. if (new_state.enabled) {
  192. glBindBufferBase(GL_SHADER_STORAGE_BUFFER, new_state.bindpoint, new_state.ssbo);
  193. }
  194. }
  195. }
  196. }
  197. // Lighting LUTs
  198. if (lighting_lut.texture_buffer != cur_state.lighting_lut.texture_buffer) {
  199. glActiveTexture(TextureUnits::LightingLUT.Enum());
  200. glBindTexture(GL_TEXTURE_BUFFER, lighting_lut.texture_buffer);
  201. }
  202. // Fog LUT
  203. if (fog_lut.texture_buffer != cur_state.fog_lut.texture_buffer) {
  204. glActiveTexture(TextureUnits::FogLUT.Enum());
  205. glBindTexture(GL_TEXTURE_BUFFER, fog_lut.texture_buffer);
  206. }
  207. // ProcTex Noise LUT
  208. if (proctex_noise_lut.texture_buffer != cur_state.proctex_noise_lut.texture_buffer) {
  209. glActiveTexture(TextureUnits::ProcTexNoiseLUT.Enum());
  210. glBindTexture(GL_TEXTURE_BUFFER, proctex_noise_lut.texture_buffer);
  211. }
  212. // ProcTex Color Map
  213. if (proctex_color_map.texture_buffer != cur_state.proctex_color_map.texture_buffer) {
  214. glActiveTexture(TextureUnits::ProcTexColorMap.Enum());
  215. glBindTexture(GL_TEXTURE_BUFFER, proctex_color_map.texture_buffer);
  216. }
  217. // ProcTex Alpha Map
  218. if (proctex_alpha_map.texture_buffer != cur_state.proctex_alpha_map.texture_buffer) {
  219. glActiveTexture(TextureUnits::ProcTexAlphaMap.Enum());
  220. glBindTexture(GL_TEXTURE_BUFFER, proctex_alpha_map.texture_buffer);
  221. }
  222. // ProcTex LUT
  223. if (proctex_lut.texture_buffer != cur_state.proctex_lut.texture_buffer) {
  224. glActiveTexture(TextureUnits::ProcTexLUT.Enum());
  225. glBindTexture(GL_TEXTURE_BUFFER, proctex_lut.texture_buffer);
  226. }
  227. // ProcTex Diff LUT
  228. if (proctex_diff_lut.texture_buffer != cur_state.proctex_diff_lut.texture_buffer) {
  229. glActiveTexture(TextureUnits::ProcTexDiffLUT.Enum());
  230. glBindTexture(GL_TEXTURE_BUFFER, proctex_diff_lut.texture_buffer);
  231. }
  232. // Framebuffer
  233. if (draw.read_framebuffer != cur_state.draw.read_framebuffer) {
  234. glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
  235. }
  236. if (draw.draw_framebuffer != cur_state.draw.draw_framebuffer) {
  237. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, draw.draw_framebuffer);
  238. }
  239. // Vertex array
  240. if (draw.vertex_array != cur_state.draw.vertex_array) {
  241. glBindVertexArray(draw.vertex_array);
  242. }
  243. // Vertex buffer
  244. if (draw.vertex_buffer != cur_state.draw.vertex_buffer) {
  245. glBindBuffer(GL_ARRAY_BUFFER, draw.vertex_buffer);
  246. }
  247. // Uniform buffer
  248. if (draw.uniform_buffer != cur_state.draw.uniform_buffer) {
  249. glBindBuffer(GL_UNIFORM_BUFFER, draw.uniform_buffer);
  250. }
  251. // Shader program
  252. if (draw.shader_program != cur_state.draw.shader_program) {
  253. glUseProgram(draw.shader_program);
  254. }
  255. // Program pipeline
  256. if (draw.program_pipeline != cur_state.draw.program_pipeline) {
  257. glBindProgramPipeline(draw.program_pipeline);
  258. }
  259. // Scissor test
  260. if (scissor.enabled != cur_state.scissor.enabled) {
  261. if (scissor.enabled) {
  262. glEnable(GL_SCISSOR_TEST);
  263. } else {
  264. glDisable(GL_SCISSOR_TEST);
  265. }
  266. }
  267. if (scissor.x != cur_state.scissor.x || scissor.y != cur_state.scissor.y ||
  268. scissor.width != cur_state.scissor.width || scissor.height != cur_state.scissor.height) {
  269. glScissor(scissor.x, scissor.y, scissor.width, scissor.height);
  270. }
  271. if (viewport.x != cur_state.viewport.x || viewport.y != cur_state.viewport.y ||
  272. viewport.width != cur_state.viewport.width ||
  273. viewport.height != cur_state.viewport.height) {
  274. glViewport(viewport.x, viewport.y, viewport.width, viewport.height);
  275. }
  276. // Clip distance
  277. for (size_t i = 0; i < clip_distance.size(); ++i) {
  278. if (clip_distance[i] != cur_state.clip_distance[i]) {
  279. if (clip_distance[i]) {
  280. glEnable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i));
  281. } else {
  282. glDisable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i));
  283. }
  284. }
  285. }
  286. cur_state = *this;
  287. }
  288. OpenGLState& OpenGLState::ResetTexture(GLuint handle) {
  289. for (auto& unit : texture_units) {
  290. if (unit.texture_2d == handle) {
  291. unit.texture_2d = 0;
  292. }
  293. }
  294. if (lighting_lut.texture_buffer == handle)
  295. lighting_lut.texture_buffer = 0;
  296. if (fog_lut.texture_buffer == handle)
  297. fog_lut.texture_buffer = 0;
  298. if (proctex_noise_lut.texture_buffer == handle)
  299. proctex_noise_lut.texture_buffer = 0;
  300. if (proctex_color_map.texture_buffer == handle)
  301. proctex_color_map.texture_buffer = 0;
  302. if (proctex_alpha_map.texture_buffer == handle)
  303. proctex_alpha_map.texture_buffer = 0;
  304. if (proctex_lut.texture_buffer == handle)
  305. proctex_lut.texture_buffer = 0;
  306. if (proctex_diff_lut.texture_buffer == handle)
  307. proctex_diff_lut.texture_buffer = 0;
  308. return *this;
  309. }
  310. OpenGLState& OpenGLState::ResetSampler(GLuint handle) {
  311. for (auto& unit : texture_units) {
  312. if (unit.sampler == handle) {
  313. unit.sampler = 0;
  314. }
  315. }
  316. return *this;
  317. }
  318. OpenGLState& OpenGLState::ResetProgram(GLuint handle) {
  319. if (draw.shader_program == handle) {
  320. draw.shader_program = 0;
  321. }
  322. return *this;
  323. }
  324. OpenGLState& OpenGLState::ResetPipeline(GLuint handle) {
  325. if (draw.program_pipeline == handle) {
  326. draw.program_pipeline = 0;
  327. }
  328. return *this;
  329. }
  330. OpenGLState& OpenGLState::ResetBuffer(GLuint handle) {
  331. if (draw.vertex_buffer == handle) {
  332. draw.vertex_buffer = 0;
  333. }
  334. if (draw.uniform_buffer == handle) {
  335. draw.uniform_buffer = 0;
  336. }
  337. return *this;
  338. }
  339. OpenGLState& OpenGLState::ResetVertexArray(GLuint handle) {
  340. if (draw.vertex_array == handle) {
  341. draw.vertex_array = 0;
  342. }
  343. return *this;
  344. }
  345. OpenGLState& OpenGLState::ResetFramebuffer(GLuint handle) {
  346. if (draw.read_framebuffer == handle) {
  347. draw.read_framebuffer = 0;
  348. }
  349. if (draw.draw_framebuffer == handle) {
  350. draw.draw_framebuffer = 0;
  351. }
  352. return *this;
  353. }