1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef API_RENDER_GLES_IDEVICE_GLES_H 17 #define API_RENDER_GLES_IDEVICE_GLES_H 18 19 #include <render/device/intf_device.h> 20 #include <render/device/intf_gpu_resource_manager.h> 21 #include <render/namespace.h> 22 23 // Platform / Backend specific typedefs. 24 #if RENDER_HAS_GLES_BACKEND 25 #include <EGL/egl.h> 26 #elif RENDER_HAS_GL_BACKEND 27 #ifndef WINAPI 28 #define WINAPI __stdcall 29 using HANDLE = void*; 30 using HINSTANCE = struct HINSTANCE__*; 31 using HMODULE = HINSTANCE; 32 using HWND = struct HWND__*; 33 using HDC = struct HDC__*; 34 using HGLRC = struct HGLRC__*; 35 #endif 36 #endif 37 38 RENDER_BEGIN_NAMESPACE() 39 /** \addtogroup group_gfx_gles_idevicegles 40 * @{ 41 */ 42 #if RENDER_HAS_GLES_BACKEND || (defined(DOXYGEN) && DOXYGEN) 43 /** Backend extra gles */ 44 struct BackendExtraGLES : public BackendExtra { 45 /** Application context, If != EGL_NO_CONTEXT the device/engine will use this EGLContext to do rendering. 46 *WARNING* GLES state caching might cause issues in this case. 47 (application can change engine GL state and engine can change application GL state) 48 */ 49 EGLContext applicationContext { EGL_NO_CONTEXT }; 50 /** Shared context If != EGL_NO_CONTEXT the device/engine will create it's own context with context sharing enabled. 51 */ 52 EGLContext sharedContext { EGL_NO_CONTEXT }; 53 /** Display to use */ 54 NativeDisplayType display { EGL_DEFAULT_DISPLAY }; 55 /** MSAA samples, 0 no MSAA, 4 = 4xMSAA backbuffer, 8 = 8X MSAA */ 56 uint32_t MSAASamples { 0 }; 57 /** Alpha bits, request 8 bits of alpha to backbuffer by default */ 58 uint32_t alphaBits { 8 }; 59 /** Depth bits, request NO depth buffer by default. */ 60 uint32_t depthBits { 0 }; 61 /** Stencil bits, request NO stencil buffer by default. */ 62 uint32_t stencilBits { 0 }; 63 }; 64 65 struct DevicePropertiesGLES { 66 // ES20 67 int32_t maxCombinedTextureImageUnits; 68 int32_t maxCubeMapTextureSize; 69 int32_t maxFragmentUniformVectors; 70 int32_t maxRenderbufferSize; 71 int32_t maxTextureImageUnits; 72 int32_t maxTextureSize; 73 int32_t maxVaryingVectors; 74 int32_t maxVertexAttribs; 75 int32_t maxVertexTextureImageUnits; 76 int32_t maxVertexUniformVectors; 77 float maxViewportDims[2]; 78 int32_t numCompressedTextureFormats; 79 int32_t numShaderBinaryFormats; 80 int32_t numProgramBinaryFormats; 81 82 // ES30 83 int32_t max3DTextureSize; 84 int32_t maxArrayTextureLayers; 85 int32_t maxColorAttachments; 86 int64_t maxCombinedFragmentUniformComponents; 87 int32_t maxCombinedUniformBlocks; 88 int64_t maxCombinedVertexUniformComponents; 89 int32_t maxDrawBuffers; 90 int64_t maxElementIndex; 91 int32_t maxElementsIndices; 92 int32_t maxElementsVertices; 93 int32_t maxFragmentInputComponents; 94 int32_t maxFragmentUniformBlocks; 95 int32_t maxFragmentUniformComponents; 96 int32_t minProgramTexelOffset; 97 int32_t maxProgramTexelOffset; 98 int32_t maxSamples; 99 int64_t maxServerWaitTimeout; 100 float maxTextureLodBias; 101 int32_t maxTransformFeedbackInterleavedComponents; 102 int32_t maxTransformFeedbackSeparateAttribs; 103 int32_t maxTransformFeedbackSeparateComponents; 104 int64_t maxUniformBlockSize; 105 int32_t maxUniformBufferBindings; 106 int32_t maxVaryingComponents; 107 int32_t maxVertexOutputComponents; 108 int32_t maxVertexUniformBlocks; 109 int32_t maxVertexUniformComponents; 110 111 // ES31 112 int32_t maxAtomicCounterBufferBindings; 113 int32_t maxAtomicCounterBufferSize; 114 int32_t maxColorTextureSamples; 115 int32_t maxCombinedAtomicCounters; 116 int32_t maxCombinedAtomicCounterBuffers; 117 int32_t maxCombinedComputeUniformComponents; 118 int32_t maxCombinedImageUniforms; 119 int32_t maxCombinedShaderOutputResources; 120 int32_t maxCombinedShaderStorageBlocks; 121 int32_t maxComputeAtomicCounters; 122 int32_t maxComputeAtomicCounterBuffers; 123 int32_t maxComputeImageUniforms; 124 int32_t maxComputeShaderStorageBlocks; 125 int32_t maxComputeSharedMemorySize; 126 int32_t maxComputeTextureImageUnits; 127 int32_t maxComputeUniformBlocks; 128 int32_t maxComputeUniformComponents; 129 int32_t maxComputeWorkGroupCount[3]; 130 int32_t maxComputeWorkGroupInvocations; 131 int32_t maxComputeWorkGroupSize[3]; 132 int32_t maxDepthTextureSamples; 133 int32_t maxFragmentAtomicCounters; 134 int32_t maxFragmentAtomicCounterBuffers; 135 int32_t maxFragmentImageUniforms; 136 int32_t maxFragmentShaderStorageBlocks; 137 int32_t maxFramebufferHeight; 138 int32_t maxFramebufferSamples; 139 int32_t maxFramebufferWidth; 140 int32_t maxImageUnits; 141 int32_t maxIntegerSamples; 142 int32_t minProgramTextureGatherOffset; 143 int32_t maxProgramTextureGatherOffset; 144 int32_t maxSampleMaskWords; 145 int64_t maxShaderStorageBlockSize; 146 int32_t maxShaderStorageBufferBindings; 147 int32_t maxUniformLocations; 148 int32_t maxVertexAtomicCounters; 149 int32_t maxVertexAtomicCounterBuffers; 150 int32_t maxVertexAttribBindings; 151 int32_t maxVertexAttribRelativeOffset; 152 int32_t maxVertexAttribStride; 153 int32_t maxVertexImageUniforms; 154 int32_t maxVertexShaderStorageBlocks; 155 int32_t uniformBufferOffsetAlignment; 156 int32_t shaderStorageBufferOffsetAlignment; 157 158 // ES32 159 float minSampleShadingValue; 160 int32_t maxDebugGroupStackDepth; 161 int32_t maxDebugLoggedMessages; 162 int32_t maxDebugMessageLength; 163 float minFragmentInterpolationOffset; 164 float maxFragmentInterpolationOffset; 165 int32_t maxFramebufferLayers; 166 int32_t maxLabelLength; 167 int32_t maxTextureBufferSize; 168 }; 169 170 /** Device platform data gles */ 171 struct DevicePlatformDataGLES : DevicePlatformData { 172 /** Display */ 173 EGLDisplay display { EGL_NO_DISPLAY }; 174 /** Config */ 175 EGLConfig config { nullptr }; 176 /** Context */ 177 EGLContext context { EGL_NO_CONTEXT }; 178 /** EGL Version */ 179 uint32_t majorVersion { 0 }; 180 uint32_t minorVersion { 0 }; 181 /** Does EGL have EGL_KHR_gl_colorspace **/ 182 bool hasColorSpaceExt { false }; 183 /** Context created by us, also destroy it */ 184 bool contextCreated { false }; 185 /** Call eglInitialize/eglTerminate or not */ 186 bool eglInitialized { false }; 187 /** Device name reported by the driver. */ 188 const char* deviceName; 189 /** Version string reported by the driver. */ 190 const char* driverVersion; 191 /** Collection of implementation dependant limits. */ 192 DevicePropertiesGLES deviceProperties; 193 }; 194 195 /** The following structure can be used to pass a TextureObject from application GLES context to Engine Context 196 * (device must be created with context sharing enabled or with app context) */ 197 struct ImageDescGLES : BackendSpecificImageDesc { 198 /** Type, GL_TEXTURE_2D / GL_TEXTURE_EXTERNAL_OES / etc */ 199 uint32_t type; 200 /** Image, Texture handle (glGenTextures) */ 201 uint32_t image; 202 /** Internal format, GL_RGBA16F etc */ 203 uint32_t internalFormat; 204 /** Format, GL_RGB etc */ 205 uint32_t format; 206 /** Data type, GL_FLOAT etc */ 207 uint32_t dataType; 208 /** Bytes per pixel */ 209 uint32_t bytesperpixel; 210 211 /** If non-zero should ba a valid EGLImage handle to be used as the source for the image. */ 212 uintptr_t eglImage { 0u }; 213 /** Platform specific hardware buffer */ 214 uintptr_t platformHwBuffer { 0u }; 215 }; 216 217 struct BufferDescGLES : BackendSpecificBufferDesc { 218 /** Platform specific hardware buffer */ 219 uintptr_t platformHwBuffer { 0u }; 220 }; 221 222 struct BackendConfigGLES final : BackendConfig { 223 /** GL_EXT_multisampled_render_to_texture2 says depth resolve is same as invalidation, but some implementations 224 * actually do a depth resolve. Application should check DeviceName and DriverVersion to make sure it's running on a 225 * device where it might work.*/ 226 bool allowDepthResolve; 227 }; 228 #endif 229 230 #if RENDER_HAS_GL_BACKEND || DOXYGEN 231 232 /** Backend extra gl */ 233 struct BackendExtraGL : public BackendExtra { 234 /** MSAA samples, 0 no MSAA, 4 = 4xMSAA backbuffer, 8 = 8X MSAA */ 235 uint32_t MSAASamples { 0 }; 236 /** Alpha bits, request 8 bits of alpha to backbuffer by default */ 237 uint32_t alphaBits { 8 }; 238 /** Depth bits, request NO depth buffer by default. */ 239 uint32_t depthBits { 0 }; 240 /** Stencil bits, request NO stencil buffer by default. */ 241 uint32_t stencilBits { 0 }; 242 #if _WIN32 || DOXYGEN 243 /** Handle to window. If no window is given, backend will try to use the current active window. */ 244 HWND window { nullptr }; 245 /** Shared context If != nullptr the device/engine will create it's own context with context sharing enabled. */ 246 HGLRC sharedContext { nullptr }; 247 #endif 248 }; 249 250 struct DevicePropertiesGL { 251 int32_t max3DTextureSize; 252 int32_t maxTextureSize; 253 int32_t maxArrayTextureLayers; 254 float maxTextureLodBias; 255 int32_t maxCubeMapTextureSize; 256 int32_t maxRenderbufferSize; 257 float maxTextureMaxAnisotropy; 258 float maxViewportDims[2]; 259 int32_t maxViewports; 260 int32_t viewportSubpixelBits; 261 int32_t viewportBoundsRange; 262 263 int32_t majorVersion; 264 int32_t minorVersion; 265 int32_t numProgramBinaryFormats; 266 int32_t numShaderBinaryFormats; 267 268 int32_t maxVertexAttribs; 269 int32_t maxVertexUniformComponents; 270 int32_t maxVertexUniformVectors; 271 int32_t maxVertexUniformBlocks; 272 int32_t maxVertexImageUniforms; 273 int32_t maxVertexOutputComponents; 274 int32_t maxVertexTextureImageUnits; 275 int32_t maxVertexAtomicCounterBuffers; 276 int32_t maxVertexAtomicCounters; 277 int32_t maxVertexShaderStorageBlocks; 278 int32_t maxCombinedVertexUniformComponents; 279 280 int32_t maxFragmentUniformComponents; 281 int32_t maxFragmentUniformVectors; 282 int32_t maxFragmentUniformBlocks; 283 int32_t maxFragmentImageUniforms; 284 int32_t maxFragmentInputComponents; 285 int32_t maxFragmentImageUnits; 286 int32_t maxFragmentAtomicCounterBuffers; 287 int32_t maxFragmentAtomicCounters; 288 int32_t maxFragmentShaderStorageBlocks; 289 int32_t maxCombinedFragmentUniformComponents; 290 291 int32_t maxComputeWorkGroupCount[3]; 292 int32_t maxComputeWorkGroupSize[3]; 293 int32_t maxComputeWorkGroupInvocations; 294 int32_t maxComputeUniformBlocks; 295 int32_t maxComputeImageUniforms; 296 int32_t maxComputeTextureImageUnits; 297 int32_t maxComputeAtomicCounterBuffers; 298 int32_t maxComputeAtomicCounters; 299 int32_t maxComputeSharedMemorySize; 300 int32_t maxComputeUniformComponents; 301 int32_t maxComputeShaderStorageBlocks; 302 int32_t maxCombinedComputeUniformComponents; 303 304 int32_t maxTextureBufferSize; 305 int32_t minProgramTexelOffset; 306 int32_t maxProgramTexelOffset; 307 int32_t minProgramTextureGatherOffset; 308 int32_t maxProgramTextureGatherOffset; 309 int32_t maxUniformBufferBindings; 310 int32_t maxUniformBlockSize; 311 int32_t uniformBufferOffsetAlignment; 312 int32_t maxCombinedUniformBlocks; 313 int32_t maxUniformLocations; 314 int32_t maxVaryingComponents; 315 int32_t maxVaryingFloats; 316 int32_t maxVaryingVectors; 317 int32_t maxCombinedTextureImageUnits; 318 int32_t maxAtomicCounterBufferBindings; 319 int32_t maxAtomicCounterBufferSize; 320 int32_t maxCombinedAtomicCounters; 321 int32_t maxCombinedAtomicCounterBuffers; 322 int32_t maxShaderStorageBufferBindings; 323 int32_t maxShaderStorageBlockSize; 324 int32_t maxCombinedShaderStorageBlocks; 325 int32_t shaderStorageBufferOffsetAlignment; 326 int32_t maxImageUnits; 327 int32_t maxCombinedShaderOutputResources; 328 int32_t maxCombinedImageUniforms; 329 330 int32_t minMapBufferAlignment; 331 int32_t maxVertexAttribRelativeOffset; 332 int32_t maxVertexAttribBindings; 333 int32_t maxVertexAttribStride; 334 int32_t maxElementsIndices; 335 int32_t maxElementsVertices; 336 int64_t maxElementIndex; 337 int32_t maxClipDistances; 338 int32_t maxColorAttachments; 339 int32_t maxFramebufferWidth; 340 int32_t maxFramebufferHeight; 341 int32_t maxFramebufferLayers; 342 int32_t maxFramebufferSamples; 343 int32_t maxSampleMaskWords; 344 int32_t maxSamples; 345 int32_t maxColorTextureSamples; 346 int32_t maxDepthTextureSamples; 347 int32_t maxIntegerSamples; 348 int64_t maxServerWaitTimeout; 349 int32_t maxDrawBuffers; 350 int32_t maxDualSourceDrawBuffers; 351 int32_t maxLabelLength; 352 }; 353 354 /** Device platform data gl */ 355 struct DevicePlatformDataGL : DevicePlatformData { 356 /** Device name reported by the driver. */ 357 const char* deviceName; 358 /** Version string reported by the driver. */ 359 const char* driverVersion; 360 /** Collection of implementation dependant limits. */ 361 DevicePropertiesGL deviceProperties; 362 #if _WIN32 || DOXYGEN 363 /** Hwnd */ 364 HWND mhWnd = nullptr; 365 /** Display */ 366 HDC display = nullptr; 367 /** Context */ 368 HGLRC context = nullptr; 369 #endif 370 }; 371 372 /** The following structure can be used to pass a TextureObject from application GLES context to Engine Context 373 * (device must be created with context sharing enabled or with app context) */ 374 struct ImageDescGL : BackendSpecificImageDesc { 375 /** Type, GL_TEXTURE_2D / GL_TEXTURE_EXTERNAL_OES / etc */ 376 uint32_t type; 377 /** Texture handle (glGenTextures) */ 378 uint32_t image; 379 /** Internal format, GL_RGBA16F etc */ 380 uint32_t internalFormat; 381 /** Format, GL_RGB etc */ 382 uint32_t format; 383 /** Data type, GL_FLOAT etc */ 384 uint32_t dataType; 385 /** Bytes per pixel */ 386 uint32_t bytesperpixel; 387 }; 388 #endif 389 /** Provides interface for low-level access */ 390 class ILowLevelDeviceGLES : public ILowLevelDevice { 391 public: 392 #if RENDER_HAS_EXPERIMENTAL 393 /** EXPERIMENTAL: Those methods have been added to be able to use opengl context initialized with lume render as a 394 platform abstraction library. 395 This should not be used while using standart render node graph rendering with lume. 396 Manual activation of gles device. When activated, DevicePlatformData will contains valid pointers 397 to the opengl context which will be made active on the current thread. All further call to GL must be from this 398 thread until this device is release with an explitic call to Deactivate(). Device will be guarded internally by a 399 mutex while activated. */ 400 virtual void Activate() = 0; 401 /** EXPERIMENTAL: Manual deactivation of gles device following an explicit activation by a call to Activate() on the 402 * same thread. */ 403 virtual void Deactivate() = 0; 404 /** EXPERIMENTAL: Manual call to GL Swapbuffer with the current default swapchain if it exists. If no swapchain 405 exists, this function has no effect. this call must apend between a call to Activate() and Deactivate(). 406 */ 407 virtual void SwapBuffers() = 0; 408 // no-support for low level resource access 409 #endif 410 protected: 411 ILowLevelDeviceGLES() = default; 412 ~ILowLevelDeviceGLES() = default; 413 }; 414 /** @} */ 415 RENDER_END_NAMESPACE() 416 417 #endif // API_RENDER_GLES_IDEVICE_GLES_H 418