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