| 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" |
| bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 12 | #include "GrBackendEffectFactory.h" |
| bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 13 | #include "GrColor.h" |
| bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 14 | #include "GrEffect.h" |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 15 | #include "gl/GrGLSL.h" |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 16 | #include "gl/GrGLUniformManager.h" |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 17 | |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 18 | #include <stdarg.h> |
| 19 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 20 | class GrGLContextInfo; |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 21 | class GrEffectStage; |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 22 | class GrGLProgramDesc; |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 23 | |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 24 | /** |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 25 | Contains all the incremental state of a shader as it is being built,as well as helpers to |
| 26 | manipulate that state. |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 27 | */ |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 28 | class GrGLShaderBuilder { |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 29 | public: |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 30 | /** |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 31 | * Passed to GrGLEffects to add texture reads to their shader code. |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 32 | */ |
| 33 | class TextureSampler { |
| 34 | public: |
| 35 | TextureSampler() |
| bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 36 | : fConfigComponentMask(0) |
| 37 | , fSamplerUniform(GrGLUniformManager::kInvalidUniformHandle) { |
| 38 | // we will memcpy the first 4 bytes from passed in swizzle. This ensures the string is |
| 39 | // terminated. |
| 40 | fSwizzle[4] = '\0'; |
| 41 | } |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 42 | |
| 43 | TextureSampler(const TextureSampler& other) { *this = other; } |
| 44 | |
| 45 | TextureSampler& operator= (const TextureSampler& other) { |
| bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 46 | GrAssert(0 == fConfigComponentMask); |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 47 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUniform); |
| 48 | |
| bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 49 | fConfigComponentMask = other.fConfigComponentMask; |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 50 | fSamplerUniform = other.fSamplerUniform; |
| 51 | return *this; |
| 52 | } |
| 53 | |
| bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 54 | // bitfield of GrColorComponentFlags present in the texture's config. |
| 55 | uint32_t configComponentMask() const { return fConfigComponentMask; } |
| 56 | |
| 57 | const char* swizzle() const { return fSwizzle; } |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 58 | |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 59 | bool isInitialized() const { return 0 != fConfigComponentMask; } |
| 60 | |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 61 | private: |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 62 | // The idx param is used to ensure multiple samplers within a single effect have unique |
| bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 63 | // uniform names. swizzle is a four char max string made up of chars 'r', 'g', 'b', and 'a'. |
| 64 | void init(GrGLShaderBuilder* builder, |
| 65 | uint32_t configComponentMask, |
| 66 | const char* swizzle, |
| 67 | int idx) { |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 68 | GrAssert(!this->isInitialized()); |
| 69 | GrAssert(0 != configComponentMask); |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 70 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUniform); |
| 71 | |
| 72 | GrAssert(NULL != builder); |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 73 | SkString name; |
| 74 | name.printf("Sampler%d_", idx); |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 75 | fSamplerUniform = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 76 | kSampler2D_GrSLType, |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 77 | name.c_str()); |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 78 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != fSamplerUniform); |
| 79 | |
| bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 80 | fConfigComponentMask = configComponentMask; |
| 81 | memcpy(fSwizzle, swizzle, 4); |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 84 | void init(GrGLShaderBuilder* builder, const GrTextureAccess* access, int idx) { |
| 85 | GrAssert(NULL != access); |
| 86 | this->init(builder, |
| 87 | GrPixelConfigComponentMask(access->getTexture()->config()), |
| 88 | access->getSwizzle(), |
| 89 | idx); |
| 90 | } |
| 91 | |
| 92 | uint32_t fConfigComponentMask; |
| 93 | char fSwizzle[5]; |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 94 | GrGLUniformManager::UniformHandle fSamplerUniform; |
| 95 | |
| bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 96 | friend class GrGLShaderBuilder; // to call init(). |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | typedef SkTArray<TextureSampler> TextureSamplerArray; |
| 100 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 101 | enum ShaderType { |
| 102 | kVertex_ShaderType = 0x1, |
| 103 | kGeometry_ShaderType = 0x2, |
| 104 | kFragment_ShaderType = 0x4, |
| 105 | }; |
| 106 | |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 107 | GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&, const GrGLProgramDesc&); |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 108 | |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 109 | /** |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 110 | * Use of these features may require a GLSL extension to be enabled. Shaders may not compile |
| 111 | * if code is added that uses one of these features without calling enableFeature() |
| 112 | */ |
| 113 | enum GLSLFeature { |
| 114 | kStandardDerivatives_GLSLFeature = 0, |
| 115 | |
| 116 | kLastGLSLFeature = kStandardDerivatives_GLSLFeature |
| 117 | }; |
| 118 | |
| 119 | /** |
| 120 | * If the feature is supported then true is returned and any necessary #extension declarations |
| 121 | * are added to the shaders. If the feature is not supported then false will be returned. |
| 122 | */ |
| 123 | bool enableFeature(GLSLFeature); |
| 124 | |
| 125 | /** |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 126 | * Called by GrGLEffects to add code to one of the shaders. |
| 127 | */ |
| 128 | void vsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
| 129 | va_list args; |
| 130 | va_start(args, format); |
| 131 | this->codeAppendf(kVertex_ShaderType, format, args); |
| 132 | va_end(args); |
| 133 | } |
| 134 | |
| 135 | void gsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
| 136 | va_list args; |
| 137 | va_start(args, format); |
| 138 | this->codeAppendf(kGeometry_ShaderType, format, args); |
| 139 | va_end(args); |
| 140 | } |
| 141 | |
| 142 | void fsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
| 143 | va_list args; |
| 144 | va_start(args, format); |
| 145 | this->codeAppendf(kFragment_ShaderType, format, args); |
| 146 | va_end(args); |
| 147 | } |
| 148 | |
| 149 | void vsCodeAppend(const char* str) { this->codeAppend(kVertex_ShaderType, str); } |
| 150 | void gsCodeAppend(const char* str) { this->codeAppend(kGeometry_ShaderType, str); } |
| 151 | void fsCodeAppend(const char* str) { this->codeAppend(kFragment_ShaderType, str); } |
| 152 | |
| bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 153 | /** Appends a 2D texture sample with projection if necessary. coordType must either be Vec2f or |
| 154 | Vec3f. The latter is interpreted as projective texture coords. The vec length and swizzle |
| 155 | order of the result depends on the GrTextureAccess associated with the TextureSampler. */ |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 156 | void appendTextureLookup(SkString* out, |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 157 | const TextureSampler&, |
| bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 158 | const char* coordName, |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 159 | GrSLType coordType = kVec2f_GrSLType) const; |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 160 | |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 161 | /** Version of above that appends the result to the shader code rather than an SkString. |
| 162 | Currently the shader type must be kFragment */ |
| 163 | void appendTextureLookup(ShaderType, |
| 164 | const TextureSampler&, |
| 165 | const char* coordName, |
| 166 | GrSLType coordType = kVec2f_GrSLType); |
| 167 | |
| 168 | |
| bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 169 | /** Does the work of appendTextureLookup and modulates the result by modulation. The result is |
| 170 | always a vec4. modulation and the swizzle specified by TextureSampler must both be vec4 or |
| 171 | float. If modulation is "" or NULL it this function acts as though appendTextureLookup were |
| 172 | called. */ |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 173 | void appendTextureLookupAndModulate(ShaderType, |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 174 | const char* modulation, |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 175 | const TextureSampler&, |
| bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 176 | const char* coordName, |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 177 | GrSLType coordType = kVec2f_GrSLType); |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 178 | |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 179 | /** Emits a helper function outside of main(). Currently ShaderType must be |
| 180 | kFragment_ShaderType. */ |
| 181 | void emitFunction(ShaderType shader, |
| 182 | GrSLType returnType, |
| 183 | const char* name, |
| 184 | int argCnt, |
| 185 | const GrGLShaderVar* args, |
| 186 | const char* body, |
| 187 | SkString* outName); |
| 188 | |
| bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 189 | /** Generates a EffectKey for the shader code based on the texture access parameters and the |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 190 | capabilities of the GL context. This is useful for keying the shader programs that may |
| 191 | have multiple representations, based on the type/format of textures used. */ |
| bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 192 | static GrBackendEffectFactory::EffectKey KeyForTextureAccess(const GrTextureAccess&, |
| 193 | const GrGLCaps&); |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 194 | |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 195 | typedef uint8_t DstReadKey; |
| 196 | |
| 197 | /** Returns a key for adding code to read the copy-of-dst color in service of effects that |
| 198 | require reading the dst. It must not return 0 because 0 indicates that there is no dst |
| 199 | copy read at all. */ |
| 200 | static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); |
| 201 | |
| bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 202 | /** If texture swizzling is available using tex parameters then it is preferred over mangling |
| 203 | the generated shader code. This potentially allows greater reuse of cached shaders. */ |
| 204 | static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps); |
| 205 | |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 206 | /** Add a uniform variable to the current program, that has visibility in one or more shaders. |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 207 | visibility is a bitfield of ShaderType values indicating from which shaders the uniform |
| 208 | should be accessible. At least one bit must be set. Geometry shader uniforms are not |
| 209 | supported at this time. The actual uniform name will be mangled. If outName is not NULL then |
| 210 | it will refer to the final uniform name after return. Use the addUniformArray variant to add |
| 211 | an array of uniforms. |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 212 | */ |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 213 | GrGLUniformManager::UniformHandle addUniform(uint32_t visibility, |
| 214 | GrSLType type, |
| 215 | const char* name, |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 216 | const char** outName = NULL) { |
| 217 | return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName); |
| 218 | } |
| 219 | GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility, |
| 220 | GrSLType type, |
| 221 | const char* name, |
| 222 | int arrayCount, |
| 223 | const char** outName = NULL); |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 224 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 225 | const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle) const; |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 226 | |
| 227 | /** |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 228 | * Shortcut for getUniformVariable(u).c_str() |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 229 | */ |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 230 | const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const { |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 231 | return this->getUniformVariable(u).c_str(); |
| 232 | } |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 233 | |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 234 | /** Add a vertex attribute to the current program that is passed in from the vertex data. |
| 235 | Returns false if the attribute was already there, true otherwise. */ |
| 236 | bool addAttribute(GrSLType type, const char* name); |
| 237 | |
| 238 | /** Add a varying variable to the current program to pass values between vertex and fragment |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 239 | shaders. If the last two parameters are non-NULL, they are filled in with the name |
| 240 | generated. */ |
| tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 241 | void addVarying(GrSLType type, |
| 242 | const char* name, |
| 243 | const char** vsOutName = NULL, |
| 244 | const char** fsInName = NULL); |
| 245 | |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 246 | /** Returns a variable name that represents the position of the fragment in the FS. The position |
| 247 | is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */ |
| 248 | const char* fragmentPosition(); |
| 249 | |
| bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 250 | /** Returns a vertex attribute that represents the vertex position in the VS. This is the |
| 251 | pre-matrix position and is commonly used by effects to compute texture coords via a matrix. |
| 252 | */ |
| 253 | const GrGLShaderVar& positionAttribute() const { return *fPositionVar; } |
| 254 | |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 255 | /** Returns a vertex attribute that represents the local coords in the VS. This may be the same |
| 256 | as positionAttribute() or it may not be. It depends upon whether the rendering code |
| 257 | specified explicit local coords or not in the GrDrawState. */ |
| 258 | const GrGLShaderVar& localCoordsAttribute() const { return *fLocalCoordsVar; } |
| 259 | |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 260 | /** Returns the color of the destination pixel. This may be NULL if no effect advertised |
| 261 | that it will read the destination. */ |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame^] | 262 | const char* dstColor(); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 263 | |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 264 | /** |
| 265 | * Are explicit local coordinates provided as input to the vertex shader. |
| 266 | */ |
| 267 | bool hasExplicitLocalCoords() const { return (fLocalCoordsVar != fPositionVar); } |
| 268 | |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 269 | /** |
| 270 | * Interfaces used by GrGLProgram. |
| 271 | * TODO: Hide these from the GrEffects using friend or splitting this into two related classes. |
| 272 | * Also, GrGLProgram's shader string construction should be moved to this class. |
| 273 | */ |
| 274 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 275 | /** Called after building is complete to get the final shader string. */ |
| 276 | void getShader(ShaderType, SkString*) const; |
| 277 | |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 278 | void setCurrentStage(int stageIdx) { fCurrentStageIdx = stageIdx; } |
| 279 | void setNonStage() { fCurrentStageIdx = kNonStageIdx; } |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 280 | // TODO: move remainder of shader code generation to this class and call this privately |
| 281 | // Handles of sampler uniforms generated for the effect are appended to samplerHandles. |
| 282 | GrGLEffect* createAndEmitGLEffect( |
| 283 | const GrEffectStage& stage, |
| 284 | GrBackendEffectFactory::EffectKey key, |
| 285 | const char* fsInColor, // NULL means no incoming color |
| 286 | const char* fsOutColor, |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 287 | SkTArray<GrGLUniformManager::UniformHandle, true>* samplerHandles); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 288 | |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 289 | GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHeightUniform; } |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 290 | GrGLUniformManager::UniformHandle getDstCopyTopLeftUniform() const { |
| 291 | return fDstCopyTopLeftUniform; |
| 292 | } |
| 293 | GrGLUniformManager::UniformHandle getDstCopyScaleUniform() const { |
| 294 | return fDstCopyScaleUniform; |
| 295 | } |
| 296 | GrGLUniformManager::UniformHandle getDstCopySamplerUniform() const { |
| 297 | return fDstCopySampler.fSamplerUniform; |
| 298 | } |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 299 | |
| 300 | struct AttributePair { |
| 301 | void set(int index, const SkString& name) { |
| 302 | fIndex = index; fName = name; |
| 303 | } |
| 304 | int fIndex; |
| 305 | SkString fName; |
| 306 | }; |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 307 | const SkTArray<AttributePair, true>& getEffectAttributes() const { |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 308 | return fEffectAttributes; |
| 309 | } |
| 310 | const SkString* getEffectAttributeName(int attributeIndex) const; |
| 311 | |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 312 | // TODO: Make this do all the compiling, linking, etc. |
| 313 | void finished(GrGLuint programID); |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 314 | |
| robertphillips@google.com | 13f181f | 2013-03-02 12:02:08 +0000 | [diff] [blame] | 315 | const GrGLContextInfo& ctxInfo() const { return fCtxInfo; } |
| 316 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 317 | private: |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 318 | void codeAppendf(ShaderType type, const char format[], va_list args); |
| 319 | void codeAppend(ShaderType type, const char* str); |
| 320 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 321 | typedef GrTAllocator<GrGLShaderVar> VarArray; |
| 322 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 323 | void appendDecls(const VarArray&, SkString*) const; |
| 324 | void appendUniformDecls(ShaderType, SkString*) const; |
| 325 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 326 | typedef GrGLUniformManager::BuilderUniform BuilderUniform; |
| 327 | GrGLUniformManager::BuilderUniformArray fUniforms; |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 328 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 329 | // TODO: Everything below here private. |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 330 | public: |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 331 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 332 | VarArray fVSAttrs; |
| 333 | VarArray fVSOutputs; |
| 334 | VarArray fGSInputs; |
| 335 | VarArray fGSOutputs; |
| 336 | VarArray fFSInputs; |
| 337 | SkString fGSHeader; // layout qualifiers specific to GS |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 338 | VarArray fFSOutputs; |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 339 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 340 | private: |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 341 | enum { |
| 342 | kNonStageIdx = -1, |
| 343 | }; |
| 344 | |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 345 | /** |
| 346 | * Features that should only be enabled by GrGLShaderBuilder itself. |
| 347 | */ |
| 348 | enum GLSLPrivateFeature { |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame^] | 349 | kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
| 350 | kEXTShaderFramebufferFetch_GLSLPrivateFeature, |
| 351 | kNVShaderFramebufferFetch_GLSLPrivateFeature, |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 352 | }; |
| 353 | bool enablePrivateFeature(GLSLPrivateFeature); |
| 354 | |
| 355 | // If we ever have VS/GS features we can expand this to take a bitmask of ShaderType and track |
| 356 | // the enables separately for each shader. |
| 357 | void addFSFeature(uint32_t featureBit, const char* extensionName); |
| 358 | |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 359 | // Interpretation of DstReadKey when generating code |
| 360 | enum { |
| 361 | kNoDstRead_DstReadKey = 0, |
| 362 | kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
| 363 | kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only. |
| 364 | kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left. |
| 365 | }; |
| 366 | |
| robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 367 | const GrGLContextInfo& fCtxInfo; |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 368 | GrGLUniformManager& fUniformManager; |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 369 | int fCurrentStageIdx; |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 370 | uint32_t fFSFeaturesAddedMask; |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 371 | SkString fFSFunctions; |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 372 | SkString fFSExtensions; |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 373 | |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 374 | bool fUsesGS; |
| 375 | |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 376 | SkString fFSCode; |
| 377 | SkString fVSCode; |
| 378 | SkString fGSCode; |
| 379 | |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 380 | bool fSetupFragPosition; |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 381 | TextureSampler fDstCopySampler; |
| 382 | |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 383 | GrGLUniformManager::UniformHandle fRTHeightUniform; |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 384 | GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform; |
| 385 | GrGLUniformManager::UniformHandle fDstCopyScaleUniform; |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 386 | |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 387 | SkSTArray<10, AttributePair, true> fEffectAttributes; |
| 388 | |
| bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 389 | GrGLShaderVar* fPositionVar; |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 390 | GrGLShaderVar* fLocalCoordsVar; |
| 391 | |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 392 | }; |
| 393 | |
| 394 | #endif |