| 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 | |
| 29 | void appendVarying(GrSLType type, |
| 30 | const char* name, |
| 31 | const char** vsOutName = NULL, |
| 32 | const char** fsInName = NULL); |
| 33 | |
| 34 | void appendVarying(GrSLType type, |
| 35 | const char* name, |
| 36 | int stageNum, |
| 37 | const char** vsOutName = NULL, |
| 38 | const char** fsInName = NULL); |
| 39 | |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 40 | void computeSwizzle(uint32_t configFlags); |
| 41 | void computeModulate(const char* fsInColor); |
| 42 | |
| 43 | void emitTextureSetup(); |
| 44 | |
| 45 | /** texture2D(samplerName, coordName), with projection |
| 46 | if necessary; if coordName is not specified, |
| 47 | uses fSampleCoords. */ |
| 48 | void emitTextureLookup(const char* samplerName, |
| 49 | const char* coordName = NULL); |
| 50 | |
| 51 | /** sets outColor to results of texture lookup, with |
| 52 | swizzle, and/or modulate as necessary */ |
| 53 | void emitDefaultFetch(const char* outColor, |
| 54 | const char* samplerName); |
| 55 | |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame^] | 56 | /* TODO: can't arbitrarily OR together enum components, so |
| 57 | VariableLifetime will need to be reworked if we add |
| 58 | Geometry shaders. */ |
| 59 | enum VariableLifetime { |
| 60 | kVertex_VariableLifetime = 1, |
| 61 | kFragment_VariableLifetime = 2, |
| 62 | kBoth_VariableLifetime = 3 |
| 63 | }; |
| 64 | |
| 65 | /** Add a uniform variable to the current program, accessed |
| 66 | in vertex, fragment, or both stages. If stageNum is |
| 67 | specified, it is appended to the name to guarantee uniqueness; |
| 68 | if count is specified, the uniform is an array. |
| 69 | */ |
| 70 | const GrGLShaderVar& addUniform(VariableLifetime lifetime, |
| 71 | GrSLType type, |
| 72 | const char* name, |
| 73 | int stageNum = -1, |
| 74 | int count = GrGLShaderVar::kNonArray); |
| 75 | |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 76 | // TODO: needs a better name |
| 77 | enum SamplerMode { |
| 78 | kDefault_SamplerMode, |
| 79 | kProj_SamplerMode, |
| 80 | kExplicitDivide_SamplerMode // must do an explicit divide |
| 81 | }; |
| 82 | |
| 83 | // TODO: computing this requires information about fetch mode |
| 84 | // && coord mapping, as well as StageDesc::fOptFlags - proably |
| 85 | // need to set up default value and have some custom stages |
| 86 | // override as necessary? |
| 87 | void setSamplerMode(SamplerMode samplerMode) { |
| 88 | fSamplerMode = samplerMode; |
| 89 | } |
| 90 | |
| 91 | |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 92 | GrStringBuilder fHeader; // VS+FS, GLSL version, etc |
| 93 | VarArray fVSUnis; |
| 94 | VarArray fVSAttrs; |
| 95 | VarArray fVSOutputs; |
| 96 | VarArray fGSInputs; |
| 97 | VarArray fGSOutputs; |
| 98 | VarArray fFSInputs; |
| 99 | GrStringBuilder fGSHeader; // layout qualifiers specific to GS |
| 100 | VarArray fFSUnis; |
| 101 | VarArray fFSOutputs; |
| 102 | GrStringBuilder fFSFunctions; |
| 103 | GrStringBuilder fVSCode; |
| 104 | GrStringBuilder fGSCode; |
| 105 | GrStringBuilder fFSCode; |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 106 | bool fUsesGS; |
| 107 | |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 108 | /// Per-stage settings - only valid while we're inside |
| 109 | /// GrGLProgram::genStageCode(). |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 110 | //@{ |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 111 | |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 112 | int fVaryingDims; |
| tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 113 | static const int fCoordDims = 2; |
| 114 | |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 115 | protected: |
| 116 | |
| 117 | SamplerMode fSamplerMode; |
| 118 | |
| 119 | public: |
| 120 | |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 121 | /// True if fSampleCoords is an expression; false if it's a bare |
| 122 | /// variable name |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 123 | bool fComplexCoord; |
| 124 | GrStringBuilder fSampleCoords; |
| 125 | |
| 126 | GrStringBuilder fSwizzle; |
| 127 | GrStringBuilder fModulate; |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 128 | |
| 129 | //@} |
| tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 130 | |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | #endif |