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" |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame^] | 11 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLShaderVar.h" |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 13 | #include "glsl/GrGLSLSampler.h" |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLProgramBuilder.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 15 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 16 | GrGLSLShaderBuilder::GrGLSLShaderBuilder(GrGLSLProgramBuilder* program) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 17 | : fProgramBuilder(program) |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 18 | , fInputs(GrGLSLProgramBuilder::kVarsPerBlock) |
| 19 | , fOutputs(GrGLSLProgramBuilder::kVarsPerBlock) |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 20 | , fFeaturesAddedMask(0) |
| 21 | , fCodeIndex(kCode) |
| 22 | , fFinalized(false) { |
| 23 | // We push back some dummy pointers which will later become our header |
| 24 | for (int i = 0; i <= kCode; i++) { |
| 25 | fShaderStrings.push_back(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 26 | fCompilerStrings.push_back(nullptr); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 27 | fCompilerStringLengths.push_back(0); |
| 28 | } |
| 29 | |
| 30 | this->main() = "void main() {"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 31 | } |
| 32 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 33 | void GrGLSLShaderBuilder::declAppend(const GrGLSLShaderVar& var) { |
egdaniel | b2f94d1 | 2014-08-29 10:08:36 -0700 | [diff] [blame] | 34 | SkString tempDecl; |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 35 | var.appendDecl(fProgramBuilder->glslCaps(), &tempDecl); |
egdaniel | b2f94d1 | 2014-08-29 10:08:36 -0700 | [diff] [blame] | 36 | this->codeAppendf("%s;", tempDecl.c_str()); |
| 37 | } |
| 38 | |
cdalton | 1f50acf | 2016-04-11 11:30:50 -0700 | [diff] [blame] | 39 | void GrGLSLShaderBuilder::appendPrecisionModifier(GrSLPrecision precision) { |
| 40 | if (fProgramBuilder->glslCaps()->usesPrecisionModifiers()) { |
| 41 | this->codeAppendf("%s ", GrGLSLPrecisionString(precision)); |
| 42 | } |
| 43 | } |
| 44 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 45 | void GrGLSLShaderBuilder::emitFunction(GrSLType returnType, |
| 46 | const char* name, |
| 47 | int argCnt, |
| 48 | const GrGLSLShaderVar* args, |
| 49 | const char* body, |
| 50 | SkString* outName) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 51 | this->functions().append(GrGLSLTypeString(returnType)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 52 | fProgramBuilder->nameVariable(outName, '\0', name); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 53 | this->functions().appendf(" %s", outName->c_str()); |
| 54 | this->functions().append("("); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 55 | for (int i = 0; i < argCnt; ++i) { |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 56 | args[i].appendDecl(fProgramBuilder->glslCaps(), &this->functions()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 57 | if (i < argCnt - 1) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 58 | this->functions().append(", "); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 61 | this->functions().append(") {\n"); |
| 62 | this->functions().append(body); |
| 63 | this->functions().append("}\n\n"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 64 | } |
| 65 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 66 | void GrGLSLShaderBuilder::appendTextureLookup(SkString* out, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 67 | SamplerHandle samplerHandle, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 68 | const char* coordName, |
| 69 | GrSLType varyingType) const { |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 70 | const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps(); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 71 | const GrGLSLSampler& sampler = fProgramBuilder->getSampler(samplerHandle); |
| 72 | GrSLType samplerType = sampler.type(); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 73 | if (samplerType == kTexture2DRectSampler_GrSLType) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 74 | if (varyingType == kVec2f_GrSLType) { |
| 75 | out->appendf("%s(%s, textureSize(%s) * %s)", |
| 76 | GrGLSLTexture2DFunctionName(varyingType, samplerType, |
| 77 | glslCaps->generation()), |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 78 | sampler.getSamplerNameForTexture2D(), |
| 79 | sampler.getSamplerNameForTexture2D(), |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 80 | coordName); |
| 81 | } else { |
| 82 | out->appendf("%s(%s, vec3(textureSize(%s) * %s.xy, %s.z))", |
| 83 | GrGLSLTexture2DFunctionName(varyingType, samplerType, |
| 84 | glslCaps->generation()), |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 85 | sampler.getSamplerNameForTexture2D(), |
| 86 | sampler.getSamplerNameForTexture2D(), |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 87 | coordName, |
| 88 | coordName); |
| 89 | } |
| 90 | } else { |
| 91 | out->appendf("%s(%s, %s)", |
| 92 | GrGLSLTexture2DFunctionName(varyingType, samplerType, glslCaps->generation()), |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 93 | sampler.getSamplerNameForTexture2D(), |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 94 | coordName); |
| 95 | } |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 96 | |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 97 | this->appendTextureSwizzle(out, sampler.config()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 98 | } |
| 99 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 100 | void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 101 | const char* coordName, |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame^] | 102 | GrSLType varyingType, |
| 103 | GrGLSLColorSpaceXformHelper* colorXformHelper) { |
| 104 | if (colorXformHelper && colorXformHelper->getXformMatrix()) { |
| 105 | // With a color gamut transform, we need to wrap the lookup in another function call |
| 106 | SkString lookup; |
| 107 | this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType); |
| 108 | this->appendColorGamutXform(lookup.c_str(), colorXformHelper); |
| 109 | } else { |
| 110 | this->appendTextureLookup(&this->code(), samplerHandle, coordName, varyingType); |
| 111 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 112 | } |
| 113 | |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame^] | 114 | void GrGLSLShaderBuilder::appendTextureLookupAndModulate( |
| 115 | const char* modulation, |
| 116 | SamplerHandle samplerHandle, |
| 117 | const char* coordName, |
| 118 | GrSLType varyingType, |
| 119 | GrGLSLColorSpaceXformHelper* colorXformHelper) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 120 | SkString lookup; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 121 | this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType); |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame^] | 122 | if (colorXformHelper && colorXformHelper->getXformMatrix()) { |
| 123 | SkString xform; |
| 124 | this->appendColorGamutXform(&xform, lookup.c_str(), colorXformHelper); |
| 125 | this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(xform)).c_str()); |
| 126 | } else { |
| 127 | this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out, |
| 132 | const char* srcColor, |
| 133 | GrGLSLColorSpaceXformHelper* colorXformHelper) { |
| 134 | // Our color is (r, g, b, a), but we want to multiply (r, g, b, 1) by our matrix, then |
| 135 | // re-insert the original alpha. The supplied srcColor is likely to be of the form |
| 136 | // "texture(...)", and we don't want to evaluate that twice. |
| 137 | // |
| 138 | // Worse: We can't do the transformation on premultiplied colors, so if the source is premul, |
| 139 | // we need to unpremul, transform, then multiply again. Anyways, we wrap all of the work in a |
| 140 | // function. |
| 141 | static const GrGLSLShaderVar gColorGamutXformArgs[] = { |
| 142 | GrGLSLShaderVar("color", kVec4f_GrSLType), |
| 143 | GrGLSLShaderVar("xform", kMat44f_GrSLType), |
| 144 | }; |
| 145 | SkString functionBody; |
| 146 | if (kPremul_SkAlphaType == colorXformHelper->alphaType()) { |
| 147 | // Unpremultiply |
| 148 | functionBody.append("\tfloat nonZeroAlpha = max(color.a, 0.00001);\n" |
| 149 | "\tcolor.rgb = color.rgb / nonZeroAlpha;\n"); |
| 150 | } |
| 151 | // Gamut xform, clamp to destination gamut |
| 152 | functionBody.append("\tcolor.rgb = clamp((vec4(color.rgb, 1.0) * xform).rgb, 0.0, 1.0);\n"); |
| 153 | if (kPremul_SkAlphaType == colorXformHelper->alphaType()) { |
| 154 | // Re-multiply by alpha |
| 155 | functionBody.append("\tcolor.rgb = color.rgb * nonZeroAlpha;\n"); |
| 156 | } |
| 157 | functionBody.append("\treturn color;"); |
| 158 | SkString colorGamutXformFuncName; |
| 159 | this->emitFunction(kVec4f_GrSLType, |
| 160 | "colorGamutXform", |
| 161 | SK_ARRAY_COUNT(gColorGamutXformArgs), |
| 162 | gColorGamutXformArgs, |
| 163 | functionBody.c_str(), |
| 164 | &colorGamutXformFuncName); |
| 165 | |
| 166 | out->appendf("%s(%s, %s)", colorGamutXformFuncName.c_str(), srcColor, |
| 167 | colorXformHelper->getXformMatrix()); |
| 168 | } |
| 169 | |
| 170 | void GrGLSLShaderBuilder::appendColorGamutXform(const char* srcColor, |
| 171 | GrGLSLColorSpaceXformHelper* colorXformHelper) { |
| 172 | SkString xform; |
| 173 | this->appendColorGamutXform(&xform, srcColor, colorXformHelper); |
| 174 | this->codeAppend(xform.c_str()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 175 | } |
| 176 | |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 177 | void GrGLSLShaderBuilder::appendTexelFetch(SkString* out, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 178 | SamplerHandle samplerHandle, |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 179 | const char* coordExpr) const { |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 180 | const GrGLSLSampler& sampler = fProgramBuilder->getSampler(samplerHandle); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 181 | SkASSERT(fProgramBuilder->glslCaps()->texelFetchSupport()); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 182 | SkASSERT(GrSLTypeIsCombinedSamplerType(sampler.type())); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 183 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 184 | out->appendf("texelFetch(%s, %s)", sampler.getSamplerNameForTexelFetch(), coordExpr); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 185 | |
| 186 | this->appendTextureSwizzle(out, sampler.config()); |
| 187 | } |
| 188 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 189 | void GrGLSLShaderBuilder::appendTexelFetch(SamplerHandle samplerHandle, const char* coordExpr) { |
| 190 | this->appendTexelFetch(&this->code(), samplerHandle, coordExpr); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void GrGLSLShaderBuilder::appendTextureSwizzle(SkString* out, GrPixelConfig config) const { |
| 194 | const GrSwizzle& configSwizzle = fProgramBuilder->glslCaps()->configTextureSwizzle(config); |
| 195 | |
| 196 | if (configSwizzle != GrSwizzle::RGBA()) { |
| 197 | out->appendf(".%s", configSwizzle.c_str()); |
| 198 | } |
| 199 | } |
| 200 | |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 201 | bool GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) { |
| 202 | if (featureBit & fFeaturesAddedMask) { |
| 203 | return false; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 204 | } |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 205 | this->extensions().appendf("#extension %s: require\n", extensionName); |
| 206 | fFeaturesAddedMask |= featureBit; |
| 207 | return true; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 208 | } |
| 209 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 210 | void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 211 | for (int i = 0; i < vars.count(); ++i) { |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 212 | vars[i].appendDecl(fProgramBuilder->glslCaps(), out); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 213 | out->append(";\n"); |
| 214 | } |
| 215 | } |
| 216 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 217 | void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) { |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 218 | SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration || |
| 219 | fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs()); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 220 | fLayoutParams[interface].push_back() = param; |
| 221 | } |
| 222 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 223 | void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() { |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 224 | static const char* interfaceQualifierNames[] = { |
| 225 | "out" |
| 226 | }; |
| 227 | |
| 228 | for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) { |
| 229 | const SkTArray<SkString>& params = fLayoutParams[interface]; |
| 230 | if (params.empty()) { |
| 231 | continue; |
| 232 | } |
| 233 | this->layoutQualifiers().appendf("layout(%s", params[0].c_str()); |
| 234 | for (int i = 1; i < params.count(); ++i) { |
| 235 | this->layoutQualifiers().appendf(", %s", params[i].c_str()); |
| 236 | } |
| 237 | this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]); |
| 238 | } |
| 239 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 240 | GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kOut_InterfaceQualifier); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 241 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1); |
| 242 | } |
| 243 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 244 | void GrGLSLShaderBuilder::finalize(uint32_t visibility) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 245 | SkASSERT(!fFinalized); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 246 | this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString(); |
| 247 | this->compileAndAppendLayoutQualifiers(); |
egdaniel | c8a66eb | 2015-11-02 07:46:25 -0800 | [diff] [blame] | 248 | SkASSERT(visibility); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 249 | fProgramBuilder->appendUniformDecls((GrShaderFlags) visibility, &this->uniforms()); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 250 | this->appendDecls(fInputs, &this->inputs()); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 251 | this->appendDecls(fOutputs, &this->outputs()); |
| 252 | this->onFinalize(); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 253 | // append the 'footer' to code |
| 254 | this->code().append("}"); |
| 255 | |
| 256 | for (int i = 0; i <= fCodeIndex; i++) { |
| 257 | fCompilerStrings[i] = fShaderStrings[i].c_str(); |
| 258 | fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); |
| 259 | } |
| 260 | |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 261 | fFinalized = true; |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 262 | } |