Cogs.Core
CapabilitiesGLES30.cpp
1#include "CapabilitiesGLES30.h"
2
3#include <sstream>
4#include <iterator>
5#include <algorithm>
6#include <cctype>
7#include <regex>
8
9#include "CommonGLES30.h"
10#include "FormatsGLES30.h"
11
12#include "Foundation/Logging/Logger.h"
13
14#ifdef __EMSCRIPTEN__
15#include <emscripten.h>
16#endif
17
18namespace
19{
20 static Cogs::Logging::Log logger = Cogs::Logging::getLogger("CapabilitiesGLES30");
21
22 void toUpper(std::string & string)
23 {
24 std::transform(string.begin(), string.end(), string.begin(), [](const char & c) { return (char)::toupper(c); });
25 }
26}
27
28void Cogs::CapabilitiesGLES30::initialize(bool isFullGL, bool useClipControl)
29{
30 isFullGL_ = isFullGL;
31
32 std::string vendor((const char *)glGetString(GL_VENDOR));
33 std::string renderer((const char *)glGetString(GL_RENDERER));
34
35 if (logStrings) {
36 LOG_DEBUG(logger, "GL_VERSION: %s", glGetString(GL_VERSION));
37 LOG_DEBUG(logger, "GL_VENDOR: %s", vendor.c_str());
38 LOG_DEBUG(logger, "GL_RENDERER: %s", renderer.c_str());
39 LOG_DEBUG(logger, "GL_SHADING_LANGUAGE_VERSION: %s", glGetString(GL_SHADING_LANGUAGE_VERSION));
40
41 }
42
43 const auto extensionsString = std::string((const char *)glGetString(GL_EXTENSIONS));
44 {
45 std::istringstream sstream(extensionsString);
46 extensions = std::unordered_set<std::string>(std::istream_iterator<std::string>(sstream), std::istream_iterator<std::string>());
47 }
48
49 if (logExtensions) {
50 std::istringstream sstream(extensionsString);
51 std::string ext;
52 while(sstream >> ext){
53 LOG_DEBUG(logger, "Extension: %s", ext.c_str());
54 }
55 }
56
57 std::string unmaskedVendor;
58 std::string unmaskedRenderer;
59 if (isSupported("GL_WEBGL_debug_renderer_info")) {
60 unmaskedVendor = std::string((const char*)glGetString(0x9245)); // GL_UNMASKED_VENDOR_WEBGL
61 unmaskedRenderer = std::string((const char*)glGetString(0x9246)); // GL_UNMASKED_RENDERER_WEBGL
62 LOG_DEBUG(logger, "GL_UNMASKED_VENDOR_WEBGL: %s", unmaskedRenderer.c_str());
63 LOG_DEBUG(logger, "GL_UNMASKED_RENDERER_WEBGL: %s", unmaskedRenderer.c_str());
64 }
65 ::toUpper(unmaskedVendor);
66 ::toUpper(unmaskedRenderer);
67
68 ::toUpper(vendor);
69 ::toUpper(renderer);
70
71 if (vendor.find("NVIDIA") != vendor.npos) {
72 this->vendor = Vendors::nVidia;
73
74 if (renderer.find("QUADRO FX") != std::string::npos) series = Series::QuadroFX;
75 else if (renderer.find("QUADRO") != std::string::npos) series = Series::Quadro;
76 else if (renderer.find("GEFORCE") != std::string::npos) series = Series::geForce;
77
78 auto modelString = renderer.substr(0, renderer.find_first_of('/'));
79
80 if (modelString.size()) {
81 std::regex numbers("\\d.*");
82 std::smatch results;
83
84 if (std::regex_search(modelString, results, numbers)) {
85 model = std::stoi(results[0].str());
86 }
87 }
88 } else if (vendor.find("AMD") != vendor.npos || vendor.find("ATI") != vendor.npos) {
89 this->vendor = Vendors::AMD;
90
91 if (renderer.find("FIREGL") != std::string::npos) series = Series::FireGL;
92 } else if (vendor.find("INTEL") != vendor.npos) {
93 this->vendor = Vendors::Intel;
94 } else {
95 this->vendor = Vendors::Unknown;
96 }
97
98 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &deviceCapabilities.MaxTextureSlots);
99 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &deviceCapabilities.MaxVertexInputElements);
100
101 if (isSupported("GL_EXT_texture_filter_anisotropic")) {
102 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &deviceCapabilities.MaxAnisotropy);
103 LOG_DEBUG(logger, "GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT: %f", deviceCapabilities.MaxAnisotropy);
104 }
105
106 GLint samples = 0;
107 glGetIntegerv(GL_MAX_SAMPLES, &samples);
108 deviceCapabilities.MaxSamples = std::max(1, samples);
109
110 // multisampling to offscreen buffers is broken in Mali-G72
111 if (renderer.find("MALI-G72") != std::string::npos || unmaskedRenderer.find("MALI-G72") != std::string::npos ||
112 renderer.find("MALI-G68") != std::string::npos || unmaskedRenderer.find("MALI-G68") != std::string::npos) {
113 LOG_DEBUG(logger, "Mali-G72 or Mali-G68 detected disabling multisampling");
114 deviceCapabilities.MaxSamples = 1;
115 }
116
117 GLint maxTextureSize = 0;
118 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
119 deviceCapabilities.MaxTexture2DSize = std::max(1, maxTextureSize);
120
121 GLint max3DTextureSize = 0;
122 glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &max3DTextureSize);
123 deviceCapabilities.MaxTexture3DSize = std::max(1, max3DTextureSize);
124
125 GLint maxCubeTextureSize = 0;
126 glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxCubeTextureSize);
127 deviceCapabilities.MaxTextureCubeSize = std::max(1, maxCubeTextureSize);
128
129 GLint maxArrayTextureLayers = 0;
130 glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &maxArrayTextureLayers);
131 deviceCapabilities.MaxTextureArrayLayers = std::max(1, maxArrayTextureLayers);
132
133 LOG_DEBUG(logger, "Texture limits: 2D=%u 3D=%u Cube=%u Array=%u",
134 deviceCapabilities.MaxTexture2DSize,
135 deviceCapabilities.MaxTexture3DSize,
136 deviceCapabilities.MaxTextureCubeSize,
137 deviceCapabilities.MaxTextureArrayLayers);
138
139 GLint uniformBufferOffsetAlignment = 0;
140 glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &uniformBufferOffsetAlignment);
141 if (uniformBufferOffsetAlignment) {
142 deviceCapabilities.ConstantBufferOffsetAlignment = uniformBufferOffsetAlignment;
143 }
144 deviceCapabilities.ConstantBufferRange = true;
145
146 if (isSupported("GL_OCULUS_multiview")) {
147 deviceCapabilities.MultiSampleMultiView = true;
148 deviceCapabilities.MultiView = true;
149 }
150 else if (isSupported("GL_OVR_multiview_multisampled_render_to_texture")) {
151 deviceCapabilities.MultiSampleMultiView = true;
152 deviceCapabilities.MultiView = true;
153 }
154 else if (isSupported("GL_OVR_multiview2")) {
155 deviceCapabilities.MultiView = true;
156 }
157 else if (isSupported("GL_OVR_multiview")) {
158 deviceCapabilities.MultiView = true;
159 }
160
161#ifdef __EMSCRIPTEN__
162 if (!deviceCapabilities.MultiView) {
163 int x = EM_ASM_INT({
164 for (const item of Object.values(Module.GL.contexts)) {
165 if (item && item.hasOwnProperty('GLctx')) {
166 const ext = item.GLctx.getExtension("OVR_multiview2");
167 return ext !== null ? 1 : 0;
168 }
169 }
170 });
171 if (x) {
172 LOG_DEBUG(logger, "Browser supports OVR_multiview2 even though emscripten doesn't, enabling regardless");
173 deviceCapabilities.MultiView = true;
174 }
175 }
176
177 if (!deviceCapabilities.MultiSampleMultiView) {
178 int x = EM_ASM_INT({
179 for (const item of Object.values(Module.GL.contexts)) {
180 if (item && item.hasOwnProperty('GLctx')) {
181 const ext = item.GLctx.getExtension("OCULUS_multiview");
182 return ext !== null ? 1 : 0;
183 }
184 }
185 });
186 if (x) {
187 LOG_DEBUG(logger, "Browser supports OCULUS_multiview even though emscripten doesn't, enabling regardless");
188 deviceCapabilities.MultiSampleMultiView = true;
189 deviceCapabilities.MultiView = true;
190 }
191 }
192#endif
193
194 // Check color encoding of default framebuffer.
195 deviceCapabilities.DefaultColorTargetHasLinearEncoding = false;
196 {
197 GLint param = GL_INVALID_ENUM;
198 glBindFramebuffer(GL_FRAMEBUFFER, 0);
199 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_BACK, GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING, &param);
200 switch (param) {
201 case GL_LINEAR:
202 LOG_DEBUG(logger, "Default render target has LINEAR encoding");
203 deviceCapabilities.DefaultColorTargetHasLinearEncoding = true;
204 break;
205 case GL_SRGB:
206 LOG_DEBUG(logger, "Default render target has SRGB encoding");
207 break;
208 default:
209 LOG_ERROR(logger, "glGetFramebufferAttachmentParameter returned unrecognized enum: #%04x", param);
210 break;
211 }
212 }
213
214 if (deviceCapabilities.MultiView) {
215 GLint maxViews = 0;
216 glGetIntegerv(GL_MAX_VIEWS_OVR, &maxViews);
217 deviceCapabilities.MaxMultiViews = std::max(0, maxViews);
218 LOG_DEBUG(logger, "MultiView rendering available, maxViews=%d, maxSamples=%d",
219 maxViews,
220 deviceCapabilities.MultiSampleMultiView ? deviceCapabilities.MaxSamples : 1);
221 }
222
223// "framebufferTextureMultisampleMultiviewOVR"
224
225#ifdef __EMSCRIPTEN__
226 // ES 2.0 supports buffer sharing, WebGL explicitly does not (WebGL 1.0, section 6.1).
227 deviceCapabilities.VertexAndIndexDataInSharedBuffers = false;
228#endif
229
230 deviceCapabilities.SyncObjects = true;
231 deviceCapabilities.UnsignedIntIndexes = true;
232 deviceCapabilities.FloatTextures = true;
233 deviceCapabilities.FragDepth = true;
234 deviceCapabilities.SupportsHlsl = false;
235 deviceCapabilities.StartInstance = false;
236 deviceCapabilities.IndependentSamplerState = false;
237 deviceCapabilities.SupportsMultipleThreads = false;
238 deviceCapabilities.TextureCubeArrays = false;
239 deviceCapabilities.VertexArrayObjects = true;
240 deviceCapabilities.OriginOnTop = false;
241 deviceCapabilities.DepthNegativeOneToOne = !useClipControl;
242
243 if (isSupported("GL_EXT_depth_clamp")) {
244 // Disabled until we have control over its use in Cogs.Core: It will break
245 // reflection passes if it is enabled during it as that technique relies
246 // on using the near-plane clip to remove anything above the reflection
247 // plane.
248 // deviceCapabilities.NoDepthClip = true;
249 }
250 if (isSupported("GL_EXT_texture_compression_s3tc") ||
251 isSupported("GL_WEBGL_compressed_texture_s3tc")) {
252 deviceCapabilities.TextureCompressionS3TC = true;
253 }
254 if (isSupported("GL_ARB_texture_compression_rgtc") ||
255 isSupported("GL_EXT_texture_compression_rgtc")) {
256 deviceCapabilities.TextureCompressionRGTC = true;
257 }
258 if (isSupported("GL_ARB_texture_compression_bptc") ||
259 isSupported("GL_EXT_texture_compression_bptc")) {
260 deviceCapabilities.TextureCompressionBPTC = true;
261 }
262 if (isSupported("WEBGL_compressed_texture_etc") ||
263 isSupported("GL_OES_compressed_ETC1_RGB8_texture")) {
264 deviceCapabilities.TextureCompressionETC = true;
265 }
266 if (isSupported("WEBGL_compressed_texture_pvrtc") ||
267 isSupported("GL_IMG_texture_compression_pvrtc")) {
268 deviceCapabilities.TextureCompressionPVRTC = true;
269 }
270 if (isSupported("WEBGL_compressed_texture_astc") ||
271 isSupported("GL_OES_texture_compression_astc") || (
272 isSupported("GL_KHR_texture_compression_astc_hdr") &&
273 isSupported("GL_KHR_texture_compression_astc_ldr"))) {
274 deviceCapabilities.TextureCompressionASTC = true;
275 }
276
277 OpenGLES30::enableExtensionFormats(this);
278}
279
280bool Cogs::CapabilitiesGLES30::isSupported(const StringView & extension) const
281{
282 return extensions.find(std::string(extension)) != extensions.end();
283}
Log implementation class.
Definition: LogManager.h:140
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:181
uint32_t MaxTexture3DSize
Using D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION as default.
Definition: ICapabilities.h:81
uint32_t MaxTexture2DSize
Using D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION as default.
Definition: ICapabilities.h:80
uint32_t MaxTextureCubeSize
Using D3D11_REQ_TEXTURECUBE_DIMENSION as default.
Definition: ICapabilities.h:82
uint32_t MaxTextureArrayLayers
Using D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION as default.
Definition: ICapabilities.h:83
unsigned ConstantBufferOffsetAlignment
Minimum offset alignment when binding constant buffers.
Definition: ICapabilities.h:78
bool ConstantBufferRange
supports binding a range of a constant buffer.
Definition: ICapabilities.h:90
@ QuadroFX
nVidia Quadro FX professional graphics adapters.
Definition: ICapabilities.h:50
@ FireGL
AMD FireGL series.
Definition: ICapabilities.h:43
@ geForce
nVidia geForce consumer adapters.
Definition: ICapabilities.h:46
@ Quadro
nVidia Quadro professional graphics adapters.
Definition: ICapabilities.h:48
@ nVidia
nVidia Corporation.
Definition: ICapabilities.h:20
@ Unknown
Unknown device vendor.
Definition: ICapabilities.h:16