blob: 66c75dfebb6c240a570ad900cbde44e9204d0624 [file] [log] [blame]
jvanverthcba99b82015-06-24 06:59:57 -07001/*
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
Brian Salomon94efbf52016-11-29 13:43:05 -05008#ifndef GrShaderCaps_DEFINED
9#define GrShaderCaps_DEFINED
jvanverthcba99b82015-06-24 06:59:57 -070010
Brian Salomon0560bd62018-06-19 14:19:13 -040011#include "GrSwizzle.h"
Brian Salomon23c55b62018-06-19 16:28:41 -040012#include "GrTypesPriv.h"
13#include "SkRefCnt.h"
14#include "glsl/GrGLSL.h"
jvanverthcba99b82015-06-24 06:59:57 -070015
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050016namespace SkSL {
Brian Salomonc7fe0f72018-05-11 10:14:21 -040017class ShaderCapsFactory;
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050018}
Brian Salomonc7fe0f72018-05-11 10:14:21 -040019
Brian Salomon94efbf52016-11-29 13:43:05 -050020struct GrContextOptions;
Brian Osman71a18892017-08-10 10:23:25 -040021class SkJSONWriter;
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050022
Brian Salomon94efbf52016-11-29 13:43:05 -050023class GrShaderCaps : public SkRefCnt {
jvanverthcba99b82015-06-24 06:59:57 -070024public:
jvanverthcba99b82015-06-24 06:59:57 -070025 /**
Brian Salomonc7fe0f72018-05-11 10:14:21 -040026 * Indicates how GLSL must interact with advanced blend equations. The KHR extension requires
27 * special layout qualifiers in the fragment shader.
28 */
jvanverthcba99b82015-06-24 06:59:57 -070029 enum AdvBlendEqInteraction {
30 kNotSupported_AdvBlendEqInteraction, //<! No _blend_equation_advanced extension
31 kAutomatic_AdvBlendEqInteraction, //<! No interaction required
32 kGeneralEnable_AdvBlendEqInteraction, //<! layout(blend_support_all_equations) out
33 kSpecificEnables_AdvBlendEqInteraction, //<! Specific layout qualifiers per equation
34
35 kLast_AdvBlendEqInteraction = kSpecificEnables_AdvBlendEqInteraction
36 };
37
Brian Salomon94efbf52016-11-29 13:43:05 -050038 GrShaderCaps(const GrContextOptions&);
39
Brian Osman71a18892017-08-10 10:23:25 -040040 void dumpJSON(SkJSONWriter*) const;
Brian Salomon94efbf52016-11-29 13:43:05 -050041
Khushal3e7548c2018-05-23 15:45:01 -070042 bool supportsDistanceFieldText() const { return fShaderDerivativeSupport; }
43
Brian Salomon94efbf52016-11-29 13:43:05 -050044 bool shaderDerivativeSupport() const { return fShaderDerivativeSupport; }
45 bool geometryShaderSupport() const { return fGeometryShaderSupport; }
Chris Daltonf1b47bb2017-10-06 11:57:51 -060046 bool gsInvocationsSupport() const { return fGSInvocationsSupport; }
Brian Salomon94efbf52016-11-29 13:43:05 -050047 bool pathRenderingSupport() const { return fPathRenderingSupport; }
48 bool dstReadInShaderSupport() const { return fDstReadInShaderSupport; }
49 bool dualSourceBlendingSupport() const { return fDualSourceBlendingSupport; }
50 bool integerSupport() const { return fIntegerSupport; }
51 bool texelBufferSupport() const { return fTexelBufferSupport; }
52 int imageLoadStoreSupport() const { return fImageLoadStoreSupport; }
53
jvanverthcba99b82015-06-24 06:59:57 -070054 /**
jvanverthcba99b82015-06-24 06:59:57 -070055 * Some helper functions for encapsulating various extensions to read FB Buffer on openglES
56 *
57 * TODO(joshualitt) On desktop opengl 4.2+ we can achieve something similar to this effect
58 */
59 bool fbFetchSupport() const { return fFBFetchSupport; }
60
61 bool fbFetchNeedsCustomOutput() const { return fFBFetchNeedsCustomOutput; }
62
egdaniel472d44e2015-10-22 08:20:00 -070063 const char* versionDeclString() const { return fVersionDeclString; }
64
jvanverthcba99b82015-06-24 06:59:57 -070065 const char* fbFetchColorName() const { return fFBFetchColorName; }
66
67 const char* fbFetchExtensionString() const { return fFBFetchExtensionString; }
68
69 bool dropsTileOnZeroDivide() const { return fDropsTileOnZeroDivide; }
70
cdaltonc08f1962016-02-12 12:14:06 -080071 bool flatInterpolationSupport() const { return fFlatInterpolationSupport; }
72
Brian Salomon41274562017-09-15 09:40:03 -070073 bool preferFlatInterpolation() const { return fPreferFlatInterpolation; }
74
cdaltonc08f1962016-02-12 12:14:06 -080075 bool noperspectiveInterpolationSupport() const { return fNoPerspectiveInterpolationSupport; }
76
cdalton9c3f1432016-03-11 10:07:37 -080077 bool externalTextureSupport() const { return fExternalTextureSupport; }
78
cdaltonf8a6ce82016-04-11 13:02:05 -070079 bool texelFetchSupport() const { return fTexelFetchSupport; }
cdaltonc04ce672016-03-11 14:07:38 -080080
Chris Dalton1d616352017-05-31 12:51:23 -060081 bool vertexIDSupport() const { return fVertexIDSupport; }
82
Chris Dalton7c7ff032018-03-28 20:09:58 -060083 // frexp, ldexp, etc.
84 bool fpManipulationSupport() const { return fFPManipulationSupport; }
85
Chris Dalton47c8ed32017-11-15 18:27:09 -070086 bool floatIs32Bits() const { return fFloatIs32Bits; }
87
88 bool halfIs32Bits() const { return fHalfIs32Bits; }
89
jvanverthcba99b82015-06-24 06:59:57 -070090 AdvBlendEqInteraction advBlendEqInteraction() const { return fAdvBlendEqInteraction; }
91
92 bool mustEnableAdvBlendEqs() const {
93 return fAdvBlendEqInteraction >= kGeneralEnable_AdvBlendEqInteraction;
94 }
95
96 bool mustEnableSpecificAdvBlendEqs() const {
97 return fAdvBlendEqInteraction == kSpecificEnables_AdvBlendEqInteraction;
98 }
halcanary9d524f22016-03-29 09:03:52 -070099
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400100 bool mustDeclareFragmentShaderOutput() const { return fGLSLGeneration > k110_GrGLSLGeneration; }
jvanverthcba99b82015-06-24 06:59:57 -0700101
egdanielf5294392015-10-21 07:14:17 -0700102 bool usesPrecisionModifiers() const { return fUsesPrecisionModifiers; }
103
Greg Daniel80a08dd2017-01-20 10:45:49 -0500104 // Returns whether we can use the glsl function any() in our shader code.
egdaniel472d44e2015-10-22 08:20:00 -0700105 bool canUseAnyFunctionInShader() const { return fCanUseAnyFunctionInShader; }
106
egdaniel8dcdedc2015-11-11 06:27:20 -0800107 bool canUseMinAndAbsTogether() const { return fCanUseMinAndAbsTogether; }
108
Florin Malita8a0044f2017-08-07 14:38:22 -0400109 bool canUseFractForNegativeValues() const { return fCanUseFractForNegativeValues; }
110
egdaniel8dcdedc2015-11-11 06:27:20 -0800111 bool mustForceNegatedAtanParamToFloat() const { return fMustForceNegatedAtanParamToFloat; }
112
Greg Daniel80a08dd2017-01-20 10:45:49 -0500113 // Returns whether a device incorrectly implements atan(y,x) as atan(y/x)
114 bool atan2ImplementedAsAtanYOverX() const { return fAtan2ImplementedAsAtanYOverX; }
115
Greg Daniel10ed2432017-12-01 16:19:43 -0500116 // If this returns true some operation (could be a no op) must be called between floor and abs
117 // to make sure the driver compiler doesn't inline them together which can cause a driver bug in
118 // the shader.
119 bool mustDoOpBetweenFloorAndAbs() const { return fMustDoOpBetweenFloorAndAbs; }
120
Brian Osmancd3261a2018-01-16 13:52:29 +0000121 // If false, SkSL uses a workaround so that sk_FragCoord doesn't actually query gl_FragCoord
122 bool canUseFragCoord() const { return fCanUseFragCoord; }
123
Chris Daltonc2d0dd62018-03-07 07:46:10 -0700124 // If true, short ints can't represent every integer in the 16-bit two's complement range as
125 // required by the spec. SKSL will always emit full ints.
126 bool incompleteShortIntPrecision() const { return fIncompleteShortIntPrecision; }
127
egdaniel138c2632016-08-17 10:59:00 -0700128 bool requiresLocalOutputColorForFBFetch() const { return fRequiresLocalOutputColorForFBFetch; }
129
Brian Osmanac1e4962017-05-25 11:34:38 -0400130 bool mustObfuscateUniformColor() const { return fMustObfuscateUniformColor; }
131
Brian Osmandff5d432017-08-01 14:46:18 -0400132 // The D3D shader compiler, when targeting PS 3.0 (ie within ANGLE) fails to compile certain
133 // constructs. See detailed comments in GrGLCaps.cpp.
134 bool mustGuardDivisionEvenAfterExplicitZeroCheck() const {
135 return fMustGuardDivisionEvenAfterExplicitZeroCheck;
136 }
137
egdaniel574a4c12015-11-02 06:22:44 -0800138 // Returns the string of an extension that must be enabled in the shader to support
139 // derivatives. If nullptr is returned then no extension needs to be enabled. Before calling
140 // this function, the caller should check that shaderDerivativeSupport exists.
141 const char* shaderDerivativeExtensionString() const {
142 SkASSERT(this->shaderDerivativeSupport());
143 return fShaderDerivativeExtensionString;
144 }
cdalton33ad7012016-02-22 07:55:44 -0800145
Chris Dalton8fd79552018-01-11 00:46:14 -0500146 // Returns the string of an extension that must be enabled in the shader to support geometry
147 // shaders. If nullptr is returned then no extension needs to be enabled. Before calling this
148 // function, the caller must verify that geometryShaderSupport exists.
149 const char* geometryShaderExtensionString() const {
150 SkASSERT(this->geometryShaderSupport());
151 return fGeometryShaderExtensionString;
152 }
153
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600154 // Returns the string of an extension that must be enabled in the shader to support
155 // geometry shader invocations. If nullptr is returned then no extension needs to be enabled.
156 // Before calling this function, the caller must verify that gsInvocationsSupport exists.
157 const char* gsInvocationsExtensionString() const {
158 SkASSERT(this->gsInvocationsSupport());
159 return fGSInvocationsExtensionString;
160 }
161
egdaniel8dcdedc2015-11-11 06:27:20 -0800162 // Returns the string of an extension that will do all necessary coord transfomations needed
163 // when reading the fragment position. If such an extension does not exisits, this function
164 // returns a nullptr, and all transforms of the frag position must be done manually in the
165 // shader.
166 const char* fragCoordConventionsExtensionString() const {
167 return fFragCoordConventionsExtensionString;
168 }
169
170 // This returns the name of an extension that must be enabled in the shader, if such a thing is
171 // required in order to use a secondary output in the shader. This returns a nullptr if no such
172 // extension is required. However, the return value of this function does not say whether dual
173 // source blending is supported.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400174 const char* secondaryOutputExtensionString() const { return fSecondaryOutputExtensionString; }
egdaniel574a4c12015-11-02 06:22:44 -0800175
Brian Osman061020e2018-04-17 14:22:15 -0400176 // This returns the name of an extension that must be enabled in the shader to support external
177 // textures. In some cases, two extensions must be enabled - the second extension is returned
178 // by secondExternalTextureExtensionString(). If that function returns nullptr, then only one
179 // extension is required.
bsalomon7ea33f52015-11-22 14:51:00 -0800180 const char* externalTextureExtensionString() const {
cdalton9c3f1432016-03-11 10:07:37 -0800181 SkASSERT(this->externalTextureSupport());
bsalomon7ea33f52015-11-22 14:51:00 -0800182 return fExternalTextureExtensionString;
183 }
184
Brian Osman061020e2018-04-17 14:22:15 -0400185 const char* secondExternalTextureExtensionString() const {
186 SkASSERT(this->externalTextureSupport());
187 return fSecondExternalTextureExtensionString;
188 }
189
cdaltonf8a6ce82016-04-11 13:02:05 -0700190 const char* texelBufferExtensionString() const {
191 SkASSERT(this->texelBufferSupport());
192 return fTexelBufferExtensionString;
cdaltonc04ce672016-03-11 14:07:38 -0800193 }
194
cdaltonc08f1962016-02-12 12:14:06 -0800195 const char* noperspectiveInterpolationExtensionString() const {
196 SkASSERT(this->noperspectiveInterpolationSupport());
197 return fNoPerspectiveInterpolationExtensionString;
198 }
199
Brian Salomonf26f7a02016-11-15 14:05:01 -0500200 const char* imageLoadStoreExtensionString() const {
201 SkASSERT(this->imageLoadStoreSupport());
202 return fImageLoadStoreExtensionString;
203 }
204
cdalton9c3f1432016-03-11 10:07:37 -0800205 int maxVertexSamplers() const { return fMaxVertexSamplers; }
206
207 int maxGeometrySamplers() const { return fMaxGeometrySamplers; }
208
209 int maxFragmentSamplers() const { return fMaxFragmentSamplers; }
210
211 int maxCombinedSamplers() const { return fMaxCombinedSamplers; }
212
Brian Salomon762d5e72017-12-01 10:25:08 -0500213 /**
214 * In general using multiple texture units for image rendering seems to be a win at smaller
215 * sizes of dst rects and a loss at larger sizes. Dst rects above this pixel area threshold will
216 * not use multitexturing.
217 */
218 size_t disableImageMultitexturingDstRectAreaThreshold() const {
219 return fDisableImageMultitexturingDstRectAreaThreshold;
220 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400221
egdanielb7e7d572015-11-04 04:23:53 -0800222 /**
bsalomoncdee0092016-01-08 13:20:12 -0800223 * Given a texture's config, this determines what swizzle must be appended to accesses to the
224 * texture in generated shader code. Swizzling may be implemented in texture parameters or a
bsalomon7f9b2e42016-01-12 13:29:26 -0800225 * sampler rather than in the shader. In this case the returned swizzle will always be "rgba".
egdanielb7e7d572015-11-04 04:23:53 -0800226 */
bsalomoncdee0092016-01-08 13:20:12 -0800227 const GrSwizzle& configTextureSwizzle(GrPixelConfig config) const {
228 return fConfigTextureSwizzle[config];
229 }
egdanielb7e7d572015-11-04 04:23:53 -0800230
bsalomon7f9b2e42016-01-12 13:29:26 -0800231 /** Swizzle that should occur on the fragment shader outputs for a given config. */
232 const GrSwizzle& configOutputSwizzle(GrPixelConfig config) const {
233 return fConfigOutputSwizzle[config];
234 }
235
jvanverthcba99b82015-06-24 06:59:57 -0700236 GrGLSLGeneration generation() const { return fGLSLGeneration; }
237
jvanverthcba99b82015-06-24 06:59:57 -0700238private:
Brian Salomon94efbf52016-11-29 13:43:05 -0500239 void applyOptionsOverrides(const GrContextOptions& options);
egdanielb7e7d572015-11-04 04:23:53 -0800240
jvanverthcba99b82015-06-24 06:59:57 -0700241 GrGLSLGeneration fGLSLGeneration;
halcanary9d524f22016-03-29 09:03:52 -0700242
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400243 bool fShaderDerivativeSupport : 1;
244 bool fGeometryShaderSupport : 1;
245 bool fGSInvocationsSupport : 1;
246 bool fPathRenderingSupport : 1;
247 bool fDstReadInShaderSupport : 1;
248 bool fDualSourceBlendingSupport : 1;
249 bool fIntegerSupport : 1;
250 bool fTexelBufferSupport : 1;
251 bool fImageLoadStoreSupport : 1;
252 bool fDropsTileOnZeroDivide : 1;
253 bool fFBFetchSupport : 1;
254 bool fFBFetchNeedsCustomOutput : 1;
255 bool fUsesPrecisionModifiers : 1;
256 bool fFlatInterpolationSupport : 1;
257 bool fPreferFlatInterpolation : 1;
cdaltonc08f1962016-02-12 12:14:06 -0800258 bool fNoPerspectiveInterpolationSupport : 1;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400259 bool fExternalTextureSupport : 1;
260 bool fTexelFetchSupport : 1;
261 bool fVertexIDSupport : 1;
262 bool fFPManipulationSupport : 1;
263 bool fFloatIs32Bits : 1;
264 bool fHalfIs32Bits : 1;
egdanielf5294392015-10-21 07:14:17 -0700265
egdaniel8dcdedc2015-11-11 06:27:20 -0800266 // Used for specific driver bug work arounds
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400267 bool fCanUseAnyFunctionInShader : 1;
268 bool fCanUseMinAndAbsTogether : 1;
269 bool fCanUseFractForNegativeValues : 1;
270 bool fMustForceNegatedAtanParamToFloat : 1;
271 bool fAtan2ImplementedAsAtanYOverX : 1;
272 bool fMustDoOpBetweenFloorAndAbs : 1;
273 bool fRequiresLocalOutputColorForFBFetch : 1;
274 bool fMustObfuscateUniformColor : 1;
Brian Osmandff5d432017-08-01 14:46:18 -0400275 bool fMustGuardDivisionEvenAfterExplicitZeroCheck : 1;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400276 bool fCanUseFragCoord : 1;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400277 bool fIncompleteShortIntPrecision : 1;
egdaniel8dcdedc2015-11-11 06:27:20 -0800278
egdaniel472d44e2015-10-22 08:20:00 -0700279 const char* fVersionDeclString;
jvanverthcba99b82015-06-24 06:59:57 -0700280
egdaniel574a4c12015-11-02 06:22:44 -0800281 const char* fShaderDerivativeExtensionString;
Chris Dalton8fd79552018-01-11 00:46:14 -0500282 const char* fGeometryShaderExtensionString;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600283 const char* fGSInvocationsExtensionString;
egdaniel8dcdedc2015-11-11 06:27:20 -0800284 const char* fFragCoordConventionsExtensionString;
285 const char* fSecondaryOutputExtensionString;
bsalomon7ea33f52015-11-22 14:51:00 -0800286 const char* fExternalTextureExtensionString;
Brian Osman061020e2018-04-17 14:22:15 -0400287 const char* fSecondExternalTextureExtensionString;
cdaltonf8a6ce82016-04-11 13:02:05 -0700288 const char* fTexelBufferExtensionString;
cdaltonc08f1962016-02-12 12:14:06 -0800289 const char* fNoPerspectiveInterpolationExtensionString;
Brian Salomonf26f7a02016-11-15 14:05:01 -0500290 const char* fImageLoadStoreExtensionString;
egdaniel574a4c12015-11-02 06:22:44 -0800291
jvanverthcba99b82015-06-24 06:59:57 -0700292 const char* fFBFetchColorName;
293 const char* fFBFetchExtensionString;
294
jvanverthe78d4872016-09-27 03:33:05 -0700295 int fMaxVertexSamplers;
296 int fMaxGeometrySamplers;
297 int fMaxFragmentSamplers;
298 int fMaxCombinedSamplers;
cdalton9c3f1432016-03-11 10:07:37 -0800299
Brian Salomon762d5e72017-12-01 10:25:08 -0500300 size_t fDisableImageMultitexturingDstRectAreaThreshold;
301
jvanverthcba99b82015-06-24 06:59:57 -0700302 AdvBlendEqInteraction fAdvBlendEqInteraction;
303
bsalomoncdee0092016-01-08 13:20:12 -0800304 GrSwizzle fConfigTextureSwizzle[kGrPixelConfigCnt];
bsalomon7f9b2e42016-01-12 13:29:26 -0800305 GrSwizzle fConfigOutputSwizzle[kGrPixelConfigCnt];
egdanielb7e7d572015-11-04 04:23:53 -0800306
Chris Dalton040238b2017-12-18 14:22:34 -0700307 friend class GrCaps; // For initialization.
308 friend class GrGLCaps;
Chris Daltonfddb6c02017-11-04 15:22:22 -0600309 friend class GrMockCaps;
Greg Danielcebcb842017-07-31 10:45:52 -0400310 friend class GrMtlCaps;
egdanielfa896322016-01-13 12:19:30 -0800311 friend class GrVkCaps;
Brian Salomonf1dd6772016-11-29 15:27:52 -0500312 friend class SkSL::ShaderCapsFactory;
jvanverthcba99b82015-06-24 06:59:57 -0700313};
314
jvanverthcba99b82015-06-24 06:59:57 -0700315#endif