xref: /aosp_15_r20/external/skia/src/gpu/ganesh/tessellate/GrPathTessellationShader.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2019 Google LLC.
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 #include "src/gpu/ganesh/tessellate/GrPathTessellationShader.h"
8 
9 #include "include/core/SkMatrix.h"
10 #include "include/private/base/SkAssert.h"
11 #include "include/private/base/SkMacros.h"
12 #include "include/private/base/SkPoint_impl.h"
13 #include "include/private/base/SkTArray.h"
14 #include "src/base/SkArenaAlloc.h"
15 #include "src/core/SkSLTypeShared.h"
16 #include "src/gpu/KeyBuilder.h"
17 #include "src/gpu/ganesh/GrShaderCaps.h"
18 #include "src/gpu/ganesh/GrShaderVar.h"
19 #include "src/gpu/ganesh/effects/GrDisableColorXP.h"
20 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
21 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
22 #include "src/gpu/ganesh/glsl/GrGLSLVarying.h"
23 #include "src/gpu/ganesh/glsl/GrGLSLVertexGeoBuilder.h"
24 #include "src/gpu/tessellate/Tessellation.h"
25 
26 #include <cstdint>
27 #include <memory>
28 
29 class GrAppliedHardClip;
30 
31 using namespace skia_private;
32 
33 namespace {
34 
35 using namespace skgpu::tess;
36 
37 // Draws a simple array of triangles.
38 class SimpleTriangleShader : public GrPathTessellationShader {
39 public:
SimpleTriangleShader(const SkMatrix & viewMatrix,SkPMColor4f color)40     SimpleTriangleShader(const SkMatrix& viewMatrix, SkPMColor4f color)
41             : GrPathTessellationShader(kTessellate_SimpleTriangleShader_ClassID,
42                                        GrPrimitiveType::kTriangles,
43                                        viewMatrix,
44                                        color,
45                                        PatchAttribs::kNone) {
46         constexpr static Attribute kInputPointAttrib{"inputPoint", kFloat2_GrVertexAttribType,
47                                                      SkSLType::kFloat2};
48         this->setVertexAttributesWithImplicitOffsets(&kInputPointAttrib, 1);
49     }
50 
51 private:
name() const52     const char* name() const final { return "tessellate_SimpleTriangleShader"; }
addToKey(const GrShaderCaps &,skgpu::KeyBuilder *) const53     void addToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const final {}
54     std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const final;
55 };
56 
makeProgramImpl(const GrShaderCaps &) const57 std::unique_ptr<GrGeometryProcessor::ProgramImpl> SimpleTriangleShader::makeProgramImpl(
58         const GrShaderCaps&) const {
59     class Impl : public GrPathTessellationShader::Impl {
60         void emitVertexCode(const GrShaderCaps&,
61                             const GrPathTessellationShader&,
62                             GrGLSLVertexBuilder* v,
63                             GrGLSLVaryingHandler*,
64                             GrGPArgs* gpArgs) override {
65             v->codeAppend(
66             "float2 localcoord = inputPoint;"
67             "float2 vertexpos = AFFINE_MATRIX * localcoord + TRANSLATE;");
68             gpArgs->fLocalCoordVar.set(SkSLType::kFloat2, "localcoord");
69             gpArgs->fPositionVar.set(SkSLType::kFloat2, "vertexpos");
70         }
71     };
72     return std::make_unique<Impl>();
73 }
74 
75 
76 // Uses instanced draws to triangulate standalone closed curves with a "middle-out" topology.
77 // Middle-out draws a triangle with vertices at T=[0, 1/2, 1] and then recurses breadth first:
78 //
79 //   depth=0: T=[0, 1/2, 1]
80 //   depth=1: T=[0, 1/4, 2/4], T=[2/4, 3/4, 1]
81 //   depth=2: T=[0, 1/8, 2/8], T=[2/8, 3/8, 4/8], T=[4/8, 5/8, 6/8], T=[6/8, 7/8, 1]
82 //   ...
83 //
84 // The shader determines how many segments are required to render each individual curve smoothly,
85 // and emits empty triangles at any vertices whose sk_VertexIDs are higher than necessary. It is the
86 // caller's responsibility to draw enough vertices per instance for the most complex curve in the
87 // batch to render smoothly (i.e., NumTrianglesAtResolveLevel() * 3).
88 class MiddleOutShader : public GrPathTessellationShader {
89 public:
MiddleOutShader(const GrShaderCaps & shaderCaps,const SkMatrix & viewMatrix,const SkPMColor4f & color,PatchAttribs attribs)90     MiddleOutShader(const GrShaderCaps& shaderCaps, const SkMatrix& viewMatrix,
91                     const SkPMColor4f& color, PatchAttribs attribs)
92             : GrPathTessellationShader(kTessellate_MiddleOutShader_ClassID,
93                                        GrPrimitiveType::kTriangles, viewMatrix, color, attribs) {
94         fInstanceAttribs.emplace_back("p01", kFloat4_GrVertexAttribType, SkSLType::kFloat4);
95         fInstanceAttribs.emplace_back("p23", kFloat4_GrVertexAttribType, SkSLType::kFloat4);
96         if (fAttribs & PatchAttribs::kFanPoint) {
97             fInstanceAttribs.emplace_back("fanPointAttrib",
98                                           kFloat2_GrVertexAttribType,
99                                           SkSLType::kFloat2);
100         }
101         if (fAttribs & PatchAttribs::kColor) {
102             fInstanceAttribs.emplace_back("colorAttrib",
103                                           (fAttribs & PatchAttribs::kWideColorIfEnabled)
104                                                   ? kFloat4_GrVertexAttribType
105                                                   : kUByte4_norm_GrVertexAttribType,
106                                           SkSLType::kHalf4);
107         }
108         if (fAttribs & PatchAttribs::kExplicitCurveType) {
109             // A conic curve is written out with p3=[w,Infinity], but GPUs that don't support
110             // infinity can't detect this. On these platforms we also write out an extra float with
111             // each patch that explicitly tells the shader what type of curve it is.
112             fInstanceAttribs.emplace_back("curveType", kFloat_GrVertexAttribType, SkSLType::kFloat);
113         }
114         this->setInstanceAttributesWithImplicitOffsets(fInstanceAttribs.data(),
115                                                        fInstanceAttribs.size());
116         SkASSERT(fInstanceAttribs.size() <= kMaxInstanceAttribCount);
117         SkASSERT(this->instanceStride() ==
118                  sizeof(SkPoint) * 4 + PatchAttribsStride(fAttribs));
119 
120         constexpr static Attribute kVertexAttrib("resolveLevel_and_idx", kFloat2_GrVertexAttribType,
121                                                  SkSLType::kFloat2);
122         this->setVertexAttributesWithImplicitOffsets(&kVertexAttrib, 1);
123     }
124 
125 private:
name() const126     const char* name() const final { return "tessellate_MiddleOutShader"; }
addToKey(const GrShaderCaps &,skgpu::KeyBuilder * b) const127     void addToKey(const GrShaderCaps&, skgpu::KeyBuilder* b) const final {
128         // When color is in a uniform, it's always wide so we need to ignore kWideColorIfEnabled.
129         // When color is in an attrib, its wideness is accounted for as part of the attrib key in
130         // GrGeometryProcessor::getAttributeKey().
131         // Either way, we get the correct key by ignoring .
132         b->add32((uint32_t)(fAttribs & ~PatchAttribs::kWideColorIfEnabled));
133     }
134     std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const final;
135 
136     constexpr static int kMaxInstanceAttribCount = 5;
137     STArray<kMaxInstanceAttribCount, Attribute> fInstanceAttribs;
138 };
139 
makeProgramImpl(const GrShaderCaps &) const140 std::unique_ptr<GrGeometryProcessor::ProgramImpl> MiddleOutShader::makeProgramImpl(
141         const GrShaderCaps&) const {
142     class Impl : public GrPathTessellationShader::Impl {
143         void emitVertexCode(const GrShaderCaps& shaderCaps,
144                             const GrPathTessellationShader& shader,
145                             GrGLSLVertexBuilder* v,
146                             GrGLSLVaryingHandler* varyingHandler,
147                             GrGPArgs* gpArgs) override {
148             const MiddleOutShader& middleOutShader = shader.cast<MiddleOutShader>();
149             v->defineConstant("PRECISION", skgpu::tess::kPrecision);
150             v->defineConstant("MAX_FIXED_RESOLVE_LEVEL",
151                               (float)skgpu::tess::kMaxResolveLevel);
152             v->defineConstant("MAX_FIXED_SEGMENTS",
153                               (float)(skgpu::tess::kMaxParametricSegments));
154             v->insertFunction(GrTessellationShader::WangsFormulaSkSL());
155             if (middleOutShader.fAttribs & PatchAttribs::kExplicitCurveType) {
156                 v->insertFunction(SkStringPrintf(
157                 "bool is_conic_curve() {"
158                     "return curveType != %g;"
159                 "}", skgpu::tess::kCubicCurveType).c_str());
160                 v->insertFunction(SkStringPrintf(
161                 "bool is_triangular_conic_curve() {"
162                     "return curveType == %g;"
163                 "}", skgpu::tess::kTriangularConicCurveType).c_str());
164             } else {
165                 SkASSERT(shaderCaps.fInfinitySupport);
166                 v->insertFunction(
167                 "bool is_conic_curve() { return isinf(p23.w); }"
168                 "bool is_triangular_conic_curve() { return isinf(p23.z); }");
169             }
170             if (shaderCaps.fBitManipulationSupport) {
171                 v->insertFunction(
172                 "float ldexp_portable(float x, float p) {"
173                     "return ldexp(x, int(p));"
174                 "}");
175             } else {
176                 v->insertFunction(
177                 "float ldexp_portable(float x, float p) {"
178                     "return x * exp2(p);"
179                 "}");
180             }
181             v->codeAppend(
182             "float resolveLevel = resolveLevel_and_idx.x;"
183             "float idxInResolveLevel = resolveLevel_and_idx.y;"
184             "float2 localcoord;");
185             if (middleOutShader.fAttribs & PatchAttribs::kFanPoint) {
186                 v->codeAppend(
187                 // A negative resolve level means this is the fan point.
188                 "if (resolveLevel < 0) {"
189                     "localcoord = fanPointAttrib;"
190                 "} else ");  // Fall through to next if (). Trailing space is important.
191             }
192             v->codeAppend(
193             "if (is_triangular_conic_curve()) {"
194                 // This patch is an exact triangle.
195                 "localcoord = (resolveLevel != 0) ? p01.zw"
196                            ": (idxInResolveLevel != 0) ? p23.xy"
197                                                       ": p01.xy;"
198             "} else {"
199                 "float2 p0=p01.xy, p1=p01.zw, p2=p23.xy, p3=p23.zw;"
200                 "float w = -1;"  // w < 0 tells us to treat the instance as an integral cubic.
201                 "float maxResolveLevel;"
202                 "if (is_conic_curve()) {"
203                     // Conics are 3 points, with the weight in p3.
204                     "w = p3.x;"
205                     "maxResolveLevel = wangs_formula_conic_log2(PRECISION, AFFINE_MATRIX * p0,"
206                                                                           "AFFINE_MATRIX * p1,"
207                                                                           "AFFINE_MATRIX * p2, w);"
208                     "p1 *= w;"  // Unproject p1.
209                     "p3 = p2;"  // Duplicate the endpoint for shared code that also runs on cubics.
210                 "} else {"
211                     // The patch is an integral cubic.
212                     "maxResolveLevel = wangs_formula_cubic_log2(PRECISION, p0, p1, p2, p3,"
213                                                                "AFFINE_MATRIX);"
214                 "}"
215                 "if (resolveLevel > maxResolveLevel) {"
216                     // This vertex is at a higher resolve level than we need. Demote to a lower
217                     // resolveLevel, which will produce a degenerate triangle.
218                     "idxInResolveLevel = floor(ldexp_portable(idxInResolveLevel,"
219                                                              "maxResolveLevel - resolveLevel));"
220                     "resolveLevel = maxResolveLevel;"
221                 "}"
222                 // Promote our location to a discrete position in the maximum fixed resolve level.
223                 // This is extra paranoia to ensure we get the exact same fp32 coordinates for
224                 // colocated points from different resolve levels (e.g., the vertices T=3/4 and
225                 // T=6/8 should be exactly colocated).
226                 "float fixedVertexID = floor(.5 + ldexp_portable("
227                         "idxInResolveLevel, MAX_FIXED_RESOLVE_LEVEL - resolveLevel));"
228                 "if (0 < fixedVertexID && fixedVertexID < MAX_FIXED_SEGMENTS) {"
229                     "float T = fixedVertexID * (1 / MAX_FIXED_SEGMENTS);"
230 
231                     // Evaluate at T. Use De Casteljau's for its accuracy and stability.
232                     "float2 ab = mix(p0, p1, T);"
233                     "float2 bc = mix(p1, p2, T);"
234                     "float2 cd = mix(p2, p3, T);"
235                     "float2 abc = mix(ab, bc, T);"
236                     "float2 bcd = mix(bc, cd, T);"
237                     "float2 abcd = mix(abc, bcd, T);"
238 
239                     // Evaluate the conic weight at T.
240                     "float u = mix(1.0, w, T);"
241                     "float v = w + 1 - u;"  // == mix(w, 1, T)
242                     "float uv = mix(u, v, T);"
243 
244                     "localcoord = (w < 0) ?" /*cubic*/ "abcd:" /*conic*/ "abc/uv;"
245                 "} else {"
246                     "localcoord = (fixedVertexID == 0) ? p0.xy : p3.xy;"
247                 "}"
248             "}"
249             "float2 vertexpos = AFFINE_MATRIX * localcoord + TRANSLATE;");
250             gpArgs->fLocalCoordVar.set(SkSLType::kFloat2, "localcoord");
251             gpArgs->fPositionVar.set(SkSLType::kFloat2, "vertexpos");
252             if (middleOutShader.fAttribs & PatchAttribs::kColor) {
253                 GrGLSLVarying colorVarying(SkSLType::kHalf4);
254                 varyingHandler->addVarying("color",
255                                            &colorVarying,
256                                            GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
257                 v->codeAppendf("%s = colorAttrib;", colorVarying.vsOut());
258                 fVaryingColorName = colorVarying.fsIn();
259             }
260         }
261     };
262     return std::make_unique<Impl>();
263 }
264 
265 }  // namespace
266 
Make(const GrShaderCaps & shaderCaps,SkArenaAlloc * arena,const SkMatrix & viewMatrix,const SkPMColor4f & color,PatchAttribs attribs)267 GrPathTessellationShader* GrPathTessellationShader::Make(const GrShaderCaps& shaderCaps,
268                                                          SkArenaAlloc* arena,
269                                                          const SkMatrix& viewMatrix,
270                                                          const SkPMColor4f& color,
271                                                          PatchAttribs attribs) {
272     // We should use explicit curve type when, and only when, there isn't infinity support.
273     // Otherwise the GPU can infer curve type based on infinity.
274     SkASSERT(shaderCaps.fInfinitySupport != (attribs & PatchAttribs::kExplicitCurveType));
275     return arena->make<MiddleOutShader>(shaderCaps, viewMatrix, color, attribs);
276 }
277 
MakeSimpleTriangleShader(SkArenaAlloc * arena,const SkMatrix & viewMatrix,const SkPMColor4f & color)278 GrPathTessellationShader* GrPathTessellationShader::MakeSimpleTriangleShader(
279         SkArenaAlloc* arena, const SkMatrix& viewMatrix, const SkPMColor4f& color) {
280     return arena->make<SimpleTriangleShader>(viewMatrix, color);
281 }
282 
MakeStencilOnlyPipeline(const ProgramArgs & args,GrAAType aaType,const GrAppliedHardClip & hardClip,GrPipeline::InputFlags pipelineFlags)283 const GrPipeline* GrPathTessellationShader::MakeStencilOnlyPipeline(
284         const ProgramArgs& args,
285         GrAAType aaType,
286         const GrAppliedHardClip& hardClip,
287         GrPipeline::InputFlags pipelineFlags) {
288     GrPipeline::InitArgs pipelineArgs;
289     pipelineArgs.fInputFlags = pipelineFlags;
290     pipelineArgs.fCaps = args.fCaps;
291     return args.fArena->make<GrPipeline>(pipelineArgs,
292                                          GrDisableColorXPFactory::MakeXferProcessor(),
293                                          hardClip);
294 }
295 
296 // Evaluate our point of interest using numerically stable linear interpolations. We add our own
297 // "safe_mix" method to guarantee we get exactly "b" when T=1. The builtin mix() function seems
298 // spec'd to behave this way, but empirical results results have shown it does not always.
299 const char* GrPathTessellationShader::Impl::kEvalRationalCubicFn =
300 "float3 safe_mix(float3 a, float3 b, float T, float one_minus_T) {"
301     "return a*one_minus_T + b*T;"
302 "}"
303 "float2 eval_rational_cubic(float4x3 P, float T) {"
304     "float one_minus_T = 1.0 - T;"
305     "float3 ab = safe_mix(P[0], P[1], T, one_minus_T);"
306     "float3 bc = safe_mix(P[1], P[2], T, one_minus_T);"
307     "float3 cd = safe_mix(P[2], P[3], T, one_minus_T);"
308     "float3 abc = safe_mix(ab, bc, T, one_minus_T);"
309     "float3 bcd = safe_mix(bc, cd, T, one_minus_T);"
310     "float3 abcd = safe_mix(abc, bcd, T, one_minus_T);"
311     "return abcd.xy / abcd.z;"
312 "}";
313 
onEmitCode(EmitArgs & args,GrGPArgs * gpArgs)314 void GrPathTessellationShader::Impl::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
315     const auto& shader = args.fGeomProc.cast<GrPathTessellationShader>();
316     args.fVaryingHandler->emitAttributes(shader);
317 
318     // Vertex shader.
319     const char* affineMatrix, *translate;
320     fAffineMatrixUniform = args.fUniformHandler->addUniform(nullptr, kVertex_GrShaderFlag,
321                                                             SkSLType::kFloat4, "affineMatrix",
322                                                             &affineMatrix);
323     fTranslateUniform = args.fUniformHandler->addUniform(nullptr, kVertex_GrShaderFlag,
324                                                          SkSLType::kFloat2, "translate", &translate);
325     args.fVertBuilder->codeAppendf("float2x2 AFFINE_MATRIX = float2x2(%s.xy, %s.zw);",
326                                    affineMatrix, affineMatrix);
327     args.fVertBuilder->codeAppendf("float2 TRANSLATE = %s;", translate);
328     this->emitVertexCode(*args.fShaderCaps,
329                          shader,
330                          args.fVertBuilder,
331                          args.fVaryingHandler,
332                          gpArgs);
333 
334     // Fragment shader.
335     if (!(shader.fAttribs & PatchAttribs::kColor)) {
336         const char* color;
337         fColorUniform = args.fUniformHandler->addUniform(nullptr, kFragment_GrShaderFlag,
338                                                          SkSLType::kHalf4, "color", &color);
339         args.fFragBuilder->codeAppendf("half4 %s = %s;", args.fOutputColor, color);
340     } else {
341         args.fFragBuilder->codeAppendf("half4 %s = %s;",
342                                        args.fOutputColor, fVaryingColorName.c_str());
343     }
344     args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
345 }
346 
setData(const GrGLSLProgramDataManager & pdman,const GrShaderCaps &,const GrGeometryProcessor & geomProc)347 void GrPathTessellationShader::Impl::setData(const GrGLSLProgramDataManager& pdman, const
348                                              GrShaderCaps&, const GrGeometryProcessor& geomProc) {
349     const auto& shader = geomProc.cast<GrPathTessellationShader>();
350     const SkMatrix& m = shader.viewMatrix();
351     pdman.set4f(fAffineMatrixUniform, m.getScaleX(), m.getSkewY(), m.getSkewX(), m.getScaleY());
352     pdman.set2f(fTranslateUniform, m.getTranslateX(), m.getTranslateY());
353 
354     if (!(shader.fAttribs & PatchAttribs::kColor)) {
355         const SkPMColor4f& color = shader.color();
356         pdman.set4f(fColorUniform, color.fR, color.fG, color.fB, color.fA);
357     }
358 }
359