gl_state.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <glad/glad.h>
  6. namespace TextureUnits {
  7. struct TextureUnit {
  8. GLint id;
  9. constexpr GLenum Enum() const {
  10. return static_cast<GLenum>(GL_TEXTURE0 + id);
  11. }
  12. };
  13. constexpr TextureUnit PicaTexture(int unit) {
  14. return TextureUnit{unit};
  15. }
  16. constexpr TextureUnit LightingLUT{3};
  17. constexpr TextureUnit FogLUT{4};
  18. constexpr TextureUnit ProcTexNoiseLUT{5};
  19. constexpr TextureUnit ProcTexColorMap{6};
  20. constexpr TextureUnit ProcTexAlphaMap{7};
  21. constexpr TextureUnit ProcTexLUT{8};
  22. constexpr TextureUnit ProcTexDiffLUT{9};
  23. } // namespace TextureUnits
  24. class OpenGLState {
  25. public:
  26. struct {
  27. bool enabled; // GL_CULL_FACE
  28. GLenum mode; // GL_CULL_FACE_MODE
  29. GLenum front_face; // GL_FRONT_FACE
  30. } cull;
  31. struct {
  32. bool test_enabled; // GL_DEPTH_TEST
  33. GLenum test_func; // GL_DEPTH_FUNC
  34. GLboolean write_mask; // GL_DEPTH_WRITEMASK
  35. } depth;
  36. struct {
  37. GLboolean red_enabled;
  38. GLboolean green_enabled;
  39. GLboolean blue_enabled;
  40. GLboolean alpha_enabled;
  41. } color_mask; // GL_COLOR_WRITEMASK
  42. struct {
  43. bool test_enabled; // GL_STENCIL_TEST
  44. GLenum test_func; // GL_STENCIL_FUNC
  45. GLint test_ref; // GL_STENCIL_REF
  46. GLuint test_mask; // GL_STENCIL_VALUE_MASK
  47. GLuint write_mask; // GL_STENCIL_WRITEMASK
  48. GLenum action_stencil_fail; // GL_STENCIL_FAIL
  49. GLenum action_depth_fail; // GL_STENCIL_PASS_DEPTH_FAIL
  50. GLenum action_depth_pass; // GL_STENCIL_PASS_DEPTH_PASS
  51. } stencil;
  52. struct {
  53. bool enabled; // GL_BLEND
  54. GLenum rgb_equation; // GL_BLEND_EQUATION_RGB
  55. GLenum a_equation; // GL_BLEND_EQUATION_ALPHA
  56. GLenum src_rgb_func; // GL_BLEND_SRC_RGB
  57. GLenum dst_rgb_func; // GL_BLEND_DST_RGB
  58. GLenum src_a_func; // GL_BLEND_SRC_ALPHA
  59. GLenum dst_a_func; // GL_BLEND_DST_ALPHA
  60. struct {
  61. GLclampf red;
  62. GLclampf green;
  63. GLclampf blue;
  64. GLclampf alpha;
  65. } color; // GL_BLEND_COLOR
  66. } blend;
  67. GLenum logic_op; // GL_LOGIC_OP_MODE
  68. // 3 texture units - one for each that is used in PICA fragment shader emulation
  69. struct {
  70. GLuint texture_2d; // GL_TEXTURE_BINDING_2D
  71. GLuint sampler; // GL_SAMPLER_BINDING
  72. } texture_units[3];
  73. struct {
  74. GLuint texture_buffer; // GL_TEXTURE_BINDING_BUFFER
  75. } lighting_lut;
  76. struct {
  77. GLuint texture_buffer; // GL_TEXTURE_BINDING_BUFFER
  78. } fog_lut;
  79. struct {
  80. GLuint texture_buffer; // GL_TEXTURE_BINDING_BUFFER
  81. } proctex_noise_lut;
  82. struct {
  83. GLuint texture_buffer; // GL_TEXTURE_BINDING_BUFFER
  84. } proctex_color_map;
  85. struct {
  86. GLuint texture_buffer; // GL_TEXTURE_BINDING_BUFFER
  87. } proctex_alpha_map;
  88. struct {
  89. GLuint texture_buffer; // GL_TEXTURE_BINDING_BUFFER
  90. } proctex_lut;
  91. struct {
  92. GLuint texture_buffer; // GL_TEXTURE_BINDING_BUFFER
  93. } proctex_diff_lut;
  94. struct {
  95. GLuint read_framebuffer; // GL_READ_FRAMEBUFFER_BINDING
  96. GLuint draw_framebuffer; // GL_DRAW_FRAMEBUFFER_BINDING
  97. GLuint vertex_array; // GL_VERTEX_ARRAY_BINDING
  98. GLuint vertex_buffer; // GL_ARRAY_BUFFER_BINDING
  99. GLuint uniform_buffer; // GL_UNIFORM_BUFFER_BINDING
  100. GLuint shader_program; // GL_CURRENT_PROGRAM
  101. } draw;
  102. OpenGLState();
  103. /// Get the currently active OpenGL state
  104. static const OpenGLState& GetCurState() {
  105. return cur_state;
  106. }
  107. /// Apply this state as the current OpenGL state
  108. void Apply() const;
  109. /// Resets and unbinds any references to the given resource in the current OpenGL state
  110. static void ResetTexture(GLuint handle);
  111. static void ResetSampler(GLuint handle);
  112. static void ResetProgram(GLuint handle);
  113. static void ResetBuffer(GLuint handle);
  114. static void ResetVertexArray(GLuint handle);
  115. static void ResetFramebuffer(GLuint handle);
  116. private:
  117. static OpenGLState cur_state;
  118. };