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