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 | #include "gl/GrGLShaderBuilder.h" |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 9 | #include "gl/GrGLProgram.h" |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 10 | #include "gl/GrGLUniformHandle.h" |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 11 | #include "GrDrawEffect.h" |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 12 | #include "GrTexture.h" |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 13 | |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 14 | // number of each input/output type in a single allocation block |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 15 | static const int kVarsPerBlock = 8; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 16 | |
| 17 | // except FS outputs where we expect 2 at most. |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 18 | static const int kMaxFSOutputs = 2; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 20 | // ES2 FS only guarantees mediump and lowp support |
| 21 | static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar::kMedium_Precision; |
| 22 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 23 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 26 | namespace { |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 27 | |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 28 | inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen) { |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 29 | if (kVec2f_GrSLType == type) { |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 30 | return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D"; |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 31 | } else { |
| 32 | GrAssert(kVec3f_GrSLType == type); |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 33 | return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj"; |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 34 | } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 35 | } |
| 36 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 37 | /** |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 38 | * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are |
| 39 | * present in the texture's config. swizzleComponentMask indicates the channels present in the |
| 40 | * shader swizzle. |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 41 | */ |
| 42 | inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 43 | uint32_t configComponentMask, |
| 44 | uint32_t swizzleComponentMask) { |
| 45 | if (caps.textureSwizzleSupport()) { |
| 46 | // Any remapping is handled using texture swizzling not shader modifications. |
| 47 | return false; |
| 48 | } |
| 49 | // check if the texture is alpha-only |
| 50 | if (kA_GrColorComponentFlag == configComponentMask) { |
| 51 | if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) { |
| 52 | // we must map the swizzle 'a's to 'r'. |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 53 | return true; |
| 54 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 55 | if (kRGB_GrColorComponentFlags & swizzleComponentMask) { |
| 56 | // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that |
| 57 | // alpha-only textures smear alpha across all four channels when read. |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 58 | return true; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 61 | return false; |
| 62 | } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 63 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 64 | void append_swizzle(SkString* outAppend, |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 65 | const GrGLShaderBuilder::TextureSampler& texSampler, |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 66 | const GrGLCaps& caps) { |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 67 | const char* swizzle = texSampler.swizzle(); |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 68 | char mangledSwizzle[5]; |
| 69 | |
| 70 | // The swizzling occurs using texture params instead of shader-mangling if ARB_texture_swizzle |
| 71 | // is available. |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 72 | if (!caps.textureSwizzleSupport() && |
| 73 | (kA_GrColorComponentFlag == texSampler.configComponentMask())) { |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 74 | char alphaChar = caps.textureRedSupport() ? 'r' : 'a'; |
| 75 | int i; |
| 76 | for (i = 0; '\0' != swizzle[i]; ++i) { |
| 77 | mangledSwizzle[i] = alphaChar; |
| 78 | } |
| 79 | mangledSwizzle[i] ='\0'; |
| 80 | swizzle = mangledSwizzle; |
| 81 | } |
bsalomon@google.com | 73d5b2f | 2012-10-04 13:26:32 +0000 | [diff] [blame] | 82 | // For shader prettiness we omit the swizzle rather than appending ".rgba". |
| 83 | if (memcmp(swizzle, "rgba", 4)) { |
| 84 | outAppend->appendf(".%s", swizzle); |
| 85 | } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 86 | } |
| 87 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 88 | } |
| 89 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 90 | static const char kDstColorName[] = "_dstColor"; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 92 | /////////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 93 | |
skia.committer@gmail.com | 631cdcb | 2013-03-01 12:12:55 +0000 | [diff] [blame] | 94 | GrGLShaderBuilder::GrGLShaderBuilder(const GrGLContextInfo& ctxInfo, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 95 | GrGLUniformManager& uniformManager, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 96 | const GrGLProgramDesc& desc) |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 97 | : fUniforms(kVarsPerBlock) |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 98 | , fVSAttrs(kVarsPerBlock) |
| 99 | , fVSOutputs(kVarsPerBlock) |
| 100 | , fGSInputs(kVarsPerBlock) |
| 101 | , fGSOutputs(kVarsPerBlock) |
| 102 | , fFSInputs(kVarsPerBlock) |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 103 | , fFSOutputs(kMaxFSOutputs) |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 104 | , fCtxInfo(ctxInfo) |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 105 | , fUniformManager(uniformManager) |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 106 | , fCurrentStageIdx(kNonStageIdx) |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 107 | #if GR_GL_EXPERIMENTAL_GS |
| 108 | , fUsesGS(desc.fExperimentalGS) |
| 109 | #else |
| 110 | , fUsesGS(false) |
| 111 | #endif |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 112 | , fSetupFragPosition(false) |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 113 | , fRTHeightUniform(GrGLUniformManager::kInvalidUniformHandle) |
| 114 | , fDstCopyTopLeftUniform (GrGLUniformManager::kInvalidUniformHandle) |
| 115 | , fDstCopyScaleUniform (GrGLUniformManager::kInvalidUniformHandle) { |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 116 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 117 | fPositionVar = &fVSAttrs.push_back(); |
| 118 | fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "aPosition"); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 119 | if (-1 != desc.fLocalCoordAttributeIndex) { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 120 | fLocalCoordsVar = &fVSAttrs.push_back(); |
| 121 | fLocalCoordsVar->set(kVec2f_GrSLType, |
| 122 | GrGLShaderVar::kAttribute_TypeModifier, |
| 123 | "aLocalCoords"); |
| 124 | } else { |
| 125 | fLocalCoordsVar = fPositionVar; |
| 126 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 127 | if (kNoDstRead_DstReadKey != desc.fDstRead) { |
| 128 | bool topDown = SkToBool(kTopLeftOrigin_DstReadKeyBit & desc.fDstRead); |
| 129 | const char* dstCopyTopLeftName; |
| 130 | const char* dstCopyCoordScaleName; |
| 131 | uint32_t configMask; |
| 132 | if (SkToBool(kUseAlphaConfig_DstReadKeyBit & desc.fDstRead)) { |
| 133 | configMask = kA_GrColorComponentFlag; |
| 134 | } else { |
| 135 | configMask = kRGBA_GrColorComponentFlags; |
| 136 | } |
| 137 | fDstCopySampler.init(this, configMask, "rgba", 0); |
| 138 | |
| 139 | fDstCopyTopLeftUniform = this->addUniform(kFragment_ShaderType, |
| 140 | kVec2f_GrSLType, |
| 141 | "DstCopyUpperLeft", |
| 142 | &dstCopyTopLeftName); |
| 143 | fDstCopyScaleUniform = this->addUniform(kFragment_ShaderType, |
| 144 | kVec2f_GrSLType, |
| 145 | "DstCopyCoordScale", |
| 146 | &dstCopyCoordScaleName); |
| 147 | const char* fragPos = this->fragmentPosition(); |
| 148 | this->fsCodeAppend("\t// Read color from copy of the destination.\n"); |
| 149 | this->fsCodeAppendf("\tvec2 _dstTexCoord = (%s.xy - %s) * %s;\n", |
| 150 | fragPos, dstCopyTopLeftName, dstCopyCoordScaleName); |
| 151 | if (!topDown) { |
| 152 | this->fsCodeAppend("\t_dstTexCoord.y = 1.0 - _dstTexCoord.y;\n"); |
| 153 | } |
| 154 | this->fsCodeAppendf("\tvec4 %s = ", kDstColorName); |
| 155 | this->appendTextureLookup(kFragment_ShaderType, fDstCopySampler, "_dstTexCoord"); |
| 156 | this->fsCodeAppend(";\n\n"); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | const char* GrGLShaderBuilder::dstColor() const { |
| 161 | if (fDstCopySampler.isInitialized()) { |
| 162 | return kDstColorName; |
| 163 | } else { |
| 164 | return NULL; |
| 165 | } |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 166 | } |
| 167 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 168 | void GrGLShaderBuilder::codeAppendf(ShaderType type, const char format[], va_list args) { |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame] | 169 | SkString* string = NULL; |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 170 | switch (type) { |
| 171 | case kVertex_ShaderType: |
| 172 | string = &fVSCode; |
| 173 | break; |
| 174 | case kGeometry_ShaderType: |
| 175 | string = &fGSCode; |
| 176 | break; |
| 177 | case kFragment_ShaderType: |
| 178 | string = &fFSCode; |
| 179 | break; |
| 180 | default: |
| 181 | GrCrash("Invalid shader type"); |
| 182 | } |
| 183 | string->appendf(format, args); |
| 184 | } |
| 185 | |
| 186 | void GrGLShaderBuilder::codeAppend(ShaderType type, const char* str) { |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame] | 187 | SkString* string = NULL; |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 188 | switch (type) { |
| 189 | case kVertex_ShaderType: |
| 190 | string = &fVSCode; |
| 191 | break; |
| 192 | case kGeometry_ShaderType: |
| 193 | string = &fGSCode; |
| 194 | break; |
| 195 | case kFragment_ShaderType: |
| 196 | string = &fFSCode; |
| 197 | break; |
| 198 | default: |
| 199 | GrCrash("Invalid shader type"); |
| 200 | } |
| 201 | string->append(str); |
| 202 | } |
| 203 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 204 | void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 205 | const GrGLShaderBuilder::TextureSampler& sampler, |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 206 | const char* coordName, |
| 207 | GrSLType varyingType) const { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 208 | GrAssert(NULL != coordName); |
bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 209 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 210 | out->appendf("%s(%s, %s)", |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 211 | sample_function_name(varyingType, fCtxInfo.glslGeneration()), |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 212 | this->getUniformCStr(sampler.fSamplerUniform), |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 213 | coordName); |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 214 | append_swizzle(out, sampler, *fCtxInfo.caps()); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 215 | } |
| 216 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 217 | void GrGLShaderBuilder::appendTextureLookup(ShaderType type, |
| 218 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 219 | const char* coordName, |
| 220 | GrSLType varyingType) { |
| 221 | GrAssert(kFragment_ShaderType == type); |
| 222 | this->appendTextureLookup(&fFSCode, sampler, coordName, varyingType); |
| 223 | } |
| 224 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 225 | void GrGLShaderBuilder::appendTextureLookupAndModulate( |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 226 | ShaderType type, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 227 | const char* modulation, |
| 228 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 229 | const char* coordName, |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 230 | GrSLType varyingType) { |
| 231 | GrAssert(kFragment_ShaderType == type); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 232 | SkString lookup; |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 233 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 234 | GrGLSLModulate4f(&fFSCode, modulation, lookup.c_str()); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 235 | } |
| 236 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 237 | GrBackendEffectFactory::EffectKey GrGLShaderBuilder::KeyForTextureAccess( |
| 238 | const GrTextureAccess& access, |
| 239 | const GrGLCaps& caps) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 240 | uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config()); |
| 241 | if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) { |
| 242 | return 1; |
| 243 | } else { |
| 244 | return 0; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 245 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | GrGLShaderBuilder::DstReadKey GrGLShaderBuilder::KeyForDstRead(const GrTexture* dstCopy, |
| 249 | const GrGLCaps& caps) { |
| 250 | uint32_t key = kYesDstRead_DstReadKeyBit; |
| 251 | if (!caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(dstCopy->config())) { |
| 252 | // The fact that the config is alpha-only must be considered when generating code. |
| 253 | key |= kUseAlphaConfig_DstReadKeyBit; |
| 254 | } |
| 255 | if (kTopLeft_GrSurfaceOrigin == dstCopy->origin()) { |
| 256 | key |= kTopLeftOrigin_DstReadKeyBit; |
| 257 | } |
| 258 | GrAssert(static_cast<DstReadKey>(key) == key); |
| 259 | return static_cast<DstReadKey>(key); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 260 | } |
| 261 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 262 | const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps) { |
| 263 | if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 264 | if (caps.textureRedSupport()) { |
| 265 | static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RED, GR_GL_RED }; |
| 266 | return gRedSmear; |
| 267 | } else { |
| 268 | static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 269 | GR_GL_ALPHA, GR_GL_ALPHA }; |
| 270 | return gAlphaSmear; |
| 271 | } |
| 272 | } else { |
| 273 | static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA }; |
| 274 | return gStraight; |
| 275 | } |
| 276 | } |
| 277 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 278 | GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniformArray(uint32_t visibility, |
| 279 | GrSLType type, |
| 280 | const char* name, |
| 281 | int count, |
| 282 | const char** outName) { |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 283 | GrAssert(name && strlen(name)); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 284 | SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_ShaderType | kFragment_ShaderType); |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 285 | GrAssert(0 == (~kVisibilityMask & visibility)); |
| 286 | GrAssert(0 != visibility); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 287 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 288 | BuilderUniform& uni = fUniforms.push_back(); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 289 | UniformHandle h = index_to_handle(fUniforms.count() - 1); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 290 | GR_DEBUGCODE(UniformHandle h2 =) |
| 291 | fUniformManager.appendUniform(type, count); |
| 292 | // We expect the uniform manager to initially have no uniforms and that all uniforms are added |
| 293 | // by this function. Therefore, the handles should match. |
| 294 | GrAssert(h2 == h); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 295 | uni.fVariable.setType(type); |
| 296 | uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 297 | SkString* uniName = uni.fVariable.accessName(); |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 298 | if (kNonStageIdx == fCurrentStageIdx) { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 299 | uniName->printf("u%s", name); |
| 300 | } else { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 301 | uniName->printf("u%s%d", name, fCurrentStageIdx); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 302 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 303 | uni.fVariable.setArrayCount(count); |
| 304 | uni.fVisibility = visibility; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 305 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 306 | // If it is visible in both the VS and FS, the precision must match. |
| 307 | // We declare a default FS precision, but not a default VS. So set the var |
| 308 | // to use the default FS precision. |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 309 | if ((kVertex_ShaderType | kFragment_ShaderType) == visibility) { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 310 | // the fragment and vertex precisions must match |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 311 | uni.fVariable.setPrecision(kDefaultFragmentPrecision); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 312 | } |
| 313 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 314 | if (NULL != outName) { |
| 315 | *outName = uni.fVariable.c_str(); |
| 316 | } |
| 317 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 318 | return h; |
| 319 | } |
| 320 | |
| 321 | const GrGLShaderVar& GrGLShaderBuilder::getUniformVariable(UniformHandle u) const { |
| 322 | return fUniforms[handle_to_index(u)].fVariable; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 323 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 324 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 325 | bool GrGLShaderBuilder::addAttribute(GrSLType type, |
| 326 | const char* name) { |
| 327 | for (int i = 0; i < fVSAttrs.count(); ++i) { |
| 328 | const GrGLShaderVar& attr = fVSAttrs[i]; |
| 329 | // if attribute already added, don't add it again |
| 330 | if (attr.getName().equals(name)) { |
| 331 | GrAssert(attr.getType() == type); |
| 332 | return false; |
| 333 | } |
| 334 | } |
| 335 | fVSAttrs.push_back().set(type, |
| 336 | GrGLShaderVar::kAttribute_TypeModifier, |
| 337 | name); |
| 338 | return true; |
| 339 | } |
| 340 | |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 341 | void GrGLShaderBuilder::addVarying(GrSLType type, |
| 342 | const char* name, |
| 343 | const char** vsOutName, |
| 344 | const char** fsInName) { |
| 345 | fVSOutputs.push_back(); |
| 346 | fVSOutputs.back().setType(type); |
| 347 | fVSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier); |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 348 | if (kNonStageIdx == fCurrentStageIdx) { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 349 | fVSOutputs.back().accessName()->printf("v%s", name); |
| 350 | } else { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 351 | fVSOutputs.back().accessName()->printf("v%s%d", name, fCurrentStageIdx); |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 352 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 353 | if (vsOutName) { |
| 354 | *vsOutName = fVSOutputs.back().getName().c_str(); |
| 355 | } |
| 356 | // input to FS comes either from VS or GS |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 357 | const SkString* fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 358 | if (fUsesGS) { |
| 359 | // if we have a GS take each varying in as an array |
| 360 | // and output as non-array. |
| 361 | fGSInputs.push_back(); |
| 362 | fGSInputs.back().setType(type); |
| 363 | fGSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier); |
| 364 | fGSInputs.back().setUnsizedArray(); |
| 365 | *fGSInputs.back().accessName() = fVSOutputs.back().getName(); |
| 366 | fGSOutputs.push_back(); |
| 367 | fGSOutputs.back().setType(type); |
| 368 | fGSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier); |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 369 | if (kNonStageIdx == fCurrentStageIdx) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 370 | fGSOutputs.back().accessName()->printf("g%s", name); |
| 371 | } else { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 372 | fGSOutputs.back().accessName()->printf("g%s%d", name, fCurrentStageIdx); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 373 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 374 | fsName = fGSOutputs.back().accessName(); |
| 375 | } else { |
| 376 | fsName = fVSOutputs.back().accessName(); |
| 377 | } |
| 378 | fFSInputs.push_back(); |
| 379 | fFSInputs.back().setType(type); |
| 380 | fFSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier); |
| 381 | fFSInputs.back().setName(*fsName); |
| 382 | if (fsInName) { |
| 383 | *fsInName = fsName->c_str(); |
| 384 | } |
| 385 | } |
| 386 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 387 | const char* GrGLShaderBuilder::fragmentPosition() { |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 388 | #if 1 |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 389 | if (fCtxInfo.caps()->fragCoordConventionsSupport()) { |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 390 | if (!fSetupFragPosition) { |
robertphillips@google.com | 9705596 | 2013-04-01 22:20:59 +0000 | [diff] [blame] | 391 | if (fCtxInfo.glslGeneration() < k150_GrGLSLGeneration) { |
| 392 | fFSHeader.append("#extension GL_ARB_fragment_coord_conventions: require\n"); |
| 393 | } |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 394 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 395 | GrGLShaderVar::kIn_TypeModifier, |
| 396 | "gl_FragCoord", |
| 397 | GrGLShaderVar::kDefault_Precision, |
| 398 | GrGLShaderVar::kUpperLeft_Origin); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 399 | fSetupFragPosition = true; |
| 400 | } |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 401 | return "gl_FragCoord"; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 402 | } else { |
| 403 | static const char* kCoordName = "fragCoordYDown"; |
| 404 | if (!fSetupFragPosition) { |
| 405 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == fRTHeightUniform); |
| 406 | const char* rtHeightName; |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 407 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 408 | // temporarily change the stage index because we're inserting a uniform whose name |
| 409 | // shouldn't be mangled to be stage-specific. |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 410 | int oldStageIdx = fCurrentStageIdx; |
| 411 | fCurrentStageIdx = kNonStageIdx; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 412 | fRTHeightUniform = this->addUniform(kFragment_ShaderType, |
| 413 | kFloat_GrSLType, |
| 414 | "RTHeight", |
| 415 | &rtHeightName); |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 416 | fCurrentStageIdx = oldStageIdx; |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 417 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 418 | this->fFSCode.prependf("\tvec4 %s = vec4(gl_FragCoord.x, %s - gl_FragCoord.y, gl_FragCoord.zw);\n", |
| 419 | kCoordName, rtHeightName); |
| 420 | fSetupFragPosition = true; |
| 421 | } |
| 422 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != fRTHeightUniform); |
| 423 | return kCoordName; |
| 424 | } |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 425 | #else |
| 426 | // This is the path we'll need to use once we have support for TopLeft |
| 427 | // render targets. |
| 428 | if (!fSetupFragPosition) { |
| 429 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 430 | GrGLShaderVar::kIn_TypeModifier, |
| 431 | "gl_FragCoord", |
| 432 | GrGLShaderVar::kDefault_Precision); |
| 433 | fSetupFragPosition = true; |
| 434 | } |
| 435 | return "gl_FragCoord"; |
| 436 | #endif |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 440 | void GrGLShaderBuilder::emitFunction(ShaderType shader, |
| 441 | GrSLType returnType, |
| 442 | const char* name, |
| 443 | int argCnt, |
| 444 | const GrGLShaderVar* args, |
| 445 | const char* body, |
| 446 | SkString* outName) { |
| 447 | GrAssert(kFragment_ShaderType == shader); |
| 448 | fFSFunctions.append(GrGLShaderVar::TypeString(returnType)); |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 449 | if (kNonStageIdx != fCurrentStageIdx) { |
| 450 | outName->printf(" %s_%d", name, fCurrentStageIdx); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 451 | } else { |
| 452 | *outName = name; |
| 453 | } |
| 454 | fFSFunctions.append(*outName); |
| 455 | fFSFunctions.append("("); |
| 456 | for (int i = 0; i < argCnt; ++i) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 457 | args[i].appendDecl(fCtxInfo, &fFSFunctions); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 458 | if (i < argCnt - 1) { |
| 459 | fFSFunctions.append(", "); |
| 460 | } |
| 461 | } |
| 462 | fFSFunctions.append(") {\n"); |
| 463 | fFSFunctions.append(body); |
| 464 | fFSFunctions.append("}\n\n"); |
| 465 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 466 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 467 | namespace { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 468 | |
| 469 | inline void append_default_precision_qualifier(GrGLShaderVar::Precision p, |
| 470 | GrGLBinding binding, |
| 471 | SkString* str) { |
| 472 | // Desktop GLSL has added precision qualifiers but they don't do anything. |
| 473 | if (kES2_GrGLBinding == binding) { |
| 474 | switch (p) { |
| 475 | case GrGLShaderVar::kHigh_Precision: |
| 476 | str->append("precision highp float;\n"); |
| 477 | break; |
| 478 | case GrGLShaderVar::kMedium_Precision: |
| 479 | str->append("precision mediump float;\n"); |
| 480 | break; |
| 481 | case GrGLShaderVar::kLow_Precision: |
| 482 | str->append("precision lowp float;\n"); |
| 483 | break; |
| 484 | case GrGLShaderVar::kDefault_Precision: |
| 485 | GrCrash("Default precision now allowed."); |
| 486 | default: |
| 487 | GrCrash("Unknown precision value."); |
| 488 | } |
| 489 | } |
| 490 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 491 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 492 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 493 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 494 | for (int i = 0; i < vars.count(); ++i) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 495 | vars[i].appendDecl(fCtxInfo, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 496 | out->append(";\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 499 | |
| 500 | void GrGLShaderBuilder::appendUniformDecls(ShaderType stype, SkString* out) const { |
| 501 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 502 | if (fUniforms[i].fVisibility & stype) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 503 | fUniforms[i].fVariable.appendDecl(fCtxInfo, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 504 | out->append(";\n"); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void GrGLShaderBuilder::getShader(ShaderType type, SkString* shaderStr) const { |
| 510 | switch (type) { |
| 511 | case kVertex_ShaderType: |
robertphillips@google.com | 9705596 | 2013-04-01 22:20:59 +0000 | [diff] [blame] | 512 | *shaderStr = fHeader; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 513 | this->appendUniformDecls(kVertex_ShaderType, shaderStr); |
| 514 | this->appendDecls(fVSAttrs, shaderStr); |
| 515 | this->appendDecls(fVSOutputs, shaderStr); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 516 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 517 | shaderStr->append(fVSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 518 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 519 | break; |
| 520 | case kGeometry_ShaderType: |
| 521 | if (fUsesGS) { |
robertphillips@google.com | 9705596 | 2013-04-01 22:20:59 +0000 | [diff] [blame] | 522 | *shaderStr = fHeader; |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 523 | shaderStr->append(fGSHeader); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 524 | this->appendDecls(fGSInputs, shaderStr); |
| 525 | this->appendDecls(fGSOutputs, shaderStr); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 526 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 527 | shaderStr->append(fGSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 528 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 529 | } else { |
| 530 | shaderStr->reset(); |
| 531 | } |
| 532 | break; |
| 533 | case kFragment_ShaderType: |
robertphillips@google.com | 9705596 | 2013-04-01 22:20:59 +0000 | [diff] [blame] | 534 | *shaderStr = fHeader; |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 535 | append_default_precision_qualifier(kDefaultFragmentPrecision, |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 536 | fCtxInfo.binding(), |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 537 | shaderStr); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 538 | shaderStr->append(fFSHeader); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 539 | this->appendUniformDecls(kFragment_ShaderType, shaderStr); |
| 540 | this->appendDecls(fFSInputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 541 | // We shouldn't have declared outputs on 1.10 |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 542 | GrAssert(k110_GrGLSLGeneration != fCtxInfo.glslGeneration() || fFSOutputs.empty()); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 543 | this->appendDecls(fFSOutputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 544 | shaderStr->append(fFSFunctions); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 545 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 546 | shaderStr->append(fFSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 547 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 548 | break; |
| 549 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 550 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 551 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 552 | void GrGLShaderBuilder::finished(GrGLuint programID) { |
| 553 | fUniformManager.getUniformLocations(programID, fUniforms); |
| 554 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 555 | |
| 556 | GrGLEffect* GrGLShaderBuilder::createAndEmitGLEffect( |
| 557 | const GrEffectStage& stage, |
| 558 | GrGLEffect::EffectKey key, |
| 559 | const char* fsInColor, |
| 560 | const char* fsOutColor, |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 561 | SkTArray<GrGLUniformManager::UniformHandle, true>* samplerHandles) { |
| 562 | GrAssert(NULL != stage.getEffect()); |
| 563 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 564 | const GrEffectRef& effect = *stage.getEffect(); |
| 565 | int numTextures = effect->numTextures(); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 566 | SkSTArray<8, GrGLShaderBuilder::TextureSampler> textureSamplers; |
| 567 | textureSamplers.push_back_n(numTextures); |
| 568 | for (int i = 0; i < numTextures; ++i) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 569 | textureSamplers[i].init(this, &effect->textureAccess(i), i); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 570 | samplerHandles->push_back(textureSamplers[i].fSamplerUniform); |
| 571 | } |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 572 | GrDrawEffect drawEffect(stage, this->hasExplicitLocalCoords()); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 573 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 574 | int numAttributes = stage.getVertexAttribIndexCount(); |
| 575 | const int* attributeIndices = stage.getVertexAttribIndices(); |
| 576 | SkSTArray<GrEffect::kMaxVertexAttribs, SkString> attributeNames; |
| 577 | for (int i = 0; i < numAttributes; ++i) { |
| 578 | SkString attributeName("aAttr"); |
| 579 | attributeName.appendS32(attributeIndices[i]); |
| 580 | |
| 581 | if (this->addAttribute(effect->vertexAttribType(i), attributeName.c_str())) { |
| 582 | fEffectAttributes.push_back().set(attributeIndices[i], attributeName); |
| 583 | } |
| 584 | } |
| 585 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 586 | GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 587 | |
| 588 | // Enclose custom code in a block to avoid namespace conflicts |
| 589 | this->fVSCode.appendf("\t{ // %s\n", glEffect->name()); |
| 590 | this->fFSCode.appendf("\t{ // %s \n", glEffect->name()); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 591 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 592 | glEffect->emitCode(this, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 593 | drawEffect, |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 594 | key, |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 595 | fsOutColor, |
| 596 | fsInColor, |
| 597 | textureSamplers); |
| 598 | this->fVSCode.appendf("\t}\n"); |
| 599 | this->fFSCode.appendf("\t}\n"); |
| 600 | |
| 601 | return glEffect; |
| 602 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 603 | |
| 604 | const SkString* GrGLShaderBuilder::getEffectAttributeName(int attributeIndex) const { |
| 605 | const AttributePair* attribEnd = this->getEffectAttributes().end(); |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 606 | for (const AttributePair* attrib = this->getEffectAttributes().begin(); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 607 | attrib != attribEnd; |
| 608 | ++attrib) { |
| 609 | if (attrib->fIndex == attributeIndex) { |
| 610 | return &attrib->fName; |
| 611 | } |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 612 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 613 | |
| 614 | return NULL; |
| 615 | } |