gl_state.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <glad/glad.h>
  7. #include "video_core/engines/maxwell_3d.h"
  8. namespace OpenGL {
  9. namespace TextureUnits {
  10. struct TextureUnit {
  11. GLint id;
  12. constexpr GLenum Enum() const {
  13. return static_cast<GLenum>(GL_TEXTURE0 + id);
  14. }
  15. };
  16. constexpr TextureUnit MaxwellTexture(int unit) {
  17. return TextureUnit{unit};
  18. }
  19. constexpr TextureUnit LightingLUT{3};
  20. constexpr TextureUnit FogLUT{4};
  21. constexpr TextureUnit ProcTexNoiseLUT{5};
  22. constexpr TextureUnit ProcTexColorMap{6};
  23. constexpr TextureUnit ProcTexAlphaMap{7};
  24. constexpr TextureUnit ProcTexLUT{8};
  25. constexpr TextureUnit ProcTexDiffLUT{9};
  26. } // namespace TextureUnits
  27. class OpenGLState {
  28. public:
  29. struct {
  30. bool enabled; // GL_FRAMEBUFFER_SRGB
  31. } framebuffer_srgb;
  32. struct {
  33. bool alpha_to_coverage; // GL_ALPHA_TO_COVERAGE
  34. bool alpha_to_one; // GL_ALPHA_TO_ONE
  35. } multisample_control;
  36. struct {
  37. bool enabled; // GL_CLAMP_FRAGMENT_COLOR_ARB
  38. } fragment_color_clamp;
  39. struct {
  40. bool far_plane;
  41. bool near_plane;
  42. } depth_clamp; // GL_DEPTH_CLAMP
  43. struct {
  44. bool enabled; // viewports arrays are only supported when geometry shaders are enabled.
  45. } geometry_shaders;
  46. struct {
  47. bool enabled; // GL_CULL_FACE
  48. GLenum mode; // GL_CULL_FACE_MODE
  49. GLenum front_face; // GL_FRONT_FACE
  50. } cull;
  51. struct {
  52. bool test_enabled; // GL_DEPTH_TEST
  53. GLenum test_func; // GL_DEPTH_FUNC
  54. GLboolean write_mask; // GL_DEPTH_WRITEMASK
  55. } depth;
  56. struct {
  57. bool enabled;
  58. GLuint index;
  59. } primitive_restart; // GL_PRIMITIVE_RESTART
  60. struct ColorMask {
  61. GLboolean red_enabled;
  62. GLboolean green_enabled;
  63. GLboolean blue_enabled;
  64. GLboolean alpha_enabled;
  65. };
  66. std::array<ColorMask, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets>
  67. color_mask; // GL_COLOR_WRITEMASK
  68. struct {
  69. bool test_enabled; // GL_STENCIL_TEST
  70. struct {
  71. GLenum test_func; // GL_STENCIL_FUNC
  72. GLint test_ref; // GL_STENCIL_REF
  73. GLuint test_mask; // GL_STENCIL_VALUE_MASK
  74. GLuint write_mask; // GL_STENCIL_WRITEMASK
  75. GLenum action_stencil_fail; // GL_STENCIL_FAIL
  76. GLenum action_depth_fail; // GL_STENCIL_PASS_DEPTH_FAIL
  77. GLenum action_depth_pass; // GL_STENCIL_PASS_DEPTH_PASS
  78. } front, back;
  79. } stencil;
  80. struct Blend {
  81. bool enabled; // GL_BLEND
  82. GLenum rgb_equation; // GL_BLEND_EQUATION_RGB
  83. GLenum a_equation; // GL_BLEND_EQUATION_ALPHA
  84. GLenum src_rgb_func; // GL_BLEND_SRC_RGB
  85. GLenum dst_rgb_func; // GL_BLEND_DST_RGB
  86. GLenum src_a_func; // GL_BLEND_SRC_ALPHA
  87. GLenum dst_a_func; // GL_BLEND_DST_ALPHA
  88. };
  89. std::array<Blend, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets> blend;
  90. struct {
  91. bool enabled;
  92. } independant_blend;
  93. struct {
  94. GLclampf red;
  95. GLclampf green;
  96. GLclampf blue;
  97. GLclampf alpha;
  98. } blend_color; // GL_BLEND_COLOR
  99. struct {
  100. bool enabled; // GL_LOGIC_OP_MODE
  101. GLenum operation;
  102. } logic_op;
  103. // 3 texture units - one for each that is used in PICA fragment shader emulation
  104. struct TextureUnit {
  105. GLuint texture; // GL_TEXTURE_BINDING_2D
  106. GLuint sampler; // GL_SAMPLER_BINDING
  107. GLenum target;
  108. struct {
  109. GLint r; // GL_TEXTURE_SWIZZLE_R
  110. GLint g; // GL_TEXTURE_SWIZZLE_G
  111. GLint b; // GL_TEXTURE_SWIZZLE_B
  112. GLint a; // GL_TEXTURE_SWIZZLE_A
  113. } swizzle;
  114. void Unbind() {
  115. texture = 0;
  116. swizzle.r = GL_RED;
  117. swizzle.g = GL_GREEN;
  118. swizzle.b = GL_BLUE;
  119. swizzle.a = GL_ALPHA;
  120. }
  121. void Reset() {
  122. Unbind();
  123. sampler = 0;
  124. target = GL_TEXTURE_2D;
  125. }
  126. };
  127. std::array<TextureUnit, Tegra::Engines::Maxwell3D::Regs::NumTextureSamplers> texture_units;
  128. struct {
  129. GLuint read_framebuffer; // GL_READ_FRAMEBUFFER_BINDING
  130. GLuint draw_framebuffer; // GL_DRAW_FRAMEBUFFER_BINDING
  131. GLuint vertex_array; // GL_VERTEX_ARRAY_BINDING
  132. GLuint shader_program; // GL_CURRENT_PROGRAM
  133. GLuint program_pipeline; // GL_PROGRAM_PIPELINE_BINDING
  134. } draw;
  135. struct viewport {
  136. GLint x;
  137. GLint y;
  138. GLint width;
  139. GLint height;
  140. GLfloat depth_range_near; // GL_DEPTH_RANGE
  141. GLfloat depth_range_far; // GL_DEPTH_RANGE
  142. struct {
  143. bool enabled; // GL_SCISSOR_TEST
  144. GLint x;
  145. GLint y;
  146. GLsizei width;
  147. GLsizei height;
  148. } scissor;
  149. };
  150. std::array<viewport, Tegra::Engines::Maxwell3D::Regs::NumViewports> viewports;
  151. struct {
  152. float size; // GL_POINT_SIZE
  153. } point;
  154. struct {
  155. bool point_enable;
  156. bool line_enable;
  157. bool fill_enable;
  158. GLfloat units;
  159. GLfloat factor;
  160. GLfloat clamp;
  161. } polygon_offset;
  162. std::array<bool, 8> clip_distance; // GL_CLIP_DISTANCE
  163. OpenGLState();
  164. /// Get the currently active OpenGL state
  165. static OpenGLState GetCurState() {
  166. return cur_state;
  167. }
  168. static bool GetsRGBUsed() {
  169. return s_rgb_used;
  170. }
  171. static void ClearsRGBUsed() {
  172. s_rgb_used = false;
  173. }
  174. /// Apply this state as the current OpenGL state
  175. void Apply() const;
  176. /// Apply only the state affecting the framebuffer
  177. void ApplyFramebufferState() const;
  178. /// Apply only the state affecting the vertex array
  179. void ApplyVertexArrayState() const;
  180. /// Set the initial OpenGL state
  181. static void ApplyDefaultState();
  182. /// Resets any references to the given resource
  183. OpenGLState& UnbindTexture(GLuint handle);
  184. OpenGLState& ResetSampler(GLuint handle);
  185. OpenGLState& ResetProgram(GLuint handle);
  186. OpenGLState& ResetPipeline(GLuint handle);
  187. OpenGLState& ResetVertexArray(GLuint handle);
  188. OpenGLState& ResetFramebuffer(GLuint handle);
  189. void EmulateViewportWithScissor();
  190. private:
  191. static OpenGLState cur_state;
  192. // Workaround for sRGB problems caused by
  193. // QT not supporting srgb output
  194. static bool s_rgb_used;
  195. void ApplySRgb() const;
  196. void ApplyCulling() const;
  197. void ApplyColorMask() const;
  198. void ApplyDepth() const;
  199. void ApplyPrimitiveRestart() const;
  200. void ApplyStencilTest() const;
  201. void ApplyViewport() const;
  202. void ApplyTargetBlending(std::size_t target, bool force) const;
  203. void ApplyGlobalBlending() const;
  204. void ApplyBlending() const;
  205. void ApplyLogicOp() const;
  206. void ApplyTextures() const;
  207. void ApplySamplers() const;
  208. void ApplyDepthClamp() const;
  209. void ApplyPolygonOffset() const;
  210. };
  211. } // namespace OpenGL