OGLGraphicsImpl.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Copyright (c) 2008-2019 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 COMPRESSED_RGB_PVRTC_4BPPV1_IMG
  51. #define COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8c00
  52. #endif
  53. #ifndef COMPRESSED_RGB_PVRTC_2BPPV1_IMG
  54. #define COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8c01
  55. #endif
  56. #ifndef COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
  57. #define COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8c02
  58. #endif
  59. #ifndef COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
  60. #define COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8c03
  61. #endif
  62. using SDL_GLContext = void *;
  63. namespace Urho3D
  64. {
  65. class Context;
  66. using ConstantBufferMap = HashMap<unsigned, SharedPtr<ConstantBuffer> >;
  67. using ShaderProgramMap = HashMap<Pair<ShaderVariation*, ShaderVariation*>, SharedPtr<ShaderProgram> >;
  68. /// Cached state of a frame buffer object
  69. struct FrameBufferObject
  70. {
  71. /// Frame buffer handle.
  72. unsigned fbo_{};
  73. /// Bound color attachment textures.
  74. RenderSurface* colorAttachments_[MAX_RENDERTARGETS]{};
  75. /// Bound depth/stencil attachment.
  76. RenderSurface* depthAttachment_{};
  77. /// Read buffer bits.
  78. unsigned readBuffers_{M_MAX_UNSIGNED};
  79. /// Draw buffer bits.
  80. unsigned drawBuffers_{M_MAX_UNSIGNED};
  81. };
  82. /// %Graphics subsystem implementation. Holds API-specific objects.
  83. class URHO3D_API GraphicsImpl
  84. {
  85. friend class Graphics;
  86. public:
  87. /// Construct.
  88. GraphicsImpl() = default;
  89. /// Return the GL Context.
  90. const SDL_GLContext& GetGLContext() { return context_; }
  91. private:
  92. /// SDL OpenGL context.
  93. SDL_GLContext context_{};
  94. /// iOS/tvOS system framebuffer handle.
  95. unsigned systemFBO_{};
  96. /// Active texture unit.
  97. unsigned activeTexture_{};
  98. /// Enabled vertex attributes bitmask.
  99. unsigned enabledVertexAttributes_{};
  100. /// Vertex attributes bitmask used by the current shader program.
  101. unsigned usedVertexAttributes_{};
  102. /// Vertex attribute instancing bitmask for keeping track of divisors.
  103. unsigned instancingVertexAttributes_{};
  104. /// 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.
  105. const HashMap<Pair<unsigned char, unsigned char>, unsigned>* vertexAttributes_{};
  106. /// Currently bound frame buffer object.
  107. unsigned boundFBO_{};
  108. /// Currently bound vertex buffer object.
  109. unsigned boundVBO_{};
  110. /// Currently bound uniform buffer object.
  111. unsigned boundUBO_{};
  112. /// Read frame buffer for multisampled texture resolves.
  113. unsigned resolveSrcFBO_{};
  114. /// Write frame buffer for multisampled texture resolves.
  115. unsigned resolveDestFBO_{};
  116. /// Current pixel format.
  117. int pixelFormat_{};
  118. /// Map for FBO's per resolution and format.
  119. HashMap<unsigned long long, FrameBufferObject> frameBuffers_;
  120. /// OpenGL texture types in use.
  121. unsigned textureTypes_[MAX_TEXTURE_UNITS]{};
  122. /// Constant buffer search map.
  123. ConstantBufferMap allConstantBuffers_;
  124. /// Currently bound constant buffers.
  125. ConstantBuffer* constantBuffers_[MAX_SHADER_PARAMETER_GROUPS * 2]{};
  126. /// Dirty constant buffers.
  127. PODVector<ConstantBuffer*> dirtyConstantBuffers_;
  128. /// Last used instance data offset.
  129. unsigned lastInstanceOffset_{};
  130. /// Map for additional depth textures, to emulate Direct3D9 ability to mix render texture and backbuffer rendering.
  131. HashMap<unsigned, SharedPtr<Texture2D> > depthTextures_;
  132. /// Shader program in use.
  133. ShaderProgram* shaderProgram_{};
  134. /// Linked shader programs.
  135. ShaderProgramMap shaderPrograms_;
  136. /// Need FBO commit flag.
  137. bool fboDirty_{};
  138. /// Need vertex attribute pointer update flag.
  139. bool vertexBuffersDirty_{};
  140. /// sRGB write mode flag.
  141. bool sRGBWrite_{};
  142. };
  143. }