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