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" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 12 | |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 13 | class GrGLShaderVar; |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 14 | class SkString; |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 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, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 37 | /** |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 38 | * Types of shader-language-specific boxed variables we can create. |
| 39 | * (Currently only GrGLShaderVars, but should be applicable to other shader |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 40 | * languages.) |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 41 | */ |
| 42 | enum GrSLType { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 43 | kVoid_GrSLType, |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 44 | kFloat_GrSLType, |
| 45 | kVec2f_GrSLType, |
| 46 | kVec3f_GrSLType, |
| 47 | kVec4f_GrSLType, |
| 48 | kMat33f_GrSLType, |
| 49 | kMat44f_GrSLType, |
| 50 | kSampler2D_GrSLType |
| 51 | }; |
| 52 | |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 53 | enum GrSLConstantVec { |
| 54 | kZeros_GrSLConstantVec, |
| 55 | kOnes_GrSLConstantVec, |
| 56 | kNone_GrSLConstantVec, |
| 57 | }; |
| 58 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 59 | namespace { |
| 60 | inline int GrSLTypeToVecLength(GrSLType type) { |
| 61 | static const int kVecLengths[] = { |
| 62 | 0, // kVoid_GrSLType |
| 63 | 1, // kFloat_GrSLType |
| 64 | 2, // kVec2f_GrSLType |
| 65 | 3, // kVec3f_GrSLType |
| 66 | 4, // kVec4f_GrSLType |
| 67 | 1, // kMat33f_GrSLType |
| 68 | 1, // kMat44f_GrSLType |
| 69 | 1, // kSampler2D_GrSLType |
| 70 | }; |
| 71 | GrAssert((size_t) type < GR_ARRAY_COUNT(kVecLengths)); |
| 72 | return kVecLengths[type]; |
| 73 | } |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 74 | |
| 75 | const char* GrGLSLOnesVecf(int count) { |
| 76 | static const char* kONESVEC[] = {"ERROR", "1.0", "vec2(1,1)", |
| 77 | "vec3(1,1,1)", "vec4(1,1,1,1)"}; |
| 78 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(kONESVEC)); |
| 79 | return kONESVEC[count]; |
| 80 | } |
| 81 | |
| 82 | const char* GrGLSLZerosVecf(int count) { |
| 83 | static const char* kZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)", |
| 84 | "vec3(0,0,0)", "vec4(0,0,0,0)"}; |
| 85 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(kZEROSVEC)); |
| 86 | return kZEROSVEC[count]; |
| 87 | } |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 88 | } |
| 89 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 90 | /** |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 91 | * Gets the most recent GLSL Generation compatible with the OpenGL context. |
| 92 | */ |
| 93 | GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding, |
| 94 | const GrGLInterface* gl); |
| 95 | |
| 96 | /** |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 97 | * 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] | 98 | * version. |
| 99 | */ |
| 100 | const char* GrGetGLSLVersionDecl(GrGLBinding binding, |
| 101 | GrGLSLGeneration v); |
| 102 | |
| 103 | /** |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 104 | * Depending on the GLSL version being emitted there may be an assumed output |
| 105 | * variable from the fragment shader for the color. Otherwise, the shader must |
| 106 | * declare an output variable for the color. If this function returns true: |
| 107 | * * Parameter var's name will be set to nameIfDeclared |
| 108 | * * The variable must be declared in the fragment shader |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 109 | * * The variable has to be bound as the color output |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 110 | * (using glBindFragDataLocation) |
| 111 | * If the function returns false: |
| 112 | * * Parameter var's name will be set to the GLSL built-in color output name. |
| 113 | * * Do not declare the variable in the shader. |
| 114 | * * Do not use glBindFragDataLocation to bind the variable |
| 115 | * In either case var is initialized to represent the color output in the |
| 116 | * shader. |
| 117 | */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 118 | bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen, |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 119 | const char* nameIfDeclared, |
| 120 | GrGLShaderVar* var); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 121 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 122 | /** Convert a count of 1..n floats into the corresponding type enum, |
| 123 | e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */ |
| 124 | GrSLType GrSLFloatVectorType(int count); |
| 125 | |
| 126 | /** 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] | 127 | 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] | 128 | const char* GrGLSLVectorHomogCoord(int count); |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 129 | const char* GrGLSLVectorHomogCoord(GrSLType type); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 130 | |
| 131 | /** 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] | 132 | 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] | 133 | const char* GrGLSLVectorNonhomogCoords(int count); |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 134 | const char* GrGLSLVectorNonhomogCoords(GrSLType type); |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * Produces a string that is the result of modulating two inputs. The inputs must be vec4 or |
| 138 | * float. The result is always a vec4. The inputs may be expressions, not just identifier names. |
| 139 | * Either can be NULL or "" in which case the default params control whether vec4(1,1,1,1) or |
| 140 | * vec4(0,0,0,0) is assumed. It is an error to pass kNone for default<i> if in<i> is NULL or "". |
| 141 | * Note that when if function determines that the result is a zeros or ones vec then any expression |
| 142 | * represented by in0 or in1 will not be emitted. The return value indicates whether a zeros, ones |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 143 | * or neither was appended. |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 144 | */ |
| 145 | GrSLConstantVec GrGLSLModulate4f(SkString* outAppend, |
| 146 | const char* in0, |
| 147 | const char* in1, |
| 148 | GrSLConstantVec default0 = kOnes_GrSLConstantVec, |
| 149 | GrSLConstantVec default1 = kOnes_GrSLConstantVec); |
| 150 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 151 | /** |
| 152 | * 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] | 153 | * 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] | 154 | * assignment of vec(0,0,0,0) will be appended (kZeros). The assignment is prepended by tabCnt tabs. |
| 155 | * 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^] | 156 | * 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] | 157 | * kZeros, otherwise kNone. |
| 158 | */ |
| 159 | GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend, |
| 160 | int tabCnt, |
| 161 | const char* vec4VarName, |
| 162 | const char* mulFactor, |
| 163 | GrSLConstantVec mulFactorDefault = kOnes_GrSLConstantVec); |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Produces a string that is the result of adding two inputs. The inputs must be vec4 or float. |
| 167 | * The result is always a vec4. The inputs may be expressions, not just identifier names. Either |
| 168 | * can be NULL or "" in which case if the default is kZeros then vec4(0,0,0,0) is assumed. It is an |
| 169 | * error to pass kOnes for either default or to pass kNone for default<i> if in<i> is NULL or "". |
| 170 | * Note that if the function determines that the result is a zeros vec any expression represented |
| 171 | * by in0 or in1 will not be emitted. The return value indicates whether a zeros vec was appended |
| 172 | * or not. |
| 173 | */ |
| 174 | GrSLConstantVec GrGLSLAdd4f(SkString* outAppend, |
| 175 | const char* in0, |
| 176 | const char* in1, |
| 177 | GrSLConstantVec default0 = kZeros_GrSLConstantVec, |
| 178 | GrSLConstantVec default1 = kZeros_GrSLConstantVec); |
| 179 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 180 | #endif |