| 1 |
/* |
| 2 |
* Copyright 2021 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 |
#ifndef skgpu_tessellate_PatchWriter_DEFINED |
| 9 |
#define skgpu_tessellate_PatchWriter_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkScalar.h" |
| 12 |
#include "include/core/SkTypes.h" |
| 13 |
#include "include/private/base/SkDebug.h" |
| 14 |
#include "include/private/base/SkFloatingPoint.h" |
| 15 |
#include "include/private/base/SkPoint_impl.h" |
| 16 |
#include "include/private/base/SkTemplates.h" |
| 17 |
#include "src/base/SkUtils.h" |
| 18 |
#include "src/base/SkVx.h" |
| 19 |
#include "src/core/SkColorData.h" |
| 20 |
#include "src/gpu/BufferWriter.h" |
| 21 |
#include "src/gpu/tessellate/LinearTolerances.h" |
| 22 |
#include "src/gpu/tessellate/MiddleOutPolygonTriangulator.h" |
| 23 |
#include "src/gpu/tessellate/Tessellation.h" |
| 24 |
#include "src/gpu/tessellate/WangsFormula.h" |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
#include <cstdint> |
| 28 |
#include <cstring> |
| 29 |
#include <math.h> |
| 30 |
#include <tuple> |
| 31 |
#include <type_traits> |
| 32 |
#include <utility> |
| 33 |
#include <variant> |
| 34 |
|
| 35 |
namespace skgpu::tess { |
| 36 |
|
| 37 |
/** |
| 38 |
* PatchWriter writes out tessellation patches, formatted with their specific attribs, to a GPU |
| 39 |
* buffer. |
| 40 |
* |
| 41 |
* PatchWriter is a template class that takes traits to configure both its compile-time and runtime |
| 42 |
* behavior for the different tessellation rendering algorithms and GPU backends. The complexity of |
| 43 |
* this system is worthwhile because the attribute writing operations and math already require |
| 44 |
* heavy inlining for performance, and the algorithmic variations tend to only differ slightly, but |
| 45 |
* do so in the inner most loops. Additionally, Graphite and Ganesh use the same fundamental |
| 46 |
* algorithms, but Graphite's architecture and higher required hardware level mean that its |
| 47 |
* attribute configurations can be determined entirely at compile time. |
| 48 |
* |
| 49 |
* Traits are specified in PatchWriter's single var-args template pack. Traits come in two main |
| 50 |
* categories: PatchAttribs configuration and feature/processing configuration. A given PatchAttrib |
| 51 |
* can be always enabled, enabled at runtime, or always disabled. A feature can be either enabled |
| 52 |
* or disabled and are coupled more closely with the control points of the curve. Across the two |
| 53 |
* GPU backends and different path rendering strategies, a "patch" has the following structure: |
| 54 |
* |
| 55 |
* - 4 control points (8 floats total) defining the curve's geometry |
| 56 |
* - quadratic curves are converted to equivalent cubics on the CPU during writing |
| 57 |
* - conic curves store {w, inf} in their last control point |
| 58 |
* - triangles store {inf, inf} in their last control point |
| 59 |
* - everything else is presumed to be a cubic defined by all 4 control points |
| 60 |
* - Enabled PatchAttrib values, constant for the entire instance |
| 61 |
* - layout is identical to PatchAttrib's definition, skipping disabled attribs |
| 62 |
* - attribs can be enabled/disabled at runtime by building a mask of attrib values |
| 63 |
* |
| 64 |
* Currently PatchWriter supports the following traits: |
| 65 |
* - Required<PatchAttrib> |
| 66 |
* - Optional<PatchAttrib> |
| 67 |
* - TrackJoinControlPoints |
| 68 |
* - AddTrianglesWhenChopping |
| 69 |
* - DiscardFlatCurves |
| 70 |
* |
| 71 |
* In addition to variable traits, PatchWriter's first template argument defines the type used for |
| 72 |
* allocating the GPU instance data. The templated "PatchAllocator" can be any type that provides: |
| 73 |
* // A GPU-backed vertex writer for a single instance worth of data. The provided |
| 74 |
* // LinearTolerances value represents the tolerances for the curve that will be written to the |
| 75 |
* // returned vertex space. |
| 76 |
* skgpu::VertexWriter append(const LinearTolerances&); |
| 77 |
* |
| 78 |
* Additionally, it must have a constructor that takes the stride as its first argument. |
| 79 |
* PatchWriter forwards any additional constructor args from its ctor to the allocator after |
| 80 |
* computing the necessary stride for its PatchAttribs configuration. |
| 81 |
*/ |
| 82 |
|
| 83 |
// *** TRAITS *** |
| 84 |
|
| 85 |
// Marks a PatchAttrib is enabled at compile time, i.e. it must always be set and will always be |
| 86 |
// written to each patch's instance data. If present, will assert if the runtime attribs do not fit. |
| 87 |
template <PatchAttribs A> struct Required {}; |
| 88 |
// Marks a PatchAttrib as supported, i.e. it can be enabled or disabled at runtime. Optional<A> is |
| 89 |
// overridden by Required<A>. If neither Required<A> nor Optional<A> are in a PatchWriter's trait |
| 90 |
// list, then the attrib is disabled at compile time and it will assert if the runtime attribs |
| 91 |
// attempt to enable it. |
| 92 |
template <PatchAttribs A> struct Optional {}; |
| 93 |
|
| 94 |
// Enables tracking of the kJoinControlPointAttrib based on control points of the previously |
| 95 |
// written patch (automatically taking into account curve chopping). When a patch is first written |
| 96 |
// (and there is no prior patch to define the join control point), the PatchWriter automatically |
| 97 |
// records the patch to a temporary buffer--sans join--until writeDeferredStrokePatch() is called, |
| 98 |
// filling in the now-defined join control point. |
| 99 |
// |
| 100 |
// This feature must be paired with Required<PatchAttribs::kJoinControlPoint> |
| 101 |
struct TrackJoinControlPoints {}; |
| 102 |
|
| 103 |
// Write additional triangular patches to fill the resulting empty area when a curve is chopped. |
| 104 |
// Normally, the patch geometry covers the curve defined by its control points, up to the implicitly |
| 105 |
// closing edge between its first and last control points. When a curve is chopped to fit within |
| 106 |
// the maximum segment count, the resulting space between the original closing edge and new closing |
| 107 |
// edges is not filled, unless some mechanism of the shader makes it so (e.g. a fan point or |
| 108 |
// stroking). |
| 109 |
// |
| 110 |
// This feature enables automatically writing triangular patches to fill this empty space when a |
| 111 |
// curve is chopped. |
| 112 |
struct AddTrianglesWhenChopping {}; |
| 113 |
|
| 114 |
// If a curve requires at most 1 segment to render accurately, it's effectively a straight line. |
| 115 |
// This feature turns on automatically ignoring those curves, with the assumption that some other |
| 116 |
// render pass will produce equivalent geometry (e.g. middle-out or inner triangulations). |
| 117 |
struct DiscardFlatCurves {}; |
| 118 |
|
| 119 |
// Upload lines as a cubic with {a, a, b, b} for control points, instead of the truly linear cubic |
| 120 |
// of {a, 2/3a + 1/3b, 1/3a + 2/3b, b}. Wang's formula will not return an tight lower bound on the |
| 121 |
// number of segments in this case, but it's convenient to detect in the vertex shader and assume |
| 122 |
// only a single segment is required. This bypasses numerical stability issues in Wang's formula |
| 123 |
// when evaluated on the ideal linear cubic for very large control point coordinates. Other curve |
| 124 |
// types with large coordinates do not need this treatment since they would be pre-chopped and |
| 125 |
// culled to lines. |
| 126 |
struct ReplicateLineEndPoints {}; |
| 127 |
|
| 128 |
// *** PatchWriter internals *** |
| 129 |
|
| 130 |
// AttribValue exposes a consistent store and write interface for a PatchAttrib's value while |
| 131 |
// abstracting over compile-time enabled, conditionally-enabled, or compile-time disabled attribs. |
| 132 |
template <PatchAttribs A, typename T, bool Required, bool Optional> |
| 133 |
struct AttribValue { |
| 134 |
using DataType = std::conditional_t<Required, T, |
| 135 |
std::conditional_t<Optional, std::pair<T, bool>, |
| 136 |
/* else */ std::monostate>>; |
| 137 |
|
| 138 |
static constexpr bool kEnabled = Required || Optional; |
| 139 |
|
| 140 |
explicit AttribValue(PatchAttribs attribs) : AttribValue(attribs, {}) {} |
| 141 |
AttribValue(PatchAttribs attribs, const T& t) { |
| 142 |
(void) attribs; // may be unused on release builds |
| 143 |
if constexpr (Required) { |
| 144 |
SkASSERT(attribs & A); |
| 145 |
} else if constexpr (Optional) { |
| 146 |
std::get<1>(fV) = attribs & A; |
| 147 |
} else { |
| 148 |
SkASSERT(!(attribs & A)); |
| 149 |
} |
| 150 |
*this = t; |
| 151 |
} |
| 152 |
|
| 153 |
AttribValue& operator=(const T& v) { |
| 154 |
if constexpr (Required) { |
| 155 |
fV = v; |
| 156 |
} else if constexpr (Optional) { |
| 157 |
// for simplicity, store even if disabled and won't be written out to VertexWriter |
| 158 |
std::get<0>(fV) = v; |
| 159 |
} // else ignore for disabled values |
| 160 |
return *this; |
| 161 |
} |
| 162 |
|
| 163 |
const T& operator*() const { |
| 164 |
if constexpr (Required) { |
| 165 |
return fV; |
| 166 |
} else if constexpr (Optional) { |
| 167 |
SkASSERT(std::get<1>(fV)); |
| 168 |
return std::get<0>(fV); |
| 169 |
} else { |
| 170 |
SkUNREACHABLE; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
DataType fV; |
| 175 |
}; |
| 176 |
|
| 177 |
template <PatchAttribs A, typename T, bool Required, bool Optional> |
| 178 |
VertexWriter& operator<<(VertexWriter& w, const AttribValue<A, T, Required, Optional>& v) { |
| 179 |
if constexpr (Required) { |
| 180 |
w << v.fV; // always write |
| 181 |
} else if constexpr (Optional) { |
| 182 |
if (std::get<1>(v.fV)) { |
| 183 |
w << std::get<0>(v.fV); // write if enabled |
| 184 |
} |
| 185 |
} // else never write |
| 186 |
return w; |
| 187 |
} |
| 188 |
|
| 189 |
// Stores state and deferred patch data when TrackJoinControlPoints is used for a PatchWriter. |
| 190 |
template <size_t Stride> |
| 191 |
struct PatchStorage { |
| 192 |
SkPoint fFirstControlPoint; // This is the first control point of the contour, e.g. moveTo |
| 193 |
SkPoint fLastControlPoint; // The last control point written within the contour |
| 194 |
|
| 195 |
// The signs of fCurveType and fN_p4 encode some special states: |
| 196 |
// - If both are negative, then nothing has been written that needs to be deferred |
| 197 |
// - If fN_p4 == 0, the caller has explicitly managed the join and nothing is deferred |
| 198 |
// - If fN_p4 < 0 and fCurveType >= 0, then a degenerate verb has been recorded so caps should |
| 199 |
// be written when the deferred patch is ended. |
| 200 |
// - If fN_p4 > 0 and fCurveType >= 0, then this is a full deferred patch |
| 201 |
float fCurveType = -1.f; // The explicit curve type passed to writePatch() |
| 202 |
float fN_p4 = -1.f; // The parametric segment value to restore on LinearTolerances |
| 203 |
|
| 204 |
// Holds an entire patch, except with an undefined join control point. |
| 205 |
char fData[Stride]; |
| 206 |
|
| 207 |
void disableDeferral() { |
| 208 |
fN_p4 = std::max(fN_p4, 0.f); // do nothing if we already have a deferred patch recorded |
| 209 |
} |
| 210 |
|
| 211 |
bool mustDefer() const { |
| 212 |
return fN_p4 < 0.f; |
| 213 |
} |
| 214 |
|
| 215 |
bool hasVerb() const { |
| 216 |
return fCurveType >= 0.f; |
| 217 |
} |
| 218 |
|
| 219 |
bool hasPending() const { |
| 220 |
// fN_p4 = 0 shouldn't happen naturally from Wang's formula and is used to disable deferring |
| 221 |
// patches if the join is defined manually via updateJoinControlPointAttrib() |
| 222 |
return fN_p4 > 0.f; |
| 223 |
} |
| 224 |
|
| 225 |
void reset(SkPoint moveTo) { |
| 226 |
fCurveType = -1.f; |
| 227 |
fN_p4 = -1.f; |
| 228 |
fFirstControlPoint = fLastControlPoint = moveTo; |
| 229 |
} |
| 230 |
}; |
| 231 |
|
| 232 |
// An empty object that has the same constructor signature as MiddleOutPolygonTriangulator, used |
| 233 |
// as a stand-in when AddTrianglesWhenChopping is not a defined trait. |
| 234 |
struct NullTriangulator { |
| 235 |
NullTriangulator(int, SkPoint) {} |
| 236 |
}; |
| 237 |
|
| 238 |
#define AI SK_ALWAYS_INLINE |
| 239 |
#define ENABLE_IF(cond) template <typename Void=void> std::enable_if_t<cond, Void> |
| 240 |
|
| 241 |
// *** PatchWriter *** |
| 242 |
template <typename PatchAllocator, typename... Traits> |
| 243 |
class PatchWriter { |
| 244 |
// Helpers to extract specifics from the template traits pack. |
| 245 |
template <typename F> struct has_trait : std::disjunction<std::is_same<F, Traits>...> {}; |
| 246 |
template <PatchAttribs A> using req_attrib = has_trait<Required<A>>; |
| 247 |
template <PatchAttribs A> using opt_attrib = has_trait<Optional<A>>; |
| 248 |
|
| 249 |
// Enabled features and attribute configuration |
| 250 |
static constexpr bool kTrackJoinControlPoints = has_trait<TrackJoinControlPoints>::value; |
| 251 |
static constexpr bool kAddTrianglesWhenChopping = has_trait<AddTrianglesWhenChopping>::value; |
| 252 |
static constexpr bool kDiscardFlatCurves = has_trait<DiscardFlatCurves>::value; |
| 253 |
static constexpr bool kReplicateLineEndPoints = has_trait<ReplicateLineEndPoints>::value; |
| 254 |
|
| 255 |
// NOTE: MSVC 19.24 cannot compile constexpr fold expressions referenced in templates, so |
| 256 |
// extract everything into constexpr bool's instead of using `req_attrib` directly, etc. :( |
| 257 |
template <PatchAttribs A, typename T, bool Req/*=req_attrib<A>*/, bool Opt/*=opt_attrib<A>*/> |
| 258 |
using attrib_t = AttribValue<A, T, Req, Opt>; |
| 259 |
|
| 260 |
// TODO: Remove when MSVC compiler is fixed, in favor of `using Name = attrib_t<>` directly. |
| 261 |
#define DEF_ATTRIB_TYPE(name, A, T) \ |
| 262 |
static constexpr bool kRequire##name = req_attrib<A>::value; \ |
| 263 |
static constexpr bool kOptional##name = opt_attrib<A>::value; \ |
| 264 |
using name = attrib_t<A, T, kRequire##name, kOptional##name> |
| 265 |
|
| 266 |
DEF_ATTRIB_TYPE(JoinAttrib, PatchAttribs::kJoinControlPoint, SkPoint); |
| 267 |
DEF_ATTRIB_TYPE(FanPointAttrib, PatchAttribs::kFanPoint, SkPoint); |
| 268 |
DEF_ATTRIB_TYPE(StrokeAttrib, PatchAttribs::kStrokeParams, StrokeParams); |
| 269 |
|
| 270 |
// kWideColorIfEnabled does not define an attribute, but changes the type of the kColor attrib. |
| 271 |
static constexpr bool kRequireWideColor = req_attrib<PatchAttribs::kWideColorIfEnabled>::value; |
| 272 |
static constexpr bool kOptionalWideColor = opt_attrib<PatchAttribs::kWideColorIfEnabled>::value; |
| 273 |
using Color = std::conditional_t<kRequireWideColor, SkPMColor4f, |
| 274 |
std::conditional_t<kOptionalWideColor, VertexColor, |
| 275 |
/* else */ uint32_t>>; |
| 276 |
|
| 277 |
DEF_ATTRIB_TYPE(ColorAttrib, PatchAttribs::kColor, Color); |
| 278 |
DEF_ATTRIB_TYPE(DepthAttrib, PatchAttribs::kPaintDepth, float); |
| 279 |
DEF_ATTRIB_TYPE(CurveTypeAttrib, PatchAttribs::kExplicitCurveType, float); |
| 280 |
DEF_ATTRIB_TYPE(SsboIndexAttrib, PatchAttribs::kSsboIndex, uint32_t); |
| 281 |
#undef DEF_ATTRIB_TYPE |
| 282 |
|
| 283 |
static constexpr size_t kMaxStride = 4 * sizeof(SkPoint) + // control points |
| 284 |
(JoinAttrib::kEnabled ? sizeof(SkPoint) : 0) + |
| 285 |
(FanPointAttrib::kEnabled ? sizeof(SkPoint) : 0) + |
| 286 |
(StrokeAttrib::kEnabled ? sizeof(StrokeParams) : 0) + |
| 287 |
(ColorAttrib::kEnabled ? std::min(sizeof(Color), sizeof(SkPMColor4f)) : 0) + |
| 288 |
(DepthAttrib::kEnabled ? sizeof(float) : 0) + |
| 289 |
(CurveTypeAttrib::kEnabled ? sizeof(float) : 0) + |
| 290 |
(SsboIndexAttrib::kEnabled ? 2 * sizeof(uint32_t) : 0); |
| 291 |
|
| 292 |
// Types that vary depending on the activated features, but do not define the patch data. |
| 293 |
using DeferredPatch = std::conditional_t<kTrackJoinControlPoints, |
| 294 |
PatchStorage<kMaxStride>, std::monostate>; |
| 295 |
using InnerTriangulator = std::conditional_t<kAddTrianglesWhenChopping, |
| 296 |
MiddleOutPolygonTriangulator, NullTriangulator>; |
| 297 |
|
| 298 |
using float2 = skvx::float2; |
| 299 |
using float4 = skvx::float4; |
| 300 |
|
| 301 |
static_assert(!kTrackJoinControlPoints || req_attrib<PatchAttribs::kJoinControlPoint>::value, |
| 302 |
"Deferred patches and auto-updating joins requires kJoinControlPoint attrib"); |
| 303 |
public: |
| 304 |
template <typename... Args> // forwarded to PatchAllocator |
| 305 |
PatchWriter(PatchAttribs attribs, |
| 306 |
Args&&... allocArgs) |
| 307 |
: fAttribs(attribs) |
| 308 |
, fPatchAllocator(PatchStride(attribs), std::forward<Args>(allocArgs)...) |
| 309 |
, fJoin(attribs) |
| 310 |
, fFanPoint(attribs) |
| 311 |
, fStrokeParams(attribs) |
| 312 |
, fColor(attribs) |
| 313 |
, fDepth(attribs) |
| 314 |
, fSsboIndex(attribs) { |
| 315 |
// Explicit curve types are provided on the writePatch signature, and not a field of |
| 316 |
// PatchWriter, so initialize one in the ctor to validate the provided runtime attribs. |
| 317 |
SkDEBUGCODE((void) CurveTypeAttrib(attribs);) |
| 318 |
// Validate the kWideColorIfEnabled attribute variant flag as well |
| 319 |
if constexpr (req_attrib<PatchAttribs::kWideColorIfEnabled>::value) { |
| 320 |
SkASSERT(attribs & PatchAttribs::kWideColorIfEnabled); // required |
| 321 |
} else if constexpr (!opt_attrib<PatchAttribs::kWideColorIfEnabled>::value) { |
| 322 |
SkASSERT(!(attribs & PatchAttribs::kWideColorIfEnabled)); // disabled |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
~PatchWriter() { |
| 327 |
if constexpr (kTrackJoinControlPoints) { |
| 328 |
// There shouldn't be any deferred flush; PatchWriter doesn't know the cap style that |
| 329 |
// needs to be applied. Callers are responsible for calling writeDeferredStrokePatch() |
| 330 |
SkASSERT(!fDeferredPatch.hasPending()); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
PatchAttribs attribs() const { return fAttribs; } |
| 335 |
|
| 336 |
// The max scale factor should be derived from the same matrix that 'xform' was. It's only used |
| 337 |
// in stroking calculations, so can be ignored for path filling. |
| 338 |
void setShaderTransform(const wangs_formula::VectorXform& xform, |
| 339 |
float maxScale = 1.f) { |
| 340 |
fApproxTransform = xform; |
| 341 |
fMaxScale = maxScale; |
| 342 |
} |
| 343 |
|
| 344 |
// Completes a closed contour of a stroke by rewriting a deferred patch with now-available |
| 345 |
// join control point information. Automatically resets the join control point attribute. |
| 346 |
// If `cap` is provided, instead of joining the last patch to the deferred patch, it ends the |
| 347 |
// contour by adding caps to the end of the last and start of the deferred. |
| 348 |
// |
| 349 |
// `moveTo` represents the start of the next contour. |
| 350 |
ENABLE_IF(kTrackJoinControlPoints) writeDeferredStrokePatch(SkPoint moveTo, |
| 351 |
std::optional<SkPaint::Cap> cap) { |
| 352 |
if (fDeferredPatch.hasPending()) { |
| 353 |
SkASSERT(fDeferredPatch.hasVerb()); |
| 354 |
SkPoint join; |
| 355 |
if (!cap.has_value()) { |
| 356 |
// When the contour is closed, there are no caps. We just have to update the join |
| 357 |
// attribute to reflect the last patch and write the deferred patch. |
| 358 |
join = *fJoin; |
| 359 |
} else { |
| 360 |
// When the contour is not closed, the last patch and the deferred patch both need |
| 361 |
// to have caps added to them. Set the join point to the first control point since |
| 362 |
// all caps either don't require joins or are oriented to join with that point. |
| 363 |
join = fDeferredPatch.fFirstControlPoint; |
| 364 |
|
| 365 |
if (cap == SkPaint::kRound_Cap) { |
| 366 |
// Also add circles for the start and end (these don't join with anything) |
| 367 |
this->writeCircle(fDeferredPatch.fLastControlPoint); |
| 368 |
this->writeCircle(fDeferredPatch.fFirstControlPoint); |
| 369 |
// Since these caps are full circles, there's no need to correct the deferred |
| 370 |
// patch's join attribute. The circle will cover up the butt-end just fine. |
| 371 |
} else if (cap == SkPaint::kSquare_Cap) { |
| 372 |
// First write a square cap that joins against the last recorded join point |
| 373 |
this->writeSquare(fDeferredPatch.fLastControlPoint, *fJoin); |
| 374 |
// Next write a square cap that joins against the incoming tangent point of the |
| 375 |
// first contour. |
| 376 |
float4 p01 = float4::Load(fDeferredPatch.fData); |
| 377 |
float4 p23 = float4::Load(fDeferredPatch.fData + 4*sizeof(float)); |
| 378 |
float2 last = fDeferredPatch.fCurveType == kCubicCurveType |
| 379 |
? p23.zw() : p23.xy(); |
| 380 |
float2 capPt = TangentPoint(p01.xy(), p01.zw(), p23.xy(), last); |
| 381 |
this->writeSquare(p01.xy(), capPt); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
// Overwrite join control point with the updated value, which is the first attribute |
| 386 |
// after the 4 control points. |
| 387 |
memcpy(SkTAddOffset<void>(fDeferredPatch.fData, 4 * sizeof(SkPoint)), |
| 388 |
&join, sizeof(SkPoint)); |
| 389 |
// Assuming that the stroke parameters aren't changing within a contour, we only have |
| 390 |
// to set the parametric segments in order to recover the LinearTolerances state at the |
| 391 |
// time the deferred patch was recorded. |
| 392 |
fTolerances.setParametricSegments(fDeferredPatch.fN_p4); |
| 393 |
if (VertexWriter vw = fPatchAllocator.append(fTolerances)) { |
| 394 |
vw << VertexWriter::Array<char>(fDeferredPatch.fData, PatchStride(fAttribs)); |
| 395 |
} |
| 396 |
} else { |
| 397 |
// Either nothing has been recorded yet or it was all degenerate (in which case we |
| 398 |
// should record a cap). |
| 399 |
if (cap.has_value() && fDeferredPatch.hasVerb()) { |
| 400 |
switch (*cap) { |
| 401 |
case SkPaint::kButt_Cap: |
| 402 |
// No actual cap geometry to produce |
| 403 |
break; |
| 404 |
case SkPaint::kRound_Cap: |
| 405 |
this->writeCircle(fDeferredPatch.fFirstControlPoint); |
| 406 |
break; |
| 407 |
case SkPaint::kSquare_Cap: { |
| 408 |
// Write a square with no joining direction to get a full square (vs. a cap) |
| 409 |
this->writeSquare(fDeferredPatch.fFirstControlPoint, std::nullopt); |
| 410 |
break; |
| 411 |
} |
| 412 |
} |
| 413 |
// Neither writeCircle() or writeSquare() should add anything that was deferred. |
| 414 |
SkASSERT(!fDeferredPatch.hasPending()); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
// Remember the first control point in the event we need to produce a cap, or a close() |
| 419 |
fDeferredPatch.reset(moveTo); |
| 420 |
} |
| 421 |
|
| 422 |
ENABLE_IF(kTrackJoinControlPoints) closeDeferredStrokePatch(SkPaint::Cap cap) { |
| 423 |
// Write a line from the last control point to the first control point, and then |
| 424 |
// write the deferred patch, which will use the join information generated by the lineTo |
| 425 |
this->writeLine(fDeferredPatch.fLastControlPoint, fDeferredPatch.fFirstControlPoint); |
| 426 |
if (fDeferredPatch.hasPending()) { |
| 427 |
// This will be true if the prior writeLine() was not degenerate or there was other |
| 428 |
// non-degenerate patches written previously. In this case we should join, so don't pass |
| 429 |
// the cap style in. |
| 430 |
this->writeDeferredStrokePatch(fDeferredPatch.fFirstControlPoint, std::nullopt); |
| 431 |
} else { |
| 432 |
// Explicitly closing empty geometry will produce caps. This is handled by |
| 433 |
// writeDeferredStrokePatch so it can catch the equivalent case of |
| 434 |
// "moveTo(p0); lineTo(p0); moveTo(p1)" which never includes a close verb but also |
| 435 |
// produces cap geometry. |
| 436 |
SkASSERT(fDeferredPatch.hasVerb()); |
| 437 |
this->writeDeferredStrokePatch(fDeferredPatch.fFirstControlPoint, cap); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
// DEPRECATED: Only used by Ganesh with StrokeIter that manages caps on its own. When used this |
| 442 |
// way, the moveTo point is always ignored and we only ever want to produce joins. |
| 443 |
ENABLE_IF(JoinAttrib::kEnabled) writeDeferredStrokePatch() { |
| 444 |
this->writeDeferredStrokePatch({0.f, 0.f}, std::nullopt); |
| 445 |
} |
| 446 |
// DEPRECATED |
| 447 |
ENABLE_IF(JoinAttrib::kEnabled) updateJoinControlPointAttrib(SkPoint lastControlPoint) { |
| 448 |
SkASSERT(fAttribs & PatchAttribs::kJoinControlPoint); // must be runtime enabled as well |
| 449 |
fJoin = lastControlPoint; |
| 450 |
fDeferredPatch.disableDeferral(); // fJoin is valid, so no need to defer next real patch |
| 451 |
} |
| 452 |
|
| 453 |
// Updates the fan point that will be written out with each patch (i.e., the point that wedges |
| 454 |
// fan around). |
| 455 |
ENABLE_IF(FanPointAttrib::kEnabled) updateFanPointAttrib(SkPoint fanPoint) { |
| 456 |
SkASSERT(fAttribs & PatchAttribs::kFanPoint); |
| 457 |
fFanPoint = fanPoint; |
| 458 |
} |
| 459 |
|
| 460 |
// Updates the stroke params that are written out with each patch. |
| 461 |
ENABLE_IF(StrokeAttrib::kEnabled) updateStrokeParamsAttrib(StrokeParams strokeParams) { |
| 462 |
SkASSERT(fAttribs & PatchAttribs::kStrokeParams); |
| 463 |
fStrokeParams = strokeParams; |
| 464 |
fTolerances.setStroke(strokeParams, fMaxScale); |
| 465 |
} |
| 466 |
// Updates tolerances to account for stroke params that are stored as uniforms instead of |
| 467 |
// dynamic instance attributes. |
| 468 |
ENABLE_IF(StrokeAttrib::kEnabled) updateUniformStrokeParams(StrokeParams strokeParams) { |
| 469 |
SkASSERT(!(fAttribs & PatchAttribs::kStrokeParams)); |
| 470 |
fTolerances.setStroke(strokeParams, fMaxScale); |
| 471 |
} |
| 472 |
|
| 473 |
// Updates the color that will be written out with each patch. |
| 474 |
ENABLE_IF(ColorAttrib::kEnabled) updateColorAttrib(const SkPMColor4f& color) { |
| 475 |
SkASSERT(fAttribs & PatchAttribs::kColor); |
| 476 |
// Converts SkPMColor4f to the selected 'Color' attrib type. The always-wide and never-wide |
| 477 |
// branches match what VertexColor does based on the runtime check. |
| 478 |
if constexpr (req_attrib<PatchAttribs::kWideColorIfEnabled>::value) { |
| 479 |
fColor = color; |
| 480 |
} else if constexpr (opt_attrib<PatchAttribs::kWideColorIfEnabled>::value) { |
| 481 |
fColor = VertexColor(color, fAttribs & PatchAttribs::kWideColorIfEnabled); |
| 482 |
} else { |
| 483 |
fColor = color.toBytes_RGBA(); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
// Updates the paint depth written out with each patch. |
| 488 |
ENABLE_IF(DepthAttrib::kEnabled) updatePaintDepthAttrib(float depth) { |
| 489 |
SkASSERT(fAttribs & PatchAttribs::kPaintDepth); |
| 490 |
fDepth = depth; |
| 491 |
} |
| 492 |
|
| 493 |
// Updates the storage buffer index used to access uniforms. |
| 494 |
ENABLE_IF(SsboIndexAttrib::kEnabled) |
| 495 |
updateSsboIndexAttrib(uint32_t ssboIndex) { |
| 496 |
SkASSERT(fAttribs & PatchAttribs::kSsboIndex); |
| 497 |
fSsboIndex = ssboIndex; |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* writeX functions for supported patch geometry types. Every geometric type is converted to an |
| 502 |
* equivalent cubic or conic, so this will always write at minimum 8 floats for the four control |
| 503 |
* points (cubic) or three control points and {w, inf} (conics). The PatchWriter additionally |
| 504 |
* writes the current values of all attributes enabled in its PatchAttribs flags. |
| 505 |
*/ |
| 506 |
|
| 507 |
// Write a cubic curve with its four control points. |
| 508 |
AI void writeCubic(float2 p0, float2 p1, float2 p2, float2 p3) { |
| 509 |
float n4 = wangs_formula::cubic_p4(kPrecision, p0, p1, p2, p3, fApproxTransform); |
| 510 |
if constexpr (kDiscardFlatCurves) { |
| 511 |
if (n4 <= 1.f) { |
| 512 |
// This cubic only needs one segment (e.g. a line) but we're not filling space with |
| 513 |
// fans or stroking, so nothing actually needs to be drawn. |
| 514 |
return; |
| 515 |
} |
| 516 |
} |
| 517 |
if (int numPatches = this->accountForCurve(n4)) { |
| 518 |
this->chopAndWriteCubics(p0, p1, p2, p3, numPatches); |
| 519 |
} else { |
| 520 |
this->writeCubicPatch(p0, p1, p2, p3); |
| 521 |
} |
| 522 |
} |
| 523 |
AI void writeCubic(const SkPoint pts[4]) { |
| 524 |
float4 p0p1 = float4::Load(pts); |
| 525 |
float4 p2p3 = float4::Load(pts + 2); |
| 526 |
this->writeCubic(p0p1.lo, p0p1.hi, p2p3.lo, p2p3.hi); |
| 527 |
} |
| 528 |
|
| 529 |
// Write a conic curve with three control points and 'w', with the last coord of the last |
| 530 |
// control point signaling a conic by being set to infinity. |
| 531 |
AI void writeConic(float2 p0, float2 p1, float2 p2, float w) { |
| 532 |
float n2 = wangs_formula::conic_p2(kPrecision, p0, p1, p2, w, fApproxTransform); |
| 533 |
if constexpr (kDiscardFlatCurves) { |
| 534 |
if (n2 <= 1.f) { |
| 535 |
// This conic only needs one segment (e.g. a line) but we're not filling space with |
| 536 |
// fans or stroking, so nothing actually needs to be drawn. |
| 537 |
return; |
| 538 |
} |
| 539 |
} |
| 540 |
if (int numPatches = this->accountForCurve(n2 * n2)) { |
| 541 |
this->chopAndWriteConics(p0, p1, p2, w, numPatches); |
| 542 |
} else { |
| 543 |
this->writeConicPatch(p0, p1, p2, w); |
| 544 |
} |
| 545 |
} |
| 546 |
AI void writeConic(const SkPoint pts[3], float w) { |
| 547 |
this->writeConic(sk_bit_cast<float2>(pts[0]), |
| 548 |
sk_bit_cast<float2>(pts[1]), |
| 549 |
sk_bit_cast<float2>(pts[2]), |
| 550 |
w); |
| 551 |
} |
| 552 |
|
| 553 |
// Write a quadratic curve that automatically converts its three control points into an |
| 554 |
// equivalent cubic. |
| 555 |
AI void writeQuadratic(float2 p0, float2 p1, float2 p2) { |
| 556 |
float n4 = wangs_formula::quadratic_p4(kPrecision, p0, p1, p2, fApproxTransform); |
| 557 |
if constexpr (kDiscardFlatCurves) { |
| 558 |
if (n4 <= 1.f) { |
| 559 |
// This quad only needs one segment (e.g. a line) but we're not filling space with |
| 560 |
// fans or stroking, so nothing actually needs to be drawn. |
| 561 |
return; |
| 562 |
} |
| 563 |
} |
| 564 |
if (int numPatches = this->accountForCurve(n4)) { |
| 565 |
this->chopAndWriteQuads(p0, p1, p2, numPatches); |
| 566 |
} else { |
| 567 |
this->writeQuadPatch(p0, p1, p2); |
| 568 |
} |
| 569 |
} |
| 570 |
AI void writeQuadratic(const SkPoint pts[3]) { |
| 571 |
this->writeQuadratic(sk_bit_cast<float2>(pts[0]), |
| 572 |
sk_bit_cast<float2>(pts[1]), |
| 573 |
sk_bit_cast<float2>(pts[2])); |
| 574 |
} |
| 575 |
|
| 576 |
// Write a line that is automatically converted into an equivalent cubic. |
| 577 |
AI void writeLine(float4 p0p1) { |
| 578 |
if constexpr (kDiscardFlatCurves) { |
| 579 |
return; |
| 580 |
} |
| 581 |
|
| 582 |
// No chopping needed, a line only ever requires one segment (the minimum required already). |
| 583 |
fTolerances.setParametricSegments(1.f); |
| 584 |
if constexpr (kReplicateLineEndPoints) { |
| 585 |
// Visually this cubic is still a line, but 't' does not move linearly over the line, |
| 586 |
// so Wang's formula is more pessimistic. Shaders should avoid evaluating Wang's |
| 587 |
// formula when a patch has control points in this arrangement. |
| 588 |
this->writeCubicPatch(p0p1.lo, p0p1.lo, p0p1.hi, p0p1.hi); |
| 589 |
} else { |
| 590 |
// In exact math, this cubic structure should have Wang's formula return 0. Due to |
| 591 |
// floating point math, this isn't always the case, so shaders need some way to restrict |
| 592 |
// the number of parametric segments if Wang's formula numerically blows up. |
| 593 |
this->writeCubicPatch(p0p1.lo, (p0p1.zwxy() - p0p1) * (1/3.f) + p0p1, p0p1.hi); |
| 594 |
} |
| 595 |
} |
| 596 |
AI void writeLine(float2 p0, float2 p1) { this->writeLine({p0, p1}); } |
| 597 |
AI void writeLine(SkPoint p0, SkPoint p1) { |
| 598 |
this->writeLine(sk_bit_cast<float2>(p0), sk_bit_cast<float2>(p1)); |
| 599 |
} |
| 600 |
|
| 601 |
// Write a triangle by setting it to a conic with w=Inf, and using a distinct |
| 602 |
// explicit curve type for when inf isn't supported in shaders. |
| 603 |
AI void writeTriangle(float2 p0, float2 p1, float2 p2) { |
| 604 |
// No chopping needed, the max supported segment count should always support 2 lines |
| 605 |
// (which form a triangle when implicitly closed). |
| 606 |
static constexpr float kTriangleSegments_p4 = 2.f * 2.f * 2.f * 2.f; |
| 607 |
fTolerances.setParametricSegments(kTriangleSegments_p4); |
| 608 |
this->writePatch(p0, p1, p2, {SK_FloatInfinity, SK_FloatInfinity}, |
| 609 |
kTriangularConicCurveType); |
| 610 |
} |
| 611 |
AI void writeTriangle(SkPoint p0, SkPoint p1, SkPoint p2) { |
| 612 |
this->writeTriangle(sk_bit_cast<float2>(p0), |
| 613 |
sk_bit_cast<float2>(p1), |
| 614 |
sk_bit_cast<float2>(p2)); |
| 615 |
} |
| 616 |
|
| 617 |
// Writes a circle used for round caps and joins in stroking, encoded as a cubic with |
| 618 |
// identical control points and an empty join. |
| 619 |
AI void writeCircle(SkPoint p) { |
| 620 |
// This does not use writePatch() because it uses its own location as the join attribute |
| 621 |
// value instead of fJoin and never defers. |
| 622 |
fTolerances.setParametricSegments(1.f); |
| 623 |
if (VertexWriter vw = fPatchAllocator.append(fTolerances)) { |
| 624 |
vw << VertexWriter::Repeat<4>(p); // p0,p1,p2,p3 = p -> 4 copies |
| 625 |
this->emitPatchAttribs(std::move(vw), {fAttribs, p}, kCubicCurveType); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
// Writes either a square in isolation, or a half-square that will be oriented and joined |
| 630 |
// with `joinTo` (e.g. it will be a cap extending the stroke radius from `p` opposite of |
| 631 |
// the join control point). It is encoded as a conic with identical control points (vs. circles |
| 632 |
// which are cubics with identical control points). |
| 633 |
AI void writeSquare(SkPoint p, std::optional<SkPoint> joinTo) { |
| 634 |
fTolerances.setParametricSegments(1.f); |
| 635 |
if (VertexWriter vw = fPatchAllocator.append(fTolerances)) { |
| 636 |
vw << VertexWriter::Repeat<3>(p) // p0,p1,p2 = p -> 3 copies |
| 637 |
<< float2{1.f, SK_FloatInfinity}; // conic with w = 1 |
| 638 |
this->emitPatchAttribs(std::move(vw), {fAttribs, joinTo.value_or(p)}, kConicCurveType); |
| 639 |
} |
| 640 |
} |
| 641 |
AI void writeSquare(float2 p, float2 joinTo) { |
| 642 |
this->writeSquare(SkPoint{p[0], p[1]}, SkPoint{joinTo[0], joinTo[1]}); |
| 643 |
} |
| 644 |
|
| 645 |
private: |
| 646 |
AI void emitPatchAttribs(VertexWriter vertexWriter, |
| 647 |
const JoinAttrib& join, |
| 648 |
float explicitCurveType) { |
| 649 |
// NOTE: operator<< overrides automatically handle optional and disabled attribs. |
| 650 |
vertexWriter << join << fFanPoint << fStrokeParams << fColor << fDepth |
| 651 |
<< CurveTypeAttrib{fAttribs, explicitCurveType} << fSsboIndex; |
| 652 |
} |
| 653 |
|
| 654 |
AI VertexWriter appendPatch(float explicitCurveType) { |
| 655 |
if constexpr (kTrackJoinControlPoints) { |
| 656 |
// Can switch to !hasPending() once updateJoinControlPointAttrib goes away. |
| 657 |
if (fDeferredPatch.mustDefer()) { |
| 658 |
SkASSERT(PatchStride(fAttribs) <= kMaxStride); |
| 659 |
// Save the computed parametric segment tolerance value so that we can pass that to |
| 660 |
// the PatchAllocator when flushing the deferred patch. |
| 661 |
fDeferredPatch.fCurveType = explicitCurveType; |
| 662 |
fDeferredPatch.fN_p4 = fTolerances.numParametricSegments_p4(); |
| 663 |
SkASSERT(!fDeferredPatch.mustDefer() && fDeferredPatch.hasPending()); |
| 664 |
return {fDeferredPatch.fData, PatchStride(fAttribs)}; |
| 665 |
} |
| 666 |
} |
| 667 |
return fPatchAllocator.append(fTolerances); |
| 668 |
} |
| 669 |
|
| 670 |
AI void writePatch(float2 p0, float2 p1, float2 p2, float2 p3, float explicitCurveType) { |
| 671 |
if constexpr (kTrackJoinControlPoints) { |
| 672 |
// Store this even if we don't write a patch to remember we should add caps |
| 673 |
fDeferredPatch.fCurveType = explicitCurveType; |
| 674 |
} |
| 675 |
|
| 676 |
float2 last = explicitCurveType == kCubicCurveType ? p3 : p2; |
| 677 |
if (all(p0 == p1 & p1 == p2 & p2 == last)) { |
| 678 |
// Degenerate patch, so skip writing. In a regular fill or stroke, this won't affect |
| 679 |
// rendering. If there is a subsequent close verb, we may add a cap later. |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
if (VertexWriter vw = this->appendPatch(explicitCurveType)) { |
| 684 |
// NOTE: fJoin will be undefined if we're writing to a deferred patch. If that's the |
| 685 |
// case, correct data will overwrite it when the contour is closed (this is fine since a |
| 686 |
// deferred patch writes to CPU memory instead of directly to the GPU buffer). |
| 687 |
vw << p0 << p1 << p2 << p3; |
| 688 |
this->emitPatchAttribs(std::move(vw), fJoin, explicitCurveType); |
| 689 |
|
| 690 |
// Automatically update join control point for next patch. |
| 691 |
if constexpr (kTrackJoinControlPoints) { |
| 692 |
last.store(&fDeferredPatch.fLastControlPoint); |
| 693 |
// Points are ordered in reverse order to get outgoing tangent control point |
| 694 |
TangentPoint(last, p2, p1, p0).store(&fJoin); |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
// Helpers that normalize curves to a generic patch, but do no other work. |
| 700 |
AI void writeCubicPatch(float2 p0, float2 p1, float2 p2, float2 p3) { |
| 701 |
this->writePatch(p0, p1, p2, p3, kCubicCurveType); |
| 702 |
} |
| 703 |
AI void writeCubicPatch(float2 p0, float4 p1p2, float2 p3) { |
| 704 |
this->writeCubicPatch(p0, p1p2.lo, p1p2.hi, p3); |
| 705 |
} |
| 706 |
AI void writeQuadPatch(float2 p0, float2 p1, float2 p2) { |
| 707 |
this->writeCubicPatch(p0, mix(float4(p0, p2), p1.xyxy(), 2/3.f), p2); |
| 708 |
} |
| 709 |
AI void writeConicPatch(float2 p0, float2 p1, float2 p2, float w) { |
| 710 |
this->writePatch(p0, p1, p2, {w, SK_FloatInfinity}, kConicCurveType); |
| 711 |
} |
| 712 |
|
| 713 |
int accountForCurve(float n4) { |
| 714 |
if (n4 <= kMaxParametricSegments_p4) { |
| 715 |
// Record n^4 and return 0 to signal no chopping, but don't let n go below 1 as that |
| 716 |
// will cause problems when we take the log of it (particularly reaching 0, which can |
| 717 |
// happen for control points that are incredibly close to each other). |
| 718 |
fTolerances.setParametricSegments(std::max(1.f, n4)); |
| 719 |
return 0; |
| 720 |
} else { |
| 721 |
// Clamp to max allowed segmentation for a patch and return required number of chops |
| 722 |
// to achieve visual correctness. |
| 723 |
fTolerances.setParametricSegments(kMaxParametricSegments_p4); |
| 724 |
return SkScalarCeilToInt(wangs_formula::root4(std::min(n4, kMaxSegmentsPerCurve_p4) / |
| 725 |
kMaxParametricSegments_p4)); |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
// This does not return b when t==1, but it otherwise seems to get better precision than |
| 730 |
// "a*(1 - t) + b*t" for things like chopping cubics on exact cusp points. |
| 731 |
// The responsibility falls on the caller to check that t != 1 before calling. |
| 732 |
static AI float4 mix(float4 a, float4 b, float4 T) { |
| 733 |
SkASSERT(all((0 <= T) & (T < 1))); |
| 734 |
return (b - a)*T + a; |
| 735 |
} |
| 736 |
|
| 737 |
// Helpers that chop the curve type into 'numPatches' parametrically uniform curves. It is |
| 738 |
// assumed that 'numPatches' is calculated such that the resulting curves require the maximum |
| 739 |
// number of segments to draw appropriately (since the original presumably needed even more). |
| 740 |
void chopAndWriteQuads(float2 p0, float2 p1, float2 p2, int numPatches) { |
| 741 |
InnerTriangulator triangulator(numPatches, sk_bit_cast<SkPoint>(p0)); |
| 742 |
for (; numPatches >= 3; numPatches -= 2) { |
| 743 |
// Chop into 3 quads. |
| 744 |
float4 T = float4(1,1,2,2) / numPatches; |
| 745 |
float4 ab = mix(p0.xyxy(), p1.xyxy(), T); |
| 746 |
float4 bc = mix(p1.xyxy(), p2.xyxy(), T); |
| 747 |
float4 abc = mix(ab, bc, T); |
| 748 |
// p1 & p2 of the cubic representation of the middle quad. |
| 749 |
float4 middle = mix(ab, bc, mix(T, T.zwxy(), 2/3.f)); |
| 750 |
|
| 751 |
this->writeQuadPatch(p0, ab.lo, abc.lo); // Write the 1st quad. |
| 752 |
if constexpr (kAddTrianglesWhenChopping) { |
| 753 |
this->writeTriangle(p0, abc.lo, abc.hi); |
| 754 |
} |
| 755 |
this->writeCubicPatch(abc.lo, middle, abc.hi); // Write the 2nd quad (already a cubic) |
| 756 |
if constexpr (kAddTrianglesWhenChopping) { |
| 757 |
this->writeTriangleStack(triangulator.pushVertex(sk_bit_cast<SkPoint>(abc.hi))); |
| 758 |
} |
| 759 |
std::tie(p0, p1) = {abc.hi, bc.hi}; // Save the 3rd quad. |
| 760 |
} |
| 761 |
if (numPatches == 2) { |
| 762 |
// Chop into 2 quads. |
| 763 |
float2 ab = (p0 + p1) * .5f; |
| 764 |
float2 bc = (p1 + p2) * .5f; |
| 765 |
float2 abc = (ab + bc) * .5f; |
| 766 |
|
| 767 |
this->writeQuadPatch(p0, ab, abc); // Write the 1st quad. |
| 768 |
if constexpr (kAddTrianglesWhenChopping) { |
| 769 |
this->writeTriangle(p0, abc, p2); |
| 770 |
} |
| 771 |
this->writeQuadPatch(abc, bc, p2); // Write the 2nd quad. |
| 772 |
} else { |
| 773 |
SkASSERT(numPatches == 1); |
| 774 |
this->writeQuadPatch(p0, p1, p2); // Write the single remaining quad. |
| 775 |
} |
| 776 |
if constexpr (kAddTrianglesWhenChopping) { |
| 777 |
this->writeTriangleStack(triangulator.pushVertex(sk_bit_cast<SkPoint>(p2))); |
| 778 |
this->writeTriangleStack(triangulator.close()); |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
void chopAndWriteConics(float2 p0, float2 p1, float2 p2, float w, int numPatches) { |
| 783 |
InnerTriangulator triangulator(numPatches, sk_bit_cast<SkPoint>(p0)); |
| 784 |
// Load the conic in 3d homogeneous (unprojected) space. |
| 785 |
float4 h0 = float4(p0,1,1); |
| 786 |
float4 h1 = float4(p1,1,1) * w; |
| 787 |
float4 h2 = float4(p2,1,1); |
| 788 |
for (; numPatches >= 2; --numPatches) { |
| 789 |
// Chop in homogeneous space. |
| 790 |
float T = 1.f/numPatches; |
| 791 |
float4 ab = mix(h0, h1, T); |
| 792 |
float4 bc = mix(h1, h2, T); |
| 793 |
float4 abc = mix(ab, bc, T); |
| 794 |
|
| 795 |
// Project and write the 1st conic. |
| 796 |
float2 midpoint = abc.xy() / abc.w(); |
| 797 |
this->writeConicPatch(h0.xy() / h0.w(), |
| 798 |
ab.xy() / ab.w(), |
| 799 |
midpoint, |
| 800 |
ab.w() / sqrtf(h0.w() * abc.w())); |
| 801 |
if constexpr (kAddTrianglesWhenChopping) { |
| 802 |
this->writeTriangleStack(triangulator.pushVertex(sk_bit_cast<SkPoint>(midpoint))); |
| 803 |
} |
| 804 |
std::tie(h0, h1) = {abc, bc}; // Save the 2nd conic (in homogeneous space). |
| 805 |
} |
| 806 |
// Project and write the remaining conic. |
| 807 |
SkASSERT(numPatches == 1); |
| 808 |
this->writeConicPatch(h0.xy() / h0.w(), |
| 809 |
h1.xy() / h1.w(), |
| 810 |
h2.xy(), // h2.w == 1 |
| 811 |
h1.w() / sqrtf(h0.w())); |
| 812 |
if constexpr (kAddTrianglesWhenChopping) { |
| 813 |
this->writeTriangleStack(triangulator.pushVertex(sk_bit_cast<SkPoint>(h2.xy()))); |
| 814 |
this->writeTriangleStack(triangulator.close()); |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
void chopAndWriteCubics(float2 p0, float2 p1, float2 p2, float2 p3, int numPatches) { |
| 819 |
InnerTriangulator triangulator(numPatches, sk_bit_cast<SkPoint>(p0)); |
| 820 |
for (; numPatches >= 3; numPatches -= 2) { |
| 821 |
// Chop into 3 cubics. |
| 822 |
float4 T = float4(1,1,2,2) / numPatches; |
| 823 |
float4 ab = mix(p0.xyxy(), p1.xyxy(), T); |
| 824 |
float4 bc = mix(p1.xyxy(), p2.xyxy(), T); |
| 825 |
float4 cd = mix(p2.xyxy(), p3.xyxy(), T); |
| 826 |
float4 abc = mix(ab, bc, T); |
| 827 |
float4 bcd = mix(bc, cd, T); |
| 828 |
float4 abcd = mix(abc, bcd, T); |
| 829 |
float4 middle = mix(abc, bcd, T.zwxy()); // p1 & p2 of the middle cubic. |
| 830 |
|
| 831 |
this->writeCubicPatch(p0, ab.lo, abc.lo, abcd.lo); // Write the 1st cubic. |
| 832 |
if constexpr (kAddTrianglesWhenChopping) { |
| 833 |
this->writeTriangle(p0, abcd.lo, abcd.hi); |
| 834 |
} |
| 835 |
this->writeCubicPatch(abcd.lo, middle, abcd.hi); // Write the 2nd cubic. |
| 836 |
if constexpr (kAddTrianglesWhenChopping) { |
| 837 |
this->writeTriangleStack(triangulator.pushVertex(sk_bit_cast<SkPoint>(abcd.hi))); |
| 838 |
} |
| 839 |
std::tie(p0, p1, p2) = {abcd.hi, bcd.hi, cd.hi}; // Save the 3rd cubic. |
| 840 |
} |
| 841 |
if (numPatches == 2) { |
| 842 |
// Chop into 2 cubics. |
| 843 |
float2 ab = (p0 + p1) * .5f; |
| 844 |
float2 bc = (p1 + p2) * .5f; |
| 845 |
float2 cd = (p2 + p3) * .5f; |
| 846 |
float2 abc = (ab + bc) * .5f; |
| 847 |
float2 bcd = (bc + cd) * .5f; |
| 848 |
float2 abcd = (abc + bcd) * .5f; |
| 849 |
|
| 850 |
this->writeCubicPatch(p0, ab, abc, abcd); // Write the 1st cubic. |
| 851 |
if constexpr (kAddTrianglesWhenChopping) { |
| 852 |
this->writeTriangle(p0, abcd, p3); |
| 853 |
} |
| 854 |
this->writeCubicPatch(abcd, bcd, cd, p3); // Write the 2nd cubic. |
| 855 |
} else { |
| 856 |
SkASSERT(numPatches == 1); |
| 857 |
this->writeCubicPatch(p0, p1, p2, p3); // Write the single remaining cubic. |
| 858 |
} |
| 859 |
if constexpr (kAddTrianglesWhenChopping) { |
| 860 |
this->writeTriangleStack(triangulator.pushVertex(sk_bit_cast<SkPoint>(p3))); |
| 861 |
this->writeTriangleStack(triangulator.close()); |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
ENABLE_IF(kAddTrianglesWhenChopping) |
| 866 |
writeTriangleStack(MiddleOutPolygonTriangulator::PoppedTriangleStack&& stack) { |
| 867 |
for (auto [p0, p1, p2] : stack) { |
| 868 |
this->writeTriangle(p0, p1, p2); |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
static float2 TangentPoint(float2 p0, float2 p1, float2 p2, float2 p3) { |
| 873 |
if (any(p0 != p1)) { |
| 874 |
return p1; |
| 875 |
} else if (any(p2 != p1)) { |
| 876 |
return p2; |
| 877 |
} else { |
| 878 |
return p3; |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
// Runtime configuration, will always contain required attribs but may not have all optional |
| 883 |
// attribs enabled (e.g. depending on caps or batching). |
| 884 |
const PatchAttribs fAttribs; |
| 885 |
|
| 886 |
// The 2x2 approximation of the local-to-device transform that will affect subsequently |
| 887 |
// recorded curves (when fully transformed in the vertex shader). |
| 888 |
wangs_formula::VectorXform fApproxTransform = {}; |
| 889 |
// A maximum scale factor extracted from the current approximate transform. |
| 890 |
float fMaxScale = 1.0f; |
| 891 |
// Tracks the linear tolerances for the most recently written patches. |
| 892 |
LinearTolerances fTolerances; |
| 893 |
|
| 894 |
PatchAllocator fPatchAllocator; |
| 895 |
DeferredPatch fDeferredPatch; // only usable if kTrackJoinControlPoints is true |
| 896 |
|
| 897 |
// Instance attribute state written after the 4 control points of a patch |
| 898 |
JoinAttrib fJoin; |
| 899 |
FanPointAttrib fFanPoint; |
| 900 |
StrokeAttrib fStrokeParams; |
| 901 |
ColorAttrib fColor; |
| 902 |
DepthAttrib fDepth; |
| 903 |
|
| 904 |
// Index into a shared storage buffer containing this PatchWriter's patches' corresponding |
| 905 |
// uniforms. Written out as an attribute with every patch, to read the appropriate uniform |
| 906 |
// values from the storage buffer on draw. |
| 907 |
SsboIndexAttrib fSsboIndex; |
| 908 |
}; |
| 909 |
|
| 910 |
} // namespace skgpu::tess |
| 911 |
|
| 912 |
#undef ENABLE_IF |
| 913 |
#undef AI |
| 914 |
|
| 915 |
#endif // skgpu_tessellate_PatchWriter_DEFINED |