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