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 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 8 | #include "GrShaderVar.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 9 | #include "GrShaderCaps.h" |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 10 | #include "GrSwizzle.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 11 | #include "glsl/GrGLSLShaderBuilder.h" |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLColorSpaceXformHelper.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 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 32 | void GrGLSLShaderBuilder::declAppend(const GrShaderVar& var) { |
egdaniel | b2f94d1 | 2014-08-29 10:08:36 -0700 | [diff] [blame] | 33 | SkString tempDecl; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 34 | var.appendDecl(fProgramBuilder->shaderCaps(), &tempDecl); |
egdaniel | b2f94d1 | 2014-08-29 10:08:36 -0700 | [diff] [blame] | 35 | this->codeAppendf("%s;", tempDecl.c_str()); |
| 36 | } |
| 37 | |
csmartdalton | 75864b0 | 2017-02-09 09:47:21 -0500 | [diff] [blame^] | 38 | void GrGLSLShaderBuilder::declareGlobal(const GrShaderVar& v) { |
| 39 | v.appendDecl(this->getProgramBuilder()->shaderCaps(), &this->definitions()); |
| 40 | this->definitions().append(";"); |
| 41 | } |
| 42 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 43 | void GrGLSLShaderBuilder::emitFunction(GrSLType returnType, |
| 44 | const char* name, |
| 45 | int argCnt, |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 46 | const GrShaderVar* args, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 47 | const char* body, |
| 48 | SkString* outName) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 49 | this->functions().append(GrGLSLTypeString(returnType)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 50 | fProgramBuilder->nameVariable(outName, '\0', name); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 51 | this->functions().appendf(" %s", outName->c_str()); |
| 52 | this->functions().append("("); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 53 | for (int i = 0; i < argCnt; ++i) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 54 | args[i].appendDecl(fProgramBuilder->shaderCaps(), &this->functions()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 55 | if (i < argCnt - 1) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 56 | this->functions().append(", "); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 59 | this->functions().append(") {\n"); |
| 60 | this->functions().append(body); |
| 61 | this->functions().append("}\n\n"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 64 | static inline void append_texture_swizzle(SkString* out, GrSwizzle swizzle) { |
| 65 | if (swizzle != GrSwizzle::RGBA()) { |
| 66 | out->appendf(".%s", swizzle.c_str()); |
| 67 | } |
| 68 | } |
| 69 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 70 | void GrGLSLShaderBuilder::appendTextureLookup(SkString* out, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 71 | SamplerHandle samplerHandle, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 72 | const char* coordName, |
| 73 | GrSLType varyingType) const { |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 74 | const GrShaderVar& sampler = fProgramBuilder->samplerVariable(samplerHandle); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 75 | GrSLType samplerType = sampler.getType(); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 76 | if (samplerType == kTexture2DRectSampler_GrSLType) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 77 | if (varyingType == kVec2f_GrSLType) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 78 | out->appendf("texture(%s, textureSize(%s) * %s)", |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 79 | sampler.c_str(), sampler.c_str(), coordName); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 80 | } else { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 81 | out->appendf("texture(%s, vec3(textureSize(%s) * %s.xy, %s.z))", |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 82 | sampler.c_str(), sampler.c_str(), coordName, coordName); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 83 | } |
| 84 | } else { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 85 | out->appendf("texture(%s, %s)", sampler.c_str(), coordName); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 86 | } |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 87 | append_texture_swizzle(out, fProgramBuilder->samplerSwizzle(samplerHandle)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 88 | } |
| 89 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 90 | void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 91 | const char* coordName, |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 92 | GrSLType varyingType, |
| 93 | GrGLSLColorSpaceXformHelper* colorXformHelper) { |
| 94 | if (colorXformHelper && colorXformHelper->getXformMatrix()) { |
| 95 | // With a color gamut transform, we need to wrap the lookup in another function call |
| 96 | SkString lookup; |
| 97 | this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType); |
| 98 | this->appendColorGamutXform(lookup.c_str(), colorXformHelper); |
| 99 | } else { |
| 100 | this->appendTextureLookup(&this->code(), samplerHandle, coordName, varyingType); |
| 101 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 102 | } |
| 103 | |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 104 | void GrGLSLShaderBuilder::appendTextureLookupAndModulate( |
| 105 | const char* modulation, |
| 106 | SamplerHandle samplerHandle, |
| 107 | const char* coordName, |
| 108 | GrSLType varyingType, |
| 109 | GrGLSLColorSpaceXformHelper* colorXformHelper) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 110 | SkString lookup; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 111 | this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType); |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 112 | if (colorXformHelper && colorXformHelper->getXformMatrix()) { |
| 113 | SkString xform; |
| 114 | this->appendColorGamutXform(&xform, lookup.c_str(), colorXformHelper); |
| 115 | this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(xform)).c_str()); |
| 116 | } else { |
| 117 | this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out, |
| 122 | const char* srcColor, |
| 123 | GrGLSLColorSpaceXformHelper* colorXformHelper) { |
| 124 | // Our color is (r, g, b, a), but we want to multiply (r, g, b, 1) by our matrix, then |
| 125 | // re-insert the original alpha. The supplied srcColor is likely to be of the form |
brianosman | 5192475 | 2016-09-12 08:50:19 -0700 | [diff] [blame] | 126 | // "texture(...)", and we don't want to evaluate that twice, so wrap everything in a function. |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 127 | static const GrShaderVar gColorGamutXformArgs[] = { |
| 128 | GrShaderVar("color", kVec4f_GrSLType), |
| 129 | GrShaderVar("xform", kMat44f_GrSLType), |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 130 | }; |
| 131 | SkString functionBody; |
Brian Osman | 0bd783f | 2017-01-04 12:54:07 -0500 | [diff] [blame] | 132 | // Gamut xform, clamp to destination gamut. We only support/have premultiplied textures, so we |
| 133 | // always just clamp to alpha. |
| 134 | functionBody.append("\tcolor.rgb = clamp((xform * vec4(color.rgb, 1.0)).rgb, 0.0, color.a);\n"); |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 135 | functionBody.append("\treturn color;"); |
| 136 | SkString colorGamutXformFuncName; |
| 137 | this->emitFunction(kVec4f_GrSLType, |
| 138 | "colorGamutXform", |
| 139 | SK_ARRAY_COUNT(gColorGamutXformArgs), |
| 140 | gColorGamutXformArgs, |
| 141 | functionBody.c_str(), |
| 142 | &colorGamutXformFuncName); |
| 143 | |
| 144 | out->appendf("%s(%s, %s)", colorGamutXformFuncName.c_str(), srcColor, |
| 145 | colorXformHelper->getXformMatrix()); |
| 146 | } |
| 147 | |
| 148 | void GrGLSLShaderBuilder::appendColorGamutXform(const char* srcColor, |
| 149 | GrGLSLColorSpaceXformHelper* colorXformHelper) { |
| 150 | SkString xform; |
| 151 | this->appendColorGamutXform(&xform, srcColor, colorXformHelper); |
| 152 | this->codeAppend(xform.c_str()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 153 | } |
| 154 | |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 155 | void GrGLSLShaderBuilder::appendTexelFetch(SkString* out, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 156 | SamplerHandle samplerHandle, |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 157 | const char* coordExpr) const { |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 158 | const GrShaderVar& sampler = fProgramBuilder->samplerVariable(samplerHandle); |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 159 | SkASSERT(fProgramBuilder->shaderCaps()->texelFetchSupport()); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 160 | SkASSERT(GrSLTypeIsCombinedSamplerType(sampler.getType())); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 161 | |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 162 | out->appendf("texelFetch(%s, %s)", sampler.c_str(), coordExpr); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 163 | |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 164 | append_texture_swizzle(out, fProgramBuilder->samplerSwizzle(samplerHandle)); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 165 | } |
| 166 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 167 | void GrGLSLShaderBuilder::appendTexelFetch(SamplerHandle samplerHandle, const char* coordExpr) { |
| 168 | this->appendTexelFetch(&this->code(), samplerHandle, coordExpr); |
cdalton | f8a6ce8 | 2016-04-11 13:02:05 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 171 | void GrGLSLShaderBuilder::appendImageStorageLoad(SkString* out, ImageStorageHandle handle, |
| 172 | const char* coordExpr) { |
| 173 | const GrShaderVar& imageStorage = fProgramBuilder->imageStorageVariable(handle); |
| 174 | out->appendf("imageLoad(%s, %s)", imageStorage.c_str(), coordExpr); |
| 175 | } |
| 176 | |
| 177 | void GrGLSLShaderBuilder::appendImageStorageLoad(ImageStorageHandle handle, const char* coordExpr) { |
| 178 | this->appendImageStorageLoad(&this->code(), handle, coordExpr); |
| 179 | } |
| 180 | |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 181 | bool GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) { |
| 182 | if (featureBit & fFeaturesAddedMask) { |
| 183 | return false; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 184 | } |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 185 | this->extensions().appendf("#extension %s: require\n", extensionName); |
| 186 | fFeaturesAddedMask |= featureBit; |
| 187 | return true; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 188 | } |
| 189 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 190 | void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 191 | for (int i = 0; i < vars.count(); ++i) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 192 | vars[i].appendDecl(fProgramBuilder->shaderCaps(), out); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 193 | out->append(";\n"); |
| 194 | } |
| 195 | } |
| 196 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 197 | void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 198 | SkASSERT(fProgramBuilder->shaderCaps()->generation() >= k330_GrGLSLGeneration || |
| 199 | fProgramBuilder->shaderCaps()->mustEnableAdvBlendEqs()); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 200 | fLayoutParams[interface].push_back() = param; |
| 201 | } |
| 202 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 203 | void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() { |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 204 | static const char* interfaceQualifierNames[] = { |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 205 | "in", |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 206 | "out" |
| 207 | }; |
| 208 | |
| 209 | for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) { |
| 210 | const SkTArray<SkString>& params = fLayoutParams[interface]; |
| 211 | if (params.empty()) { |
| 212 | continue; |
| 213 | } |
| 214 | this->layoutQualifiers().appendf("layout(%s", params[0].c_str()); |
| 215 | for (int i = 1; i < params.count(); ++i) { |
| 216 | this->layoutQualifiers().appendf(", %s", params[i].c_str()); |
| 217 | } |
| 218 | this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]); |
| 219 | } |
| 220 | |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 221 | GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kIn_InterfaceQualifier); |
| 222 | GR_STATIC_ASSERT(1 == GrGLSLShaderBuilder::kOut_InterfaceQualifier); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 223 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1); |
| 224 | } |
| 225 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 226 | void GrGLSLShaderBuilder::finalize(uint32_t visibility) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 227 | SkASSERT(!fFinalized); |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 228 | this->versionDecl() = fProgramBuilder->shaderCaps()->versionDeclString(); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 229 | this->compileAndAppendLayoutQualifiers(); |
egdaniel | c8a66eb | 2015-11-02 07:46:25 -0800 | [diff] [blame] | 230 | SkASSERT(visibility); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 231 | fProgramBuilder->appendUniformDecls((GrShaderFlags) visibility, &this->uniforms()); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 232 | this->appendDecls(fInputs, &this->inputs()); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 233 | this->appendDecls(fOutputs, &this->outputs()); |
| 234 | this->onFinalize(); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 235 | // append the 'footer' to code |
| 236 | this->code().append("}"); |
| 237 | |
| 238 | for (int i = 0; i <= fCodeIndex; i++) { |
| 239 | fCompilerStrings[i] = fShaderStrings[i].c_str(); |
| 240 | fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); |
| 241 | } |
| 242 | |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 243 | fFinalized = true; |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 244 | } |