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 ::toUpper(vendor);
58 ::toUpper(renderer);
59
60 if (vendor.find("NVIDIA") != vendor.npos) {
61 this->vendor = Vendors::nVidia;
62
63 if (renderer.find("QUADRO FX") != std::string::npos) series = Series::QuadroFX;
64 else if (renderer.find("QUADRO") != std::string::npos) series = Series::Quadro;
65 else if (renderer.find("GEFORCE") != std::string::npos) series = Series::geForce;
66
67 auto modelString = renderer.substr(0, renderer.find_first_of('/'));
68
69 if (modelString.size()) {
70 std::regex numbers("\\d.*");
71 std::smatch results;
72
73 if (std::regex_search(modelString, results, numbers)) {
74 model = std::stoi(results[0].str());
75 }
76 }
77 } else if (vendor.find("AMD") != vendor.npos || vendor.find("ATI") != vendor.npos) {
78 this->vendor = Vendors::AMD;
79
80 if (renderer.find("FIREGL") != std::string::npos) series = Series::FireGL;
81 } else if (vendor.find("INTEL") != vendor.npos) {
82 this->vendor = Vendors::Intel;
83 } else {
84 this->vendor = Vendors::Unknown;
85 }
86
87 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &deviceCapabilities.MaxTextureSlots);
88 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &deviceCapabilities.MaxVertexInputElements);
89
90 if (isSupported("GL_EXT_texture_filter_anisotropic")) {
91 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &deviceCapabilities.MaxAnisotropy);
92 LOG_DEBUG(logger, "GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT: %f", deviceCapabilities.MaxAnisotropy);
93 }
94
95 GLint samples = 0;
96 glGetIntegerv(GL_MAX_SAMPLES, &samples);
97 deviceCapabilities.MaxSamples = std::max(1, samples);
98
99 GLint maxTextureSize = 0;
100 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
101 deviceCapabilities.MaxTexture2DSize = std::max(1, maxTextureSize);
102
103 GLint max3DTextureSize = 0;
104 glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &max3DTextureSize);
105 deviceCapabilities.MaxTexture3DSize = std::max(1, max3DTextureSize);
106
107 GLint maxCubeTextureSize = 0;
108 glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxCubeTextureSize);
109 deviceCapabilities.MaxTextureCubeSize = std::max(1, maxCubeTextureSize);
110
111 GLint maxArrayTextureLayers = 0;
112 glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &maxArrayTextureLayers);
113 deviceCapabilities.MaxTextureArrayLayers = std::max(1, maxArrayTextureLayers);
114
115 LOG_DEBUG(logger, "Texture limits: 2D=%u 3D=%u Cube=%u Array=%u",
116 deviceCapabilities.MaxTexture2DSize,
117 deviceCapabilities.MaxTexture3DSize,
118 deviceCapabilities.MaxTextureCubeSize,
119 deviceCapabilities.MaxTextureArrayLayers);
120
121 GLint uniformBufferOffsetAlignment = 0;
122 glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &uniformBufferOffsetAlignment);
123 if (uniformBufferOffsetAlignment) {
124 deviceCapabilities.ConstantBufferOffsetAlignment = uniformBufferOffsetAlignment;
125 }
126 deviceCapabilities.ConstantBufferRange = true;
127
128 if (isSupported("GL_OCULUS_multiview")) {
129 deviceCapabilities.MultiSampleMultiView = true;
130 deviceCapabilities.MultiView = true;
131 }
132 else if (isSupported("GL_OVR_multiview_multisampled_render_to_texture")) {
133 deviceCapabilities.MultiSampleMultiView = true;
134 deviceCapabilities.MultiView = true;
135 }
136 else if (isSupported("GL_OVR_multiview2")) {
137 deviceCapabilities.MultiView = true;
138 }
139 else if (isSupported("GL_OVR_multiview")) {
140 deviceCapabilities.MultiView = true;
141 }
142
143#ifdef __EMSCRIPTEN__
144 if (!deviceCapabilities.MultiView) {
145 int x = EM_ASM_INT({
146 for (const item of Object.values(Module.GL.contexts)) {
147 if (item && item.hasOwnProperty('GLctx')) {
148 const ext = item.GLctx.getExtension("OVR_multiview2");
149 return ext !== null ? 1 : 0;
150 }
151 }
152 });
153 if (x) {
154 LOG_DEBUG(logger, "Browser supports OVR_multiview2 even though emscripten doesn't, enabling regardless");
155 deviceCapabilities.MultiView = true;
156 }
157 }
158
159 if (!deviceCapabilities.MultiSampleMultiView) {
160 int x = EM_ASM_INT({
161 for (const item of Object.values(Module.GL.contexts)) {
162 if (item && item.hasOwnProperty('GLctx')) {
163 const ext = item.GLctx.getExtension("OCULUS_multiview");
164 return ext !== null ? 1 : 0;
165 }
166 }
167 });
168 if (x) {
169 LOG_DEBUG(logger, "Browser supports OCULUS_multiview even though emscripten doesn't, enabling regardless");
170 deviceCapabilities.MultiSampleMultiView = true;
171 deviceCapabilities.MultiView = true;
172 }
173 }
174#endif
175
176 // Check color encoding of default framebuffer.
177 deviceCapabilities.DefaultColorTargetHasLinearEncoding = false;
178 {
179 GLint param = GL_INVALID_ENUM;
180 glBindFramebuffer(GL_FRAMEBUFFER, 0);
181 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_BACK, GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING, &param);
182 switch (param) {
183 case GL_LINEAR:
184 LOG_DEBUG(logger, "Default render target has LINEAR encoding");
185 deviceCapabilities.DefaultColorTargetHasLinearEncoding = true;
186 break;
187 case GL_SRGB:
188 LOG_DEBUG(logger, "Default render target has SRGB encoding");
189 break;
190 default:
191 LOG_ERROR(logger, "glGetFramebufferAttachmentParameter returned unrecognized enum: #%04x", param);
192 break;
193 }
194 }
195
196 if (deviceCapabilities.MultiView) {
197 GLint maxViews = 0;
198 glGetIntegerv(GL_MAX_VIEWS_OVR, &maxViews);
199 deviceCapabilities.MaxMultiViews = std::max(0, maxViews);
200 LOG_DEBUG(logger, "MultiView rendering available, maxViews=%d, maxSamples=%d",
201 maxViews,
202 deviceCapabilities.MultiSampleMultiView ? deviceCapabilities.MaxSamples : 1);
203 }
204
205// "framebufferTextureMultisampleMultiviewOVR"
206
207#ifdef EMSCRIPTEN
208 // ES 2.0 supports buffer sharing, WebGL explicitly does not (WebGL 1.0, section 6.1).
209 deviceCapabilities.VertexAndIndexDataInSharedBuffers = false;
210#endif
211
212 deviceCapabilities.SyncObjects = true;
213 deviceCapabilities.UnsignedIntIndexes = true;
214 deviceCapabilities.FloatTextures = true;
215 deviceCapabilities.FragDepth = true;
216 deviceCapabilities.SupportsHlsl = false;
217 deviceCapabilities.StartInstance = false;
218 deviceCapabilities.IndependentSamplerState = false;
219 deviceCapabilities.SupportsMultipleThreads = false;
220 deviceCapabilities.TextureCubeArrays = false;
221 deviceCapabilities.VertexArrayObjects = true;
222 deviceCapabilities.OriginOnTop = false;
223 deviceCapabilities.DepthNegativeOneToOne = !useClipControl;
224
225 if (isSupported("GL_EXT_depth_clamp")) {
226 // Disabled until we have control over its use in Cogs.Core: It will break
227 // reflection passes if it is enabled during it as that technique relies
228 // on using the near-plane clip to remove anything above the reflection
229 // plane.
230 // deviceCapabilities.NoDepthClip = true;
231 }
232 if (isSupported("GL_EXT_texture_compression_s3tc") ||
233 isSupported("GL_WEBGL_compressed_texture_s3tc")) {
234 deviceCapabilities.TextureCompressionS3TC = true;
235 }
236 if (isSupported("GL_ARB_texture_compression_rgtc") ||
237 isSupported("GL_EXT_texture_compression_rgtc")) {
238 deviceCapabilities.TextureCompressionRGTC = true;
239 }
240 if (isSupported("GL_ARB_texture_compression_bptc") ||
241 isSupported("GL_EXT_texture_compression_bptc")) {
242 deviceCapabilities.TextureCompressionBPTC = true;
243 }
244 if (isSupported("WEBGL_compressed_texture_etc") ||
245 isSupported("GL_OES_compressed_ETC1_RGB8_texture")) {
246 deviceCapabilities.TextureCompressionETC = true;
247 }
248 if (isSupported("WEBGL_compressed_texture_pvrtc") ||
249 isSupported("GL_IMG_texture_compression_pvrtc")) {
250 deviceCapabilities.TextureCompressionPVRTC = true;
251 }
252 if (isSupported("WEBGL_compressed_texture_astc") ||
253 isSupported("GL_OES_texture_compression_astc") || (
254 isSupported("GL_KHR_texture_compression_astc_hdr") &&
255 isSupported("GL_KHR_texture_compression_astc_ldr"))) {
256 deviceCapabilities.TextureCompressionASTC = true;
257 }
258
259 OpenGLES30::enableExtensionFormats(this);
260}
261
262bool Cogs::CapabilitiesGLES30::isSupported(const StringView & extension) const
263{
264 return extensions.find(std::string(extension)) != extensions.end();
265}
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