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 | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 27 | inline const char* sample_function_name(GrSLType type) { |
| 28 | if (kVec2f_GrSLType == type) { |
| 29 | return "texture2D"; |
| 30 | } else { |
| 31 | GrAssert(kVec3f_GrSLType == type); |
| 32 | return "texture2DProj"; |
| 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 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 84 | GrGLShaderBuilder::GrGLShaderBuilder(const GrGLContextInfo& ctx, GrGLUniformManager& uniformManager) |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 85 | : fUniforms(kVarsPerBlock) |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 86 | , fVSAttrs(kVarsPerBlock) |
| 87 | , fVSOutputs(kVarsPerBlock) |
| 88 | , fGSInputs(kVarsPerBlock) |
| 89 | , fGSOutputs(kVarsPerBlock) |
| 90 | , fFSInputs(kVarsPerBlock) |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 91 | , fFSOutputs(kMaxFSOutputs) |
tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 92 | , fUsesGS(false) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 93 | , fContext(ctx) |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 94 | , fUniformManager(uniformManager) |
bsalomon@google.com | d9e0181 | 2012-08-29 19:35:44 +0000 | [diff] [blame] | 95 | , fCurrentStage(kNonStageIdx) |
| 96 | , fTexCoordVaryingType(kVoid_GrSLType) { |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 97 | } |
| 98 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 99 | void GrGLShaderBuilder::setupTextureAccess(const char* varyingFSName, GrSLType varyingType) { |
| 100 | // FIXME: We don't know how the custom stage will manipulate the coords. So we give up on using |
| 101 | // projective texturing and always give the stage 2D coords. This will be fixed when custom |
| 102 | // stages are repsonsible for setting up their own tex coords / tex matrices. |
| 103 | switch (varyingType) { |
| 104 | case kVec2f_GrSLType: |
| 105 | fDefaultTexCoordsName = varyingFSName; |
| 106 | fTexCoordVaryingType = kVec2f_GrSLType; |
| 107 | break; |
| 108 | case kVec3f_GrSLType: { |
| 109 | fDefaultTexCoordsName = "inCoord"; |
| 110 | GrAssert(kNonStageIdx != fCurrentStage); |
| 111 | fDefaultTexCoordsName.appendS32(fCurrentStage); |
| 112 | fTexCoordVaryingType = kVec3f_GrSLType; |
| 113 | fFSCode.appendf("\t%s %s = %s.xy / %s.z;\n", |
| 114 | GrGLShaderVar::TypeString(kVec2f_GrSLType), |
| 115 | fDefaultTexCoordsName.c_str(), |
| 116 | varyingFSName, |
| 117 | varyingFSName); |
| 118 | break; |
| 119 | } |
| 120 | default: |
| 121 | GrCrash("Tex coords must either be Vec2f or Vec3f"); |
tomhudson@google.com | de78823 | 2012-08-02 20:13:12 +0000 | [diff] [blame] | 122 | } |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 123 | } |
| 124 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 125 | void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 126 | const GrGLShaderBuilder::TextureSampler& sampler, |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 127 | const char* coordName, |
| 128 | GrSLType varyingType) const { |
bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 129 | GrAssert(NULL != sampler.textureAccess()); |
bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 130 | |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 131 | if (NULL == coordName) { |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 132 | coordName = fDefaultTexCoordsName.c_str(); |
| 133 | varyingType = kVec2f_GrSLType; |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 134 | } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 135 | out->appendf("%s(%s, %s)", |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 136 | sample_function_name(varyingType), |
| 137 | this->getUniformCStr(sampler.fSamplerUniform), |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 138 | coordName); |
| 139 | append_swizzle(out, *sampler.textureAccess(), fContext.caps()); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 140 | } |
| 141 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 142 | void GrGLShaderBuilder::appendTextureLookupAndModulate( |
| 143 | SkString* out, |
| 144 | const char* modulation, |
| 145 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 146 | const char* coordName, |
| 147 | GrSLType varyingType) const { |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 148 | GrAssert(NULL != out); |
| 149 | SkString lookup; |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 150 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 151 | GrGLSLModulate4f(out, modulation, lookup.c_str()); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 152 | } |
| 153 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 154 | GrCustomStage::StageKey GrGLShaderBuilder::KeyForTextureAccess(const GrTextureAccess& access, |
| 155 | const GrGLCaps& caps) { |
| 156 | GrCustomStage::StageKey key = 0; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 157 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 158 | // Assume that swizzle support implies that we never have to modify a shader to adjust |
| 159 | // for texture format/swizzle settings. |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 160 | if (!caps.textureSwizzleSupport() && swizzle_requires_alpha_remapping(caps, access)) { |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 161 | key = 1; |
| 162 | } |
bsalomon@google.com | 73d5b2f | 2012-10-04 13:26:32 +0000 | [diff] [blame^] | 163 | #if GR_DEBUG |
| 164 | // Assert that key is set iff the swizzle will be modified. |
| 165 | SkString origString(access.getSwizzle()); |
| 166 | origString.prepend("."); |
| 167 | SkString modifiedString; |
| 168 | append_swizzle(&modifiedString, access, caps); |
| 169 | if (!modifiedString.size()) { |
| 170 | modifiedString = ".rgba"; |
| 171 | } |
| 172 | GrAssert(SkToBool(key) == (modifiedString != origString)); |
| 173 | #endif |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 174 | return key; |
| 175 | } |
| 176 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 177 | const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps) { |
| 178 | if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 179 | if (caps.textureRedSupport()) { |
| 180 | static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RED, GR_GL_RED }; |
| 181 | return gRedSmear; |
| 182 | } else { |
| 183 | static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 184 | GR_GL_ALPHA, GR_GL_ALPHA }; |
| 185 | return gAlphaSmear; |
| 186 | } |
| 187 | } else { |
| 188 | static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA }; |
| 189 | return gStraight; |
| 190 | } |
| 191 | } |
| 192 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 193 | GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniformArray(uint32_t visibility, |
| 194 | GrSLType type, |
| 195 | const char* name, |
| 196 | int count, |
| 197 | const char** outName) { |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 198 | GrAssert(name && strlen(name)); |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 199 | static const uint32_t kVisibilityMask = kVertex_ShaderType | kFragment_ShaderType; |
| 200 | GrAssert(0 == (~kVisibilityMask & visibility)); |
| 201 | GrAssert(0 != visibility); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 203 | BuilderUniform& uni = fUniforms.push_back(); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 204 | UniformHandle h = index_to_handle(fUniforms.count() - 1); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 205 | GR_DEBUGCODE(UniformHandle h2 =) |
| 206 | fUniformManager.appendUniform(type, count); |
| 207 | // We expect the uniform manager to initially have no uniforms and that all uniforms are added |
| 208 | // by this function. Therefore, the handles should match. |
| 209 | GrAssert(h2 == h); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 210 | uni.fVariable.setType(type); |
| 211 | uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 212 | SkString* uniName = uni.fVariable.accessName(); |
| 213 | if (kNonStageIdx == fCurrentStage) { |
| 214 | uniName->printf("u%s", name); |
| 215 | } else { |
| 216 | uniName->printf("u%s%d", name, fCurrentStage); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 217 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 218 | uni.fVariable.setArrayCount(count); |
| 219 | uni.fVisibility = visibility; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 220 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 221 | // If it is visible in both the VS and FS, the precision must match. |
| 222 | // We declare a default FS precision, but not a default VS. So set the var |
| 223 | // to use the default FS precision. |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 224 | if ((kVertex_ShaderType | kFragment_ShaderType) == visibility) { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 225 | // the fragment and vertex precisions must match |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 226 | uni.fVariable.setPrecision(kDefaultFragmentPrecision); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 227 | } |
| 228 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 229 | if (NULL != outName) { |
| 230 | *outName = uni.fVariable.c_str(); |
| 231 | } |
| 232 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 233 | return h; |
| 234 | } |
| 235 | |
| 236 | const GrGLShaderVar& GrGLShaderBuilder::getUniformVariable(UniformHandle u) const { |
| 237 | return fUniforms[handle_to_index(u)].fVariable; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 238 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 239 | |
| 240 | void GrGLShaderBuilder::addVarying(GrSLType type, |
| 241 | const char* name, |
| 242 | const char** vsOutName, |
| 243 | const char** fsInName) { |
| 244 | fVSOutputs.push_back(); |
| 245 | fVSOutputs.back().setType(type); |
| 246 | fVSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier); |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 247 | if (kNonStageIdx == fCurrentStage) { |
| 248 | fVSOutputs.back().accessName()->printf("v%s", name); |
| 249 | } else { |
| 250 | fVSOutputs.back().accessName()->printf("v%s%d", name, fCurrentStage); |
| 251 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 252 | if (vsOutName) { |
| 253 | *vsOutName = fVSOutputs.back().getName().c_str(); |
| 254 | } |
| 255 | // input to FS comes either from VS or GS |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 256 | const SkString* fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 257 | if (fUsesGS) { |
| 258 | // if we have a GS take each varying in as an array |
| 259 | // and output as non-array. |
| 260 | fGSInputs.push_back(); |
| 261 | fGSInputs.back().setType(type); |
| 262 | fGSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier); |
| 263 | fGSInputs.back().setUnsizedArray(); |
| 264 | *fGSInputs.back().accessName() = fVSOutputs.back().getName(); |
| 265 | fGSOutputs.push_back(); |
| 266 | fGSOutputs.back().setType(type); |
| 267 | fGSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 268 | if (kNonStageIdx == fCurrentStage) { |
| 269 | fGSOutputs.back().accessName()->printf("g%s", name); |
| 270 | } else { |
| 271 | fGSOutputs.back().accessName()->printf("g%s%d", name, fCurrentStage); |
| 272 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 273 | fsName = fGSOutputs.back().accessName(); |
| 274 | } else { |
| 275 | fsName = fVSOutputs.back().accessName(); |
| 276 | } |
| 277 | fFSInputs.push_back(); |
| 278 | fFSInputs.back().setType(type); |
| 279 | fFSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier); |
| 280 | fFSInputs.back().setName(*fsName); |
| 281 | if (fsInName) { |
| 282 | *fsInName = fsName->c_str(); |
| 283 | } |
| 284 | } |
| 285 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 286 | void GrGLShaderBuilder::emitFunction(ShaderType shader, |
| 287 | GrSLType returnType, |
| 288 | const char* name, |
| 289 | int argCnt, |
| 290 | const GrGLShaderVar* args, |
| 291 | const char* body, |
| 292 | SkString* outName) { |
| 293 | GrAssert(kFragment_ShaderType == shader); |
| 294 | fFSFunctions.append(GrGLShaderVar::TypeString(returnType)); |
| 295 | if (kNonStageIdx != fCurrentStage) { |
| 296 | outName->printf(" %s_%d", name, fCurrentStage); |
| 297 | } else { |
| 298 | *outName = name; |
| 299 | } |
| 300 | fFSFunctions.append(*outName); |
| 301 | fFSFunctions.append("("); |
| 302 | for (int i = 0; i < argCnt; ++i) { |
| 303 | args[i].appendDecl(fContext, &fFSFunctions); |
| 304 | if (i < argCnt - 1) { |
| 305 | fFSFunctions.append(", "); |
| 306 | } |
| 307 | } |
| 308 | fFSFunctions.append(") {\n"); |
| 309 | fFSFunctions.append(body); |
| 310 | fFSFunctions.append("}\n\n"); |
| 311 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 312 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 313 | namespace { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 314 | |
| 315 | inline void append_default_precision_qualifier(GrGLShaderVar::Precision p, |
| 316 | GrGLBinding binding, |
| 317 | SkString* str) { |
| 318 | // Desktop GLSL has added precision qualifiers but they don't do anything. |
| 319 | if (kES2_GrGLBinding == binding) { |
| 320 | switch (p) { |
| 321 | case GrGLShaderVar::kHigh_Precision: |
| 322 | str->append("precision highp float;\n"); |
| 323 | break; |
| 324 | case GrGLShaderVar::kMedium_Precision: |
| 325 | str->append("precision mediump float;\n"); |
| 326 | break; |
| 327 | case GrGLShaderVar::kLow_Precision: |
| 328 | str->append("precision lowp float;\n"); |
| 329 | break; |
| 330 | case GrGLShaderVar::kDefault_Precision: |
| 331 | GrCrash("Default precision now allowed."); |
| 332 | default: |
| 333 | GrCrash("Unknown precision value."); |
| 334 | } |
| 335 | } |
| 336 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 337 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 338 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 339 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 340 | for (int i = 0; i < vars.count(); ++i) { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 341 | vars[i].appendDecl(fContext, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 342 | out->append(";\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 345 | |
| 346 | void GrGLShaderBuilder::appendUniformDecls(ShaderType stype, SkString* out) const { |
| 347 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 348 | if (fUniforms[i].fVisibility & stype) { |
| 349 | fUniforms[i].fVariable.appendDecl(fContext, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 350 | out->append(";\n"); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void GrGLShaderBuilder::getShader(ShaderType type, SkString* shaderStr) const { |
| 356 | switch (type) { |
| 357 | case kVertex_ShaderType: |
| 358 | *shaderStr = fHeader; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 359 | this->appendUniformDecls(kVertex_ShaderType, shaderStr); |
| 360 | this->appendDecls(fVSAttrs, shaderStr); |
| 361 | this->appendDecls(fVSOutputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 362 | shaderStr->append(fVSCode); |
| 363 | break; |
| 364 | case kGeometry_ShaderType: |
| 365 | if (fUsesGS) { |
| 366 | *shaderStr = fHeader; |
| 367 | shaderStr->append(fGSHeader); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 368 | this->appendDecls(fGSInputs, shaderStr); |
| 369 | this->appendDecls(fGSOutputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 370 | shaderStr->append(fGSCode); |
| 371 | } else { |
| 372 | shaderStr->reset(); |
| 373 | } |
| 374 | break; |
| 375 | case kFragment_ShaderType: |
| 376 | *shaderStr = fHeader; |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 377 | append_default_precision_qualifier(kDefaultFragmentPrecision, |
| 378 | fContext.binding(), |
| 379 | shaderStr); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 380 | this->appendUniformDecls(kFragment_ShaderType, shaderStr); |
| 381 | this->appendDecls(fFSInputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 382 | // We shouldn't have declared outputs on 1.10 |
| 383 | GrAssert(k110_GrGLSLGeneration != fContext.glslGeneration() || fFSOutputs.empty()); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 384 | this->appendDecls(fFSOutputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 385 | shaderStr->append(fFSFunctions); |
| 386 | shaderStr->append(fFSCode); |
| 387 | break; |
| 388 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 389 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 390 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 391 | void GrGLShaderBuilder::finished(GrGLuint programID) { |
| 392 | fUniformManager.getUniformLocations(programID, fUniforms); |
| 393 | } |