blob: 23c53c68f1be9507a8a71bcac0942180698160b9 [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.com6b0cf022013-05-03 13:35:14 +000080
81 kLast_MSFBOType = kES_EXT_MsToTexture_MSFBOType
82 };
83
84 enum FBFetchType {
85 kNone_FBFetchType,
86 /** GL_EXT_shader_framebuffer_fetch */
87 kEXT_FBFetchType,
88 /** GL_NV_shader_framebuffer_fetch */
89 kNV_FBFetchType,
90
91 kLast_FBFetchType = kNV_FBFetchType,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000092 };
93
bsalomon@google.comc9668ec2012-04-11 18:16:41 +000094 enum CoverageAAType {
95 /**
96 * No coverage sample support
97 */
98 kNone_CoverageAAType,
99
100 /**
101 * GL_NV_framebuffer_multisample_coverage
102 */
103 kNVDesktop_CoverageAAType,
104 };
105
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000106 /**
107 * Creates a GrGLCaps that advertises no support for any extensions,
108 * formats, etc. Call init to initialize from a GrGLContextInfo.
109 */
110 GrGLCaps();
111
112 GrGLCaps(const GrGLCaps& caps);
113
114 GrGLCaps& operator = (const GrGLCaps& caps);
115
116 /**
117 * Resets the caps such that nothing is supported.
118 */
bsalomon@google.combcce8922013-03-25 15:38:39 +0000119 virtual void reset() SK_OVERRIDE;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000120
121 /**
122 * Initializes the GrGLCaps to the set of features supported in the current
123 * OpenGL context accessible via ctxInfo.
124 */
robertphillips@google.com6177e692013-02-28 20:16:25 +0000125 void init(const GrGLContextInfo& ctxInfo, const GrGLInterface* interface);
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000126
127 /**
128 * Call to note that a color config has been verified as a valid color
129 * attachment. This may save future calls to glCheckFramebufferStatus
130 * using isConfigVerifiedColorAttachment().
131 */
132 void markConfigAsValidColorAttachment(GrPixelConfig config) {
133 fVerifiedColorConfigs.markVerified(config);
134 }
135
136 /**
137 * Call to check whether a config has been verified as a valid color
138 * attachment.
139 */
140 bool isConfigVerifiedColorAttachment(GrPixelConfig config) const {
141 return fVerifiedColorConfigs.isVerified(config);
142 }
143
144 /**
145 * Call to note that a color config / stencil format pair passed
146 * FBO status check. We may skip calling glCheckFramebufferStatus for
147 * this combination in the future using
148 * isColorConfigAndStencilFormatVerified().
149 */
150 void markColorConfigAndStencilFormatAsVerified(
151 GrPixelConfig config,
152 const GrGLStencilBuffer::Format& format);
153
154 /**
155 * Call to check whether color config / stencil format pair has already
156 * passed FBO status check.
157 */
158 bool isColorConfigAndStencilFormatVerified(
159 GrPixelConfig config,
160 const GrGLStencilBuffer::Format& format) const;
161
162 /**
163 * Reports the type of MSAA FBO support.
164 */
165 MSFBOType msFBOType() const { return fMSFBOType; }
166
167 /**
bsalomon@google.com347c3822013-05-01 20:10:01 +0000168 * Does the supported MSAA FBO extension have MSAA renderbuffers?
169 */
170 bool usesMSAARenderBuffers() const {
171 return kNone_MSFBOType != fMSFBOType &&
172 kES_IMG_MsToTexture_MSFBOType != fMSFBOType &&
173 kES_EXT_MsToTexture_MSFBOType != fMSFBOType;
174 }
175
176 /**
177 * Is the MSAA FBO extension one where the texture is multisampled when bound to an FBO and
178 * then implicitly resolved when read.
179 */
180 bool usesImplicitMSAAResolve() const {
181 return kES_IMG_MsToTexture_MSFBOType == fMSFBOType ||
182 kES_EXT_MsToTexture_MSFBOType == fMSFBOType;
183 }
184
185 /**
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000186 * Reports the type of coverage sample AA support.
187 */
188 CoverageAAType coverageAAType() const { return fCoverageAAType; }
189
190 /**
191 * Chooses a supported coverage mode based on a desired sample count. The
192 * desired sample count is rounded up the next supported coverage sample
193 * count unless a it is larger than the max in which case it is rounded
194 * down. Once a coverage sample count is decided, the supported mode with
195 * the fewest color samples is chosen.
196 */
197 const MSAACoverageMode& getMSAACoverageMode(int desiredSampleCount) const;
198
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000199 FBFetchType fbFetchType() const { return fFBFetchType; }
200
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000201 /**
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000202 * Prints the caps info using GrPrintf.
203 */
bsalomon@google.combcce8922013-03-25 15:38:39 +0000204 virtual void print() const SK_OVERRIDE;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000205
206 /**
207 * Gets an array of legal stencil formats. These formats are not guaranteed
208 * to be supported by the driver but are legal GLenum names given the GL
209 * version and extensions supported.
210 */
211 const SkTArray<StencilFormat, true>& stencilFormats() const {
212 return fStencilFormats;
213 }
214
215 /// The maximum number of fragment uniform vectors (GLES has min. 16).
216 int maxFragmentUniformVectors() const { return fMaxFragmentUniformVectors; }
217
bsalomon@google.com60da4172012-06-01 19:25:00 +0000218 // maximum number of attribute values per vertex
219 int maxVertexAttributes() const { return fMaxVertexAttributes; }
220
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000221 /// ES requires an extension to support RGBA8 in RenderBufferStorage
222 bool rgba8RenderbufferSupport() const { return fRGBA8RenderbufferSupport; }
223
224 /// Is GL_BGRA supported
225 bool bgraFormatSupport() const { return fBGRAFormatSupport; }
226
227 /**
228 * Depending on the ES extensions present the BGRA external format may
229 * correspond either a BGRA or RGBA internalFormat. On desktop GL it is
230 * RGBA.
231 */
232 bool bgraIsInternalFormat() const { return fBGRAIsInternalFormat; }
233
234 /// GL_ARB_texture_swizzle support
235 bool textureSwizzleSupport() const { return fTextureSwizzleSupport; }
236
237 /// Is there support for GL_UNPACK_ROW_LENGTH
238 bool unpackRowLengthSupport() const { return fUnpackRowLengthSupport; }
239
240 /// Is there support for GL_UNPACK_FLIP_Y
241 bool unpackFlipYSupport() const { return fUnpackFlipYSupport; }
242
243 /// Is there support for GL_PACK_ROW_LENGTH
244 bool packRowLengthSupport() const { return fPackRowLengthSupport; }
245
246 /// Is there support for GL_PACK_REVERSE_ROW_ORDER
247 bool packFlipYSupport() const { return fPackFlipYSupport; }
248
249 /// Is there support for texture parameter GL_TEXTURE_USAGE
250 bool textureUsageSupport() const { return fTextureUsageSupport; }
251
252 /// Is there support for glTexStorage
253 bool texStorageSupport() const { return fTexStorageSupport; }
254
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000255 /// Is there support for GL_RED and GL_R8
256 bool textureRedSupport() const { return fTextureRedSupport; }
257
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000258 /// Is GL_ARB_IMAGING supported
259 bool imagingSupport() const { return fImagingSupport; }
260
bsalomon@google.com706f6682012-10-23 14:53:55 +0000261 /// Is GL_ARB_fragment_coord_conventions supported?
262 bool fragCoordConventionsSupport() const { return fFragCoordsConventionSupport; }
263
bsalomon@google.com07631cf2013-03-05 14:14:58 +0000264 /// Is there support for Vertex Array Objects?
265 bool vertexArrayObjectSupport() const { return fVertexArrayObjectSupport; }
266
267 /// Use indices or vertices in CPU arrays rather than VBOs for dynamic content.
bsalomon@google.com96966a52013-02-21 16:34:21 +0000268 bool useNonVBOVertexAndIndexDynamicData() const {
269 return fUseNonVBOVertexAndIndexDynamicData;
270 }
271
bsalomon@google.com07631cf2013-03-05 14:14:58 +0000272 /// Does ReadPixels support the provided format/type combo?
robertphillips@google.com1d89c932012-06-27 19:31:41 +0000273 bool readPixelsSupported(const GrGLInterface* intf,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000274 GrGLenum format,
robertphillips@google.com1d89c932012-06-27 19:31:41 +0000275 GrGLenum type) const;
skia.committer@gmail.com631cdcb2013-03-01 12:12:55 +0000276
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000277 bool isCoreProfile() const { return fIsCoreProfile; }
robertphillips@google.com1d89c932012-06-27 19:31:41 +0000278
robertphillips@google.coma6ffb582013-04-29 16:50:17 +0000279 /// Is there support for discarding the frame buffer
280 bool discardFBSupport() const { return fDiscardFBSupport; }
281
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000282private:
283 /**
284 * Maintains a bit per GrPixelConfig. It is used to avoid redundantly
285 * performing glCheckFrameBufferStatus for the same config.
286 */
287 struct VerifiedColorConfigs {
288 VerifiedColorConfigs() {
289 this->reset();
290 }
291
292 void reset() {
293 for (int i = 0; i < kNumUints; ++i) {
294 fVerifiedColorConfigs[i] = 0;
295 }
296 }
297
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000298 static const int kNumUints = (kGrPixelConfigCnt + 31) / 32;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000299 uint32_t fVerifiedColorConfigs[kNumUints];
300
301 void markVerified(GrPixelConfig config) {
302#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
303 return;
304#endif
305 int u32Idx = config / 32;
306 int bitIdx = config % 32;
307 fVerifiedColorConfigs[u32Idx] |= 1 << bitIdx;
308 }
309
310 bool isVerified(GrPixelConfig config) const {
311#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
312 return false;
313#endif
314 int u32Idx = config / 32;
315 int bitIdx = config % 32;
316 return SkToBool(fVerifiedColorConfigs[u32Idx] & (1 << bitIdx));
317 }
318 };
319
robertphillips@google.com6177e692013-02-28 20:16:25 +0000320 void initFSAASupport(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli);
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000321 void initStencilFormats(const GrGLContextInfo& ctxInfo);
322
323 // tracks configs that have been verified to pass the FBO completeness when
324 // used as a color attachment
325 VerifiedColorConfigs fVerifiedColorConfigs;
326
327 SkTArray<StencilFormat, true> fStencilFormats;
328 // tracks configs that have been verified to pass the FBO completeness when
329 // used as a color attachment when a particular stencil format is used
330 // as a stencil attachment.
331 SkTArray<VerifiedColorConfigs, true> fStencilVerifiedColorConfigs;
332
333 int fMaxFragmentUniformVectors;
bsalomon@google.com60da4172012-06-01 19:25:00 +0000334 int fMaxVertexAttributes;
335
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000336 MSFBOType fMSFBOType;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000337 CoverageAAType fCoverageAAType;
338 SkTDArray<MSAACoverageMode> fMSAACoverageModes;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000339
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000340 FBFetchType fFBFetchType;
341
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000342 bool fRGBA8RenderbufferSupport : 1;
343 bool fBGRAFormatSupport : 1;
344 bool fBGRAIsInternalFormat : 1;
345 bool fTextureSwizzleSupport : 1;
346 bool fUnpackRowLengthSupport : 1;
347 bool fUnpackFlipYSupport : 1;
348 bool fPackRowLengthSupport : 1;
349 bool fPackFlipYSupport : 1;
350 bool fTextureUsageSupport : 1;
351 bool fTexStorageSupport : 1;
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000352 bool fTextureRedSupport : 1;
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000353 bool fImagingSupport : 1;
robertphillips@google.com1d89c932012-06-27 19:31:41 +0000354 bool fTwoFormatLimit : 1;
bsalomon@google.com706f6682012-10-23 14:53:55 +0000355 bool fFragCoordsConventionSupport : 1;
bsalomon@google.com07631cf2013-03-05 14:14:58 +0000356 bool fVertexArrayObjectSupport : 1;
bsalomon@google.com96966a52013-02-21 16:34:21 +0000357 bool fUseNonVBOVertexAndIndexDynamicData : 1;
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000358 bool fIsCoreProfile : 1;
robertphillips@google.coma6ffb582013-04-29 16:50:17 +0000359 bool fDiscardFBSupport : 1;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000360
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000361 typedef GrDrawTargetCaps INHERITED;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000362};
363
364#endif