blob: 1ce5fa6bd97f4724f5e523ac17d7d8bc1e4dc118 [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#ifndef GrGLCaps_DEFINED
10#define GrGLCaps_DEFINED
11
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000012#include "GrDrawTargetCaps.h"
13#include "GrGLStencilBuffer.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000014#include "SkTArray.h"
15#include "SkTDArray.h"
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000016
17class GrGLContextInfo;
18
19/**
20 * Stores some capabilities of a GL context. Most are determined by the GL
21 * version and the extensions string. It also tracks formats that have passed
22 * the FBO completeness test.
23 */
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000024class GrGLCaps : public GrDrawTargetCaps {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000025public:
bsalomon@google.combcce8922013-03-25 15:38:39 +000026 SK_DECLARE_INST_COUNT(GrGLCaps)
27
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000028 typedef GrGLStencilBuffer::Format StencilFormat;
29
30 /**
bsalomon@google.comc9668ec2012-04-11 18:16:41 +000031 * Represents a supported multisampling/coverage-sampling mode.
32 */
33 struct MSAACoverageMode {
34 // "Coverage samples" includes samples that actually have color, depth,
35 // stencil, ... as well as those that don't (coverage only). All samples
36 // are coverage samples. (We're using the word "coverage sample" to
37 // match the NV extension language.)
38 int fCoverageSampleCnt;
39
40 // Color samples are samples that store data values (color, stencil,
41 // depth) rather than just representing coverage. They are a subset
42 // of coverage samples. (Again the wording was chosen to match the
43 // extension.)
44 int fColorSampleCnt;
45 };
46
47 /**
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000048 * The type of MSAA for FBOs supported. Different extensions have different
49 * semantics of how / when a resolve is performed.
50 */
51 enum MSFBOType {
52 /**
53 * no support for MSAA FBOs
54 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +000055 kNone_MSFBOType = 0,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000056 /**
57 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
58 */
bsalomon@google.com347c3822013-05-01 20:10:01 +000059 kDesktop_ARB_MSFBOType,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000060 /**
61 * earlier GL_EXT_framebuffer* extensions
62 */
bsalomon@google.com347c3822013-05-01 20:10:01 +000063 kDesktop_EXT_MSFBOType,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000064 /**
65 * GL_APPLE_framebuffer_multisample ES extension
66 */
bsalomon@google.com347c3822013-05-01 20:10:01 +000067 kES_Apple_MSFBOType,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +000068 /**
bsalomon@google.com347c3822013-05-01 20:10:01 +000069 * GL_IMG_multisampled_render_to_texture. This variation does not have MSAA renderbuffers.
70 * Instead the texture is multisampled when bound to the FBO and then resolved automatically
71 * when read. It also defines an alternate value for GL_MAX_SAMPLES (which we call
72 * GR_GL_MAX_SAMPLES_IMG).
bsalomon@google.comf3a60c02013-03-19 19:06:09 +000073 */
bsalomon@google.com347c3822013-05-01 20:10:01 +000074 kES_IMG_MsToTexture_MSFBOType,
75 /**
76 * GL_EXT_multisampled_render_to_texture. Same as the IMG one above but uses the standard
77 * GL_MAX_SAMPLES value.
78 */
79 kES_EXT_MsToTexture_MSFBOType,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000080 };
81
bsalomon@google.comc9668ec2012-04-11 18:16:41 +000082 enum CoverageAAType {
83 /**
84 * No coverage sample support
85 */
86 kNone_CoverageAAType,
87
88 /**
89 * GL_NV_framebuffer_multisample_coverage
90 */
91 kNVDesktop_CoverageAAType,
92 };
93
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000094 /**
95 * Creates a GrGLCaps that advertises no support for any extensions,
96 * formats, etc. Call init to initialize from a GrGLContextInfo.
97 */
98 GrGLCaps();
99
100 GrGLCaps(const GrGLCaps& caps);
101
102 GrGLCaps& operator = (const GrGLCaps& caps);
103
104 /**
105 * Resets the caps such that nothing is supported.
106 */
bsalomon@google.combcce8922013-03-25 15:38:39 +0000107 virtual void reset() SK_OVERRIDE;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000108
109 /**
110 * Initializes the GrGLCaps to the set of features supported in the current
111 * OpenGL context accessible via ctxInfo.
112 */
robertphillips@google.com6177e692013-02-28 20:16:25 +0000113 void init(const GrGLContextInfo& ctxInfo, const GrGLInterface* interface);
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000114
115 /**
116 * Call to note that a color config has been verified as a valid color
117 * attachment. This may save future calls to glCheckFramebufferStatus
118 * using isConfigVerifiedColorAttachment().
119 */
120 void markConfigAsValidColorAttachment(GrPixelConfig config) {
121 fVerifiedColorConfigs.markVerified(config);
122 }
123
124 /**
125 * Call to check whether a config has been verified as a valid color
126 * attachment.
127 */
128 bool isConfigVerifiedColorAttachment(GrPixelConfig config) const {
129 return fVerifiedColorConfigs.isVerified(config);
130 }
131
132 /**
133 * Call to note that a color config / stencil format pair passed
134 * FBO status check. We may skip calling glCheckFramebufferStatus for
135 * this combination in the future using
136 * isColorConfigAndStencilFormatVerified().
137 */
138 void markColorConfigAndStencilFormatAsVerified(
139 GrPixelConfig config,
140 const GrGLStencilBuffer::Format& format);
141
142 /**
143 * Call to check whether color config / stencil format pair has already
144 * passed FBO status check.
145 */
146 bool isColorConfigAndStencilFormatVerified(
147 GrPixelConfig config,
148 const GrGLStencilBuffer::Format& format) const;
149
150 /**
151 * Reports the type of MSAA FBO support.
152 */
153 MSFBOType msFBOType() const { return fMSFBOType; }
154
155 /**
bsalomon@google.com347c3822013-05-01 20:10:01 +0000156 * Does the supported MSAA FBO extension have MSAA renderbuffers?
157 */
158 bool usesMSAARenderBuffers() const {
159 return kNone_MSFBOType != fMSFBOType &&
160 kES_IMG_MsToTexture_MSFBOType != fMSFBOType &&
161 kES_EXT_MsToTexture_MSFBOType != fMSFBOType;
162 }
163
164 /**
165 * Is the MSAA FBO extension one where the texture is multisampled when bound to an FBO and
166 * then implicitly resolved when read.
167 */
168 bool usesImplicitMSAAResolve() const {
169 return kES_IMG_MsToTexture_MSFBOType == fMSFBOType ||
170 kES_EXT_MsToTexture_MSFBOType == fMSFBOType;
171 }
172
173 /**
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000174 * Reports the type of coverage sample AA support.
175 */
176 CoverageAAType coverageAAType() const { return fCoverageAAType; }
177
178 /**
179 * Chooses a supported coverage mode based on a desired sample count. The
180 * desired sample count is rounded up the next supported coverage sample
181 * count unless a it is larger than the max in which case it is rounded
182 * down. Once a coverage sample count is decided, the supported mode with
183 * the fewest color samples is chosen.
184 */
185 const MSAACoverageMode& getMSAACoverageMode(int desiredSampleCount) const;
186
187 /**
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000188 * Prints the caps info using GrPrintf.
189 */
bsalomon@google.combcce8922013-03-25 15:38:39 +0000190 virtual void print() const SK_OVERRIDE;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000191
192 /**
193 * Gets an array of legal stencil formats. These formats are not guaranteed
194 * to be supported by the driver but are legal GLenum names given the GL
195 * version and extensions supported.
196 */
197 const SkTArray<StencilFormat, true>& stencilFormats() const {
198 return fStencilFormats;
199 }
200
201 /// The maximum number of fragment uniform vectors (GLES has min. 16).
202 int maxFragmentUniformVectors() const { return fMaxFragmentUniformVectors; }
203
bsalomon@google.com60da4172012-06-01 19:25:00 +0000204 // maximum number of attribute values per vertex
205 int maxVertexAttributes() const { return fMaxVertexAttributes; }
206
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000207 /// ES requires an extension to support RGBA8 in RenderBufferStorage
208 bool rgba8RenderbufferSupport() const { return fRGBA8RenderbufferSupport; }
209
210 /// Is GL_BGRA supported
211 bool bgraFormatSupport() const { return fBGRAFormatSupport; }
212
213 /**
214 * Depending on the ES extensions present the BGRA external format may
215 * correspond either a BGRA or RGBA internalFormat. On desktop GL it is
216 * RGBA.
217 */
218 bool bgraIsInternalFormat() const { return fBGRAIsInternalFormat; }
219
220 /// GL_ARB_texture_swizzle support
221 bool textureSwizzleSupport() const { return fTextureSwizzleSupport; }
222
223 /// Is there support for GL_UNPACK_ROW_LENGTH
224 bool unpackRowLengthSupport() const { return fUnpackRowLengthSupport; }
225
226 /// Is there support for GL_UNPACK_FLIP_Y
227 bool unpackFlipYSupport() const { return fUnpackFlipYSupport; }
228
229 /// Is there support for GL_PACK_ROW_LENGTH
230 bool packRowLengthSupport() const { return fPackRowLengthSupport; }
231
232 /// Is there support for GL_PACK_REVERSE_ROW_ORDER
233 bool packFlipYSupport() const { return fPackFlipYSupport; }
234
235 /// Is there support for texture parameter GL_TEXTURE_USAGE
236 bool textureUsageSupport() const { return fTextureUsageSupport; }
237
238 /// Is there support for glTexStorage
239 bool texStorageSupport() const { return fTexStorageSupport; }
240
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000241 /// Is there support for GL_RED and GL_R8
242 bool textureRedSupport() const { return fTextureRedSupport; }
243
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000244 /// Is GL_ARB_IMAGING supported
245 bool imagingSupport() const { return fImagingSupport; }
246
bsalomon@google.com706f6682012-10-23 14:53:55 +0000247 /// Is GL_ARB_fragment_coord_conventions supported?
248 bool fragCoordConventionsSupport() const { return fFragCoordsConventionSupport; }
249
bsalomon@google.com07631cf2013-03-05 14:14:58 +0000250 /// Is there support for Vertex Array Objects?
251 bool vertexArrayObjectSupport() const { return fVertexArrayObjectSupport; }
252
253 /// Use indices or vertices in CPU arrays rather than VBOs for dynamic content.
bsalomon@google.com96966a52013-02-21 16:34:21 +0000254 bool useNonVBOVertexAndIndexDynamicData() const {
255 return fUseNonVBOVertexAndIndexDynamicData;
256 }
257
bsalomon@google.com07631cf2013-03-05 14:14:58 +0000258 /// Does ReadPixels support the provided format/type combo?
robertphillips@google.com1d89c932012-06-27 19:31:41 +0000259 bool readPixelsSupported(const GrGLInterface* intf,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000260 GrGLenum format,
robertphillips@google.com1d89c932012-06-27 19:31:41 +0000261 GrGLenum type) const;
skia.committer@gmail.com631cdcb2013-03-01 12:12:55 +0000262
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000263 bool isCoreProfile() const { return fIsCoreProfile; }
robertphillips@google.com1d89c932012-06-27 19:31:41 +0000264
robertphillips@google.coma6ffb582013-04-29 16:50:17 +0000265 /// Is there support for discarding the frame buffer
266 bool discardFBSupport() const { return fDiscardFBSupport; }
267
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000268private:
269 /**
270 * Maintains a bit per GrPixelConfig. It is used to avoid redundantly
271 * performing glCheckFrameBufferStatus for the same config.
272 */
273 struct VerifiedColorConfigs {
274 VerifiedColorConfigs() {
275 this->reset();
276 }
277
278 void reset() {
279 for (int i = 0; i < kNumUints; ++i) {
280 fVerifiedColorConfigs[i] = 0;
281 }
282 }
283
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000284 static const int kNumUints = (kGrPixelConfigCnt + 31) / 32;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000285 uint32_t fVerifiedColorConfigs[kNumUints];
286
287 void markVerified(GrPixelConfig config) {
288#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
289 return;
290#endif
291 int u32Idx = config / 32;
292 int bitIdx = config % 32;
293 fVerifiedColorConfigs[u32Idx] |= 1 << bitIdx;
294 }
295
296 bool isVerified(GrPixelConfig config) const {
297#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
298 return false;
299#endif
300 int u32Idx = config / 32;
301 int bitIdx = config % 32;
302 return SkToBool(fVerifiedColorConfigs[u32Idx] & (1 << bitIdx));
303 }
304 };
305
robertphillips@google.com6177e692013-02-28 20:16:25 +0000306 void initFSAASupport(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli);
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000307 void initStencilFormats(const GrGLContextInfo& ctxInfo);
308
309 // tracks configs that have been verified to pass the FBO completeness when
310 // used as a color attachment
311 VerifiedColorConfigs fVerifiedColorConfigs;
312
313 SkTArray<StencilFormat, true> fStencilFormats;
314 // tracks configs that have been verified to pass the FBO completeness when
315 // used as a color attachment when a particular stencil format is used
316 // as a stencil attachment.
317 SkTArray<VerifiedColorConfigs, true> fStencilVerifiedColorConfigs;
318
319 int fMaxFragmentUniformVectors;
bsalomon@google.com60da4172012-06-01 19:25:00 +0000320 int fMaxVertexAttributes;
321
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000322 MSFBOType fMSFBOType;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000323 CoverageAAType fCoverageAAType;
324 SkTDArray<MSAACoverageMode> fMSAACoverageModes;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000325
326 bool fRGBA8RenderbufferSupport : 1;
327 bool fBGRAFormatSupport : 1;
328 bool fBGRAIsInternalFormat : 1;
329 bool fTextureSwizzleSupport : 1;
330 bool fUnpackRowLengthSupport : 1;
331 bool fUnpackFlipYSupport : 1;
332 bool fPackRowLengthSupport : 1;
333 bool fPackFlipYSupport : 1;
334 bool fTextureUsageSupport : 1;
335 bool fTexStorageSupport : 1;
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000336 bool fTextureRedSupport : 1;
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000337 bool fImagingSupport : 1;
robertphillips@google.com1d89c932012-06-27 19:31:41 +0000338 bool fTwoFormatLimit : 1;
bsalomon@google.com706f6682012-10-23 14:53:55 +0000339 bool fFragCoordsConventionSupport : 1;
bsalomon@google.com07631cf2013-03-05 14:14:58 +0000340 bool fVertexArrayObjectSupport : 1;
bsalomon@google.com96966a52013-02-21 16:34:21 +0000341 bool fUseNonVBOVertexAndIndexDynamicData : 1;
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000342 bool fIsCoreProfile : 1;
robertphillips@google.coma6ffb582013-04-29 16:50:17 +0000343 bool fDiscardFBSupport : 1;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000344
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000345 typedef GrDrawTargetCaps INHERITED;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000346};
347
348#endif