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) { |
| 105 | SkString* string; |
| 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) { |
| 123 | SkString* string; |
| 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 | |
| 261 | void GrGLShaderBuilder::addVarying(GrSLType type, |
| 262 | const char* name, |
| 263 | const char** vsOutName, |
| 264 | const char** fsInName) { |
| 265 | fVSOutputs.push_back(); |
| 266 | fVSOutputs.back().setType(type); |
| 267 | fVSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier); |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 268 | if (kNonStageIdx == fCurrentStageIdx) { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 269 | fVSOutputs.back().accessName()->printf("v%s", name); |
| 270 | } else { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 271 | fVSOutputs.back().accessName()->printf("v%s%d", name, fCurrentStageIdx); |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 272 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 273 | if (vsOutName) { |
| 274 | *vsOutName = fVSOutputs.back().getName().c_str(); |
| 275 | } |
| 276 | // input to FS comes either from VS or GS |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 277 | const SkString* fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 278 | if (fUsesGS) { |
| 279 | // if we have a GS take each varying in as an array |
| 280 | // and output as non-array. |
| 281 | fGSInputs.push_back(); |
| 282 | fGSInputs.back().setType(type); |
| 283 | fGSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier); |
| 284 | fGSInputs.back().setUnsizedArray(); |
| 285 | *fGSInputs.back().accessName() = fVSOutputs.back().getName(); |
| 286 | fGSOutputs.push_back(); |
| 287 | fGSOutputs.back().setType(type); |
| 288 | fGSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier); |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 289 | if (kNonStageIdx == fCurrentStageIdx) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 290 | fGSOutputs.back().accessName()->printf("g%s", name); |
| 291 | } else { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 292 | fGSOutputs.back().accessName()->printf("g%s%d", name, fCurrentStageIdx); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 293 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 294 | fsName = fGSOutputs.back().accessName(); |
| 295 | } else { |
| 296 | fsName = fVSOutputs.back().accessName(); |
| 297 | } |
| 298 | fFSInputs.push_back(); |
| 299 | fFSInputs.back().setType(type); |
| 300 | fFSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier); |
| 301 | fFSInputs.back().setName(*fsName); |
| 302 | if (fsInName) { |
| 303 | *fsInName = fsName->c_str(); |
| 304 | } |
| 305 | } |
| 306 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 307 | const char* GrGLShaderBuilder::fragmentPosition() { |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 308 | #if 1 |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 309 | if (fCtxInfo.caps().fragCoordConventionsSupport()) { |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 310 | if (!fSetupFragPosition) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame^] | 311 | if (fCtxInfo.glslGeneration() < k150_GrGLSLGeneration) { |
| 312 | fFSHeader.append("#extension GL_ARB_fragment_coord_conventions: require\n"); |
| 313 | } |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 314 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 315 | GrGLShaderVar::kIn_TypeModifier, |
| 316 | "gl_FragCoord", |
| 317 | GrGLShaderVar::kDefault_Precision, |
| 318 | GrGLShaderVar::kUpperLeft_Origin); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 319 | fSetupFragPosition = true; |
| 320 | } |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 321 | return "gl_FragCoord"; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 322 | } else { |
| 323 | static const char* kCoordName = "fragCoordYDown"; |
| 324 | if (!fSetupFragPosition) { |
| 325 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == fRTHeightUniform); |
| 326 | const char* rtHeightName; |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 327 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 328 | // temporarily change the stage index because we're inserting a uniform whose name |
| 329 | // shouldn't be mangled to be stage-specific. |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 330 | int oldStageIdx = fCurrentStageIdx; |
| 331 | fCurrentStageIdx = kNonStageIdx; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 332 | fRTHeightUniform = this->addUniform(kFragment_ShaderType, |
| 333 | kFloat_GrSLType, |
| 334 | "RTHeight", |
| 335 | &rtHeightName); |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 336 | fCurrentStageIdx = oldStageIdx; |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 337 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 338 | this->fFSCode.prependf("\tvec4 %s = vec4(gl_FragCoord.x, %s - gl_FragCoord.y, gl_FragCoord.zw);\n", |
| 339 | kCoordName, rtHeightName); |
| 340 | fSetupFragPosition = true; |
| 341 | } |
| 342 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != fRTHeightUniform); |
| 343 | return kCoordName; |
| 344 | } |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 345 | #else |
| 346 | // This is the path we'll need to use once we have support for TopLeft |
| 347 | // render targets. |
| 348 | if (!fSetupFragPosition) { |
| 349 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 350 | GrGLShaderVar::kIn_TypeModifier, |
| 351 | "gl_FragCoord", |
| 352 | GrGLShaderVar::kDefault_Precision); |
| 353 | fSetupFragPosition = true; |
| 354 | } |
| 355 | return "gl_FragCoord"; |
| 356 | #endif |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 360 | void GrGLShaderBuilder::emitFunction(ShaderType shader, |
| 361 | GrSLType returnType, |
| 362 | const char* name, |
| 363 | int argCnt, |
| 364 | const GrGLShaderVar* args, |
| 365 | const char* body, |
| 366 | SkString* outName) { |
| 367 | GrAssert(kFragment_ShaderType == shader); |
| 368 | fFSFunctions.append(GrGLShaderVar::TypeString(returnType)); |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 369 | if (kNonStageIdx != fCurrentStageIdx) { |
| 370 | outName->printf(" %s_%d", name, fCurrentStageIdx); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 371 | } else { |
| 372 | *outName = name; |
| 373 | } |
| 374 | fFSFunctions.append(*outName); |
| 375 | fFSFunctions.append("("); |
| 376 | for (int i = 0; i < argCnt; ++i) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 377 | args[i].appendDecl(fCtxInfo, &fFSFunctions); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 378 | if (i < argCnt - 1) { |
| 379 | fFSFunctions.append(", "); |
| 380 | } |
| 381 | } |
| 382 | fFSFunctions.append(") {\n"); |
| 383 | fFSFunctions.append(body); |
| 384 | fFSFunctions.append("}\n\n"); |
| 385 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 386 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 387 | namespace { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 388 | |
| 389 | inline void append_default_precision_qualifier(GrGLShaderVar::Precision p, |
| 390 | GrGLBinding binding, |
| 391 | SkString* str) { |
| 392 | // Desktop GLSL has added precision qualifiers but they don't do anything. |
| 393 | if (kES2_GrGLBinding == binding) { |
| 394 | switch (p) { |
| 395 | case GrGLShaderVar::kHigh_Precision: |
| 396 | str->append("precision highp float;\n"); |
| 397 | break; |
| 398 | case GrGLShaderVar::kMedium_Precision: |
| 399 | str->append("precision mediump float;\n"); |
| 400 | break; |
| 401 | case GrGLShaderVar::kLow_Precision: |
| 402 | str->append("precision lowp float;\n"); |
| 403 | break; |
| 404 | case GrGLShaderVar::kDefault_Precision: |
| 405 | GrCrash("Default precision now allowed."); |
| 406 | default: |
| 407 | GrCrash("Unknown precision value."); |
| 408 | } |
| 409 | } |
| 410 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 411 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 412 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 413 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 414 | for (int i = 0; i < vars.count(); ++i) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 415 | vars[i].appendDecl(fCtxInfo, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 416 | out->append(";\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 419 | |
| 420 | void GrGLShaderBuilder::appendUniformDecls(ShaderType stype, SkString* out) const { |
| 421 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 422 | if (fUniforms[i].fVisibility & stype) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 423 | fUniforms[i].fVariable.appendDecl(fCtxInfo, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 424 | out->append(";\n"); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void GrGLShaderBuilder::getShader(ShaderType type, SkString* shaderStr) const { |
| 430 | switch (type) { |
| 431 | case kVertex_ShaderType: |
| 432 | *shaderStr = fHeader; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 433 | this->appendUniformDecls(kVertex_ShaderType, shaderStr); |
| 434 | this->appendDecls(fVSAttrs, shaderStr); |
| 435 | this->appendDecls(fVSOutputs, shaderStr); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 436 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 437 | shaderStr->append(fVSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 438 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 439 | break; |
| 440 | case kGeometry_ShaderType: |
| 441 | if (fUsesGS) { |
| 442 | *shaderStr = fHeader; |
| 443 | shaderStr->append(fGSHeader); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 444 | this->appendDecls(fGSInputs, shaderStr); |
| 445 | this->appendDecls(fGSOutputs, shaderStr); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 446 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 447 | shaderStr->append(fGSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 448 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 449 | } else { |
| 450 | shaderStr->reset(); |
| 451 | } |
| 452 | break; |
| 453 | case kFragment_ShaderType: |
| 454 | *shaderStr = fHeader; |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 455 | append_default_precision_qualifier(kDefaultFragmentPrecision, |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 456 | fCtxInfo.binding(), |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 457 | shaderStr); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 458 | shaderStr->append(fFSHeader); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 459 | this->appendUniformDecls(kFragment_ShaderType, shaderStr); |
| 460 | this->appendDecls(fFSInputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 461 | // We shouldn't have declared outputs on 1.10 |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 462 | GrAssert(k110_GrGLSLGeneration != fCtxInfo.glslGeneration() || fFSOutputs.empty()); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 463 | this->appendDecls(fFSOutputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 464 | shaderStr->append(fFSFunctions); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 465 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 466 | shaderStr->append(fFSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 467 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 468 | break; |
| 469 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 470 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 471 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 472 | void GrGLShaderBuilder::finished(GrGLuint programID) { |
| 473 | fUniformManager.getUniformLocations(programID, fUniforms); |
| 474 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 475 | |
| 476 | GrGLEffect* GrGLShaderBuilder::createAndEmitGLEffect( |
| 477 | const GrEffectStage& stage, |
| 478 | GrGLEffect::EffectKey key, |
| 479 | const char* fsInColor, |
| 480 | const char* fsOutColor, |
| 481 | const char* vsInCoord, |
| 482 | SkTArray<GrGLUniformManager::UniformHandle, true>* samplerHandles) { |
| 483 | GrAssert(NULL != stage.getEffect()); |
| 484 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 485 | const GrEffectRef& effect = *stage.getEffect(); |
| 486 | int numTextures = effect->numTextures(); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 487 | SkSTArray<8, GrGLShaderBuilder::TextureSampler> textureSamplers; |
| 488 | textureSamplers.push_back_n(numTextures); |
| 489 | for (int i = 0; i < numTextures; ++i) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 490 | textureSamplers[i].init(this, &effect->textureAccess(i), i); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 491 | samplerHandles->push_back(textureSamplers[i].fSamplerUniform); |
| 492 | } |
| 493 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 494 | GrGLEffect* glEffect = effect->getFactory().createGLInstance(effect); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 495 | |
| 496 | // Enclose custom code in a block to avoid namespace conflicts |
| 497 | this->fVSCode.appendf("\t{ // %s\n", glEffect->name()); |
| 498 | this->fFSCode.appendf("\t{ // %s \n", glEffect->name()); |
| 499 | glEffect->emitCode(this, |
| 500 | stage, |
| 501 | key, |
| 502 | vsInCoord, |
| 503 | fsOutColor, |
| 504 | fsInColor, |
| 505 | textureSamplers); |
| 506 | this->fVSCode.appendf("\t}\n"); |
| 507 | this->fFSCode.appendf("\t}\n"); |
| 508 | |
| 509 | return glEffect; |
| 510 | } |