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