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
2 changes: 1 addition & 1 deletion src/pixie/paths.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@ proc blendLineCoverageMask(
) {.hasSimd.} =
for i in 0 ..< len:
let coverage = coverages[i]
if coverage == 255:
if coverage == 255 and rgbx.a == 255:
discard
else:
line[i] = blendMask(line[i], rgbx * coverage)
Expand Down
7 changes: 5 additions & 2 deletions src/pixie/simd/avx2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ template applyCoverage(rgbxVec: M256i, coverage: M128i): M256i =
rgbxOdd = mm256_and_si256(rgbxVec, oddMask)
rgbxEven = mm256_mulhi_epu16(rgbxEven, unpacked)
rgbxOdd = mm256_mulhi_epu16(rgbxOdd, unpacked)
# Match scalar coverage rounding before dividing by 255.
rgbxEven = mm256_add_epi16(rgbxEven, mm256_set1_epi16(127))
rgbxOdd = mm256_add_epi16(rgbxOdd, mm256_set1_epi16(127))
rgbxEven = mm256_srli_epi16(mm256_mulhi_epu16(rgbxEven, div255), 7)
rgbxOdd = mm256_srli_epi16(mm256_mulhi_epu16(rgbxOdd, div255), 7)

Expand Down Expand Up @@ -687,7 +690,7 @@ proc blendLineCoverageMaskAvx2*(
let coverage = coverages[i]
if coverage == 0:
line[i] = rgbx(0, 0, 0, 0)
elif coverage == 255:
elif coverage == 255 and rgbx.a == 255:
discard
else:
line[i] = blendMask(line[i], rgbx * coverage)
Expand Down Expand Up @@ -739,7 +742,7 @@ proc blendLineCoverageMaskAvx2*(
let coverage = coverages[i]
if coverage == 0:
line[i] = rgbx(0, 0, 0, 0)
elif coverage == 255:
elif coverage == 255 and rgbx.a == 255:
discard
else:
line[i] = blendMask(line[i], rgbx * coverage)
Expand Down
4 changes: 2 additions & 2 deletions src/pixie/simd/neon.nim
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ proc blendLineCoverageMaskNeon*(
let coverage = coverages[i]
if coverage == 0:
line[i] = rgbx(0, 0, 0, 0)
elif coverage == 255:
elif coverage == 255 and rgbx.a == 255:
discard
else:
line[i] = blendMask(line[i], rgbx * coverage)
Expand Down Expand Up @@ -703,7 +703,7 @@ proc blendLineCoverageMaskNeon*(
let coverage = coverages[i]
if coverage == 0:
line[i] = rgbx(0, 0, 0, 0)
elif coverage == 255:
elif coverage == 255 and rgbx.a == 255:
discard
else:
line[i] = blendMask(line[i], rgbx * coverage)
Expand Down
7 changes: 5 additions & 2 deletions src/pixie/simd/sse2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ template applyCoverage(rgbxVec, coverage: M128i): M128i =
rgbxOdd = mm_and_si128(rgbxVec, oddMask)
rgbxEven = mm_mulhi_epu16(rgbxEven, unpacked)
rgbxOdd = mm_mulhi_epu16(rgbxOdd, unpacked)
# Match scalar coverage rounding before dividing by 255.
rgbxEven = mm_add_epi16(rgbxEven, mm_set1_epi16(127))
rgbxOdd = mm_add_epi16(rgbxOdd, mm_set1_epi16(127))
rgbxEven = mm_srli_epi16(mm_mulhi_epu16(rgbxEven, div255), 7)
rgbxOdd = mm_srli_epi16(mm_mulhi_epu16(rgbxOdd, div255), 7)

Expand Down Expand Up @@ -739,7 +742,7 @@ proc blendLineCoverageMaskSse2*(
let coverage = coverages[i]
if coverage == 0:
line[i] = rgbx(0, 0, 0, 0)
elif coverage == 255:
elif coverage == 255 and rgbx.a == 255:
discard
else:
line[i] = blendMask(line[i], rgbx * coverage)
Expand Down Expand Up @@ -777,7 +780,7 @@ proc blendLineCoverageMaskSse2*(
let coverage = coverages[i]
if coverage == 0:
line[i] = rgbx(0, 0, 0, 0)
elif coverage == 255:
elif coverage == 255 and rgbx.a == 255:
discard
else:
line[i] = blendMask(line[i], rgbx * coverage)
Expand Down
34 changes: 34 additions & 0 deletions tests/test_paths_mask.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pixie

block:
# Sloping edges put fully covered pixels in the same scanline as partial
# coverage, exercising both SIMD blocks and scalar prefixes/tails.
let white = rgbx(255, 255, 255, 255)
for width in [1, 15, 16, 17, 31, 32, 33, 64, 80]:
for shift in 0 .. 7:
let
path = newPath()
x = shift.float32
path.moveTo(x + 0.25, 0.25)
path.lineTo(x + width.float32 + 0.75, 1.75)
path.lineTo(x + width.float32 + 0.25, 7.75)
path.lineTo(x + 0.75, 6.25)
path.closePath()

let coverage = newImage(width + 32, 9)
coverage.fillPath(path, white)
for alpha in [1'u8, 128, 201, 255]:
let
actual = newImage(coverage.width, coverage.height)
paint = newPaint(SolidPaint)
mask = rgbx(alpha, alpha, alpha, alpha)
actual.fill(white)
paint.color = color(1, 1, 1, alpha.float32 / 255)
paint.blendMode = MaskBlend
actual.fillPath(path, paint)
for i, pixel in actual.data:
let expected = mask * coverage.data[i].a
doAssert pixel == expected,
"mask width=" & $width & " shift=" & $shift &
" alpha=" & $alpha & " pixel=" & $(i mod actual.width) &
"," & $(i div actual.width)
158 changes: 112 additions & 46 deletions tests/test_simd.nim
Original file line number Diff line number Diff line change
@@ -1,51 +1,117 @@
import pixie/simd

when allowSimd and defined(amd64) and not defined(pixieNoAvx):
when allowSimd and defined(amd64):
import chroma, pixie/blends, pixie/common

if cpuHasAvx2:
let
original = rgbx(120, 80, 40, 255)
mask = rgbx(255, 255, 255, 255)
proc coveragePixel(
backdrop, color: ColorRGBX, coverage: uint8, mode: static BlendMode
): ColorRGBX =
when mode == OverwriteBlend:
if coverage == 0: backdrop
else: color * coverage
elif mode == MaskBlend:
blendMask(backdrop, color * coverage)
else:
blendNormal(backdrop, color * coverage)

proc testCoverage(name: string, blender: auto, mode: static BlendMode) =
## Compares vector coverage with scalar pixels across every alignment.
let background = rgbx(90, 110, 130, 255)
var
pixels: array[104, ColorRGBX]
coverages: array[80, uint8]
for color in [rgbx(240, 144, 72, 255), rgbx(13, 97, 151, 201),
rgbx(255, 255, 255, 255), rgbx(0, 0, 0, 0)]:
for coverage in 0 .. 255:
for value in coverages.mitems:
value = coverage.uint8
let expected = coveragePixel(background, color, coverage.uint8, mode)
for shift in 0 .. 7:
let start = 8 + shift
for pixel in pixels.mitems:
pixel = background
blender(
cast[ptr UncheckedArray[ColorRGBX]](pixels[start].addr),
cast[ptr UncheckedArray[uint8]](coverages[0].addr),
color,
coverages.len
)
for i, pixel in pixels:
let wanted =
if i >= start and i < start + coverages.len:
expected
else:
background
doAssert pixel == wanted,
name & " coverage=" & $coverage & " shift=" & $shift &
" color=" & $color & " pixel=" & $(i - start)
echo name, " coverage rounding passed"

proc testCoverageSpans(name: string, blender: auto, mode: static BlendMode) =
let original = rgbx(120, 80, 40, 255)
var cases: int
for length in 0 .. 160:
for shift in 0 .. 7:
for pattern in 0 .. 3:
var pixels = newSeq[ColorRGBX](length + 96)
for pixel in pixels.mitems:
pixel = original

# Exercise every pixel alignment, with sentinels on both sides.
var start: int
while (cast[uint](pixels[start].addr) and 31) != 0:
inc start
start += 16 + shift

# Padding makes the old overread safe while its extra stores still
# corrupt the sentinels, independently of the allocator's layout.
var coverages = newSeq[uint8](length + 32)
for i in 0 ..< length:
coverages[i] = case pattern
of 0: 0'u8
of 1: 255'u8
of 2: 128'u8
else: ((i * 73 + 19) mod 256).uint8

blendLineCoverageMaskAvx2(
cast[ptr UncheckedArray[ColorRGBX]](pixels[start].addr),
cast[ptr UncheckedArray[uint8]](coverages[0].addr),
mask,
length
)

for i, pixel in pixels:
let expected =
if i >= start and i < start + length:
blendMask(original, mask * coverages[i - start])
else:
original
doAssert pixel == expected,
"length=" & $length & " shift=" & $shift &
" pattern=" & $pattern & " pixel=" & $(i - start)
inc cases
echo "AVX2 coverage mask: ", cases, " cases passed"
for color in [rgbx(255, 255, 255, 255), rgbx(13, 97, 151, 201),
rgbx(1, 0, 1, 1), rgbx(0, 0, 0, 0)]:
for length in 0 .. 160:
for shift in 0 .. 7:
for pattern in 0 .. 3:
var pixels = newSeq[ColorRGBX](length + 96)
for pixel in pixels.mitems:
pixel = original

# Exercise every pixel alignment, with sentinels on both sides.
var start: int
while (cast[uint](pixels[start].addr) and 31) != 0:
inc start
start += 16 + shift

# Vary coverage alignment independently of pixel alignment.
# Padding keeps an overread safe while excess stores still
# corrupt the sentinels, regardless of the allocator's layout.
let coverageStart = (length + shift) mod 32
var coverages = newSeq[uint8](length + 64)
for coverage in coverages.mitems:
coverage = 93
for i in 0 ..< length:
coverages[coverageStart + i] = case pattern
of 0: 0'u8
of 1: 255'u8
of 2: 128'u8
else: ((i * 73 + 19) mod 256).uint8

blender(
cast[ptr UncheckedArray[ColorRGBX]](pixels[start].addr),
cast[ptr UncheckedArray[uint8]](coverages[coverageStart].addr),
color,
length
)

for i, pixel in pixels:
let expected =
if i >= start and i < start + length:
coveragePixel(
original, color, coverages[coverageStart + i - start], mode
)
else:
original
doAssert pixel == expected,
name & " length=" & $length & " shift=" & $shift &
" pattern=" & $pattern & " color=" & $color &
" pixel=" & $(i - start)
inc cases
echo name, " coverage spans: ", cases, " cases passed"

template testCoverageBlender(name, blender, mode: untyped) =
testCoverage(name, blender, mode)
testCoverageSpans(name, blender, mode)

testCoverageBlender("SSE2 normal", blendLineCoverageNormalSse2, NormalBlend)
testCoverageBlender("SSE2 overwrite", blendLineCoverageOverwriteSse2, OverwriteBlend)
testCoverageBlender("SSE2 mask", blendLineCoverageMaskSse2, MaskBlend)
when not defined(pixieNoAvx):
if cpuHasAvx2:
testCoverageBlender("AVX2 normal", blendLineCoverageNormalAvx2, NormalBlend)
testCoverageBlender("AVX2 overwrite", blendLineCoverageOverwriteAvx2, OverwriteBlend)
testCoverageBlender("AVX2 mask", blendLineCoverageMaskAvx2, MaskBlend)
else:
echo "AVX2 coverage tests skipped: CPU does not support AVX2"
1 change: 1 addition & 0 deletions tests/tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import
test_kerning,
test_paints,
test_paths,
test_paths_mask,
test_paths_overwrite,
test_png,
test_ppm,
Expand Down
Loading