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, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 66 | SamplerHandle samplerHandle, |
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 | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 70 | const GrGLSLSampler& sampler = fProgramBuilder->getSampler(samplerHandle); |
| 71 | GrSLType samplerType = sampler.type(); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame^] | 72 | if (samplerType == kTexture2DRectSampler_GrSLType) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 73 | if (varyingType == kVec2f_GrSLType) { |
| 74 | out->appendf("%s(%s, textureSize(%s) * %s)", |
| 75 | GrGLSLTexture2DFunctionName(varyingType, samplerType, |
| 76 | glslCaps->generation()), |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 77 | sampler.getSamplerNameForTexture2D(), |
| 78 | sampler.getSamplerNameForTexture2D(), |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 79 | coordName); |
| 80 | } else { |
| 81 | out->appendf("%s(%s, vec3(textureSize(%s) * %s.xy, %s.z))", |
| 82 | GrGLSLTexture2DFunctionName(varyingType, samplerType, |
| 83 | glslCaps->generation()), |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 84 | sampler.getSamplerNameForTexture2D(), |
| 85 | sampler.getSamplerNameForTexture2D(), |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 86 | coordName, |
| 87 | coordName); |
| 88 | } |
| 89 | } else { |
| 90 | out->appendf("%s(%s, %s)", |
| 91 | GrGLSLTexture2DFunctionName(varyingType, samplerType, glslCaps->generation()), |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 92 | sampler.getSamplerNameForTexture2D(), |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 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 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 99 | void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 100 | const char* coordName, |
| 101 | GrSLType varyingType) { |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 102 | this->appendTextureLookup(&this->code(), samplerHandle, 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, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 106 | SamplerHandle samplerHandle, |
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; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 110 | this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 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, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 115 | SamplerHandle samplerHandle, |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 116 | const char* coordExpr) const { |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 117 | const GrGLSLSampler& sampler = fProgramBuilder->getSampler(samplerHandle); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 118 | SkASSERT(fProgramBuilder->glslCaps()->texelFetchSupport()); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame^] | 119 | SkASSERT(GrSLTypeIsCombinedSamplerType(sampler.type())); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 120 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 121 | out->appendf("texelFetch(%s, %s)", sampler.getSamplerNameForTexelFetch(), coordExpr); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 122 | |
| 123 | this->appendTextureSwizzle(out, sampler.config()); |
| 124 | } |
| 125 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 126 | void GrGLSLShaderBuilder::appendTexelFetch(SamplerHandle samplerHandle, const char* coordExpr) { |
| 127 | this->appendTexelFetch(&this->code(), samplerHandle, coordExpr); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void GrGLSLShaderBuilder::appendTextureSwizzle(SkString* out, GrPixelConfig config) const { |
| 131 | const GrSwizzle& configSwizzle = fProgramBuilder->glslCaps()->configTextureSwizzle(config); |
| 132 | |
| 133 | if (configSwizzle != GrSwizzle::RGBA()) { |
| 134 | out->appendf(".%s", configSwizzle.c_str()); |
| 135 | } |
| 136 | } |
| 137 | |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 138 | bool GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) { |
| 139 | if (featureBit & fFeaturesAddedMask) { |
| 140 | return false; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 141 | } |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 142 | this->extensions().appendf("#extension %s: require\n", extensionName); |
| 143 | fFeaturesAddedMask |= featureBit; |
| 144 | return true; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 145 | } |
| 146 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 147 | void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 148 | for (int i = 0; i < vars.count(); ++i) { |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 149 | vars[i].appendDecl(fProgramBuilder->glslCaps(), out); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 150 | out->append(";\n"); |
| 151 | } |
| 152 | } |
| 153 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 154 | void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) { |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 155 | SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration || |
| 156 | fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs()); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 157 | fLayoutParams[interface].push_back() = param; |
| 158 | } |
| 159 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 160 | void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() { |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 161 | static const char* interfaceQualifierNames[] = { |
| 162 | "out" |
| 163 | }; |
| 164 | |
| 165 | for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) { |
| 166 | const SkTArray<SkString>& params = fLayoutParams[interface]; |
| 167 | if (params.empty()) { |
| 168 | continue; |
| 169 | } |
| 170 | this->layoutQualifiers().appendf("layout(%s", params[0].c_str()); |
| 171 | for (int i = 1; i < params.count(); ++i) { |
| 172 | this->layoutQualifiers().appendf(", %s", params[i].c_str()); |
| 173 | } |
| 174 | this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]); |
| 175 | } |
| 176 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 177 | GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kOut_InterfaceQualifier); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 178 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1); |
| 179 | } |
| 180 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 181 | void GrGLSLShaderBuilder::finalize(uint32_t visibility) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 182 | SkASSERT(!fFinalized); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 183 | this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString(); |
| 184 | this->compileAndAppendLayoutQualifiers(); |
egdaniel | c8a66eb | 2015-11-02 07:46:25 -0800 | [diff] [blame] | 185 | SkASSERT(visibility); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 186 | fProgramBuilder->appendUniformDecls((GrShaderFlags) visibility, &this->uniforms()); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 187 | this->appendDecls(fInputs, &this->inputs()); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 188 | this->appendDecls(fOutputs, &this->outputs()); |
| 189 | this->onFinalize(); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 190 | // append the 'footer' to code |
| 191 | this->code().append("}"); |
| 192 | |
| 193 | for (int i = 0; i <= fCodeIndex; i++) { |
| 194 | fCompilerStrings[i] = fShaderStrings[i].c_str(); |
| 195 | fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); |
| 196 | } |
| 197 | |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 198 | fFinalized = true; |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 199 | } |