gl_state.h 4.7 KB

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