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