joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 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 | |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 8 | #include "GrSwizzle.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 9 | #include "glsl/GrGLSLShaderBuilder.h" |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 10 | #include "glsl/GrGLSLCaps.h" |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 11 | #include "glsl/GrGLSLShaderVar.h" |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLSampler.h" |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLProgramBuilder.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 14 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 15 | GrGLSLShaderBuilder::GrGLSLShaderBuilder(GrGLSLProgramBuilder* program) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 16 | : fProgramBuilder(program) |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 17 | , fInputs(GrGLSLProgramBuilder::kVarsPerBlock) |
| 18 | , fOutputs(GrGLSLProgramBuilder::kVarsPerBlock) |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 19 | , fFeaturesAddedMask(0) |
| 20 | , fCodeIndex(kCode) |
| 21 | , fFinalized(false) { |
| 22 | // We push back some dummy pointers which will later become our header |
| 23 | for (int i = 0; i <= kCode; i++) { |
| 24 | fShaderStrings.push_back(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 25 | fCompilerStrings.push_back(nullptr); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 26 | fCompilerStringLengths.push_back(0); |
| 27 | } |
| 28 | |
| 29 | this->main() = "void main() {"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 30 | } |
| 31 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 32 | void GrGLSLShaderBuilder::declAppend(const GrGLSLShaderVar& var) { |
egdaniel | b2f94d1 | 2014-08-29 10:08:36 -0700 | [diff] [blame] | 33 | SkString tempDecl; |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 34 | var.appendDecl(fProgramBuilder->glslCaps(), &tempDecl); |
egdaniel | b2f94d1 | 2014-08-29 10:08:36 -0700 | [diff] [blame] | 35 | this->codeAppendf("%s;", tempDecl.c_str()); |
| 36 | } |
| 37 | |
cdalton | 1f50acf | 2016-04-11 11:30:50 -0700 | [diff] [blame] | 38 | void GrGLSLShaderBuilder::appendPrecisionModifier(GrSLPrecision precision) { |
| 39 | if (fProgramBuilder->glslCaps()->usesPrecisionModifiers()) { |
| 40 | this->codeAppendf("%s ", GrGLSLPrecisionString(precision)); |
| 41 | } |
| 42 | } |
| 43 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 44 | void GrGLSLShaderBuilder::emitFunction(GrSLType returnType, |
| 45 | const char* name, |
| 46 | int argCnt, |
| 47 | const GrGLSLShaderVar* args, |
| 48 | const char* body, |
| 49 | SkString* outName) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 50 | this->functions().append(GrGLSLTypeString(returnType)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 51 | fProgramBuilder->nameVariable(outName, '\0', name); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 52 | this->functions().appendf(" %s", outName->c_str()); |
| 53 | this->functions().append("("); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 54 | for (int i = 0; i < argCnt; ++i) { |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 55 | args[i].appendDecl(fProgramBuilder->glslCaps(), &this->functions()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 56 | if (i < argCnt - 1) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 57 | this->functions().append(", "); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 60 | this->functions().append(") {\n"); |
| 61 | this->functions().append(body); |
| 62 | this->functions().append("}\n\n"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 63 | } |
| 64 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 65 | void GrGLSLShaderBuilder::appendTextureLookup(SkString* out, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 66 | const GrGLSLSampler& sampler, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 67 | const char* coordName, |
| 68 | GrSLType varyingType) const { |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 69 | const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 70 | GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 71 | GrSLType samplerType = uniformHandler->getUniformVariable(sampler.fSamplerUniform).getType(); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 72 | if (samplerType == kSampler2DRect_GrSLType) { |
| 73 | if (varyingType == kVec2f_GrSLType) { |
| 74 | out->appendf("%s(%s, textureSize(%s) * %s)", |
| 75 | GrGLSLTexture2DFunctionName(varyingType, samplerType, |
| 76 | glslCaps->generation()), |
| 77 | uniformHandler->getUniformCStr(sampler.fSamplerUniform), |
| 78 | uniformHandler->getUniformCStr(sampler.fSamplerUniform), |
| 79 | coordName); |
| 80 | } else { |
| 81 | out->appendf("%s(%s, vec3(textureSize(%s) * %s.xy, %s.z))", |
| 82 | GrGLSLTexture2DFunctionName(varyingType, samplerType, |
| 83 | glslCaps->generation()), |
| 84 | uniformHandler->getUniformCStr(sampler.fSamplerUniform), |
| 85 | uniformHandler->getUniformCStr(sampler.fSamplerUniform), |
| 86 | coordName, |
| 87 | coordName); |
| 88 | } |
| 89 | } else { |
| 90 | out->appendf("%s(%s, %s)", |
| 91 | GrGLSLTexture2DFunctionName(varyingType, samplerType, glslCaps->generation()), |
| 92 | uniformHandler->getUniformCStr(sampler.fSamplerUniform), |
| 93 | coordName); |
| 94 | } |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 95 | |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame^] | 96 | this->appendTextureSwizzle(out, sampler.config()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 97 | } |
| 98 | |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 99 | void GrGLSLShaderBuilder::appendTextureLookup(const GrGLSLSampler& sampler, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 100 | const char* coordName, |
| 101 | GrSLType varyingType) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 102 | this->appendTextureLookup(&this->code(), sampler, coordName, varyingType); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 103 | } |
| 104 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 105 | void GrGLSLShaderBuilder::appendTextureLookupAndModulate(const char* modulation, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 106 | const GrGLSLSampler& sampler, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 107 | const char* coordName, |
| 108 | GrSLType varyingType) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 109 | SkString lookup; |
| 110 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
| 111 | this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
| 112 | } |
| 113 | |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame^] | 114 | void GrGLSLShaderBuilder::appendTexelFetch(SkString* out, |
| 115 | const GrGLSLSampler& sampler, |
| 116 | const char* coordExpr) const { |
| 117 | const GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler(); |
| 118 | SkASSERT(fProgramBuilder->glslCaps()->texelFetchSupport()); |
| 119 | SkASSERT(GrSLTypeIsSamplerType( |
| 120 | uniformHandler->getUniformVariable(sampler.fSamplerUniform).getType())); |
| 121 | |
| 122 | out->appendf("texelFetch(%s, %s)", |
| 123 | uniformHandler->getUniformCStr(sampler.fSamplerUniform), |
| 124 | coordExpr); |
| 125 | |
| 126 | this->appendTextureSwizzle(out, sampler.config()); |
| 127 | } |
| 128 | |
| 129 | void GrGLSLShaderBuilder::appendTexelFetch(const GrGLSLSampler& sampler, const char* coordExpr) { |
| 130 | this->appendTexelFetch(&this->code(), sampler, coordExpr); |
| 131 | } |
| 132 | |
| 133 | void GrGLSLShaderBuilder::appendTextureSwizzle(SkString* out, GrPixelConfig config) const { |
| 134 | const GrSwizzle& configSwizzle = fProgramBuilder->glslCaps()->configTextureSwizzle(config); |
| 135 | |
| 136 | if (configSwizzle != GrSwizzle::RGBA()) { |
| 137 | out->appendf(".%s", configSwizzle.c_str()); |
| 138 | } |
| 139 | } |
| 140 | |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 141 | bool GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) { |
| 142 | if (featureBit & fFeaturesAddedMask) { |
| 143 | return false; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 144 | } |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 145 | this->extensions().appendf("#extension %s: require\n", extensionName); |
| 146 | fFeaturesAddedMask |= featureBit; |
| 147 | return true; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 148 | } |
| 149 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 150 | void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 151 | for (int i = 0; i < vars.count(); ++i) { |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 152 | vars[i].appendDecl(fProgramBuilder->glslCaps(), out); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 153 | out->append(";\n"); |
| 154 | } |
| 155 | } |
| 156 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 157 | void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) { |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 158 | SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration || |
| 159 | fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs()); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 160 | fLayoutParams[interface].push_back() = param; |
| 161 | } |
| 162 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 163 | void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() { |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 164 | static const char* interfaceQualifierNames[] = { |
| 165 | "out" |
| 166 | }; |
| 167 | |
| 168 | for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) { |
| 169 | const SkTArray<SkString>& params = fLayoutParams[interface]; |
| 170 | if (params.empty()) { |
| 171 | continue; |
| 172 | } |
| 173 | this->layoutQualifiers().appendf("layout(%s", params[0].c_str()); |
| 174 | for (int i = 1; i < params.count(); ++i) { |
| 175 | this->layoutQualifiers().appendf(", %s", params[i].c_str()); |
| 176 | } |
| 177 | this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]); |
| 178 | } |
| 179 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 180 | GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kOut_InterfaceQualifier); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 181 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1); |
| 182 | } |
| 183 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 184 | void GrGLSLShaderBuilder::finalize(uint32_t visibility) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 185 | SkASSERT(!fFinalized); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 186 | this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString(); |
| 187 | this->compileAndAppendLayoutQualifiers(); |
egdaniel | c8a66eb | 2015-11-02 07:46:25 -0800 | [diff] [blame] | 188 | SkASSERT(visibility); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 189 | fProgramBuilder->appendUniformDecls((GrShaderFlags) visibility, &this->uniforms()); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 190 | this->appendDecls(fInputs, &this->inputs()); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 191 | this->appendDecls(fOutputs, &this->outputs()); |
| 192 | this->onFinalize(); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 193 | // append the 'footer' to code |
| 194 | this->code().append("}"); |
| 195 | |
| 196 | for (int i = 0; i <= fCodeIndex; i++) { |
| 197 | fCompilerStrings[i] = fShaderStrings[i].c_str(); |
| 198 | fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); |
| 199 | } |
| 200 | |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 201 | fFinalized = true; |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 202 | } |