gl_state.h 5.0 KB

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