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