gl_state.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. using Regs = Tegra::Engines::Maxwell3D::Regs;
  10. namespace TextureUnits {
  11. struct TextureUnit {
  12. GLint id;
  13. constexpr GLenum Enum() const {
  14. return static_cast<GLenum>(GL_TEXTURE0 + id);
  15. }
  16. };
  17. constexpr TextureUnit MaxwellTexture(int unit) {
  18. return TextureUnit{unit};
  19. }
  20. constexpr TextureUnit LightingLUT{3};
  21. constexpr TextureUnit FogLUT{4};
  22. constexpr TextureUnit ProcTexNoiseLUT{5};
  23. constexpr TextureUnit ProcTexColorMap{6};
  24. constexpr TextureUnit ProcTexAlphaMap{7};
  25. constexpr TextureUnit ProcTexLUT{8};
  26. constexpr TextureUnit ProcTexDiffLUT{9};
  27. } // namespace TextureUnits
  28. class OpenGLState {
  29. public:
  30. struct {
  31. bool enabled; // GL_CULL_FACE
  32. GLenum mode; // GL_CULL_FACE_MODE
  33. GLenum front_face; // GL_FRONT_FACE
  34. } cull;
  35. struct {
  36. bool test_enabled; // GL_DEPTH_TEST
  37. GLenum test_func; // GL_DEPTH_FUNC
  38. GLboolean write_mask; // GL_DEPTH_WRITEMASK
  39. } depth;
  40. struct {
  41. GLboolean red_enabled;
  42. GLboolean green_enabled;
  43. GLboolean blue_enabled;
  44. GLboolean alpha_enabled;
  45. } color_mask; // GL_COLOR_WRITEMASK
  46. struct {
  47. bool test_enabled; // GL_STENCIL_TEST
  48. struct {
  49. GLenum test_func; // GL_STENCIL_FUNC
  50. GLint test_ref; // GL_STENCIL_REF
  51. GLuint test_mask; // GL_STENCIL_VALUE_MASK
  52. GLuint write_mask; // GL_STENCIL_WRITEMASK
  53. GLenum action_stencil_fail; // GL_STENCIL_FAIL
  54. GLenum action_depth_fail; // GL_STENCIL_PASS_DEPTH_FAIL
  55. GLenum action_depth_pass; // GL_STENCIL_PASS_DEPTH_PASS
  56. } front, back;
  57. } stencil;
  58. struct {
  59. bool enabled; // GL_BLEND
  60. GLenum rgb_equation; // GL_BLEND_EQUATION_RGB
  61. GLenum a_equation; // GL_BLEND_EQUATION_ALPHA
  62. GLenum src_rgb_func; // GL_BLEND_SRC_RGB
  63. GLenum dst_rgb_func; // GL_BLEND_DST_RGB
  64. GLenum src_a_func; // GL_BLEND_SRC_ALPHA
  65. GLenum dst_a_func; // GL_BLEND_DST_ALPHA
  66. struct {
  67. GLclampf red;
  68. GLclampf green;
  69. GLclampf blue;
  70. GLclampf alpha;
  71. } color; // GL_BLEND_COLOR
  72. } blend;
  73. struct {
  74. bool enabled; // GL_LOGIC_OP_MODE
  75. GLenum operation;
  76. } logic_op;
  77. // 3 texture units - one for each that is used in PICA fragment shader emulation
  78. struct TextureUnit {
  79. GLuint texture; // GL_TEXTURE_BINDING_2D
  80. GLuint sampler; // GL_SAMPLER_BINDING
  81. GLenum target;
  82. struct {
  83. GLint r; // GL_TEXTURE_SWIZZLE_R
  84. GLint g; // GL_TEXTURE_SWIZZLE_G
  85. GLint b; // GL_TEXTURE_SWIZZLE_B
  86. GLint a; // GL_TEXTURE_SWIZZLE_A
  87. } swizzle;
  88. void Unbind() {
  89. texture = 0;
  90. swizzle.r = GL_RED;
  91. swizzle.g = GL_GREEN;
  92. swizzle.b = GL_BLUE;
  93. swizzle.a = GL_ALPHA;
  94. }
  95. void Reset() {
  96. Unbind();
  97. sampler = 0;
  98. target = GL_TEXTURE_2D;
  99. }
  100. };
  101. std::array<TextureUnit, 32> texture_units;
  102. struct {
  103. GLuint read_framebuffer; // GL_READ_FRAMEBUFFER_BINDING
  104. GLuint draw_framebuffer; // GL_DRAW_FRAMEBUFFER_BINDING
  105. GLuint vertex_array; // GL_VERTEX_ARRAY_BINDING
  106. GLuint vertex_buffer; // GL_ARRAY_BUFFER_BINDING
  107. GLuint uniform_buffer; // GL_UNIFORM_BUFFER_BINDING
  108. GLuint shader_program; // GL_CURRENT_PROGRAM
  109. GLuint program_pipeline; // GL_PROGRAM_PIPELINE_BINDING
  110. } draw;
  111. struct {
  112. bool enabled; // GL_SCISSOR_TEST
  113. GLint x;
  114. GLint y;
  115. GLsizei width;
  116. GLsizei height;
  117. } scissor;
  118. struct {
  119. GLint x;
  120. GLint y;
  121. GLsizei width;
  122. GLsizei height;
  123. } viewport;
  124. std::array<bool, 2> clip_distance; // GL_CLIP_DISTANCE
  125. OpenGLState();
  126. /// Get the currently active OpenGL state
  127. static OpenGLState GetCurState() {
  128. return cur_state;
  129. }
  130. /// Apply this state as the current OpenGL state
  131. void Apply() const;
  132. /// Resets any references to the given resource
  133. OpenGLState& UnbindTexture(GLuint handle);
  134. OpenGLState& ResetSampler(GLuint handle);
  135. OpenGLState& ResetProgram(GLuint handle);
  136. OpenGLState& ResetPipeline(GLuint handle);
  137. OpenGLState& ResetBuffer(GLuint handle);
  138. OpenGLState& ResetVertexArray(GLuint handle);
  139. OpenGLState& ResetFramebuffer(GLuint handle);
  140. private:
  141. static OpenGLState cur_state;
  142. };
  143. } // namespace OpenGL