blob: 797abb4d80e9b5f246fa2d202eadf510f52beabe [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.
203 SkQSortCompareProc compareProc =
204 reinterpret_cast<SkQSortCompareProc>(&coverage_mode_compare);
205 SkQSort(&fMSAACoverageModes[0],
206 count,
207 sizeof(MSAACoverageMode),
208 compareProc);
209 }
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