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 "assert.h" 16 #include "SkSLString.h" 17 #include "SkSLStringStream.h" 18 19 #ifndef SKSL_STANDALONE 20 #include "GrContextOptions.h" 21 #include "GrShaderCaps.h" 22 #endif 23 24 #ifdef SKSL_STANDALONE 25 #if defined(_WIN32) || defined(__SYMBIAN32__) 26 #define SKSL_BUILD_FOR_WIN 27 #endif 28 #else 29 #ifdef SK_BUILD_FOR_WIN 30 #define SKSL_BUILD_FOR_WIN 31 #endif // SK_BUILD_FOR_WIN 32 #endif // SKSL_STANDALONE 33 34 namespace SkSL { 35 36 #ifdef SKSL_STANDALONE 37 38 // we're being compiled standalone, so we don't have access to caps... 39 enum GrGLSLGeneration { 40 k110_GrGLSLGeneration, 41 k130_GrGLSLGeneration, 42 k140_GrGLSLGeneration, 43 k150_GrGLSLGeneration, 44 k330_GrGLSLGeneration, 45 k400_GrGLSLGeneration, 46 k420_GrGLSLGeneration, 47 k310es_GrGLSLGeneration, 48 k320es_GrGLSLGeneration, 49 }; 50 51 #define SKSL_CAPS_CLASS StandaloneShaderCaps 52 class StandaloneShaderCaps { 53 public: generation()54 GrGLSLGeneration generation() const { 55 return k400_GrGLSLGeneration; 56 } 57 canUseMinAndAbsTogether()58 bool canUseMinAndAbsTogether() const { 59 return true; 60 } 61 mustForceNegatedAtanParamToFloat()62 bool mustForceNegatedAtanParamToFloat() const { 63 return false; 64 } 65 shaderDerivativeSupport()66 bool shaderDerivativeSupport() const { 67 return true; 68 } 69 usesPrecisionModifiers()70 bool usesPrecisionModifiers() const { 71 return true; 72 } 73 mustDeclareFragmentShaderOutput()74 bool mustDeclareFragmentShaderOutput() const { 75 return true; 76 } 77 fbFetchSupport()78 bool fbFetchSupport() const { 79 return true; 80 } 81 fbFetchNeedsCustomOutput()82 bool fbFetchNeedsCustomOutput() const { 83 return false; 84 } 85 bindlessTextureSupport()86 bool bindlessTextureSupport() const { 87 return false; 88 } 89 dropsTileOnZeroDivide()90 bool dropsTileOnZeroDivide() const { 91 return false; 92 } 93 flatInterpolationSupport()94 bool flatInterpolationSupport() const { 95 return true; 96 } 97 noperspectiveInterpolationSupport()98 bool noperspectiveInterpolationSupport() const { 99 return true; 100 } 101 multisampleInterpolationSupport()102 bool multisampleInterpolationSupport() const { 103 return true; 104 } 105 sampleVariablesSupport()106 bool sampleVariablesSupport() const { 107 return true; 108 } 109 sampleMaskOverrideCoverageSupport()110 bool sampleMaskOverrideCoverageSupport() const { 111 return true; 112 } 113 externalTextureSupport()114 bool externalTextureSupport() const { 115 return true; 116 } 117 texelFetchSupport()118 bool texelFetchSupport() const { 119 return true; 120 } 121 imageLoadStoreSupport()122 bool imageLoadStoreSupport() const { 123 return true; 124 } 125 mustEnableAdvBlendEqs()126 bool mustEnableAdvBlendEqs() const { 127 return false; 128 } 129 mustEnableSpecificAdvBlendEqs()130 bool mustEnableSpecificAdvBlendEqs() const { 131 return false; 132 } 133 canUseAnyFunctionInShader()134 bool canUseAnyFunctionInShader() const { 135 return false; 136 } 137 floatPrecisionVaries()138 bool floatPrecisionVaries() const { 139 return false; 140 } 141 integerSupport()142 bool integerSupport() const { 143 return false; 144 } 145 shaderDerivativeExtensionString()146 const char* shaderDerivativeExtensionString() const { 147 return nullptr; 148 } 149 fragCoordConventionsExtensionString()150 const char* fragCoordConventionsExtensionString() const { 151 return nullptr; 152 } 153 imageLoadStoreExtensionString()154 const char* imageLoadStoreExtensionString() const { 155 return nullptr; 156 } 157 versionDeclString()158 const char* versionDeclString() const { 159 return ""; 160 } 161 mustImplementGSInvocationsWithLoop()162 bool mustImplementGSInvocationsWithLoop() const { 163 return false; 164 } 165 }; 166 167 extern StandaloneShaderCaps standaloneCaps; 168 169 #else 170 171 #define SKSL_CAPS_CLASS GrShaderCaps 172 // Various sets of caps for use in tests 173 class ShaderCapsFactory { 174 public: 175 static sk_sp<GrShaderCaps> Default() { 176 sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); 177 result->fVersionDeclString = "#version 400"; 178 result->fShaderDerivativeSupport = true; 179 return result; 180 } 181 182 static sk_sp<GrShaderCaps> Version450Core() { 183 sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); 184 result->fVersionDeclString = "#version 450 core"; 185 return result; 186 } 187 188 static sk_sp<GrShaderCaps> Version110() { 189 sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); 190 result->fVersionDeclString = "#version 110"; 191 result->fGLSLGeneration = GrGLSLGeneration::k110_GrGLSLGeneration; 192 return result; 193 } 194 195 static sk_sp<GrShaderCaps> UsesPrecisionModifiers() { 196 sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); 197 result->fVersionDeclString = "#version 400"; 198 result->fUsesPrecisionModifiers = true; 199 return result; 200 } 201 202 static sk_sp<GrShaderCaps> CannotUseMinAndAbsTogether() { 203 sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); 204 result->fVersionDeclString = "#version 400"; 205 result->fCanUseMinAndAbsTogether = false; 206 return result; 207 } 208 209 static sk_sp<GrShaderCaps> MustForceNegatedAtanParamToFloat() { 210 sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); 211 result->fVersionDeclString = "#version 400"; 212 result->fMustForceNegatedAtanParamToFloat = true; 213 return result; 214 } 215 216 static sk_sp<GrShaderCaps> ShaderDerivativeExtensionString() { 217 sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); 218 result->fVersionDeclString = "#version 400"; 219 result->fShaderDerivativeSupport = true; 220 result->fShaderDerivativeExtensionString = "GL_OES_standard_derivatives"; 221 result->fUsesPrecisionModifiers = true; 222 return result; 223 } 224 225 static sk_sp<GrShaderCaps> FragCoordsOld() { 226 sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); 227 result->fVersionDeclString = "#version 110"; 228 result->fGLSLGeneration = GrGLSLGeneration::k110_GrGLSLGeneration; 229 result->fFragCoordConventionsExtensionString = "GL_ARB_fragment_coord_conventions"; 230 return result; 231 } 232 233 static sk_sp<GrShaderCaps> FragCoordsNew() { 234 sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); 235 result->fVersionDeclString = "#version 400"; 236 result->fFragCoordConventionsExtensionString = "GL_ARB_fragment_coord_conventions"; 237 return result; 238 } 239 240 static sk_sp<GrShaderCaps> MustImplementGSInvocationsWithLoop() { 241 sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); 242 result->fVersionDeclString = "#version 400"; 243 result->fMustImplementGSInvocationsWithLoop = true; 244 return result; 245 } 246 247 static sk_sp<GrShaderCaps> VariousCaps() { 248 sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); 249 result->fVersionDeclString = "#version 400"; 250 result->fExternalTextureSupport = true; 251 result->fFBFetchSupport = false; 252 result->fDropsTileOnZeroDivide = true; 253 result->fTexelFetchSupport = true; 254 result->fCanUseAnyFunctionInShader = false; 255 return result; 256 } 257 }; 258 #endif 259 260 void write_stringstream(const StringStream& d, OutputStream& out); 261 262 #if _MSC_VER 263 #define NORETURN __declspec(noreturn) 264 #else 265 #define NORETURN __attribute__((__noreturn__)) 266 #endif 267 268 NORETURN void sksl_abort(); 269 270 } // namespace 271 272 #ifdef SKSL_STANDALONE 273 #define ASSERT(x) (void)((x) || (ABORT("failed assert(%s): %s:%d\n", #x, __FILE__, __LINE__), 0)) 274 #define ASSERT_RESULT(x) ASSERT(x) 275 #define SKSL_DEBUGCODE(x) x 276 #else 277 #define ASSERT SkASSERT 278 #define ASSERT_RESULT(x) SkAssertResult(x) 279 #define SKSL_DEBUGCODE(x) SkDEBUGCODE(x) 280 #endif 281 282 #define SKSL_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 283 284 #if defined(__clang__) || defined(__GNUC__) 285 #define SKSL_PRINTF_LIKE(A, B) __attribute__((format(printf, (A), (B)))) 286 #else 287 #define SKSL_PRINTF_LIKE(A, B) 288 #endif 289 290 #define ABORT(...) (printf(__VA_ARGS__), sksl_abort()) 291 292 #endif 293