OGLGraphicsImpl.h 4.6 KB

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