blob: 1444c0c8de242d487ac2624da8d0f95e479914f6 [file] [log] [blame]
joshualitt8072caa2015-02-12 14:20:52 -08001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
joshualitt8072caa2015-02-12 14:20:52 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrCoordTransform.h"
Ethan Nicholas58430122020-04-14 09:54:02 -040011#include "src/gpu/GrPipeline.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#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"
joshualitt8072caa2015-02-12 14:20:52 -080016
Ethan Nicholasd3a95c22020-06-03 13:24:46 -040017#include <unordered_map>
18
egdaniele659a582015-11-13 09:55:43 -080019void GrGLSLGeometryProcessor::emitCode(EmitArgs& args) {
joshualitt8072caa2015-02-12 14:20:52 -080020 GrGPArgs gpArgs;
21 this->onEmitCode(args, &gpArgs);
Chris Daltonc17bf322017-10-24 10:59:03 -060022
Michael Ludwig553db622020-06-19 10:47:30 -040023 // 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 Dalton5a2f9622019-12-27 14:56:38 -070030 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 Daltonc17bf322017-10-24 10:59:03 -060036 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 Dalton23261772017-12-10 16:41:45 -070039 SkASSERT(kFloat2_GrSLType == gpArgs.fPositionVar.getType() ||
40 kFloat3_GrSLType == gpArgs.fPositionVar.getType());
Chris Daltonc17bf322017-10-24 10:59:03 -060041 vBuilder->emitNormalizedSkPosition(gpArgs.fPositionVar.c_str(), args.fRTAdjustName,
42 gpArgs.fPositionVar.getType());
Chris Dalton23261772017-12-10 16:41:45 -070043 if (kFloat2_GrSLType == gpArgs.fPositionVar.getType()) {
44 args.fVaryingHandler->setNoPerspective();
45 }
Chris Daltonc17bf322017-10-24 10:59:03 -060046 } 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 Dalton23261772017-12-10 16:41:45 -070050 // The subclass must call setNoPerspective on the varying handler, if applicable.
Chris Daltonc17bf322017-10-24 10:59:03 -060051 vBuilder->codeAppendf("sk_Position = float4(%s", gpArgs.fPositionVar.c_str());
Chris Dalton23261772017-12-10 16:41:45 -070052 switch (gpArgs.fPositionVar.getType()) {
53 case kFloat_GrSLType:
John Stiles30212b72020-06-11 17:55:07 -040054 vBuilder->codeAppend(", 0");
55 [[fallthrough]];
Chris Dalton23261772017-12-10 16:41:45 -070056 case kFloat2_GrSLType:
John Stiles30212b72020-06-11 17:55:07 -040057 vBuilder->codeAppend(", 0");
58 [[fallthrough]];
Chris Dalton23261772017-12-10 16:41:45 -070059 case kFloat3_GrSLType:
John Stiles30212b72020-06-11 17:55:07 -040060 vBuilder->codeAppend(", 1");
61 [[fallthrough]];
Chris Dalton23261772017-12-10 16:41:45 -070062 case kFloat4_GrSLType:
63 vBuilder->codeAppend(");");
64 break;
65 default:
66 SK_ABORT("Invalid position var type");
67 break;
Chris Daltonc17bf322017-10-24 10:59:03 -060068 }
cdaltonc08f1962016-02-12 12:14:06 -080069 }
joshualitt8072caa2015-02-12 14:20:52 -080070}
71
Michael Ludwig553db622020-06-19 10:47:30 -040072void GrGLSLGeometryProcessor::collectTransforms(GrGLSLVertexBuilder* vb,
73 GrGLSLVaryingHandler* varyingHandler,
74 GrGLSLUniformHandler* uniformHandler,
75 const GrShaderVar& localCoordsVar,
76 FPCoordTransformHandler* handler) {
Michael Ludwige88320b2020-06-24 09:04:56 -040077 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 Salomon706851d2020-02-20 14:18:00 -050094 }
Michael Ludwige88320b2020-06-24 09:04:56 -040095 return GrShaderVar(SkString(baseLocalCoord.fsIn()), baseLocalCoord.type(),
96 GrShaderVar::TypeModifier::In);
Brian Salomon706851d2020-02-20 14:18:00 -050097 };
Brian Salomon04460cc2017-12-06 14:47:42 -050098
Brian Salomon7d8b3972019-11-26 22:34:44 -050099 for (int i = 0; *handler; ++*handler, ++i) {
100 auto [coordTransform, fp] = handler->get();
Michael Ludwige88320b2020-06-24 09:04:56 -0400101
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 Salomon7eabfe82019-12-02 14:20:20 -0500116 SkString strUniName;
117 strUniName.printf("CoordTransformMatrix_%d", i);
Michael Ludwige88320b2020-06-24 09:04:56 -0400118 auto flag = localCoordComputedInFS ? kFragment_GrShaderFlag
119 : kVertex_GrShaderFlag;
Brian Salomon8d1dcd72020-03-20 21:06:41 -0400120 auto& uni = fInstalledTransforms.push_back();
Brian Salomon14ed8852020-03-24 10:43:38 -0400121 if (fp.isSampledWithExplicitCoords() && coordTransform.matrix().isScaleTranslate()) {
Brian Salomon8d1dcd72020-03-20 21:06:41 -0400122 uni.fType = kFloat4_GrSLType;
123 } else {
124 uni.fType = kFloat3x3_GrSLType;
125 }
126 uni.fHandle =
Michael Ludwige88320b2020-06-24 09:04:56 -0400127 uniformHandler->addUniform(&fp, flag, uni.fType, strUniName.c_str());
Brian Salomon8d1dcd72020-03-20 21:06:41 -0400128 transformVar = uniformHandler->getUniformVariable(uni.fHandle);
Brian Salomon706851d2020-02-20 14:18:00 -0500129 } else {
Michael Ludwige88320b2020-06-24 09:04:56 -0400130 // Must stay parallel with calls to handler
Brian Salomon706851d2020-02-20 14:18:00 -0500131 fInstalledTransforms.push_back();
Brian Salomon706851d2020-02-20 14:18:00 -0500132 }
133
Michael Ludwige88320b2020-06-24 09:04:56 -0400134 // 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 Salomon7eabfe82019-12-02 14:20:20 -0500153
Michael Ludwige88320b2020-06-24 09:04:56 -0400154 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 Nicholasbffad832020-05-05 14:31:55 -0400206 } else {
Michael Ludwige88320b2020-06-24 09:04:56 -0400207 // The FP transform hierarchy is the identity, so use the original local coord
208 varyingVar = getBaseLocalCoord();
Ethan Nicholasbffad832020-05-05 14:31:55 -0400209 }
Brian Salomon7eabfe82019-12-02 14:20:20 -0500210 }
joshualitt8072caa2015-02-12 14:20:52 -0800211 }
Michael Ludwige88320b2020-06-24 09:04:56 -0400212
213 if (varyingVar.getType() != kVoid_GrSLType || transformVar.getType() != kVoid_GrSLType) {
214 handler->specifyCoordsForCurrCoordTransform(transformVar, varyingVar);
215 } else {
216 handler->omitCoordsForCurrCoordTransform();
217 }
Ethan Nicholas58430122020-04-14 09:54:02 -0400218 }
219}
220
221void GrGLSLGeometryProcessor::emitTransformCode(GrGLSLVertexBuilder* vb,
222 GrGLSLUniformHandler* uniformHandler) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400223 std::unordered_map<const GrFragmentProcessor*, GrShaderVar> localCoordsMap;
Ethan Nicholas58430122020-04-14 09:54:02 -0400224 for (const auto& tr : fTransformInfos) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400225 // 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 Osman061a5cf2020-06-24 14:50:25 -0400258 *base->parent(), SkString(base->sampleMatrix().fExpression));
Michael Ludwige88320b2020-06-24 09:04:56 -0400259
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 Osman061a5cf2020-06-24 14:50:25 -0400267 matrix = SkString(base->sampleMatrix().fExpression);
Michael Ludwige88320b2020-06-24 09:04:56 -0400268 }
269
270 if (!transformExpression.isEmpty()) {
271 transformExpression.append(" * ");
272 }
273 transformExpression.appendf("(%s)", matrix.c_str());
Ethan Nicholasd3a95c22020-06-03 13:24:46 -0400274 } else {
Michael Ludwige88320b2020-06-24 09:04:56 -0400275 // 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 Nicholasd3a95c22020-06-03 13:24:46 -0400278 }
Michael Ludwige88320b2020-06-24 09:04:56 -0400279
280 base = base->parent();
Ethan Nicholasd3a95c22020-06-03 13:24:46 -0400281 }
Michael Ludwige88320b2020-06-24 09:04:56 -0400282 }
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 Nicholas58430122020-04-14 09:54:02 -0400318 }
joshualitt8072caa2015-02-12 14:20:52 -0800319 }
320}
321
Michael Ludwig553db622020-06-19 10:47:30 -0400322void GrGLSLGeometryProcessor::setTransformDataHelper(const GrGLSLProgramDataManager& pdman,
Brian Salomonc241b582019-11-27 08:57:17 -0500323 const CoordTransformRange& transformRange) {
bsalomona624bf32016-09-20 09:12:47 -0700324 int i = 0;
Brian Salomonc241b582019-11-27 08:57:17 -0500325 for (auto [transform, fp] : transformRange) {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500326 if (fInstalledTransforms[i].fHandle.isValid()) {
Michael Ludwig553db622020-06-19 10:47:30 -0400327 SkMatrix m = GetTransformMatrix(transform, SkMatrix::I());
Mike Reed2c383152019-12-18 16:47:47 -0500328 if (!SkMatrixPriv::CheapEqual(fInstalledTransforms[i].fCurrentValue, m)) {
Brian Salomon8d1dcd72020-03-20 21:06:41 -0400329 if (fInstalledTransforms[i].fType == kFloat4_GrSLType) {
330 float values[4] = {m.getScaleX(), m.getTranslateX(),
331 m.getScaleY(), m.getTranslateY()};
Brian Salomon14ed8852020-03-24 10:43:38 -0400332 SkASSERT(m.isScaleTranslate());
Brian Salomon8d1dcd72020-03-20 21:06:41 -0400333 pdman.set4fv(fInstalledTransforms[i].fHandle.toIndex(), 1, values);
334 } else {
Brian Salomon14ed8852020-03-24 10:43:38 -0400335 SkASSERT(!m.isScaleTranslate() || !fp.isSampledWithExplicitCoords());
Brian Salomon8d1dcd72020-03-20 21:06:41 -0400336 SkASSERT(fInstalledTransforms[i].fType == kFloat3x3_GrSLType);
337 pdman.setSkMatrix(fInstalledTransforms[i].fHandle.toIndex(), m);
338 }
Brian Salomon7eabfe82019-12-02 14:20:20 -0500339 fInstalledTransforms[i].fCurrentValue = m;
340 }
bsalomona624bf32016-09-20 09:12:47 -0700341 }
342 ++i;
343 }
344 SkASSERT(i == fInstalledTransforms.count());
345}
346
Michael Ludwig553db622020-06-19 10:47:30 -0400347void 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
369static 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 Salomon7f235432017-08-16 09:41:48 -0400428void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder,
429 GrGPArgs* gpArgs,
430 const char* posName) {
Michael Ludwig553db622020-06-19 10:47:30 -0400431 // 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);
joshualitt5559ca22015-05-21 15:50:36 -0700435}
436
Brian Salomon7f235432017-08-16 09:41:48 -0400437void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder,
438 GrGLSLUniformHandler* uniformHandler,
439 GrGPArgs* gpArgs,
440 const char* posName,
441 const SkMatrix& mat,
442 UniformHandle* viewMatrixUniform) {
Michael Ludwig553db622020-06-19 10:47:30 -0400443 GrShaderVar inPos(posName, kFloat2_GrSLType);
444 write_vertex_position(vertBuilder, uniformHandler, inPos, mat, "viewMatrix",
445 &gpArgs->fPositionVar, viewMatrixUniform);
446}
447
448void 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);
joshualitt8072caa2015-02-12 14:20:52 -0800456}