forked from DiligentGraphics/DiligentCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderDeviceGLImpl.cpp
More file actions
1774 lines (1547 loc) · 89.2 KB
/
RenderDeviceGLImpl.cpp
File metadata and controls
1774 lines (1547 loc) · 89.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019-2025 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/
#include "pch.h"
#include "RenderDeviceGLImpl.hpp"
#include "BufferGLImpl.hpp"
#include "ShaderGLImpl.hpp"
#include "Texture1D_GL.hpp"
#include "Texture1DArray_GL.hpp"
#include "Texture2D_GL.hpp"
#include "Texture2DArray_GL.hpp"
#include "Texture3D_GL.hpp"
#include "TextureCube_GL.hpp"
#include "TextureCubeArray_GL.hpp"
#include "SamplerGLImpl.hpp"
#include "DeviceContextGLImpl.hpp"
#include "PipelineStateGLImpl.hpp"
#include "ShaderResourceBindingGLImpl.hpp"
#include "FenceGLImpl.hpp"
#include "QueryGLImpl.hpp"
#include "RenderPassGLImpl.hpp"
#include "FramebufferGLImpl.hpp"
#include "PipelineResourceSignatureGLImpl.hpp"
#include "GLTypeConversions.hpp"
#include "VAOCache.hpp"
#include "EngineMemory.h"
#include "StringTools.hpp"
namespace Diligent
{
#if GL_KHR_debug
static void GLAPIENTRY openglCallbackFunction(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam)
{
const int* ShowDebugOutput = reinterpret_cast<const int*>(userParam);
if (*ShowDebugOutput == 0)
return;
// Note: disabling flood of notifications through glDebugMessageControl() has no effect,
// so we have to filter them out here
if (id == 131185 || // Buffer detailed info: Buffer object <X> (bound to GL_XXXX ... , usage hint is GL_DYNAMIC_DRAW)
// will use VIDEO memory as the source for buffer object operations.
id == 131186 // Buffer object <X> (bound to GL_XXXX, usage hint is GL_DYNAMIC_DRAW) is being copied/moved from VIDEO memory to HOST memory.
)
return;
std::stringstream MessageSS;
MessageSS << "OpenGL debug message " << id << " (";
switch (source)
{
// clang-format off
case GL_DEBUG_SOURCE_API: MessageSS << "Source: API."; break;
case GL_DEBUG_SOURCE_WINDOW_SYSTEM: MessageSS << "Source: Window System."; break;
case GL_DEBUG_SOURCE_SHADER_COMPILER: MessageSS << "Source: Shader Compiler."; break;
case GL_DEBUG_SOURCE_THIRD_PARTY: MessageSS << "Source: Third Party."; break;
case GL_DEBUG_SOURCE_APPLICATION: MessageSS << "Source: Application."; break;
case GL_DEBUG_SOURCE_OTHER: MessageSS << "Source: Other."; break;
default: MessageSS << "Source: Unknown (" << source << ").";
// clang-format on
}
switch (type)
{
// clang-format off
case GL_DEBUG_TYPE_ERROR: MessageSS << " Type: ERROR."; break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: MessageSS << " Type: Deprecated Behaviour."; break;
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: MessageSS << " Type: UNDEFINED BEHAVIOUR."; break;
case GL_DEBUG_TYPE_PORTABILITY: MessageSS << " Type: Portability."; break;
case GL_DEBUG_TYPE_PERFORMANCE: MessageSS << " Type: PERFORMANCE."; break;
case GL_DEBUG_TYPE_MARKER: MessageSS << " Type: Marker."; break;
case GL_DEBUG_TYPE_PUSH_GROUP: MessageSS << " Type: Push Group."; break;
case GL_DEBUG_TYPE_POP_GROUP: MessageSS << " Type: Pop Group."; break;
case GL_DEBUG_TYPE_OTHER: MessageSS << " Type: Other."; break;
default: MessageSS << " Type: Unknown (" << type << ").";
// clang-format on
}
switch (severity)
{
// clang-format off
case GL_DEBUG_SEVERITY_HIGH: MessageSS << " Severity: HIGH"; break;
case GL_DEBUG_SEVERITY_MEDIUM: MessageSS << " Severity: Medium"; break;
case GL_DEBUG_SEVERITY_LOW: MessageSS << " Severity: Low"; break;
case GL_DEBUG_SEVERITY_NOTIFICATION: MessageSS << " Severity: Notification"; break;
default: MessageSS << " Severity: Unknown (" << severity << ")"; break;
// clang-format on
}
MessageSS << "): " << message;
LOG_INFO_MESSAGE(MessageSS.str().c_str());
}
#endif // GL_KHR_debug
class BottomLevelASGLImpl
{};
class TopLevelASGLImpl
{};
class ShaderBindingTableGLImpl
{};
class DeviceMemoryGLImpl
{};
static void VerifyEngineGLCreateInfo(const EngineGLCreateInfo& EngineCI) noexcept(false)
{
if (EngineCI.Features.ShaderResourceQueries == DEVICE_FEATURE_STATE_ENABLED &&
EngineCI.Features.SeparablePrograms == DEVICE_FEATURE_STATE_DISABLED)
{
LOG_ERROR_AND_THROW("Requested state for ShaderResourceQueries feature is ENABLED, while requested state for SeparablePrograms feature is DISABLED. "
"ShaderResourceQueries may only be enabled when SeparablePrograms feature is also enabled.");
}
if (EngineCI.Features.GeometryShaders == DEVICE_FEATURE_STATE_ENABLED &&
EngineCI.Features.SeparablePrograms == DEVICE_FEATURE_STATE_DISABLED)
{
LOG_ERROR_AND_THROW("Requested state for GeometryShaders feature is ENABLED, while requested state for SeparablePrograms feature is DISABLED. "
"GeometryShaders may only be enabled when SeparablePrograms feature is also enabled.");
}
if (EngineCI.Features.Tessellation == DEVICE_FEATURE_STATE_ENABLED &&
EngineCI.Features.SeparablePrograms == DEVICE_FEATURE_STATE_DISABLED)
{
LOG_ERROR_AND_THROW("Requested state for Tessellation feature is ENABLED, while requested state for SeparablePrograms feature is DISABLED. "
"Tessellation may only be enabled when SeparablePrograms feature is also enabled.");
}
}
RenderDeviceGLImpl::RenderDeviceGLImpl(IReferenceCounters* pRefCounters,
IMemoryAllocator& RawMemAllocator,
IEngineFactory* pEngineFactory,
const EngineGLCreateInfo& EngineCI,
const SwapChainDesc* pSCDesc) :
// clang-format off
TRenderDeviceBase
{
pRefCounters,
RawMemAllocator,
pEngineFactory,
EngineCI,
GraphicsAdapterInfo{} // Adapter properties can only be queried after GL context is initialized
},
// Device caps must be filled in before the constructor of Pipeline Cache is called!
m_GLContext{EngineCI, m_DeviceInfo.Type, m_DeviceInfo.APIVersion, pSCDesc}
// clang-format on
{
VerifyEngineGLCreateInfo(EngineCI);
VERIFY(EngineCI.NumDeferredContexts == 0, "EngineCI.NumDeferredContexts > 0 should've been caught by CreateDeviceAndSwapChainGL() or AttachToActiveGLContext()");
GLint NumExtensions = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &NumExtensions);
CHECK_GL_ERROR("Failed to get the number of extensions");
m_ExtensionStrings.reserve(NumExtensions);
for (int Ext = 0; Ext < NumExtensions; ++Ext)
{
const GLubyte* CurrExtension = glGetStringi(GL_EXTENSIONS, Ext);
CHECK_GL_ERROR("Failed to get extension string #", Ext);
m_ExtensionStrings.emplace(reinterpret_cast<const Char*>(CurrExtension));
}
#if GL_KHR_debug
if (EngineCI.EnableValidation && glDebugMessageCallback != nullptr)
{
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(openglCallbackFunction, &m_ShowDebugGLOutput);
if (glDebugMessageControl != nullptr)
{
glDebugMessageControl(
GL_DONT_CARE, // Source of debug messages to enable or disable
GL_DONT_CARE, // Type of debug messages to enable or disable
GL_DONT_CARE, // Severity of debug messages to enable or disable
0, // The length of the array ids
nullptr, // Array of unsigned integers containing the ids of the messages to enable or disable
GL_TRUE // Flag determining whether the selected messages should be enabled or disabled
);
// Disable messages from glPushDebugGroup and glDebugMessageInsert
glDebugMessageControl(
GL_DEBUG_SOURCE_APPLICATION, // Source of debug messages to enable or disable
GL_DONT_CARE, // Type of debug messages to enable or disable
GL_DONT_CARE, // Severity of debug messages to enable or disable
0, // The length of the array ids
nullptr, // Array of unsigned integers containing the ids of the messages to enable or disable
GL_FALSE // Flag determining whether the selected messages should be enabled or disabled
);
}
if (glGetError() != GL_NO_ERROR)
LOG_ERROR_MESSAGE("Failed to enable debug messages");
}
#endif
#if PLATFORM_WIN32 || PLATFORM_LINUX || PLATFORM_MACOS
if (m_DeviceInfo.APIVersion >= Version{4, 6} || CheckExtension("GL_ARB_ES3_compatibility"))
{
glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
if (glGetError() != GL_NO_ERROR)
LOG_ERROR_MESSAGE("Failed to enable primitive restart fixed index");
}
else
{
glEnable(GL_PRIMITIVE_RESTART);
if (glGetError() == GL_NO_ERROR)
{
glPrimitiveRestartIndex(0xFFFFFFFFu);
if (glGetError() != GL_NO_ERROR)
LOG_ERROR_MESSAGE("Failed to set the primitive restart index");
}
else
{
LOG_ERROR_MESSAGE("Failed to enable primitive restart");
}
}
{
// In all APIs except for OpenGL, the first primitive vertex is the provoking vertex
// for flat shading. In OpenGL, the last vertex is the provoking vertex by default.
// Make the behavior consistent across all APIs
glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
if (glGetError() != GL_NO_ERROR)
LOG_ERROR_MESSAGE("Failed to set provoking vertex convention to GL_FIRST_VERTEX_CONVENTION");
}
#endif
InitAdapterInfo();
// Enable requested device features
m_DeviceInfo.Features = EnableDeviceFeatures(m_AdapterInfo.Features, EngineCI.Features);
if (m_AdapterInfo.Features.SeparablePrograms && !EngineCI.Features.SeparablePrograms)
{
VERIFY_EXPR(!m_DeviceInfo.Features.SeparablePrograms);
LOG_INFO_MESSAGE("Disabling separable shader programs");
}
m_DeviceInfo.Features.ShaderResourceQueries = m_DeviceInfo.Features.SeparablePrograms;
m_DeviceInfo.Features.GeometryShaders = std::min(m_DeviceInfo.Features.SeparablePrograms, m_DeviceInfo.Features.GeometryShaders);
m_DeviceInfo.Features.Tessellation = std::min(m_DeviceInfo.Features.SeparablePrograms, m_DeviceInfo.Features.Tessellation);
FlagSupportedTexFormats();
if (EngineCI.ZeroToOneNDZ && (CheckExtension("GL_ARB_clip_control") || CheckExtension("GL_EXT_clip_control")))
{
glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
m_DeviceInfo.NDC = NDCAttribs{0.0f, 1.0f, 0.5f};
}
else
{
m_DeviceInfo.NDC = NDCAttribs{-1.0f, 0.5f, 0.5f};
}
if (m_GLCaps.FramebufferSRGB)
{
// When GL_FRAMEBUFFER_SRGB is enabled, and if the destination image is in the sRGB colorspace
// then OpenGL will assume the shader's output is in the linear RGB colorspace. It will therefore
// convert the output from linear RGB to sRGB.
// Any writes to images that are not in the sRGB format should not be affected.
// Thus this setting should be just set once and left that way
glEnable(GL_FRAMEBUFFER_SRGB);
if (glGetError() != GL_NO_ERROR)
{
LOG_ERROR_MESSAGE("Failed to enable SRGB framebuffers");
m_GLCaps.FramebufferSRGB = false;
}
}
#if PLATFORM_WIN32 || PLATFORM_LINUX || PLATFORM_MACOS
if (m_GLCaps.SemalessCubemaps)
{
// Under the standard filtering rules for cubemaps, filtering does not work across faces of the cubemap.
// This results in a seam across the faces of a cubemap. This was a hardware limitation in the past, but
// modern hardware is capable of interpolating across a cube face boundary.
// GL_TEXTURE_CUBE_MAP_SEAMLESS is not defined in OpenGLES
glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
if (glGetError() != GL_NO_ERROR)
{
LOG_ERROR_MESSAGE("Failed to enable seamless cubemap filtering");
m_GLCaps.SemalessCubemaps = false;
}
}
#endif
// get device limits
{
glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &m_DeviceLimits.MaxUniformBlocks);
CHECK_GL_ERROR("glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS) failed");
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &m_DeviceLimits.MaxTextureUnits);
CHECK_GL_ERROR("glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS) failed");
if (m_AdapterInfo.Features.ComputeShaders)
{
#if GL_ARB_shader_storage_buffer_object
glGetIntegerv(GL_MAX_COMBINED_SHADER_STORAGE_BLOCKS, &m_DeviceLimits.MaxStorageBlock);
CHECK_GL_ERROR("glGetIntegerv(GL_MAX_COMBINED_SHADER_STORAGE_BLOCKS) failed");
#endif
#if GL_ARB_shader_image_load_store
glGetIntegerv(GL_MAX_IMAGE_UNITS, &m_DeviceLimits.MaxImagesUnits);
CHECK_GL_ERROR("glGetIntegerv(GL_MAX_IMAGE_UNITS) failed");
#endif
}
}
if (m_DeviceInfo.Type == RENDER_DEVICE_TYPE_GL)
m_DeviceInfo.MaxShaderVersion.GLSL = m_DeviceInfo.APIVersion;
else
m_DeviceInfo.MaxShaderVersion.GLESSL = m_DeviceInfo.APIVersion;
#if !DILIGENT_NO_HLSL
m_DeviceInfo.MaxShaderVersion.HLSL = {5, 0};
#endif
#if GL_KHR_parallel_shader_compile
if (m_DeviceInfo.Features.AsyncShaderCompilation)
{
glMaxShaderCompilerThreadsKHR(EngineCI.NumAsyncShaderCompilationThreads);
}
#endif
}
RenderDeviceGLImpl::~RenderDeviceGLImpl()
{
}
IMPLEMENT_QUERY_INTERFACE(RenderDeviceGLImpl, IID_RenderDeviceGL, TRenderDeviceBase)
void RenderDeviceGLImpl::CreateBuffer(const BufferDesc& BuffDesc, const BufferData* pBuffData, IBuffer** ppBuffer, bool bIsDeviceInternal)
{
RefCntAutoPtr<DeviceContextGLImpl> pDeviceContext = GetImmediateContext(0);
VERIFY(pDeviceContext, "Immediate device context has been destroyed");
CreateBufferImpl(ppBuffer, BuffDesc, std::ref(pDeviceContext->GetContextState()), pBuffData, bIsDeviceInternal);
}
void RenderDeviceGLImpl::CreateBuffer(const BufferDesc& BuffDesc, const BufferData* BuffData, IBuffer** ppBuffer)
{
CreateBuffer(BuffDesc, BuffData, ppBuffer, false);
}
void RenderDeviceGLImpl::CreateBufferFromGLHandle(Uint32 GLHandle, const BufferDesc& BuffDesc, RESOURCE_STATE InitialState, IBuffer** ppBuffer)
{
DEV_CHECK_ERR(GLHandle != 0, "GL buffer handle must not be null");
RefCntAutoPtr<DeviceContextGLImpl> pDeviceContext = GetImmediateContext(0);
VERIFY(pDeviceContext, "Immediate device context has been destroyed");
CreateBufferImpl(ppBuffer, BuffDesc, std::ref(pDeviceContext->GetContextState()), GLHandle, /*bIsDeviceInternal =*/false);
}
void RenderDeviceGLImpl::CreateShader(const ShaderCreateInfo& ShaderCreateInfo,
IShader** ppShader,
IDataBlob** ppCompilerOutput,
bool bIsDeviceInternal)
{
const ShaderGLImpl::CreateInfo GLShaderCI{
GetDeviceInfo(),
GetAdapterInfo(),
ppCompilerOutput,
};
CreateShaderImpl(ppShader, ShaderCreateInfo, GLShaderCI, bIsDeviceInternal);
}
void RenderDeviceGLImpl::CreateShader(const ShaderCreateInfo& ShaderCreateInfo,
IShader** ppShader,
IDataBlob** ppCompilerOutput)
{
CreateShader(ShaderCreateInfo, ppShader, ppCompilerOutput, false);
}
void RenderDeviceGLImpl::CreateTexture(const TextureDesc& TexDesc, const TextureData* pData, ITexture** ppTexture, bool bIsDeviceInternal)
{
CreateDeviceObject(
"texture", TexDesc, ppTexture,
[&]() //
{
RefCntAutoPtr<DeviceContextGLImpl> pDeviceContext = GetImmediateContext(0);
VERIFY(pDeviceContext, "Immediate device context has been destroyed");
GLContextState& GLState = pDeviceContext->GetContextState();
const TextureFormatInfo& FmtInfo = GetTextureFormatInfo(TexDesc.Format);
if (!FmtInfo.Supported)
{
LOG_ERROR_AND_THROW(FmtInfo.Name, " is not supported texture format");
}
TextureBaseGL* pTextureOGL = nullptr;
switch (TexDesc.Type)
{
case RESOURCE_DIM_TEX_1D:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "Texture1D_GL instance", Texture1D_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, pData, bIsDeviceInternal);
break;
case RESOURCE_DIM_TEX_1D_ARRAY:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "Texture1DArray_GL instance", Texture1DArray_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, pData, bIsDeviceInternal);
break;
case RESOURCE_DIM_TEX_2D:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "Texture2D_GL instance", Texture2D_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, pData, bIsDeviceInternal);
break;
case RESOURCE_DIM_TEX_2D_ARRAY:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "Texture2DArray_GL instance", Texture2DArray_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, pData, bIsDeviceInternal);
break;
case RESOURCE_DIM_TEX_3D:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "Texture3D_GL instance", Texture3D_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, pData, bIsDeviceInternal);
break;
case RESOURCE_DIM_TEX_CUBE:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "TextureCube_GL instance", TextureCube_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, pData, bIsDeviceInternal);
break;
case RESOURCE_DIM_TEX_CUBE_ARRAY:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "TextureCubeArray_GL instance", TextureCubeArray_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, pData, bIsDeviceInternal);
break;
default: LOG_ERROR_AND_THROW("Unknown texture type. (Did you forget to initialize the Type member of TextureDesc structure?)");
}
pTextureOGL->QueryInterface(IID_Texture, reinterpret_cast<IObject**>(ppTexture));
pTextureOGL->CreateDefaultViews();
} //
);
}
void RenderDeviceGLImpl::CreateTexture(const TextureDesc& TexDesc, const TextureData* Data, ITexture** ppTexture)
{
CreateTexture(TexDesc, Data, ppTexture, false);
}
void RenderDeviceGLImpl::CreateTextureFromGLHandle(Uint32 GLHandle,
Uint32 GLBindTarget,
const TextureDesc& TexDesc,
RESOURCE_STATE InitialState,
ITexture** ppTexture)
{
VERIFY(GLHandle, "GL texture handle must not be null");
CreateDeviceObject(
"texture", TexDesc, ppTexture,
[&]() //
{
RefCntAutoPtr<DeviceContextGLImpl> pDeviceContext = GetImmediateContext(0);
VERIFY(pDeviceContext, "Immediate device context has been destroyed");
GLContextState& GLState = pDeviceContext->GetContextState();
TextureBaseGL* pTextureOGL = nullptr;
switch (TexDesc.Type)
{
case RESOURCE_DIM_TEX_1D:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "Texture1D_GL instance", Texture1D_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, GLHandle, GLBindTarget);
break;
case RESOURCE_DIM_TEX_1D_ARRAY:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "Texture1DArray_GL instance", Texture1DArray_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, GLHandle, GLBindTarget);
break;
case RESOURCE_DIM_TEX_2D:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "Texture2D_GL instance", Texture2D_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, GLHandle, GLBindTarget);
break;
case RESOURCE_DIM_TEX_2D_ARRAY:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "Texture2DArray_GL instance", Texture2DArray_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, GLHandle, GLBindTarget);
break;
case RESOURCE_DIM_TEX_3D:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "Texture3D_GL instance", Texture3D_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, GLHandle, GLBindTarget);
break;
case RESOURCE_DIM_TEX_CUBE:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "TextureCube_GL instance", TextureCube_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, GLHandle, GLBindTarget);
break;
case RESOURCE_DIM_TEX_CUBE_ARRAY:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "TextureCubeArray_GL instance", TextureCubeArray_GL)(m_TexViewObjAllocator, this, GLState, TexDesc, GLHandle, GLBindTarget);
break;
default: LOG_ERROR_AND_THROW("Unknown texture type. (Did you forget to initialize the Type member of TextureDesc structure?)");
}
pTextureOGL->QueryInterface(IID_Texture, reinterpret_cast<IObject**>(ppTexture));
pTextureOGL->CreateDefaultViews();
} //
);
}
void RenderDeviceGLImpl::CreateDummyTexture(const TextureDesc& TexDesc, RESOURCE_STATE InitialState, ITexture** ppTexture)
{
CreateDeviceObject(
"texture", TexDesc, ppTexture,
[&]() //
{
TextureBaseGL* pTextureOGL = nullptr;
switch (TexDesc.Type)
{
case RESOURCE_DIM_TEX_2D:
pTextureOGL = NEW_RC_OBJ(m_TexObjAllocator, "Dummy Texture2D_GL instance", Texture2D_GL)(m_TexViewObjAllocator, this, TexDesc);
break;
default: LOG_ERROR_AND_THROW("Unsupported texture type.");
}
pTextureOGL->QueryInterface(IID_Texture, reinterpret_cast<IObject**>(ppTexture));
pTextureOGL->CreateDefaultViews();
} //
);
}
void RenderDeviceGLImpl::CreateSampler(const SamplerDesc& SamplerDesc, ISampler** ppSampler, bool bIsDeviceInternal)
{
CreateSamplerImpl(ppSampler, SamplerDesc, bIsDeviceInternal);
}
void RenderDeviceGLImpl::CreateSampler(const SamplerDesc& SamplerDesc, ISampler** ppSampler)
{
CreateSampler(SamplerDesc, ppSampler, false);
}
void RenderDeviceGLImpl::CreateGraphicsPipelineState(const GraphicsPipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState, bool bIsDeviceInternal)
{
CreatePipelineStateImpl(ppPipelineState, PSOCreateInfo, bIsDeviceInternal);
}
void RenderDeviceGLImpl::CreateComputePipelineState(const ComputePipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState, bool bIsDeviceInternal)
{
CreatePipelineStateImpl(ppPipelineState, PSOCreateInfo, bIsDeviceInternal);
}
void RenderDeviceGLImpl::CreateGraphicsPipelineState(const GraphicsPipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState)
{
CreatePipelineStateImpl(ppPipelineState, PSOCreateInfo, false);
}
void RenderDeviceGLImpl::CreateComputePipelineState(const ComputePipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState)
{
CreatePipelineStateImpl(ppPipelineState, PSOCreateInfo, false);
}
void RenderDeviceGLImpl::CreateRayTracingPipelineState(const RayTracingPipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState)
{
UNSUPPORTED("Ray tracing is not supported in OpenGL");
*ppPipelineState = nullptr;
}
void RenderDeviceGLImpl::CreateFence(const FenceDesc& Desc, IFence** ppFence)
{
CreateFenceImpl(ppFence, Desc);
}
void RenderDeviceGLImpl::CreateQuery(const QueryDesc& Desc, IQuery** ppQuery)
{
CreateQueryImpl(ppQuery, Desc);
}
void RenderDeviceGLImpl::CreateRenderPass(const RenderPassDesc& Desc, IRenderPass** ppRenderPass)
{
CreateRenderPassImpl(ppRenderPass, Desc);
}
void RenderDeviceGLImpl::CreateFramebuffer(const FramebufferDesc& Desc, IFramebuffer** ppFramebuffer)
{
RefCntAutoPtr<DeviceContextGLImpl> pDeviceContext = GetImmediateContext(0);
VERIFY(pDeviceContext, "Immediate device context has been destroyed");
GLContextState& GLState = pDeviceContext->GetContextState();
CreateFramebufferImpl(ppFramebuffer, Desc, std::ref(GLState));
}
void RenderDeviceGLImpl::CreatePipelineResourceSignature(const PipelineResourceSignatureDesc& Desc,
IPipelineResourceSignature** ppSignature)
{
CreatePipelineResourceSignature(Desc, ppSignature, SHADER_TYPE_UNKNOWN, false);
}
void RenderDeviceGLImpl::CreatePipelineResourceSignature(const PipelineResourceSignatureDesc& Desc,
IPipelineResourceSignature** ppSignature,
SHADER_TYPE ShaderStages,
bool IsDeviceInternal)
{
CreatePipelineResourceSignatureImpl(ppSignature, Desc, ShaderStages, IsDeviceInternal);
}
void RenderDeviceGLImpl::CreatePipelineResourceSignature(const PipelineResourceSignatureDesc& Desc,
const PipelineResourceSignatureInternalDataGL& InternalData,
IPipelineResourceSignature** ppSignature)
{
CreatePipelineResourceSignatureImpl(ppSignature, Desc, InternalData);
}
void RenderDeviceGLImpl::CreateBLAS(const BottomLevelASDesc& Desc,
IBottomLevelAS** ppBLAS)
{
UNSUPPORTED("CreateBLAS is not supported in OpenGL");
*ppBLAS = nullptr;
}
void RenderDeviceGLImpl::CreateTLAS(const TopLevelASDesc& Desc,
ITopLevelAS** ppTLAS)
{
UNSUPPORTED("CreateTLAS is not supported in OpenGL");
*ppTLAS = nullptr;
}
void RenderDeviceGLImpl::CreateSBT(const ShaderBindingTableDesc& Desc,
IShaderBindingTable** ppSBT)
{
UNSUPPORTED("CreateSBT is not supported in OpenGL");
*ppSBT = nullptr;
}
void RenderDeviceGLImpl::CreateDeviceMemory(const DeviceMemoryCreateInfo& CreateInfo, IDeviceMemory** ppMemory)
{
UNSUPPORTED("CreateDeviceMemory is not supported in OpenGL");
*ppMemory = nullptr;
}
void RenderDeviceGLImpl::CreatePipelineStateCache(const PipelineStateCacheCreateInfo& CreateInfo,
IPipelineStateCache** ppPSOCache)
{
*ppPSOCache = nullptr;
}
void RenderDeviceGLImpl::CreateDeferredContext(IDeviceContext** ppContext)
{
LOG_ERROR_MESSAGE("Deferred contexts are not supported in OpenGL backend.");
*ppContext = nullptr;
}
SparseTextureFormatInfo RenderDeviceGLImpl::GetSparseTextureFormatInfo(TEXTURE_FORMAT TexFormat,
RESOURCE_DIMENSION Dimension,
Uint32 SampleCount) const
{
UNSUPPORTED("GetSparseTextureFormatInfo is not supported in OpenGL");
return {};
}
bool RenderDeviceGLImpl::CheckExtension(const Char* ExtensionString) const
{
return m_ExtensionStrings.find(ExtensionString) != m_ExtensionStrings.end();
}
void RenderDeviceGLImpl::InitAdapterInfo()
{
const Version GLVersion = m_DeviceInfo.APIVersion;
// Set graphics adapter properties
{
const std::string glstrVendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
const std::string glstrRenderer = reinterpret_cast<const char*>(glGetString(GL_RENDERER));
const std::string Vendor = StrToLower(glstrVendor);
LOG_INFO_MESSAGE("GPU Vendor: ", Vendor);
LOG_INFO_MESSAGE("GPU Renderer: ", glstrRenderer);
for (size_t i = 0; i < _countof(m_AdapterInfo.Description) - 1 && i < glstrRenderer.length(); ++i)
m_AdapterInfo.Description[i] = glstrRenderer[i];
m_AdapterInfo.Type = ADAPTER_TYPE_UNKNOWN;
m_AdapterInfo.VendorId = 0;
m_AdapterInfo.DeviceId = 0;
m_AdapterInfo.NumOutputs = 0;
if (Vendor.find("intel") != std::string::npos)
m_AdapterInfo.Vendor = ADAPTER_VENDOR_INTEL;
else if (Vendor.find("nvidia") != std::string::npos)
m_AdapterInfo.Vendor = ADAPTER_VENDOR_NVIDIA;
else if (Vendor.find("ati") != std::string::npos ||
Vendor.find("amd") != std::string::npos)
m_AdapterInfo.Vendor = ADAPTER_VENDOR_AMD;
else if (Vendor.find("qualcomm") != std::string::npos)
m_AdapterInfo.Vendor = ADAPTER_VENDOR_QUALCOMM;
else if (Vendor.find("arm") != std::string::npos)
m_AdapterInfo.Vendor = ADAPTER_VENDOR_ARM;
else if (Vendor.find("microsoft") != std::string::npos)
m_AdapterInfo.Vendor = ADAPTER_VENDOR_MSFT;
else if (Vendor.find("apple") != std::string::npos)
m_AdapterInfo.Vendor = ADAPTER_VENDOR_APPLE;
else if (Vendor.find("mesa") != std::string::npos)
m_AdapterInfo.Vendor = ADAPTER_VENDOR_MESA;
else if (Vendor.find("broadcom") != std::string::npos)
m_AdapterInfo.Vendor = ADAPTER_VENDOR_BROADCOM;
else
m_AdapterInfo.Vendor = ADAPTER_VENDOR_UNKNOWN;
}
// Set memory properties
{
AdapterMemoryInfo& Mem = m_AdapterInfo.Memory;
switch (m_AdapterInfo.Vendor)
{
case ADAPTER_VENDOR_NVIDIA:
{
#ifndef GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX
static constexpr GLenum GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX = 0x9048;
#endif
GLint AvailableMemoryKb = 0;
glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX, &AvailableMemoryKb);
if (glGetError() == GL_NO_ERROR)
{
Mem.LocalMemory = static_cast<Uint64>(AvailableMemoryKb) * Uint64{1024};
}
else
{
LOG_WARNING_MESSAGE("Unable to read available memory size for NVidia GPU");
}
}
break;
case ADAPTER_VENDOR_AMD:
{
#ifndef GL_TEXTURE_FREE_MEMORY_ATI
static constexpr GLenum GL_TEXTURE_FREE_MEMORY_ATI = 0x87FC;
#endif
// https://www.khronos.org/registry/OpenGL/extensions/ATI/ATI_meminfo.txt
// param[0] - total memory free in the pool
// param[1] - largest available free block in the pool
// param[2] - total auxiliary memory free
// param[3] - largest auxiliary free block
GLint MemoryParamsKb[4] = {};
glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, MemoryParamsKb);
if (glGetError() == GL_NO_ERROR)
{
Mem.LocalMemory = static_cast<Uint64>(MemoryParamsKb[0]) * Uint64{1024};
}
else
{
LOG_WARNING_MESSAGE("Unable to read free memory size for AMD GPU");
}
}
break;
default:
// No way to get memory info
break;
}
}
// Enable features and set properties
{
#define ENABLE_FEATURE(FeatureName, Supported) \
Features.FeatureName = (Supported) ? DEVICE_FEATURE_STATE_ENABLED : DEVICE_FEATURE_STATE_DISABLED;
DeviceFeatures& Features = m_AdapterInfo.Features;
GLint MaxTextureSize = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &MaxTextureSize);
CHECK_GL_ERROR("Failed to get maximum texture size");
GLint Max3DTextureSize = 0;
glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &Max3DTextureSize);
CHECK_GL_ERROR("Failed to get maximum 3d texture size");
GLint MaxCubeTextureSize = 0;
glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &MaxCubeTextureSize);
CHECK_GL_ERROR("Failed to get maximum cubemap texture size");
GLint MaxLayers = 0;
glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &MaxLayers);
CHECK_GL_ERROR("Failed to get maximum number of texture array layers");
Features.MeshShaders = DEVICE_FEATURE_STATE_DISABLED;
Features.RayTracing = DEVICE_FEATURE_STATE_DISABLED;
Features.ShaderResourceStaticArrays = DEVICE_FEATURE_STATE_ENABLED;
Features.ShaderResourceRuntimeArrays = DEVICE_FEATURE_STATE_DISABLED;
Features.InstanceDataStepRate = DEVICE_FEATURE_STATE_ENABLED;
Features.NativeFence = DEVICE_FEATURE_STATE_DISABLED;
Features.TileShaders = DEVICE_FEATURE_STATE_DISABLED;
Features.SubpassFramebufferFetch = DEVICE_FEATURE_STATE_DISABLED;
Features.TextureComponentSwizzle = DEVICE_FEATURE_STATE_DISABLED;
{
bool WireframeFillSupported = (glPolygonMode != nullptr);
if (WireframeFillSupported)
{
// Test glPolygonMode() function to check if it fails
// (It does fail on NVidia Shield tablet, but works fine
// on Intel hw)
VERIFY(glGetError() == GL_NO_ERROR, "Unhandled gl error encountered");
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (glGetError() != GL_NO_ERROR)
WireframeFillSupported = false;
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
if (glGetError() != GL_NO_ERROR)
WireframeFillSupported = false;
}
ENABLE_FEATURE(WireframeFill, WireframeFillSupported);
}
{
GLint MaxVertexSSBOs = 0;
#if GL_ARB_shader_storage_buffer_object
bool IsGL43OrAbove = (m_DeviceInfo.Type == RENDER_DEVICE_TYPE_GL) && (GLVersion >= Version{4, 3});
bool IsGLES31OrAbove = (m_DeviceInfo.Type == RENDER_DEVICE_TYPE_GLES) && (GLVersion >= Version{3, 1});
if (IsGL43OrAbove || IsGLES31OrAbove)
{
glGetIntegerv(GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS, &MaxVertexSSBOs);
CHECK_GL_ERROR("glGetIntegerv(GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS)");
}
#endif
ENABLE_FEATURE(VertexPipelineUAVWritesAndAtomics, MaxVertexSSBOs);
}
TextureProperties& TexProps = m_AdapterInfo.Texture;
SamplerProperties& SamProps = m_AdapterInfo.Sampler;
if (m_DeviceInfo.Type == RENDER_DEVICE_TYPE_GL)
{
const bool IsGL46OrAbove = GLVersion >= Version{4, 6};
const bool IsGL43OrAbove = GLVersion >= Version{4, 3};
const bool IsGL42OrAbove = GLVersion >= Version{4, 2};
const bool IsGL41OrAbove = GLVersion >= Version{4, 1};
const bool IsGL40OrAbove = GLVersion >= Version{4, 0};
// Separable programs may be disabled
Features.SeparablePrograms = DEVICE_FEATURE_STATE_OPTIONAL;
// clang-format off
ENABLE_FEATURE(WireframeFill, true);
ENABLE_FEATURE(MultithreadedResourceCreation, false);
ENABLE_FEATURE(ComputeShaders, IsGL43OrAbove || CheckExtension("GL_ARB_compute_shader"));
ENABLE_FEATURE(GeometryShaders, IsGL40OrAbove || CheckExtension("GL_ARB_geometry_shader4"));
ENABLE_FEATURE(Tessellation, IsGL40OrAbove || CheckExtension("GL_ARB_tessellation_shader"));
ENABLE_FEATURE(BindlessResources, false);
ENABLE_FEATURE(OcclusionQueries, true); // Present since 3.3
ENABLE_FEATURE(BinaryOcclusionQueries, true); // Present since 3.3
ENABLE_FEATURE(TimestampQueries, true); // Present since 3.3
ENABLE_FEATURE(PipelineStatisticsQueries, true); // Present since 3.3
ENABLE_FEATURE(DurationQueries, true); // Present since 3.3
ENABLE_FEATURE(DepthBiasClamp, false); // There is no depth bias clamp in OpenGL
ENABLE_FEATURE(DepthClamp, IsGL40OrAbove || CheckExtension("GL_ARB_depth_clamp"));
ENABLE_FEATURE(IndependentBlend, true);
ENABLE_FEATURE(DualSourceBlend, IsGL41OrAbove || CheckExtension("GL_ARB_blend_func_extended"));
ENABLE_FEATURE(MultiViewport, IsGL41OrAbove || CheckExtension("GL_ARB_viewport_array"));
ENABLE_FEATURE(PixelUAVWritesAndAtomics, IsGL42OrAbove || CheckExtension("GL_ARB_shader_image_load_store"));
ENABLE_FEATURE(TextureUAVExtendedFormats, false);
ENABLE_FEATURE(ShaderFloat16, CheckExtension("GL_EXT_shader_explicit_arithmetic_types_float16"));
ENABLE_FEATURE(ResourceBuffer16BitAccess, CheckExtension("GL_EXT_shader_16bit_storage"));
ENABLE_FEATURE(UniformBuffer16BitAccess, CheckExtension("GL_EXT_shader_16bit_storage"));
ENABLE_FEATURE(ShaderInputOutput16, false);
ENABLE_FEATURE(ShaderInt8, CheckExtension("GL_EXT_shader_explicit_arithmetic_types_int8"));
ENABLE_FEATURE(ResourceBuffer8BitAccess, CheckExtension("GL_EXT_shader_8bit_storage"));
ENABLE_FEATURE(UniformBuffer8BitAccess, CheckExtension("GL_EXT_shader_8bit_storage"));
ENABLE_FEATURE(TextureComponentSwizzle, IsGL46OrAbove || CheckExtension("GL_ARB_texture_swizzle"));
ENABLE_FEATURE(TextureSubresourceViews, IsGL43OrAbove || CheckExtension("GL_ARB_texture_view"));
ENABLE_FEATURE(NativeMultiDraw, IsGL46OrAbove || CheckExtension("GL_ARB_shader_draw_parameters")); // Requirements for gl_DrawID
ENABLE_FEATURE(AsyncShaderCompilation, CheckExtension("GL_KHR_parallel_shader_compile"));
ENABLE_FEATURE(FormattedBuffers, IsGL40OrAbove);
// clang-format on
TexProps.MaxTexture1DDimension = MaxTextureSize;
TexProps.MaxTexture1DArraySlices = MaxLayers;
TexProps.MaxTexture2DDimension = MaxTextureSize;
TexProps.MaxTexture2DArraySlices = MaxLayers;
TexProps.MaxTexture3DDimension = Max3DTextureSize;
TexProps.MaxTextureCubeDimension = MaxCubeTextureSize;
TexProps.Texture2DMSSupported = IsGL43OrAbove || CheckExtension("GL_ARB_texture_storage_multisample");
TexProps.Texture2DMSArraySupported = IsGL43OrAbove || CheckExtension("GL_ARB_texture_storage_multisample");
TexProps.TextureViewSupported = IsGL43OrAbove || CheckExtension("GL_ARB_texture_view");
TexProps.CubemapArraysSupported = IsGL43OrAbove || CheckExtension("GL_ARB_texture_cube_map_array");
TexProps.TextureView2DOn3DSupported = TexProps.TextureViewSupported;
ASSERT_SIZEOF(TexProps, 32, "Did you add a new member to TextureProperites? Please initialize it here.");
SamProps.BorderSamplingModeSupported = True;
if (IsGL46OrAbove || CheckExtension("GL_ARB_texture_filter_anisotropic"))
{
GLint MaxAnisotropy = 0;
glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &MaxAnisotropy);
CHECK_GL_ERROR("glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY)");
SamProps.MaxAnisotropy = static_cast<Uint8>(MaxAnisotropy);
}
SamProps.LODBiasSupported = True;
ASSERT_SIZEOF(SamProps, 3, "Did you add a new member to SamplerProperites? Please initialize it here.");
m_GLCaps.FramebufferSRGB = IsGL40OrAbove || CheckExtension("GL_ARB_framebuffer_sRGB");
m_GLCaps.SemalessCubemaps = IsGL40OrAbove || CheckExtension("GL_ARB_seamless_cube_map");
}
else
{
VERIFY(m_DeviceInfo.Type == RENDER_DEVICE_TYPE_GLES, "Unexpected device type: OpenGLES expected");
const char* Extensions = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
LOG_INFO_MESSAGE("Supported extensions: \n", Extensions);
const bool IsGLES31OrAbove = GLVersion >= Version{3, 1};
const bool IsGLES32OrAbove = GLVersion >= Version{3, 2};
// Separable programs may be disabled
Features.SeparablePrograms = (IsGLES31OrAbove || strstr(Extensions, "separate_shader_objects")) ? DEVICE_FEATURE_STATE_OPTIONAL : DEVICE_FEATURE_STATE_DISABLED;
// clang-format off
ENABLE_FEATURE(WireframeFill, false);
ENABLE_FEATURE(MultithreadedResourceCreation, false);
ENABLE_FEATURE(ComputeShaders, IsGLES31OrAbove || strstr(Extensions, "compute_shader"));
ENABLE_FEATURE(GeometryShaders, IsGLES32OrAbove || strstr(Extensions, "geometry_shader"));
ENABLE_FEATURE(Tessellation, IsGLES32OrAbove || strstr(Extensions, "tessellation_shader"));
ENABLE_FEATURE(BindlessResources, false);
ENABLE_FEATURE(OcclusionQueries, false);
ENABLE_FEATURE(BinaryOcclusionQueries, true); // Supported in GLES3.0
#if GL_TIMESTAMP
const bool DisjointTimerQueriesSupported = strstr(Extensions, "disjoint_timer_query");
ENABLE_FEATURE(TimestampQueries, DisjointTimerQueriesSupported);
ENABLE_FEATURE(DurationQueries, DisjointTimerQueriesSupported);
#else
ENABLE_FEATURE(TimestampQueries, false);
ENABLE_FEATURE(DurationQueries, false);
#endif
ENABLE_FEATURE(PipelineStatisticsQueries, false);
ENABLE_FEATURE(DepthBiasClamp, false); // There is no depth bias clamp in OpenGL
ENABLE_FEATURE(DepthClamp, strstr(Extensions, "depth_clamp"));
ENABLE_FEATURE(IndependentBlend, IsGLES32OrAbove);
ENABLE_FEATURE(DualSourceBlend, strstr(Extensions, "blend_func_extended"));
ENABLE_FEATURE(MultiViewport, strstr(Extensions, "viewport_array"));
ENABLE_FEATURE(PixelUAVWritesAndAtomics, IsGLES31OrAbove || strstr(Extensions, "shader_image_load_store"));
ENABLE_FEATURE(TextureUAVExtendedFormats, false);
ENABLE_FEATURE(ShaderFloat16, strstr(Extensions, "shader_explicit_arithmetic_types_float16"));
ENABLE_FEATURE(ResourceBuffer16BitAccess, strstr(Extensions, "shader_16bit_storage"));
ENABLE_FEATURE(UniformBuffer16BitAccess, strstr(Extensions, "shader_16bit_storage"));
ENABLE_FEATURE(ShaderInputOutput16, false);
ENABLE_FEATURE(ShaderInt8, strstr(Extensions, "shader_explicit_arithmetic_types_int8"));
ENABLE_FEATURE(ResourceBuffer8BitAccess, strstr(Extensions, "shader_8bit_storage"));
ENABLE_FEATURE(UniformBuffer8BitAccess, strstr(Extensions, "shader_8bit_storage"));
ENABLE_FEATURE(TextureComponentSwizzle, true);
ENABLE_FEATURE(TextureSubresourceViews, strstr(Extensions, "texture_view"));
ENABLE_FEATURE(NativeMultiDraw, strstr(Extensions, "multi_draw"));
ENABLE_FEATURE(AsyncShaderCompilation, strstr(Extensions, "parallel_shader_compile"));
ENABLE_FEATURE(FormattedBuffers, IsGLES32OrAbove);
// clang-format on
TexProps.MaxTexture1DDimension = 0; // Not supported in GLES 3.2
TexProps.MaxTexture1DArraySlices = 0; // Not supported in GLES 3.2
TexProps.MaxTexture2DDimension = MaxTextureSize;
TexProps.MaxTexture2DArraySlices = MaxLayers;
TexProps.MaxTexture3DDimension = Max3DTextureSize;
TexProps.MaxTextureCubeDimension = MaxCubeTextureSize;
TexProps.Texture2DMSSupported = IsGLES31OrAbove || strstr(Extensions, "texture_storage_multisample");
TexProps.Texture2DMSArraySupported = IsGLES32OrAbove || strstr(Extensions, "texture_storage_multisample_2d_array");
TexProps.TextureViewSupported = IsGLES31OrAbove || strstr(Extensions, "texture_view");
TexProps.CubemapArraysSupported = IsGLES32OrAbove || strstr(Extensions, "texture_cube_map_array");
TexProps.TextureView2DOn3DSupported = TexProps.TextureViewSupported;
ASSERT_SIZEOF(TexProps, 32, "Did you add a new member to TextureProperites? Please initialize it here.");
SamProps.BorderSamplingModeSupported = GL_TEXTURE_BORDER_COLOR && (IsGLES32OrAbove || strstr(Extensions, "texture_border_clamp"));
if (strstr(Extensions, "texture_filter_anisotropic"))
{
GLint MaxAnisotropy = 0;
glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &MaxAnisotropy);
CHECK_GL_ERROR("glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY)");
SamProps.MaxAnisotropy = static_cast<Uint8>(MaxAnisotropy);
}
SamProps.LODBiasSupported = GL_TEXTURE_LOD_BIAS && IsGLES31OrAbove;
ASSERT_SIZEOF(SamProps, 3, "Did you add a new member to SamplerProperites? Please initialize it here.");
m_GLCaps.FramebufferSRGB = strstr(Extensions, "sRGB_write_control");
m_GLCaps.SemalessCubemaps = false;
}
#ifdef GL_KHR_shader_subgroup
if (CheckExtension("GL_KHR_shader_subgroup"))
{
GLint SubgroupSize = 0;
glGetIntegerv(GL_SUBGROUP_SIZE_KHR, &SubgroupSize);
CHECK_GL_ERROR("glGetIntegerv(GL_SUBGROUP_SIZE_KHR)");
GLint SubgroupStages = 0;
glGetIntegerv(GL_SUBGROUP_SUPPORTED_STAGES_KHR, &SubgroupStages);
CHECK_GL_ERROR("glGetIntegerv(GL_SUBGROUP_SUPPORTED_STAGES_KHR)");
GLint SubgroupFeatures = 0;