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
8 #include "src/gpu/tessellate/shaders/GrStrokeTessellationShader.h"
9
10 #include "src/gpu/KeyBuilder.h"
11 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
12 #include "src/gpu/glsl/GrGLSLVarying.h"
13 #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
14 #include "src/gpu/tessellate/StrokeTessellator.h"
15
GrStrokeTessellationShader(const GrShaderCaps & shaderCaps,Mode mode,PatchAttribs attribs,const SkMatrix & viewMatrix,const SkStrokeRec & stroke,SkPMColor4f color,int8_t maxParametricSegments_log2)16 GrStrokeTessellationShader::GrStrokeTessellationShader(const GrShaderCaps& shaderCaps,
17 Mode mode,
18 PatchAttribs attribs,
19 const SkMatrix& viewMatrix,
20 const SkStrokeRec& stroke,
21 SkPMColor4f color,
22 int8_t maxParametricSegments_log2)
23 : GrTessellationShader(kTessellate_GrStrokeTessellationShader_ClassID,
24 (mode == Mode::kHardwareTessellation)
25 ? GrPrimitiveType::kPatches
26 : GrPrimitiveType::kTriangleStrip,
27 (mode == Mode::kHardwareTessellation) ? 1 : 0, viewMatrix, color)
28 , fMode(mode)
29 , fPatchAttribs(attribs | PatchAttribs::kJoinControlPoint)
30 , fStroke(stroke)
31 , fMaxParametricSegments_log2(maxParametricSegments_log2) {
32 // We should use explicit curve type when, and only when, there isn't infinity support.
33 // Otherwise the GPU can infer curve type based on infinity.
34 SkASSERT(shaderCaps.infinitySupport() != (attribs & PatchAttribs::kExplicitCurveType));
35 if (fMode == Mode::kHardwareTessellation) {
36 // Explicit curve type is not implemented for tessellation shaders.
37 SkASSERT(!(attribs & PatchAttribs::kExplicitCurveType));
38 }
39 if (fMode == Mode::kHardwareTessellation) {
40 // pts 0..3 define the stroke as a cubic bezier. If p3.y is infinity, then it's a conic
41 // with w=p3.x.
42 //
43 // If p0 == prevCtrlPtAttr, then no join is emitted.
44 //
45 // pts=[p0, p3, p3, p3] is a reserved pattern that means this patch is a join only,
46 // whose start and end tangents are (p0 - inputPrevCtrlPt) and (p3 - p0).
47 //
48 // pts=[p0, p0, p0, p3] is a reserved pattern that means this patch is a "bowtie", or
49 // double-sided round join, anchored on p0 and rotating from (p0 - prevCtrlPtAttr) to
50 // (p3 - p0).
51 fAttribs.emplace_back("pts01Attr", kFloat4_GrVertexAttribType, SkSLType::kFloat4);
52 fAttribs.emplace_back("pts23Attr", kFloat4_GrVertexAttribType, SkSLType::kFloat4);
53 // A join calculates its starting angle using prevCtrlPtAttr.
54 fAttribs.emplace_back("prevCtrlPtAttr", kFloat2_GrVertexAttribType, SkSLType::kFloat2);
55 } else {
56 // pts 0..3 define the stroke as a cubic bezier. If p3.y is infinity, then it's a conic
57 // with w=p3.x.
58 //
59 // An empty stroke (p0==p1==p2==p3) is a special case that denotes a circle, or
60 // 180-degree point stroke.
61 fAttribs.emplace_back("pts01Attr", kFloat4_GrVertexAttribType, SkSLType::kFloat4);
62 fAttribs.emplace_back("pts23Attr", kFloat4_GrVertexAttribType, SkSLType::kFloat4);
63 if (fMode == Mode::kLog2Indirect) {
64 // argsAttr.xy contains the lastControlPoint for setting up the join.
65 //
66 // "argsAttr.z=numTotalEdges" tells the shader the literal number of edges in the
67 // triangle strip being rendered (i.e., it should be vertexCount/2). If
68 // numTotalEdges is negative and the join type is "kRound", it also instructs the
69 // shader to only allocate one segment the preceding round join.
70 fAttribs.emplace_back("argsAttr", kFloat3_GrVertexAttribType, SkSLType::kFloat3);
71 } else {
72 SkASSERT(fMode == Mode::kFixedCount);
73 // argsAttr contains the lastControlPoint for setting up the join.
74 fAttribs.emplace_back("argsAttr", kFloat2_GrVertexAttribType, SkSLType::kFloat2);
75 }
76 }
77 if (fPatchAttribs & PatchAttribs::kStrokeParams) {
78 fAttribs.emplace_back("dynamicStrokeAttr", kFloat2_GrVertexAttribType,
79 SkSLType::kFloat2);
80 }
81 if (fPatchAttribs & PatchAttribs::kColor) {
82 fAttribs.emplace_back("dynamicColorAttr",
83 (fPatchAttribs & PatchAttribs::kWideColorIfEnabled)
84 ? kFloat4_GrVertexAttribType
85 : kUByte4_norm_GrVertexAttribType,
86 SkSLType::kHalf4);
87 }
88 if (fPatchAttribs & PatchAttribs::kExplicitCurveType) {
89 // A conic curve is written out with p3=[w,Infinity], but GPUs that don't support
90 // infinity can't detect this. On these platforms we write out an extra float with each
91 // patch that explicitly tells the shader what type of curve it is.
92 fAttribs.emplace_back("curveTypeAttr", kFloat_GrVertexAttribType, SkSLType::kFloat);
93 }
94 if (fMode == Mode::kHardwareTessellation) {
95 this->setVertexAttributesWithImplicitOffsets(fAttribs.data(), fAttribs.count());
96 SkASSERT(this->vertexStride() == sizeof(SkPoint) * 4 + PatchAttribsStride(fPatchAttribs));
97 } else {
98 this->setInstanceAttributesWithImplicitOffsets(fAttribs.data(), fAttribs.count());
99 SkASSERT(this->instanceStride() == sizeof(SkPoint) * 4 + PatchAttribsStride(fPatchAttribs));
100 if (!shaderCaps.vertexIDSupport()) {
101 constexpr static Attribute kVertexAttrib("edgeID", kFloat_GrVertexAttribType,
102 SkSLType::kFloat);
103 this->setVertexAttributesWithImplicitOffsets(&kVertexAttrib, 1);
104 }
105 }
106 SkASSERT(fAttribs.count() <= kMaxAttribCount);
107 }
108
109 const char* GrStrokeTessellationShader::Impl::kCosineBetweenVectorsFn = R"(
110 float cosine_between_vectors(float2 a, float2 b) {
111 // FIXME(crbug.com/800804,skbug.com/11268): This can overflow if we don't normalize exponents.
112 float ab_cosTheta = dot(a,b);
113 float ab_pow2 = dot(a,a) * dot(b,b);
114 return (ab_pow2 == 0.0) ? 1.0 : clamp(ab_cosTheta * inversesqrt(ab_pow2), -1.0, 1.0);
115 })";
116
117 // Extends the middle radius to either the miter point, or the bevel edge if we surpassed the miter
118 // limit and need to revert to a bevel join.
119 const char* GrStrokeTessellationShader::Impl::kMiterExtentFn = R"(
120 float miter_extent(float cosTheta, float miterLimit) {
121 float x = fma(cosTheta, .5, .5);
122 return (x * miterLimit * miterLimit >= 1.0) ? inversesqrt(x) : sqrt(x);
123 })";
124
125 // Returns the number of radial segments required for each radian of rotation, in order for the
126 // curve to appear "smooth" as defined by the parametricPrecision.
127 const char* GrStrokeTessellationShader::Impl::kNumRadialSegmentsPerRadianFn = R"(
128 float num_radial_segments_per_radian(float parametricPrecision, float strokeRadius) {
129 return .5 / acos(max(1.0 - 1.0/(parametricPrecision * strokeRadius), -1.0));
130 })";
131
132 // Unlike mix(), this does not return b when t==1. But it otherwise seems to get better
133 // precision than "a*(1 - t) + b*t" for things like chopping cubics on exact cusp points.
134 // We override this result anyway when t==1 so it shouldn't be a problem.
135 const char* GrStrokeTessellationShader::Impl::kUncheckedMixFn = R"(
136 float unchecked_mix(float a, float b, float T) {
137 return fma(b - a, T, a);
138 }
139 float2 unchecked_mix(float2 a, float2 b, float T) {
140 return fma(b - a, float2(T), a);
141 }
142 float4 unchecked_mix(float4 a, float4 b, float4 T) {
143 return fma(b - a, T, a);
144 })";
145
emitTessellationCode(const GrStrokeTessellationShader & shader,SkString * code,GrGPArgs * gpArgs,const GrShaderCaps & shaderCaps) const146 void GrStrokeTessellationShader::Impl::emitTessellationCode(
147 const GrStrokeTessellationShader& shader, SkString* code, GrGPArgs* gpArgs,
148 const GrShaderCaps& shaderCaps) const {
149 // The subclass is responsible to define the following symbols before calling this method:
150 //
151 // // Functions.
152 // float2 unchecked_mix(float2, float2, float);
153 // float unchecked_mix(float, float, float);
154 //
155 // // Values provided by either uniforms or attribs.
156 // float2 p0, p1, p2, p3;
157 // float w;
158 // float STROKE_RADIUS;
159 // float 2x2 AFFINE_MATRIX;
160 // float2 TRANSLATE;
161 //
162 // // Values calculated by the specific subclass.
163 // float combinedEdgeID;
164 // bool isFinalEdge;
165 // float numParametricSegments;
166 // float radsPerSegment;
167 // float2 tan0;
168 // float2 tan1;
169 // float strokeOutset;
170 //
171 code->appendf(R"(
172 float2 tangent, strokeCoord;
173 if (combinedEdgeID != 0 && !isFinalEdge) {
174 // Compute the location and tangent direction of the stroke edge with the integral id
175 // "combinedEdgeID", where combinedEdgeID is the sorted-order index of parametric and radial
176 // edges. Start by finding the tangent function's power basis coefficients. These define a
177 // tangent direction (scaled by some uniform value) as:
178 // |T^2|
179 // Tangent_Direction(T) = dx,dy = |A 2B C| * |T |
180 // |. . .| |1 |
181 float2 A, B, C = p1 - p0;
182 float2 D = p3 - p0;
183 if (w >= 0.0) {
184 // P0..P2 represent a conic and P3==P2. The derivative of a conic has a cumbersome
185 // order-4 denominator. However, this isn't necessary if we are only interested in a
186 // vector in the same *direction* as a given tangent line. Since the denominator scales
187 // dx and dy uniformly, we can throw it out completely after evaluating the derivative
188 // with the standard quotient rule. This leaves us with a simpler quadratic function
189 // that we use to find a tangent.
190 C *= w;
191 B = .5*D - C;
192 A = (w - 1.0) * D;
193 p1 *= w;
194 } else {
195 float2 E = p2 - p1;
196 B = E - C;
197 A = fma(float2(-3), E, D);
198 }
199 // FIXME(crbug.com/800804,skbug.com/11268): Consider normalizing the exponents in A,B,C at
200 // this point in order to prevent fp32 overflow.
201
202 // Now find the coefficients that give a tangent direction from a parametric edge ID:
203 //
204 // |parametricEdgeID^2|
205 // Tangent_Direction(parametricEdgeID) = dx,dy = |A B_ C_| * |parametricEdgeID |
206 // |. . .| |1 |
207 //
208 float2 B_ = B * (numParametricSegments * 2.0);
209 float2 C_ = C * (numParametricSegments * numParametricSegments);
210
211 // Run a binary search to determine the highest parametric edge that is located on or before
212 // the combinedEdgeID. A combined ID is determined by the sum of complete parametric and
213 // radial segments behind it. i.e., find the highest parametric edge where:
214 //
215 // parametricEdgeID + floor(numRadialSegmentsAtParametricT) <= combinedEdgeID
216 //
217 float lastParametricEdgeID = 0.0;
218 float maxParametricEdgeID = min(numParametricSegments - 1.0, combinedEdgeID);
219 // FIXME(crbug.com/800804,skbug.com/11268): This normalize() can overflow.
220 float2 tan0norm = normalize(tan0);
221 float negAbsRadsPerSegment = -abs(radsPerSegment);
222 float maxRotation0 = (1.0 + combinedEdgeID) * abs(radsPerSegment);
223 for (int exp = %i - 1; exp >= 0; --exp) {
224 // Test the parametric edge at lastParametricEdgeID + 2^exp.
225 float testParametricID = lastParametricEdgeID + exp2(float(exp));
226 if (testParametricID <= maxParametricEdgeID) {
227 float2 testTan = fma(float2(testParametricID), A, B_);
228 testTan = fma(float2(testParametricID), testTan, C_);
229 float cosRotation = dot(normalize(testTan), tan0norm);
230 float maxRotation = fma(testParametricID, negAbsRadsPerSegment, maxRotation0);
231 maxRotation = min(maxRotation, PI);
232 // Is rotation <= maxRotation? (i.e., is the number of complete radial segments
233 // behind testT, + testParametricID <= combinedEdgeID?)
234 if (cosRotation >= cos(maxRotation)) {
235 // testParametricID is on or before the combinedEdgeID. Keep it!
236 lastParametricEdgeID = testParametricID;
237 }
238 }
239 }
240
241 // Find the T value of the parametric edge at lastParametricEdgeID.
242 float parametricT = lastParametricEdgeID / numParametricSegments;
243
244 // Now that we've identified the highest parametric edge on or before the
245 // combinedEdgeID, the highest radial edge is easy:
246 float lastRadialEdgeID = combinedEdgeID - lastParametricEdgeID;
247
248 // Find the angle of tan0, or the angle between tan0norm and the positive x axis.
249 float angle0 = acos(clamp(tan0norm.x, -1.0, 1.0));
250 angle0 = tan0norm.y >= 0.0 ? angle0 : -angle0;
251
252 // Find the tangent vector on the edge at lastRadialEdgeID.
253 float radialAngle = fma(lastRadialEdgeID, radsPerSegment, angle0);
254 tangent = float2(cos(radialAngle), sin(radialAngle));
255 float2 norm = float2(-tangent.y, tangent.x);
256
257 // Find the T value where the tangent is orthogonal to norm. This is a quadratic:
258 //
259 // dot(norm, Tangent_Direction(T)) == 0
260 //
261 // |T^2|
262 // norm * |A 2B C| * |T | == 0
263 // |. . .| |1 |
264 //
265 float a=dot(norm,A), b_over_2=dot(norm,B), c=dot(norm,C);
266 float discr_over_4 = max(b_over_2*b_over_2 - a*c, 0.0);
267 float q = sqrt(discr_over_4);
268 if (b_over_2 > 0.0) {
269 q = -q;
270 }
271 q -= b_over_2;
272
273 // Roots are q/a and c/q. Since each curve section does not inflect or rotate more than 180
274 // degrees, there can only be one tangent orthogonal to "norm" inside 0..1. Pick the root
275 // nearest .5.
276 float _5qa = -.5*q*a;
277 float2 root = (abs(fma(q,q,_5qa)) < abs(fma(a,c,_5qa))) ? float2(q,a) : float2(c,q);
278 float radialT = (root.t != 0.0) ? root.s / root.t : 0.0;
279 radialT = clamp(radialT, 0.0, 1.0);
280
281 if (lastRadialEdgeID == 0.0) {
282 // The root finder above can become unstable when lastRadialEdgeID == 0 (e.g., if
283 // there are roots at exatly 0 and 1 both). radialT should always == 0 in this case.
284 radialT = 0.0;
285 }
286
287 // Now that we've identified the T values of the last parametric and radial edges, our final
288 // T value for combinedEdgeID is whichever is larger.
289 float T = max(parametricT, radialT);
290
291 // Evaluate the cubic at T. Use De Casteljau's for its accuracy and stability.
292 float2 ab = unchecked_mix(p0, p1, T);
293 float2 bc = unchecked_mix(p1, p2, T);
294 float2 cd = unchecked_mix(p2, p3, T);
295 float2 abc = unchecked_mix(ab, bc, T);
296 float2 bcd = unchecked_mix(bc, cd, T);
297 float2 abcd = unchecked_mix(abc, bcd, T);
298
299 // Evaluate the conic weight at T.
300 float u = unchecked_mix(1.0, w, T);
301 float v = w + 1 - u; // == mix(w, 1, T)
302 float uv = unchecked_mix(u, v, T);
303
304 // If we went with T=parametricT, then update the tangent. Otherwise leave it at the radial
305 // tangent found previously. (In the event that parametricT == radialT, we keep the radial
306 // tangent.)
307 if (T != radialT) {
308 tangent = (w >= 0.0) ? bc*u - ab*v : bcd - abc;
309 }
310
311 strokeCoord = (w >= 0.0) ? abc/uv : abcd;
312 } else {
313 // Edges at the beginning and end of the strip use exact endpoints and tangents. This
314 // ensures crack-free seaming between instances.
315 tangent = (combinedEdgeID == 0) ? tan0 : tan1;
316 strokeCoord = (combinedEdgeID == 0) ? p0 : p3;
317 })", shader.maxParametricSegments_log2() /* Parametric/radial sort loop count. */);
318
319 code->append(R"(
320 // FIXME(crbug.com/800804,skbug.com/11268): This normalize() can overflow.
321 float2 ortho = normalize(float2(tangent.y, -tangent.x));
322 strokeCoord += ortho * (STROKE_RADIUS * strokeOutset);)");
323
324 if (!shader.stroke().isHairlineStyle()) {
325 // Normal case. Do the transform after tessellation.
326 code->append(R"(
327 float2 devCoord = AFFINE_MATRIX * strokeCoord + TRANSLATE;)");
328 gpArgs->fPositionVar.set(SkSLType::kFloat2, "devCoord");
329 gpArgs->fLocalCoordVar.set(SkSLType::kFloat2, "strokeCoord");
330 } else {
331 // Hairline case. The scale and skew already happened before tessellation.
332 code->append(R"(
333 float2 devCoord = strokeCoord + TRANSLATE;
334 float2 localCoord = inverse(AFFINE_MATRIX) * strokeCoord;)");
335 gpArgs->fPositionVar.set(SkSLType::kFloat2, "devCoord");
336 gpArgs->fLocalCoordVar.set(SkSLType::kFloat2, "localCoord");
337 }
338 }
339
emitFragmentCode(const GrStrokeTessellationShader & shader,const EmitArgs & args)340 void GrStrokeTessellationShader::Impl::emitFragmentCode(const GrStrokeTessellationShader& shader,
341 const EmitArgs& args) {
342 if (!shader.hasDynamicColor()) {
343 // The fragment shader just outputs a uniform color.
344 const char* colorUniformName;
345 fColorUniform = args.fUniformHandler->addUniform(nullptr, kFragment_GrShaderFlag,
346 SkSLType::kHalf4, "color",
347 &colorUniformName);
348 args.fFragBuilder->codeAppendf("half4 %s = %s;", args.fOutputColor, colorUniformName);
349 } else {
350 args.fFragBuilder->codeAppendf("half4 %s = %s;", args.fOutputColor,
351 fDynamicColorName.c_str());
352 }
353 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
354 }
355
setData(const GrGLSLProgramDataManager & pdman,const GrShaderCaps &,const GrGeometryProcessor & geomProc)356 void GrStrokeTessellationShader::Impl::setData(const GrGLSLProgramDataManager& pdman,
357 const GrShaderCaps&,
358 const GrGeometryProcessor& geomProc) {
359 const auto& shader = geomProc.cast<GrStrokeTessellationShader>();
360 const auto& stroke = shader.stroke();
361
362 if (!shader.hasDynamicStroke()) {
363 // Set up the tessellation control uniforms.
364 skgpu::StrokeTolerances tolerances;
365 if (!stroke.isHairlineStyle()) {
366 tolerances = skgpu::StrokeTolerances::MakeNonHairline(shader.viewMatrix().getMaxScale(),
367 stroke.getWidth());
368 } else {
369 // In the hairline case we transform prior to tessellation. Set up tolerances for an
370 // identity viewMatrix and a strokeWidth of 1.
371 tolerances = skgpu::StrokeTolerances::MakeNonHairline(1, 1);
372 }
373 float strokeRadius = (stroke.isHairlineStyle()) ? .5f : stroke.getWidth() * .5;
374 pdman.set4f(fTessControlArgsUniform,
375 tolerances.fParametricPrecision, // PARAMETRIC_PRECISION
376 tolerances.fNumRadialSegmentsPerRadian, // NUM_RADIAL_SEGMENTS_PER_RADIAN
377 skgpu::GetJoinType(stroke), // JOIN_TYPE
378 strokeRadius); // STROKE_RADIUS
379 } else {
380 SkASSERT(!stroke.isHairlineStyle());
381 float maxScale = shader.viewMatrix().getMaxScale();
382 pdman.set1f(fTessControlArgsUniform,
383 skgpu::StrokeTolerances::CalcParametricPrecision(maxScale));
384 }
385
386 if (shader.mode() == GrStrokeTessellationShader::Mode::kFixedCount) {
387 SkASSERT(shader.fixedCountNumTotalEdges() != 0);
388 pdman.set1f(fEdgeCountUniform, (float)shader.fixedCountNumTotalEdges());
389 }
390
391 // Set up the view matrix, if any.
392 const SkMatrix& m = shader.viewMatrix();
393 pdman.set2f(fTranslateUniform, m.getTranslateX(), m.getTranslateY());
394 pdman.set4f(fAffineMatrixUniform, m.getScaleX(), m.getSkewY(), m.getSkewX(),
395 m.getScaleY());
396
397 if (!shader.hasDynamicColor()) {
398 pdman.set4fv(fColorUniform, 1, shader.color().vec());
399 }
400 }
401
addToKey(const GrShaderCaps &,skgpu::KeyBuilder * b) const402 void GrStrokeTessellationShader::addToKey(const GrShaderCaps&, skgpu::KeyBuilder* b) const {
403 bool keyNeedsJoin = (fMode != Mode::kHardwareTessellation) &&
404 !(fPatchAttribs & PatchAttribs::kStrokeParams);
405 SkASSERT((int)fMode >> 2 == 0);
406 SkASSERT(fStroke.getJoin() >> 2 == 0);
407 // Attribs get worked into the key automatically during GrGeometryProcessor::getAttributeKey().
408 // When color is in a uniform, it's always wide. kWideColor doesn't need to be considered here.
409 uint32_t key = (uint32_t)(fPatchAttribs & ~PatchAttribs::kColor);
410 key = (key << 2) | (uint32_t)fMode;
411 key = (key << 2) | ((keyNeedsJoin) ? fStroke.getJoin() : 0);
412 key = (key << 1) | (uint32_t)fStroke.isHairlineStyle();
413 key = (key << 8) | fMaxParametricSegments_log2;
414 b->add32(key);
415 }
416
makeProgramImpl(const GrShaderCaps &) const417 std::unique_ptr<GrGeometryProcessor::ProgramImpl> GrStrokeTessellationShader::makeProgramImpl(
418 const GrShaderCaps&) const {
419 switch (fMode) {
420 case Mode::kHardwareTessellation:
421 return std::make_unique<HardwareImpl>();
422 case Mode::kLog2Indirect:
423 case Mode::kFixedCount:
424 return std::make_unique<InstancedImpl>();
425 }
426 SkUNREACHABLE;
427 }
428