1 /*
2  * Copyright (c) 2024 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     int32_t 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 BackendConfigGLES final : BackendConfig {
218     /** GL_EXT_multisampled_render_to_texture2 says depth resolve is same as invalidation, but some implementations
219      * actually do a depth resolve. Application should check DeviceName and DriverVersion to make sure it's running on a
220      * device where it might work.*/
221     bool allowDepthResolve;
222 };
223 #endif
224 
225 #if RENDER_HAS_GL_BACKEND || DOXYGEN
226 
227 /** Backend extra gl */
228 struct BackendExtraGL : public BackendExtra {
229     /** MSAA samples, 0 no MSAA, 4 = 4xMSAA backbuffer, 8 = 8X MSAA */
230     uint32_t MSAASamples { 0 };
231     /** Alpha bits, request 8 bits of alpha to backbuffer by default */
232     uint32_t alphaBits { 8 };
233     /** Depth bits, request NO depth buffer by default. */
234     uint32_t depthBits { 0 };
235     /** Stencil bits, request NO stencil buffer by default. */
236     uint32_t stencilBits { 0 };
237 #if _WIN32 || DOXYGEN
238     /** Handle to window. If no window is given, backend will try to use the current active window. */
239     HWND window { nullptr };
240     /** Shared context If != nullptr the device/engine will create it's own context with context sharing enabled. */
241     HGLRC sharedContext { nullptr };
242 #endif
243 };
244 
245 struct DevicePropertiesGL {
246     int32_t max3DTextureSize;
247     int32_t maxTextureSize;
248     int32_t maxArrayTextureLayers;
249     float maxTextureLodBias;
250     int32_t maxCubeMapTextureSize;
251     int32_t maxRenderbufferSize;
252     float maxTextureMaxAnisotropy;
253     float maxViewportDims[2];
254     int32_t maxViewports;
255     int32_t viewportSubpixelBits;
256     int32_t viewportBoundsRange;
257 
258     int32_t majorVersion;
259     int32_t minorVersion;
260     int32_t numProgramBinaryFormats;
261     int32_t numShaderBinaryFormats;
262 
263     int32_t maxVertexAttribs;
264     int32_t maxVertexUniformComponents;
265     int32_t maxVertexUniformVectors;
266     int32_t maxVertexUniformBlocks;
267     int32_t maxVertexImageUniforms;
268     int32_t maxVertexOutputComponents;
269     int32_t maxVertexTextureImageUnits;
270     int32_t maxVertexAtomicCounterBuffers;
271     int32_t maxVertexAtomicCounters;
272     int32_t maxVertexShaderStorageBlocks;
273     int32_t maxCombinedVertexUniformComponents;
274 
275     int32_t maxFragmentUniformComponents;
276     int32_t maxFragmentUniformVectors;
277     int32_t maxFragmentUniformBlocks;
278     int32_t maxFragmentImageUniforms;
279     int32_t maxFragmentInputComponents;
280     int32_t maxFragmentImageUnits;
281     int32_t maxFragmentAtomicCounterBuffers;
282     int32_t maxFragmentAtomicCounters;
283     int32_t maxFragmentShaderStorageBlocks;
284     int32_t maxCombinedFragmentUniformComponents;
285 
286     int32_t maxComputeWorkGroupCount[3];
287     int32_t maxComputeWorkGroupSize[3];
288     int32_t maxComputeWorkGroupInvocations;
289     int32_t maxComputeUniformBlocks;
290     int32_t maxComputeImageUniforms;
291     int32_t maxComputeTextureImageUnits;
292     int32_t maxComputeAtomicCounterBuffers;
293     int32_t maxComputeAtomicCounters;
294     int32_t maxComputeSharedMemorySize;
295     int32_t maxComputeUniformComponents;
296     int32_t maxComputeShaderStorageBlocks;
297     int32_t maxCombinedComputeUniformComponents;
298 
299     int32_t maxTextureBufferSize;
300     int32_t minProgramTexelOffset;
301     int32_t maxProgramTexelOffset;
302     int32_t minProgramTextureGatherOffset;
303     int32_t maxProgramTextureGatherOffset;
304     int32_t maxUniformBufferBindings;
305     int32_t maxUniformBlockSize;
306     int32_t uniformBufferOffsetAlignment;
307     int32_t maxCombinedUniformBlocks;
308     int32_t maxUniformLocations;
309     int32_t maxVaryingComponents;
310     int32_t maxVaryingFloats;
311     int32_t maxVaryingVectors;
312     int32_t maxCombinedTextureImageUnits;
313     int32_t maxAtomicCounterBufferBindings;
314     int32_t maxAtomicCounterBufferSize;
315     int32_t maxCombinedAtomicCounters;
316     int32_t maxCombinedAtomicCounterBuffers;
317     int32_t maxShaderStorageBufferBindings;
318     int32_t maxShaderStorageBlockSize;
319     int32_t maxCombinedShaderStorageBlocks;
320     int32_t shaderStorageBufferOffsetAlignment;
321     int32_t maxImageUnits;
322     int32_t maxCombinedShaderOutputResources;
323     int32_t maxCombinedImageUniforms;
324 
325     int32_t minMapBufferAlignment;
326     int32_t maxVertexAttribRelativeOffset;
327     int32_t maxVertexAttribBindings;
328     int32_t maxVertexAttribStride;
329     int32_t maxElementsIndices;
330     int32_t maxElementsVertices;
331     int64_t maxElementIndex;
332     int32_t maxClipDistances;
333     int32_t maxColorAttachments;
334     int32_t maxFramebufferWidth;
335     int32_t maxFramebufferHeight;
336     int32_t maxFramebufferLayers;
337     int32_t maxFramebufferSamples;
338     int32_t maxSampleMaskWords;
339     int32_t maxSamples;
340     int32_t maxColorTextureSamples;
341     int32_t maxDepthTextureSamples;
342     int32_t maxIntegerSamples;
343     int64_t maxServerWaitTimeout;
344     int32_t maxDrawBuffers;
345     int32_t maxDualSourceDrawBuffers;
346     int32_t maxLabelLength;
347 };
348 
349 /** Device platform data gl */
350 struct DevicePlatformDataGL : DevicePlatformData {
351     /** Device name reported by the driver. */
352     const char* deviceName;
353     /** Version string reported by the driver. */
354     const char* driverVersion;
355     /** Collection of implementation dependant limits. */
356     DevicePropertiesGL deviceProperties;
357 #if _WIN32 || DOXYGEN
358     /** Hwnd */
359     HWND mhWnd = nullptr;
360     /** Display */
361     HDC display = nullptr;
362     /** Context */
363     HGLRC context = nullptr;
364 #endif
365 };
366 
367 /** The following structure can be used to pass a TextureObject from application GLES context to Engine Context
368  * (device must be created with context sharing enabled or with app context) */
369 struct ImageDescGL : BackendSpecificImageDesc {
370     /** Type, GL_TEXTURE_2D / GL_TEXTURE_EXTERNAL_OES / etc */
371     uint32_t type;
372     /** Texture handle (glGenTextures) */
373     uint32_t image;
374     /** Internal format, GL_RGBA16F etc */
375     uint32_t internalFormat;
376     /** Format, GL_RGB etc */
377     uint32_t format;
378     /** Data type, GL_FLOAT etc */
379     uint32_t dataType;
380     /** Bytes per pixel */
381     uint32_t bytesperpixel;
382 };
383 #endif
384 /** Provides interface for low-level access */
385 class ILowLevelDeviceGLES : public ILowLevelDevice {
386 public:
387 #if RENDER_HAS_EXPERIMENTAL
388     /** EXPERIMENTAL: Those methods have been added to be able to use opengl context initialized with lume render as a
389        platform abstraction library.
390        This should not be used while using standart render node graph rendering with lume.
391        Manual activation of gles device. When activated, DevicePlatformData will contains valid pointers
392        to the opengl context which will be made active on the current thread. All further call to GL must be from this
393        thread until this device is release with an explitic call to Deactivate(). Device will be guarded internally by a
394        mutex while activated. */
395     virtual void Activate() = 0;
396     /** EXPERIMENTAL: Manual deactivation of gles device following an explicit activation by a call to Activate() on the
397      * same thread. */
398     virtual void Deactivate() = 0;
399     /** EXPERIMENTAL: Manual call to GL Swapbuffer with the current default swapchain if it exists. If no swapchain
400        exists, this function has no effect. this call must apend between a call to Activate() and Deactivate().
401     */
402     virtual void SwapBuffers() = 0;
403 // no-support for low level resource access
404 #endif
405 protected:
406     ILowLevelDeviceGLES() = default;
407     ~ILowLevelDeviceGLES() = default;
408 };
409 /** @} */
410 RENDER_END_NAMESPACE()
411 
412 #endif // API_RENDER_GLES_IDEVICE_GLES_H
413