OGLGraphicsImpl.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../../Container/HashMap.h"
  5. #include "../../Core/Timer.h"
  6. #include "../../GraphicsAPI/ConstantBuffer.h"
  7. #include "../../GraphicsAPI/OpenGL/OGLShaderProgram.h"
  8. #include "../../GraphicsAPI/Texture2D.h"
  9. #include "../../Math/Color.h"
  10. #if defined(IOS) || defined(TVOS)
  11. #if URHO3D_GLES3
  12. #include <OpenGLES/ES3/gl.h>
  13. #include <OpenGLES/ES3/glext.h>
  14. #else
  15. #include <OpenGLES/ES2/gl.h>
  16. #include <OpenGLES/ES2/glext.h>
  17. #define URHO3D_GLES2
  18. #endif
  19. #elif defined(__ANDROID__) || defined (__arm__) || defined(__aarch64__) || defined (__EMSCRIPTEN__)
  20. #if URHO3D_GLES3
  21. #include <GLES3/gl3.h>
  22. #ifndef __EMSCRIPTEN__
  23. #include <GLES3/gl3ext.h>
  24. #else
  25. #include <GLES3/gl2ext.h>
  26. #endif
  27. #else
  28. #include <GLES2/gl2.h>
  29. #include <GLES2/gl2ext.h>
  30. #define URHO3D_GLES2
  31. #endif
  32. #else
  33. #include <GLEW/glew.h>
  34. #endif
  35. #ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
  36. #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83f1
  37. #endif
  38. #ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
  39. #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83f2
  40. #endif
  41. #ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
  42. #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83f3
  43. #endif
  44. #ifndef GL_ETC1_RGB8_OES
  45. #define GL_ETC1_RGB8_OES 0x8d64
  46. #endif
  47. #ifndef GL_ETC2_RGB8_OES
  48. #define GL_ETC2_RGB8_OES 0x9274
  49. #endif
  50. #ifndef GL_ETC2_RGBA8_OES
  51. #define GL_ETC2_RGBA8_OES 0x9278
  52. #endif
  53. #ifndef GL_DEPTH_COMPONENT24_OES
  54. #define GL_DEPTH_COMPONENT24_OES 0x81A6
  55. #endif
  56. #ifndef GL_DEPTH24_STENCIL8_OES
  57. #define GL_DEPTH24_STENCIL8_OES 0x88F0
  58. #endif
  59. #ifndef COMPRESSED_RGB_PVRTC_4BPPV1_IMG
  60. #define COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8c00
  61. #endif
  62. #ifndef COMPRESSED_RGB_PVRTC_2BPPV1_IMG
  63. #define COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8c01
  64. #endif
  65. #ifndef COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
  66. #define COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8c02
  67. #endif
  68. #ifndef COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
  69. #define COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8c03
  70. #endif
  71. using SDL_GLContext = void *;
  72. namespace Urho3D
  73. {
  74. class Context;
  75. using ConstantBufferMap = HashMap<unsigned, SharedPtr<ConstantBuffer>>;
  76. using ShaderProgramMap_OGL = HashMap<Pair<ShaderVariation*, ShaderVariation*>, SharedPtr<ShaderProgram_OGL>>;
  77. /// Cached state of a frame buffer object.
  78. struct FrameBufferObject
  79. {
  80. /// Frame buffer handle.
  81. unsigned fbo_{};
  82. /// Bound color attachment textures.
  83. RenderSurface* colorAttachments_[MAX_RENDERTARGETS]{};
  84. /// Bound depth/stencil attachment.
  85. RenderSurface* depthAttachment_{};
  86. /// Read buffer bits.
  87. unsigned readBuffers_{M_MAX_UNSIGNED};
  88. /// Draw buffer bits.
  89. unsigned drawBuffers_{M_MAX_UNSIGNED};
  90. };
  91. /// %Graphics subsystem implementation. Holds API-specific objects.
  92. class URHO3D_API GraphicsImpl_OGL
  93. {
  94. friend class Graphics;
  95. public:
  96. /// Construct.
  97. GraphicsImpl_OGL() = default;
  98. /// Return the GL Context.
  99. const SDL_GLContext& GetGLContext() { return context_; }
  100. private:
  101. /// SDL OpenGL context.
  102. SDL_GLContext context_{};
  103. /// iOS/tvOS system framebuffer handle.
  104. unsigned systemFBO_{};
  105. /// Active texture unit.
  106. unsigned activeTexture_{};
  107. /// Enabled vertex attributes bitmask.
  108. unsigned enabledVertexAttributes_{};
  109. /// Vertex attributes bitmask used by the current shader program.
  110. unsigned usedVertexAttributes_{};
  111. /// Vertex attribute instancing bitmask for keeping track of divisors.
  112. unsigned instancingVertexAttributes_{};
  113. /// Current mapping of vertex attribute locations by semantic. The map is owned by the shader program, so care must be taken to switch a null shader program when it's destroyed.
  114. const HashMap<Pair<i8, i8>, unsigned>* vertexAttributes_{};
  115. /// Currently bound frame buffer object.
  116. unsigned boundFBO_{};
  117. /// Currently bound vertex buffer object.
  118. unsigned boundVBO_{};
  119. /// Currently bound uniform buffer object.
  120. unsigned boundUBO_{};
  121. /// Read frame buffer for multisampled texture resolves.
  122. unsigned resolveSrcFBO_{};
  123. /// Write frame buffer for multisampled texture resolves.
  124. unsigned resolveDestFBO_{};
  125. /// Current pixel format.
  126. int pixelFormat_{};
  127. /// Map for FBO's per resolution and format.
  128. HashMap<hash64, FrameBufferObject> frameBuffers_;
  129. /// OpenGL texture types in use.
  130. unsigned textureTypes_[MAX_TEXTURE_UNITS]{};
  131. /// Constant buffer search map.
  132. ConstantBufferMap allConstantBuffers_;
  133. /// Currently bound constant buffers.
  134. ConstantBuffer* constantBuffers_[MAX_SHADER_PARAMETER_GROUPS * 2]{};
  135. /// Dirty constant buffers.
  136. Vector<ConstantBuffer*> dirtyConstantBuffers_;
  137. /// Last used instance data offset.
  138. unsigned lastInstanceOffset_{};
  139. /// Map for additional depth textures, to emulate Direct3D9 ability to mix render texture and backbuffer rendering.
  140. HashMap<unsigned, SharedPtr<Texture2D>> depthTextures_;
  141. /// Shader program in use.
  142. ShaderProgram_OGL* shaderProgram_{};
  143. /// Linked shader programs.
  144. ShaderProgramMap_OGL shaderPrograms_;
  145. /// Need FBO commit flag.
  146. bool fboDirty_{};
  147. /// Need vertex attribute pointer update flag.
  148. bool vertexBuffersDirty_{};
  149. /// sRGB write mode flag.
  150. bool sRGBWrite_{};
  151. };
  152. }