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 | |
tomhudson@google.com | 6bf38b5 | 2012-02-14 15:11:59 +0000 | [diff] [blame] | 11 | #include "gl/GrGLInterface.h" |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 12 | #include "GrColor.h" |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 13 | #include "GrTypesPriv.h" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | 06f0598 | 2013-08-30 15:52:36 +0000 | [diff] [blame^] | 15 | class GrGLContextInfo; |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 16 | class GrGLShaderVar; |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 17 | class SkString; |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 18 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 19 | // Limited set of GLSL versions we build shaders for. Caller should round |
| 20 | // down the GLSL version to one of these enums. |
| 21 | enum GrGLSLGeneration { |
| 22 | /** |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 23 | * 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] | 24 | */ |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 25 | k110_GrGLSLGeneration, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Desktop GLSL 1.30 |
| 28 | */ |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 29 | k130_GrGLSLGeneration, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 30 | /** |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 31 | * Desktop GLSL 1.40 |
| 32 | */ |
| 33 | k140_GrGLSLGeneration, |
| 34 | /** |
| 35 | * Desktop GLSL 1.50 |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 36 | */ |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 37 | k150_GrGLSLGeneration, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 40 | enum GrSLConstantVec { |
| 41 | kZeros_GrSLConstantVec, |
| 42 | kOnes_GrSLConstantVec, |
| 43 | kNone_GrSLConstantVec, |
| 44 | }; |
| 45 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 46 | namespace { |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 47 | static inline int GrSLTypeToVecLength(GrSLType type) { |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 48 | static const int kVecLengths[] = { |
| 49 | 0, // kVoid_GrSLType |
| 50 | 1, // kFloat_GrSLType |
| 51 | 2, // kVec2f_GrSLType |
| 52 | 3, // kVec3f_GrSLType |
| 53 | 4, // kVec4f_GrSLType |
| 54 | 1, // kMat33f_GrSLType |
| 55 | 1, // kMat44f_GrSLType |
| 56 | 1, // kSampler2D_GrSLType |
| 57 | }; |
commit-bot@chromium.org | 5d7ca95 | 2013-04-22 20:26:44 +0000 | [diff] [blame] | 58 | GR_STATIC_ASSERT(kGrSLTypeCount == GR_ARRAY_COUNT(kVecLengths)); |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 59 | return kVecLengths[type]; |
| 60 | } |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 61 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 62 | static inline const char* GrGLSLOnesVecf(int count) { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 63 | static const char* kONESVEC[] = {"ERROR", "1.0", "vec2(1,1)", |
| 64 | "vec3(1,1,1)", "vec4(1,1,1,1)"}; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 65 | SkASSERT(count >= 1 && count < (int)GR_ARRAY_COUNT(kONESVEC)); |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 66 | return kONESVEC[count]; |
| 67 | } |
| 68 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 69 | static inline const char* GrGLSLZerosVecf(int count) { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 70 | static const char* kZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)", |
| 71 | "vec3(0,0,0)", "vec4(0,0,0,0)"}; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 72 | SkASSERT(count >= 1 && count < (int)GR_ARRAY_COUNT(kZEROSVEC)); |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 73 | return kZEROSVEC[count]; |
| 74 | } |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 75 | } |
| 76 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 77 | /** |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 78 | * Gets the most recent GLSL Generation compatible with the OpenGL context. |
| 79 | */ |
| 80 | GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding, |
| 81 | const GrGLInterface* gl); |
| 82 | |
| 83 | /** |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 84 | * Returns a string to include at the beginning of a shader to declare the GLSL |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 85 | * version. |
| 86 | */ |
commit-bot@chromium.org | 06f0598 | 2013-08-30 15:52:36 +0000 | [diff] [blame^] | 87 | const char* GrGetGLSLVersionDecl(const GrGLContextInfo&); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 90 | * Depending on the GLSL version being emitted there may be an assumed output |
| 91 | * variable from the fragment shader for the color. Otherwise, the shader must |
| 92 | * declare an output variable for the color. If this function returns true: |
| 93 | * * Parameter var's name will be set to nameIfDeclared |
| 94 | * * The variable must be declared in the fragment shader |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 95 | * * The variable has to be bound as the color output |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 96 | * (using glBindFragDataLocation) |
| 97 | * If the function returns false: |
| 98 | * * Parameter var's name will be set to the GLSL built-in color output name. |
| 99 | * * Do not declare the variable in the shader. |
| 100 | * * Do not use glBindFragDataLocation to bind the variable |
| 101 | * In either case var is initialized to represent the color output in the |
| 102 | * shader. |
| 103 | */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 104 | bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen, |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 105 | const char* nameIfDeclared, |
| 106 | GrGLShaderVar* var); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 107 | /** |
| 108 | * Converts a GrSLType to a string containing the name of the equivalent GLSL type. |
| 109 | */ |
| 110 | static const char* GrGLSLTypeString(GrSLType t) { |
| 111 | switch (t) { |
| 112 | case kVoid_GrSLType: |
| 113 | return "void"; |
| 114 | case kFloat_GrSLType: |
| 115 | return "float"; |
| 116 | case kVec2f_GrSLType: |
| 117 | return "vec2"; |
| 118 | case kVec3f_GrSLType: |
| 119 | return "vec3"; |
| 120 | case kVec4f_GrSLType: |
| 121 | return "vec4"; |
| 122 | case kMat33f_GrSLType: |
| 123 | return "mat3"; |
| 124 | case kMat44f_GrSLType: |
| 125 | return "mat4"; |
| 126 | case kSampler2D_GrSLType: |
| 127 | return "sampler2D"; |
| 128 | default: |
| 129 | GrCrash("Unknown shader var type."); |
| 130 | return ""; // suppress warning |
| 131 | } |
| 132 | } |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 133 | |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 134 | /** Return the type enum for a vector of floats of length n (1..4), |
| 135 | e.g. 1 -> "float", 2 -> "vec2", ... */ |
| 136 | static inline const char* GrGLSLFloatVectorTypeString(int n) { |
| 137 | return GrGLSLTypeString(GrSLFloatVectorType(n)); |
| 138 | } |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 139 | |
| 140 | /** Return the GLSL swizzle operator for a homogenous component of a vector |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 141 | with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 142 | const char* GrGLSLVectorHomogCoord(int count); |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 143 | const char* GrGLSLVectorHomogCoord(GrSLType type); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 144 | |
| 145 | /** Return the GLSL swizzle operator for a nonhomogenous components of a vector |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 146 | with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 147 | const char* GrGLSLVectorNonhomogCoords(int count); |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 148 | const char* GrGLSLVectorNonhomogCoords(GrSLType type); |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 149 | |
| 150 | /** |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 151 | * Produces a string that is the result of modulating two inputs. The inputs must be vecN or |
| 152 | * float. The result is always a vecN. The inputs may be expressions, not just identifier names. |
| 153 | * Either can be NULL or "" in which case the default params control whether a vector of ones or |
| 154 | * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the |
| 155 | * function determines that the result is a zeros or ones vec then any expression represented by |
| 156 | * or in1 will not be emitted (side effects won't occur). The return value indicates whether a |
| 157 | * known zeros or ones vector resulted. The output can be suppressed when known vector is produced |
| 158 | * by passing true for omitIfConstVec. |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 159 | */ |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 160 | template <int N> |
| 161 | GrSLConstantVec GrGLSLModulatef(SkString* outAppend, |
| 162 | const char* in0, |
| 163 | const char* in1, |
| 164 | GrSLConstantVec default0 = kOnes_GrSLConstantVec, |
| 165 | GrSLConstantVec default1 = kOnes_GrSLConstantVec, |
| 166 | bool omitIfConstVec = false); |
| 167 | |
| 168 | /** |
| 169 | * Produces a string that is the result of adding two inputs. The inputs must be vecN or |
| 170 | * float. The result is always a vecN. The inputs may be expressions, not just identifier names. |
| 171 | * Either can be NULL or "" in which case the default params control whether a vector of ones or |
| 172 | * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the |
| 173 | * function determines that the result is a zeros or ones vec then any expression represented by |
| 174 | * or in1 will not be emitted (side effects won't occur). The return value indicates whether a |
| 175 | * known zeros or ones vector resulted. The output can be suppressed when known vector is produced |
| 176 | * by passing true for omitIfConstVec. |
| 177 | */ |
| 178 | template <int N> |
| 179 | GrSLConstantVec GrGLSLAddf(SkString* outAppend, |
| 180 | const char* in0, |
| 181 | const char* in1, |
| 182 | GrSLConstantVec default0 = kZeros_GrSLConstantVec, |
| 183 | GrSLConstantVec default1 = kZeros_GrSLConstantVec, |
| 184 | bool omitIfConstVec = false); |
| 185 | |
| 186 | /** |
| 187 | * Produces a string that is the result of subtracting two inputs. The inputs must be vecN or |
| 188 | * float. The result is always a vecN. The inputs may be expressions, not just identifier names. |
| 189 | * Either can be NULL or "" in which case the default params control whether a vector of ones or |
| 190 | * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the |
| 191 | * function determines that the result is a zeros or ones vec then any expression represented by |
| 192 | * or in1 will not be emitted (side effects won't occur). The return value indicates whether a |
| 193 | * known zeros or ones vector resulted. The output can be suppressed when known vector is produced |
| 194 | * by passing true for omitIfConstVec. |
| 195 | */ |
| 196 | template <int N> |
| 197 | GrSLConstantVec GrGLSLSubtractf(SkString* outAppend, |
| 198 | const char* in0, |
| 199 | const char* in1, |
| 200 | GrSLConstantVec default0 = kZeros_GrSLConstantVec, |
| 201 | GrSLConstantVec default1 = kZeros_GrSLConstantVec, |
| 202 | bool omitIfConstVec = false); |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 203 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 204 | /** |
| 205 | * Does an inplace mul, *=, of vec4VarName by mulFactor. If mulFactorDefault is not kNone then |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 206 | * mulFactor may be either "" or NULL. In this case either nothing will be appended (kOnes) or an |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 207 | * assignment of vec(0,0,0,0) will be appended (kZeros). The assignment is prepended by tabCnt tabs. |
| 208 | * A semicolon and newline are added after the assignment. (TODO: Remove tabCnt when we auto-insert |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 209 | * tabs to GrGLEffect-generated lines.) If a zeros vec is assigned then the return value is |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 210 | * kZeros, otherwise kNone. |
| 211 | */ |
| 212 | GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend, |
| 213 | int tabCnt, |
| 214 | const char* vec4VarName, |
| 215 | const char* mulFactor, |
| 216 | GrSLConstantVec mulFactorDefault = kOnes_GrSLConstantVec); |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 217 | |
| 218 | /** |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 219 | * Given an expression that evaluates to a GLSL vec4, extract a component. If expr is NULL or "" |
| 220 | * the value of defaultExpr is used. It is an error to pass an empty expr and have set defaultExpr |
| 221 | * to kNone. The return value indicates whether the value is known to be 0 or 1. If omitIfConst is |
| 222 | * set then nothing is appended when the return is not kNone. |
| 223 | */ |
| 224 | GrSLConstantVec GrGLSLGetComponent4f(SkString* outAppend, |
| 225 | const char* expr, |
| 226 | GrColorComponentFlags component, |
| 227 | GrSLConstantVec defaultExpr = kNone_GrSLConstantVec, |
| 228 | bool omitIfConst = false); |
| 229 | |
| 230 | #include "GrGLSL_impl.h" |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 231 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 232 | #endif |