gl_state.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 enabled; // GL_CULL_FACE
  34. GLenum mode; // GL_CULL_FACE_MODE
  35. GLenum front_face; // GL_FRONT_FACE
  36. } cull;
  37. struct {
  38. bool test_enabled; // GL_DEPTH_TEST
  39. GLenum test_func; // GL_DEPTH_FUNC
  40. GLboolean write_mask; // GL_DEPTH_WRITEMASK
  41. } depth;
  42. struct {
  43. bool enabled;
  44. GLuint index;
  45. } primitive_restart; // GL_PRIMITIVE_RESTART
  46. struct ColorMask {
  47. GLboolean red_enabled;
  48. GLboolean green_enabled;
  49. GLboolean blue_enabled;
  50. GLboolean alpha_enabled;
  51. };
  52. std::array<ColorMask, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets>
  53. color_mask; // GL_COLOR_WRITEMASK
  54. struct {
  55. bool test_enabled; // GL_STENCIL_TEST
  56. struct {
  57. GLenum test_func; // GL_STENCIL_FUNC
  58. GLint test_ref; // GL_STENCIL_REF
  59. GLuint test_mask; // GL_STENCIL_VALUE_MASK
  60. GLuint write_mask; // GL_STENCIL_WRITEMASK
  61. GLenum action_stencil_fail; // GL_STENCIL_FAIL
  62. GLenum action_depth_fail; // GL_STENCIL_PASS_DEPTH_FAIL
  63. GLenum action_depth_pass; // GL_STENCIL_PASS_DEPTH_PASS
  64. } front, back;
  65. } stencil;
  66. struct Blend {
  67. bool enabled; // GL_BLEND
  68. bool separate_alpha; // Independent blend enabled
  69. GLenum rgb_equation; // GL_BLEND_EQUATION_RGB
  70. GLenum a_equation; // GL_BLEND_EQUATION_ALPHA
  71. GLenum src_rgb_func; // GL_BLEND_SRC_RGB
  72. GLenum dst_rgb_func; // GL_BLEND_DST_RGB
  73. GLenum src_a_func; // GL_BLEND_SRC_ALPHA
  74. GLenum dst_a_func; // GL_BLEND_DST_ALPHA
  75. };
  76. std::array<Blend, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets> blend;
  77. struct {
  78. bool enabled;
  79. } independant_blend;
  80. struct {
  81. GLclampf red;
  82. GLclampf green;
  83. GLclampf blue;
  84. GLclampf alpha;
  85. } blend_color; // GL_BLEND_COLOR
  86. struct {
  87. bool enabled; // GL_LOGIC_OP_MODE
  88. GLenum operation;
  89. } logic_op;
  90. // 3 texture units - one for each that is used in PICA fragment shader emulation
  91. struct TextureUnit {
  92. GLuint texture; // GL_TEXTURE_BINDING_2D
  93. GLuint sampler; // GL_SAMPLER_BINDING
  94. GLenum target;
  95. struct {
  96. GLint r; // GL_TEXTURE_SWIZZLE_R
  97. GLint g; // GL_TEXTURE_SWIZZLE_G
  98. GLint b; // GL_TEXTURE_SWIZZLE_B
  99. GLint a; // GL_TEXTURE_SWIZZLE_A
  100. } swizzle;
  101. void Unbind() {
  102. texture = 0;
  103. swizzle.r = GL_RED;
  104. swizzle.g = GL_GREEN;
  105. swizzle.b = GL_BLUE;
  106. swizzle.a = GL_ALPHA;
  107. }
  108. void Reset() {
  109. Unbind();
  110. sampler = 0;
  111. target = GL_TEXTURE_2D;
  112. }
  113. };
  114. std::array<TextureUnit, Tegra::Engines::Maxwell3D::Regs::NumTextureSamplers> texture_units;
  115. struct {
  116. GLuint read_framebuffer; // GL_READ_FRAMEBUFFER_BINDING
  117. GLuint draw_framebuffer; // GL_DRAW_FRAMEBUFFER_BINDING
  118. GLuint vertex_array; // GL_VERTEX_ARRAY_BINDING
  119. GLuint vertex_buffer; // GL_ARRAY_BUFFER_BINDING
  120. GLuint uniform_buffer; // GL_UNIFORM_BUFFER_BINDING
  121. GLuint shader_program; // GL_CURRENT_PROGRAM
  122. GLuint program_pipeline; // GL_PROGRAM_PIPELINE_BINDING
  123. } draw;
  124. struct viewport {
  125. GLfloat x;
  126. GLfloat y;
  127. GLfloat width;
  128. GLfloat height;
  129. GLfloat depth_range_near; // GL_DEPTH_RANGE
  130. GLfloat depth_range_far; // GL_DEPTH_RANGE
  131. };
  132. std::array<viewport, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets> viewports;
  133. struct {
  134. bool enabled; // GL_SCISSOR_TEST
  135. GLint x;
  136. GLint y;
  137. GLsizei width;
  138. GLsizei height;
  139. } scissor;
  140. struct {
  141. float size; // GL_POINT_SIZE
  142. } point;
  143. std::array<bool, 2> clip_distance; // GL_CLIP_DISTANCE
  144. OpenGLState();
  145. /// Get the currently active OpenGL state
  146. static OpenGLState GetCurState() {
  147. return cur_state;
  148. }
  149. static bool GetsRGBUsed() {
  150. return s_rgb_used;
  151. }
  152. static void ClearsRGBUsed() {
  153. s_rgb_used = false;
  154. }
  155. /// Apply this state as the current OpenGL state
  156. void Apply() const;
  157. /// Set the initial OpenGL state
  158. static void ApplyDefaultState();
  159. /// Resets any references to the given resource
  160. OpenGLState& UnbindTexture(GLuint handle);
  161. OpenGLState& ResetSampler(GLuint handle);
  162. OpenGLState& ResetProgram(GLuint handle);
  163. OpenGLState& ResetPipeline(GLuint handle);
  164. OpenGLState& ResetBuffer(GLuint handle);
  165. OpenGLState& ResetVertexArray(GLuint handle);
  166. OpenGLState& ResetFramebuffer(GLuint handle);
  167. private:
  168. static OpenGLState cur_state;
  169. // Workaround for sRGB problems caused by
  170. // QT not supporting srgb output
  171. static bool s_rgb_used;
  172. void ApplySRgb() const;
  173. void ApplyCulling() const;
  174. void ApplyColorMask() const;
  175. void ApplyDepth() const;
  176. void ApplyPrimitiveRestart() const;
  177. void ApplyStencilTest() const;
  178. void ApplyViewport() const;
  179. void ApplyTargetBlending(int target, bool force) const;
  180. void ApplyGlobalBlending() const;
  181. void ApplyBlending() const;
  182. void ApplyLogicOp() const;
  183. void ApplyTextures() const;
  184. void ApplySamplers() const;
  185. void ApplyScissor() const;
  186. };
  187. } // namespace OpenGL