vector: add sample-aware path clipping
Operating System
- Windows
- macOS
- Linux
- FreeBSD
- OpenBSD
- Android
- iOS
- Nintendo Switch
- PlayStation 5
- Xbox
- Web Browsers
This is a platform-independent feature request. The exploratory coverage measurements below were run on macOS only.
What feature would you like to be added?
Add path-based clipping to vector, usable with filled and stroked paths, with antialiasing that preserves shape/clip sample coverage until clipping has been applied.
This is a prerequisite for the internal OpenType SVG renderer proposed in #3796, but the feature belongs to vector and should be usable independently of SVG.
The preferred implementation direction is sample-aware clipping: intersect coverage at corresponding sample positions before averaging it into pixel alpha. This uses curved paths as clip geometry, but does not require computing new Bezier outlines through geometric Boolean operations.
Tentative API
One candidate is a clipping field on DrawPathOptions, with types along these lines:
type ClipElement struct {
Path *Path
FillRule FillRule
}
type Clip struct {
Elements []ClipElement
}
// Additional field in DrawPathOptions:
Clips []ClipProposed semantics:
- Elements within one
Clipform a union, with each element evaluated using its own fill rule. - Multiple clips form an intersection.
- Clip paths use destination coordinates, consistent with the current path drawing API.
Clips == niland an emptyClipsslice impose no restriction and preserve existing zero-value rendering behavior.- A clip with no elements contains no points:
Clips: []Clip{{}}renders nothing.
Concatenating all outlines into one path is not generally equivalent to the union of independently filled elements. The fill rule must be respected before combining their coverage.
The names and exact representation are provisional. Validate nested composition, including cases that require clipping individual elements before a union, before freezing the public API. Specify path lifetime/mutation behavior and invalid/nil element handling consistently with existing drawing operations.
Implementation and validation
- Prototype access to the existing rasterizer's per-sample winding/coverage information. Apply each element's fill rule, combine clip elements by union and clips by intersection, and intersect the result with the drawn shape before coverage resolution.
- Exercise native antialiasing, disabled antialiasing, and supersampled rendering. Clipping semantics must not change with the selected quality policy.
- Validate clipping of strokes, transformed paths, different fill rules, multiple elements, and multiple restrictions. Check destination subimages and the existing batching/resource lifecycle.
- Test composition boundaries with intermediate layers and isolated opacity groups. Correctly clipping one draw does not prove that clipping a previously resolved group image is correct. Identify what information the drawing pipeline must preserve; leave SVG parsing and its group tree to the caller.
- Measure the cost of simple and complex clips, nesting, repeated use, and temporary buffers. Keep an ordinary alpha-mask implementation from becoming the only internal representation, since it loses correlated coverage information.
- Finalize the API and add behavioral tests once the semantics and implementation are verified across representative graphics backends.
Acceptance cases should include:
- No clips gives the same result as existing rendering.
- An empty clip produces no output.
- An identical shape used as its own clip preserves the unclipped pixels.
- A partial clip actually removes the appropriate region, so ignoring clipping cannot pass the identity test alone.
- Duplicate clip restrictions are idempotent, and union/intersection results do not depend on operand order.
- Nonzero and even-odd holes, disjoint unions, fractional positions, curves, strokes, and nested composition produce the intended coverage.
- Shared and coincident boundaries do not become too faint through multiplication of already-resolved alpha.
A gradient/shader-paint API may be useful independently, but is not required as part of this clipping feature.
Why is this needed?
Curved clipping is useful for vector drawing and is required for correct OpenType SVG glyph rendering. Ordinary image alpha multiplication is not sufficient for antialiased geometric clipping.
For example, if four of eight samples are inside a shape, its coverage is 1/2. An identical clip contains those same four samples, so clipping should leave coverage at 1/2. Multiplying the two resolved alpha values gives 1/4 instead and incorrectly thins the edge.
A rendering prototype at Ebitengine commit 3f273b3f52b9912926023938d38aec5ec9438f00 reproduced this with a circle clipped by an identical circle. At 24px with a 0.375px offset, the loss of alpha area was:
| Rendering/composition method | Lost fully opaque pixel equivalents |
|---|---|
| Native vector AA, multiply resolved alpha | 7.969 |
| 2x vector AA, multiply alpha, then downsample | 4.357 |
| 4x vector AA, multiply alpha, then downsample | 2.173 |
| 8x point sampling, compose before final downsampling | 0.000 |
The point-sampled method preserved the complete pixel output in all eight tested combinations of size (16, 24, 64, 256px) and offset (0, 0.375px). This demonstrates the importance of composition before sample resolution. It is not a proposal to always allocate 8x surfaces: those have 64 times the pixels per surface.
The current vector implementation already retains separate information for its AA samples internally, making it a plausible foundation for a more efficient implementation. That implementation has not yet been proven by the prototype. Supersampling quality policy and clipping correctness should remain separate concerns.
Authored by Codex (OpenAI), on behalf of @hajimehoshi, following a rendering prototype and design discussion.
Source: hajimehoshi/ebiten