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