GlState.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef ANKI_GL_GL_STATE_H
  2. #define ANKI_GL_GL_STATE_H
  3. #include "anki/util/Singleton.h"
  4. #include "anki/gl/Ogl.h"
  5. #include "anki/util/Assert.h"
  6. #include "anki/util/StdTypes.h"
  7. #include "anki/util/Array.h"
  8. #include "anki/Math.h"
  9. namespace anki {
  10. class Vao;
  11. class Fbo;
  12. class ShaderProgram;
  13. /// @addtogroup OpenGL
  14. /// @{
  15. /// Common stuff for all GL states
  16. class GlStateCommon
  17. {
  18. public:
  19. /// Knowing the ventor allows some optimizations
  20. enum Gpu
  21. {
  22. GPU_UNKNOWN,
  23. GPU_ARM,
  24. GPU_NVIDIA
  25. };
  26. /// Return something like 430
  27. U32 getVersion() const
  28. {
  29. ANKI_ASSERT(version != -1);
  30. return version;
  31. }
  32. Gpu getGpu() const
  33. {
  34. return gpu;
  35. }
  36. Bool isTessellationSupported() const
  37. {
  38. return version >= 400;
  39. }
  40. Bool isComputeShaderSupported() const
  41. {
  42. return version >= 430;
  43. }
  44. void init(const U32 major, const U32 minor,
  45. Bool registerDebugMessages = false);
  46. private:
  47. /// Minor major GL version
  48. I32 version = -1;
  49. Gpu gpu = GPU_UNKNOWN;
  50. };
  51. typedef Singleton<GlStateCommon> GlStateCommonSingleton;
  52. /// Access the GL state machine.
  53. /// This class saves us from calling the GL functions
  54. class GlState
  55. {
  56. public:
  57. GlState()
  58. {
  59. sync();
  60. }
  61. ~GlState()
  62. {}
  63. /// @name Alter the GL state when needed
  64. /// @{
  65. void enable(GLenum cap, Bool enable = true);
  66. void disable(GLenum cap)
  67. {
  68. enable(cap, false);
  69. }
  70. bool isEnabled(GLenum cap);
  71. void setViewport(U32 x, U32 y, U32 w, U32 h);
  72. void setClearColor(const Vec4& color);
  73. void setClearDepthValue(const GLfloat depth);
  74. void setClearStencilValue(const GLint s);
  75. void setBlendFunctions(const GLenum sFactor, const GLenum dFactor);
  76. void setDepthMaskEnabled(const Bool enable);
  77. void setDepthFunc(const GLenum val);
  78. void setPolygonMode(const GLenum mode);
  79. /// @}
  80. private:
  81. /// @name The GL state
  82. /// @{
  83. Array<Bool, 7> flags;
  84. Array<GLint, 4> viewport;
  85. GLfloat clearDepthValue;
  86. GLint clearStencilValue;
  87. Array<GLenum, 2> blendFuncs;
  88. Array<GLint, 4> colorMask;
  89. GLint depthMask;
  90. GLenum depthFunc;
  91. #if ANKI_GL == ANKI_GL_DESKTOP
  92. GLenum polyMode;
  93. #endif
  94. /// @}
  95. /// Sync the local members with the opengl ones
  96. void sync();
  97. static U getIndexFromGlEnum(const GLenum cap);
  98. };
  99. typedef SingletonThreadSafe<GlState> GlStateSingleton;
  100. /// @}
  101. } // end namespace anki
  102. #endif