| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2014 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 "GrGLShaderBuilder.h" | |
| 9 #include "glsl/GrGLSLCaps.h" | |
| 10 #include "glsl/GrGLSLShaderVar.h" | |
| 11 #include "glsl/GrGLSLTextureSampler.h" | |
| 12 #include "glsl/GrGLSLProgramBuilder.h" | |
| 13 | |
| 14 static void map_swizzle(const char* swizzleMap, const char* swizzle, char* mangl
edSwizzle) { | |
| 15 int i; | |
| 16 for (i = 0; '\0' != swizzle[i]; ++i) { | |
| 17 switch (swizzle[i]) { | |
| 18 case 'r': | |
| 19 mangledSwizzle[i] = swizzleMap[0]; | |
| 20 break; | |
| 21 case 'g': | |
| 22 mangledSwizzle[i] = swizzleMap[1]; | |
| 23 break; | |
| 24 case 'b': | |
| 25 mangledSwizzle[i] = swizzleMap[2]; | |
| 26 break; | |
| 27 case 'a': | |
| 28 mangledSwizzle[i] = swizzleMap[3]; | |
| 29 break; | |
| 30 default: | |
| 31 SkFAIL("Unsupported swizzle"); | |
| 32 } | |
| 33 } | |
| 34 mangledSwizzle[i] ='\0'; | |
| 35 } | |
| 36 | |
| 37 static void append_texture_lookup(SkString* out, | |
| 38 const GrGLSLCaps* glslCaps, | |
| 39 const char* samplerName, | |
| 40 const char* coordName, | |
| 41 GrPixelConfig config, | |
| 42 const char* swizzle, | |
| 43 GrSLType varyingType = kVec2f_GrSLType) { | |
| 44 SkASSERT(coordName); | |
| 45 | |
| 46 out->appendf("%s(%s, %s)", | |
| 47 GrGLSLTexture2DFunctionName(varyingType, glslCaps->generation()
), | |
| 48 samplerName, | |
| 49 coordName); | |
| 50 | |
| 51 char mangledSwizzle[5]; | |
| 52 | |
| 53 // This refers to any swizzling we may need to get from some backend interna
l format to the | |
| 54 // format used in GrPixelConfig. Some backends will automatically do the siz
zling for us. | |
| 55 if (glslCaps->mustSwizzleInShader()) { | |
| 56 const char* swizzleMap = glslCaps->getSwizzleMap(config); | |
| 57 // if the map is simply 'rgba' then we don't need to do any manual swizz
ling to get us to | |
| 58 // a GrPixelConfig format. | |
| 59 if (memcmp(swizzleMap, "rgba", 4)) { | |
| 60 // Manually 'swizzle' the swizzle using our mapping | |
| 61 map_swizzle(swizzleMap, swizzle, mangledSwizzle); | |
| 62 swizzle = mangledSwizzle; | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 // For shader prettiness we omit the swizzle rather than appending ".rgba". | |
| 67 if (memcmp(swizzle, "rgba", 4)) { | |
| 68 out->appendf(".%s", swizzle); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 GrGLShaderBuilder::GrGLShaderBuilder(GrGLSLProgramBuilder* program) | |
| 73 : fProgramBuilder(program) | |
| 74 , fInputs(GrGLSLProgramBuilder::kVarsPerBlock) | |
| 75 , fOutputs(GrGLSLProgramBuilder::kVarsPerBlock) | |
| 76 , fFeaturesAddedMask(0) | |
| 77 , fCodeIndex(kCode) | |
| 78 , fFinalized(false) { | |
| 79 // We push back some dummy pointers which will later become our header | |
| 80 for (int i = 0; i <= kCode; i++) { | |
| 81 fShaderStrings.push_back(); | |
| 82 fCompilerStrings.push_back(nullptr); | |
| 83 fCompilerStringLengths.push_back(0); | |
| 84 } | |
| 85 | |
| 86 this->main() = "void main() {"; | |
| 87 } | |
| 88 | |
| 89 void GrGLShaderBuilder::declAppend(const GrGLSLShaderVar& var) { | |
| 90 SkString tempDecl; | |
| 91 var.appendDecl(fProgramBuilder->glslCaps(), &tempDecl); | |
| 92 this->codeAppendf("%s;", tempDecl.c_str()); | |
| 93 } | |
| 94 | |
| 95 void GrGLShaderBuilder::emitFunction(GrSLType returnType, | |
| 96 const char* name, | |
| 97 int argCnt, | |
| 98 const GrGLSLShaderVar* args, | |
| 99 const char* body, | |
| 100 SkString* outName) { | |
| 101 this->functions().append(GrGLSLTypeString(returnType)); | |
| 102 fProgramBuilder->nameVariable(outName, '\0', name); | |
| 103 this->functions().appendf(" %s", outName->c_str()); | |
| 104 this->functions().append("("); | |
| 105 for (int i = 0; i < argCnt; ++i) { | |
| 106 args[i].appendDecl(fProgramBuilder->glslCaps(), &this->functions()); | |
| 107 if (i < argCnt - 1) { | |
| 108 this->functions().append(", "); | |
| 109 } | |
| 110 } | |
| 111 this->functions().append(") {\n"); | |
| 112 this->functions().append(body); | |
| 113 this->functions().append("}\n\n"); | |
| 114 } | |
| 115 | |
| 116 void GrGLShaderBuilder::appendTextureLookup(SkString* out, | |
| 117 const GrGLSLTextureSampler& sampler, | |
| 118 const char* coordName, | |
| 119 GrSLType varyingType) const { | |
| 120 append_texture_lookup(out, | |
| 121 fProgramBuilder->glslCaps(), | |
| 122 fProgramBuilder->getUniformCStr(sampler.fSamplerUnifor
m), | |
| 123 coordName, | |
| 124 sampler.config(), | |
| 125 sampler.swizzle(), | |
| 126 varyingType); | |
| 127 } | |
| 128 | |
| 129 void GrGLShaderBuilder::appendTextureLookup(const GrGLSLTextureSampler& sampler, | |
| 130 const char* coordName, | |
| 131 GrSLType varyingType) { | |
| 132 this->appendTextureLookup(&this->code(), sampler, coordName, varyingType); | |
| 133 } | |
| 134 | |
| 135 void GrGLShaderBuilder::appendTextureLookupAndModulate(const char* modulation, | |
| 136 const GrGLSLTextureSample
r& sampler, | |
| 137 const char* coordName, | |
| 138 GrSLType varyingType) { | |
| 139 SkString lookup; | |
| 140 this->appendTextureLookup(&lookup, sampler, coordName, varyingType); | |
| 141 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); | |
| 142 } | |
| 143 | |
| 144 void GrGLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionNam
e) { | |
| 145 if (!(featureBit & fFeaturesAddedMask)) { | |
| 146 this->extensions().appendf("#extension %s: require\n", extensionName); | |
| 147 fFeaturesAddedMask |= featureBit; | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { | |
| 152 for (int i = 0; i < vars.count(); ++i) { | |
| 153 vars[i].appendDecl(fProgramBuilder->glslCaps(), out); | |
| 154 out->append(";\n"); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 void GrGLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier
interface) { | |
| 159 SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration
|| | |
| 160 fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs()); | |
| 161 fLayoutParams[interface].push_back() = param; | |
| 162 } | |
| 163 | |
| 164 void GrGLShaderBuilder::compileAndAppendLayoutQualifiers() { | |
| 165 static const char* interfaceQualifierNames[] = { | |
| 166 "out" | |
| 167 }; | |
| 168 | |
| 169 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) { | |
| 170 const SkTArray<SkString>& params = fLayoutParams[interface]; | |
| 171 if (params.empty()) { | |
| 172 continue; | |
| 173 } | |
| 174 this->layoutQualifiers().appendf("layout(%s", params[0].c_str()); | |
| 175 for (int i = 1; i < params.count(); ++i) { | |
| 176 this->layoutQualifiers().appendf(", %s", params[i].c_str()); | |
| 177 } | |
| 178 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[inte
rface]); | |
| 179 } | |
| 180 | |
| 181 GR_STATIC_ASSERT(0 == GrGLShaderBuilder::kOut_InterfaceQualifier); | |
| 182 GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQu
alifier + 1); | |
| 183 } | |
| 184 | |
| 185 void GrGLShaderBuilder::finalize(uint32_t visibility) { | |
| 186 SkASSERT(!fFinalized); | |
| 187 this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString(); | |
| 188 this->compileAndAppendLayoutQualifiers(); | |
| 189 SkASSERT(visibility); | |
| 190 fProgramBuilder->appendUniformDecls((GrGLSLProgramBuilder::ShaderVisibility)
visibility, | |
| 191 &this->uniforms()); | |
| 192 this->appendDecls(fInputs, &this->inputs()); | |
| 193 // We should not have any outputs in the fragment shader when using version
1.10 | |
| 194 SkASSERT(GrGLSLProgramBuilder::kFragment_Visibility != visibility || | |
| 195 k110_GrGLSLGeneration != fProgramBuilder->glslCaps()->generation()
|| | |
| 196 fOutputs.empty()); | |
| 197 this->appendDecls(fOutputs, &this->outputs()); | |
| 198 this->onFinalize(); | |
| 199 // append the 'footer' to code | |
| 200 this->code().append("}"); | |
| 201 | |
| 202 for (int i = 0; i <= fCodeIndex; i++) { | |
| 203 fCompilerStrings[i] = fShaderStrings[i].c_str(); | |
| 204 fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); | |
| 205 } | |
| 206 | |
| 207 fFinalized = true; | |
| 208 } | |
| 209 | |
| OLD | NEW |