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" |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLTextureSampler.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 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 38 | void GrGLSLShaderBuilder::emitFunction(GrSLType returnType, |
| 39 | const char* name, |
| 40 | int argCnt, |
| 41 | const GrGLSLShaderVar* args, |
| 42 | const char* body, |
| 43 | SkString* outName) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 44 | this->functions().append(GrGLSLTypeString(returnType)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 45 | fProgramBuilder->nameVariable(outName, '\0', name); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 46 | this->functions().appendf(" %s", outName->c_str()); |
| 47 | this->functions().append("("); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 48 | for (int i = 0; i < argCnt; ++i) { |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 49 | args[i].appendDecl(fProgramBuilder->glslCaps(), &this->functions()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 50 | if (i < argCnt - 1) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 51 | this->functions().append(", "); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 54 | this->functions().append(") {\n"); |
| 55 | this->functions().append(body); |
| 56 | this->functions().append("}\n\n"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 57 | } |
| 58 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 59 | void GrGLSLShaderBuilder::appendTextureLookup(SkString* out, |
| 60 | const GrGLSLTextureSampler& sampler, |
| 61 | const char* coordName, |
| 62 | GrSLType varyingType) const { |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 63 | const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 64 | GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 65 | GrSLType samplerType = uniformHandler->getUniformVariable(sampler.fSamplerUniform).getType(); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 66 | if (samplerType == kSampler2DRect_GrSLType) { |
| 67 | if (varyingType == kVec2f_GrSLType) { |
| 68 | out->appendf("%s(%s, textureSize(%s) * %s)", |
| 69 | GrGLSLTexture2DFunctionName(varyingType, samplerType, |
| 70 | glslCaps->generation()), |
| 71 | uniformHandler->getUniformCStr(sampler.fSamplerUniform), |
| 72 | uniformHandler->getUniformCStr(sampler.fSamplerUniform), |
| 73 | coordName); |
| 74 | } else { |
| 75 | out->appendf("%s(%s, vec3(textureSize(%s) * %s.xy, %s.z))", |
| 76 | GrGLSLTexture2DFunctionName(varyingType, samplerType, |
| 77 | glslCaps->generation()), |
| 78 | uniformHandler->getUniformCStr(sampler.fSamplerUniform), |
| 79 | uniformHandler->getUniformCStr(sampler.fSamplerUniform), |
| 80 | coordName, |
| 81 | coordName); |
| 82 | } |
| 83 | } else { |
| 84 | out->appendf("%s(%s, %s)", |
| 85 | GrGLSLTexture2DFunctionName(varyingType, samplerType, glslCaps->generation()), |
| 86 | uniformHandler->getUniformCStr(sampler.fSamplerUniform), |
| 87 | coordName); |
| 88 | } |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 89 | |
| 90 | // This refers to any swizzling we may need to get from some backend internal format to the |
| 91 | // format used in GrPixelConfig. If this is implemented by the GrGpu object, then swizzle will |
| 92 | // be rgba. For shader prettiness we omit the swizzle rather than appending ".rgba". |
| 93 | const GrSwizzle& configSwizzle = glslCaps->configTextureSwizzle(sampler.config()); |
| 94 | |
| 95 | if (configSwizzle != GrSwizzle::RGBA()) { |
| 96 | out->appendf(".%s", configSwizzle.c_str()); |
| 97 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 98 | } |
| 99 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 100 | void GrGLSLShaderBuilder::appendTextureLookup(const GrGLSLTextureSampler& sampler, |
| 101 | const char* coordName, |
| 102 | GrSLType varyingType) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 103 | this->appendTextureLookup(&this->code(), sampler, coordName, varyingType); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 104 | } |
| 105 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 106 | void GrGLSLShaderBuilder::appendTextureLookupAndModulate(const char* modulation, |
| 107 | const GrGLSLTextureSampler& sampler, |
| 108 | const char* coordName, |
| 109 | GrSLType varyingType) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 110 | SkString lookup; |
| 111 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
| 112 | this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
| 113 | } |
| 114 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 115 | void GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 116 | if (!(featureBit & fFeaturesAddedMask)) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 117 | this->extensions().appendf("#extension %s: require\n", extensionName); |
| 118 | fFeaturesAddedMask |= featureBit; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 122 | void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 123 | for (int i = 0; i < vars.count(); ++i) { |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 124 | vars[i].appendDecl(fProgramBuilder->glslCaps(), out); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 125 | out->append(";\n"); |
| 126 | } |
| 127 | } |
| 128 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 129 | void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) { |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 130 | SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration || |
| 131 | fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs()); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 132 | fLayoutParams[interface].push_back() = param; |
| 133 | } |
| 134 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 135 | void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() { |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 136 | static const char* interfaceQualifierNames[] = { |
| 137 | "out" |
| 138 | }; |
| 139 | |
| 140 | for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) { |
| 141 | const SkTArray<SkString>& params = fLayoutParams[interface]; |
| 142 | if (params.empty()) { |
| 143 | continue; |
| 144 | } |
| 145 | this->layoutQualifiers().appendf("layout(%s", params[0].c_str()); |
| 146 | for (int i = 1; i < params.count(); ++i) { |
| 147 | this->layoutQualifiers().appendf(", %s", params[i].c_str()); |
| 148 | } |
| 149 | this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]); |
| 150 | } |
| 151 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 152 | GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kOut_InterfaceQualifier); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 153 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1); |
| 154 | } |
| 155 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 156 | void GrGLSLShaderBuilder::finalize(uint32_t visibility) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 157 | SkASSERT(!fFinalized); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 158 | this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString(); |
| 159 | this->compileAndAppendLayoutQualifiers(); |
egdaniel | c8a66eb | 2015-11-02 07:46:25 -0800 | [diff] [blame] | 160 | SkASSERT(visibility); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 161 | fProgramBuilder->appendUniformDecls((GrGLSLUniformHandler::ShaderVisibility) visibility, |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 162 | &this->uniforms()); |
| 163 | this->appendDecls(fInputs, &this->inputs()); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 164 | this->appendDecls(fOutputs, &this->outputs()); |
| 165 | this->onFinalize(); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 166 | // append the 'footer' to code |
| 167 | this->code().append("}"); |
| 168 | |
| 169 | for (int i = 0; i <= fCodeIndex; i++) { |
| 170 | fCompilerStrings[i] = fShaderStrings[i].c_str(); |
| 171 | fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); |
| 172 | } |
| 173 | |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 174 | fFinalized = true; |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 175 | } |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 176 | |