| 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 { |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 24 | public: |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 25 | /** |
| 26 | * Used by GrGLProgramStages to add texture reads to their shader code. |
| 27 | */ |
| 28 | class TextureSampler { |
| 29 | public: |
| 30 | TextureSampler() |
| 31 | : fTextureAccess(NULL) |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 32 | , fSamplerUniform(GrGLUniformManager::kInvalidUniformHandle) {} |
| 33 | |
| 34 | TextureSampler(const TextureSampler& other) { *this = other; } |
| 35 | |
| 36 | TextureSampler& operator= (const TextureSampler& other) { |
| 37 | GrAssert(NULL == fTextureAccess); |
| 38 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUniform); |
| 39 | |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 40 | fTextureAccess = other.fTextureAccess; |
| 41 | fSamplerUniform = other.fSamplerUniform; |
| 42 | return *this; |
| 43 | } |
| 44 | |
| 45 | const GrTextureAccess* textureAccess() const { return fTextureAccess; } |
| 46 | |
| 47 | private: |
| 48 | void init(GrGLShaderBuilder* builder, const GrTextureAccess* access) { |
| 49 | GrAssert(NULL == fTextureAccess); |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 50 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUniform); |
| 51 | |
| 52 | GrAssert(NULL != builder); |
| 53 | GrAssert(NULL != access); |
| 54 | fSamplerUniform = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 55 | kSampler2D_GrSLType, |
| 56 | "Sampler"); |
| 57 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != fSamplerUniform); |
| 58 | |
| 59 | fTextureAccess = access; |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | const GrTextureAccess* fTextureAccess; |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 63 | GrGLUniformManager::UniformHandle fSamplerUniform; |
| 64 | |
| 65 | friend class GrGLShaderBuilder; // to access fSamplerUniform |
| 66 | friend class GrGLProgram; // to construct these and access fSamplerUniform. |
| 67 | }; |
| 68 | |
| 69 | typedef SkTArray<TextureSampler> TextureSamplerArray; |
| 70 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 71 | enum ShaderType { |
| 72 | kVertex_ShaderType = 0x1, |
| 73 | kGeometry_ShaderType = 0x2, |
| 74 | kFragment_ShaderType = 0x4, |
| 75 | }; |
| 76 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 77 | GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&); |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 78 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 79 | /** Determines whether we should use texture2D() or texture2Dproj(), and if an explicit divide |
| 80 | 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] | 81 | initialize it. This should only be called by GrGLProgram.*/ |
| 82 | void setupTextureAccess(const char* varyingFSName, GrSLType varyingType); |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 83 | |
| bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 84 | /** Appends a texture sample with projection if necessary; if coordName is not |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 85 | specified, uses fSampleCoords. coordType must either be Vec2f or Vec3f. The latter is |
| bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 86 | interpreted as projective texture coords. The vec length and swizzle order of the result |
| 87 | depends on the GrTextureAccess associated with the TextureSampler. */ |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 88 | void appendTextureLookup(SkString* out, |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 89 | const TextureSampler&, |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 90 | const char* coordName = NULL, |
| 91 | GrSLType coordType = kVec2f_GrSLType) const; |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 92 | |
| bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 93 | /** Does the work of appendTextureLookup and modulates the result by modulation. The result is |
| 94 | always a vec4. modulation and the swizzle specified by TextureSampler must both be vec4 or |
| 95 | float. If modulation is "" or NULL it this function acts as though appendTextureLookup were |
| 96 | called. */ |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 97 | void appendTextureLookupAndModulate(SkString* out, |
| 98 | const char* modulation, |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 99 | const TextureSampler&, |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 100 | const char* coordName = NULL, |
| bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 101 | GrSLType coordType = kVec2f_GrSLType) const; |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 102 | |
| 103 | /** Gets the name of the default texture coords which are always kVec2f */ |
| 104 | const char* defaultTexCoordsName() const { return fDefaultTexCoordsName.c_str(); } |
| 105 | |
| 106 | /* Returns true if the texture matrix from which the default texture coords are computed has |
| 107 | perspective. */ |
| 108 | bool defaultTextureMatrixIsPerspective() const { |
| 109 | return fTexCoordVaryingType == kVec3f_GrSLType; |
| 110 | } |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 111 | |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 112 | /** Emits a helper function outside of main(). Currently ShaderType must be |
| 113 | kFragment_ShaderType. */ |
| 114 | void emitFunction(ShaderType shader, |
| 115 | GrSLType returnType, |
| 116 | const char* name, |
| 117 | int argCnt, |
| 118 | const GrGLShaderVar* args, |
| 119 | const char* body, |
| 120 | SkString* outName); |
| 121 | |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 122 | /** Generates a StageKey for the shader code based on the texture access parameters and the |
| 123 | capabilities of the GL context. This is useful for keying the shader programs that may |
| 124 | have multiple representations, based on the type/format of textures used. */ |
| 125 | static GrCustomStage::StageKey KeyForTextureAccess(const GrTextureAccess& access, |
| 126 | const GrGLCaps& caps); |
| 127 | |
| bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame^] | 128 | /** If texture swizzling is available using tex parameters then it is preferred over mangling |
| 129 | the generated shader code. This potentially allows greater reuse of cached shaders. */ |
| 130 | static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps); |
| 131 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 132 | /** 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] | 133 | visibility is a bitfield of ShaderType values indicating from which shaders the uniform |
| 134 | should be accessible. At least one bit must be set. Geometry shader uniforms are not |
| 135 | supported at this time. The actual uniform name will be mangled. If outName is not NULL then |
| 136 | it will refer to the final uniform name after return. Use the addUniformArray variant to add |
| 137 | an array of uniforms. |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 138 | */ |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 139 | GrGLUniformManager::UniformHandle addUniform(uint32_t visibility, |
| 140 | GrSLType type, |
| 141 | const char* name, |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 142 | const char** outName = NULL) { |
| 143 | return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName); |
| 144 | } |
| 145 | GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility, |
| 146 | GrSLType type, |
| 147 | const char* name, |
| 148 | int arrayCount, |
| 149 | const char** outName = NULL); |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 150 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 151 | const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle) const; |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Shorcut for getUniformVariable(u).c_str() |
| 155 | */ |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 156 | const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const { |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 157 | return this->getUniformVariable(u).c_str(); |
| 158 | } |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 159 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 160 | /** Add a varying variable to the current program to pass values between vertex and fragment |
| 161 | shaders. If the last two parameters are non-NULL, they are filled in with the name |
| 162 | generated. */ |
| tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 163 | void addVarying(GrSLType type, |
| 164 | const char* name, |
| 165 | const char** vsOutName = NULL, |
| 166 | const char** fsInName = NULL); |
| 167 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 168 | /** Called after building is complete to get the final shader string. */ |
| 169 | void getShader(ShaderType, SkString*) const; |
| 170 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 171 | /** |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 172 | * 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] | 173 | */ |
| 174 | void finished(GrGLuint programID); |
| 175 | |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 176 | /** |
| 177 | * Sets the current stage (used to make variable names unique). |
| 178 | * TODO: Hide from the custom stages |
| 179 | */ |
| 180 | void setCurrentStage(int stage) { fCurrentStage = stage; } |
| 181 | void setNonStage() { fCurrentStage = kNonStageIdx; } |
| 182 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 183 | private: |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 184 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 185 | typedef GrTAllocator<GrGLShaderVar> VarArray; |
| 186 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 187 | void appendDecls(const VarArray&, SkString*) const; |
| 188 | void appendUniformDecls(ShaderType, SkString*) const; |
| 189 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 190 | typedef GrGLUniformManager::BuilderUniform BuilderUniform; |
| 191 | GrGLUniformManager::BuilderUniformArray fUniforms; |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 192 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 193 | // TODO: Everything below here private. |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 194 | public: |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 195 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 196 | SkString fHeader; // VS+FS, GLSL version, etc |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 197 | VarArray fVSAttrs; |
| 198 | VarArray fVSOutputs; |
| 199 | VarArray fGSInputs; |
| 200 | VarArray fGSOutputs; |
| 201 | VarArray fFSInputs; |
| 202 | SkString fGSHeader; // layout qualifiers specific to GS |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 203 | VarArray fFSOutputs; |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 204 | SkString fVSCode; |
| 205 | SkString fGSCode; |
| 206 | SkString fFSCode; |
| 207 | bool fUsesGS; |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 208 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 209 | private: |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 210 | enum { |
| 211 | kNonStageIdx = -1, |
| 212 | }; |
| 213 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 214 | const GrGLContextInfo& fContext; |
| 215 | GrGLUniformManager& fUniformManager; |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 216 | int fCurrentStage; |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 217 | SkString fFSFunctions; |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 218 | |
| 219 | /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode(). |
| 220 | //@{ |
| 221 | GrSLType fTexCoordVaryingType; // the type, either Vec2f or Vec3f, of the coords passed |
| 222 | // as a varying from the VS to the FS. |
| 223 | SkString fDefaultTexCoordsName; // the name of the default 2D coords value. |
| 224 | //@} |
| 225 | |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | #endif |