gl_state.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <glad/glad.h>
  7. #include "common/assert.h"
  8. #include "common/logging/log.h"
  9. #include "common/microprofile.h"
  10. #include "video_core/renderer_opengl/gl_state.h"
  11. MICROPROFILE_DEFINE(OpenGL_State, "OpenGL", "State Change", MP_RGB(192, 128, 128));
  12. namespace OpenGL {
  13. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  14. OpenGLState OpenGLState::cur_state;
  15. namespace {
  16. template <typename T>
  17. bool UpdateValue(T& current_value, const T new_value) {
  18. const bool changed = current_value != new_value;
  19. current_value = new_value;
  20. return changed;
  21. }
  22. template <typename T1, typename T2>
  23. bool UpdateTie(T1 current_value, const T2 new_value) {
  24. const bool changed = current_value != new_value;
  25. current_value = new_value;
  26. return changed;
  27. }
  28. template <typename T>
  29. std::optional<std::pair<GLuint, GLsizei>> UpdateArray(T& current_values, const T& new_values) {
  30. std::optional<std::size_t> first;
  31. std::size_t last;
  32. for (std::size_t i = 0; i < std::size(current_values); ++i) {
  33. if (!UpdateValue(current_values[i], new_values[i])) {
  34. continue;
  35. }
  36. if (!first) {
  37. first = i;
  38. }
  39. last = i;
  40. }
  41. if (!first) {
  42. return std::nullopt;
  43. }
  44. return std::make_pair(static_cast<GLuint>(*first), static_cast<GLsizei>(last - *first + 1));
  45. }
  46. void Enable(GLenum cap, bool enable) {
  47. if (enable) {
  48. glEnable(cap);
  49. } else {
  50. glDisable(cap);
  51. }
  52. }
  53. void Enable(GLenum cap, GLuint index, bool enable) {
  54. if (enable) {
  55. glEnablei(cap, index);
  56. } else {
  57. glDisablei(cap, index);
  58. }
  59. }
  60. void Enable(GLenum cap, bool& current_value, bool new_value) {
  61. if (UpdateValue(current_value, new_value)) {
  62. Enable(cap, new_value);
  63. }
  64. }
  65. void Enable(GLenum cap, GLuint index, bool& current_value, bool new_value) {
  66. if (UpdateValue(current_value, new_value)) {
  67. Enable(cap, index, new_value);
  68. }
  69. }
  70. } // Anonymous namespace
  71. OpenGLState::OpenGLState() = default;
  72. void OpenGLState::SetDefaultViewports() {
  73. viewports.fill(Viewport{});
  74. }
  75. void OpenGLState::ApplyFramebufferState() {
  76. if (UpdateValue(cur_state.draw.read_framebuffer, draw.read_framebuffer)) {
  77. glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
  78. }
  79. if (UpdateValue(cur_state.draw.draw_framebuffer, draw.draw_framebuffer)) {
  80. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, draw.draw_framebuffer);
  81. }
  82. }
  83. void OpenGLState::ApplyShaderProgram() {
  84. if (UpdateValue(cur_state.draw.shader_program, draw.shader_program)) {
  85. glUseProgram(draw.shader_program);
  86. }
  87. }
  88. void OpenGLState::ApplyProgramPipeline() {
  89. if (UpdateValue(cur_state.draw.program_pipeline, draw.program_pipeline)) {
  90. glBindProgramPipeline(draw.program_pipeline);
  91. }
  92. }
  93. void OpenGLState::ApplyClipDistances() {
  94. for (std::size_t i = 0; i < clip_distance.size(); ++i) {
  95. Enable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i), cur_state.clip_distance[i],
  96. clip_distance[i]);
  97. }
  98. }
  99. void OpenGLState::ApplyRasterizerDiscard() {
  100. Enable(GL_RASTERIZER_DISCARD, cur_state.rasterizer_discard, rasterizer_discard);
  101. }
  102. void OpenGLState::ApplyStencilTest() {
  103. Enable(GL_STENCIL_TEST, cur_state.stencil.test_enabled, stencil.test_enabled);
  104. const auto ConfigStencil = [](GLenum face, const auto& config, auto& current) {
  105. if (current.test_func != config.test_func || current.test_ref != config.test_ref ||
  106. current.test_mask != config.test_mask) {
  107. current.test_func = config.test_func;
  108. current.test_ref = config.test_ref;
  109. current.test_mask = config.test_mask;
  110. glStencilFuncSeparate(face, config.test_func, config.test_ref, config.test_mask);
  111. }
  112. if (current.action_depth_fail != config.action_depth_fail ||
  113. current.action_depth_pass != config.action_depth_pass ||
  114. current.action_stencil_fail != config.action_stencil_fail) {
  115. current.action_depth_fail = config.action_depth_fail;
  116. current.action_depth_pass = config.action_depth_pass;
  117. current.action_stencil_fail = config.action_stencil_fail;
  118. glStencilOpSeparate(face, config.action_stencil_fail, config.action_depth_fail,
  119. config.action_depth_pass);
  120. }
  121. if (current.write_mask != config.write_mask) {
  122. current.write_mask = config.write_mask;
  123. glStencilMaskSeparate(face, config.write_mask);
  124. }
  125. };
  126. ConfigStencil(GL_FRONT, stencil.front, cur_state.stencil.front);
  127. ConfigStencil(GL_BACK, stencil.back, cur_state.stencil.back);
  128. }
  129. void OpenGLState::ApplyViewport() {
  130. for (GLuint i = 0; i < static_cast<GLuint>(Maxwell::NumViewports); ++i) {
  131. const auto& updated = viewports[i];
  132. auto& current = cur_state.viewports[i];
  133. if (current.x != updated.x || current.y != updated.y || current.width != updated.width ||
  134. current.height != updated.height) {
  135. current.x = updated.x;
  136. current.y = updated.y;
  137. current.width = updated.width;
  138. current.height = updated.height;
  139. glViewportIndexedf(i, static_cast<GLfloat>(updated.x), static_cast<GLfloat>(updated.y),
  140. static_cast<GLfloat>(updated.width),
  141. static_cast<GLfloat>(updated.height));
  142. }
  143. if (current.depth_range_near != updated.depth_range_near ||
  144. current.depth_range_far != updated.depth_range_far) {
  145. current.depth_range_near = updated.depth_range_near;
  146. current.depth_range_far = updated.depth_range_far;
  147. glDepthRangeIndexed(i, updated.depth_range_near, updated.depth_range_far);
  148. }
  149. }
  150. }
  151. void OpenGLState::ApplyGlobalBlending() {
  152. const Blend& updated = blend[0];
  153. Blend& current = cur_state.blend[0];
  154. Enable(GL_BLEND, current.enabled, updated.enabled);
  155. if (current.src_rgb_func != updated.src_rgb_func ||
  156. current.dst_rgb_func != updated.dst_rgb_func || current.src_a_func != updated.src_a_func ||
  157. current.dst_a_func != updated.dst_a_func) {
  158. current.src_rgb_func = updated.src_rgb_func;
  159. current.dst_rgb_func = updated.dst_rgb_func;
  160. current.src_a_func = updated.src_a_func;
  161. current.dst_a_func = updated.dst_a_func;
  162. glBlendFuncSeparate(updated.src_rgb_func, updated.dst_rgb_func, updated.src_a_func,
  163. updated.dst_a_func);
  164. }
  165. if (current.rgb_equation != updated.rgb_equation || current.a_equation != updated.a_equation) {
  166. current.rgb_equation = updated.rgb_equation;
  167. current.a_equation = updated.a_equation;
  168. glBlendEquationSeparate(updated.rgb_equation, updated.a_equation);
  169. }
  170. }
  171. void OpenGLState::ApplyTargetBlending(std::size_t target, bool force) {
  172. const Blend& updated = blend[target];
  173. Blend& current = cur_state.blend[target];
  174. if (current.enabled != updated.enabled || force) {
  175. current.enabled = updated.enabled;
  176. Enable(GL_BLEND, static_cast<GLuint>(target), updated.enabled);
  177. }
  178. if (UpdateTie(std::tie(current.src_rgb_func, current.dst_rgb_func, current.src_a_func,
  179. current.dst_a_func),
  180. std::tie(updated.src_rgb_func, updated.dst_rgb_func, updated.src_a_func,
  181. updated.dst_a_func))) {
  182. glBlendFuncSeparatei(static_cast<GLuint>(target), updated.src_rgb_func,
  183. updated.dst_rgb_func, updated.src_a_func, updated.dst_a_func);
  184. }
  185. if (UpdateTie(std::tie(current.rgb_equation, current.a_equation),
  186. std::tie(updated.rgb_equation, updated.a_equation))) {
  187. glBlendEquationSeparatei(static_cast<GLuint>(target), updated.rgb_equation,
  188. updated.a_equation);
  189. }
  190. }
  191. void OpenGLState::ApplyBlending() {
  192. if (independant_blend.enabled) {
  193. const bool force = independant_blend.enabled != cur_state.independant_blend.enabled;
  194. for (std::size_t target = 0; target < Maxwell::NumRenderTargets; ++target) {
  195. ApplyTargetBlending(target, force);
  196. }
  197. } else {
  198. ApplyGlobalBlending();
  199. }
  200. cur_state.independant_blend.enabled = independant_blend.enabled;
  201. }
  202. void OpenGLState::ApplyClipControl() {
  203. if (UpdateTie(std::tie(cur_state.clip_control.origin, cur_state.clip_control.depth_mode),
  204. std::tie(clip_control.origin, clip_control.depth_mode))) {
  205. glClipControl(clip_control.origin, clip_control.depth_mode);
  206. }
  207. }
  208. void OpenGLState::ApplyRenderBuffer() {
  209. if (cur_state.renderbuffer != renderbuffer) {
  210. cur_state.renderbuffer = renderbuffer;
  211. glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
  212. }
  213. }
  214. void OpenGLState::ApplyTextures() {
  215. const std::size_t size = std::size(textures);
  216. for (std::size_t i = 0; i < size; ++i) {
  217. if (UpdateValue(cur_state.textures[i], textures[i])) {
  218. // BindTextureUnit doesn't support binding null textures, skip those binds.
  219. // TODO(Rodrigo): Stop using null textures
  220. if (textures[i] != 0) {
  221. glBindTextureUnit(static_cast<GLuint>(i), textures[i]);
  222. }
  223. }
  224. }
  225. }
  226. void OpenGLState::ApplySamplers() {
  227. const std::size_t size = std::size(samplers);
  228. for (std::size_t i = 0; i < size; ++i) {
  229. if (UpdateValue(cur_state.samplers[i], samplers[i])) {
  230. glBindSampler(static_cast<GLuint>(i), samplers[i]);
  231. }
  232. }
  233. }
  234. void OpenGLState::ApplyImages() {
  235. if (const auto update = UpdateArray(cur_state.images, images)) {
  236. glBindImageTextures(update->first, update->second, images.data() + update->first);
  237. }
  238. }
  239. void OpenGLState::Apply() {
  240. MICROPROFILE_SCOPE(OpenGL_State);
  241. ApplyFramebufferState();
  242. ApplyShaderProgram();
  243. ApplyProgramPipeline();
  244. ApplyClipDistances();
  245. ApplyRasterizerDiscard();
  246. ApplyViewport();
  247. ApplyStencilTest();
  248. ApplyBlending();
  249. ApplyTextures();
  250. ApplySamplers();
  251. ApplyImages();
  252. ApplyClipControl();
  253. ApplyRenderBuffer();
  254. }
  255. OpenGLState& OpenGLState::UnbindTexture(GLuint handle) {
  256. for (auto& texture : textures) {
  257. if (texture == handle) {
  258. texture = 0;
  259. }
  260. }
  261. return *this;
  262. }
  263. OpenGLState& OpenGLState::ResetSampler(GLuint handle) {
  264. for (auto& sampler : samplers) {
  265. if (sampler == handle) {
  266. sampler = 0;
  267. }
  268. }
  269. return *this;
  270. }
  271. OpenGLState& OpenGLState::ResetProgram(GLuint handle) {
  272. if (draw.shader_program == handle) {
  273. draw.shader_program = 0;
  274. }
  275. return *this;
  276. }
  277. OpenGLState& OpenGLState::ResetPipeline(GLuint handle) {
  278. if (draw.program_pipeline == handle) {
  279. draw.program_pipeline = 0;
  280. }
  281. return *this;
  282. }
  283. OpenGLState& OpenGLState::ResetFramebuffer(GLuint handle) {
  284. if (draw.read_framebuffer == handle) {
  285. draw.read_framebuffer = 0;
  286. }
  287. if (draw.draw_framebuffer == handle) {
  288. draw.draw_framebuffer = 0;
  289. }
  290. return *this;
  291. }
  292. OpenGLState& OpenGLState::ResetRenderbuffer(GLuint handle) {
  293. if (renderbuffer == handle) {
  294. renderbuffer = 0;
  295. }
  296. return *this;
  297. }
  298. } // namespace OpenGL