ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 11 | #include "stdlib.h" |
| 12 | #include "assert.h" |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 13 | #include "SkOpts.h" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 14 | #include "SkRefCnt.h" |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 15 | #include "SkStream.h" |
| 16 | #include "SkString.h" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 17 | #include "SkTypes.h" |
| 18 | #include "glsl/GrGLSLCaps.h" |
| 19 | #include "GrContextOptions.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 20 | |
| 21 | namespace SkSL { |
| 22 | |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 23 | // Various sets of caps for use in tests |
| 24 | class GLSLCapsFactory { |
| 25 | public: |
| 26 | static sk_sp<GrGLSLCaps> Default() { |
| 27 | sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); |
| 28 | result->fVersionDeclString = "#version 400"; |
| 29 | result->fShaderDerivativeSupport = true; |
| 30 | return result; |
| 31 | } |
| 32 | |
| 33 | static sk_sp<GrGLSLCaps> Version450Core() { |
| 34 | sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); |
| 35 | result->fVersionDeclString = "#version 450 core"; |
| 36 | return result; |
| 37 | } |
| 38 | |
| 39 | static sk_sp<GrGLSLCaps> Version110() { |
| 40 | sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); |
| 41 | result->fVersionDeclString = "#version 110"; |
| 42 | result->fGLSLGeneration = GrGLSLGeneration::k110_GrGLSLGeneration; |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | static sk_sp<GrGLSLCaps> UsesPrecisionModifiers() { |
| 47 | sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); |
| 48 | result->fVersionDeclString = "#version 400"; |
| 49 | result->fUsesPrecisionModifiers = true; |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | static sk_sp<GrGLSLCaps> CannotUseMinAndAbsTogether() { |
| 54 | sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); |
| 55 | result->fVersionDeclString = "#version 400"; |
| 56 | result->fCanUseMinAndAbsTogether = false; |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | static sk_sp<GrGLSLCaps> MustForceNegatedAtanParamToFloat() { |
| 61 | sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); |
| 62 | result->fVersionDeclString = "#version 400"; |
| 63 | result->fMustForceNegatedAtanParamToFloat = true; |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | static sk_sp<GrGLSLCaps> ShaderDerivativeExtensionString() { |
| 68 | sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); |
| 69 | result->fVersionDeclString = "#version 400"; |
| 70 | result->fShaderDerivativeSupport = true; |
| 71 | result->fShaderDerivativeExtensionString = "GL_OES_standard_derivatives"; |
| 72 | return result; |
| 73 | } |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 74 | |
| 75 | static sk_sp<GrGLSLCaps> VariousCaps() { |
| 76 | sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); |
| 77 | result->fVersionDeclString = "#version 400"; |
| 78 | result->fExternalTextureSupport = true; |
| 79 | result->fFBFetchSupport = false; |
| 80 | result->fDropsTileOnZeroDivide = true; |
| 81 | result->fTexelFetchSupport = true; |
| 82 | result->fCanUseAnyFunctionInShader = false; |
| 83 | return result; |
| 84 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 85 | }; |
| 86 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 87 | void write_data(const SkData& d, SkWStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 88 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 89 | SkString operator+(const SkString& s, const char* c); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 90 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 91 | SkString operator+(const char* c, const SkString& s); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 92 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 93 | SkString operator+(const SkString& s1, const SkString& s2); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 94 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 95 | bool operator==(const SkString& s1, const char* s2); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 96 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 97 | bool operator!=(const SkString& s1, const char* s2); |
| 98 | |
| 99 | bool operator!=(const char* s1, const SkString& s2); |
| 100 | |
| 101 | SkString to_string(double value); |
| 102 | |
| 103 | SkString to_string(int32_t value); |
| 104 | |
| 105 | SkString to_string(uint32_t value); |
| 106 | |
| 107 | SkString to_string(int64_t value); |
| 108 | |
| 109 | SkString to_string(uint64_t value); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 110 | |
| 111 | #if _MSC_VER |
| 112 | #define NORETURN __declspec(noreturn) |
| 113 | #else |
| 114 | #define NORETURN __attribute__((__noreturn__)) |
| 115 | #endif |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 116 | int stoi(SkString s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 117 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 118 | double stod(SkString s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 119 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 120 | long stol(SkString s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 121 | |
| 122 | NORETURN void sksl_abort(); |
| 123 | |
| 124 | } // namespace |
| 125 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 126 | #define ASSERT(x) SkASSERT(x) |
| 127 | #define ASSERT_RESULT(x) SkAssertResult(x); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 128 | |
| 129 | #ifdef SKIA |
| 130 | #define ABORT(...) { SkDebugf(__VA_ARGS__); sksl_abort(); } |
| 131 | #else |
| 132 | #define ABORT(...) { sksl_abort(); } |
| 133 | #endif |
| 134 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 135 | namespace std { |
| 136 | template<> struct hash<SkString> { |
| 137 | size_t operator()(const SkString& s) const { |
| 138 | return SkOpts::hash_fn(s.c_str(), s.size(), 0); |
| 139 | } |
| 140 | }; |
| 141 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 142 | #endif |