Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLRhiCapabilities.cpp

raw
1 #define NCINE_INCLUDE_OPENGL
2 #include "../../../CommonHeaders.h"
3
4 #include "GLRhiCapabilities.h"
5
6 #include <cstdio> // for sscanf()
7 #include <cstring> // for CheckGLExtensions()
8
9 #include <Containers/ArrayView.h>
10
11 using namespace Death::Containers;
12
13 namespace nCine::RHI::GL
14 {
15 GLRhiCapabilities::GLRhiCapabilities()
16 {
17 const char* version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
18
19 if (version != nullptr) {
20 #if defined(RHI_GL_PROFILE_ES) || defined(DEATH_TARGET_EMSCRIPTEN)
21 # if defined(DEATH_TARGET_MSVC)
22 sscanf_s(version, "OpenGL ES %2d.%2d", &_majorVersion, &_minorVersion);
23 # else
24 sscanf(version, "OpenGL ES %2d.%2d", &_majorVersion, &_minorVersion);
25 # endif
26 #else
27 # if defined(DEATH_TARGET_MSVC)
28 sscanf_s(version, "%2d.%2d.%2d", &_majorVersion, &_minorVersion, &_releaseVersion);
29 # else
30 sscanf(version, "%2d.%2d.%2d", &_majorVersion, &_minorVersion, &_releaseVersion);
31 # endif
32 #endif
33 }
34
35 _infoStrings.vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
36 _infoStrings.renderer = reinterpret_cast<const char*>(glGetString(GL_RENDERER));
37 _infoStrings.apiVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
38 _infoStrings.shadingLanguageVersion = reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION));
39
40 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_intValues[(std::int32_t)IntValues::MaxTextureSize]);
41 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &_intValues[(std::int32_t)IntValues::MaxTextureImageUnits]);
42 #if defined(RHI_GL_PROFILE_ES2)
43 // ES2 has no uniform buffer objects, MRT or the related glGet enums (all ES 3.0) - querying them would
44 // raise GL_INVALID_ENUM and leave the values at 0 (a zero UNIFORM_BUFFER_OFFSET_ALIGNMENT would even
45 // divide-by-zero in RenderBuffersManager). Publish safe synthetic values for the pipeline code that reads
46 // them before its ES2 arms skip the UBO work, and log the real ES2 uniform-vector limits instead
47 _intValues[(std::int32_t)IntValues::MaxUniformBlockSize] = 16 * 1024;
48 _intValues[(std::int32_t)IntValues::MaxUniformBlockSizeNormalized] = 16 * 1024;
49 _intValues[(std::int32_t)IntValues::MaxUniformBufferBindings] = 0;
50 _intValues[(std::int32_t)IntValues::MaxVertexUniformBlocks] = 0;
51 _intValues[(std::int32_t)IntValues::MaxFragmentUniformBlocks] = 0;
52 _intValues[(std::int32_t)IntValues::UniformBufferOffsetAlignment] = 16;
53 _intValues[(std::int32_t)IntValues::MaxColorAttachments] = 1;
54
55 GLint maxVertexUniformVectors = 0, maxFragmentUniformVectors = 0, maxVaryingVectors = 0;
56 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &maxVertexUniformVectors);
57 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &maxFragmentUniformVectors);
58 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryingVectors);
59 LOGI("GL_MAX_VERTEX_UNIFORM_VECTORS: {}, GL_MAX_FRAGMENT_UNIFORM_VECTORS: {}, GL_MAX_VARYING_VECTORS: {}",
60 maxVertexUniformVectors, maxFragmentUniformVectors, maxVaryingVectors);
61 #else
62 glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &_intValues[(std::int32_t)IntValues::MaxUniformBlockSize]);
63 glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &_intValues[(std::int32_t)IntValues::MaxUniformBufferBindings]);
64 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_BLOCKS, &_intValues[(std::int32_t)IntValues::MaxVertexUniformBlocks]);
65 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_BLOCKS, &_intValues[(std::int32_t)IntValues::MaxFragmentUniformBlocks]);
66 glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &_intValues[(std::int32_t)IntValues::UniformBufferOffsetAlignment]);
67 # if !defined(DEATH_TARGET_EMSCRIPTEN) && !(defined(DEATH_TARGET_APPLE) && defined(DEATH_TARGET_ARM)) && (defined(RHI_GL_PROFILE_CORE) || GL_ES_VERSION_3_1)
68 glGetIntegerv(GL_MAX_VERTEX_ATTRIB_STRIDE, &_intValues[(std::int32_t)IntValues::MaxVertexAttribStride]);
69 # endif
70 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &_intValues[(std::int32_t)IntValues::MaxColorAttachments]);
71
72 // MAX_UNIFORM_BLOCK_SIZE is sometimes not reported correctly, so try to adjust it here
73 NormalizeUniformBlockSize();
74 #endif
75
76 // Targets whose batch size is a deliberate choice rather than a consequence of the block budget: the
77 // batch a 64 KB block allows is far more per-draw uniform data than these drivers want to move, and
78 // on ANGLE it is also what the D3D backend behind it has to translate every frame. Stated here rather
79 // than in AppConfiguration so the number sits with the device it describes (see IntValues::MaxBatchSize).
80 #if defined(DEATH_TARGET_WINDOWS_RT)
81 _intValues[(std::int32_t)IntValues::MaxBatchSize] = 24;
82 #elif defined(DEATH_TARGET_EMSCRIPTEN) || defined(WITH_ANGLE)
83 _intValues[(std::int32_t)IntValues::MaxBatchSize] = 10;
84 #endif
85
86 const char* ExtensionNames[] = {
87 "GL_KHR_debug", "GL_ARB_texture_storage", "GL_ARB_buffer_storage", "GL_ARB_get_program_binary",
88 #if defined(RHI_GL_PROFILE_ES) && !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_SWITCH) && !defined(DEATH_TARGET_UNIX)
89 "GL_OES_get_program_binary",
90 #endif
91 #if defined(DEATH_TARGET_EMSCRIPTEN)
92 "WEBGL_compressed_texture_s3tc", "WEBGL_compressed_texture_atc", "WEBGL_compressed_texture_pvrtc", "WEBGL_compressed_texture_astc"
93 #else
94 "GL_EXT_texture_compression_s3tc", "GL_AMD_compressed_ATC_texture", "GL_IMG_texture_compression_pvrtc", "GL_KHR_texture_compression_astc_ldr"
95 #endif
96 #if defined(DEATH_TARGET_EMSCRIPTEN)
97 , "WEBGL_compressed_texture_etc1",
98 #elif defined(RHI_GL_PROFILE_ES)
99 , "GL_OES_compressed_ETC1_RGB8_texture",
100 #endif
101 };
102 static_assert(std::int32_t(arraySize(ExtensionNames)) == (std::int32_t)Extensions::Count, "Extensions count mismatch");
103
104 CheckGLExtensions(ExtensionNames, _extensions, (std::int32_t)Extensions::Count);
105
106 // PS Vita's vitaGL exposes no program-binary format enums (neither the ARB `GL_PROGRAM_BINARY_FORMATS` nor
107 // the OES spelling) and does not support shader binary caching, so this query is skipped there,
108 // NUM_PROGRAM_BINARY_FORMATS then stays 0 and the cache stays off.
109 #if !defined(DEATH_TARGET_VITA)
110 # if defined(RHI_GL_PROFILE_ES) && !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_SWITCH) && !defined(DEATH_TARGET_UNIX)
111 if (HasExtension(Extensions::OesGetProgramBinary)) {
112 glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS_OES, &_intValues[(std::int32_t)IntValues::NumProgramBinaryFormats]);
113 DEATH_ASSERT(_intValues[(std::int32_t)IntValues::NumProgramBinaryFormats] <= MaxProgramBinaryFormats);
114 glGetIntegerv(GL_PROGRAM_BINARY_FORMATS_OES, _programBinaryFormats);
115 } else
116 # endif
117 if (HasExtension(Extensions::ArbGetProgramBinary)) {
118 glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &_intValues[(std::int32_t)IntValues::NumProgramBinaryFormats]);
119 DEATH_ASSERT(_intValues[(std::int32_t)IntValues::NumProgramBinaryFormats] <= MaxProgramBinaryFormats);
120 glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, _programBinaryFormats);
121 }
122 #endif
123
124 // The device info and the limits themselves are reported the same way by every backend
125 LogCapabilities();
126
127 #if defined(DEATH_TRACE)
128 // Program binary formats and extension availability, both of which only this backend has. A zero
129 // format count is worth logging rather than hiding, because it is why BinaryShaderCache stays off.
130 LOGI("Program binary formats: {}", _intValues[(std::int32_t)IntValues::NumProgramBinaryFormats]);
131 LOGI("GL_KHR_debug: {}", _extensions[(std::int32_t)Extensions::KhrDebug]);
132 LOGI("GL_ARB_texture_storage: {}", _extensions[(std::int32_t)Extensions::ArbTextureStorage]);
133 LOGI("GL_ARB_buffer_storage: {}", _extensions[(std::int32_t)Extensions::ArbBufferStorage]);
134 LOGI("GL_ARB_get_program_binary: {}", _extensions[(std::int32_t)Extensions::ArbGetProgramBinary]);
135 # if defined(RHI_GL_PROFILE_ES) && !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_SWITCH) && !defined(DEATH_TARGET_UNIX)
136 LOGI("GL_OES_get_program_binary: {}", _extensions[(std::int32_t)Extensions::OesGetProgramBinary]);
137 # endif
138 LOGI("GL_EXT_texture_compression_s3tc: {}", _extensions[(std::int32_t)Extensions::ExtTextureCompressionS3tc]);
139 # if defined(RHI_GL_PROFILE_ES) || defined(DEATH_TARGET_EMSCRIPTEN)
140 LOGI("GL_OES_compressed_ETC1_RGB8_texture: {}", _extensions[(std::int32_t)Extensions::OesCompressedEtc1Rgb8Texture]);
141 # endif
142 LOGI("GL_AMD_compressed_ATC_texture: {}", _extensions[(std::int32_t)Extensions::AmdCompressedAtcTexture]);
143 LOGI("GL_IMG_texture_compression_pvrtc: {}", _extensions[(std::int32_t)Extensions::ImgTextureCompressionPvrtc]);
144 LOGI("GL_KHR_texture_compression_astc_ldr: {}", _extensions[(std::int32_t)Extensions::KhrTextureCompressionAstcLdr]);
145 LOGI("---");
146
147 // Every extension the driver advertises, not just the ones the engine looks for
148 /*GLint numExtensions;
149 glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
150
151 LOGI("--- OpenGL extensions ---");
152 for (GLuint i = 0; i < static_cast<GLuint>(numExtensions); i++) {
153 LOGI("Extension {}: {}", i, glGetStringi(GL_EXTENSIONS, i));
154 }
155 LOGI("--- OpenGL extensions ---");*/
156 #endif
157 }
158
159 void GLRhiCapabilities::CheckGLExtensions(const char* extensionNames[], bool results[], std::uint32_t numExtensionsToCheck) const
160 {
161 #if defined(RHI_GL_PROFILE_ES2)
162 // ES2 has neither GL_NUM_EXTENSIONS nor glGetStringi() (both ES 3.0) - the extension list is the classic
163 // single space-separated glGetString(GL_EXTENSIONS) string, matched here token by token
164 const char* extensions = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
165 if (extensions == nullptr) {
166 return;
167 }
168
169 const char* extension = extensions;
170 while (*extension != '\0') {
171 while (*extension == ' ') {
172 extension++;
173 }
174 std::size_t extLength = 0;
175 while (extension[extLength] != '\0' && extension[extLength] != ' ') {
176 extLength++;
177 }
178 if (extLength == 0) {
179 break;
180 }
181
182 for (std::uint32_t j = 0; j < numExtensionsToCheck; j++) {
183 const std::size_t nameLength = std::strlen(extensionNames[j]);
184 if (!results[j] && nameLength == extLength && strncmp(extensionNames[j], extension, extLength) == 0) {
185 results[j] = true;
186 }
187 }
188 extension += extLength;
189 }
190 #else
191 GLint numExtensions;
192 glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
193 if (numExtensions <= 0) {
194 return;
195 }
196
197 for (GLuint i = 0; i < static_cast<GLuint>(numExtensions); i++) {
198 const char* extension = reinterpret_cast<const char*>(glGetStringi(GL_EXTENSIONS, i));
199 const std::size_t extLength = std::strlen(extension);
200
201 for (std::uint32_t j = 0; j < numExtensionsToCheck; j++) {
202 const size_t nameLength = std::strlen(extensionNames[j]);
203 if (!results[j] && nameLength == extLength && strncmp(extensionNames[j], extension, extLength) == 0) {
204 results[j] = true;
205 }
206 }
207 }
208 #endif
209 }
210 }