| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 GrGLShaderBuilder_DEFINED |
| 9 | #define GrGLShaderBuilder_DEFINED |
| 10 | |
| 11 | #include "GrAllocator.h" |
| 12 | #include "gl/GrGLShaderVar.h" |
| 13 | #include "gl/GrGLSL.h" |
| 14 | |
| 15 | typedef GrTAllocator<GrGLShaderVar> VarArray; |
| 16 | |
| 17 | /** |
| 18 | Containts all the incremental state of a shader as it is being built, |
| 19 | as well as helpers to manipulate that state. |
| 20 | TODO: migrate CompileShaders() here? |
| 21 | */ |
| 22 | |
| 23 | class GrGLShaderBuilder { |
| 24 | |
| 25 | public: |
| 26 | |
| 27 | GrGLShaderBuilder(); |
| 28 | |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 29 | void computeSwizzle(uint32_t configFlags); |
| 30 | void computeModulate(const char* fsInColor); |
| 31 | |
| tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 32 | // TODO: needs a better name |
| 33 | enum SamplerMode { |
| 34 | kDefault_SamplerMode, |
| 35 | kProj_SamplerMode, |
| 36 | kExplicitDivide_SamplerMode // must do an explicit divide |
| 37 | }; |
| 38 | |
| 39 | /** Determines whether we should use texture2D() or texture2Dproj(), |
| 40 | and if an explicit divide is required for the sample coordinates, |
| 41 | creates the new variable and emits the code to initialize it. */ |
| 42 | void setupTextureAccess(SamplerMode samplerMode, int stageNum); |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 43 | |
| 44 | /** texture2D(samplerName, coordName), with projection |
| 45 | if necessary; if coordName is not specified, |
| 46 | uses fSampleCoords. */ |
| 47 | void emitTextureLookup(const char* samplerName, |
| 48 | const char* coordName = NULL); |
| 49 | |
| 50 | /** sets outColor to results of texture lookup, with |
| 51 | swizzle, and/or modulate as necessary */ |
| 52 | void emitDefaultFetch(const char* outColor, |
| 53 | const char* samplerName); |
| 54 | |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 55 | /* TODO: can't arbitrarily OR together enum components, so |
| 56 | VariableLifetime will need to be reworked if we add |
| 57 | Geometry shaders. */ |
| 58 | enum VariableLifetime { |
| 59 | kVertex_VariableLifetime = 1, |
| 60 | kFragment_VariableLifetime = 2, |
| 61 | kBoth_VariableLifetime = 3 |
| 62 | }; |
| 63 | |
| 64 | /** Add a uniform variable to the current program, accessed |
| 65 | in vertex, fragment, or both stages. If stageNum is |
| 66 | specified, it is appended to the name to guarantee uniqueness; |
| 67 | if count is specified, the uniform is an array. |
| 68 | */ |
| 69 | const GrGLShaderVar& addUniform(VariableLifetime lifetime, |
| 70 | GrSLType type, |
| 71 | const char* name, |
| 72 | int stageNum = -1, |
| 73 | int count = GrGLShaderVar::kNonArray); |
| 74 | |
| tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 75 | /** Add a varying variable to the current program to pass |
| 76 | values between vertex and fragment shaders. |
| 77 | If the last two parameters are non-NULL, they are filled |
| 78 | in with the name generated. */ |
| 79 | void addVarying(GrSLType type, |
| 80 | const char* name, |
| 81 | const char** vsOutName = NULL, |
| 82 | const char** fsInName = NULL); |
| 83 | |
| 84 | /** Add a varying variable to the current program to pass |
| 85 | values between vertex and fragment shaders; |
| 86 | stageNum is appended to the name to guarantee uniqueness. |
| 87 | If the last two parameters are non-NULL, they are filled |
| 88 | in with the name generated. */ |
| 89 | void addVarying(GrSLType type, |
| 90 | const char* name, |
| 91 | int stageNum, |
| 92 | const char** vsOutName = NULL, |
| 93 | const char** fsInName = NULL); |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 94 | |
| 95 | SkString fHeader; // VS+FS, GLSL version, etc |
| 96 | VarArray fVSUnis; |
| 97 | VarArray fVSAttrs; |
| 98 | VarArray fVSOutputs; |
| 99 | VarArray fGSInputs; |
| 100 | VarArray fGSOutputs; |
| 101 | VarArray fFSInputs; |
| 102 | SkString fGSHeader; // layout qualifiers specific to GS |
| 103 | VarArray fFSUnis; |
| 104 | VarArray fFSOutputs; |
| 105 | SkString fFSFunctions; |
| 106 | SkString fVSCode; |
| 107 | SkString fGSCode; |
| 108 | SkString fFSCode; |
| 109 | bool fUsesGS; |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 110 | |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 111 | /// Per-stage settings - only valid while we're inside |
| 112 | /// GrGLProgram::genStageCode(). |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 113 | //@{ |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 114 | |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 115 | int fVaryingDims; |
| tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 116 | static const int fCoordDims = 2; |
| 117 | |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 118 | /// True if fSampleCoords is an expression; false if it's a bare |
| 119 | /// variable name |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 120 | bool fComplexCoord; |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 121 | SkString fSampleCoords; |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 122 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 123 | SkString fSwizzle; |
| 124 | SkString fModulate; |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 125 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 126 | SkString fTexFunc; |
| tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 127 | |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 128 | //@} |
| tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 129 | |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | #endif |