blob: 7b2bb35eadcb1c4421d3b59d6728091777419032 [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 {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000101 if (ctxInfo.hasExtension("GL_APPLE_texture_format_BGRA8888")) {
102 fBGRAFormatSupport = true;
103 } else if (ctxInfo.hasExtension("GL_EXT_texture_format_BGRA8888")) {
104 fBGRAFormatSupport = true;
105 fBGRAIsInternalFormat = true;
106 }
107 GrAssert(fBGRAFormatSupport ||
108 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
109 }
110
111 if (kDesktop_GrGLBinding == binding) {
112 fTextureSwizzleSupport = version >= GR_GL_VER(3,3) ||
113 ctxInfo.hasExtension("GL_ARB_texture_swizzle");
114 } else {
115 fTextureSwizzleSupport = false;
116 }
117
118 if (kDesktop_GrGLBinding == binding) {
119 fUnpackRowLengthSupport = true;
120 fUnpackFlipYSupport = false;
121 fPackRowLengthSupport = true;
122 fPackFlipYSupport = false;
123 } else {
124 fUnpackRowLengthSupport =ctxInfo.hasExtension("GL_EXT_unpack_subimage");
125 fUnpackFlipYSupport = ctxInfo.hasExtension("GL_CHROMIUM_flipy");
126 // no extension for pack row length
127 fPackRowLengthSupport = false;
128 fPackFlipYSupport =
129 ctxInfo.hasExtension("GL_ANGLE_pack_reverse_row_order");
130 }
131
132 fTextureUsageSupport = (kES2_GrGLBinding == binding) &&
133 ctxInfo.hasExtension("GL_ANGLE_texture_usage");
134
135 // Tex storage is in desktop 4.2 and can be an extension to desktop or ES.
136 fTexStorageSupport = (kDesktop_GrGLBinding == binding &&
137 version >= GR_GL_VER(4,2)) ||
138 ctxInfo.hasExtension("GL_ARB_texture_storage") ||
139 ctxInfo.hasExtension("GL_EXT_texture_storage");
140
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000141 // ARB_texture_rg is part of OpenGL 3.0
142 if (kDesktop_GrGLBinding == binding) {
143 fTextureRedSupport = version >= GR_GL_VER(3,0) ||
144 ctxInfo.hasExtension("GL_ARB_texture_rg");
145 } else {
146 fTextureRedSupport = ctxInfo.hasExtension("GL_EXT_texture_rg");
147 }
148
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000149 this->initFSAASupport(ctxInfo);
150 this->initStencilFormats(ctxInfo);
151}
152
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000153namespace {
154int coverage_mode_compare(const GrGLCaps::MSAACoverageMode* left,
155 const GrGLCaps::MSAACoverageMode* right) {
156 if (left->fCoverageSampleCnt < right->fCoverageSampleCnt) {
157 return -1;
158 } else if (right->fCoverageSampleCnt < left->fCoverageSampleCnt) {
159 return 1;
160 } else if (left->fColorSampleCnt < right->fColorSampleCnt) {
161 return -1;
162 } else if (right->fColorSampleCnt < left->fColorSampleCnt) {
163 return 1;
164 }
165 return 0;
166}
167}
168
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000169void GrGLCaps::initFSAASupport(const GrGLContextInfo& ctxInfo) {
170
171 fMSFBOType = kNone_MSFBOType;
172 if (kDesktop_GrGLBinding != ctxInfo.binding()) {
173 if (ctxInfo.hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
174 // chrome's extension is equivalent to the EXT msaa
175 // and fbo_blit extensions.
176 fMSFBOType = kDesktopEXT_MSFBOType;
177 } else if (ctxInfo.hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000178 fMSFBOType = kAppleES_MSFBOType;
179 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000180 } else {
181 if ((ctxInfo.version() >= GR_GL_VER(3,0)) ||
182 ctxInfo.hasExtension("GL_ARB_framebuffer_object")) {
183 fMSFBOType = GrGLCaps::kDesktopARB_MSFBOType;
184 } else if (ctxInfo.hasExtension("GL_EXT_framebuffer_multisample") &&
185 ctxInfo.hasExtension("GL_EXT_framebuffer_blit")) {
186 fMSFBOType = GrGLCaps::kDesktopEXT_MSFBOType;
187 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000188 // TODO: We could populate fMSAACoverageModes using GetInternalformativ
189 // on GL 4.2+. It's format-specific, though. See also
190 // http://code.google.com/p/skia/issues/detail?id=470 about using actual
191 // rather than requested sample counts in cache key.
192 if (ctxInfo.hasExtension("GL_NV_framebuffer_multisample_coverage")) {
193 fCoverageAAType = kNVDesktop_CoverageAAType;
194 GrGLint count;
195 GR_GL_GetIntegerv(ctxInfo.interface(),
196 GR_GL_MAX_MULTISAMPLE_COVERAGE_MODES,
197 &count);
198 fMSAACoverageModes.setCount(count);
199 GR_GL_GetIntegerv(ctxInfo.interface(),
200 GR_GL_MULTISAMPLE_COVERAGE_MODES,
201 (int*)&fMSAACoverageModes[0]);
202 // The NV driver seems to return the modes already sorted but the
203 // spec doesn't require this. So we sort.
reed@google.comc7a67cb2012-05-07 14:52:12 +0000204 qsort(&fMSAACoverageModes[0],
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000205 count,
206 sizeof(MSAACoverageMode),
reed@google.comc7a67cb2012-05-07 14:52:12 +0000207 SkCastForQSort(coverage_mode_compare));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000208 }
209 }
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000210 if (kNone_MSFBOType != fMSFBOType) {
211 GR_GL_GetIntegerv(ctxInfo.interface(),
212 GR_GL_MAX_SAMPLES,
213 &fMaxSampleCount);
214 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000215}
216
217const GrGLCaps::MSAACoverageMode& GrGLCaps::getMSAACoverageMode(
218 int desiredSampleCount) const {
219 static const MSAACoverageMode kNoneMode = {0, 0};
220 if (0 == fMSAACoverageModes.count()) {
221 return kNoneMode;
222 } else {
223 GrAssert(kNone_CoverageAAType != fCoverageAAType);
224 int max = (fMSAACoverageModes.end() - 1)->fCoverageSampleCnt;
225 desiredSampleCount = GrMin(desiredSampleCount, max);
226 MSAACoverageMode desiredMode = {desiredSampleCount, 0};
227 int idx = SkTSearch<MSAACoverageMode>(&fMSAACoverageModes[0],
228 fMSAACoverageModes.count(),
229 desiredMode,
230 sizeof(MSAACoverageMode),
231 &coverage_mode_compare);
232 if (idx < 0) {
233 idx = ~idx;
234 }
235 GrAssert(idx >= 0 && idx < fMSAACoverageModes.count());
236 return fMSAACoverageModes[idx];
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000237 }
238}
239
240namespace {
241const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
242}
243
244void GrGLCaps::initStencilFormats(const GrGLContextInfo& ctxInfo) {
245
246 // Build up list of legal stencil formats (though perhaps not supported on
247 // the particular gpu/driver) from most preferred to least.
248
249 // these consts are in order of most preferred to least preferred
250 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
251
252 static const StencilFormat
253 // internal Format stencil bits total bits packed?
254 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
255 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
256 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
257 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000258 // gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000259 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
260
261 if (kDesktop_GrGLBinding == ctxInfo.binding()) {
262 bool supportsPackedDS =
263 ctxInfo.version() >= GR_GL_VER(3,0) ||
264 ctxInfo.hasExtension("GL_EXT_packed_depth_stencil") ||
265 ctxInfo.hasExtension("GL_ARB_framebuffer_object");
266
267 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
268 // require FBO support we can expect these are legal formats and don't
269 // check. These also all support the unsized GL_STENCIL_INDEX.
270 fStencilFormats.push_back() = gS8;
271 fStencilFormats.push_back() = gS16;
272 if (supportsPackedDS) {
273 fStencilFormats.push_back() = gD24S8;
274 }
275 fStencilFormats.push_back() = gS4;
276 if (supportsPackedDS) {
277 fStencilFormats.push_back() = gDS;
278 }
279 } else {
280 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
281 // for other formats.
282 // ES doesn't support using the unsized format.
283
284 fStencilFormats.push_back() = gS8;
285 //fStencilFormats.push_back() = gS16;
286 if (ctxInfo.hasExtension("GL_OES_packed_depth_stencil")) {
287 fStencilFormats.push_back() = gD24S8;
288 }
289 if (ctxInfo.hasExtension("GL_OES_stencil4")) {
290 fStencilFormats.push_back() = gS4;
291 }
292 }
293 GrAssert(0 == fStencilVerifiedColorConfigs.count());
294 fStencilVerifiedColorConfigs.push_back_n(fStencilFormats.count());
295}
296
297void GrGLCaps::markColorConfigAndStencilFormatAsVerified(
298 GrPixelConfig config,
299 const GrGLStencilBuffer::Format& format) {
300#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
301 return;
302#endif
303 GrAssert((unsigned)config < kGrPixelConfigCount);
304 GrAssert(fStencilFormats.count() == fStencilVerifiedColorConfigs.count());
305 int count = fStencilFormats.count();
306 // we expect a really small number of possible formats so linear search
307 // should be OK
308 GrAssert(count < 16);
309 for (int i = 0; i < count; ++i) {
310 if (format.fInternalFormat ==
311 fStencilFormats[i].fInternalFormat) {
312 fStencilVerifiedColorConfigs[i].markVerified(config);
313 return;
314 }
315 }
316 GrCrash("Why are we seeing a stencil format that "
317 "GrGLCaps doesn't know about.");
318}
319
320bool GrGLCaps::isColorConfigAndStencilFormatVerified(
321 GrPixelConfig config,
322 const GrGLStencilBuffer::Format& format) const {
323#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
324 return false;
325#endif
326 GrAssert((unsigned)config < kGrPixelConfigCount);
327 int count = fStencilFormats.count();
328 // we expect a really small number of possible formats so linear search
329 // should be OK
330 GrAssert(count < 16);
331 for (int i = 0; i < count; ++i) {
332 if (format.fInternalFormat ==
333 fStencilFormats[i].fInternalFormat) {
334 return fStencilVerifiedColorConfigs[i].isVerified(config);
335 }
336 }
337 GrCrash("Why are we seeing a stencil format that "
338 "GLCaps doesn't know about.");
339 return false;
340}
341
342void GrGLCaps::print() const {
343 for (int i = 0; i < fStencilFormats.count(); ++i) {
344 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
345 i,
346 fStencilFormats[i].fStencilBits,
347 fStencilFormats[i].fTotalBits);
348 }
349
350 GR_STATIC_ASSERT(0 == kNone_MSFBOType);
351 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBOType);
352 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBOType);
353 GR_STATIC_ASSERT(3 == kAppleES_MSFBOType);
354 static const char* gMSFBOExtStr[] = {
355 "None",
356 "ARB",
357 "EXT",
358 "Apple",
359 };
360 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
361 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
362 GrPrintf("Support RGBA8 Render Buffer: %s\n",
363 (fRGBA8RenderbufferSupport ? "YES": "NO"));
364 GrPrintf("BGRA is an internal format: %s\n",
365 (fBGRAIsInternalFormat ? "YES": "NO"));
366 GrPrintf("Support texture swizzle: %s\n",
367 (fTextureSwizzleSupport ? "YES": "NO"));
368 GrPrintf("Unpack Row length support: %s\n",
369 (fUnpackRowLengthSupport ? "YES": "NO"));
370 GrPrintf("Unpack Flip Y support: %s\n",
371 (fUnpackFlipYSupport ? "YES": "NO"));
372 GrPrintf("Pack Row length support: %s\n",
373 (fPackRowLengthSupport ? "YES": "NO"));
374 GrPrintf("Pack Flip Y support: %s\n",
375 (fPackFlipYSupport ? "YES": "NO"));
376}
377