1 | /* |
2 | * Copyright 2012 Google Inc. |
3 | * |
4 | * Use of this source code is governed by a BSD-style license that can be |
5 | * found in the LICENSE file. |
6 | */ |
7 | |
8 | #ifndef GrGLUtil_DEFINED |
9 | #define GrGLUtil_DEFINED |
10 | |
11 | #include "include/gpu/gl/GrGLInterface.h" |
12 | #include "include/private/GrTypesPriv.h" |
13 | #include "include/private/SkImageInfoPriv.h" |
14 | #include "src/gpu/GrDataUtils.h" |
15 | #include "src/gpu/GrStencilSettings.h" |
16 | #include "src/gpu/gl/GrGLDefines.h" |
17 | |
18 | class SkMatrix; |
19 | |
20 | //////////////////////////////////////////////////////////////////////////////// |
21 | |
22 | typedef uint32_t GrGLVersion; |
23 | typedef uint32_t GrGLSLVersion; |
24 | typedef uint64_t GrGLDriverVersion; |
25 | |
26 | #define GR_GL_VER(major, minor) ((static_cast<uint32_t>(major) << 16) | \ |
27 | static_cast<uint32_t>(minor)) |
28 | #define GR_GLSL_VER(major, minor) ((static_cast<uint32_t>(major) << 16) | \ |
29 | static_cast<uint32_t>(minor)) |
30 | #define GR_GL_DRIVER_VER(major, minor, point) ((static_cast<uint64_t>(major) << 32) | \ |
31 | (static_cast<uint64_t>(minor) << 16) | \ |
32 | static_cast<uint64_t>(point)) |
33 | |
34 | #define GR_GL_MAJOR_VER(version) (static_cast<uint32_t>(version) >> 16) |
35 | #define GR_GL_MINOR_VER(version) (static_cast<uint32_t>(version) & 0xFFFF) |
36 | |
37 | #define GR_GL_INVALID_VER GR_GL_VER(0, 0) |
38 | #define GR_GLSL_INVALID_VER GR_GLSL_VER(0, 0) |
39 | #define GR_GL_DRIVER_UNKNOWN_VER GR_GL_DRIVER_VER(0, 0, 0) |
40 | |
41 | static constexpr uint32_t GrGLFormatChannels(GrGLFormat format) { |
42 | switch (format) { |
43 | case GrGLFormat::kUnknown: return 0; |
44 | case GrGLFormat::kRGBA8: return kRGBA_SkColorChannelFlags; |
45 | case GrGLFormat::kR8: return kRed_SkColorChannelFlag; |
46 | case GrGLFormat::kALPHA8: return kAlpha_SkColorChannelFlag; |
47 | case GrGLFormat::kLUMINANCE8: return kGray_SkColorChannelFlag; |
48 | case GrGLFormat::kBGRA8: return kRGBA_SkColorChannelFlags; |
49 | case GrGLFormat::kRGB565: return kRGB_SkColorChannelFlags; |
50 | case GrGLFormat::kRGBA16F: return kRGBA_SkColorChannelFlags; |
51 | case GrGLFormat::kR16F: return kRed_SkColorChannelFlag; |
52 | case GrGLFormat::kRGB8: return kRGB_SkColorChannelFlags; |
53 | case GrGLFormat::kRG8: return kRG_SkColorChannelFlags; |
54 | case GrGLFormat::kRGB10_A2: return kRGBA_SkColorChannelFlags; |
55 | case GrGLFormat::kRGBA4: return kRGBA_SkColorChannelFlags; |
56 | case GrGLFormat::kSRGB8_ALPHA8: return kRGBA_SkColorChannelFlags; |
57 | case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return kRGB_SkColorChannelFlags; |
58 | case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return kRGB_SkColorChannelFlags; |
59 | case GrGLFormat::kCOMPRESSED_RGB8_BC1: return kRGB_SkColorChannelFlags; |
60 | case GrGLFormat::kCOMPRESSED_RGBA8_BC1: return kRGBA_SkColorChannelFlags; |
61 | case GrGLFormat::kR16: return kRed_SkColorChannelFlag; |
62 | case GrGLFormat::kRG16: return kRG_SkColorChannelFlags; |
63 | case GrGLFormat::kRGBA16: return kRGBA_SkColorChannelFlags; |
64 | case GrGLFormat::kRG16F: return kRG_SkColorChannelFlags; |
65 | case GrGLFormat::kLUMINANCE16F: return kGray_SkColorChannelFlag; |
66 | } |
67 | SkUNREACHABLE; |
68 | } |
69 | |
70 | /** |
71 | * The Vendor and Renderer enum values are lazily updated as required. |
72 | */ |
73 | enum GrGLVendor { |
74 | kARM_GrGLVendor, |
75 | kGoogle_GrGLVendor, |
76 | kImagination_GrGLVendor, |
77 | kIntel_GrGLVendor, |
78 | kQualcomm_GrGLVendor, |
79 | kNVIDIA_GrGLVendor, |
80 | kATI_GrGLVendor, |
81 | |
82 | kOther_GrGLVendor |
83 | }; |
84 | |
85 | enum GrGLRenderer { |
86 | kTegra_PreK1_GrGLRenderer, // Legacy Tegra architecture (pre-K1). |
87 | kTegra_GrGLRenderer, // Tegra with the same architecture as NVIDIA desktop GPUs (K1+). |
88 | kPowerVR54x_GrGLRenderer, |
89 | kPowerVRRogue_GrGLRenderer, |
90 | kAdreno3xx_GrGLRenderer, |
91 | kAdreno430_GrGLRenderer, |
92 | kAdreno4xx_other_GrGLRenderer, |
93 | kAdreno5xx_GrGLRenderer, |
94 | kAdreno615_GrGLRenderer, // Pixel3a |
95 | kAdreno630_GrGLRenderer, // Pixel3 |
96 | kAdreno640_GrGLRenderer, // Pixel4 |
97 | kGoogleSwiftShader_GrGLRenderer, |
98 | |
99 | /** Intel GPU families, ordered by generation **/ |
100 | // 6th gen |
101 | kIntelSandyBridge_GrGLRenderer, |
102 | |
103 | // 7th gen |
104 | kIntelIvyBridge_GrGLRenderer, |
105 | kIntelValleyView_GrGLRenderer, // aka BayTrail |
106 | kIntelHaswell_GrGLRenderer, |
107 | |
108 | // 8th gen |
109 | kIntelCherryView_GrGLRenderer, // aka Braswell |
110 | kIntelBroadwell_GrGLRenderer, |
111 | |
112 | // 9th gen |
113 | kIntelApolloLake_GrGLRenderer, |
114 | kIntelSkyLake_GrGLRenderer, |
115 | kIntelGeminiLake_GrGLRenderer, |
116 | kIntelKabyLake_GrGLRenderer, |
117 | kIntelCoffeeLake_GrGLRenderer, |
118 | |
119 | // 11th gen |
120 | kIntelIceLake_GrGLRenderer, |
121 | |
122 | kGalliumLLVM_GrGLRenderer, |
123 | kMali4xx_GrGLRenderer, |
124 | /** G-3x, G-5x, or G-7x */ |
125 | kMaliG_GrGLRenderer, |
126 | /** T-6xx, T-7xx, or T-8xx */ |
127 | kMaliT_GrGLRenderer, |
128 | kANGLE_GrGLRenderer, |
129 | |
130 | kAMDRadeonHD7xxx_GrGLRenderer, // AMD Radeon HD 7000 Series |
131 | kAMDRadeonR9M3xx_GrGLRenderer, // AMD Radeon R9 M300 Series |
132 | kAMDRadeonR9M4xx_GrGLRenderer, // AMD Radeon R9 M400 Series |
133 | kAMDRadeonProVegaxx_GrGLRenderer, // AMD Radeon Pro Vega |
134 | |
135 | kOther_GrGLRenderer |
136 | }; |
137 | |
138 | enum GrGLDriver { |
139 | kMesa_GrGLDriver, |
140 | kChromium_GrGLDriver, |
141 | kNVIDIA_GrGLDriver, |
142 | kIntel_GrGLDriver, |
143 | kANGLE_GrGLDriver, |
144 | kSwiftShader_GrGLDriver, |
145 | kQualcomm_GrGLDriver, |
146 | kAndroidEmulator_GrGLDriver, |
147 | kUnknown_GrGLDriver |
148 | }; |
149 | |
150 | enum class GrGLANGLEBackend { |
151 | kUnknown, |
152 | kD3D9, |
153 | kD3D11, |
154 | kOpenGL |
155 | }; |
156 | |
157 | enum class GrGLANGLEVendor { |
158 | kUnknown, |
159 | kIntel |
160 | }; |
161 | |
162 | enum class GrGLANGLERenderer { |
163 | kUnknown, |
164 | kSandyBridge, |
165 | kIvyBridge, |
166 | kSkylake |
167 | }; |
168 | |
169 | //////////////////////////////////////////////////////////////////////////////// |
170 | |
171 | /** |
172 | * Some drivers want the var-int arg to be zero-initialized on input. |
173 | */ |
174 | #define GR_GL_INIT_ZERO 0 |
175 | #define GR_GL_GetIntegerv(gl, e, p) \ |
176 | do { \ |
177 | *(p) = GR_GL_INIT_ZERO; \ |
178 | GR_GL_CALL(gl, GetIntegerv(e, p)); \ |
179 | } while (0) |
180 | |
181 | #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p) \ |
182 | do { \ |
183 | *(p) = GR_GL_INIT_ZERO; \ |
184 | GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p)); \ |
185 | } while (0) |
186 | |
187 | #define GR_GL_GetInternalformativ(gl, t, f, n, s, p) \ |
188 | do { \ |
189 | *(p) = GR_GL_INIT_ZERO; \ |
190 | GR_GL_CALL(gl, GetInternalformativ(t, f, n, s, p)); \ |
191 | } while (0) |
192 | |
193 | #define GR_GL_GetNamedFramebufferAttachmentParameteriv(gl, fb, a, pname, p) \ |
194 | do { \ |
195 | *(p) = GR_GL_INIT_ZERO; \ |
196 | GR_GL_CALL(gl, GetNamedFramebufferAttachmentParameteriv(fb, a, pname, p)); \ |
197 | } while (0) |
198 | |
199 | #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p) \ |
200 | do { \ |
201 | *(p) = GR_GL_INIT_ZERO; \ |
202 | GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p)); \ |
203 | } while (0) |
204 | |
205 | #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p) \ |
206 | do { \ |
207 | *(p) = GR_GL_INIT_ZERO; \ |
208 | GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p)); \ |
209 | } while (0) |
210 | |
211 | #define GR_GL_GetShaderPrecisionFormat(gl, st, pt, range, precision) \ |
212 | do { \ |
213 | (range)[0] = GR_GL_INIT_ZERO; \ |
214 | (range)[1] = GR_GL_INIT_ZERO; \ |
215 | (*precision) = GR_GL_INIT_ZERO; \ |
216 | GR_GL_CALL(gl, GetShaderPrecisionFormat(st, pt, range, precision)); \ |
217 | } while (0) |
218 | |
219 | //////////////////////////////////////////////////////////////////////////////// |
220 | |
221 | /** |
222 | * Helpers for glGetString() |
223 | */ |
224 | |
225 | // these variants assume caller already has a string from glGetString() |
226 | GrGLVersion GrGLGetVersionFromString(const char* versionString); |
227 | GrGLStandard GrGLGetStandardInUseFromString(const char* versionString); |
228 | GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString); |
229 | GrGLVendor GrGLGetVendorFromString(const char* vendorString); |
230 | GrGLRenderer GrGLGetRendererFromStrings(const char* rendererString, const GrGLExtensions&); |
231 | void GrGLGetANGLEInfoFromString(const char* rendererString, GrGLANGLEBackend*, |
232 | GrGLANGLEVendor*, GrGLANGLERenderer*); |
233 | |
234 | void GrGLGetDriverInfo(GrGLStandard standard, |
235 | GrGLVendor vendor, |
236 | const char* rendererString, |
237 | const char* versionString, |
238 | GrGLDriver* outDriver, |
239 | GrGLDriverVersion* outVersion); |
240 | |
241 | // these variants call glGetString() |
242 | GrGLVersion GrGLGetVersion(const GrGLInterface*); |
243 | GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*); |
244 | GrGLVendor GrGLGetVendor(const GrGLInterface*); |
245 | GrGLRenderer GrGLGetRenderer(const GrGLInterface*); |
246 | |
247 | /** |
248 | * Helpers for glGetError() |
249 | */ |
250 | |
251 | void GrGLCheckErr(const GrGLInterface* gl, |
252 | const char* location, |
253 | const char* call); |
254 | |
255 | void GrGLClearErr(const GrGLInterface* gl); |
256 | |
257 | //////////////////////////////////////////////////////////////////////////////// |
258 | |
259 | /** |
260 | * Macros for using GrGLInterface to make GL calls |
261 | */ |
262 | |
263 | // internal macro to conditionally call glGetError based on compile-time and |
264 | // run-time flags. |
265 | #if GR_GL_CHECK_ERROR |
266 | extern bool gCheckErrorGL; |
267 | #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \ |
268 | if (gCheckErrorGL) \ |
269 | GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X) |
270 | #else |
271 | #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) |
272 | #endif |
273 | |
274 | // internal macro to conditionally log the gl call using SkDebugf based on |
275 | // compile-time and run-time flags. |
276 | #if GR_GL_LOG_CALLS |
277 | extern bool gLogCallsGL; |
278 | #define GR_GL_LOG_CALLS_IMPL(X) \ |
279 | if (gLogCallsGL) \ |
280 | SkDebugf(GR_FILE_AND_LINE_STR "GL: " #X "\n") |
281 | #else |
282 | #define GR_GL_LOG_CALLS_IMPL(X) |
283 | #endif |
284 | |
285 | // makes a GL call on the interface and does any error checking and logging |
286 | #define GR_GL_CALL(IFACE, X) \ |
287 | do { \ |
288 | GR_GL_CALL_NOERRCHECK(IFACE, X); \ |
289 | GR_GL_CHECK_ERROR_IMPL(IFACE, X); \ |
290 | } while (false) |
291 | |
292 | // Variant of above that always skips the error check. This is useful when |
293 | // the caller wants to do its own glGetError() call and examine the error value. |
294 | #define GR_GL_CALL_NOERRCHECK(IFACE, X) \ |
295 | do { \ |
296 | (IFACE)->fFunctions.f##X; \ |
297 | GR_GL_LOG_CALLS_IMPL(X); \ |
298 | } while (false) |
299 | |
300 | // same as GR_GL_CALL but stores the return value of the gl call in RET |
301 | #define GR_GL_CALL_RET(IFACE, RET, X) \ |
302 | do { \ |
303 | GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X); \ |
304 | GR_GL_CHECK_ERROR_IMPL(IFACE, X); \ |
305 | } while (false) |
306 | |
307 | // same as GR_GL_CALL_RET but always skips the error check. |
308 | #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X) \ |
309 | do { \ |
310 | (RET) = (IFACE)->fFunctions.f##X; \ |
311 | GR_GL_LOG_CALLS_IMPL(X); \ |
312 | } while (false) |
313 | |
314 | // call glGetError without doing a redundant error check or logging. |
315 | #define GR_GL_GET_ERROR(IFACE) (IFACE)->fFunctions.fGetError() |
316 | |
317 | static constexpr GrGLFormat GrGLFormatFromGLEnum(GrGLenum glFormat) { |
318 | switch (glFormat) { |
319 | case GR_GL_RGBA8: return GrGLFormat::kRGBA8; |
320 | case GR_GL_R8: return GrGLFormat::kR8; |
321 | case GR_GL_ALPHA8: return GrGLFormat::kALPHA8; |
322 | case GR_GL_LUMINANCE8: return GrGLFormat::kLUMINANCE8; |
323 | case GR_GL_BGRA8: return GrGLFormat::kBGRA8; |
324 | case GR_GL_RGB565: return GrGLFormat::kRGB565; |
325 | case GR_GL_RGBA16F: return GrGLFormat::kRGBA16F; |
326 | case GR_GL_LUMINANCE16F: return GrGLFormat::kLUMINANCE16F; |
327 | case GR_GL_R16F: return GrGLFormat::kR16F; |
328 | case GR_GL_RGB8: return GrGLFormat::kRGB8; |
329 | case GR_GL_RG8: return GrGLFormat::kRG8; |
330 | case GR_GL_RGB10_A2: return GrGLFormat::kRGB10_A2; |
331 | case GR_GL_RGBA4: return GrGLFormat::kRGBA4; |
332 | case GR_GL_SRGB8_ALPHA8: return GrGLFormat::kSRGB8_ALPHA8; |
333 | case GR_GL_COMPRESSED_ETC1_RGB8: return GrGLFormat::kCOMPRESSED_ETC1_RGB8; |
334 | case GR_GL_COMPRESSED_RGB8_ETC2: return GrGLFormat::kCOMPRESSED_RGB8_ETC2; |
335 | case GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return GrGLFormat::kCOMPRESSED_RGB8_BC1; |
336 | case GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return GrGLFormat::kCOMPRESSED_RGBA8_BC1; |
337 | case GR_GL_R16: return GrGLFormat::kR16; |
338 | case GR_GL_RG16: return GrGLFormat::kRG16; |
339 | case GR_GL_RGBA16: return GrGLFormat::kRGBA16; |
340 | case GR_GL_RG16F: return GrGLFormat::kRG16F; |
341 | |
342 | default: return GrGLFormat::kUnknown; |
343 | } |
344 | } |
345 | |
346 | /** Returns either the sized internal format or compressed internal format of the GrGLFormat. */ |
347 | static constexpr GrGLenum GrGLFormatToEnum(GrGLFormat format) { |
348 | switch (format) { |
349 | case GrGLFormat::kRGBA8: return GR_GL_RGBA8; |
350 | case GrGLFormat::kR8: return GR_GL_R8; |
351 | case GrGLFormat::kALPHA8: return GR_GL_ALPHA8; |
352 | case GrGLFormat::kLUMINANCE8: return GR_GL_LUMINANCE8; |
353 | case GrGLFormat::kBGRA8: return GR_GL_BGRA8; |
354 | case GrGLFormat::kRGB565: return GR_GL_RGB565; |
355 | case GrGLFormat::kRGBA16F: return GR_GL_RGBA16F; |
356 | case GrGLFormat::kLUMINANCE16F: return GR_GL_LUMINANCE16F; |
357 | case GrGLFormat::kR16F: return GR_GL_R16F; |
358 | case GrGLFormat::kRGB8: return GR_GL_RGB8; |
359 | case GrGLFormat::kRG8: return GR_GL_RG8; |
360 | case GrGLFormat::kRGB10_A2: return GR_GL_RGB10_A2; |
361 | case GrGLFormat::kRGBA4: return GR_GL_RGBA4; |
362 | case GrGLFormat::kSRGB8_ALPHA8: return GR_GL_SRGB8_ALPHA8; |
363 | case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return GR_GL_COMPRESSED_ETC1_RGB8; |
364 | case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return GR_GL_COMPRESSED_RGB8_ETC2; |
365 | case GrGLFormat::kCOMPRESSED_RGB8_BC1: return GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT; |
366 | case GrGLFormat::kCOMPRESSED_RGBA8_BC1: return GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
367 | case GrGLFormat::kR16: return GR_GL_R16; |
368 | case GrGLFormat::kRG16: return GR_GL_RG16; |
369 | case GrGLFormat::kRGBA16: return GR_GL_RGBA16; |
370 | case GrGLFormat::kRG16F: return GR_GL_RG16F; |
371 | case GrGLFormat::kUnknown: return 0; |
372 | } |
373 | SkUNREACHABLE; |
374 | } |
375 | |
376 | #if GR_TEST_UTILS |
377 | static constexpr const char* GrGLFormatToStr(GrGLenum glFormat) { |
378 | switch (glFormat) { |
379 | case GR_GL_RGBA8: return "RGBA8" ; |
380 | case GR_GL_R8: return "R8" ; |
381 | case GR_GL_ALPHA8: return "ALPHA8" ; |
382 | case GR_GL_LUMINANCE8: return "LUMINANCE8" ; |
383 | case GR_GL_BGRA8: return "BGRA8" ; |
384 | case GR_GL_RGB565: return "RGB565" ; |
385 | case GR_GL_RGBA16F: return "RGBA16F" ; |
386 | case GR_GL_LUMINANCE16F: return "LUMINANCE16F" ; |
387 | case GR_GL_R16F: return "R16F" ; |
388 | case GR_GL_RGB8: return "RGB8" ; |
389 | case GR_GL_RG8: return "RG8" ; |
390 | case GR_GL_RGB10_A2: return "RGB10_A2" ; |
391 | case GR_GL_RGBA4: return "RGBA4" ; |
392 | case GR_GL_RGBA32F: return "RGBA32F" ; |
393 | case GR_GL_SRGB8_ALPHA8: return "SRGB8_ALPHA8" ; |
394 | case GR_GL_COMPRESSED_ETC1_RGB8: return "ETC1" ; |
395 | case GR_GL_COMPRESSED_RGB8_ETC2: return "ETC2" ; |
396 | case GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return "RGB8_BC1" ; |
397 | case GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return "RGBA8_BC1" ; |
398 | case GR_GL_R16: return "R16" ; |
399 | case GR_GL_RG16: return "RG16" ; |
400 | case GR_GL_RGBA16: return "RGBA16" ; |
401 | case GR_GL_RG16F: return "RG16F" ; |
402 | |
403 | default: return "Unknown" ; |
404 | } |
405 | } |
406 | #endif |
407 | |
408 | GrGLenum GrToGLStencilFunc(GrStencilTest test); |
409 | |
410 | /** |
411 | * Returns true if the format is compressed. |
412 | */ |
413 | bool GrGLFormatIsCompressed(GrGLFormat); |
414 | |
415 | /** |
416 | * This will return CompressionType::kNone if the format is uncompressed. |
417 | */ |
418 | SkImage::CompressionType GrGLFormatToCompressionType(GrGLFormat); |
419 | |
420 | #endif |
421 | |