2021-08-14 13:05:26 +02:00
|
|
|
// Copyright 2021 The Ebiten 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 atlas_test
|
|
|
|
|
|
|
|
import (
|
2023-04-27 17:55:10 +02:00
|
|
|
"image"
|
2021-08-14 13:05:26 +02:00
|
|
|
"image/color"
|
2024-01-29 15:14:12 +01:00
|
|
|
"runtime"
|
2021-08-14 13:05:26 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
2021-08-14 13:05:26 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2021-08-14 13:05:26 +02:00
|
|
|
etesting "github.com/hajimehoshi/ebiten/v2/internal/testing"
|
2022-03-19 15:55:14 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/ui"
|
2021-08-14 13:05:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShaderFillTwice(t *testing.T) {
|
|
|
|
const w, h = 1, 1
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
dst := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2021-08-14 13:05:26 +02:00
|
|
|
|
|
|
|
vs := quadVertices(w, h, 0, 0, 1)
|
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
2023-10-14 17:06:07 +02:00
|
|
|
g := ui.Get().GraphicsDriverForTesting()
|
2022-03-21 09:48:47 +01:00
|
|
|
s0 := atlas.NewShader(etesting.ShaderProgramFill(0xff, 0xff, 0xff, 0xff))
|
2023-11-04 10:09:47 +01:00
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s0, nil, graphicsdriver.FillAll)
|
2021-08-14 13:05:26 +02:00
|
|
|
|
|
|
|
// Vertices must be recreated (#1755)
|
|
|
|
vs = quadVertices(w, h, 0, 0, 1)
|
2022-03-21 09:48:47 +01:00
|
|
|
s1 := atlas.NewShader(etesting.ShaderProgramFill(0x80, 0x80, 0x80, 0xff))
|
2023-11-04 10:09:47 +01:00
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s1, nil, graphicsdriver.FillAll)
|
2021-08-14 13:05:26 +02:00
|
|
|
|
2022-08-01 17:28:16 +02:00
|
|
|
pix := make([]byte, 4*w*h)
|
2024-03-26 04:59:31 +01:00
|
|
|
ok, err := dst.ReadPixels(g, pix, image.Rect(0, 0, w, h))
|
|
|
|
if err != nil {
|
2021-08-14 13:05:26 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2024-03-26 04:59:31 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("ReadPixels failed")
|
|
|
|
}
|
2023-01-28 11:06:38 +01:00
|
|
|
if got, want := (color.RGBA{R: pix[0], G: pix[1], B: pix[2], A: pix[3]}), (color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0xff}); got != want {
|
2021-08-14 13:05:26 +02:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageDrawTwice(t *testing.T) {
|
|
|
|
const w, h = 1, 1
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
dst := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
|
|
|
src0 := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-04-27 17:55:10 +02:00
|
|
|
src0.WritePixels([]byte{0xff, 0xff, 0xff, 0xff}, image.Rect(0, 0, w, h))
|
2022-06-07 16:57:56 +02:00
|
|
|
src1 := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-04-27 17:55:10 +02:00
|
|
|
src1.WritePixels([]byte{0x80, 0x80, 0x80, 0xff}, image.Rect(0, 0, w, h))
|
2021-08-14 13:05:26 +02:00
|
|
|
|
|
|
|
vs := quadVertices(w, h, 0, 0, 1)
|
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
2023-11-04 10:09:47 +01:00
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src0}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2021-08-14 13:05:26 +02:00
|
|
|
|
|
|
|
// Vertices must be recreated (#1755)
|
|
|
|
vs = quadVertices(w, h, 0, 0, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2021-08-14 13:05:26 +02:00
|
|
|
|
2022-08-01 17:28:16 +02:00
|
|
|
pix := make([]byte, 4*w*h)
|
2024-03-26 04:59:31 +01:00
|
|
|
ok, err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, w, h))
|
|
|
|
if err != nil {
|
2021-08-14 13:05:26 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2024-03-26 04:59:31 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("ReadPixels failed")
|
|
|
|
}
|
2023-01-28 11:06:38 +01:00
|
|
|
if got, want := (color.RGBA{R: pix[0], G: pix[1], B: pix[2], A: pix[3]}), (color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0xff}); got != want {
|
2021-08-14 13:05:26 +02:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2024-01-29 15:14:12 +01:00
|
|
|
|
|
|
|
func TestGCShader(t *testing.T) {
|
|
|
|
s := atlas.NewShader(etesting.ShaderProgramFill(0xff, 0xff, 0xff, 0xff))
|
|
|
|
|
|
|
|
// Use the shader to initialize it.
|
|
|
|
const w, h = 1, 1
|
|
|
|
dst := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
|
|
|
vs := quadVertices(w, h, 0, 0, 1)
|
|
|
|
is := graphics.QuadIndices()
|
|
|
|
dr := image.Rect(0, 0, w, h)
|
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s, nil, graphicsdriver.FillAll)
|
|
|
|
|
|
|
|
// Ensure other objects are GCed, as GC appends deferred functions for collected objects.
|
2024-02-24 14:41:37 +01:00
|
|
|
ensureGC()
|
2024-01-29 15:14:12 +01:00
|
|
|
|
|
|
|
// Get the difference of the number of deferred functions before and after s is GCed.
|
|
|
|
c := atlas.DeferredFuncCountForTesting()
|
|
|
|
runtime.KeepAlive(s)
|
2024-02-24 14:41:37 +01:00
|
|
|
ensureGC()
|
2024-02-14 19:00:49 +01:00
|
|
|
|
|
|
|
diff := atlas.DeferredFuncCountForTesting() - c
|
2024-01-29 15:14:12 +01:00
|
|
|
if got, want := diff, 1; got != want {
|
|
|
|
t.Errorf("got: %d, want: %d", got, want)
|
|
|
|
}
|
|
|
|
}
|