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