blob: b23a1f81535164246e7f9354e802b643b7f6e6ce [file] [log] [blame]
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001/*
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
9#include "GrGLCaps.h"
10#include "GrGLContextInfo.h"
bsalomon@google.comc9668ec2012-04-11 18:16:41 +000011#include "SkTSearch.h"
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000012
13GrGLCaps::GrGLCaps() {
14 this->reset();
15}
16
17void GrGLCaps::reset() {
18 fVerifiedColorConfigs.reset();
19 fStencilFormats.reset();
20 fStencilVerifiedColorConfigs.reset();
21 fMSFBOType = kNone_MSFBOType;
bsalomon@google.comf6b070d2012-04-27 14:25:44 +000022 fMaxSampleCount = 0;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +000023 fCoverageAAType = kNone_CoverageAAType;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000024 fMaxFragmentUniformVectors = 0;
bsalomon@google.com60da4172012-06-01 19:25:00 +000025 fMaxVertexAttributes = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000026 fRGBA8RenderbufferSupport = false;
27 fBGRAFormatSupport = false;
28 fBGRAIsInternalFormat = false;
29 fTextureSwizzleSupport = false;
30 fUnpackRowLengthSupport = false;
31 fUnpackFlipYSupport = false;
32 fPackRowLengthSupport = false;
33 fPackFlipYSupport = false;
34 fTextureUsageSupport = false;
35 fTexStorageSupport = false;
robertphillips@google.com443e5a52012-04-30 20:01:21 +000036 fTextureRedSupport = false;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000037}
38
39GrGLCaps::GrGLCaps(const GrGLCaps& caps) {
40 *this = caps;
41}
42
43GrGLCaps& GrGLCaps::operator = (const GrGLCaps& caps) {
44 fVerifiedColorConfigs = caps.fVerifiedColorConfigs;
45 fStencilFormats = caps.fStencilFormats;
46 fStencilVerifiedColorConfigs = caps.fStencilVerifiedColorConfigs;
47 fMaxFragmentUniformVectors = caps.fMaxFragmentUniformVectors;
bsalomon@google.com60da4172012-06-01 19:25:00 +000048 fMaxVertexAttributes = caps.fMaxVertexAttributes;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000049 fMSFBOType = caps.fMSFBOType;
bsalomon@google.comf6b070d2012-04-27 14:25:44 +000050 fMaxSampleCount = caps.fMaxSampleCount;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +000051 fCoverageAAType = caps.fCoverageAAType;
52 fMSAACoverageModes = caps.fMSAACoverageModes;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000053 fRGBA8RenderbufferSupport = caps.fRGBA8RenderbufferSupport;
54 fBGRAFormatSupport = caps.fBGRAFormatSupport;
55 fBGRAIsInternalFormat = caps.fBGRAIsInternalFormat;
56 fTextureSwizzleSupport = caps.fTextureSwizzleSupport;
57 fUnpackRowLengthSupport = caps.fUnpackRowLengthSupport;
58 fUnpackFlipYSupport = caps.fUnpackFlipYSupport;
59 fPackRowLengthSupport = caps.fPackRowLengthSupport;
60 fPackFlipYSupport = caps.fPackFlipYSupport;
61 fTextureUsageSupport = caps.fTextureUsageSupport;
62 fTexStorageSupport = caps.fTexStorageSupport;
robertphillips@google.com443e5a52012-04-30 20:01:21 +000063 fTextureRedSupport = caps.fTextureRedSupport;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000064
65 return *this;
66}
67
68void GrGLCaps::init(const GrGLContextInfo& ctxInfo) {
69
70 this->reset();
71 if (!ctxInfo.isInitialized()) {
72 return;
73 }
74
75 const GrGLInterface* gli = ctxInfo.interface();
76 GrGLBinding binding = ctxInfo.binding();
77 GrGLVersion version = ctxInfo.version();
78
79 if (kES2_GrGLBinding == binding) {
80 GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
81 &fMaxFragmentUniformVectors);
82 } else {
83 GrAssert(kDesktop_GrGLBinding == binding);
84 GrGLint max;
85 GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
86 fMaxFragmentUniformVectors = max / 4;
87 }
bsalomon@google.com60da4172012-06-01 19:25:00 +000088 GR_GL_GetIntegerv(gli, GR_GL_MAX_VERTEX_ATTRIBS, &fMaxVertexAttributes);
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000089
90 if (kDesktop_GrGLBinding == binding) {
91 fRGBA8RenderbufferSupport = true;
92 } else {
93 fRGBA8RenderbufferSupport = ctxInfo.hasExtension("GL_OES_rgb8_rgba8") ||
94 ctxInfo.hasExtension("GL_ARM_rgba8");
95 }
96
97 if (kDesktop_GrGLBinding == binding) {
98 fBGRAFormatSupport = version >= GR_GL_VER(1,2) ||
99 ctxInfo.hasExtension("GL_EXT_bgra");
100 } else {
101 bool hasBGRAExt = false;
102 if (ctxInfo.hasExtension("GL_APPLE_texture_format_BGRA8888")) {
103 fBGRAFormatSupport = true;
104 } else if (ctxInfo.hasExtension("GL_EXT_texture_format_BGRA8888")) {
105 fBGRAFormatSupport = true;
106 fBGRAIsInternalFormat = true;
107 }
108 GrAssert(fBGRAFormatSupport ||
109 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
110 }
111
112 if (kDesktop_GrGLBinding == binding) {
113 fTextureSwizzleSupport = version >= GR_GL_VER(3,3) ||
114 ctxInfo.hasExtension("GL_ARB_texture_swizzle");
115 } else {
116 fTextureSwizzleSupport = false;
117 }
118
119 if (kDesktop_GrGLBinding == binding) {
120 fUnpackRowLengthSupport = true;
121 fUnpackFlipYSupport = false;
122 fPackRowLengthSupport = true;
123 fPackFlipYSupport = false;
124 } else {
125 fUnpackRowLengthSupport =ctxInfo.hasExtension("GL_EXT_unpack_subimage");
126 fUnpackFlipYSupport = ctxInfo.hasExtension("GL_CHROMIUM_flipy");
127 // no extension for pack row length
128 fPackRowLengthSupport = false;
129 fPackFlipYSupport =
130 ctxInfo.hasExtension("GL_ANGLE_pack_reverse_row_order");
131 }
132
133 fTextureUsageSupport = (kES2_GrGLBinding == binding) &&
134 ctxInfo.hasExtension("GL_ANGLE_texture_usage");
135
136 // Tex storage is in desktop 4.2 and can be an extension to desktop or ES.
137 fTexStorageSupport = (kDesktop_GrGLBinding == binding &&
138 version >= GR_GL_VER(4,2)) ||
139 ctxInfo.hasExtension("GL_ARB_texture_storage") ||
140 ctxInfo.hasExtension("GL_EXT_texture_storage");
141
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000142 // ARB_texture_rg is part of OpenGL 3.0
143 if (kDesktop_GrGLBinding == binding) {
144 fTextureRedSupport = version >= GR_GL_VER(3,0) ||
145 ctxInfo.hasExtension("GL_ARB_texture_rg");
146 } else {
147 fTextureRedSupport = ctxInfo.hasExtension("GL_EXT_texture_rg");
148 }
149
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000150 this->initFSAASupport(ctxInfo);
151 this->initStencilFormats(ctxInfo);
152}
153
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000154namespace {
155int coverage_mode_compare(const GrGLCaps::MSAACoverageMode* left,
156 const GrGLCaps::MSAACoverageMode* right) {
157 if (left->fCoverageSampleCnt < right->fCoverageSampleCnt) {
158 return -1;
159 } else if (right->fCoverageSampleCnt < left->fCoverageSampleCnt) {
160 return 1;
161 } else if (left->fColorSampleCnt < right->fColorSampleCnt) {
162 return -1;
163 } else if (right->fColorSampleCnt < left->fColorSampleCnt) {
164 return 1;
165 }
166 return 0;
167}
168}
169
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000170void GrGLCaps::initFSAASupport(const GrGLContextInfo& ctxInfo) {
171
172 fMSFBOType = kNone_MSFBOType;
173 if (kDesktop_GrGLBinding != ctxInfo.binding()) {
174 if (ctxInfo.hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
175 // chrome's extension is equivalent to the EXT msaa
176 // and fbo_blit extensions.
177 fMSFBOType = kDesktopEXT_MSFBOType;
178 } else if (ctxInfo.hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000179 fMSFBOType = kAppleES_MSFBOType;
180 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000181 } else {
182 if ((ctxInfo.version() >= GR_GL_VER(3,0)) ||
183 ctxInfo.hasExtension("GL_ARB_framebuffer_object")) {
184 fMSFBOType = GrGLCaps::kDesktopARB_MSFBOType;
185 } else if (ctxInfo.hasExtension("GL_EXT_framebuffer_multisample") &&
186 ctxInfo.hasExtension("GL_EXT_framebuffer_blit")) {
187 fMSFBOType = GrGLCaps::kDesktopEXT_MSFBOType;
188 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000189 // TODO: We could populate fMSAACoverageModes using GetInternalformativ
190 // on GL 4.2+. It's format-specific, though. See also
191 // http://code.google.com/p/skia/issues/detail?id=470 about using actual
192 // rather than requested sample counts in cache key.
193 if (ctxInfo.hasExtension("GL_NV_framebuffer_multisample_coverage")) {
194 fCoverageAAType = kNVDesktop_CoverageAAType;
195 GrGLint count;
196 GR_GL_GetIntegerv(ctxInfo.interface(),
197 GR_GL_MAX_MULTISAMPLE_COVERAGE_MODES,
198 &count);
199 fMSAACoverageModes.setCount(count);
200 GR_GL_GetIntegerv(ctxInfo.interface(),
201 GR_GL_MULTISAMPLE_COVERAGE_MODES,
202 (int*)&fMSAACoverageModes[0]);
203 // The NV driver seems to return the modes already sorted but the
204 // spec doesn't require this. So we sort.
reed@google.comc7a67cb2012-05-07 14:52:12 +0000205 qsort(&fMSAACoverageModes[0],
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000206 count,
207 sizeof(MSAACoverageMode),
reed@google.comc7a67cb2012-05-07 14:52:12 +0000208 SkCastForQSort(coverage_mode_compare));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000209 }
210 }
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000211 if (kNone_MSFBOType != fMSFBOType) {
212 GR_GL_GetIntegerv(ctxInfo.interface(),
213 GR_GL_MAX_SAMPLES,
214 &fMaxSampleCount);
215 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000216}
217
218const GrGLCaps::MSAACoverageMode& GrGLCaps::getMSAACoverageMode(
219 int desiredSampleCount) const {
220 static const MSAACoverageMode kNoneMode = {0, 0};
221 if (0 == fMSAACoverageModes.count()) {
222 return kNoneMode;
223 } else {
224 GrAssert(kNone_CoverageAAType != fCoverageAAType);
225 int max = (fMSAACoverageModes.end() - 1)->fCoverageSampleCnt;
226 desiredSampleCount = GrMin(desiredSampleCount, max);
227 MSAACoverageMode desiredMode = {desiredSampleCount, 0};
228 int idx = SkTSearch<MSAACoverageMode>(&fMSAACoverageModes[0],
229 fMSAACoverageModes.count(),
230 desiredMode,
231 sizeof(MSAACoverageMode),
232 &coverage_mode_compare);
233 if (idx < 0) {
234 idx = ~idx;
235 }
236 GrAssert(idx >= 0 && idx < fMSAACoverageModes.count());
237 return fMSAACoverageModes[idx];
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000238 }
239}
240
241namespace {
242const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
243}
244
245void GrGLCaps::initStencilFormats(const GrGLContextInfo& ctxInfo) {
246
247 // Build up list of legal stencil formats (though perhaps not supported on
248 // the particular gpu/driver) from most preferred to least.
249
250 // these consts are in order of most preferred to least preferred
251 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
252
253 static const StencilFormat
254 // internal Format stencil bits total bits packed?
255 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
256 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
257 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
258 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
259 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
260 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
261
262 if (kDesktop_GrGLBinding == ctxInfo.binding()) {
263 bool supportsPackedDS =
264 ctxInfo.version() >= GR_GL_VER(3,0) ||
265 ctxInfo.hasExtension("GL_EXT_packed_depth_stencil") ||
266 ctxInfo.hasExtension("GL_ARB_framebuffer_object");
267
268 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
269 // require FBO support we can expect these are legal formats and don't
270 // check. These also all support the unsized GL_STENCIL_INDEX.
271 fStencilFormats.push_back() = gS8;
272 fStencilFormats.push_back() = gS16;
273 if (supportsPackedDS) {
274 fStencilFormats.push_back() = gD24S8;
275 }
276 fStencilFormats.push_back() = gS4;
277 if (supportsPackedDS) {
278 fStencilFormats.push_back() = gDS;
279 }
280 } else {
281 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
282 // for other formats.
283 // ES doesn't support using the unsized format.
284
285 fStencilFormats.push_back() = gS8;
286 //fStencilFormats.push_back() = gS16;
287 if (ctxInfo.hasExtension("GL_OES_packed_depth_stencil")) {
288 fStencilFormats.push_back() = gD24S8;
289 }
290 if (ctxInfo.hasExtension("GL_OES_stencil4")) {
291 fStencilFormats.push_back() = gS4;
292 }
293 }
294 GrAssert(0 == fStencilVerifiedColorConfigs.count());
295 fStencilVerifiedColorConfigs.push_back_n(fStencilFormats.count());
296}
297
298void GrGLCaps::markColorConfigAndStencilFormatAsVerified(
299 GrPixelConfig config,
300 const GrGLStencilBuffer::Format& format) {
301#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
302 return;
303#endif
304 GrAssert((unsigned)config < kGrPixelConfigCount);
305 GrAssert(fStencilFormats.count() == fStencilVerifiedColorConfigs.count());
306 int count = fStencilFormats.count();
307 // we expect a really small number of possible formats so linear search
308 // should be OK
309 GrAssert(count < 16);
310 for (int i = 0; i < count; ++i) {
311 if (format.fInternalFormat ==
312 fStencilFormats[i].fInternalFormat) {
313 fStencilVerifiedColorConfigs[i].markVerified(config);
314 return;
315 }
316 }
317 GrCrash("Why are we seeing a stencil format that "
318 "GrGLCaps doesn't know about.");
319}
320
321bool GrGLCaps::isColorConfigAndStencilFormatVerified(
322 GrPixelConfig config,
323 const GrGLStencilBuffer::Format& format) const {
324#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
325 return false;
326#endif
327 GrAssert((unsigned)config < kGrPixelConfigCount);
328 int count = fStencilFormats.count();
329 // we expect a really small number of possible formats so linear search
330 // should be OK
331 GrAssert(count < 16);
332 for (int i = 0; i < count; ++i) {
333 if (format.fInternalFormat ==
334 fStencilFormats[i].fInternalFormat) {
335 return fStencilVerifiedColorConfigs[i].isVerified(config);
336 }
337 }
338 GrCrash("Why are we seeing a stencil format that "
339 "GLCaps doesn't know about.");
340 return false;
341}
342
343void GrGLCaps::print() const {
344 for (int i = 0; i < fStencilFormats.count(); ++i) {
345 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
346 i,
347 fStencilFormats[i].fStencilBits,
348 fStencilFormats[i].fTotalBits);
349 }
350
351 GR_STATIC_ASSERT(0 == kNone_MSFBOType);
352 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBOType);
353 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBOType);
354 GR_STATIC_ASSERT(3 == kAppleES_MSFBOType);
355 static const char* gMSFBOExtStr[] = {
356 "None",
357 "ARB",
358 "EXT",
359 "Apple",
360 };
361 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
362 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
363 GrPrintf("Support RGBA8 Render Buffer: %s\n",
364 (fRGBA8RenderbufferSupport ? "YES": "NO"));
365 GrPrintf("BGRA is an internal format: %s\n",
366 (fBGRAIsInternalFormat ? "YES": "NO"));
367 GrPrintf("Support texture swizzle: %s\n",
368 (fTextureSwizzleSupport ? "YES": "NO"));
369 GrPrintf("Unpack Row length support: %s\n",
370 (fUnpackRowLengthSupport ? "YES": "NO"));
371 GrPrintf("Unpack Flip Y support: %s\n",
372 (fUnpackFlipYSupport ? "YES": "NO"));
373 GrPrintf("Pack Row length support: %s\n",
374 (fPackRowLengthSupport ? "YES": "NO"));
375 GrPrintf("Pack Flip Y support: %s\n",
376 (fPackFlipYSupport ? "YES": "NO"));
377}
378