internal/testing: bug fix: considering the case when Y is inverted

Closes #2003
This commit is contained in:
Hajime Hoshi 2022-02-27 20:11:27 +09:00
parent 4ca1220671
commit 77f765d483
8 changed files with 51 additions and 29 deletions

View File

@ -21,6 +21,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/affine"
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
etesting "github.com/hajimehoshi/ebiten/v2/internal/testing"
)
@ -38,13 +39,13 @@ func TestShaderFillTwice(t *testing.T) {
Width: w,
Height: h,
}
p0 := etesting.ShaderProgramFill(0xff, 0xff, 0xff, 0xff)
p0 := etesting.ShaderProgramFill(graphicscommand.NeedsInvertY(), 0xff, 0xff, 0xff, 0xff)
s0 := atlas.NewShader(&p0)
dst.DrawTriangles([graphics.ShaderImageNum]*atlas.Image{}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageNum - 1][2]float32{}, s0, nil, false)
// Vertices must be recreated (#1755)
vs = quadVertices(w, h, 0, 0, 1)
p1 := etesting.ShaderProgramFill(0x80, 0x80, 0x80, 0xff)
p1 := etesting.ShaderProgramFill(graphicscommand.NeedsInvertY(), 0x80, 0x80, 0x80, 0xff)
s1 := atlas.NewShader(&p1)
dst.DrawTriangles([graphics.ShaderImageNum]*atlas.Image{}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageNum - 1][2]float32{}, s1, nil, false)

View File

@ -32,6 +32,10 @@ func SetGraphicsDriver(driver graphicsdriver.Graphics) {
theGraphicsDriver = driver
}
func NeedsInvertY() bool {
return theGraphicsDriver.FramebufferYDirection() != theGraphicsDriver.NDCYDirection()
}
func NeedsRestoring() bool {
return theGraphicsDriver.NeedsRestoring()
}

View File

@ -103,7 +103,7 @@ func TestShader(t *testing.T) {
}
dst.DrawTriangles([graphics.ShaderImageNum]*graphicscommand.Image{clr}, [graphics.ShaderImageNum - 1][2]float32{}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeClear, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, nil, nil, false)
ir := etesting.ShaderProgramFill(0xff, 0, 0, 0xff)
ir := etesting.ShaderProgramFill(graphicscommand.NeedsInvertY(), 0xff, 0, 0, 0xff)
s := graphicscommand.NewShader(&ir)
dst.DrawTriangles([graphics.ShaderImageNum]*graphicscommand.Image{}, [graphics.ShaderImageNum - 1][2]float32{}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeSourceOver, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, s, nil, false)

View File

@ -23,12 +23,12 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
t "github.com/hajimehoshi/ebiten/v2/internal/testing"
etesting "github.com/hajimehoshi/ebiten/v2/internal/testing"
)
func TestMain(m *testing.M) {
restorable.EnableRestoringForTesting()
t.MainWithRunLoop(m)
etesting.MainWithRunLoop(m)
}
func pixelsToColor(p *restorable.Pixels, i, j int) color.RGBA {

View File

@ -20,6 +20,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/affine"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
etesting "github.com/hajimehoshi/ebiten/v2/internal/testing"
@ -57,7 +58,7 @@ func TestShader(t *testing.T) {
img := restorable.NewImage(1, 1)
defer img.Dispose()
ir := etesting.ShaderProgramFill(0xff, 0, 0, 0xff)
ir := etesting.ShaderProgramFill(graphicscommand.NeedsInvertY(), 0xff, 0, 0, 0xff)
s := restorable.NewShader(&ir)
dr := graphicsdriver.Region{
X: 0,
@ -92,7 +93,7 @@ func TestShaderChain(t *testing.T) {
imgs[0].ReplacePixels([]byte{0xff, 0, 0, 0xff}, 0, 0, 1, 1)
ir := etesting.ShaderProgramImages(1)
ir := etesting.ShaderProgramImages(graphicscommand.NeedsInvertY(), 1)
s := restorable.NewShader(&ir)
for i := 0; i < num-1; i++ {
dr := graphicsdriver.Region{
@ -131,7 +132,7 @@ func TestShaderMultipleSources(t *testing.T) {
dst := restorable.NewImage(1, 1)
ir := etesting.ShaderProgramImages(3)
ir := etesting.ShaderProgramImages(graphicscommand.NeedsInvertY(), 3)
s := restorable.NewShader(&ir)
var offsets [graphics.ShaderImageNum - 1][2]float32
dr := graphicsdriver.Region{
@ -170,7 +171,7 @@ func TestShaderMultipleSourcesOnOneTexture(t *testing.T) {
dst := restorable.NewImage(1, 1)
ir := etesting.ShaderProgramImages(3)
ir := etesting.ShaderProgramImages(graphicscommand.NeedsInvertY(), 3)
s := restorable.NewShader(&ir)
offsets := [graphics.ShaderImageNum - 1][2]float32{
{1, 0},
@ -205,7 +206,7 @@ func TestShaderDispose(t *testing.T) {
img := restorable.NewImage(1, 1)
defer img.Dispose()
ir := etesting.ShaderProgramFill(0xff, 0, 0, 0xff)
ir := etesting.ShaderProgramFill(graphicscommand.NeedsInvertY(), 0xff, 0, 0, 0xff)
s := restorable.NewShader(&ir)
dr := graphicsdriver.Region{
X: 0,

View File

@ -21,8 +21,17 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
var (
projectionMatrix = shaderir.Expr{
func projectionMatrix(invertY bool) shaderir.Expr {
m11s := 1
if invertY {
m11s = -1
}
m31 := -1
if invertY {
m31 = 1
}
return shaderir.Expr{
Type: shaderir.Call,
Exprs: []shaderir.Expr{
{
@ -79,7 +88,7 @@ var (
Exprs: []shaderir.Expr{
{
Type: shaderir.NumberExpr,
Const: constant.MakeFloat64(2),
Const: constant.MakeFloat64(float64(m11s) * 2),
ConstType: shaderir.ConstTypeFloat,
},
{
@ -134,7 +143,7 @@ var (
},
{
Type: shaderir.NumberExpr,
Const: constant.MakeFloat64(-1),
Const: constant.MakeFloat64(float64(m31)),
ConstType: shaderir.ConstTypeFloat,
},
{
@ -149,7 +158,10 @@ var (
},
},
}
vertexPosition = shaderir.Expr{
}
func vertexPosition() shaderir.Expr {
return shaderir.Expr{
Type: shaderir.Call,
Exprs: []shaderir.Expr{
{
@ -172,7 +184,10 @@ var (
},
},
}
defaultVertexFunc = shaderir.VertexFunc{
}
func defaultVertexFunc(invertY bool) shaderir.VertexFunc {
return shaderir.VertexFunc{
Block: &shaderir.Block{
LocalVarIndexOffset: 4 + 1,
Stmts: []shaderir.Stmt{
@ -200,8 +215,8 @@ var (
Type: shaderir.Binary,
Op: shaderir.Mul,
Exprs: []shaderir.Expr{
projectionMatrix,
vertexPosition,
projectionMatrix(invertY),
vertexPosition(),
},
},
},
@ -209,9 +224,9 @@ var (
},
},
}
)
}
func defaultProgram() shaderir.Program {
func defaultProgram(invertY bool) shaderir.Program {
p := shaderir.Program{
Attributes: []shaderir.Type{
{Main: shaderir.Vec2}, // Local var (0) in the vertex shader
@ -221,7 +236,7 @@ func defaultProgram() shaderir.Program {
Varyings: []shaderir.Type{
{Main: shaderir.Vec2}, // Local var (4) in the vertex shader, (1) in the fragment shader
},
VertexFunc: defaultVertexFunc,
VertexFunc: defaultVertexFunc(invertY),
}
p.Uniforms = make([]shaderir.Type, graphics.PreservedUniformVariablesNum)
@ -255,7 +270,7 @@ func defaultProgram() shaderir.Program {
// Uniform variable's index and its value are:
//
// 0: the framebuffer size (Vec2)
func ShaderProgramFill(r, g, b, a byte) shaderir.Program {
func ShaderProgramFill(invertY bool, r, g, b, a byte) shaderir.Program {
clr := shaderir.Expr{
Type: shaderir.Call,
Exprs: []shaderir.Expr{
@ -286,7 +301,7 @@ func ShaderProgramFill(r, g, b, a byte) shaderir.Program {
},
}
p := defaultProgram()
p := defaultProgram(invertY)
p.FragmentFunc = shaderir.FragmentFunc{
Block: &shaderir.Block{
LocalVarIndexOffset: 2 + 1,
@ -315,12 +330,12 @@ func ShaderProgramFill(r, g, b, a byte) shaderir.Program {
// 0: the framebuffer size (Vec2)
//
// The size and region values are actually not used in this shader so far.
func ShaderProgramImages(imageNum int) shaderir.Program {
func ShaderProgramImages(invertY bool, imageNum int) shaderir.Program {
if imageNum <= 0 {
panic("testing: imageNum must be >= 1")
}
p := defaultProgram()
p := defaultProgram(invertY)
p.TextureNum = imageNum
// In the fragment shader, local variables are:

View File

@ -19,5 +19,10 @@ import (
)
func init() {
// TODO: Should graphics() be moved to the graphicscommand package?
graphicscommand.SetGraphicsDriver(graphics())
}
func NeedsInvertY() bool {
return graphicscommand.NeedsInvertY()
}

View File

@ -67,7 +67,3 @@ const (
WindowResizingModeOnlyFullscreenEnabled
WindowResizingModeEnabled
)
func NeedsInvertY() bool {
return graphics().FramebufferYDirection() != graphics().NDCYDirection()
}