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 <cstdarg> 12 #include <memory> 13 #include "stdlib.h" 14 #include "string.h" 15 #include "src/sksl/SkSLDefines.h" 16 #include "src/sksl/SkSLLexer.h" 17 18 #ifndef SKSL_STANDALONE 19 #include "include/core/SkTypes.h" 20 #if SK_SUPPORT_GPU 21 #include "include/core/SkRefCnt.h" 22 #include "src/gpu/GrShaderCaps.h" 23 #endif // SK_SUPPORT_GPU 24 #endif // SKSL_STANDALONE 25 26 namespace SkSL { 27 28 class OutputStream; 29 class StringStream; 30 31 #if defined(SKSL_STANDALONE) || !SK_SUPPORT_GPU 32 33 // we're being compiled standalone, so we don't have access to caps... 34 enum GrGLSLGeneration { 35 k110_GrGLSLGeneration, 36 k130_GrGLSLGeneration, 37 k140_GrGLSLGeneration, 38 k150_GrGLSLGeneration, 39 k330_GrGLSLGeneration, 40 k400_GrGLSLGeneration, 41 k420_GrGLSLGeneration, 42 k310es_GrGLSLGeneration, 43 k320es_GrGLSLGeneration, 44 }; 45 46 #define SKSL_CAPS_CLASS StandaloneShaderCaps 47 class StandaloneShaderCaps { 48 public: generation()49 GrGLSLGeneration generation() const { 50 return k400_GrGLSLGeneration; 51 } 52 atan2ImplementedAsAtanYOverX()53 bool atan2ImplementedAsAtanYOverX() const { 54 return false; 55 } 56 canUseMinAndAbsTogether()57 bool canUseMinAndAbsTogether() const { 58 return true; 59 } 60 mustForceNegatedAtanParamToFloat()61 bool mustForceNegatedAtanParamToFloat() const { 62 return false; 63 } 64 shaderDerivativeSupport()65 bool shaderDerivativeSupport() const { 66 return true; 67 } 68 usesPrecisionModifiers()69 bool usesPrecisionModifiers() const { 70 return true; 71 } 72 mustDeclareFragmentShaderOutput()73 bool mustDeclareFragmentShaderOutput() const { 74 return true; 75 } 76 fbFetchSupport()77 bool fbFetchSupport() const { 78 return true; 79 } 80 fbFetchNeedsCustomOutput()81 bool fbFetchNeedsCustomOutput() const { 82 return false; 83 } 84 flatInterpolationSupport()85 bool flatInterpolationSupport() const { 86 return true; 87 } 88 noperspectiveInterpolationSupport()89 bool noperspectiveInterpolationSupport() const { 90 return true; 91 } 92 multisampleInterpolationSupport()93 bool multisampleInterpolationSupport() const { 94 return true; 95 } 96 sampleMaskSupport()97 bool sampleMaskSupport() const { 98 return true; 99 } 100 externalTextureSupport()101 bool externalTextureSupport() const { 102 return true; 103 } 104 mustDoOpBetweenFloorAndAbs()105 bool mustDoOpBetweenFloorAndAbs() const { 106 return false; 107 } 108 mustGuardDivisionEvenAfterExplicitZeroCheck()109 bool mustGuardDivisionEvenAfterExplicitZeroCheck() const { 110 return false; 111 } 112 inBlendModesFailRandomlyForAllZeroVec()113 bool inBlendModesFailRandomlyForAllZeroVec() const { 114 return false; 115 } 116 mustEnableAdvBlendEqs()117 bool mustEnableAdvBlendEqs() const { 118 return false; 119 } 120 mustEnableSpecificAdvBlendEqs()121 bool mustEnableSpecificAdvBlendEqs() const { 122 return false; 123 } 124 canUseAnyFunctionInShader()125 bool canUseAnyFunctionInShader() const { 126 return false; 127 } 128 noDefaultPrecisionForExternalSamplers()129 bool noDefaultPrecisionForExternalSamplers() const { 130 return false; 131 } 132 floatIs32Bits()133 bool floatIs32Bits() const { 134 return true; 135 } 136 integerSupport()137 bool integerSupport() const { 138 return false; 139 } 140 builtinFMASupport()141 bool builtinFMASupport() const { 142 return true; 143 } 144 shaderDerivativeExtensionString()145 const char* shaderDerivativeExtensionString() const { 146 return nullptr; 147 } 148 fragCoordConventionsExtensionString()149 const char* fragCoordConventionsExtensionString() const { 150 return nullptr; 151 } 152 geometryShaderExtensionString()153 const char* geometryShaderExtensionString() const { 154 return nullptr; 155 } 156 gsInvocationsExtensionString()157 const char* gsInvocationsExtensionString() const { 158 return nullptr; 159 } 160 externalTextureExtensionString()161 const char* externalTextureExtensionString() const { 162 return nullptr; 163 } 164 secondExternalTextureExtensionString()165 const char* secondExternalTextureExtensionString() const { 166 return nullptr; 167 } 168 versionDeclString()169 const char* versionDeclString() const { 170 return ""; 171 } 172 gsInvocationsSupport()173 bool gsInvocationsSupport() const { 174 return true; 175 } 176 canUseFractForNegativeValues()177 bool canUseFractForNegativeValues() const { 178 return true; 179 } 180 canUseFragCoord()181 bool canUseFragCoord() const { 182 return true; 183 } 184 incompleteShortIntPrecision()185 bool incompleteShortIntPrecision() const { 186 return false; 187 } 188 addAndTrueToLoopCondition()189 bool addAndTrueToLoopCondition() const { 190 return false; 191 } 192 unfoldShortCircuitAsTernary()193 bool unfoldShortCircuitAsTernary() const { 194 return false; 195 } 196 emulateAbsIntFunction()197 bool emulateAbsIntFunction() const { 198 return false; 199 } 200 rewriteDoWhileLoops()201 bool rewriteDoWhileLoops() const { 202 return false; 203 } 204 removePowWithConstantExponent()205 bool removePowWithConstantExponent() const { 206 return false; 207 } 208 fbFetchColorName()209 const char* fbFetchColorName() const { 210 return nullptr; 211 } 212 }; 213 214 extern StandaloneShaderCaps standaloneCaps; 215 216 #else 217 218 #define SKSL_CAPS_CLASS GrShaderCaps 219 // Various sets of caps for use in tests 220 class ShaderCapsFactory { 221 public: 222 static sk_sp<GrShaderCaps> Default(); 223 224 static sk_sp<GrShaderCaps> Version450Core(); 225 226 static sk_sp<GrShaderCaps> Version110(); 227 228 static sk_sp<GrShaderCaps> UsesPrecisionModifiers(); 229 230 static sk_sp<GrShaderCaps> CannotUseMinAndAbsTogether(); 231 232 static sk_sp<GrShaderCaps> CannotUseFractForNegativeValues(); 233 234 static sk_sp<GrShaderCaps> MustForceNegatedAtanParamToFloat(); 235 236 static sk_sp<GrShaderCaps> ShaderDerivativeExtensionString(); 237 238 static sk_sp<GrShaderCaps> FragCoordsOld(); 239 240 static sk_sp<GrShaderCaps> FragCoordsNew(); 241 242 static sk_sp<GrShaderCaps> GeometryShaderSupport(); 243 244 static sk_sp<GrShaderCaps> NoGSInvocationsSupport(); 245 246 static sk_sp<GrShaderCaps> GeometryShaderExtensionString(); 247 248 static sk_sp<GrShaderCaps> GSInvocationsExtensionString(); 249 250 static sk_sp<GrShaderCaps> VariousCaps(); 251 252 static sk_sp<GrShaderCaps> CannotUseFragCoord(); 253 254 static sk_sp<GrShaderCaps> IncompleteShortIntPrecision(); 255 256 static sk_sp<GrShaderCaps> AddAndTrueToLoopCondition(); 257 258 static sk_sp<GrShaderCaps> UnfoldShortCircuitAsTernary(); 259 260 static sk_sp<GrShaderCaps> EmulateAbsIntFunction(); 261 262 static sk_sp<GrShaderCaps> RewriteDoWhileLoops(); 263 264 static sk_sp<GrShaderCaps> RemovePowWithConstantExponent(); 265 266 static sk_sp<GrShaderCaps> SampleMaskSupport(); 267 }; 268 #endif 269 270 void write_stringstream(const StringStream& d, OutputStream& out); 271 272 // Returns true if op is '=' or any compound assignment operator ('+=', '-=', etc.) 273 bool is_assignment(Token::Kind op); 274 275 // Given a compound assignment operator, returns the non-assignment version of the operator (e.g. 276 // '+=' becomes '+') 277 Token::Kind remove_assignment(Token::Kind op); 278 279 NORETURN void sksl_abort(); 280 281 } // namespace 282 283 #endif 284