mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 02:42:02 +01:00
internal/atlas: do not adjust pixels for DrawTriangles(Shader)
Adjusting pixels is needed to avoid strainge rendering to avoid unexpected rendering (#1171). However, this adjustment caused unexpected holes especially in a thick stroke. This change moves the logic of adjusting pixels from atlas to graphics.QuadVertices so that adjusting works only for DrawImage and DrawRectShader. Updates #1171 Updates #1843
This commit is contained in:
parent
1ff55bc745
commit
99e777b0c5
@ -60,4 +60,3 @@ func (i *Image) EnsureIsolatedForTesting() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ResolveDeferredForTesting = resolveDeferred
|
var ResolveDeferredForTesting = resolveDeferred
|
||||||
var AdjustDestinationPixelForTesting = adjustDestinationPixel
|
|
||||||
|
@ -434,8 +434,8 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
|
|||||||
swf, shf := float32(sw), float32(sh)
|
swf, shf := float32(sw), float32(sh)
|
||||||
n := len(vertices)
|
n := len(vertices)
|
||||||
for i := 0; i < n; i += graphics.VertexFloatCount {
|
for i := 0; i < n; i += graphics.VertexFloatCount {
|
||||||
vertices[i] = adjustDestinationPixel(vertices[i] + dx)
|
vertices[i] = vertices[i] + dx
|
||||||
vertices[i+1] = adjustDestinationPixel(vertices[i+1] + dy)
|
vertices[i+1] = vertices[i+1] + dy
|
||||||
vertices[i+2] = (vertices[i+2] + oxf) / swf
|
vertices[i+2] = (vertices[i+2] + oxf) / swf
|
||||||
vertices[i+3] = (vertices[i+3] + oyf) / shf
|
vertices[i+3] = (vertices[i+3] + oyf) / shf
|
||||||
}
|
}
|
||||||
@ -448,8 +448,8 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
|
|||||||
} else {
|
} else {
|
||||||
n := len(vertices)
|
n := len(vertices)
|
||||||
for i := 0; i < n; i += graphics.VertexFloatCount {
|
for i := 0; i < n; i += graphics.VertexFloatCount {
|
||||||
vertices[i] = adjustDestinationPixel(vertices[i] + dx)
|
vertices[i] = vertices[i] + dx
|
||||||
vertices[i+1] = adjustDestinationPixel(vertices[i+1] + dy)
|
vertices[i+1] = vertices[i+1] + dy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -786,29 +786,3 @@ func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, err
|
|||||||
defer backendsM.Unlock()
|
defer backendsM.Unlock()
|
||||||
return restorable.DumpImages(graphicsDriver, dir)
|
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.
|
|
||||||
//
|
|
||||||
// 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(int(x))
|
|
||||||
if x < 0 && x != ix {
|
|
||||||
ix -= 1
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -736,44 +736,4 @@ func TestImageWritePixelsModify(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAdjustPixel(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
X float32
|
|
||||||
Y float32
|
|
||||||
Delta float32
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
X: -0.1,
|
|
||||||
Y: 0.9,
|
|
||||||
Delta: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
X: -1,
|
|
||||||
Y: 0,
|
|
||||||
Delta: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
X: -1.9,
|
|
||||||
Y: 1.1,
|
|
||||||
Delta: 3,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
X: -2,
|
|
||||||
Y: 1,
|
|
||||||
Delta: 3,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tc := range tests {
|
|
||||||
if rx, ry := atlas.AdjustDestinationPixelForTesting(tc.X)+tc.Delta, atlas.AdjustDestinationPixelForTesting(tc.Y); rx != ry {
|
|
||||||
t.Errorf("adjustDestinationPixel(%f) + 1 must equal to adjustDestinationPixel(%f) but not (%f vs %f)", tc.X, tc.Y, rx, ry)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
// TODO: Add tests to extend image on an atlas out of the main loop
|
||||||
|
17
internal/graphics/export_test.go
Normal file
17
internal/graphics/export_test.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// Copyright 2022 The Ebitengine Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package graphics
|
||||||
|
|
||||||
|
var AdjustDestinationPixelForTesting = adjustDestinationPixel
|
@ -39,3 +39,43 @@ func TestInternalImageSize(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAdjustPixel(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
X float32
|
||||||
|
Y float32
|
||||||
|
Delta float32
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
X: -0.1,
|
||||||
|
Y: 0.9,
|
||||||
|
Delta: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
X: -1,
|
||||||
|
Y: 0,
|
||||||
|
Delta: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
X: -1.9,
|
||||||
|
Y: 1.1,
|
||||||
|
Delta: 3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
X: -2,
|
||||||
|
Y: 1,
|
||||||
|
Delta: 3,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range tests {
|
||||||
|
if rx, ry := graphics.AdjustDestinationPixelForTesting(tc.X)+tc.Delta, graphics.AdjustDestinationPixelForTesting(tc.Y); rx != ry {
|
||||||
|
t.Errorf("adjustDestinationPixel(%f) + 1 must equal to adjustDestinationPixel(%f) but not (%f vs %f)", tc.X, tc.Y, rx, ry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkAdjustPixel(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
graphics.AdjustDestinationPixelForTesting(float32(i) / 17)
|
||||||
|
}
|
||||||
|
}
|
@ -150,8 +150,8 @@ func QuadVertices(sx0, sy0, sx1, sy1 float32, a, b, c, d, tx, ty float32, cr, cg
|
|||||||
// This function is very performance-sensitive and implement in a very dumb way.
|
// This function is very performance-sensitive and implement in a very dumb way.
|
||||||
_ = vs[:4*VertexFloatCount]
|
_ = vs[:4*VertexFloatCount]
|
||||||
|
|
||||||
vs[0] = tx
|
vs[0] = adjustDestinationPixel(tx)
|
||||||
vs[1] = ty
|
vs[1] = adjustDestinationPixel(ty)
|
||||||
vs[2] = u0
|
vs[2] = u0
|
||||||
vs[3] = v0
|
vs[3] = v0
|
||||||
vs[4] = cr
|
vs[4] = cr
|
||||||
@ -159,8 +159,8 @@ func QuadVertices(sx0, sy0, sx1, sy1 float32, a, b, c, d, tx, ty float32, cr, cg
|
|||||||
vs[6] = cb
|
vs[6] = cb
|
||||||
vs[7] = ca
|
vs[7] = ca
|
||||||
|
|
||||||
vs[8] = ax + tx
|
vs[8] = adjustDestinationPixel(ax + tx)
|
||||||
vs[9] = cx + ty
|
vs[9] = adjustDestinationPixel(cx + ty)
|
||||||
vs[10] = u1
|
vs[10] = u1
|
||||||
vs[11] = v0
|
vs[11] = v0
|
||||||
vs[12] = cr
|
vs[12] = cr
|
||||||
@ -168,8 +168,8 @@ func QuadVertices(sx0, sy0, sx1, sy1 float32, a, b, c, d, tx, ty float32, cr, cg
|
|||||||
vs[14] = cb
|
vs[14] = cb
|
||||||
vs[15] = ca
|
vs[15] = ca
|
||||||
|
|
||||||
vs[16] = by + tx
|
vs[16] = adjustDestinationPixel(by + tx)
|
||||||
vs[17] = dy + ty
|
vs[17] = adjustDestinationPixel(dy + ty)
|
||||||
vs[18] = u0
|
vs[18] = u0
|
||||||
vs[19] = v1
|
vs[19] = v1
|
||||||
vs[20] = cr
|
vs[20] = cr
|
||||||
@ -177,8 +177,8 @@ func QuadVertices(sx0, sy0, sx1, sy1 float32, a, b, c, d, tx, ty float32, cr, cg
|
|||||||
vs[22] = cb
|
vs[22] = cb
|
||||||
vs[23] = ca
|
vs[23] = ca
|
||||||
|
|
||||||
vs[24] = ax + by + tx
|
vs[24] = adjustDestinationPixel(ax + by + tx)
|
||||||
vs[25] = cx + dy + ty
|
vs[25] = adjustDestinationPixel(cx + dy + ty)
|
||||||
vs[26] = u1
|
vs[26] = u1
|
||||||
vs[27] = v1
|
vs[27] = v1
|
||||||
vs[28] = cr
|
vs[28] = cr
|
||||||
@ -188,3 +188,29 @@ func QuadVertices(sx0, sy0, sx1, sy1 float32, a, b, c, d, tx, ty float32, cr, cg
|
|||||||
|
|
||||||
return vs
|
return vs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(int(x))
|
||||||
|
if x < 0 && x != ix {
|
||||||
|
ix -= 1
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user