gl_state.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <type_traits>
  7. #include <glad/glad.h>
  8. #include "video_core/engines/maxwell_3d.h"
  9. namespace OpenGL {
  10. class OpenGLState {
  11. public:
  12. struct {
  13. bool enabled = false; // GL_FRAMEBUFFER_SRGB
  14. } framebuffer_srgb;
  15. struct {
  16. bool alpha_to_coverage = false; // GL_ALPHA_TO_COVERAGE
  17. bool alpha_to_one = false; // GL_ALPHA_TO_ONE
  18. } multisample_control;
  19. struct {
  20. bool enabled = false; // GL_CLAMP_FRAGMENT_COLOR_ARB
  21. } fragment_color_clamp;
  22. struct {
  23. bool far_plane = false;
  24. bool near_plane = false;
  25. } depth_clamp; // GL_DEPTH_CLAMP
  26. struct {
  27. bool test_enabled = false; // GL_DEPTH_TEST
  28. GLboolean write_mask = GL_TRUE; // GL_DEPTH_WRITEMASK
  29. GLenum test_func = GL_LESS; // GL_DEPTH_FUNC
  30. } depth;
  31. struct {
  32. bool enabled = false;
  33. GLuint index = 0;
  34. } primitive_restart; // GL_PRIMITIVE_RESTART
  35. bool rasterizer_discard = false; // GL_RASTERIZER_DISCARD
  36. struct ColorMask {
  37. GLboolean red_enabled = GL_TRUE;
  38. GLboolean green_enabled = GL_TRUE;
  39. GLboolean blue_enabled = GL_TRUE;
  40. GLboolean alpha_enabled = GL_TRUE;
  41. };
  42. std::array<ColorMask, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets>
  43. color_mask; // GL_COLOR_WRITEMASK
  44. struct {
  45. bool test_enabled = false; // GL_STENCIL_TEST
  46. struct {
  47. GLenum test_func = GL_ALWAYS; // GL_STENCIL_FUNC
  48. GLint test_ref = 0; // GL_STENCIL_REF
  49. GLuint test_mask = 0xFFFFFFFF; // GL_STENCIL_VALUE_MASK
  50. GLuint write_mask = 0xFFFFFFFF; // GL_STENCIL_WRITEMASK
  51. GLenum action_stencil_fail = GL_KEEP; // GL_STENCIL_FAIL
  52. GLenum action_depth_fail = GL_KEEP; // GL_STENCIL_PASS_DEPTH_FAIL
  53. GLenum action_depth_pass = GL_KEEP; // GL_STENCIL_PASS_DEPTH_PASS
  54. } front, back;
  55. } stencil;
  56. struct Blend {
  57. bool enabled = false; // GL_BLEND
  58. GLenum rgb_equation = GL_FUNC_ADD; // GL_BLEND_EQUATION_RGB
  59. GLenum a_equation = GL_FUNC_ADD; // GL_BLEND_EQUATION_ALPHA
  60. GLenum src_rgb_func = GL_ONE; // GL_BLEND_SRC_RGB
  61. GLenum dst_rgb_func = GL_ZERO; // GL_BLEND_DST_RGB
  62. GLenum src_a_func = GL_ONE; // GL_BLEND_SRC_ALPHA
  63. GLenum dst_a_func = GL_ZERO; // GL_BLEND_DST_ALPHA
  64. };
  65. std::array<Blend, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets> blend;
  66. struct {
  67. bool enabled = false;
  68. } independant_blend;
  69. struct {
  70. GLclampf red = 0.0f;
  71. GLclampf green = 0.0f;
  72. GLclampf blue = 0.0f;
  73. GLclampf alpha = 0.0f;
  74. } blend_color; // GL_BLEND_COLOR
  75. struct {
  76. bool enabled = false; // GL_LOGIC_OP_MODE
  77. GLenum operation = GL_COPY;
  78. } logic_op;
  79. static constexpr std::size_t NumSamplers = 32 * 5;
  80. static constexpr std::size_t NumImages = 8 * 5;
  81. std::array<GLuint, NumSamplers> textures = {};
  82. std::array<GLuint, NumSamplers> samplers = {};
  83. std::array<GLuint, NumImages> images = {};
  84. struct {
  85. GLuint read_framebuffer = 0; // GL_READ_FRAMEBUFFER_BINDING
  86. GLuint draw_framebuffer = 0; // GL_DRAW_FRAMEBUFFER_BINDING
  87. GLuint vertex_array = 0; // GL_VERTEX_ARRAY_BINDING
  88. GLuint shader_program = 0; // GL_CURRENT_PROGRAM
  89. GLuint program_pipeline = 0; // GL_PROGRAM_PIPELINE_BINDING
  90. } draw;
  91. struct Viewport {
  92. GLint x = 0;
  93. GLint y = 0;
  94. GLint width = 0;
  95. GLint height = 0;
  96. GLfloat depth_range_near = 0.0f; // GL_DEPTH_RANGE
  97. GLfloat depth_range_far = 1.0f; // GL_DEPTH_RANGE
  98. struct {
  99. bool enabled = false; // GL_SCISSOR_TEST
  100. GLint x = 0;
  101. GLint y = 0;
  102. GLsizei width = 0;
  103. GLsizei height = 0;
  104. } scissor;
  105. };
  106. std::array<Viewport, Tegra::Engines::Maxwell3D::Regs::NumViewports> viewports;
  107. struct {
  108. bool point_enable = false;
  109. bool line_enable = false;
  110. bool fill_enable = false;
  111. GLfloat units = 0.0f;
  112. GLfloat factor = 0.0f;
  113. GLfloat clamp = 0.0f;
  114. } polygon_offset;
  115. struct {
  116. bool enabled = false; // GL_ALPHA_TEST
  117. GLenum func = GL_ALWAYS; // GL_ALPHA_TEST_FUNC
  118. GLfloat ref = 0.0f; // GL_ALPHA_TEST_REF
  119. } alpha_test;
  120. std::array<bool, 8> clip_distance = {}; // GL_CLIP_DISTANCE
  121. struct {
  122. GLenum origin = GL_LOWER_LEFT;
  123. GLenum depth_mode = GL_NEGATIVE_ONE_TO_ONE;
  124. } clip_control;
  125. GLuint renderbuffer{}; // GL_RENDERBUFFER_BINDING
  126. OpenGLState();
  127. /// Get the currently active OpenGL state
  128. static OpenGLState GetCurState() {
  129. return cur_state;
  130. }
  131. void SetDefaultViewports();
  132. /// Apply this state as the current OpenGL state
  133. void Apply();
  134. void ApplyFramebufferState();
  135. void ApplyVertexArrayState();
  136. void ApplyShaderProgram();
  137. void ApplyProgramPipeline();
  138. void ApplyClipDistances();
  139. void ApplyFragmentColorClamp();
  140. void ApplyMultisample();
  141. void ApplySRgb();
  142. void ApplyRasterizerDiscard();
  143. void ApplyColorMask();
  144. void ApplyDepth();
  145. void ApplyPrimitiveRestart();
  146. void ApplyStencilTest();
  147. void ApplyViewport();
  148. void ApplyTargetBlending(std::size_t target, bool force);
  149. void ApplyGlobalBlending();
  150. void ApplyBlending();
  151. void ApplyLogicOp();
  152. void ApplyTextures();
  153. void ApplySamplers();
  154. void ApplyImages();
  155. void ApplyDepthClamp();
  156. void ApplyPolygonOffset();
  157. void ApplyAlphaTest();
  158. void ApplyClipControl();
  159. void ApplyRenderBuffer();
  160. /// Resets any references to the given resource
  161. OpenGLState& UnbindTexture(GLuint handle);
  162. OpenGLState& ResetSampler(GLuint handle);
  163. OpenGLState& ResetProgram(GLuint handle);
  164. OpenGLState& ResetPipeline(GLuint handle);
  165. OpenGLState& ResetVertexArray(GLuint handle);
  166. OpenGLState& ResetFramebuffer(GLuint handle);
  167. OpenGLState& ResetRenderbuffer(GLuint handle);
  168. /// Viewport does not affects glClearBuffer so emulate viewport using scissor test
  169. void EmulateViewportWithScissor();
  170. private:
  171. static OpenGLState cur_state;
  172. };
  173. static_assert(std::is_trivially_copyable_v<OpenGLState>);
  174. } // namespace OpenGL