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