OGLGraphicsImpl.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../../Container/HashMap.h"
  24. #include "../../Core/Timer.h"
  25. #include "../../Graphics/ConstantBuffer.h"
  26. #include "../../Graphics/ShaderProgram.h"
  27. #include "../../Graphics/Texture2D.h"
  28. #include "../../Math/Color.h"
  29. #if defined(IOS) || defined(TVOS)
  30. #include <OpenGLES/ES2/gl.h>
  31. #include <OpenGLES/ES2/glext.h>
  32. #elif defined(__ANDROID__) || defined (__arm__) || defined(__aarch64__) || defined (__EMSCRIPTEN__)
  33. #include <GLES2/gl2.h>
  34. #include <GLES2/gl2ext.h>
  35. #else
  36. #include <GLEW/glew.h>
  37. #endif
  38. #ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
  39. #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83f1
  40. #endif
  41. #ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
  42. #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83f2
  43. #endif
  44. #ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
  45. #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83f3
  46. #endif
  47. #ifndef GL_ETC1_RGB8_OES
  48. #define GL_ETC1_RGB8_OES 0x8d64
  49. #endif
  50. #ifndef GL_ETC2_RGB8_OES
  51. #define GL_ETC2_RGB8_OES 0x9274
  52. #endif
  53. #ifndef GL_ETC2_RGBA8_OES
  54. #define GL_ETC2_RGBA8_OES 0x9278
  55. #endif
  56. #ifndef COMPRESSED_RGB_PVRTC_4BPPV1_IMG
  57. #define COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8c00
  58. #endif
  59. #ifndef COMPRESSED_RGB_PVRTC_2BPPV1_IMG
  60. #define COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8c01
  61. #endif
  62. #ifndef COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
  63. #define COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8c02
  64. #endif
  65. #ifndef COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
  66. #define COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8c03
  67. #endif
  68. using SDL_GLContext = void *;
  69. namespace Urho3D
  70. {
  71. class Context;
  72. using ConstantBufferMap = HashMap<unsigned, SharedPtr<ConstantBuffer> >;
  73. using ShaderProgramMap = HashMap<Pair<ShaderVariation*, ShaderVariation*>, SharedPtr<ShaderProgram> >;
  74. /// Cached state of a frame buffer object.
  75. struct FrameBufferObject
  76. {
  77. /// Frame buffer handle.
  78. unsigned fbo_{};
  79. /// Bound color attachment textures.
  80. RenderSurface* colorAttachments_[MAX_RENDERTARGETS]{};
  81. /// Bound depth/stencil attachment.
  82. RenderSurface* depthAttachment_{};
  83. /// Read buffer bits.
  84. unsigned readBuffers_{M_MAX_UNSIGNED};
  85. /// Draw buffer bits.
  86. unsigned drawBuffers_{M_MAX_UNSIGNED};
  87. };
  88. /// %Graphics subsystem implementation. Holds API-specific objects.
  89. class URHO3D_API GraphicsImpl
  90. {
  91. friend class Graphics;
  92. public:
  93. /// Construct.
  94. GraphicsImpl() = default;
  95. /// Return the GL Context.
  96. const SDL_GLContext& GetGLContext() { return context_; }
  97. private:
  98. /// SDL OpenGL context.
  99. SDL_GLContext context_{};
  100. /// iOS/tvOS system framebuffer handle.
  101. unsigned systemFBO_{};
  102. /// Active texture unit.
  103. unsigned activeTexture_{};
  104. /// Enabled vertex attributes bitmask.
  105. unsigned enabledVertexAttributes_{};
  106. /// Vertex attributes bitmask used by the current shader program.
  107. unsigned usedVertexAttributes_{};
  108. /// Vertex attribute instancing bitmask for keeping track of divisors.
  109. unsigned instancingVertexAttributes_{};
  110. /// 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.
  111. const HashMap<Pair<unsigned char, unsigned char>, unsigned>* vertexAttributes_{};
  112. /// Currently bound frame buffer object.
  113. unsigned boundFBO_{};
  114. /// Currently bound vertex buffer object.
  115. unsigned boundVBO_{};
  116. /// Currently bound uniform buffer object.
  117. unsigned boundUBO_{};
  118. /// Read frame buffer for multisampled texture resolves.
  119. unsigned resolveSrcFBO_{};
  120. /// Write frame buffer for multisampled texture resolves.
  121. unsigned resolveDestFBO_{};
  122. /// Current pixel format.
  123. int pixelFormat_{};
  124. /// Map for FBO's per resolution and format.
  125. HashMap<unsigned long long, FrameBufferObject> frameBuffers_;
  126. /// OpenGL texture types in use.
  127. unsigned textureTypes_[MAX_TEXTURE_UNITS]{};
  128. /// Constant buffer search map.
  129. ConstantBufferMap allConstantBuffers_;
  130. /// Currently bound constant buffers.
  131. ConstantBuffer* constantBuffers_[MAX_SHADER_PARAMETER_GROUPS * 2]{};
  132. /// Dirty constant buffers.
  133. PODVector<ConstantBuffer*> dirtyConstantBuffers_;
  134. /// Last used instance data offset.
  135. unsigned lastInstanceOffset_{};
  136. /// Map for additional depth textures, to emulate Direct3D9 ability to mix render texture and backbuffer rendering.
  137. HashMap<unsigned, SharedPtr<Texture2D> > depthTextures_;
  138. /// Shader program in use.
  139. ShaderProgram* shaderProgram_{};
  140. /// Linked shader programs.
  141. ShaderProgramMap shaderPrograms_;
  142. /// Need FBO commit flag.
  143. bool fboDirty_{};
  144. /// Need vertex attribute pointer update flag.
  145. bool vertexBuffersDirty_{};
  146. /// sRGB write mode flag.
  147. bool sRGBWrite_{};
  148. };
  149. }