Debian Codegraph @

src/sksl/SkSLUtil.h

raw
1 /*
2 * Copyright 2016 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 #ifndef SKSL_UTIL
9 #define SKSL_UTIL
10
11 #include "include/core/SkTypes.h"
12 #include "include/sksl/SkSLVersion.h"
13 #include "src/sksl/SkSLGLSL.h"
14
15 #include <memory>
16
17 enum class SkSLType : char;
18
19 namespace SkSL {
20
21 class Context;
22 class OutputStream;
23 class StringStream;
24 class Type;
25
26 struct ShaderCaps {
27 /**
28 * Indicates how GLSL must interact with advanced blend equations. The KHR extension requires
29 * special layout qualifiers in the fragment shader.
30 */
31 enum AdvBlendEqInteraction {
32 kNotSupported_AdvBlendEqInteraction, //<! No _blend_equation_advanced extension
33 kAutomatic_AdvBlendEqInteraction, //<! No interaction required
34 kGeneralEnable_AdvBlendEqInteraction, //<! layout(blend_support_all_equations) out
35
36 kLast_AdvBlendEqInteraction = kGeneralEnable_AdvBlendEqInteraction
37 };
38
39 bool mustEnableAdvBlendEqs() const {
40 return fAdvBlendEqInteraction >= kGeneralEnable_AdvBlendEqInteraction;
41 }
42
43 bool mustDeclareFragmentShaderOutput() const {
44 return fGLSLGeneration > SkSL::GLSLGeneration::k110;
45 }
46
47 /**
48 * Returns the string of an extension that must be enabled in the shader to support derivatives.
49 * If nullptr is returned then no extension needs to be enabled. Before calling this function,
50 * the caller should check that shaderDerivativeSupport exists.
51 */
52 const char* shaderDerivativeExtensionString() const {
53 SkASSERT(this->fShaderDerivativeSupport);
54 return fShaderDerivativeExtensionString;
55 }
56
57 /**
58 * This returns the name of an extension that must be enabled in the shader to support external
59 * textures. In some cases, two extensions must be enabled - the second extension is returned
60 * by secondExternalTextureExtensionString(). If that function returns nullptr, then only one
61 * extension is required.
62 */
63 const char* externalTextureExtensionString() const {
64 SkASSERT(this->fExternalTextureSupport);
65 return fExternalTextureExtensionString;
66 }
67
68 const char* secondExternalTextureExtensionString() const {
69 SkASSERT(this->fExternalTextureSupport);
70 return fSecondExternalTextureExtensionString;
71 }
72
73 /**
74 * SkSL 300 requires support for derivatives, nonsquare matrices and bitwise integer operations.
75 */
76 SkSL::Version supportedSkSLVerion() const {
77 if (fShaderDerivativeSupport && fNonsquareMatrixSupport && fIntegerSupport &&
78 fGLSLGeneration >= SkSL::GLSLGeneration::k330) {
79 return SkSL::Version::k300;
80 }
81 return SkSL::Version::k100;
82 }
83
84 bool supportsDistanceFieldText() const { return fShaderDerivativeSupport; }
85
86 /**
87 * The following group of attributes are only needed for Ganesh/GL. However, we keep there here
88 * as opposed to demoting them to being defined in Ganesh's GrShaderCaps for 2 reasons.
89 *
90 * 1) Some backend-agnostic SkSL utilies reference these capabilities.
91 *
92 * 2) Even though we do not anticipate Dawn relying upon Skia's GLSL generation for GL support,
93 * sometimes SkSL compilation itself can be impacted by some of these capabilities. Such
94 * attributes will be kept here in case Dawn/GL eventually needs to leverage them.
95 */
96 bool fFloatIs32Bits = true; /* Queried by SkSLSetting + impacts SkSL generation */
97 /**
98 * TODO: Modify SkSLGLSLCodeGenerator to use GrShaderCaps rather than this struct since GL
99 * should only be used with Ganesh. Once that is complete, the following attribute could be
100 * downgraded to GrShaderCaps.
101 */
102 SkSL::GLSLGeneration fGLSLGeneration = SkSL::GLSLGeneration::k330;
103 bool fFlatInterpolationSupport = false; /* Supported by all backends except for some GL impls */
104 bool fUsesPrecisionModifiers = false; /* GLSL is the only shading language that uses these */
105
106 bool fDualSourceBlendingSupport = false;
107 bool fShaderDerivativeSupport = false;
108 /** Enables sampleGrad and sampleLod functions that don't rely on implicit derivatives */
109 bool fExplicitTextureLodSupport = false;
110 /** Indicates true 32-bit integer support, with unsigned types and bitwise operations */
111 bool fIntegerSupport = false;
112 bool fNonsquareMatrixSupport = false;
113 /** asinh(), acosh(), atanh() */
114 bool fInverseHyperbolicSupport = false;
115 bool fFBFetchSupport = false;
116 bool fFBFetchNeedsCustomOutput = false;
117 bool fNoPerspectiveInterpolationSupport = false;
118 bool fSampleMaskSupport = false;
119 bool fExternalTextureSupport = false;
120
121 /** isinf() is defined + floating point infinities are handled according to IEEE standards. */
122 bool fInfinitySupport = false;
123
124 /** Used by SkSL to know when to generate polyfills. */
125 bool fBuiltinFMASupport = true;
126 bool fBuiltinDeterminantSupport = true;
127
128 /** Used for specific driver bug work arounds */
129 bool fCanUseVoidInSequenceExpressions = true;
130 bool fCanUseMinAndAbsTogether = true;
131 bool fCanUseFractForNegativeValues = true;
132 bool fMustForceNegatedAtanParamToFloat = false;
133 bool fMustForceNegatedLdexpParamToMultiply = false; // skbug.com/40043167
134 /** Returns whether a device incorrectly implements atan(y,x) as atan(y/x) */
135 bool fAtan2ImplementedAsAtanYOverX = false;
136 /**
137 * If this returns true some operation (could be a no op) must be called between floor and abs
138 * to make sure the driver compiler doesn't inline them together which can cause a driver bug in
139 * the shader.
140 */
141 bool fMustDoOpBetweenFloorAndAbs = false;
142 /**
143 * The D3D shader compiler, when targeting PS 3.0 (ie within ANGLE) fails to compile certain
144 * constructs. See detailed comments in GrGLCaps.cpp.
145 */
146 bool fMustGuardDivisionEvenAfterExplicitZeroCheck = false;
147 /** If false, SkSL uses a workaround so that sk_FragCoord doesn't actually query gl_FragCoord */
148 bool fCanUseFragCoord = true;
149 /** If true, then conditions in for loops need "&& true" to work around driver bugs. */
150 bool fAddAndTrueToLoopCondition = false;
151 /**
152 * If true, then expressions such as "x && y" or "x || y" are rewritten as ternary to work
153 * around driver bugs.
154 */
155 bool fUnfoldShortCircuitAsTernary = false;
156 bool fEmulateAbsIntFunction = false;
157 bool fRewriteDoWhileLoops = false;
158 bool fRewriteSwitchStatements = false;
159 bool fRemovePowWithConstantExponent = false;
160 /**
161 * The Android emulator claims samplerExternalOES is an unknown type if a default precision
162 * statement is made for the type.
163 */
164 bool fNoDefaultPrecisionForExternalSamplers = false;
165 /**
166 * ARM GPUs calculate `matrix * vector` in SPIR-V at full precision, even when the inputs are
167 * RelaxedPrecision. Rewriting the multiply as a sum of vector*scalar fixes this.
168 * (skbug.com/40042841)
169 */
170 bool fRewriteMatrixVectorMultiply = false;
171 /** Rewrites matrix equality comparisons to avoid an Adreno driver bug. (skbug.com/40042682) */
172 bool fRewriteMatrixComparisons = false;
173 /** Strips const from function parameters in the GLSL code generator. (skbug.com/40044949) */
174 bool fRemoveConstFromFunctionParameters = false;
175 /**
176 * On some Android devices colors aren't accurate enough for the double lookup in the
177 * Perlin noise shader. This workaround aggressively snaps colors to multiples of 1/255.
178 */
179 bool fPerlinNoiseRoundingFix = false;
180 /**
181 * Vulkan requires certain builtin variables be present, even if they're unused. At one time,
182 * validation errors would result if sk_Clockwise was missing. Now, it's just (Adreno) driver
183 * bugs that drop or corrupt draws if they're missing.
184 */
185 bool fMustDeclareFragmentFrontFacing = false;
186 /**
187 * SPIR-V currently doesn't handle different array strides being passed in to a fixed sized
188 * array function parameter, so fForceStd430ArrayLayout will make all array strides conform
189 * to std430 stride alignment rules.
190 */
191 bool fForceStd430ArrayLayout = false;
192 /**
193 * Some NVIDIA drivers fail to create a pipeline if RelaxedPrecision is applied to
194 * OpImageSampleImplicitLod when sampling from a YCbCr image. This workaround simply disables
195 * RelaxedPrecision for that op regardless of image kind. (skbug.com/421927604)
196 */
197 bool fCannotUseRelaxedPrecisionOnImageSample = false;
198 /**
199 * Clamp, Min, Max, intrinsics all appear to be broken on Intel UHD630, so shaders for these
200 * devices will break these intrinsics into individual scalar commands.
201 */
202 bool fVectorClampMinMaxSupport = true;
203
204 const char* fVersionDeclString = "";
205
206 const char* fShaderDerivativeExtensionString = nullptr;
207 const char* fExternalTextureExtensionString = nullptr;
208 const char* fSecondExternalTextureExtensionString = nullptr;
209 const char* fFBFetchColorName = nullptr;
210
211 AdvBlendEqInteraction fAdvBlendEqInteraction = kNotSupported_AdvBlendEqInteraction;
212 };
213
214 // Various sets of caps for use in tests
215 class ShaderCapsFactory {
216 public:
217 static const ShaderCaps* Default() {
218 static const SkSL::ShaderCaps* const sCaps = [] {
219 std::unique_ptr<ShaderCaps> caps = MakeShaderCaps();
220 caps->fVersionDeclString = "#version 400";
221 caps->fShaderDerivativeSupport = true;
222 return caps.release();
223 }();
224 return sCaps;
225 }
226
227 static const ShaderCaps* Standalone() {
228 static const SkSL::ShaderCaps* const sCaps = MakeShaderCaps().release();
229 return sCaps;
230 }
231
232 protected:
233 static std::unique_ptr<ShaderCaps> MakeShaderCaps();
234 };
235
236 bool type_to_sksltype(const Context& context, const Type& type, SkSLType* outType);
237
238 void write_stringstream(const StringStream& d, OutputStream& out);
239
240 } // namespace SkSL
241
242 #endif // SKSL_UTIL