gl_state.h 4.6 KB

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