From 9319266c01da4e987fab773a20ce1a5d7f1b08a0 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 15 Sep 2022 01:25:26 +0900 Subject: [PATCH] internal/atlas: add a benchmark for adjustDestinationPixel --- internal/atlas/export_test.go | 5 ++--- internal/atlas/image.go | 6 ++++++ internal/atlas/image_test.go | 6 ++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/internal/atlas/export_test.go b/internal/atlas/export_test.go index 92db960f7..807d093ba 100644 --- a/internal/atlas/export_test.go +++ b/internal/atlas/export_test.go @@ -59,6 +59,5 @@ func (i *Image) EnsureIsolatedForTesting() { i.ensureIsolated() } -func ResolveDeferredForTesting() { - resolveDeferred() -} +var ResolveDeferredForTesting = resolveDeferred +var AdjustDestinationPixelForTesting = adjustDestinationPixel diff --git a/internal/atlas/image.go b/internal/atlas/image.go index aaefdb643..bbb314abc 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -799,6 +799,12 @@ func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, err func adjustDestinationPixel(x float32) float32 { // Avoid the center of the pixel, which is problematic (#929, #1171). // Instead, align the vertices with about 1/3 pixels. + // + // The intention here is roughly this code: + // + // float32(math.Floor((float64(x)+1.0/6.0)*3) / 3) + // + // The actual implementation is more optimized than the above implementation. ix := float32(math.Floor(float64(x))) frac := x - ix switch { diff --git a/internal/atlas/image_test.go b/internal/atlas/image_test.go index 4eacbd885..24335a98b 100644 --- a/internal/atlas/image_test.go +++ b/internal/atlas/image_test.go @@ -737,4 +737,10 @@ func TestImageWritePixelsModify(t *testing.T) { } } +func BenchmarkAdjustPixel(b *testing.B) { + for i := 0; i < b.N; i++ { + atlas.AdjustDestinationPixelForTesting(float32(i) / 17) + } +} + // TODO: Add tests to extend image on an atlas out of the main loop