| 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" |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 12 | #include "GrCustomStage.h" |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 13 | #include "gl/GrGLShaderVar.h" |
| 14 | #include "gl/GrGLSL.h" |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 15 | #include "gl/GrGLUniformManager.h" |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 16 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 17 | class GrGLContextInfo; |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 18 | |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 19 | /** |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 20 | Contains all the incremental state of a shader as it is being built,as well as helpers to |
| 21 | manipulate that state. |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 22 | */ |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 23 | class GrGLShaderBuilder { |
| 24 | |
| 25 | public: |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 26 | enum ShaderType { |
| 27 | kVertex_ShaderType = 0x1, |
| 28 | kGeometry_ShaderType = 0x2, |
| 29 | kFragment_ShaderType = 0x4, |
| 30 | }; |
| 31 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 32 | GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&); |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 33 | |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 34 | void computeSwizzle(uint32_t configFlags); |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 35 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 36 | /** Determines whether we should use texture2D() or texture2Dproj(), and if an explicit divide |
| 37 | is required for the sample coordinates, creates the new variable and emits the code to |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 38 | initialize it. This should only be called by GrGLProgram.*/ |
| 39 | void setupTextureAccess(const char* varyingFSName, GrSLType varyingType); |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 40 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 41 | /** texture2D(samplerName, coordName), with projection if necessary; if coordName is not |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 42 | specified, uses fSampleCoords. coordType must either be Vec2f or Vec3f. The latter is |
| 43 | interpreted as projective texture coords. */ |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 44 | void appendTextureLookup(SkString* out, |
| 45 | const char* samplerName, |
| 46 | const char* coordName = NULL, |
| 47 | GrSLType coordType = kVec2f_GrSLType) const; |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 48 | |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 49 | /** appends a texture lookup, with swizzle as necessary. If coordName is NULL then it as if |
| 50 | defaultTexCoordsName() was passed. coordType must be either kVec2f or kVec3f. If modulateVar |
| 51 | is not NULL or "" then the texture lookup will be modulated by it. modulation must refer to |
| 52 | be expression that evaluates to a float or vec4. */ |
| 53 | void appendTextureLookupAndModulate(SkString* out, |
| 54 | const char* modulation, |
| 55 | const char* samplerName, |
| 56 | const char* coordName = NULL, |
| 57 | GrSLType varyingType = kVec2f_GrSLType) const; |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 58 | |
| 59 | /** Gets the name of the default texture coords which are always kVec2f */ |
| 60 | const char* defaultTexCoordsName() const { return fDefaultTexCoordsName.c_str(); } |
| 61 | |
| 62 | /* Returns true if the texture matrix from which the default texture coords are computed has |
| 63 | perspective. */ |
| 64 | bool defaultTextureMatrixIsPerspective() const { |
| 65 | return fTexCoordVaryingType == kVec3f_GrSLType; |
| 66 | } |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 67 | |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 68 | /** Emits a texture lookup to the shader code with the form: |
| 69 | texture2D{Proj}(samplerName, coordName).swizzle |
| 70 | The routine selects the type of texturing based on samplerMode. |
| 71 | The generated swizzle state is built based on the format of the texture and the requested |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 72 | swizzle access pattern. coordType must either be Vec2f or Vec3f. The latter is interpreted |
| 73 | as projective texture coords.*/ |
| 74 | void emitCustomTextureLookup(const GrTextureAccess& textureAccess, |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 75 | const char* samplerName, |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 76 | const char* coordName, |
| 77 | GrSLType coordType = kVec2f_GrSLType); |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 78 | |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 79 | /** Emits a helper function outside of main(). Currently ShaderType must be |
| 80 | kFragment_ShaderType. */ |
| 81 | void emitFunction(ShaderType shader, |
| 82 | GrSLType returnType, |
| 83 | const char* name, |
| 84 | int argCnt, |
| 85 | const GrGLShaderVar* args, |
| 86 | const char* body, |
| 87 | SkString* outName); |
| 88 | |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 89 | /** Generates a StageKey for the shader code based on the texture access parameters and the |
| 90 | capabilities of the GL context. This is useful for keying the shader programs that may |
| 91 | have multiple representations, based on the type/format of textures used. */ |
| 92 | static GrCustomStage::StageKey KeyForTextureAccess(const GrTextureAccess& access, |
| 93 | const GrGLCaps& caps); |
| 94 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 95 | /** Add a uniform variable to the current program, that has visibilty in one or more shaders. |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 96 | visibility is a bitfield of ShaderType values indicating from which shaders the uniform |
| 97 | should be accessible. At least one bit must be set. Geometry shader uniforms are not |
| 98 | supported at this time. The actual uniform name will be mangled. If outName is not NULL then |
| 99 | it will refer to the final uniform name after return. Use the addUniformArray variant to add |
| 100 | an array of uniforms. |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 101 | */ |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 102 | GrGLUniformManager::UniformHandle addUniform(uint32_t visibility, |
| 103 | GrSLType type, |
| 104 | const char* name, |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 105 | const char** outName = NULL) { |
| 106 | return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName); |
| 107 | } |
| 108 | GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility, |
| 109 | GrSLType type, |
| 110 | const char* name, |
| 111 | int arrayCount, |
| 112 | const char** outName = NULL); |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 113 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 114 | const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle) const; |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Shorcut for getUniformVariable(u).c_str() |
| 118 | */ |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 119 | const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const { |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 120 | return this->getUniformVariable(u).c_str(); |
| 121 | } |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 122 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 123 | /** Add a varying variable to the current program to pass values between vertex and fragment |
| 124 | shaders. If the last two parameters are non-NULL, they are filled in with the name |
| 125 | generated. */ |
| tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 126 | void addVarying(GrSLType type, |
| 127 | const char* name, |
| 128 | const char** vsOutName = NULL, |
| 129 | const char** fsInName = NULL); |
| 130 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 131 | /** Called after building is complete to get the final shader string. */ |
| 132 | void getShader(ShaderType, SkString*) const; |
| 133 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 134 | /** |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 135 | * TODO: Make this do all the compiling, linking, etc. Hide from the custom stages |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 136 | */ |
| 137 | void finished(GrGLuint programID); |
| 138 | |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 139 | /** |
| 140 | * Sets the current stage (used to make variable names unique). |
| 141 | * TODO: Hide from the custom stages |
| 142 | */ |
| 143 | void setCurrentStage(int stage) { fCurrentStage = stage; } |
| 144 | void setNonStage() { fCurrentStage = kNonStageIdx; } |
| 145 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 146 | private: |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 147 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 148 | typedef GrTAllocator<GrGLShaderVar> VarArray; |
| 149 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 150 | void appendDecls(const VarArray&, SkString*) const; |
| 151 | void appendUniformDecls(ShaderType, SkString*) const; |
| 152 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 153 | typedef GrGLUniformManager::BuilderUniform BuilderUniform; |
| 154 | GrGLUniformManager::BuilderUniformArray fUniforms; |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 155 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 156 | // TODO: Everything below here private. |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 157 | public: |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 158 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 159 | SkString fHeader; // VS+FS, GLSL version, etc |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 160 | VarArray fVSAttrs; |
| 161 | VarArray fVSOutputs; |
| 162 | VarArray fGSInputs; |
| 163 | VarArray fGSOutputs; |
| 164 | VarArray fFSInputs; |
| 165 | SkString fGSHeader; // layout qualifiers specific to GS |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 166 | VarArray fFSOutputs; |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 167 | SkString fVSCode; |
| 168 | SkString fGSCode; |
| 169 | SkString fFSCode; |
| 170 | bool fUsesGS; |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 171 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 172 | /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode(). |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 173 | //@{ |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 174 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 175 | SkString fSwizzle; |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 176 | |
| 177 | //@} |
| tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 178 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 179 | private: |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 180 | enum { |
| 181 | kNonStageIdx = -1, |
| 182 | }; |
| 183 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 184 | const GrGLContextInfo& fContext; |
| 185 | GrGLUniformManager& fUniformManager; |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 186 | int fCurrentStage; |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 187 | SkString fFSFunctions; |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 188 | |
| 189 | /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode(). |
| 190 | //@{ |
| 191 | GrSLType fTexCoordVaryingType; // the type, either Vec2f or Vec3f, of the coords passed |
| 192 | // as a varying from the VS to the FS. |
| 193 | SkString fDefaultTexCoordsName; // the name of the default 2D coords value. |
| 194 | //@} |
| 195 | |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | #endif |