tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 GrGLSL_DEFINED |
| 9 | #define GrGLSL_DEFINED |
| 10 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 11 | #include "GrTypesPriv.h" |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 13 | |
egdaniel | cb7ba1e | 2015-10-26 08:38:25 -0700 | [diff] [blame] | 14 | class GrGLSLCaps; |
| 15 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 16 | // Limited set of GLSL versions we build shaders for. Caller should round |
| 17 | // down the GLSL version to one of these enums. |
| 18 | enum GrGLSLGeneration { |
| 19 | /** |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 20 | * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20) |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 21 | */ |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 22 | k110_GrGLSLGeneration, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 23 | /** |
| 24 | * Desktop GLSL 1.30 |
| 25 | */ |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 26 | k130_GrGLSLGeneration, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 27 | /** |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 28 | * Desktop GLSL 1.40 |
| 29 | */ |
| 30 | k140_GrGLSLGeneration, |
| 31 | /** |
| 32 | * Desktop GLSL 1.50 |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 33 | */ |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 34 | k150_GrGLSLGeneration, |
rmistry | 63a9f84 | 2014-10-17 06:07:08 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Desktop GLSL 3.30, and ES GLSL 3.00 |
| 37 | */ |
| 38 | k330_GrGLSLGeneration, |
| 39 | /** |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 40 | * Desktop GLSL 4.00 |
| 41 | */ |
| 42 | k400_GrGLSLGeneration, |
| 43 | /** |
rmistry | 63a9f84 | 2014-10-17 06:07:08 -0700 | [diff] [blame] | 44 | * ES GLSL 3.10 only TODO Make GLSLCap objects to make this more granular |
| 45 | */ |
| 46 | k310es_GrGLSLGeneration, |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 47 | /** |
| 48 | * ES GLSL 3.20 |
| 49 | */ |
| 50 | k320es_GrGLSLGeneration, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
bsalomon | bc50e5c | 2015-06-04 08:49:34 -0700 | [diff] [blame] | 53 | bool GrGLSLSupportsNamedFragmentShaderOutputs(GrGLSLGeneration); |
| 54 | |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 55 | /** |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 56 | * Gets the name of the function that should be used to sample a 2D texture. Coord type is used |
| 57 | * to indicate whether the texture is sampled using projective textured (kVec3f) or not (kVec2f). |
| 58 | */ |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 59 | inline const char* GrGLSLTexture2DFunctionName(GrSLType coordType, GrSLType samplerType, |
| 60 | GrGLSLGeneration glslGen) { |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame^] | 61 | SkASSERT(GrSLTypeIs2DCombinedSamplerType(samplerType)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 62 | SkASSERT(kVec2f_GrSLType == coordType || kVec3f_GrSLType == coordType); |
| 63 | // GL_TEXTURE_RECTANGLE_ARB is written against OpenGL 2.0/GLSL 1.10. At that time there were |
| 64 | // separate texture*() functions. In OpenGL 3.0/GLSL 1.30 the different texture*() functions |
| 65 | // were deprecated in favor or the unified texture() function. RECTANGLE textures became |
| 66 | // standard in OpenGL 3.2/GLSL 1.50 and use texture(). It isn't completely clear what function |
| 67 | // should be used for RECTANGLE textures in GLSL versions >= 1.30 && < 1.50. We're going with |
| 68 | // using texture(). |
| 69 | if (glslGen >= k130_GrGLSLGeneration) { |
| 70 | return (kVec2f_GrSLType == coordType) ? "texture" : "textureProj"; |
| 71 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 72 | if (kVec2f_GrSLType == coordType) { |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame^] | 73 | return (samplerType == kTexture2DRectSampler_GrSLType) ? "texture2DRect" : "texture2D"; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 74 | } else { |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame^] | 75 | return (samplerType == kTexture2DRectSampler_GrSLType) ? "texture2DRectProj" |
| 76 | : "texture2DProj"; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
egdaniel | cb7ba1e | 2015-10-26 08:38:25 -0700 | [diff] [blame] | 81 | * Adds a line of GLSL code to declare the default precision for float types. |
| 82 | */ |
| 83 | void GrGLSLAppendDefaultFloatPrecisionDeclaration(GrSLPrecision, |
| 84 | const GrGLSLCaps& glslCaps, |
| 85 | SkString* out); |
| 86 | |
| 87 | /** |
cdalton | 1f50acf | 2016-04-11 11:30:50 -0700 | [diff] [blame] | 88 | * Converts a GrSLPrecision to its corresponding GLSL precision qualifier. |
| 89 | */ |
| 90 | static inline const char* GrGLSLPrecisionString(GrSLPrecision p) { |
| 91 | switch (p) { |
| 92 | case kLow_GrSLPrecision: |
| 93 | return "lowp"; |
| 94 | case kMedium_GrSLPrecision: |
| 95 | return "mediump"; |
| 96 | case kHigh_GrSLPrecision: |
| 97 | return "highp"; |
| 98 | default: |
| 99 | SkFAIL("Unexpected precision type."); |
| 100 | return ""; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 105 | * Converts a GrSLType to a string containing the name of the equivalent GLSL type. |
| 106 | */ |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 107 | static inline const char* GrGLSLTypeString(GrSLType t) { |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 108 | switch (t) { |
| 109 | case kVoid_GrSLType: |
| 110 | return "void"; |
| 111 | case kFloat_GrSLType: |
| 112 | return "float"; |
| 113 | case kVec2f_GrSLType: |
| 114 | return "vec2"; |
| 115 | case kVec3f_GrSLType: |
| 116 | return "vec3"; |
| 117 | case kVec4f_GrSLType: |
| 118 | return "vec4"; |
cdalton | 8d988b3 | 2016-03-07 15:39:09 -0800 | [diff] [blame] | 119 | case kMat22f_GrSLType: |
| 120 | return "mat2"; |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 121 | case kMat33f_GrSLType: |
| 122 | return "mat3"; |
| 123 | case kMat44f_GrSLType: |
| 124 | return "mat4"; |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame^] | 125 | case kTexture2DSampler_GrSLType: |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 126 | return "sampler2D"; |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame^] | 127 | case kTextureExternalSampler_GrSLType: |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 128 | return "samplerExternalOES"; |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame^] | 129 | case kTexture2DRectSampler_GrSLType: |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 130 | return "sampler2DRect"; |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame^] | 131 | case kTextureBufferSampler_GrSLType: |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 132 | return "samplerBuffer"; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 133 | case kBool_GrSLType: |
| 134 | return "bool"; |
| 135 | case kInt_GrSLType: |
| 136 | return "int"; |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 137 | case kUint_GrSLType: |
| 138 | return "uint"; |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame^] | 139 | case kTexture2D_GrSLType: |
| 140 | return "texture2D"; |
| 141 | case kSampler_GrSLType: |
| 142 | return "sampler"; |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 143 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 144 | SkFAIL("Unknown shader var type."); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 145 | return ""; // suppress warning |
| 146 | } |
| 147 | } |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 148 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 149 | /** A generic base-class representing a GLSL expression. |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 150 | * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant |
| 151 | * folding with help of 1 and 0. |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 152 | * |
| 153 | * Clients should not use this class, rather the specific instantiations defined |
| 154 | * later, for example GrGLSLExpr4. |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 155 | */ |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 156 | template <typename Self> |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 157 | class GrGLSLExpr { |
| 158 | public: |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 159 | bool isOnes() const { return kOnes_ExprType == fType; } |
| 160 | bool isZeros() const { return kZeros_ExprType == fType; } |
| 161 | |
| 162 | const char* c_str() const { |
| 163 | if (kZeros_ExprType == fType) { |
| 164 | return Self::ZerosStr(); |
| 165 | } else if (kOnes_ExprType == fType) { |
| 166 | return Self::OnesStr(); |
| 167 | } |
| 168 | SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used. |
| 169 | return fExpr.c_str(); |
| 170 | } |
| 171 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 172 | bool isValid() const { |
| 173 | return kFullExpr_ExprType != fType || !fExpr.isEmpty(); |
| 174 | } |
| 175 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 176 | protected: |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 177 | /** Constructs an invalid expression. |
| 178 | * Useful only as a return value from functions that never actually return |
| 179 | * this and instances that will be assigned to later. */ |
| 180 | GrGLSLExpr() |
| 181 | : fType(kFullExpr_ExprType) { |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 182 | // The only constructor that is allowed to build an empty expression. |
| 183 | SkASSERT(!this->isValid()); |
| 184 | } |
| 185 | |
| 186 | /** Constructs an expression with all components as value v */ |
| 187 | explicit GrGLSLExpr(int v) { |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 188 | if (v == 0) { |
| 189 | fType = kZeros_ExprType; |
| 190 | } else if (v == 1) { |
| 191 | fType = kOnes_ExprType; |
| 192 | } else { |
| 193 | fType = kFullExpr_ExprType; |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 194 | fExpr.appendf(Self::CastIntStr(), v); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | /** Constructs an expression from a string. |
| 199 | * Argument expr is a simple expression or a parenthesized expression. */ |
| 200 | // TODO: make explicit once effects input Exprs. |
| 201 | GrGLSLExpr(const char expr[]) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 202 | if (nullptr == expr) { // TODO: remove this once effects input Exprs. |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 203 | fType = kOnes_ExprType; |
| 204 | } else { |
| 205 | fType = kFullExpr_ExprType; |
| 206 | fExpr = expr; |
| 207 | } |
| 208 | SkASSERT(this->isValid()); |
| 209 | } |
| 210 | |
| 211 | /** Constructs an expression from a string. |
| 212 | * Argument expr is a simple expression or a parenthesized expression. */ |
| 213 | // TODO: make explicit once effects input Exprs. |
| 214 | GrGLSLExpr(const SkString& expr) { |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 215 | if (expr.isEmpty()) { // TODO: remove this once effects input Exprs. |
| 216 | fType = kOnes_ExprType; |
| 217 | } else { |
| 218 | fType = kFullExpr_ExprType; |
| 219 | fExpr = expr; |
| 220 | } |
| 221 | SkASSERT(this->isValid()); |
| 222 | } |
| 223 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 224 | /** Constructs an expression from a string with one substitution. */ |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 225 | GrGLSLExpr(const char format[], const char in0[]) |
| 226 | : fType(kFullExpr_ExprType) { |
| 227 | fExpr.appendf(format, in0); |
| 228 | } |
| 229 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 230 | /** Constructs an expression from a string with two substitutions. */ |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 231 | GrGLSLExpr(const char format[], const char in0[], const char in1[]) |
| 232 | : fType(kFullExpr_ExprType) { |
| 233 | fExpr.appendf(format, in0, in1); |
| 234 | } |
| 235 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 236 | /** Returns expression casted to another type. |
| 237 | * Generic implementation that is called for non-trivial cases of casts. */ |
| 238 | template <typename T> |
| 239 | static Self VectorCastImpl(const T& other); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 240 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 241 | /** Returns a GLSL multiplication: component-wise or component-by-scalar. |
| 242 | * The multiplication will be component-wise or multiply each component by a scalar. |
| 243 | * |
| 244 | * The returned expression will compute the value of: |
| 245 | * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise) |
| 246 | * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar) |
| 247 | * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector) |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 248 | */ |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 249 | template <typename T0, typename T1> |
| 250 | static Self Mul(T0 in0, T1 in1); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 251 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 252 | /** Returns a GLSL addition: component-wise or add a scalar to each component. |
| 253 | * Return value computes: |
| 254 | * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...). |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 255 | */ |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 256 | template <typename T0, typename T1> |
| 257 | static Self Add(T0 in0, T1 in1); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 258 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 259 | /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar. |
| 260 | * Return value computes |
| 261 | * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...). |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 262 | */ |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 263 | template <typename T0, typename T1> |
| 264 | static Self Sub(T0 in0, T1 in1); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 265 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 266 | /** Returns expression that accesses component(s) of the expression. |
| 267 | * format should be the form "%s.x" where 'x' is the component(s) to access. |
| 268 | * Caller is responsible for making sure the amount of components in the |
| 269 | * format string is equal to dim(T). |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 270 | */ |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 271 | template <typename T> |
| 272 | T extractComponents(const char format[]) const; |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 273 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 274 | private: |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 275 | enum ExprType { |
| 276 | kZeros_ExprType, |
| 277 | kOnes_ExprType, |
| 278 | kFullExpr_ExprType, |
| 279 | }; |
| 280 | ExprType fType; |
| 281 | SkString fExpr; |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 284 | class GrGLSLExpr1; |
| 285 | class GrGLSLExpr4; |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 286 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 287 | /** Class representing a float GLSL expression. */ |
| 288 | class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> { |
| 289 | public: |
| 290 | GrGLSLExpr1() |
| 291 | : INHERITED() { |
| 292 | } |
| 293 | explicit GrGLSLExpr1(int v) |
| 294 | : INHERITED(v) { |
| 295 | } |
| 296 | GrGLSLExpr1(const char* expr) |
| 297 | : INHERITED(expr) { |
| 298 | } |
| 299 | GrGLSLExpr1(const SkString& expr) |
| 300 | : INHERITED(expr) { |
| 301 | } |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 302 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 303 | static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 304 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 305 | private: |
| 306 | GrGLSLExpr1(const char format[], const char in0[]) |
| 307 | : INHERITED(format, in0) { |
| 308 | } |
| 309 | GrGLSLExpr1(const char format[], const char in0[], const char in1[]) |
| 310 | : INHERITED(format, in0, in1) { |
| 311 | } |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 312 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 313 | static const char* ZerosStr(); |
| 314 | static const char* OnesStr(); |
| 315 | static const char* CastStr(); |
| 316 | static const char* CastIntStr(); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 317 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 318 | friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); |
| 319 | friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); |
| 320 | friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 321 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 322 | friend class GrGLSLExpr<GrGLSLExpr1>; |
| 323 | friend class GrGLSLExpr<GrGLSLExpr4>; |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 324 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 325 | typedef GrGLSLExpr<GrGLSLExpr1> INHERITED; |
| 326 | }; |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 327 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 328 | /** Class representing a float vector (vec4) GLSL expression. */ |
| 329 | class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> { |
| 330 | public: |
| 331 | GrGLSLExpr4() |
| 332 | : INHERITED() { |
| 333 | } |
| 334 | explicit GrGLSLExpr4(int v) |
| 335 | : INHERITED(v) { |
| 336 | } |
| 337 | GrGLSLExpr4(const char* expr) |
| 338 | : INHERITED(expr) { |
| 339 | } |
| 340 | GrGLSLExpr4(const SkString& expr) |
| 341 | : INHERITED(expr) { |
| 342 | } |
| 343 | |
| 344 | typedef GrGLSLExpr1 AExpr; |
| 345 | AExpr a() const; |
| 346 | |
| 347 | /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */ |
| 348 | static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr); |
| 349 | static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr); |
| 350 | |
| 351 | private: |
| 352 | GrGLSLExpr4(const char format[], const char in0[]) |
| 353 | : INHERITED(format, in0) { |
| 354 | } |
| 355 | GrGLSLExpr4(const char format[], const char in0[], const char in1[]) |
| 356 | : INHERITED(format, in0, in1) { |
| 357 | } |
| 358 | |
| 359 | static const char* ZerosStr(); |
| 360 | static const char* OnesStr(); |
| 361 | static const char* CastStr(); |
| 362 | static const char* CastIntStr(); |
| 363 | |
| 364 | // The vector-by-scalar and scalar-by-vector binary operations. |
| 365 | friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); |
| 366 | friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); |
| 367 | friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); |
| 368 | friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); |
| 369 | friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); |
| 370 | friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); |
| 371 | |
| 372 | // The vector-by-vector, i.e. component-wise, binary operations. |
| 373 | friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); |
| 374 | friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); |
| 375 | friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); |
| 376 | |
| 377 | friend class GrGLSLExpr<GrGLSLExpr4>; |
| 378 | |
| 379 | typedef GrGLSLExpr<GrGLSLExpr4> INHERITED; |
| 380 | }; |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 381 | |
| 382 | /** |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 383 | * Does an inplace mul, *=, of vec4VarName by mulFactor. |
egdaniel | 089f8de | 2014-10-09 10:34:58 -0700 | [diff] [blame] | 384 | * A semicolon is added after the assignment. |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 385 | */ |
egdaniel | 089f8de | 2014-10-09 10:34:58 -0700 | [diff] [blame] | 386 | void GrGLSLMulVarBy4f(SkString* outAppend, const char* vec4VarName, const GrGLSLExpr4& mulFactor); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 387 | |
| 388 | #include "GrGLSL_impl.h" |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 389 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 390 | #endif |