diff --git a/internal/atlas/image.go b/internal/atlas/image.go index 6f43db3e9..736dc2671 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -17,6 +17,7 @@ package atlas import ( "fmt" "image" + "math" "runtime" "sync" @@ -446,8 +447,8 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []f swf, shf := float32(sw), float32(sh) n := len(vertices) for i := 0; i < n; i += graphics.VertexFloatNum { - vertices[i] += dx - vertices[i+1] += dy + vertices[i] = adjustDestinationPixel(vertices[i] + dx) + vertices[i+1] = adjustDestinationPixel(vertices[i+1] + dy) vertices[i+2] = (vertices[i+2] + oxf) / swf vertices[i+3] = (vertices[i+3] + oyf) / shf } @@ -460,8 +461,8 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []f } else { n := len(vertices) for i := 0; i < n; i += graphics.VertexFloatNum { - vertices[i] += dx - vertices[i+1] += dy + vertices[i] = adjustDestinationPixel(vertices[i] + dx) + vertices[i+1] = adjustDestinationPixel(vertices[i+1] + dy) } } @@ -847,3 +848,20 @@ func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) error { defer backendsM.Unlock() return restorable.DumpImages(graphicsDriver, dir) } + +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. + ix := float32(math.Floor(float64(x))) + frac := x - ix + switch { + case frac < 3.0/16.0: + return ix + case frac < 8.0/16.0: + return ix + 5.0/16.0 + case frac < 13.0/16.0: + return ix + 11.0/16.0 + default: + return ix + 16.0/16.0 + } +} diff --git a/internal/graphicscommand/command.go b/internal/graphicscommand/command.go index 14a1ba1af..b84086074 100644 --- a/internal/graphicscommand/command.go +++ b/internal/graphicscommand/command.go @@ -208,42 +208,6 @@ func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics) error { vs := q.vertices debug.Logf("Graphics commands:\n") - if graphicsDriver.HasHighPrecisionFloat() { - n := q.nvertices / graphics.VertexFloatNum - for i := 0; i < n; i++ { - idx := i * graphics.VertexFloatNum - - // Avoid the center of the pixel, which is problematic (#929, #1171). - // Instead, align the vertices with about 1/3 pixels. - x := vs[idx] - y := vs[idx+1] - ix := float32(math.Floor(float64(x))) - iy := float32(math.Floor(float64(y))) - fracx := x - ix - fracy := y - iy - switch { - case fracx < 3.0/16.0: - vs[idx] = ix - case fracx < 8.0/16.0: - vs[idx] = ix + 5.0/16.0 - case fracx < 13.0/16.0: - vs[idx] = ix + 11.0/16.0 - default: - vs[idx] = ix + 16.0/16.0 - } - switch { - case fracy < 3.0/16.0: - vs[idx+1] = iy - case fracy < 8.0/16.0: - vs[idx+1] = iy + 5.0/16.0 - case fracy < 13.0/16.0: - vs[idx+1] = iy + 11.0/16.0 - default: - vs[idx+1] = iy + 16.0/16.0 - } - } - } - if err := graphicsDriver.Begin(); err != nil { return err }