joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrCoordTransform.h" |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrPipeline.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 13 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
| 14 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 15 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 16 | |
Ethan Nicholas | d3a95c2 | 2020-06-03 13:24:46 -0400 | [diff] [blame] | 17 | #include <unordered_map> |
| 18 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 19 | void GrGLSLGeometryProcessor::emitCode(EmitArgs& args) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 20 | GrGPArgs gpArgs; |
| 21 | this->onEmitCode(args, &gpArgs); |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 22 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 23 | // FIXME This must always be called at the moment, even when fLocalCoordVar is uninitialized |
| 24 | // and void because collectTransforms registers the uniforms for legacy coord transforms, which |
| 25 | // still need to be added even if the FPs are sampled explicitly. When they are gone, we only |
| 26 | // need to call this if the local coord isn't void (plus verify that FPs really don't need it). |
| 27 | this->collectTransforms(args.fVertBuilder, args.fVaryingHandler, args.fUniformHandler, |
| 28 | gpArgs.fLocalCoordVar, args.fFPCoordTransformHandler); |
| 29 | |
Chris Dalton | 5a2f962 | 2019-12-27 14:56:38 -0700 | [diff] [blame] | 30 | if (args.fGP.willUseTessellationShaders()) { |
| 31 | // Tessellation shaders are temporarily responsible for integrating their own code strings |
| 32 | // while we work out full support. |
| 33 | return; |
| 34 | } |
| 35 | |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 36 | GrGLSLVertexBuilder* vBuilder = args.fVertBuilder; |
| 37 | if (!args.fGP.willUseGeoShader()) { |
| 38 | // Emit the vertex position to the hardware in the normalized window coordinates it expects. |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 39 | SkASSERT(kFloat2_GrSLType == gpArgs.fPositionVar.getType() || |
| 40 | kFloat3_GrSLType == gpArgs.fPositionVar.getType()); |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 41 | vBuilder->emitNormalizedSkPosition(gpArgs.fPositionVar.c_str(), args.fRTAdjustName, |
| 42 | gpArgs.fPositionVar.getType()); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 43 | if (kFloat2_GrSLType == gpArgs.fPositionVar.getType()) { |
| 44 | args.fVaryingHandler->setNoPerspective(); |
| 45 | } |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 46 | } else { |
| 47 | // Since we have a geometry shader, leave the vertex position in Skia device space for now. |
| 48 | // The geometry Shader will operate in device space, and then convert the final positions to |
| 49 | // normalized hardware window coordinates under the hood, once everything else has finished. |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 50 | // The subclass must call setNoPerspective on the varying handler, if applicable. |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 51 | vBuilder->codeAppendf("sk_Position = float4(%s", gpArgs.fPositionVar.c_str()); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 52 | switch (gpArgs.fPositionVar.getType()) { |
| 53 | case kFloat_GrSLType: |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 54 | vBuilder->codeAppend(", 0"); |
| 55 | [[fallthrough]]; |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 56 | case kFloat2_GrSLType: |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 57 | vBuilder->codeAppend(", 0"); |
| 58 | [[fallthrough]]; |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 59 | case kFloat3_GrSLType: |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 60 | vBuilder->codeAppend(", 1"); |
| 61 | [[fallthrough]]; |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 62 | case kFloat4_GrSLType: |
| 63 | vBuilder->codeAppend(");"); |
| 64 | break; |
| 65 | default: |
| 66 | SK_ABORT("Invalid position var type"); |
| 67 | break; |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 68 | } |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 69 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 72 | void GrGLSLGeometryProcessor::collectTransforms(GrGLSLVertexBuilder* vb, |
| 73 | GrGLSLVaryingHandler* varyingHandler, |
| 74 | GrGLSLUniformHandler* uniformHandler, |
| 75 | const GrShaderVar& localCoordsVar, |
| 76 | FPCoordTransformHandler* handler) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 77 | SkASSERT(localCoordsVar.getType() == kFloat2_GrSLType || |
| 78 | localCoordsVar.getType() == kFloat3_GrSLType || |
| 79 | localCoordsVar.getType() == kVoid_GrSLType /* until coord transforms are gone */); |
| 80 | // Cached varyings produced by parent FPs. If parent FPs introduce transformations, but all |
| 81 | // subsequent children are not transformed, they should share the same varying. |
| 82 | std::unordered_map<const GrFragmentProcessor*, GrShaderVar> localCoordsMap; |
| 83 | |
| 84 | GrGLSLVarying baseLocalCoord; |
| 85 | auto getBaseLocalCoord = [&baseLocalCoord, &localCoordsVar, vb, varyingHandler]() { |
| 86 | SkASSERT(GrSLTypeIsFloatType(localCoordsVar.getType())); |
| 87 | if (baseLocalCoord.type() == kVoid_GrSLType) { |
| 88 | // Initialize to the GP provided coordinate |
| 89 | SkString baseLocalCoordName = SkStringPrintf("LocalCoord"); |
| 90 | baseLocalCoord = GrGLSLVarying(localCoordsVar.getType()); |
| 91 | varyingHandler->addVarying(baseLocalCoordName.c_str(), &baseLocalCoord); |
| 92 | vb->codeAppendf("%s = %s;\n", baseLocalCoord.vsOut(), |
| 93 | localCoordsVar.getName().c_str()); |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 94 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 95 | return GrShaderVar(SkString(baseLocalCoord.fsIn()), baseLocalCoord.type(), |
| 96 | GrShaderVar::TypeModifier::In); |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 97 | }; |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 98 | |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 99 | for (int i = 0; *handler; ++*handler, ++i) { |
| 100 | auto [coordTransform, fp] = handler->get(); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 101 | |
| 102 | // FPs that use the legacy coord transform system will need a uniform registered for them |
| 103 | // to hold the coord transform's matrix. |
| 104 | GrShaderVar transformVar; |
| 105 | // FPs that use local coordinates need a varying to convey the coordinate. This may be the |
| 106 | // base GP's local coord if transforms have to be computed in the FS, or it may be a unique |
| 107 | // varying that computes the equivalent transformation hierarchy in the VS. |
| 108 | GrShaderVar varyingVar; |
| 109 | |
| 110 | // If this is true, the FP's signature takes a float2 local coordinate. Otherwise, it |
| 111 | // doesn't use local coordinates, or it can be lifted to a varying and referenced directly. |
| 112 | bool localCoordComputedInFS = fp.isSampledWithExplicitCoords(); |
| 113 | if (!coordTransform.isNoOp()) { |
| 114 | // Legacy coord transform that actually is doing something. This matrix is the last |
| 115 | // transformation to affect the local coordinate. |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 116 | SkString strUniName; |
| 117 | strUniName.printf("CoordTransformMatrix_%d", i); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 118 | auto flag = localCoordComputedInFS ? kFragment_GrShaderFlag |
| 119 | : kVertex_GrShaderFlag; |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 120 | auto& uni = fInstalledTransforms.push_back(); |
Brian Salomon | 14ed885 | 2020-03-24 10:43:38 -0400 | [diff] [blame] | 121 | if (fp.isSampledWithExplicitCoords() && coordTransform.matrix().isScaleTranslate()) { |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 122 | uni.fType = kFloat4_GrSLType; |
| 123 | } else { |
| 124 | uni.fType = kFloat3x3_GrSLType; |
| 125 | } |
| 126 | uni.fHandle = |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 127 | uniformHandler->addUniform(&fp, flag, uni.fType, strUniName.c_str()); |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 128 | transformVar = uniformHandler->getUniformVariable(uni.fHandle); |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 129 | } else { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 130 | // Must stay parallel with calls to handler |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 131 | fInstalledTransforms.push_back(); |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 134 | // If the FP references local coords, we need to make sure the vertex shader sets up the |
| 135 | // right transforms or pass-through variables for the FP to evaluate in the fragment shader |
| 136 | if (fp.referencesSampleCoords()) { |
| 137 | if (localCoordComputedInFS) { |
| 138 | // If the FP local coords are evaluated in the fragment shader, we only need to |
| 139 | // produce the original local coordinate to pass into the root; any other situation, |
| 140 | // the FP will have a 2nd parameter to its function and the caller sends the coords |
| 141 | if (!fp.parent()) { |
| 142 | varyingVar = getBaseLocalCoord(); |
| 143 | } |
| 144 | } else { |
| 145 | // The FP's local coordinates are determined by the const/uniform transform |
| 146 | // hierarchy from this FP to the root, and can be computed in the vertex shader. |
| 147 | // If this hierarchy would be the identity transform, then we should use the |
| 148 | // original local coordinate. |
| 149 | // NOTE: The actual transform logic is handled in emitTransformCode(), this just |
| 150 | // needs to determine if a unique varying should be added for the FP. |
| 151 | GrShaderVar transformedLocalCoord; |
| 152 | const GrFragmentProcessor* coordOwner = nullptr; |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 153 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 154 | const GrFragmentProcessor* node = &fp; |
| 155 | while(node) { |
| 156 | SkASSERT(!node->isSampledWithExplicitCoords() && |
| 157 | (node->sampleMatrix().isNoOp() || |
| 158 | node->sampleMatrix().isConstUniform())); |
| 159 | |
| 160 | if (node->sampleMatrix().isConstUniform()) { |
| 161 | // We can stop once we hit an FP that adds transforms; this FP can reuse |
| 162 | // that FPs varying (possibly vivifying it if this was the first use). |
| 163 | transformedLocalCoord = localCoordsMap[node]; |
| 164 | coordOwner = node; |
| 165 | break; |
| 166 | } // else intervening FP is an identity transform so skip past it |
| 167 | |
| 168 | node = node->parent(); |
| 169 | } |
| 170 | |
| 171 | // Legacy coord transform workaround (if the transform hierarchy appears identity |
| 172 | // but we have GrCoordTransform that does something, we still need to record a |
| 173 | // varying for it). |
| 174 | if (!coordOwner && !coordTransform.isNoOp()) { |
| 175 | coordOwner = &fp; |
| 176 | } |
| 177 | |
| 178 | if (coordOwner) { |
| 179 | // The FP will use coordOwner's varying; add varying if this was the first use |
| 180 | if (transformedLocalCoord.getType() == kVoid_GrSLType) { |
| 181 | GrGLSLVarying v(kFloat2_GrSLType); |
| 182 | if (coordTransform.matrix().hasPerspective() || |
| 183 | GrSLTypeVecLength(localCoordsVar.getType()) == 3 || |
| 184 | coordOwner->hasPerspectiveTransform()) { |
| 185 | v = GrGLSLVarying(kFloat3_GrSLType); |
| 186 | } |
| 187 | SkString strVaryingName; |
| 188 | strVaryingName.printf("TransformedCoords_%d", i); |
| 189 | varyingHandler->addVarying(strVaryingName.c_str(), &v); |
| 190 | |
| 191 | fTransformInfos.push_back({GrShaderVar(v.vsOut(), v.type()), |
| 192 | transformVar.getName(), |
| 193 | localCoordsVar, |
| 194 | coordOwner}); |
| 195 | transformedLocalCoord = GrShaderVar(SkString(v.fsIn()), v.type(), |
| 196 | GrShaderVar::TypeModifier::In); |
| 197 | if (coordOwner->numCoordTransforms() < 1 || |
| 198 | coordOwner->coordTransform(0).isNoOp()) { |
| 199 | // As long as a legacy coord transform doesn't get in the way, we can |
| 200 | // reuse this expression for children (see comment in emitTransformCode) |
| 201 | localCoordsMap[coordOwner] = transformedLocalCoord; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | varyingVar = transformedLocalCoord; |
Ethan Nicholas | bffad83 | 2020-05-05 14:31:55 -0400 | [diff] [blame] | 206 | } else { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 207 | // The FP transform hierarchy is the identity, so use the original local coord |
| 208 | varyingVar = getBaseLocalCoord(); |
Ethan Nicholas | bffad83 | 2020-05-05 14:31:55 -0400 | [diff] [blame] | 209 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 210 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 211 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 212 | |
| 213 | if (varyingVar.getType() != kVoid_GrSLType || transformVar.getType() != kVoid_GrSLType) { |
| 214 | handler->specifyCoordsForCurrCoordTransform(transformVar, varyingVar); |
| 215 | } else { |
| 216 | handler->omitCoordsForCurrCoordTransform(); |
| 217 | } |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | void GrGLSLGeometryProcessor::emitTransformCode(GrGLSLVertexBuilder* vb, |
| 222 | GrGLSLUniformHandler* uniformHandler) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 223 | std::unordered_map<const GrFragmentProcessor*, GrShaderVar> localCoordsMap; |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 224 | for (const auto& tr : fTransformInfos) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 225 | // If we recorded a transform info, its sample matrix must be const/uniform, or we have a |
| 226 | // legacy coord transform that actually does something. |
| 227 | SkASSERT(tr.fFP->sampleMatrix().isConstUniform() || |
| 228 | (tr.fFP->sampleMatrix().isNoOp() && !tr.fMatrix.isEmpty())); |
| 229 | |
| 230 | SkString localCoords; |
| 231 | // Build a concatenated matrix expression that we apply to the root local coord. |
| 232 | // If we have an expression cached from an early FP in the hierarchy chain, we can stop |
| 233 | // there instead of going all the way to the GP. |
| 234 | SkString transformExpression; |
| 235 | if (!tr.fMatrix.isEmpty()) { |
| 236 | // We have both a const/uniform sample matrix and a legacy coord transform |
| 237 | transformExpression.printf("%s", tr.fMatrix.c_str()); |
| 238 | } |
| 239 | |
| 240 | // If the sample matrix is kNone, then the current transform expression of just the |
| 241 | // coord transform matrix is sufficient. |
| 242 | if (tr.fFP->sampleMatrix().isConstUniform()) { |
| 243 | const auto* base = tr.fFP; |
| 244 | while(base) { |
| 245 | GrShaderVar cachedBaseCoord = localCoordsMap[base]; |
| 246 | if (cachedBaseCoord.getType() != kVoid_GrSLType) { |
| 247 | // Can stop here, as this varying already holds all transforms from higher FPs |
| 248 | if (cachedBaseCoord.getType() == kFloat3_GrSLType) { |
| 249 | localCoords = cachedBaseCoord.getName(); |
| 250 | } else { |
| 251 | localCoords = SkStringPrintf("%s.xy1", cachedBaseCoord.getName().c_str()); |
| 252 | } |
| 253 | break; |
| 254 | } else if (base->sampleMatrix().isConstUniform()) { |
| 255 | // The FP knows the matrix expression it's sampled with, but its parent defined |
| 256 | // the uniform (when the expression is not a constant). |
| 257 | GrShaderVar uniform = uniformHandler->liftUniformToVertexShader( |
Brian Osman | 061a5cf | 2020-06-24 14:50:25 -0400 | [diff] [blame^] | 258 | *base->parent(), SkString(base->sampleMatrix().fExpression)); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 259 | |
| 260 | // Accumulate the base matrix expression as a preConcat |
| 261 | SkString matrix; |
| 262 | if (uniform.getType() != kVoid_GrSLType) { |
| 263 | SkASSERT(uniform.getType() == kFloat3x3_GrSLType); |
| 264 | matrix = uniform.getName(); |
| 265 | } else { |
| 266 | // No uniform found, so presumably this is a constant |
Brian Osman | 061a5cf | 2020-06-24 14:50:25 -0400 | [diff] [blame^] | 267 | matrix = SkString(base->sampleMatrix().fExpression); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | if (!transformExpression.isEmpty()) { |
| 271 | transformExpression.append(" * "); |
| 272 | } |
| 273 | transformExpression.appendf("(%s)", matrix.c_str()); |
Ethan Nicholas | d3a95c2 | 2020-06-03 13:24:46 -0400 | [diff] [blame] | 274 | } else { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 275 | // This intermediate FP is just a pass through and doesn't need to be built |
| 276 | // in to the expression, but must visit its parents in case they add transforms |
| 277 | SkASSERT(base->sampleMatrix().isNoOp()); |
Ethan Nicholas | d3a95c2 | 2020-06-03 13:24:46 -0400 | [diff] [blame] | 278 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 279 | |
| 280 | base = base->parent(); |
Ethan Nicholas | d3a95c2 | 2020-06-03 13:24:46 -0400 | [diff] [blame] | 281 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | if (localCoords.isEmpty()) { |
| 285 | // Must use GP's local coords |
| 286 | if (tr.fLocalCoords.getType() == kFloat3_GrSLType) { |
| 287 | localCoords = tr.fLocalCoords.getName(); |
| 288 | } else { |
| 289 | localCoords = SkStringPrintf("%s.xy1", tr.fLocalCoords.getName().c_str()); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | vb->codeAppend("{\n"); |
| 294 | if (tr.fOutputCoords.getType() == kFloat2_GrSLType) { |
| 295 | vb->codeAppendf("%s = ((%s) * %s).xy", tr.fOutputCoords.getName().c_str(), |
| 296 | transformExpression.c_str(), |
| 297 | localCoords.c_str()); |
| 298 | } else { |
| 299 | SkASSERT(tr.fOutputCoords.getType() == kFloat3_GrSLType); |
| 300 | vb->codeAppendf("%s = (%s) * %s", tr.fOutputCoords.getName().c_str(), |
| 301 | transformExpression.c_str(), |
| 302 | localCoords.c_str()); |
| 303 | } |
| 304 | vb->codeAppend(";\n"); |
| 305 | vb->codeAppend("}\n"); |
| 306 | |
| 307 | if (tr.fMatrix.isEmpty()) { |
| 308 | // Subtle work around: only cache the intermediate varying when there's no extra |
| 309 | // coord transform. If the FP uses a coord transform for a legacy effect, but also |
| 310 | // delegates to a child FP, we want the coordinates pre-GrCoordTransform to be sent |
| 311 | // to the child FP, but have the FP use the post-coordtransform legacy values |
| 312 | // (e.g. sampling a texture and relying on the GrCoordTransform for normalization |
| 313 | // and mixing with a child FP that should not be normalized). |
| 314 | // FIXME: It's not really possible to apply this logic cleanly when transforms |
| 315 | // have been moved to the FS; in practice this doesn't seem to occur in our tests and |
| 316 | // the issue will go away once legacy coord transforms only have no-op matrices. |
| 317 | localCoordsMap.insert({ tr.fFP, tr.fOutputCoords }); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 318 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 322 | void GrGLSLGeometryProcessor::setTransformDataHelper(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 323 | const CoordTransformRange& transformRange) { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 324 | int i = 0; |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 325 | for (auto [transform, fp] : transformRange) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 326 | if (fInstalledTransforms[i].fHandle.isValid()) { |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 327 | SkMatrix m = GetTransformMatrix(transform, SkMatrix::I()); |
Mike Reed | 2c38315 | 2019-12-18 16:47:47 -0500 | [diff] [blame] | 328 | if (!SkMatrixPriv::CheapEqual(fInstalledTransforms[i].fCurrentValue, m)) { |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 329 | if (fInstalledTransforms[i].fType == kFloat4_GrSLType) { |
| 330 | float values[4] = {m.getScaleX(), m.getTranslateX(), |
| 331 | m.getScaleY(), m.getTranslateY()}; |
Brian Salomon | 14ed885 | 2020-03-24 10:43:38 -0400 | [diff] [blame] | 332 | SkASSERT(m.isScaleTranslate()); |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 333 | pdman.set4fv(fInstalledTransforms[i].fHandle.toIndex(), 1, values); |
| 334 | } else { |
Brian Salomon | 14ed885 | 2020-03-24 10:43:38 -0400 | [diff] [blame] | 335 | SkASSERT(!m.isScaleTranslate() || !fp.isSampledWithExplicitCoords()); |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 336 | SkASSERT(fInstalledTransforms[i].fType == kFloat3x3_GrSLType); |
| 337 | pdman.setSkMatrix(fInstalledTransforms[i].fHandle.toIndex(), m); |
| 338 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 339 | fInstalledTransforms[i].fCurrentValue = m; |
| 340 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 341 | } |
| 342 | ++i; |
| 343 | } |
| 344 | SkASSERT(i == fInstalledTransforms.count()); |
| 345 | } |
| 346 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 347 | void GrGLSLGeometryProcessor::setTransform(const GrGLSLProgramDataManager& pdman, |
| 348 | const UniformHandle& uniform, |
| 349 | const SkMatrix& matrix, |
| 350 | SkMatrix* state) const { |
| 351 | if (!uniform.isValid() || (state && SkMatrixPriv::CheapEqual(*state, matrix))) { |
| 352 | // No update needed |
| 353 | return; |
| 354 | } |
| 355 | if (state) { |
| 356 | *state = matrix; |
| 357 | } |
| 358 | if (matrix.isScaleTranslate()) { |
| 359 | // ComputeMatrixKey and writeX() assume the uniform is a float4 (can't assert since nothing |
| 360 | // is exposed on a handle, but should be caught lower down). |
| 361 | float values[4] = {matrix.getScaleX(), matrix.getTranslateX(), |
| 362 | matrix.getScaleY(), matrix.getTranslateY()}; |
| 363 | pdman.set4fv(uniform, 1, values); |
| 364 | } else { |
| 365 | pdman.setSkMatrix(uniform, matrix); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | static void write_vertex_position(GrGLSLVertexBuilder* vertBuilder, |
| 370 | GrGLSLUniformHandler* uniformHandler, |
| 371 | const GrShaderVar& inPos, |
| 372 | const SkMatrix& matrix, |
| 373 | const char* matrixName, |
| 374 | GrShaderVar* outPos, |
| 375 | GrGLSLGeometryProcessor::UniformHandle* matrixUniform) { |
| 376 | SkASSERT(inPos.getType() == kFloat3_GrSLType || inPos.getType() == kFloat2_GrSLType); |
| 377 | SkString outName = vertBuilder->newTmpVarName(inPos.getName().c_str()); |
| 378 | |
| 379 | if (matrix.isIdentity()) { |
| 380 | // Direct assignment, we won't use a uniform for the matrix. |
| 381 | outPos->set(inPos.getType(), outName.c_str()); |
| 382 | vertBuilder->codeAppendf("float%d %s = %s;", GrSLTypeVecLength(inPos.getType()), |
| 383 | outName.c_str(), inPos.getName().c_str()); |
| 384 | } else { |
| 385 | SkASSERT(matrixUniform); |
| 386 | |
| 387 | bool useCompactTransform = matrix.isScaleTranslate(); |
| 388 | const char* mangledMatrixName; |
| 389 | *matrixUniform = uniformHandler->addUniform(nullptr, |
| 390 | kVertex_GrShaderFlag, |
| 391 | useCompactTransform ? kFloat4_GrSLType |
| 392 | : kFloat3x3_GrSLType, |
| 393 | matrixName, |
| 394 | &mangledMatrixName); |
| 395 | |
| 396 | if (inPos.getType() == kFloat3_GrSLType) { |
| 397 | // A float3 stays a float3 whether or not the matrix adds perspective |
| 398 | if (useCompactTransform) { |
| 399 | vertBuilder->codeAppendf("float3 %s = %s.xz1 * %s + %s.yw0;\n", |
| 400 | outName.c_str(), mangledMatrixName, |
| 401 | inPos.getName().c_str(), mangledMatrixName); |
| 402 | } else { |
| 403 | vertBuilder->codeAppendf("float3 %s = %s * %s;\n", outName.c_str(), |
| 404 | mangledMatrixName, inPos.getName().c_str()); |
| 405 | } |
| 406 | outPos->set(kFloat3_GrSLType, outName.c_str()); |
| 407 | } else if (matrix.hasPerspective()) { |
| 408 | // A float2 is promoted to a float3 if we add perspective via the matrix |
| 409 | SkASSERT(!useCompactTransform); |
| 410 | vertBuilder->codeAppendf("float3 %s = (%s * %s.xy1);", |
| 411 | outName.c_str(), mangledMatrixName, inPos.getName().c_str()); |
| 412 | outPos->set(kFloat3_GrSLType, outName.c_str()); |
| 413 | } else { |
| 414 | if (useCompactTransform) { |
| 415 | vertBuilder->codeAppendf("float2 %s = %s.xz * %s + %s.yw;\n", |
| 416 | outName.c_str(), mangledMatrixName, |
| 417 | inPos.getName().c_str(), mangledMatrixName); |
| 418 | } else { |
| 419 | vertBuilder->codeAppendf("float2 %s = (%s * %s.xy1).xy;\n", |
| 420 | outName.c_str(), mangledMatrixName, |
| 421 | inPos.getName().c_str()); |
| 422 | } |
| 423 | outPos->set(kFloat2_GrSLType, outName.c_str()); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 428 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 429 | GrGPArgs* gpArgs, |
| 430 | const char* posName) { |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 431 | // writeOutputPosition assumes the incoming pos name points to a float2 variable |
| 432 | GrShaderVar inPos(posName, kFloat2_GrSLType); |
| 433 | write_vertex_position(vertBuilder, nullptr, inPos, SkMatrix::I(), "viewMatrix", |
| 434 | &gpArgs->fPositionVar, nullptr); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 437 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 438 | GrGLSLUniformHandler* uniformHandler, |
| 439 | GrGPArgs* gpArgs, |
| 440 | const char* posName, |
| 441 | const SkMatrix& mat, |
| 442 | UniformHandle* viewMatrixUniform) { |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 443 | GrShaderVar inPos(posName, kFloat2_GrSLType); |
| 444 | write_vertex_position(vertBuilder, uniformHandler, inPos, mat, "viewMatrix", |
| 445 | &gpArgs->fPositionVar, viewMatrixUniform); |
| 446 | } |
| 447 | |
| 448 | void GrGLSLGeometryProcessor::writeLocalCoord(GrGLSLVertexBuilder* vertBuilder, |
| 449 | GrGLSLUniformHandler* uniformHandler, |
| 450 | GrGPArgs* gpArgs, |
| 451 | GrShaderVar localVar, |
| 452 | const SkMatrix& localMatrix, |
| 453 | UniformHandle* localMatrixUniform) { |
| 454 | write_vertex_position(vertBuilder, uniformHandler, localVar, localMatrix, "localMatrix", |
| 455 | &gpArgs->fLocalCoordVar, localMatrixUniform); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 456 | } |