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 | |
| 8 | #include "GrGLShaderBuilder.h" |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 9 | #include "gl/GrGLGpu.h" |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 10 | #include "gl/builders/GrGLProgramBuilder.h" |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 11 | #include "glsl/GrGLSLCaps.h" |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLShaderVar.h" |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 13 | #include "glsl/GrGLSLTextureSampler.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 14 | |
| 15 | namespace { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 16 | void append_texture_lookup(SkString* out, |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 17 | GrGLGpu* gpu, |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 18 | const char* samplerName, |
| 19 | const char* coordName, |
| 20 | uint32_t configComponentMask, |
| 21 | const char* swizzle, |
| 22 | GrSLType varyingType = kVec2f_GrSLType) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 23 | SkASSERT(coordName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 24 | |
| 25 | out->appendf("%s(%s, %s)", |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 26 | GrGLSLTexture2DFunctionName(varyingType, gpu->glslGeneration()), |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 27 | samplerName, |
| 28 | coordName); |
| 29 | |
| 30 | char mangledSwizzle[5]; |
| 31 | |
| 32 | // The swizzling occurs using texture params instead of shader-mangling if ARB_texture_swizzle |
| 33 | // is available. |
| 34 | if (!gpu->glCaps().textureSwizzleSupport() && |
| 35 | (kA_GrColorComponentFlag == configComponentMask)) { |
| 36 | char alphaChar = gpu->glCaps().textureRedSupport() ? 'r' : 'a'; |
| 37 | int i; |
| 38 | for (i = 0; '\0' != swizzle[i]; ++i) { |
| 39 | mangledSwizzle[i] = alphaChar; |
| 40 | } |
| 41 | mangledSwizzle[i] ='\0'; |
| 42 | swizzle = mangledSwizzle; |
| 43 | } |
| 44 | // For shader prettiness we omit the swizzle rather than appending ".rgba". |
| 45 | if (memcmp(swizzle, "rgba", 4)) { |
| 46 | out->appendf(".%s", swizzle); |
| 47 | } |
| 48 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | GrGLShaderBuilder::GrGLShaderBuilder(GrGLProgramBuilder* program) |
| 52 | : fProgramBuilder(program) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 53 | , fInputs(GrGLProgramBuilder::kVarsPerBlock) |
| 54 | , fOutputs(GrGLProgramBuilder::kVarsPerBlock) |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 55 | , fFeaturesAddedMask(0) |
| 56 | , fCodeIndex(kCode) |
| 57 | , fFinalized(false) { |
| 58 | // We push back some dummy pointers which will later become our header |
| 59 | for (int i = 0; i <= kCode; i++) { |
| 60 | fShaderStrings.push_back(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 61 | fCompilerStrings.push_back(nullptr); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 62 | fCompilerStringLengths.push_back(0); |
| 63 | } |
| 64 | |
| 65 | this->main() = "void main() {"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 66 | } |
| 67 | |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 68 | void GrGLShaderBuilder::declAppend(const GrGLSLShaderVar& var) { |
egdaniel | b2f94d1 | 2014-08-29 10:08:36 -0700 | [diff] [blame] | 69 | SkString tempDecl; |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 70 | var.appendDecl(fProgramBuilder->glslCaps(), &tempDecl); |
egdaniel | b2f94d1 | 2014-08-29 10:08:36 -0700 | [diff] [blame] | 71 | this->codeAppendf("%s;", tempDecl.c_str()); |
| 72 | } |
| 73 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 74 | void GrGLShaderBuilder::emitFunction(GrSLType returnType, |
| 75 | const char* name, |
| 76 | int argCnt, |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 77 | const GrGLSLShaderVar* args, |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 78 | const char* body, |
| 79 | SkString* outName) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 80 | this->functions().append(GrGLSLTypeString(returnType)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 81 | fProgramBuilder->nameVariable(outName, '\0', name); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 82 | this->functions().appendf(" %s", outName->c_str()); |
| 83 | this->functions().append("("); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 84 | for (int i = 0; i < argCnt; ++i) { |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 85 | args[i].appendDecl(fProgramBuilder->glslCaps(), &this->functions()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 86 | if (i < argCnt - 1) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 87 | this->functions().append(", "); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 90 | this->functions().append(") {\n"); |
| 91 | this->functions().append(body); |
| 92 | this->functions().append("}\n\n"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 96 | const GrGLSLTextureSampler& sampler, |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 97 | const char* coordName, |
| 98 | GrSLType varyingType) const { |
| 99 | append_texture_lookup(out, |
| 100 | fProgramBuilder->gpu(), |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 101 | fProgramBuilder->getUniformCStr(sampler.fSamplerUniform), |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 102 | coordName, |
| 103 | sampler.configComponentMask(), |
| 104 | sampler.swizzle(), |
| 105 | varyingType); |
| 106 | } |
| 107 | |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 108 | void GrGLShaderBuilder::appendTextureLookup(const GrGLSLTextureSampler& sampler, |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 109 | const char* coordName, |
| 110 | GrSLType varyingType) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 111 | this->appendTextureLookup(&this->code(), sampler, coordName, varyingType); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void GrGLShaderBuilder::appendTextureLookupAndModulate(const char* modulation, |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 115 | const GrGLSLTextureSampler& sampler, |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 116 | const char* coordName, |
| 117 | GrSLType varyingType) { |
| 118 | SkString lookup; |
| 119 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
| 120 | this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
| 121 | } |
| 122 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 123 | void GrGLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) { |
| 124 | if (!(featureBit & fFeaturesAddedMask)) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 125 | this->extensions().appendf("#extension %s: require\n", extensionName); |
| 126 | fFeaturesAddedMask |= featureBit; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 130 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
| 131 | for (int i = 0; i < vars.count(); ++i) { |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 132 | vars[i].appendDecl(fProgramBuilder->glslCaps(), out); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 133 | out->append(";\n"); |
| 134 | } |
| 135 | } |
| 136 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 137 | void GrGLShaderBuilder::appendTextureLookup(const char* samplerName, |
| 138 | const char* coordName, |
| 139 | uint32_t configComponentMask, |
| 140 | const char* swizzle) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 141 | append_texture_lookup(&this->code(), |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 142 | fProgramBuilder->gpu(), |
| 143 | samplerName, |
| 144 | coordName, |
| 145 | configComponentMask, |
| 146 | swizzle, |
| 147 | kVec2f_GrSLType); |
| 148 | } |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 149 | |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 150 | void GrGLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) { |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 151 | SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration || |
| 152 | fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs()); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 153 | fLayoutParams[interface].push_back() = param; |
| 154 | } |
| 155 | |
| 156 | void GrGLShaderBuilder::compileAndAppendLayoutQualifiers() { |
| 157 | static const char* interfaceQualifierNames[] = { |
| 158 | "out" |
| 159 | }; |
| 160 | |
| 161 | for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) { |
| 162 | const SkTArray<SkString>& params = fLayoutParams[interface]; |
| 163 | if (params.empty()) { |
| 164 | continue; |
| 165 | } |
| 166 | this->layoutQualifiers().appendf("layout(%s", params[0].c_str()); |
| 167 | for (int i = 1; i < params.count(); ++i) { |
| 168 | this->layoutQualifiers().appendf(", %s", params[i].c_str()); |
| 169 | } |
| 170 | this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]); |
| 171 | } |
| 172 | |
| 173 | GR_STATIC_ASSERT(0 == GrGLShaderBuilder::kOut_InterfaceQualifier); |
| 174 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1); |
| 175 | } |
| 176 | |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 177 | void GrGLShaderBuilder::finalize(uint32_t visibility) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 178 | SkASSERT(!fFinalized); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 179 | this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString(); |
| 180 | this->compileAndAppendLayoutQualifiers(); |
egdaniel | c8a66eb | 2015-11-02 07:46:25 -0800 | [diff] [blame^] | 181 | SkASSERT(visibility); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 182 | fProgramBuilder->appendUniformDecls((GrGLProgramBuilder::ShaderVisibility) visibility, |
| 183 | &this->uniforms()); |
| 184 | this->appendDecls(fInputs, &this->inputs()); |
egdaniel | c8a66eb | 2015-11-02 07:46:25 -0800 | [diff] [blame^] | 185 | // We should not have any outputs in the fragment shader when using version 1.10 |
| 186 | SkASSERT(GrGLProgramBuilder::kFragment_Visibility != visibility || |
| 187 | k110_GrGLSLGeneration != fProgramBuilder->glslCaps()->generation() || |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 188 | fOutputs.empty()); |
| 189 | this->appendDecls(fOutputs, &this->outputs()); |
| 190 | this->onFinalize(); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 191 | // append the 'footer' to code |
| 192 | this->code().append("}"); |
| 193 | |
| 194 | for (int i = 0; i <= fCodeIndex; i++) { |
| 195 | fCompilerStrings[i] = fShaderStrings[i].c_str(); |
| 196 | fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); |
| 197 | } |
| 198 | |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 199 | fFinalized = true; |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 200 | } |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 201 | |