Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/pixie/paths.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1597,10 +1597,19 @@ proc fillShapes(
windingRule: WindingRule,
blendMode: BlendMode
) =
template hasCoverage(area: float32): bool =
# Quantize geometry independently of paint alpha, using the same rounding
# as the color conversion on this backend.
when allowSimd and defined(amd64):
mm_cvtss_si32(mm_set_ss(area * 255)) != 0
else:
round(area * 255) != 0

# Figure out the total bounds of all the shapes,
# rasterize only within the total bounds
let
rgbx = color.asRgbx()
clearsUncovered = blendMode == MaskBlend
segments = shapes.shapesToSegments()
bounds = computeBounds(segments).snapToPixels()
startX = max(0, bounds.x.int)
Expand Down Expand Up @@ -1806,7 +1815,8 @@ proc fillShapes(
applyOpacity(vecRgbx, area)
else:
rgbx * area
image.data[dataIndex] = blender(backdrop, source)
if hasCoverage(area) or clearsUncovered:
image.data[dataIndex] = blender(backdrop, source)

block: # Right-side partial coverage
let
Expand Down Expand Up @@ -1844,7 +1854,8 @@ proc fillShapes(
applyOpacity(vecRgbx, area)
else:
rgbx * area
image.data[dataIndex] = blender(backdrop, source)
if hasCoverage(area) or clearsUncovered:
image.data[dataIndex] = blender(backdrop, source)

let
fillBegin = leftCoverEnd.clamp(0, image.width)
Expand Down
14 changes: 13 additions & 1 deletion src/pixie/simd/avx2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,19 @@ proc blendLineCoverageOverwriteAvx2*(
mm_srli_si128(coverageHi, 8),
]
for j in 0 ..< 4:
mm256_store_si256(line[i].addr, rgbxVec.applyCoverage(coverages[j]))
let
preserve = mm256_cmpeq_epi32(
mm256_cvtepu8_epi32(coverages[j]), vecZero
)
source = rgbxVec.applyCoverage(coverages[j])
backdrop = mm256_load_si256(line[i].addr)
mm256_store_si256(
line[i].addr,
mm256_or_si256(
mm256_and_si256(preserve, backdrop),
mm256_andnot_si256(preserve, source)
)
)
i += 8

for i in i ..< len:
Expand Down
17 changes: 13 additions & 4 deletions src/pixie/simd/neon.nim
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,20 @@ proc blendLineCoverageOverwriteNeon*(
elif mask255 == uint64.high:
vst4q_u8(line[i].addr, vecRgbx)
else:
let backdrop = vld4q_u8(line[i].addr)
var source: uint8x16x4
source.val[0] = multiplyDiv255(vecRgbx.val[0], coverage)
source.val[1] = multiplyDiv255(vecRgbx.val[1], coverage)
source.val[2] = multiplyDiv255(vecRgbx.val[2], coverage)
source.val[3] = multiplyDiv255(vecRgbx.val[3], coverage)
source.val[0] = vbslq_u8(
eqZero, backdrop.val[0], multiplyDiv255(vecRgbx.val[0], coverage)
)
source.val[1] = vbslq_u8(
eqZero, backdrop.val[1], multiplyDiv255(vecRgbx.val[1], coverage)
)
source.val[2] = vbslq_u8(
eqZero, backdrop.val[2], multiplyDiv255(vecRgbx.val[2], coverage)
)
source.val[3] = vbslq_u8(
eqZero, backdrop.val[3], multiplyDiv255(vecRgbx.val[3], coverage)
)
vst4q_u8(line[i].addr, source)

i += 16
Expand Down
18 changes: 16 additions & 2 deletions src/pixie/simd/sse2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,24 @@ proc blendLineCoverageOverwriteSse2*(
mm_store_si128(line[i].addr, rgbxVec)
i += 4
else:
var coverage = coverage
var
coverage = coverage
eqZero = eqZero
for _ in 0 ..< 4:
mm_store_si128(line[i].addr, rgbxVec.applyCoverage(coverage))
var preserve = mm_unpacklo_epi8(eqZero, eqZero)
preserve = mm_unpacklo_epi16(preserve, preserve)
let
source = rgbxVec.applyCoverage(coverage)
backdrop = mm_load_si128(line[i].addr)
mm_store_si128(
line[i].addr,
mm_or_si128(
mm_and_si128(preserve, backdrop),
mm_andnot_si128(preserve, source)
)
)
coverage = mm_srli_si128(coverage, 4)
eqZero = mm_srli_si128(eqZero, 4)
i += 4

for i in i ..< len:
Expand Down
Binary file added tests/paths/overwriteCircle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/paths/overwriteStar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 38 additions & 1 deletion tests/test_paths.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chroma, pixie, pixie/fileformats/png, strformat, xrays
import chroma, pixie, std/math, pixie/fileformats/png, strformat, xrays

block:
let pathStr = """
Expand Down Expand Up @@ -760,3 +760,40 @@ block:
"""
let path = parsePath(pathStr)
doAssert path.computeBounds() == rect(0, 0, 0, 100)

block:
# OverwriteBlend must leave pixels the path does not cover, so on an opaque
# backdrop nothing can end up fully transparent.
let
image = newImage(128, 64)
paint = newPaint(SolidPaint)
path = newPath()
image.fill(rgbx(0, 0, 255, 255))
paint.color = color(1, 0, 0, 1)
paint.blendMode = OverwriteBlend
path.circle(circle(vec2(64, 32), 40))
image.fillPath(path, paint)
for c in image.data:
doAssert c.a != 0
image.xray("tests/paths/overwriteCircle.png")

block:
# Spikes thin enough that coverage rounds to nothing near their tips.
let
image = newImage(260, 200)
paint = newPaint(SolidPaint)
path = newPath()
image.fill(rgbx(0, 0, 255, 255))
paint.color = color(1, 0.85, 0.1, 1)
paint.blendMode = OverwriteBlend
for i in 0 ..< 80:
let
a = i.float32 * PI.float32 / 40
r = if (i and 1) == 0: 42.0 else: 92.0
pt = vec2(130 + cos(a) * r, 100 + sin(a) * r)
if i == 0: path.moveTo(pt) else: path.lineTo(pt)
path.closePath()
image.fillPath(path, paint)
for c in image.data:
doAssert c.a != 0
image.xray("tests/paths/overwriteStar.png")
68 changes: 68 additions & 0 deletions tests/test_paths_overwrite.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pixie, std/math

block:
# clearRect uses transparent OverwriteBlend paint. Positive path coverage
# must still clear a pixel even though the resulting source alpha is zero.
let image = newImage(128, 64)
let backdrop = rgbx(0, 0, 255, 255)
image.fill(backdrop)
let ctx = image.newContext()
ctx.clearRect(10.25, 10.25, 70.5, 40.5)
for y in 0 ..< image.height:
for x in 0 ..< image.width:
let expected =
if x in 10 .. 80 and y in 10 .. 50:
rgbx(0, 0, 0, 0)
else:
backdrop
doAssert image[x, y] == expected, "clearRect pixel " & $x & "," & $y

block:
# Overwrite must preserve uncovered pixels and replace covered pixels,
# including when translucent paint rounds to a fully transparent source.
for shape in ["circle", "star", "triangle", "rectangle"]:
let
width = if shape == "star": 260 else: 128
height = if shape == "star": 200 else: 64
path = newPath()
case shape
of "circle":
path.circle(circle(vec2(64, 32), 40))
of "star":
for i in 0 ..< 80:
let
angle = i.float32 * PI.float32 / 40
radius = if (i and 1) == 0: 42.0 else: 92.0
point = vec2(130 + cos(angle) * radius, 100 + sin(angle) * radius)
if i == 0: path.moveTo(point)
else: path.lineTo(point)
path.closePath()
of "triangle":
path.moveTo(10.25, 10.25)
path.lineTo(90.75, 10.25)
path.lineTo(30.25, 50.75)
path.closePath()
else:
path.rect(10.25, 10.25, 70.5, 40.5)

# Opaque paint reveals coverage independently of the tested paint's alpha.
let coverage = newImage(width, height)
coverage.fillPath(path, color(1, 0, 0, 1))
for alpha in [255, 128, 1, 0]:
let
paint = newPaint(SolidPaint)
source = newImage(width, height)
actual = newImage(width, height)
backdrop = rgbx(0, 0, 255, 255)
paint.color = color(1, 0, 0, alpha.float32 / 255)
# NormalBlend onto transparent black yields the expected source color.
source.fillPath(path, paint)
actual.fill(backdrop)
paint.blendMode = OverwriteBlend
actual.fillPath(path, paint)
for i, pixel in actual.data:
let expected =
if coverage.data[i].a == 0: backdrop
else: source.data[i]
doAssert pixel == expected,
shape & " alpha=" & $alpha & " pixel=" & $(i mod width) & "," & $(i div width)
1 change: 1 addition & 0 deletions tests/tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import
test_jpeg,
test_paints,
test_paths,
test_paths_overwrite,
test_png,
test_ppm,
test_qoi,
Expand Down