From ff72022e860ab3c16520e4386bbd2a0fc577f133 Mon Sep 17 00:00:00 2001 From: treeform Date: Sat, 23 Jul 2022 15:29:50 -0700 Subject: [PATCH 1/5] de flake mix floats test --- tests/bench_images.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/bench_images.nim b/tests/bench_images.nim index 0185110..b99c3eb 100644 --- a/tests/bench_images.nim +++ b/tests/bench_images.nim @@ -134,9 +134,9 @@ timeIt "mix integers": doAssert z > 0 timeIt "mix floats": - for i in 0 ..< 100000: - let c = image[0, 0] + for i in 0 ..< 1000: + let c = image[0, 0].color var z: int for t in 0 .. 100: - z += mix(c.color, c.color, t.float32 / 100).rgba().a.int + z += mix(c, c, t.float32 / 100).rgba().a.int doAssert z > 0 From 3add6bb3f02dd1a27c6e315e6a6c3e21fd015671 Mon Sep 17 00:00:00 2001 From: treeform Date: Sat, 23 Jul 2022 15:35:16 -0700 Subject: [PATCH 2/5] deflake bench_blends.nim --- tests/bench_blends.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/bench_blends.nim b/tests/bench_blends.nim index d149f20..59befb7 100644 --- a/tests/bench_blends.nim +++ b/tests/bench_blends.nim @@ -1,8 +1,8 @@ import benchy, chroma, pixie/blends, pixie/images, vmath let - backdrop = newImage(512, 512) - source = newImage(512, 512) + backdrop = newImage(256, 256) + source = newImage(256, 256) source.fill(rgba(100, 100, 100, 100)) template reset() = From e48a191174128430252c3a8f1b435367a28a2929 Mon Sep 17 00:00:00 2001 From: treeform Date: Sun, 24 Jul 2022 15:58:45 -0700 Subject: [PATCH 3/5] Blit strategy --- src/pixie/images.nim | 75 ++++++++++++++++++++++++++++-- src/pixie/simd/sse2.nim | 45 ++++++++++++++++++ tests/test_images_draw_correct.nim | 37 +++++++++++++++ 3 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 tests/test_images_draw_correct.nim diff --git a/src/pixie/images.nim b/src/pixie/images.nim index 8e2cfd8..43c9508 100644 --- a/src/pixie/images.nim +++ b/src/pixie/images.nim @@ -454,15 +454,63 @@ proc getRgbaSmooth*( else: topMix +proc blitLine(a, b: ptr UncheckedArray[ColorRGBX], len: int, blender: Blender) = + for i in 0 ..< len: + a[i] = blender(a[i], b[i]) + +proc blitLineNormal(a, b: ptr UncheckedArray[ColorRGBX], len: int) {.hasSimd.} = + for i in 0 ..< len: + a[i] = blendNormal(a[i], b[i]) + +proc blitLineOverwrite(a, b: ptr UncheckedArray[ColorRGBX], len: int) = + copyMem(a[0].addr, b[0].addr, len * 4) + +template getUncheckedArray(a: Image, x, y: int): ptr UncheckedArray[ColorRGBX] = + cast[ptr UncheckedArray[ColorRGBX]](a.data[a.dataIndex(x, y)].addr) + +proc blitRect( + a, b: Image, pos = ivec2(0, 0), blendMode = NormalBlend +) = + ## Blits one image onto another using integer position with color blending. + let + px = pos.x.int + py = pos.y.int + xStart = max(-px, 0) + yStart = max(-py, 0) + xEnd = min(b.width, a.width - px) + yEnd = min(b.height, a.height - py) + + case blendMode: + of NormalBlend: + for y in yStart ..< yEnd: + blitLineNormal( + a.getUncheckedArray(xStart + px, y + py), + b.getUncheckedArray(xStart, y), + xEnd - xStart + ) + of OverwriteBlend: + {.linearScanEnd.} + for y in yStart ..< yEnd: + blitLineOverwrite( + a.getUncheckedArray(xStart + px, y + py), + b.getUncheckedArray(xStart, y), + xEnd - xStart + ) + else: + let blender = blendMode.blender() + for y in yStart ..< yEnd: + blitLine( + a.getUncheckedArray(xStart + px, y + py), + b.getUncheckedArray(xStart, y), + xEnd - xStart, + blender + ) + proc drawCorrect( a, b: Image | Mask, transform = mat3(), blendMode = NormalBlend, tiled = false ) {.raises: [PixieError].} = ## Draws one image onto another using matrix with color blending. - when type(a) is Image: - let blender = blendMode.blender() - else: # a is a Mask - let masker = blendMode.masker() var inverseTransform = transform.inverse() @@ -489,6 +537,25 @@ proc drawCorrect( filterBy2 *= 2 inverseTransform = scale(vec2(2, 2)) * inverseTransform + let + hasRotationOrScaling = not(dx == vec2(1, 0) and dy == vec2(0, 1)) + smooth = not( + dx.length == 1.0 and + dy.length == 1.0 and + transform[2, 0].fractional == 0.0 and + transform[2, 1].fractional == 0.0 + ) + + when type(a) is Image and type(b) is Image: + if not hasRotationOrScaling and not smooth and not tiled: + blitRect(a, b, ivec2(transform[2, 0].int32, transform[2, 1].int32), blendMode) + return + + when type(a) is Image: + let blender = blendMode.blender() + else: # a is a Mask + let masker = blendMode.masker() + for y in 0 ..< a.height: for x in 0 ..< a.width: let diff --git a/src/pixie/simd/sse2.nim b/src/pixie/simd/sse2.nim index 0f9c347..5724cfd 100644 --- a/src/pixie/simd/sse2.nim +++ b/src/pixie/simd/sse2.nim @@ -417,5 +417,50 @@ proc applyOpacitySse2*(mask: Mask, opacity: float32) {.simd.} = for i in i ..< mask.data.len: mask.data[i] = ((mask.data[i] * opacity) div 255).uint8 +proc blitLineNormalSse2*(a, b: ptr UncheckedArray[ColorRGBX], len: int) {.simd.} = + + # TODO align to 16 + + var i = 0 + while i < len - 4: + + let + source = mm_loadu_si128(b[i].addr) + backdrop = mm_loadu_si128(a[i].addr) + alphaMask = mm_set1_epi32(cast[int32](0xff000000)) + oddMask = mm_set1_epi16(cast[int16](0xff00)) + div255 = mm_set1_epi16(cast[int16](0x8081)) + + var + sourceAlpha = mm_and_si128(source, alphaMask) + backdropEven = mm_slli_epi16(backdrop, 8) + backdropOdd = mm_and_si128(backdrop, oddMask) + + sourceAlpha = mm_or_si128(sourceAlpha, mm_srli_epi32(sourceAlpha, 16)) + + let k = mm_sub_epi32( + mm_set1_epi32(cast[int32]([0.uint8, 255, 0, 255])), + sourceAlpha + ) + + backdropEven = mm_mulhi_epu16(backdropEven, k) + backdropOdd = mm_mulhi_epu16(backdropOdd, k) + + backdropEven = mm_srli_epi16(mm_mulhi_epu16(backdropEven, div255), 7) + backdropOdd = mm_srli_epi16(mm_mulhi_epu16(backdropOdd, div255), 7) + + let done = mm_add_epi8( + source, + mm_or_si128(backdropEven, mm_slli_epi16(backdropOdd, 8)) + ) + + mm_storeu_si128(a[i].addr, done) + + i += 4 + + # TODO last 1-3 pixels + # for i in i ..< len: + # a[i] = blendNormal(a[i], b[i]) + when defined(release): {.pop.} diff --git a/tests/test_images_draw_correct.nim b/tests/test_images_draw_correct.nim new file mode 100644 index 0000000..bed3c6f --- /dev/null +++ b/tests/test_images_draw_correct.nim @@ -0,0 +1,37 @@ +import pixie, strformat, xrays +import pixie/images {.all.} + +block: + let + a = newImage(1000, 1000) + b = newImage(500, 500) + a.fill(rgba(255, 0, 0, 255)) + b.fill(rgba(0, 255, 0, 255)) + + a.drawCorrect(b, translate(vec2(250, 250)), blendMode = OverwriteBlend) + a.writeFile("tests/images/rotate0.png") + +import benchy + +block: + let + a = newImage(1000, 1000) + b = newImage(500, 500) + + timeIt "drawCorrect": + a.fill(rgba(255, 0, 0, 255)) + b.fill(rgba(0, 255, 0, 255)) + + a.drawCorrect(b, translate(vec2(250, 250)), blendMode = OverwriteBlend) + + +block: + let + a = newImage(1000, 1000) + b = newImage(500, 500) + + timeIt "draw": + a.fill(rgba(255, 0, 0, 255)) + b.fill(rgba(0, 255, 0, 255)) + + a.draw(b, translate(vec2(250, 250)), blendMode = OverwriteBlend) From b1e1e0c0952923147d1d2dc4295e442e9df24da9 Mon Sep 17 00:00:00 2001 From: treeform Date: Sun, 24 Jul 2022 16:01:58 -0700 Subject: [PATCH 4/5] rename test_iamges_draw_correct --- src/pixie/images.nim | 1 - ...{test_images_draw_correct.nim => tmp_images_draw_correct.nim} | 0 2 files changed, 1 deletion(-) rename tests/{test_images_draw_correct.nim => tmp_images_draw_correct.nim} (100%) diff --git a/src/pixie/images.nim b/src/pixie/images.nim index 43c9508..7e83979 100644 --- a/src/pixie/images.nim +++ b/src/pixie/images.nim @@ -511,7 +511,6 @@ proc drawCorrect( ) {.raises: [PixieError].} = ## Draws one image onto another using matrix with color blending. - var inverseTransform = transform.inverse() # Compute movement vectors diff --git a/tests/test_images_draw_correct.nim b/tests/tmp_images_draw_correct.nim similarity index 100% rename from tests/test_images_draw_correct.nim rename to tests/tmp_images_draw_correct.nim From 19acda9112a378a88868d7af05d4e1354ffbad67 Mon Sep 17 00:00:00 2001 From: treeform Date: Sun, 24 Jul 2022 16:26:10 -0700 Subject: [PATCH 5/5] more rename --- ...{tmp_images_draw_correct.nim => bench_images_draw_correct.nim} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{tmp_images_draw_correct.nim => bench_images_draw_correct.nim} (100%) diff --git a/tests/tmp_images_draw_correct.nim b/tests/bench_images_draw_correct.nim similarity index 100% rename from tests/tmp_images_draw_correct.nim rename to tests/bench_images_draw_correct.nim