gl_state.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <type_traits>
  7. #include <glad/glad.h>
  8. #include "video_core/engines/maxwell_3d.h"
  9. namespace OpenGL {
  10. class OpenGLState {
  11. public:
  12. struct {
  13. bool enabled = false; // GL_FRAMEBUFFER_SRGB
  14. } framebuffer_srgb;
  15. struct {
  16. bool alpha_to_coverage = false; // GL_ALPHA_TO_COVERAGE
  17. bool alpha_to_one = false; // GL_ALPHA_TO_ONE
  18. } multisample_control;
  19. struct {
  20. bool enabled = false; // GL_CLAMP_FRAGMENT_COLOR_ARB
  21. } fragment_color_clamp;
  22. bool rasterizer_discard = false; // GL_RASTERIZER_DISCARD
  23. struct ColorMask {
  24. GLboolean red_enabled = GL_TRUE;
  25. GLboolean green_enabled = GL_TRUE;
  26. GLboolean blue_enabled = GL_TRUE;
  27. GLboolean alpha_enabled = GL_TRUE;
  28. };
  29. std::array<ColorMask, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets>
  30. color_mask; // GL_COLOR_WRITEMASK
  31. struct {
  32. bool test_enabled = false; // GL_STENCIL_TEST
  33. struct {
  34. GLenum test_func = GL_ALWAYS; // GL_STENCIL_FUNC
  35. GLint test_ref = 0; // GL_STENCIL_REF
  36. GLuint test_mask = 0xFFFFFFFF; // GL_STENCIL_VALUE_MASK
  37. GLuint write_mask = 0xFFFFFFFF; // GL_STENCIL_WRITEMASK
  38. GLenum action_stencil_fail = GL_KEEP; // GL_STENCIL_FAIL
  39. GLenum action_depth_fail = GL_KEEP; // GL_STENCIL_PASS_DEPTH_FAIL
  40. GLenum action_depth_pass = GL_KEEP; // GL_STENCIL_PASS_DEPTH_PASS
  41. } front, back;
  42. } stencil;
  43. struct Blend {
  44. bool enabled = false; // GL_BLEND
  45. GLenum rgb_equation = GL_FUNC_ADD; // GL_BLEND_EQUATION_RGB
  46. GLenum a_equation = GL_FUNC_ADD; // GL_BLEND_EQUATION_ALPHA
  47. GLenum src_rgb_func = GL_ONE; // GL_BLEND_SRC_RGB
  48. GLenum dst_rgb_func = GL_ZERO; // GL_BLEND_DST_RGB
  49. GLenum src_a_func = GL_ONE; // GL_BLEND_SRC_ALPHA
  50. GLenum dst_a_func = GL_ZERO; // GL_BLEND_DST_ALPHA
  51. };
  52. std::array<Blend, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets> blend;
  53. struct {
  54. bool enabled = false;
  55. } independant_blend;
  56. static constexpr std::size_t NumSamplers = 32 * 5;
  57. static constexpr std::size_t NumImages = 8 * 5;
  58. std::array<GLuint, NumSamplers> textures = {};
  59. std::array<GLuint, NumSamplers> samplers = {};
  60. std::array<GLuint, NumImages> images = {};
  61. struct {
  62. GLuint read_framebuffer = 0; // GL_READ_FRAMEBUFFER_BINDING
  63. GLuint draw_framebuffer = 0; // GL_DRAW_FRAMEBUFFER_BINDING
  64. GLuint shader_program = 0; // GL_CURRENT_PROGRAM
  65. GLuint program_pipeline = 0; // GL_PROGRAM_PIPELINE_BINDING
  66. } draw;
  67. struct Viewport {
  68. GLint x = 0;
  69. GLint y = 0;
  70. GLint width = 0;
  71. GLint height = 0;
  72. GLfloat depth_range_near = 0.0f; // GL_DEPTH_RANGE
  73. GLfloat depth_range_far = 1.0f; // GL_DEPTH_RANGE
  74. struct {
  75. bool enabled = false; // GL_SCISSOR_TEST
  76. GLint x = 0;
  77. GLint y = 0;
  78. GLsizei width = 0;
  79. GLsizei height = 0;
  80. } scissor;
  81. };
  82. std::array<Viewport, Tegra::Engines::Maxwell3D::Regs::NumViewports> viewports;
  83. std::array<bool, 8> clip_distance = {}; // GL_CLIP_DISTANCE
  84. struct {
  85. GLenum origin = GL_LOWER_LEFT;
  86. GLenum depth_mode = GL_NEGATIVE_ONE_TO_ONE;
  87. } clip_control;
  88. GLuint renderbuffer{}; // GL_RENDERBUFFER_BINDING
  89. OpenGLState();
  90. /// Get the currently active OpenGL state
  91. static OpenGLState GetCurState() {
  92. return cur_state;
  93. }
  94. void SetDefaultViewports();
  95. /// Apply this state as the current OpenGL state
  96. void Apply();
  97. void ApplyFramebufferState();
  98. void ApplyShaderProgram();
  99. void ApplyProgramPipeline();
  100. void ApplyClipDistances();
  101. void ApplyFragmentColorClamp();
  102. void ApplyMultisample();
  103. void ApplySRgb();
  104. void ApplyRasterizerDiscard();
  105. void ApplyColorMask();
  106. void ApplyStencilTest();
  107. void ApplyViewport();
  108. void ApplyTargetBlending(std::size_t target, bool force);
  109. void ApplyGlobalBlending();
  110. void ApplyBlending();
  111. void ApplyTextures();
  112. void ApplySamplers();
  113. void ApplyImages();
  114. void ApplyClipControl();
  115. void ApplyRenderBuffer();
  116. /// Resets any references to the given resource
  117. OpenGLState& UnbindTexture(GLuint handle);
  118. OpenGLState& ResetSampler(GLuint handle);
  119. OpenGLState& ResetProgram(GLuint handle);
  120. OpenGLState& ResetPipeline(GLuint handle);
  121. OpenGLState& ResetFramebuffer(GLuint handle);
  122. OpenGLState& ResetRenderbuffer(GLuint handle);
  123. /// Viewport does not affects glClearBuffer so emulate viewport using scissor test
  124. void EmulateViewportWithScissor();
  125. private:
  126. static OpenGLState cur_state;
  127. };
  128. static_assert(std::is_trivially_copyable_v<OpenGLState>);
  129. } // namespace OpenGL