Coverage Report

Created: 2021-08-22 09:07

/src/skia/src/gpu/GrGeometryProcessor.cpp
Line
Count
Source (jump to first uncovered line)
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
8
#include "src/gpu/GrGeometryProcessor.h"
9
10
#include "src/core/SkMatrixPriv.h"
11
#include "src/gpu/GrPipeline.h"
12
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
13
#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
14
#include "src/gpu/glsl/GrGLSLUniformHandler.h"
15
#include "src/gpu/glsl/GrGLSLVarying.h"
16
17
#include <queue>
18
19
117k
GrGeometryProcessor::GrGeometryProcessor(ClassID classID) : GrProcessor(classID) {}
20
21
0
const GrGeometryProcessor::TextureSampler& GrGeometryProcessor::textureSampler(int i) const {
22
0
    SkASSERT(i >= 0 && i < this->numTextureSamplers());
23
0
    return this->onTextureSampler(i);
24
0
}
Unexecuted instantiation: GrGeometryProcessor::textureSampler(int) const
Unexecuted instantiation: GrGeometryProcessor::textureSampler(int) const
25
26
0
uint32_t GrGeometryProcessor::ComputeCoordTransformsKey(const GrFragmentProcessor& fp) {
27
    // This is highly coupled with the code in ProgramImpl::collectTransforms().
28
0
    uint32_t key = static_cast<uint32_t>(fp.sampleUsage().kind()) << 1;
29
    // This needs to be updated if GP starts specializing varyings on additional matrix types.
30
0
    if (fp.sampleUsage().hasPerspective()) {
31
0
        key |= 0b1;
32
0
    }
33
0
    return key;
34
0
}
35
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38
static inline GrSamplerState::Filter clamp_filter(GrTextureType type,
39
11.2k
                                                  GrSamplerState::Filter requestedFilter) {
40
11.2k
    if (GrTextureTypeHasRestrictedSampling(type)) {
41
0
        return std::min(requestedFilter, GrSamplerState::Filter::kLinear);
42
0
    }
43
11.2k
    return requestedFilter;
44
11.2k
}
45
46
GrGeometryProcessor::TextureSampler::TextureSampler(GrSamplerState samplerState,
47
                                                    const GrBackendFormat& backendFormat,
48
9.37k
                                                    const GrSwizzle& swizzle) {
49
9.37k
    this->reset(samplerState, backendFormat, swizzle);
50
9.37k
}
51
52
void GrGeometryProcessor::TextureSampler::reset(GrSamplerState samplerState,
53
                                                const GrBackendFormat& backendFormat,
54
11.2k
                                                const GrSwizzle& swizzle) {
55
11.2k
    fSamplerState = samplerState;
56
11.2k
    fSamplerState.setFilterMode(clamp_filter(backendFormat.textureType(), samplerState.filter()));
57
11.2k
    fBackendFormat = backendFormat;
58
11.2k
    fSwizzle = swizzle;
59
11.2k
    fIsInitialized = true;
60
11.2k
}
61
62
//////////////////////////////////////////////////////////////////////////////
63
64
using ProgramImpl = GrGeometryProcessor::ProgramImpl;
65
66
0
ProgramImpl::FPCoordsMap ProgramImpl::emitCode(EmitArgs& args, const GrPipeline& pipeline) {
67
0
    GrGPArgs gpArgs;
68
0
    this->onEmitCode(args, &gpArgs);
69
70
0
    GrShaderVar positionVar = gpArgs.fPositionVar;
71
    // skia:12198
72
0
    if (args.fGeomProc.willUseGeoShader() || args.fGeomProc.willUseTessellationShaders()) {
73
0
        positionVar = {};
74
0
    }
75
0
    FPCoordsMap transformMap = this->collectTransforms(args.fVertBuilder,
76
0
                                                       args.fVaryingHandler,
77
0
                                                       args.fUniformHandler,
78
0
                                                       gpArgs.fLocalCoordVar,
79
0
                                                       positionVar,
80
0
                                                       pipeline);
81
82
0
    if (args.fGeomProc.willUseTessellationShaders()) {
83
        // Tessellation shaders are temporarily responsible for integrating their own code strings
84
        // while we work out full support.
85
0
        return transformMap;
86
0
    }
87
88
0
    GrGLSLVertexBuilder* vBuilder = args.fVertBuilder;
89
0
    if (!args.fGeomProc.willUseGeoShader()) {
90
        // Emit the vertex position to the hardware in the normalized window coordinates it expects.
91
0
        SkASSERT(kFloat2_GrSLType == gpArgs.fPositionVar.getType() ||
92
0
                 kFloat3_GrSLType == gpArgs.fPositionVar.getType());
93
0
        vBuilder->emitNormalizedSkPosition(gpArgs.fPositionVar.c_str(),
94
0
                                           gpArgs.fPositionVar.getType());
95
0
        if (kFloat2_GrSLType == gpArgs.fPositionVar.getType()) {
96
0
            args.fVaryingHandler->setNoPerspective();
97
0
        }
98
0
    } else {
99
        // Since we have a geometry shader, leave the vertex position in Skia device space for now.
100
        // The geometry Shader will operate in device space, and then convert the final positions to
101
        // normalized hardware window coordinates under the hood, once everything else has finished.
102
        // The subclass must call setNoPerspective on the varying handler, if applicable.
103
0
        vBuilder->codeAppendf("sk_Position = float4(%s", gpArgs.fPositionVar.c_str());
104
0
        switch (gpArgs.fPositionVar.getType()) {
105
0
            case kFloat_GrSLType:
106
0
                vBuilder->codeAppend(", 0");
107
0
                [[fallthrough]];
108
0
            case kFloat2_GrSLType:
109
0
                vBuilder->codeAppend(", 0");
110
0
                [[fallthrough]];
111
0
            case kFloat3_GrSLType:
112
0
                vBuilder->codeAppend(", 1");
113
0
                [[fallthrough]];
114
0
            case kFloat4_GrSLType:
115
0
                vBuilder->codeAppend(");");
116
0
                break;
117
0
            default:
118
0
                SK_ABORT("Invalid position var type");
119
0
                break;
120
0
        }
121
0
    }
122
0
    return transformMap;
123
0
}
Unexecuted instantiation: GrGeometryProcessor::ProgramImpl::emitCode(GrGeometryProcessor::ProgramImpl::EmitArgs&, GrPipeline const&)
Unexecuted instantiation: GrGeometryProcessor::ProgramImpl::emitCode(GrGeometryProcessor::ProgramImpl::EmitArgs&, GrPipeline const&)
124
125
ProgramImpl::FPCoordsMap ProgramImpl::collectTransforms(GrGLSLVertexBuilder* vb,
126
                                                        GrGLSLVaryingHandler* varyingHandler,
127
                                                        GrGLSLUniformHandler* uniformHandler,
128
                                                        const GrShaderVar& localCoordsVar,
129
                                                        const GrShaderVar& positionVar,
130
0
                                                        const GrPipeline& pipeline) {
131
0
    SkASSERT(localCoordsVar.getType() == kFloat2_GrSLType ||
132
0
             localCoordsVar.getType() == kFloat3_GrSLType ||
133
0
             localCoordsVar.getType() == kVoid_GrSLType);
134
0
    SkASSERT(positionVar.getType() == kFloat2_GrSLType ||
135
0
             positionVar.getType() == kFloat3_GrSLType ||
136
0
             positionVar.getType() == kVoid_GrSLType);
137
138
0
    enum class BaseCoord { kNone, kLocal, kPosition };
139
140
0
    auto baseLocalCoordFSVar = [&, baseLocalCoord = GrGLSLVarying()]() mutable {
141
0
        SkASSERT(GrSLTypeIsFloatType(localCoordsVar.getType()));
142
0
        if (baseLocalCoord.type() == kVoid_GrSLType) {
143
            // Initialize to the GP provided coordinate
144
0
            baseLocalCoord = GrGLSLVarying(localCoordsVar.getType());
145
0
            varyingHandler->addVarying("LocalCoord", &baseLocalCoord);
146
0
            vb->codeAppendf("%s = %s;\n", baseLocalCoord.vsOut(), localCoordsVar.getName().c_str());
147
0
        }
148
0
        return baseLocalCoord.fsInVar();
149
0
    };
Unexecuted instantiation: GrGeometryProcessor.cpp:GrGeometryProcessor::ProgramImpl::collectTransforms(GrGLSLVertexBuilder*, GrGLSLVaryingHandler*, GrGLSLUniformHandler*, GrShaderVar const&, GrShaderVar const&, GrPipeline const&)::$_0::operator()()
Unexecuted instantiation: GrGeometryProcessor.cpp:GrGeometryProcessor::ProgramImpl::collectTransforms(GrGLSLVertexBuilder*, GrGLSLVaryingHandler*, GrGLSLUniformHandler*, GrShaderVar const&, GrShaderVar const&, GrPipeline const&)::$_4::operator()()
150
151
0
    bool canUsePosition = positionVar.getType() != kVoid_GrSLType;
152
153
0
    FPCoordsMap result;
154
    // Performs a pre-order traversal of FP hierarchy rooted at fp and identifies FPs that are
155
    // sampled with a series of matrices applied to local coords. For each such FP a varying is
156
    // added to the varying handler and added to 'result'.
157
0
    auto liftTransforms = [&, traversalIndex = 0](
158
0
                                  auto& self,
159
0
                                  const GrFragmentProcessor& fp,
160
0
                                  bool hasPerspective,
161
0
                                  const GrFragmentProcessor* lastMatrixFP = nullptr,
162
0
                                  int lastMatrixTraversalIndex = -1,
163
0
                                  BaseCoord baseCoord = BaseCoord::kLocal) mutable -> void {
164
0
        ++traversalIndex;
165
0
        switch (fp.sampleUsage().kind()) {
166
0
            case SkSL::SampleUsage::Kind::kNone:
167
                // This should only happen at the root. Otherwise how did this FP get added?
168
0
                SkASSERT(!fp.parent());
169
0
                break;
170
0
            case SkSL::SampleUsage::Kind::kPassThrough:
171
0
                break;
172
0
            case SkSL::SampleUsage::Kind::kUniformMatrix:
173
                // Update tracking of last matrix and matrix props.
174
0
                hasPerspective |= fp.sampleUsage().hasPerspective();
175
0
                lastMatrixFP = &fp;
176
0
                lastMatrixTraversalIndex = traversalIndex;
177
0
                break;
178
0
            case SkSL::SampleUsage::Kind::kFragCoord:
179
0
                hasPerspective = positionVar.getType() == kFloat3_GrSLType;
180
0
                lastMatrixFP = nullptr;
181
0
                lastMatrixTraversalIndex = -1;
182
0
                baseCoord = BaseCoord::kPosition;
183
0
                break;
184
0
            case SkSL::SampleUsage::Kind::kExplicit:
185
0
                baseCoord = BaseCoord::kNone;
186
0
                break;
187
0
        }
188
189
0
        auto& [varyingFSVar, hasCoordsParam] = result[&fp];
190
0
        hasCoordsParam = fp.usesSampleCoordsDirectly();
191
192
        // We add a varying if we're in a chain of matrices multiplied by local or device coords.
193
        // If the coord is the untransformed local coord we add a varying. We don't if it is
194
        // untransformed device coords since it doesn't save us anything over "sk_FragCoord.xy". Of
195
        // course, if the FP doesn't directly use its coords then we don't add a varying.
196
0
        if (fp.usesSampleCoordsDirectly() &&
197
0
            (baseCoord == BaseCoord::kLocal ||
198
0
             (baseCoord == BaseCoord::kPosition && lastMatrixFP && canUsePosition))) {
199
            // Associate the varying with the highest possible node in the FP tree that shares the
200
            // same coordinates so that multiple FPs in a subtree can share. If there are no matrix
201
            // sample nodes on the way up the tree then directly use the local coord.
202
0
            if (!lastMatrixFP) {
203
0
                varyingFSVar = baseLocalCoordFSVar();
204
0
            } else {
205
                // If there is an already a varying that incorporates all matrices from the root to
206
                // lastMatrixFP just use it. Otherwise, we add it.
207
0
                auto& [varying, inputCoords, varyingIdx] = fTransformVaryingsMap[lastMatrixFP];
208
0
                if (varying.type() == kVoid_GrSLType) {
209
0
                    varying = GrGLSLVarying(hasPerspective ? kFloat3_GrSLType : kFloat2_GrSLType);
210
0
                    SkString strVaryingName = SkStringPrintf("TransformedCoords_%d",
211
0
                                                             lastMatrixTraversalIndex);
212
0
                    varyingHandler->addVarying(strVaryingName.c_str(), &varying);
213
0
                    inputCoords = baseCoord == BaseCoord::kLocal ? localCoordsVar : positionVar;
214
0
                    varyingIdx = lastMatrixTraversalIndex;
215
0
                }
216
0
                SkASSERT(varyingIdx == lastMatrixTraversalIndex);
217
                // The FP will use the varying in the fragment shader as its coords.
218
0
                varyingFSVar = varying.fsInVar();
219
0
            }
220
0
            hasCoordsParam = false;
221
0
        }
222
223
0
        for (int c = 0; c < fp.numChildProcessors(); ++c) {
224
0
            if (auto* child = fp.childProcessor(c)) {
225
0
                self(self,
226
0
                     *child,
227
0
                     hasPerspective,
228
0
                     lastMatrixFP,
229
0
                     lastMatrixTraversalIndex,
230
0
                     baseCoord);
231
                // If we have a varying then we never need a param. Otherwise, if one of our
232
                // children takes a non-explicit coord then we'll need our coord.
233
0
                hasCoordsParam |= varyingFSVar.getType() == kVoid_GrSLType &&
234
0
                                  !child->sampleUsage().isExplicit()       &&
235
0
                                  !child->sampleUsage().isFragCoord()      &&
236
0
                                  result[child].hasCoordsParam;
237
0
            }
238
0
        }
239
0
    };
Unexecuted instantiation: GrGeometryProcessor.cpp:_ZZN19GrGeometryProcessor11ProgramImpl17collectTransformsEP19GrGLSLVertexBuilderP20GrGLSLVaryingHandlerP20GrGLSLUniformHandlerRK11GrShaderVarS9_RK10GrPipelineEN3$_1clISD_EEvRT_RK19GrFragmentProcessorbPSI_iZNS0_17collectTransformsES2_S4_S6_S9_S9_SC_E9BaseCoord
Unexecuted instantiation: GrGeometryProcessor.cpp:_ZZN19GrGeometryProcessor11ProgramImpl17collectTransformsEP19GrGLSLVertexBuilderP20GrGLSLVaryingHandlerP20GrGLSLUniformHandlerRK11GrShaderVarS9_RK10GrPipelineEN3$_5clISD_EEvRT_RK19GrFragmentProcessorbPSI_iZNS0_17collectTransformsES2_S4_S6_S9_S9_SC_E9BaseCoord
240
241
0
    bool hasPerspective = GrSLTypeVecLength(localCoordsVar.getType()) == 3;
242
0
    for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
243
0
        liftTransforms(liftTransforms, pipeline.getFragmentProcessor(i), hasPerspective);
244
0
    }
245
0
    return result;
246
0
}
Unexecuted instantiation: GrGeometryProcessor::ProgramImpl::collectTransforms(GrGLSLVertexBuilder*, GrGLSLVaryingHandler*, GrGLSLUniformHandler*, GrShaderVar const&, GrShaderVar const&, GrPipeline const&)
Unexecuted instantiation: GrGeometryProcessor::ProgramImpl::collectTransforms(GrGLSLVertexBuilder*, GrGLSLVaryingHandler*, GrGLSLUniformHandler*, GrShaderVar const&, GrShaderVar const&, GrPipeline const&)
247
248
0
void ProgramImpl::emitTransformCode(GrGLSLVertexBuilder* vb, GrGLSLUniformHandler* uniformHandler) {
249
    // Because descendant varyings may be computed using the varyings of ancestor FPs we make
250
    // sure to visit the varyings according to FP pre-order traversal by dumping them into a
251
    // priority queue.
252
0
    using FPAndInfo = std::tuple<const GrFragmentProcessor*, TransformInfo>;
253
0
    auto compare = [](const FPAndInfo& a, const FPAndInfo& b) {
254
0
        return std::get<1>(a).traversalOrder > std::get<1>(b).traversalOrder;
255
0
    };
Unexecuted instantiation: GrGeometryProcessor.cpp:GrGeometryProcessor::ProgramImpl::emitTransformCode(GrGLSLVertexBuilder*, GrGLSLUniformHandler*)::$_2::operator()(std::__1::tuple<GrFragmentProcessor const*, GrGeometryProcessor::ProgramImpl::TransformInfo> const&, std::__1::tuple<GrFragmentProcessor const*, GrGeometryProcessor::ProgramImpl::TransformInfo> const&) const
Unexecuted instantiation: GrGeometryProcessor.cpp:GrGeometryProcessor::ProgramImpl::emitTransformCode(GrGLSLVertexBuilder*, GrGLSLUniformHandler*)::$_6::operator()(std::__1::tuple<GrFragmentProcessor const*, GrGeometryProcessor::ProgramImpl::TransformInfo> const&, std::__1::tuple<GrFragmentProcessor const*, GrGeometryProcessor::ProgramImpl::TransformInfo> const&) const
256
0
    std::priority_queue<FPAndInfo, std::vector<FPAndInfo>, decltype(compare)> pq(compare);
257
0
    std::for_each(fTransformVaryingsMap.begin(), fTransformVaryingsMap.end(), [&pq](auto entry) {
258
0
        pq.push(entry);
259
0
    });
Unexecuted instantiation: GrGeometryProcessor.cpp:_ZZN19GrGeometryProcessor11ProgramImpl17emitTransformCodeEP19GrGLSLVertexBuilderP20GrGLSLUniformHandlerENK3$_3clINSt3__14pairIKPK19GrFragmentProcessorNS0_13TransformInfoEEEEEDaT_
Unexecuted instantiation: GrGeometryProcessor.cpp:_ZZN19GrGeometryProcessor11ProgramImpl17emitTransformCodeEP19GrGLSLVertexBuilderP20GrGLSLUniformHandlerENK3$_7clINSt3__14pairIKPK19GrFragmentProcessorNS0_13TransformInfoEEEEEDaT_
260
0
    for (; !pq.empty(); pq.pop()) {
261
0
        const auto& [fp, info] = pq.top();
262
        // If we recorded a transform info, its sample matrix must be uniform
263
0
        SkASSERT(fp->sampleUsage().isUniformMatrix());
264
0
        GrShaderVar uniform = uniformHandler->liftUniformToVertexShader(
265
0
                *fp->parent(), SkString(SkSL::SampleUsage::MatrixUniformName()));
266
        // Start with this matrix and accumulate additional matrices as we walk up the FP tree
267
        // to either the base coords or an ancestor FP that has an associated varying.
268
0
        SkString transformExpression = uniform.getName();
269
270
        // If we hit an ancestor with a varying on our walk up then save off the varying as the
271
        // input to our accumulated transformExpression. Start off assuming we'll reach the root.
272
0
        GrShaderVar inputCoords = info.inputCoords;
273
274
0
        for (const auto* base = fp->parent(); base; base = base->parent()) {
275
0
            if (auto iter = fTransformVaryingsMap.find(base); iter != fTransformVaryingsMap.end()) {
276
                // Can stop here, as this varying already holds all transforms from higher FPs
277
                // We'll apply the residual transformExpression we've accumulated up from our
278
                // starting FP to this varying.
279
0
                inputCoords = iter->second.varying.vsOutVar();
280
0
                break;
281
0
            } else if (base->sampleUsage().isUniformMatrix()) {
282
                // Accumulate any matrices along the path to either the original local/device coords
283
                // or a parent varying. Getting here means this FP was sampled with a uniform matrix
284
                // but all uses of coords below here in the FP hierarchy are beneath additional
285
                // matrix samples and thus this node wasn't assigned a varying.
286
0
                GrShaderVar parentUniform = uniformHandler->liftUniformToVertexShader(
287
0
                        *base->parent(), SkString(SkSL::SampleUsage::MatrixUniformName()));
288
0
                transformExpression.appendf(" * %s", parentUniform.getName().c_str());
289
0
            } else if (base->sampleUsage().isFragCoord()) {
290
                // Our chain of matrices starts here and is based on the device space position.
291
0
                break;
292
0
            } else {
293
                // This intermediate FP is just a pass through and doesn't need to be built
294
                // in to the expression, but we must visit its parents in case they add transforms.
295
0
                SkASSERT(base->sampleUsage().isPassThrough() || !base->sampleUsage().isSampled());
296
0
            }
297
0
        }
298
299
0
        SkString inputStr;
300
0
        if (inputCoords.getType() == kFloat2_GrSLType) {
301
0
            inputStr = SkStringPrintf("%s.xy1", inputCoords.getName().c_str());
302
0
        } else {
303
0
            SkASSERT(inputCoords.getType() == kFloat3_GrSLType);
304
0
            inputStr = inputCoords.getName();
305
0
        }
306
307
0
        vb->codeAppend("{\n");
308
0
        if (info.varying.type() == kFloat2_GrSLType) {
309
0
            if (vb->getProgramBuilder()->shaderCaps()->nonsquareMatrixSupport()) {
310
0
                vb->codeAppendf("%s = float3x2(%s) * %s",
311
0
                                info.varying.vsOut(),
312
0
                                transformExpression.c_str(),
313
0
                                inputStr.c_str());
314
0
            } else {
315
0
                vb->codeAppendf("%s = (%s * %s).xy",
316
0
                                info.varying.vsOut(),
317
0
                                transformExpression.c_str(),
318
0
                                inputStr.c_str());
319
0
            }
320
0
        } else {
321
0
            SkASSERT(info.varying.type() == kFloat3_GrSLType);
322
0
            vb->codeAppendf("%s = %s * %s",
323
0
                            info.varying.vsOut(),
324
0
                            transformExpression.c_str(),
325
0
                            inputStr.c_str());
326
0
        }
327
0
        vb->codeAppend(";\n");
328
0
        vb->codeAppend("}\n");
329
0
    }
330
    // We don't need this map anymore.
331
0
    fTransformVaryingsMap.clear();
332
0
}
Unexecuted instantiation: GrGeometryProcessor::ProgramImpl::emitTransformCode(GrGLSLVertexBuilder*, GrGLSLUniformHandler*)
Unexecuted instantiation: GrGeometryProcessor::ProgramImpl::emitTransformCode(GrGLSLVertexBuilder*, GrGLSLUniformHandler*)
333
334
void ProgramImpl::setupUniformColor(GrGLSLFPFragmentBuilder* fragBuilder,
335
                                    GrGLSLUniformHandler* uniformHandler,
336
                                    const char* outputName,
337
0
                                    UniformHandle* colorUniform) {
338
0
    SkASSERT(colorUniform);
339
0
    const char* stagedLocalVarName;
340
0
    *colorUniform = uniformHandler->addUniform(nullptr,
341
0
                                               kFragment_GrShaderFlag,
342
0
                                               kHalf4_GrSLType,
343
0
                                               "Color",
344
0
                                               &stagedLocalVarName);
345
0
    fragBuilder->codeAppendf("%s = %s;", outputName, stagedLocalVarName);
346
0
    if (fragBuilder->getProgramBuilder()->shaderCaps()->mustObfuscateUniformColor()) {
347
0
        fragBuilder->codeAppendf("%s = max(%s, half4(0));", outputName, outputName);
348
0
    }
349
0
}
Unexecuted instantiation: GrGeometryProcessor::ProgramImpl::setupUniformColor(GrGLSLFPFragmentBuilder*, GrGLSLUniformHandler*, char const*, GrResourceHandle<GrGLSLProgramDataManager::UniformHandleKind>*)
Unexecuted instantiation: GrGeometryProcessor::ProgramImpl::setupUniformColor(GrGLSLFPFragmentBuilder*, GrGLSLUniformHandler*, char const*, GrResourceHandle<GrGLSLProgramDataManager::UniformHandleKind>*)
350
351
void ProgramImpl::SetTransform(const GrGLSLProgramDataManager& pdman,
352
                               const GrShaderCaps& shaderCaps,
353
                               const UniformHandle& uniform,
354
                               const SkMatrix& matrix,
355
0
                               SkMatrix* state) {
356
0
    if (!uniform.isValid() || (state && SkMatrixPriv::CheapEqual(*state, matrix))) {
357
        // No update needed
358
0
        return;
359
0
    }
360
0
    if (state) {
361
0
        *state = matrix;
362
0
    }
363
0
    if (matrix.isScaleTranslate() && !shaderCaps.reducedShaderMode()) {
364
        // ComputeMatrixKey and writeX() assume the uniform is a float4 (can't assert since nothing
365
        // is exposed on a handle, but should be caught lower down).
366
0
        float values[4] = {matrix.getScaleX(), matrix.getTranslateX(),
367
0
                           matrix.getScaleY(), matrix.getTranslateY()};
368
0
        pdman.set4fv(uniform, 1, values);
369
0
    } else {
370
0
        pdman.setSkMatrix(uniform, matrix);
371
0
    }
372
0
}
373
374
static void write_passthrough_vertex_position(GrGLSLVertexBuilder* vertBuilder,
375
                                              const GrShaderVar& inPos,
376
0
                                              GrShaderVar* outPos) {
377
0
    SkASSERT(inPos.getType() == kFloat3_GrSLType || inPos.getType() == kFloat2_GrSLType);
378
0
    SkString outName = vertBuilder->newTmpVarName(inPos.getName().c_str());
379
0
    outPos->set(inPos.getType(), outName.c_str());
380
0
    vertBuilder->codeAppendf("float%d %s = %s;",
381
0
                             GrSLTypeVecLength(inPos.getType()),
382
0
                             outName.c_str(),
383
0
                             inPos.getName().c_str());
384
0
}
Unexecuted instantiation: GrGeometryProcessor.cpp:write_passthrough_vertex_position(GrGLSLVertexBuilder*, GrShaderVar const&, GrShaderVar*)
Unexecuted instantiation: GrGeometryProcessor.cpp:write_passthrough_vertex_position(GrGLSLVertexBuilder*, GrShaderVar const&, GrShaderVar*)
385
386
static void write_vertex_position(GrGLSLVertexBuilder* vertBuilder,
387
                                  GrGLSLUniformHandler* uniformHandler,
388
                                  const GrShaderCaps& shaderCaps,
389
                                  const GrShaderVar& inPos,
390
                                  const SkMatrix& matrix,
391
                                  const char* matrixName,
392
                                  GrShaderVar* outPos,
393
0
                                  ProgramImpl::UniformHandle* matrixUniform) {
394
0
    SkASSERT(inPos.getType() == kFloat3_GrSLType || inPos.getType() == kFloat2_GrSLType);
395
0
    SkString outName = vertBuilder->newTmpVarName(inPos.getName().c_str());
396
397
0
    if (matrix.isIdentity() && !shaderCaps.reducedShaderMode()) {
398
0
        write_passthrough_vertex_position(vertBuilder, inPos, outPos);
399
0
        return;
400
0
    }
401
0
    SkASSERT(matrixUniform);
402
403
0
    bool useCompactTransform = matrix.isScaleTranslate() && !shaderCaps.reducedShaderMode();
404
0
    const char* mangledMatrixName;
405
0
    *matrixUniform = uniformHandler->addUniform(nullptr,
406
0
                                                kVertex_GrShaderFlag,
407
0
                                                useCompactTransform ? kFloat4_GrSLType
408
0
                                                                    : kFloat3x3_GrSLType,
409
0
                                                matrixName,
410
0
                                                &mangledMatrixName);
411
412
0
    if (inPos.getType() == kFloat3_GrSLType) {
413
        // A float3 stays a float3 whether or not the matrix adds perspective
414
0
        if (useCompactTransform) {
415
0
            vertBuilder->codeAppendf("float3 %s = %s.xz1 * %s + %s.yw0;\n",
416
0
                                     outName.c_str(),
417
0
                                     mangledMatrixName,
418
0
                                     inPos.getName().c_str(),
419
0
                                     mangledMatrixName);
420
0
        } else {
421
0
            vertBuilder->codeAppendf("float3 %s = %s * %s;\n",
422
0
                                     outName.c_str(),
423
0
                                     mangledMatrixName,
424
0
                                     inPos.getName().c_str());
425
0
        }
426
0
        outPos->set(kFloat3_GrSLType, outName.c_str());
427
0
        return;
428
0
    }
429
0
    if (matrix.hasPerspective()) {
430
        // A float2 is promoted to a float3 if we add perspective via the matrix
431
0
        SkASSERT(!useCompactTransform);
432
0
        vertBuilder->codeAppendf("float3 %s = (%s * %s.xy1);",
433
0
                                 outName.c_str(),
434
0
                                 mangledMatrixName,
435
0
                                 inPos.getName().c_str());
436
0
        outPos->set(kFloat3_GrSLType, outName.c_str());
437
0
        return;
438
0
    }
439
0
    if (useCompactTransform) {
440
0
        vertBuilder->codeAppendf("float2 %s = %s.xz * %s + %s.yw;\n",
441
0
                                 outName.c_str(),
442
0
                                 mangledMatrixName,
443
0
                                 inPos.getName().c_str(),
444
0
                                 mangledMatrixName);
445
0
    } else if (shaderCaps.nonsquareMatrixSupport()) {
446
0
        vertBuilder->codeAppendf("float2 %s = float3x2(%s) * %s.xy1;\n",
447
0
                                 outName.c_str(),
448
0
                                 mangledMatrixName,
449
0
                                 inPos.getName().c_str());
450
0
    } else {
451
0
        vertBuilder->codeAppendf("float2 %s = (%s * %s.xy1).xy;\n",
452
0
                                 outName.c_str(),
453
0
                                 mangledMatrixName,
454
0
                                 inPos.getName().c_str());
455
0
    }
456
0
    outPos->set(kFloat2_GrSLType, outName.c_str());
457
0
}
Unexecuted instantiation: GrGeometryProcessor.cpp:write_vertex_position(GrGLSLVertexBuilder*, GrGLSLUniformHandler*, GrShaderCaps const&, GrShaderVar const&, SkMatrix const&, char const*, GrShaderVar*, GrResourceHandle<GrGLSLProgramDataManager::UniformHandleKind>*)
Unexecuted instantiation: GrGeometryProcessor.cpp:write_vertex_position(GrGLSLVertexBuilder*, GrGLSLUniformHandler*, GrShaderCaps const&, GrShaderVar const&, SkMatrix const&, char const*, GrShaderVar*, GrResourceHandle<GrGLSLProgramDataManager::UniformHandleKind>*)
458
459
void ProgramImpl::WriteOutputPosition(GrGLSLVertexBuilder* vertBuilder,
460
                                      GrGPArgs* gpArgs,
461
0
                                      const char* posName) {
462
    // writeOutputPosition assumes the incoming pos name points to a float2 variable
463
0
    GrShaderVar inPos(posName, kFloat2_GrSLType);
464
0
    write_passthrough_vertex_position(vertBuilder, inPos, &gpArgs->fPositionVar);
465
0
}
466
467
void ProgramImpl::WriteOutputPosition(GrGLSLVertexBuilder* vertBuilder,
468
                                      GrGLSLUniformHandler* uniformHandler,
469
                                      const GrShaderCaps& shaderCaps,
470
                                      GrGPArgs* gpArgs,
471
                                      const char* posName,
472
                                      const SkMatrix& mat,
473
0
                                      UniformHandle* viewMatrixUniform) {
474
0
    GrShaderVar inPos(posName, kFloat2_GrSLType);
475
0
    write_vertex_position(vertBuilder,
476
0
                          uniformHandler,
477
0
                          shaderCaps,
478
0
                          inPos,
479
0
                          mat,
480
0
                          "viewMatrix",
481
0
                          &gpArgs->fPositionVar,
482
0
                          viewMatrixUniform);
483
0
}
484
485
void ProgramImpl::WriteLocalCoord(GrGLSLVertexBuilder* vertBuilder,
486
                                  GrGLSLUniformHandler* uniformHandler,
487
                                  const GrShaderCaps& shaderCaps,
488
                                  GrGPArgs* gpArgs,
489
                                  GrShaderVar localVar,
490
                                  const SkMatrix& localMatrix,
491
0
                                  UniformHandle* localMatrixUniform) {
492
0
    write_vertex_position(vertBuilder,
493
0
                          uniformHandler,
494
0
                          shaderCaps,
495
0
                          localVar,
496
0
                          localMatrix,
497
0
                          "localMatrix",
498
0
                          &gpArgs->fLocalCoordVar,
499
0
                          localMatrixUniform);
500
0
}