Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrStrokeTessellationShader.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 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
9
10#include "src/gpu/KeyBuilder.h"
16
17namespace {
18
19// float2 robust_normalize_diff(float2 a, float b) { ... }
20//
21// Returns the normalized difference between a and b, i.e. normalize(a - b), with care taken for
22// if 'a' and/or 'b' have large coordinates.
23static const char* kRobustNormalizeDiffFn =
24"float2 robust_normalize_diff(float2 a, float2 b) {"
25 "float2 diff = a - b;"
26 "if (diff == float2(0.0)) {"
27 "return float2(0.0);"
28 "} else {"
29 "float invMag = 1.0 / max(abs(diff.x), abs(diff.y));"
30 "return normalize(invMag * diff);"
31 "}"
32"}";
33
34// float cosine_between_unit_vectors(float2 a, float2 b) { ...
35//
36// Returns the cosine of the angle between a and b, assuming a and b are unit vectors already.
37// Guaranteed to be between [-1, 1].
38static const char* kCosineBetweenUnitVectorsFn =
39"float cosine_between_unit_vectors(float2 a, float2 b) {"
40 // Since a and b are assumed to be normalized, the cosine is equal to the dot product, although
41 // we clamp that to ensure it falls within the expected range of [-1, 1].
42 "return clamp(dot(a, b), -1.0, 1.0);"
43"}"
44;
45
46
47// float miter_extent(float cosTheta, float miterLimit) { ...
48//
49// Extends the middle radius to either the miter point, or the bevel edge if we surpassed the
50// miter limit and need to revert to a bevel join.
51static const char* kMiterExtentFn =
52"float miter_extent(float cosTheta, float miterLimit) {"
53 "float x = fma(cosTheta, .5, .5);"
54 "return (x * miterLimit * miterLimit >= 1.0) ? inversesqrt(x) : sqrt(x);"
55"}"
56;
57
58// float num_radial_segments_per_radian(float approxDevStrokeRadius) { ...
59//
60// Returns the number of radial segments required for each radian of rotation, in order for the
61// curve to appear "smooth" as defined by the approximate device-space stroke radius.
62static const char* kNumRadialSegmentsPerRadianFn =
63"float num_radial_segments_per_radian(float approxDevStrokeRadius) {"
64 "return .5 / acos(max(1.0 - (1.0 / PRECISION) / approxDevStrokeRadius, -1.0));"
65"}";
66
67// float<N> unchecked_mix(float<N> a, float<N> b, float<N> T) { ...
68//
69// Unlike mix(), this does not return b when t==1. But it otherwise seems to get better
70// precision than "a*(1 - t) + b*t" for things like chopping cubics on exact cusp points.
71// We override this result anyway when t==1 so it shouldn't be a problem.
72static const char* kUncheckedMixFn =
73"float unchecked_mix(float a, float b, float T) {"
74 "return fma(b - a, T, a);"
75"}"
76"float2 unchecked_mix(float2 a, float2 b, float T) {"
77 "return fma(b - a, float2(T), a);"
78"}"
79"float4 unchecked_mix(float4 a, float4 b, float4 T) {"
80 "return fma(b - a, T, a);"
81"}"
82;
83
85
86} // anonymous namespace
87
89 PatchAttribs attribs,
90 const SkMatrix& viewMatrix,
91 const SkStrokeRec& stroke,
93 : GrTessellationShader(kTessellate_GrStrokeTessellationShader_ClassID,
95 , fPatchAttribs(attribs | PatchAttribs::kJoinControlPoint)
96 , fStroke(stroke) {
97 // We should use explicit curve type when, and only when, there isn't infinity support.
98 // Otherwise the GPU can infer curve type based on infinity.
99 SkASSERT(shaderCaps.fInfinitySupport != (attribs & PatchAttribs::kExplicitCurveType));
100 // pts 0..3 define the stroke as a cubic bezier. If p3.y is infinity, then it's a conic
101 // with w=p3.x.
102 //
103 // An empty stroke (p0==p1==p2==p3) is a special case that denotes a circle, or
104 // 180-degree point stroke.
105 fAttribs.emplace_back("pts01Attr", kFloat4_GrVertexAttribType, SkSLType::kFloat4);
106 fAttribs.emplace_back("pts23Attr", kFloat4_GrVertexAttribType, SkSLType::kFloat4);
107
108 // argsAttr contains the lastControlPoint for setting up the join.
109 fAttribs.emplace_back("argsAttr", kFloat2_GrVertexAttribType, SkSLType::kFloat2);
110
111 if (fPatchAttribs & PatchAttribs::kStrokeParams) {
112 fAttribs.emplace_back("dynamicStrokeAttr", kFloat2_GrVertexAttribType,
114 }
115 if (fPatchAttribs & PatchAttribs::kColor) {
116 fAttribs.emplace_back("dynamicColorAttr",
117 (fPatchAttribs & PatchAttribs::kWideColorIfEnabled)
121 }
122 if (fPatchAttribs & PatchAttribs::kExplicitCurveType) {
123 // A conic curve is written out with p3=[w,Infinity], but GPUs that don't support
124 // infinity can't detect this. On these platforms we write out an extra float with each
125 // patch that explicitly tells the shader what type of curve it is.
126 fAttribs.emplace_back("curveTypeAttr", kFloat_GrVertexAttribType, SkSLType::kFloat);
127 }
128
129 this->setInstanceAttributesWithImplicitOffsets(fAttribs.data(), fAttribs.size());
130 SkASSERT(this->instanceStride() == sizeof(SkPoint) * 4 + PatchAttribsStride(fPatchAttribs));
131 if (!shaderCaps.fVertexIDSupport) {
132 constexpr static Attribute kVertexAttrib("edgeID", kFloat_GrVertexAttribType,
134 this->setVertexAttributesWithImplicitOffsets(&kVertexAttrib, 1);
135 }
136 SkASSERT(fAttribs.size() <= kMaxAttribCount);
137}
138
139// This base class emits shader code for our parametric/radial stroke tessellation algorithm
140// described above. The subclass emits its own specific setup code before calling into
141// emitTessellationCode and emitFragment code.
143 void onEmitCode(EmitArgs&, GrGPArgs*) override;
144
145 // Emits code that calculates the vertex position and any other inputs to the fragment shader.
146 // The onEmitCode() is responsible to define the following symbols before calling this method:
147 //
148 // // Functions.
149 // float2 unchecked_mix(float2, float2, float);
150 // float unchecked_mix(float, float, float);
151 //
152 // // Values provided by either uniforms or attribs.
153 // float2 p0, p1, p2, p3;
154 // float w;
155 // float STROKE_RADIUS;
156 // float 2x2 AFFINE_MATRIX;
157 // float2 TRANSLATE;
158 //
159 // // Values calculated by the specific subclass.
160 // float combinedEdgeID;
161 // bool isFinalEdge;
162 // float numParametricSegments;
163 // float radsPerSegment;
164 // float2 tan0; // Must be pre-normalized
165 // float2 tan1; // Must be pre-normalized
166 // float strokeOutset;
167 //
168 void emitTessellationCode(const GrStrokeTessellationShader& shader, SkString* code,
169 GrGPArgs* gpArgs, const GrShaderCaps& shaderCaps) const;
170
171 // Emits all necessary fragment code. If using dynamic color, the impl is responsible to set up
172 // a half4 varying for color and provide its name in 'fDynamicColorName'.
173 void emitFragmentCode(const GrStrokeTessellationShader&, const EmitArgs&);
174
175 void setData(const GrGLSLProgramDataManager& pdman, const GrShaderCaps&,
176 const GrGeometryProcessor&) final;
177
178 GrGLSLUniformHandler::UniformHandle fTessControlArgsUniform;
179 GrGLSLUniformHandler::UniformHandle fTranslateUniform;
180 GrGLSLUniformHandler::UniformHandle fAffineMatrixUniform;
182 SkString fDynamicColorName;
183};
184
186 const auto& shader = args.fGeomProc.cast<GrStrokeTessellationShader>();
187 SkPaint::Join joinType = shader.stroke().getJoin();
188 args.fVaryingHandler->emitAttributes(shader);
189
190 args.fVertBuilder->defineConstant("float", "PI", "3.141592653589793238");
191 args.fVertBuilder->defineConstant("PRECISION", skgpu::tess::kPrecision);
192 // There is an artificial maximum number of edges (compared to the max limit calculated based on
193 // the number of radial segments per radian, Wang's formula, and join type). When there is
194 // vertex ID support, the limit is what can be represented in a uint16; otherwise the limit is
195 // the size of the fallback vertex buffer.
196 float maxEdges = args.fShaderCaps->fVertexIDSupport ? FixedCountStrokes::kMaxEdges
197 : FixedCountStrokes::kMaxEdgesNoVertexIDs;
198 args.fVertBuilder->defineConstant("NUM_TOTAL_EDGES", maxEdges);
199
200 // Helper functions.
201 if (shader.hasDynamicStroke()) {
202 args.fVertBuilder->insertFunction(kNumRadialSegmentsPerRadianFn);
203 }
204 args.fVertBuilder->insertFunction(kRobustNormalizeDiffFn);
205 args.fVertBuilder->insertFunction(kCosineBetweenUnitVectorsFn);
206 args.fVertBuilder->insertFunction(kMiterExtentFn);
207 args.fVertBuilder->insertFunction(kUncheckedMixFn);
208 args.fVertBuilder->insertFunction(GrTessellationShader::WangsFormulaSkSL());
209
210 // Tessellation control uniforms and/or dynamic attributes.
211 if (!shader.hasDynamicStroke()) {
212 // [NUM_RADIAL_SEGMENTS_PER_RADIAN, JOIN_TYPE, STROKE_RADIUS]
213 const char* tessArgsName;
214 fTessControlArgsUniform = args.fUniformHandler->addUniform(
215 nullptr, kVertex_GrShaderFlag, SkSLType::kFloat3, "tessControlArgs",
216 &tessArgsName);
217 args.fVertBuilder->codeAppendf(
218 "float NUM_RADIAL_SEGMENTS_PER_RADIAN = %s.x;"
219 "float JOIN_TYPE = %s.y;"
220 "float STROKE_RADIUS = %s.z;", tessArgsName, tessArgsName, tessArgsName);
221 } else {
222 // The shader does not currently support dynamic hairlines, so this case only needs to
223 // configure NUM_RADIAL_SEGMENTS_PER_RADIAN based on the fixed maxScale and per-instance
224 // stroke radius attribute that's defined in local space.
225 SkASSERT(!shader.stroke().isHairlineStyle());
226 const char* maxScaleName;
227 fTessControlArgsUniform = args.fUniformHandler->addUniform(
228 nullptr, kVertex_GrShaderFlag, SkSLType::kFloat, "maxScale",
229 &maxScaleName);
230 args.fVertBuilder->codeAppendf(
231 "float STROKE_RADIUS = dynamicStrokeAttr.x;"
232 "float JOIN_TYPE = dynamicStrokeAttr.y;"
233 "float NUM_RADIAL_SEGMENTS_PER_RADIAN = num_radial_segments_per_radian("
234 "%s * STROKE_RADIUS);", maxScaleName);
235
236 }
237
238 if (shader.hasDynamicColor()) {
239 // Create a varying for color to get passed in through.
240 GrGLSLVarying dynamicColor{SkSLType::kHalf4};
241 args.fVaryingHandler->addVarying("dynamicColor", &dynamicColor);
242 args.fVertBuilder->codeAppendf("%s = dynamicColorAttr;", dynamicColor.vsOut());
243 fDynamicColorName = dynamicColor.fsIn();
244 }
245
246 // View matrix uniforms.
247 const char* translateName, *affineMatrixName;
248 fAffineMatrixUniform = args.fUniformHandler->addUniform(nullptr, kVertex_GrShaderFlag,
249 SkSLType::kFloat4, "affineMatrix",
250 &affineMatrixName);
251 fTranslateUniform = args.fUniformHandler->addUniform(nullptr, kVertex_GrShaderFlag,
252 SkSLType::kFloat2, "translate",
253 &translateName);
254 args.fVertBuilder->codeAppendf("float2x2 AFFINE_MATRIX = float2x2(%s.xy, %s.zw);\n",
255 affineMatrixName, affineMatrixName);
256 args.fVertBuilder->codeAppendf("float2 TRANSLATE = %s;\n", translateName);
257
258 if (shader.hasExplicitCurveType()) {
259 args.fVertBuilder->insertFunction(SkStringPrintf(
260 "bool is_conic_curve() { return curveTypeAttr != %g; }",
262 } else {
263 args.fVertBuilder->insertFunction(
264 "bool is_conic_curve() { return isinf(pts23Attr.w); }");
265 }
266
267 // Tessellation code.
268 args.fVertBuilder->codeAppend(
269 "float2 p0=pts01Attr.xy, p1=pts01Attr.zw, p2=pts23Attr.xy, p3=pts23Attr.zw;"
270 "float2 lastControlPoint = argsAttr.xy;"
271 "float w = -1;" // w<0 means the curve is an integral cubic.
272 "if (is_conic_curve()) {"
273 // Conics are 3 points, with the weight in p3.
274 "w = p3.x;"
275 "p3 = p2;" // Setting p3 equal to p2 works for the remaining rotational logic.
276 "}"
277 );
278
279 // Emit code to call Wang's formula to determine parametric segments. We do this before
280 // transform points for hairlines so that it is consistent with how the CPU tested the control
281 // points for chopping.
282 args.fVertBuilder->codeAppend(
283 // Find how many parametric segments this stroke requires.
284 "float numParametricSegments;"
285 "if (w < 0) {"
286 "if (p0 == p1 && p2 == p3) {"
287 "numParametricSegments = 1;" // a line
288 "} else {"
289 "numParametricSegments = wangs_formula_cubic(PRECISION, p0, p1, p2, p3, AFFINE_MATRIX);"
290 "}"
291 "} else {"
292 "numParametricSegments = wangs_formula_conic(PRECISION,"
293 "AFFINE_MATRIX * p0,"
294 "AFFINE_MATRIX * p1,"
295 "AFFINE_MATRIX * p2, w);"
296 "}"
297 );
298
299 if (shader.stroke().isHairlineStyle()) {
300 // Hairline case. Transform the points before tessellation. We can still hold off on the
301 // translate until the end; we just need to perform the scale and skew right now.
302 args.fVertBuilder->codeAppend(
303 "p0 = AFFINE_MATRIX * p0;"
304 "p1 = AFFINE_MATRIX * p1;"
305 "p2 = AFFINE_MATRIX * p2;"
306 "p3 = AFFINE_MATRIX * p3;"
307 "lastControlPoint = AFFINE_MATRIX * lastControlPoint;"
308 );
309 }
310
311 args.fVertBuilder->codeAppend(
312 // Find the starting and ending tangents.
313 "float2 tan0 = robust_normalize_diff((p0 == p1) ? ((p1 == p2) ? p3 : p2) : p1, p0);"
314 "float2 tan1 = robust_normalize_diff(p3, (p3 == p2) ? ((p2 == p1) ? p0 : p1) : p2);"
315 "if (tan0 == float2(0)) {"
316 // The stroke is a point. This special case tells us to draw a stroke-width circle as a
317 // 180 degree point stroke instead.
318 "tan0 = float2(1,0);"
319 "tan1 = float2(-1,0);"
320 "}"
321 );
322
323 if (args.fShaderCaps->fVertexIDSupport) {
324 // If we don't have sk_VertexID support then "edgeID" already came in as a vertex attrib.
325 args.fVertBuilder->codeAppend(
326 "float edgeID = float(sk_VertexID >> 1);"
327 "if ((sk_VertexID & 1) != 0) {"
328 "edgeID = -edgeID;"
329 "}"
330 );
331 }
332
333 // Potential optimization: (shader.hasDynamicStroke() && shader.hasRoundJoins())?
334 if (shader.stroke().getJoin() == SkPaint::kRound_Join || shader.hasDynamicStroke()) {
335 args.fVertBuilder->codeAppend(
336 // Determine how many edges to give to the round join. We emit the first and final edges
337 // of the join twice: once full width and once restricted to half width. This guarantees
338 // perfect seaming by matching the vertices from the join as well as from the strokes on
339 // either side.
340 "float2 prevTan = robust_normalize_diff(p0, lastControlPoint);"
341 "float joinRads = acos(cosine_between_unit_vectors(prevTan, tan0));"
342 "float numRadialSegmentsInJoin = max(ceil(joinRads * NUM_RADIAL_SEGMENTS_PER_RADIAN), 1);"
343 // +2 because we emit the beginning and ending edges twice (see above comment).
344 "float numEdgesInJoin = numRadialSegmentsInJoin + 2;"
345 // The stroke section needs at least two edges. Don't assign more to the join than
346 // "NUM_TOTAL_EDGES - 2". (This is only relevant when the ideal max edge count calculated
347 // on the CPU had to be limited to NUM_TOTAL_EDGES in the draw call).
348 "numEdgesInJoin = min(numEdgesInJoin, NUM_TOTAL_EDGES - 2);");
349 if (shader.hasDynamicStroke()) {
350 args.fVertBuilder->codeAppend(
351 "if (JOIN_TYPE >= 0) {" // Is the join not a round type?
352 // Bevel and miter joins get 1 and 2 segments respectively.
353 // +2 because we emit the beginning and ending edges twice (see above comments).
354 "numEdgesInJoin = sign(JOIN_TYPE) + 1 + 2;"
355 "}");
356 }
357 } else {
358 args.fVertBuilder->codeAppendf("float numEdgesInJoin = %i;",
360 }
361
362 args.fVertBuilder->codeAppend(
363 // Find which direction the curve turns.
364 // NOTE: Since the curve is not allowed to inflect, we can just check F'(.5) x F''(.5).
365 // NOTE: F'(.5) x F''(.5) has the same sign as (P2 - P0) x (P3 - P1)
366 "float turn = cross_length_2d(p2 - p0, p3 - p1);"
367 "float combinedEdgeID = abs(edgeID) - numEdgesInJoin;"
368 "if (combinedEdgeID < 0) {"
369 "tan1 = tan0;"
370 // Don't let tan0 become zero. The code as-is isn't built to handle that case. tan0=0
371 // means the join is disabled, and to disable it with the existing code we can leave
372 // tan0 equal to tan1.
373 "if (lastControlPoint != p0) {"
374 "tan0 = robust_normalize_diff(p0, lastControlPoint);"
375 "}"
376 "turn = cross_length_2d(tan0, tan1);"
377 "}"
378
379 // Calculate the curve's starting angle and rotation.
380 "float cosTheta = cosine_between_unit_vectors(tan0, tan1);"
381 "float rotation = acos(cosTheta);"
382 "if (turn < 0) {"
383 // Adjust sign of rotation to match the direction the curve turns.
384 "rotation = -rotation;"
385 "}"
386
387 "float numRadialSegments;"
388 "float strokeOutset = sign(edgeID);"
389 "if (combinedEdgeID < 0) {"
390 // We belong to the preceding join. The first and final edges get duplicated, so we only
391 // have "numEdgesInJoin - 2" segments.
392 "numRadialSegments = numEdgesInJoin - 2;"
393 "numParametricSegments = 1;" // Joins don't have parametric segments.
394 "p3 = p2 = p1 = p0;" // Colocate all points on the junction point.
395 // Shift combinedEdgeID to the range [-1, numRadialSegments]. This duplicates the first
396 // edge and lands one edge at the very end of the join. (The duplicated final edge will
397 // actually come from the section of our strip that belongs to the stroke.)
398 "combinedEdgeID += numRadialSegments + 1;"
399 // We normally restrict the join on one side of the junction, but if the tangents are
400 // nearly equivalent this could theoretically result in bad seaming and/or cracks on the
401 // side we don't put it on. If the tangents are nearly equivalent then we leave the join
402 // double-sided.
403 " float sinEpsilon = 1e-2;" // ~= sin(180deg / 3000)
404 "bool tangentsNearlyParallel ="
405 "(abs(turn) * inversesqrt(dot(tan0, tan0) * dot(tan1, tan1))) < sinEpsilon;"
406 "if (!tangentsNearlyParallel || dot(tan0, tan1) < 0) {"
407 // There are two edges colocated at the beginning. Leave the first one double sided
408 // for seaming with the previous stroke. (The double sided edge at the end will
409 // actually come from the section of our strip that belongs to the stroke.)
410 "if (combinedEdgeID >= 0) {"
411 "strokeOutset = (turn < 0) ? min(strokeOutset, 0) : max(strokeOutset, 0);"
412 "}"
413 "}"
414 "combinedEdgeID = max(combinedEdgeID, 0);"
415 "} else {"
416 // We belong to the stroke. Unless NUM_RADIAL_SEGMENTS_PER_RADIAN is incredibly high,
417 // clamping to maxCombinedSegments will be a no-op because the draw call was invoked with
418 // sufficient vertices to cover the worst case scenario of 180 degree rotation.
419 "float maxCombinedSegments = NUM_TOTAL_EDGES - numEdgesInJoin - 1;"
420 "numRadialSegments = max(ceil(abs(rotation) * NUM_RADIAL_SEGMENTS_PER_RADIAN), 1);"
421 "numRadialSegments = min(numRadialSegments, maxCombinedSegments);"
422 "numParametricSegments = min(numParametricSegments,"
423 "maxCombinedSegments - numRadialSegments + 1);"
424 "}"
425
426 // Additional parameters for emitTessellationCode().
427 "float radsPerSegment = rotation / numRadialSegments;"
428 "float numCombinedSegments = numParametricSegments + numRadialSegments - 1;"
429 "bool isFinalEdge = (combinedEdgeID >= numCombinedSegments);"
430 "if (combinedEdgeID > numCombinedSegments) {"
431 "strokeOutset = 0;" // The strip has more edges than we need. Drop this one.
432 "}");
433
434 if (joinType == SkPaint::kMiter_Join || shader.hasDynamicStroke()) {
435 args.fVertBuilder->codeAppendf(
436 // Edge #2 extends to the miter point.
437 "if (abs(edgeID) == 2 && %s) {"
438 "strokeOutset *= miter_extent(cosTheta, JOIN_TYPE);" // miterLimit
439 "}", shader.hasDynamicStroke() ? "JOIN_TYPE > 0" /*Is the join a miter type?*/ : "true");
440 }
441
442 this->emitTessellationCode(shader, &args.fVertBuilder->code(), gpArgs, *args.fShaderCaps);
443
444 this->emitFragmentCode(shader, args);
445}
446
447void GrStrokeTessellationShader::Impl::emitTessellationCode(
448 const GrStrokeTessellationShader& shader, SkString* code, GrGPArgs* gpArgs,
449 const GrShaderCaps& shaderCaps) const {
450 // The subclass is responsible to define the following symbols before calling this method:
451 //
452 // // Functions.
453 // float2 unchecked_mix(float2, float2, float);
454 // float unchecked_mix(float, float, float);
455 //
456 // // Values provided by either uniforms or attribs.
457 // float2 p0, p1, p2, p3;
458 // float w;
459 // float STROKE_RADIUS;
460 // float 2x2 AFFINE_MATRIX;
461 // float2 TRANSLATE;
462 //
463 // // Values calculated by the specific subclass.
464 // float combinedEdgeID;
465 // bool isFinalEdge;
466 // float numParametricSegments;
467 // float radsPerSegment;
468 // float2 tan0; // Must be pre-normalized
469 // float2 tan1; // Must be pre-normalized
470 // float strokeOutset;
471 //
472 code->appendf(
473 "float2 tangent, strokeCoord;"
474 "if (combinedEdgeID != 0 && !isFinalEdge) {"
475 // Compute the location and tangent direction of the stroke edge with the integral id
476 // "combinedEdgeID", where combinedEdgeID is the sorted-order index of parametric and radial
477 // edges. Start by finding the tangent function's power basis coefficients. These define a
478 // tangent direction (scaled by some uniform value) as:
479 // |T^2|
480 // Tangent_Direction(T) = dx,dy = |A 2B C| * |T |
481 // |. . .| |1 |
482 "float2 A, B, C = p1 - p0;"
483 "float2 D = p3 - p0;"
484 "if (w >= 0.0) {"
485 // P0..P2 represent a conic and P3==P2. The derivative of a conic has a cumbersome
486 // order-4 denominator. However, this isn't necessary if we are only interested in a
487 // vector in the same *direction* as a given tangent line. Since the denominator scales
488 // dx and dy uniformly, we can throw it out completely after evaluating the derivative
489 // with the standard quotient rule. This leaves us with a simpler quadratic function
490 // that we use to find a tangent.
491 "C *= w;"
492 "B = .5*D - C;"
493 "A = (w - 1.0) * D;"
494 "p1 *= w;"
495 "} else {"
496 "float2 E = p2 - p1;"
497 "B = E - C;"
498 "A = fma(float2(-3), E, D);"
499 "}"
500 // FIXME(crbug.com/800804,skbug.com/11268): Consider normalizing the exponents in A,B,C at
501 // this point in order to prevent fp32 overflow.
502
503 // Now find the coefficients that give a tangent direction from a parametric edge ID:
504 //
505 // |parametricEdgeID^2|
506 // Tangent_Direction(parametricEdgeID) = dx,dy = |A B_ C_| * |parametricEdgeID |
507 // |. . .| |1 |
508 //
509 "float2 B_ = B * (numParametricSegments * 2.0);"
510 "float2 C_ = C * (numParametricSegments * numParametricSegments);"
511
512 // Run a binary search to determine the highest parametric edge that is located on or before
513 // the combinedEdgeID. A combined ID is determined by the sum of complete parametric and
514 // radial segments behind it. i.e., find the highest parametric edge where:
515 //
516 // parametricEdgeID + floor(numRadialSegmentsAtParametricT) <= combinedEdgeID
517 //
518 "float lastParametricEdgeID = 0.0;"
519 "float maxParametricEdgeID = min(numParametricSegments - 1.0, combinedEdgeID);"
520 "float negAbsRadsPerSegment = -abs(radsPerSegment);"
521 "float maxRotation0 = (1.0 + combinedEdgeID) * abs(radsPerSegment);"
522 "for (int exp = %i - 1; exp >= 0; --exp) {"
523 // Test the parametric edge at lastParametricEdgeID + 2^exp.
524 "float testParametricID = lastParametricEdgeID + exp2(float(exp));"
525 "if (testParametricID <= maxParametricEdgeID) {"
526 "float2 testTan = fma(float2(testParametricID), A, B_);"
527 "testTan = fma(float2(testParametricID), testTan, C_);"
528 "float cosRotation = dot(normalize(testTan), tan0);"
529 "float maxRotation = fma(testParametricID, negAbsRadsPerSegment, maxRotation0);"
530 "maxRotation = min(maxRotation, PI);"
531 // Is rotation <= maxRotation? (i.e., is the number of complete radial segments
532 // behind testT, + testParametricID <= combinedEdgeID?)
533 "if (cosRotation >= cos(maxRotation)) {"
534 // testParametricID is on or before the combinedEdgeID. Keep it!
535 "lastParametricEdgeID = testParametricID;"
536 "}"
537 "}"
538 "}"
539
540 // Find the T value of the parametric edge at lastParametricEdgeID.
541 "float parametricT = lastParametricEdgeID / numParametricSegments;"
542
543 // Now that we've identified the highest parametric edge on or before the
544 // combinedEdgeID, the highest radial edge is easy:
545 "float lastRadialEdgeID = combinedEdgeID - lastParametricEdgeID;"
546
547 // Find the angle of tan0, i.e. the angle between tan0 and the positive x axis.
548 "float angle0 = acos(clamp(tan0.x, -1.0, 1.0));"
549 "angle0 = tan0.y >= 0.0 ? angle0 : -angle0;"
550
551 // Find the tangent vector on the edge at lastRadialEdgeID. By construction it is already
552 // normalized.
553 "float radialAngle = fma(lastRadialEdgeID, radsPerSegment, angle0);"
554 "tangent = float2(cos(radialAngle), sin(radialAngle));"
555 "float2 norm = float2(-tangent.y, tangent.x);"
556
557 // Find the T value where the tangent is orthogonal to norm. This is a quadratic:
558 //
559 // dot(norm, Tangent_Direction(T)) == 0
560 //
561 // |T^2|
562 // norm * |A 2B C| * |T | == 0
563 // |. . .| |1 |
564 //
565 "float a=dot(norm,A), b_over_2=dot(norm,B), c=dot(norm,C);"
566 "float discr_over_4 = max(b_over_2*b_over_2 - a*c, 0.0);"
567 "float q = sqrt(discr_over_4);"
568 "if (b_over_2 > 0.0) {"
569 "q = -q;"
570 "}"
571 "q -= b_over_2;"
572
573 // Roots are q/a and c/q. Since each curve section does not inflect or rotate more than 180
574 // degrees, there can only be one tangent orthogonal to "norm" inside 0..1. Pick the root
575 // nearest .5.
576 "float _5qa = -.5*q*a;"
577 "float2 root = (abs(fma(q,q,_5qa)) < abs(fma(a,c,_5qa))) ? float2(q,a) : float2(c,q);"
578 "float radialT = (root.t != 0.0) ? root.s / root.t : 0.0;"
579 "radialT = clamp(radialT, 0.0, 1.0);"
580
581 "if (lastRadialEdgeID == 0.0) {"
582 // The root finder above can become unstable when lastRadialEdgeID == 0 (e.g., if
583 // there are roots at exatly 0 and 1 both). radialT should always == 0 in this case.
584 "radialT = 0.0;"
585 "}"
586
587 // Now that we've identified the T values of the last parametric and radial edges, our final
588 // T value for combinedEdgeID is whichever is larger.
589 "float T = max(parametricT, radialT);"
590
591 // Evaluate the cubic at T. Use De Casteljau's for its accuracy and stability.
592 "float2 ab = unchecked_mix(p0, p1, T);"
593 "float2 bc = unchecked_mix(p1, p2, T);"
594 "float2 cd = unchecked_mix(p2, p3, T);"
595 "float2 abc = unchecked_mix(ab, bc, T);"
596 "float2 bcd = unchecked_mix(bc, cd, T);"
597 "float2 abcd = unchecked_mix(abc, bcd, T);"
598
599 // Evaluate the conic weight at T.
600 "float u = unchecked_mix(1.0, w, T);"
601 "float v = w + 1 - u;" // == mix(w, 1, T)
602 "float uv = unchecked_mix(u, v, T);"
603
604 // If we went with T=parametricT, then update the tangent. Otherwise leave it at the radial
605 // tangent found previously. (In the event that parametricT == radialT, we keep the radial
606 // tangent.)
607 "if (T != radialT) {"
608 // We must re-normalize here because the tangent is determined by the curve coefficients
609 "tangent = w >= 0.0 ? robust_normalize_diff(bc*u, ab*v)"
610 ": robust_normalize_diff(bcd, abc);"
611 "}"
612
613 "strokeCoord = (w >= 0.0) ? abc/uv : abcd;"
614 "} else {"
615 // Edges at the beginning and end of the strip use exact endpoints and tangents. This
616 // ensures crack-free seaming between instances.
617 "tangent = (combinedEdgeID == 0) ? tan0 : tan1;"
618 "strokeCoord = (combinedEdgeID == 0) ? p0 : p3;"
619 "}", skgpu::tess::kMaxResolveLevel /* Parametric/radial sort loop count. */);
620
621 code->append(
622 // At this point 'tangent' is normalized, so the orthogonal vector is also normalized.
623 "float2 ortho = float2(tangent.y, -tangent.x);"
624 "strokeCoord += ortho * (STROKE_RADIUS * strokeOutset);");
625
626 if (!shader.stroke().isHairlineStyle()) {
627 // Normal case. Do the transform after tessellation.
628 code->append("float2 devCoord = AFFINE_MATRIX * strokeCoord + TRANSLATE;");
629 gpArgs->fPositionVar.set(SkSLType::kFloat2, "devCoord");
630 gpArgs->fLocalCoordVar.set(SkSLType::kFloat2, "strokeCoord");
631 } else {
632 // Hairline case. The scale and skew already happened before tessellation.
633 code->append(
634 "float2 devCoord = strokeCoord + TRANSLATE;"
635 "float2 localCoord = inverse(AFFINE_MATRIX) * strokeCoord;");
636 gpArgs->fPositionVar.set(SkSLType::kFloat2, "devCoord");
637 gpArgs->fLocalCoordVar.set(SkSLType::kFloat2, "localCoord");
638 }
639}
640
641void GrStrokeTessellationShader::Impl::emitFragmentCode(const GrStrokeTessellationShader& shader,
642 const EmitArgs& args) {
643 if (!shader.hasDynamicColor()) {
644 // The fragment shader just outputs a uniform color.
645 const char* colorUniformName;
646 fColorUniform = args.fUniformHandler->addUniform(nullptr, kFragment_GrShaderFlag,
647 SkSLType::kHalf4, "color",
648 &colorUniformName);
649 args.fFragBuilder->codeAppendf("half4 %s = %s;", args.fOutputColor, colorUniformName);
650 } else {
651 args.fFragBuilder->codeAppendf("half4 %s = %s;", args.fOutputColor,
652 fDynamicColorName.c_str());
653 }
654 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
655}
656
658 const GrShaderCaps&,
659 const GrGeometryProcessor& geomProc) {
660 const auto& shader = geomProc.cast<GrStrokeTessellationShader>();
661 const auto& stroke = shader.stroke();
662
663 // getMaxScale() returns -1 if it can't compute a scale factor (e.g. perspective), taking the
664 // absolute value automatically converts that to an identity scale factor for our purposes.
665 const float maxScale = std::abs(shader.viewMatrix().getMaxScale());
666 if (!shader.hasDynamicStroke()) {
667 // Set up the tessellation control uniforms. In the hairline case we transform prior to
668 // tessellation, so it will be defined in device space units instead of local units.
669 const float strokeRadius = 0.5f * (stroke.isHairlineStyle() ? 1.f : stroke.getWidth());
670 float numRadialSegmentsPerRadian = skgpu::tess::CalcNumRadialSegmentsPerRadian(
671 (stroke.isHairlineStyle() ? 1.f : maxScale) * strokeRadius);
672
673 pdman.set3f(fTessControlArgsUniform,
674 numRadialSegmentsPerRadian, // NUM_RADIAL_SEGMENTS_PER_RADIAN
675 skgpu::tess::GetJoinType(stroke), // JOIN_TYPE
676 strokeRadius); // STROKE_RADIUS
677 } else {
679 pdman.set1f(fTessControlArgsUniform, maxScale);
680 }
681
682 // Set up the view matrix, if any.
683 const SkMatrix& m = shader.viewMatrix();
684 pdman.set2f(fTranslateUniform, m.getTranslateX(), m.getTranslateY());
685 pdman.set4f(fAffineMatrixUniform, m.getScaleX(), m.getSkewY(), m.getSkewX(),
686 m.getScaleY());
687
688 if (!shader.hasDynamicColor()) {
689 pdman.set4fv(fColorUniform, 1, shader.color().vec());
690 }
691}
692
694 bool keyNeedsJoin = !(fPatchAttribs & PatchAttribs::kStrokeParams);
695 SkASSERT(fStroke.getJoin() >> 2 == 0);
696 // Attribs get worked into the key automatically during GrGeometryProcessor::getAttributeKey().
697 // When color is in a uniform, it's always wide. kWideColor doesn't need to be considered here.
698 uint32_t key = (uint32_t)(fPatchAttribs & ~PatchAttribs::kColor);
699 key = (key << 2) | ((keyNeedsJoin) ? fStroke.getJoin() : 0);
700 key = (key << 1) | (uint32_t)fStroke.isHairlineStyle();
701 b->add32(key);
702}
703
704std::unique_ptr<GrGeometryProcessor::ProgramImpl> GrStrokeTessellationShader::makeProgramImpl(
705 const GrShaderCaps&) const {
706 return std::make_unique<Impl>();
707}
@ kVertex_GrShaderFlag
@ kFragment_GrShaderFlag
GrPrimitiveType
Definition GrTypesPriv.h:42
@ kFloat2_GrVertexAttribType
@ kUByte4_norm_GrVertexAttribType
@ kFloat_GrVertexAttribType
@ kFloat4_GrVertexAttribType
SkColor4f color
#define SkASSERT(cond)
Definition SkAssert.h:116
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
virtual void set4fv(UniformHandle, int arrayCount, const float v[]) const =0
virtual void set2f(UniformHandle, float, float) const =0
virtual void set4f(UniformHandle, float, float, float, float) const =0
virtual void set3f(UniformHandle, float, float, float) const =0
virtual void set1f(UniformHandle, float v0) const =0
GrGLSLProgramDataManager::UniformHandle UniformHandle
void setInstanceAttributesWithImplicitOffsets(const Attribute *attrs, int attrCount)
size_t instanceStride() const
void setVertexAttributesWithImplicitOffsets(const Attribute *attrs, int attrCount)
const T & cast() const
void setData(const GrGLSLProgramDataManager &pdman, const GrShaderCaps &, const GrGeometryProcessor &) final
void onEmitCode(EmitArgs &, GrGPArgs *) override
GrStrokeTessellationShader(const GrShaderCaps &, PatchAttribs, const SkMatrix &viewMatrix, const SkStrokeRec &, SkPMColor4f)
const SkStrokeRec & stroke() const
void addToKey(const GrShaderCaps &, skgpu::KeyBuilder *) const override
std::unique_ptr< ProgramImpl > makeProgramImpl(const GrShaderCaps &) const final
static const char * WangsFormulaSkSL()
const SkPMColor4f & color() const
const SkMatrix & viewMatrix() const
SkScalar getMaxScale() const
@ kRound_Join
adds circle
Definition SkPaint.h:360
@ kMiter_Join
extends to miter limit
Definition SkPaint.h:359
bool isHairlineStyle() const
Definition SkStrokeRec.h:47
SkScalar getWidth() const
Definition SkStrokeRec.h:42
SkPaint::Join getJoin() const
Definition SkStrokeRec.h:45
static bool b
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
float GetJoinType(const SkStrokeRec &stroke)
float CalcNumRadialSegmentsPerRadian(float approxDevStrokeRadius)
static constexpr int kMaxResolveLevel
static constexpr float kPrecision
static constexpr float kCubicCurveType
constexpr int NumFixedEdgesInJoin(SkPaint::Join joinType)
bool fVertexIDSupport
const float * vec() const
Definition SkColor.h:308
bool fInfinitySupport
Definition SkSLUtil.h:103