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