gl_state.h 6.6 KB

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