-
Notifications
You must be signed in to change notification settings - Fork 23
Implement adaptive anti-aliasing for polygon rendering #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jserv
wants to merge
2
commits into
main
Choose a base branch
from
aa-adaptive
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This improves span fill for perfectly vertical edges (dx=0) with span-width threshold. This optimization targets the common case of rectangles and UI elements while preserving full quality for text, curves, and diagonal strokes. Implementation Strategy: - Conservative approach: only optimize dx=0 edges (mathematically safe) - Rejected slope-based thresholds (caused text aliasing at 30-50°) - Use constant coverage values to eliminate array lookups - Apply only to wide spans (>=16 pixels) to avoid branch overhead Technical Details: - Add _span_fill_vertical() for vertical edge fast path - Constant multiplication: count * 0x10 (compiler optimizes to shift) - Full pixel coverage: 0x40 (vs array lookup) - Span width threshold: 16 pixels * 4 samples = 64 samples Performance Results (mado-perf, 3-run average, Apple M1): - 500x500 rectangles: +7.9% (p<0.05, statistically significant) - 100x100 rectangles: +4.0% (p≈0.07, borderline significant) - 500x500 vertical lines: +3.1% - 100x100 vertical lines: +2.6% - Text shapes: +0-2% (quality preserved, zero visual regression) Precision Trade-off: - Row 2 partial pixels: 0.4% rounding error (1/255) - Visually imperceptible, verified with demo-sdl - Error: 0x10 vs 0x0f for first sub-pixel - Full pixel: 0x40 vs 0x3f (accepted design choice) Threshold Validation: - Tested 4-pixel threshold: -30% to -35% regression (branch overhead) - 16-pixel threshold: optimal balance (+2.6% to +7.9%)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 issue found across 2 files
Prompt for AI agents (all 1 issues)
Understand the root cause of the following 1 issues and fix them.
<file name="src/poly.c">
<violation number="1" location="src/poly.c:389">
The vertical-span fast path is triggered by checking `edge_start->dx == 0`, but this dx has already been reduced to the Bresenham remainder, so integer-slope diagonals (e.g., 45° lines where dx == dy) also satisfy the condition and drop to 1x1 AA, causing visible aliasing. Please base the check on a flag that preserves the original slope (e.g., `aa_quality`).</violation>
</file>
React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.
The previous implementation incorrectly checked 'dx == 0' to detect vertical edges, but dx is modified by Bresenham reduction. This caused diagonal lines with integer slopes to incorrectly use the vertical optimization, resulting in thin diagonal strokes disappearing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
Anti-aliasing (AA) reduces visual artifacts (jaggies) on rasterized edges by sampling at sub-pixel positions. Traditional fixed AA uses the same sampling grid (e.g., 4x4) for all edges, which is accurate but expensive.
Standard 4x4 AA samples each pixel at 16 sub-pixel positions to compute coverage. Adaptive AA observes that:
Implementation Strategy
Conservative Approach: Only optimize perfectly vertical edges (dx=0)
Span-Width Threshold: Apply optimization only to wide spans (≥16 pixels)
Technical Details
_span_fill_vertical()for vertical edge fast pathPerformance Results
Measured with
mado-perf, 3-run average, Apple M1:Statistical Validation:
Precision Trade-off
Threshold Validation
Empirical testing of different threshold values:
Why Conservative Approach?
Initial attempts with aggressive AA optimization caused issues:
Summary by cubic
Implemented adaptive anti-aliasing for polygon rendering that uses optimized span fill on perfectly vertical edges (dx=0) with 16-pixel span-width threshold. This delivers 2.6-7.9% speedups on common UI elements with zero visible quality loss, while preserving full 4x4 AA for text, curves, and diagonal strokes.
_span_fill_vertical()optimized fast path for vertical edgesWritten for commit 5841ceb29843f8831c80ff3968588ad44a60dfc3.