2018-04-04 19:35:30 +02:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
package atlas_test
|
2018-04-04 19:35:30 +02:00
|
|
|
|
|
|
|
import (
|
2023-04-27 17:55:10 +02:00
|
|
|
"image"
|
2018-04-04 19:35:30 +02:00
|
|
|
"image/color"
|
2018-07-21 22:52:11 +02:00
|
|
|
"runtime"
|
2018-04-04 19:35:30 +02:00
|
|
|
"testing"
|
2024-02-14 19:00:49 +01:00
|
|
|
"time"
|
2024-02-24 14:41:37 +01:00
|
|
|
"unsafe"
|
2018-04-04 19:35:30 +02:00
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2020-10-03 19:35:13 +02:00
|
|
|
t "github.com/hajimehoshi/ebiten/v2/internal/testing"
|
2022-03-19 15:55:14 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/ui"
|
2018-04-04 19:35:30 +02:00
|
|
|
)
|
|
|
|
|
2020-06-27 10:09:23 +02:00
|
|
|
const (
|
2023-02-23 09:21:17 +01:00
|
|
|
minSourceImageSizeForTesting = 1024
|
|
|
|
minDestinationImageSizeForTesting = 256
|
|
|
|
maxImageSizeForTesting = 4096
|
2020-06-27 10:09:23 +02:00
|
|
|
)
|
|
|
|
|
2018-04-04 19:35:30 +02:00
|
|
|
func TestMain(m *testing.M) {
|
2023-02-23 09:21:17 +01:00
|
|
|
atlas.SetImageSizeForTesting(minSourceImageSizeForTesting, minDestinationImageSizeForTesting, maxImageSizeForTesting)
|
2021-10-02 12:58:48 +02:00
|
|
|
defer atlas.ResetImageSizeForTesting()
|
2020-04-01 11:03:08 +02:00
|
|
|
t.MainWithRunLoop(m)
|
2018-04-04 19:35:30 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 19:35:18 +02:00
|
|
|
func quadVertices(sw, sh, x, y int, scalex float32) []float32 {
|
|
|
|
dx0 := float32(x)
|
|
|
|
dy0 := float32(y)
|
|
|
|
dx1 := float32(x) + float32(sw)*scalex
|
|
|
|
dy1 := float32(y) + float32(sh)
|
|
|
|
sx0 := float32(0)
|
|
|
|
sy0 := float32(0)
|
|
|
|
sx1 := float32(sw)
|
|
|
|
sy1 := float32(sh)
|
|
|
|
return []float32{
|
2020-07-02 16:06:58 +02:00
|
|
|
dx0, dy0, sx0, sy0, 1, 1, 1, 1,
|
|
|
|
dx1, dy0, sx1, sy0, 1, 1, 1, 1,
|
|
|
|
dx0, dy1, sx0, sy1, 1, 1, 1, 1,
|
|
|
|
dx1, dy1, sx1, sy1, 1, 1, 1, 1,
|
2019-09-20 19:35:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:12:08 +02:00
|
|
|
const bigSize = 2049
|
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
func TestEnsureIsolatedFromSourceBackend(t *testing.T) {
|
2018-04-04 19:35:30 +02:00
|
|
|
// Create img1 and img2 with this size so that the next images are allocated
|
|
|
|
// with non-upper-left location.
|
2022-06-07 16:57:56 +02:00
|
|
|
img1 := atlas.NewImage(bigSize, 100, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img1.Deallocate()
|
2018-04-29 12:15:04 +02:00
|
|
|
// Ensure img1's region is allocated.
|
2023-04-27 17:55:10 +02:00
|
|
|
img1.WritePixels(make([]byte, 4*bigSize*100), image.Rect(0, 0, bigSize, 100))
|
2018-04-04 19:35:30 +02:00
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
img2 := atlas.NewImage(100, bigSize, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img2.Deallocate()
|
2023-04-27 17:55:10 +02:00
|
|
|
img2.WritePixels(make([]byte, 4*100*bigSize), image.Rect(0, 0, 100, bigSize))
|
2018-04-04 19:35:30 +02:00
|
|
|
|
|
|
|
const size = 32
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
img3 := atlas.NewImage(size/2, size/2, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img3.Deallocate()
|
2023-04-27 17:55:10 +02:00
|
|
|
img3.WritePixels(make([]byte, (size/2)*(size/2)*4), image.Rect(0, 0, size/2, size/2))
|
2018-04-04 19:35:30 +02:00
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
img4 := atlas.NewImage(size, size, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img4.Deallocate()
|
2018-04-04 19:35:30 +02:00
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
img5 := atlas.NewImage(size/2, size/2, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img3.Deallocate()
|
2022-04-01 17:32:16 +02:00
|
|
|
|
2018-04-04 19:35:30 +02:00
|
|
|
pix := make([]byte, size*size*4)
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
pix[4*(i+j*size)] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+1] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+2] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+3] = byte(i + j)
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 17:55:10 +02:00
|
|
|
img4.WritePixels(pix, image.Rect(0, 0, size, size))
|
2018-04-04 19:35:30 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
dx0 = size / 4
|
|
|
|
dy0 = size / 4
|
|
|
|
dx1 = size * 3 / 4
|
|
|
|
dy1 = size * 3 / 4
|
|
|
|
)
|
2023-02-23 09:21:17 +01:00
|
|
|
// img4.ensureIsolatedFromSource() should be called.
|
2019-09-20 19:35:18 +02:00
|
|
|
vs := quadVertices(size/2, size/2, size/4, size/4, 1)
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, size, size)
|
2023-11-04 10:09:47 +01:00
|
|
|
img4.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := img4.IsOnSourceBackendForTesting(), false; got != want {
|
2022-04-01 17:32:16 +02:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
// img5 is not allocated now, but is allocated at DrawTriangles.
|
2022-04-01 17:32:16 +02:00
|
|
|
vs = quadVertices(0, 0, size/2, size/2, 1)
|
2023-09-28 07:29:55 +02:00
|
|
|
dr = image.Rect(0, 0, size/2, size/2)
|
2023-11-04 10:09:47 +01:00
|
|
|
img3.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img5}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := img3.IsOnSourceBackendForTesting(), false; got != want {
|
2018-07-11 18:40:24 +02:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
2018-04-04 19:35:30 +02:00
|
|
|
|
2022-08-01 17:28:16 +02:00
|
|
|
pix = make([]byte, 4*size*size)
|
2023-10-24 16:15:29 +02:00
|
|
|
if err := img4.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, size, size)); err != nil {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-04-04 19:35:30 +02:00
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
2020-04-18 13:01:40 +02:00
|
|
|
r := pix[4*(size*j+i)]
|
|
|
|
g := pix[4*(size*j+i)+1]
|
|
|
|
b := pix[4*(size*j+i)+2]
|
|
|
|
a := pix[4*(size*j+i)+3]
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: r, G: g, B: b, A: a}
|
2018-04-04 19:35:30 +02:00
|
|
|
var want color.RGBA
|
|
|
|
if i < dx0 || dx1 <= i || j < dy0 || dy1 <= j {
|
|
|
|
c := byte(i + j)
|
2023-01-28 11:06:38 +01:00
|
|
|
want = color.RGBA{R: c, G: c, B: c, A: c}
|
2018-04-04 19:35:30 +02:00
|
|
|
}
|
|
|
|
if got != want {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Errorf("at(%d, %d): got: %v, want: %v", i, j, got, want)
|
2018-04-04 19:35:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-03 07:10:43 +02:00
|
|
|
|
|
|
|
// Check further drawing doesn't cause panic.
|
|
|
|
// This bug was fixed by 03dcd948.
|
2023-03-02 11:04:20 +01:00
|
|
|
vs = quadVertices(0, 0, size/2, size/2, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
img4.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2018-04-04 19:35:30 +02:00
|
|
|
}
|
2018-07-11 18:40:24 +02:00
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
func TestReputOnSourceBackend(t *testing.T) {
|
2018-07-11 18:40:24 +02:00
|
|
|
const size = 16
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
img0 := atlas.NewImage(size, size, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img0.Deallocate()
|
2023-04-27 17:55:10 +02:00
|
|
|
img0.WritePixels(make([]byte, 4*size*size), image.Rect(0, 0, size, size))
|
2018-07-11 18:40:24 +02:00
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
img1 := atlas.NewImage(size, size, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img1.Deallocate()
|
2023-04-27 17:55:10 +02:00
|
|
|
img1.WritePixels(make([]byte, 4*size*size), image.Rect(0, 0, size, size))
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := img1.IsOnSourceBackendForTesting(), true; got != want {
|
2018-07-11 18:40:24 +02:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
img2 := atlas.NewImage(size, size, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img2.Deallocate()
|
2018-07-11 18:40:24 +02:00
|
|
|
pix := make([]byte, 4*size*size)
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
pix[4*(i+j*size)] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+1] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+2] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+3] = byte(i + j)
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 17:55:10 +02:00
|
|
|
img2.WritePixels(pix, image.Rect(0, 0, size, size))
|
2018-07-11 18:40:24 +02:00
|
|
|
|
2023-03-02 11:04:20 +01:00
|
|
|
// Create a volatile image. This should always be on a non-source backend.
|
2022-06-07 16:57:56 +02:00
|
|
|
img3 := atlas.NewImage(size, size, atlas.ImageTypeVolatile)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img3.Deallocate()
|
2023-04-27 17:55:10 +02:00
|
|
|
img3.WritePixels(make([]byte, 4*size*size), image.Rect(0, 0, size, size))
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := img3.IsOnSourceBackendForTesting(), false; got != want {
|
2018-07-24 18:08:04 +02:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:40:24 +02:00
|
|
|
// Use img1 as a render target.
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, size, size)
|
2023-03-02 11:04:20 +01:00
|
|
|
// Render onto img1. The count should not matter.
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
vs := quadVertices(size, size, 0, 0, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
img1.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-03-02 11:04:20 +01:00
|
|
|
if got, want := img1.IsOnSourceBackendForTesting(), false; got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use img1 as a render source.
|
2021-03-09 18:29:56 +01:00
|
|
|
// Use the doubled count since img1 was on a texture atlas and became an isolated image once.
|
2023-01-28 11:06:38 +01:00
|
|
|
// Then, img1 requires longer time to recover to be on a texture atlas again.
|
2023-02-23 09:21:17 +01:00
|
|
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend*2; i++ {
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-03-02 11:04:20 +01:00
|
|
|
vs := quadVertices(size, size, 0, 0, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := img1.IsOnSourceBackendForTesting(), false; got != want {
|
2018-07-11 18:40:24 +02:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2023-03-02 10:58:24 +01:00
|
|
|
// Finally, img1 is on a source backend.
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-03-02 11:04:20 +01:00
|
|
|
vs := quadVertices(size, size, 0, 0, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-03-02 10:58:24 +01:00
|
|
|
if got, want := img1.IsOnSourceBackendForTesting(), true; got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2018-07-11 18:40:24 +02:00
|
|
|
|
2022-08-01 17:28:16 +02:00
|
|
|
pix = make([]byte, 4*size*size)
|
2023-10-24 16:15:29 +02:00
|
|
|
if err := img1.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, size, size)); err != nil {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-11 18:40:24 +02:00
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: byte(i + j), G: byte(i + j), B: byte(i + j), A: byte(i + j)}
|
2020-04-18 13:01:40 +02:00
|
|
|
r := pix[4*(size*j+i)]
|
|
|
|
g := pix[4*(size*j+i)+1]
|
|
|
|
b := pix[4*(size*j+i)+2]
|
|
|
|
a := pix[4*(size*j+i)+3]
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: r, G: g, B: b, A: a}
|
2018-07-11 18:40:24 +02:00
|
|
|
if got != want {
|
2020-06-14 09:03:26 +02:00
|
|
|
t.Errorf("At(%d, %d): got: %v, want: %v", i, j, got, want)
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 11:04:20 +01:00
|
|
|
vs = quadVertices(size, size, 0, 0, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := img1.IsOnSourceBackendForTesting(), true; got != want {
|
2018-07-11 18:40:24 +02:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
2022-08-01 17:28:16 +02:00
|
|
|
pix = make([]byte, 4*size*size)
|
2023-10-24 16:15:29 +02:00
|
|
|
if err := img1.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, size, size)); err != nil {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-11 18:40:24 +02:00
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: byte(i + j), G: byte(i + j), B: byte(i + j), A: byte(i + j)}
|
2020-04-18 13:01:40 +02:00
|
|
|
r := pix[4*(size*j+i)]
|
|
|
|
g := pix[4*(size*j+i)+1]
|
|
|
|
b := pix[4*(size*j+i)+2]
|
|
|
|
a := pix[4*(size*j+i)+3]
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: r, G: g, B: b, A: a}
|
2018-07-11 18:40:24 +02:00
|
|
|
if got != want {
|
2020-06-14 09:03:26 +02:00
|
|
|
t.Errorf("At(%d, %d): got: %v, want: %v", i, j, got, want)
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 22:52:11 +02:00
|
|
|
|
2023-03-02 11:04:20 +01:00
|
|
|
// Use img1 as a render target again. The count should not matter.
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
vs := quadVertices(size, size, 0, 0, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
img1.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-03-02 11:04:20 +01:00
|
|
|
if got, want := img1.IsOnSourceBackendForTesting(), false; got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
2020-11-05 18:10:03 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 20:24:46 +02:00
|
|
|
// Use img1 as a render source, but call WritePixels.
|
2021-03-09 18:29:56 +01:00
|
|
|
// Now use 4x count as img1 became an isolated image again.
|
2023-02-23 09:21:17 +01:00
|
|
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend*4; i++ {
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-04-27 17:55:10 +02:00
|
|
|
img1.WritePixels(make([]byte, 4*size*size), image.Rect(0, 0, size, size))
|
2023-03-02 11:04:20 +01:00
|
|
|
vs := quadVertices(size, size, 0, 0, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := img1.IsOnSourceBackendForTesting(), false; got != want {
|
2020-11-05 18:10:03 +01:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2020-11-05 18:10:03 +01:00
|
|
|
|
2022-08-07 20:24:46 +02:00
|
|
|
// img1 is not on an atlas due to WritePixels.
|
2023-03-02 11:04:20 +01:00
|
|
|
vs = quadVertices(size, size, 0, 0, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := img1.IsOnSourceBackendForTesting(), false; got != want {
|
2020-11-05 18:10:03 +01:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
// Use img3 as a render source. As img3 is volatile, img3 is never on an atlas.
|
2023-02-23 09:21:17 +01:00
|
|
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend*2; i++ {
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-03-02 11:04:20 +01:00
|
|
|
vs := quadVertices(size, size, 0, 0, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := img3.IsOnSourceBackendForTesting(), false; got != want {
|
2018-07-24 18:08:04 +02:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 22:52:11 +02:00
|
|
|
runtime.GC()
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
2018-12-15 11:51:17 +01:00
|
|
|
|
|
|
|
func TestExtend(t *testing.T) {
|
2018-12-15 21:09:43 +01:00
|
|
|
const w0, h0 = 100, 100
|
2022-06-07 16:57:56 +02:00
|
|
|
img0 := atlas.NewImage(w0, h0, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img0.Deallocate()
|
2020-11-14 05:15:42 +01:00
|
|
|
|
2018-12-15 21:09:43 +01:00
|
|
|
p0 := make([]byte, 4*w0*h0)
|
|
|
|
for i := 0; i < w0*h0; i++ {
|
2018-12-15 11:51:17 +01:00
|
|
|
p0[4*i] = byte(i)
|
|
|
|
p0[4*i+1] = byte(i)
|
|
|
|
p0[4*i+2] = byte(i)
|
|
|
|
p0[4*i+3] = byte(i)
|
|
|
|
}
|
2023-04-27 17:55:10 +02:00
|
|
|
img0.WritePixels(p0, image.Rect(0, 0, w0, h0))
|
2018-12-15 11:51:17 +01:00
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
const w1, h1 = minSourceImageSizeForTesting + 1, 100
|
2022-06-07 16:57:56 +02:00
|
|
|
img1 := atlas.NewImage(w1, h1, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img1.Deallocate()
|
2020-11-13 17:02:06 +01:00
|
|
|
|
2018-12-15 21:09:43 +01:00
|
|
|
p1 := make([]byte, 4*w1*h1)
|
|
|
|
for i := 0; i < w1*h1; i++ {
|
|
|
|
p1[4*i] = byte(i)
|
|
|
|
p1[4*i+1] = byte(i)
|
|
|
|
p1[4*i+2] = byte(i)
|
|
|
|
p1[4*i+3] = byte(i)
|
|
|
|
}
|
2018-12-15 11:51:17 +01:00
|
|
|
// Ensure to allocate
|
2023-04-27 17:55:10 +02:00
|
|
|
img1.WritePixels(p1, image.Rect(0, 0, w1, h1))
|
2018-12-15 11:51:17 +01:00
|
|
|
|
2022-08-01 17:28:16 +02:00
|
|
|
pix0 := make([]byte, 4*w0*h0)
|
2023-10-24 16:15:29 +02:00
|
|
|
if err := img0.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix0, image.Rect(0, 0, w0, h0)); err != nil {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-15 21:09:43 +01:00
|
|
|
for j := 0; j < h0; j++ {
|
|
|
|
for i := 0; i < w0; i++ {
|
2020-04-18 13:01:40 +02:00
|
|
|
r := pix0[4*(w0*j+i)]
|
|
|
|
g := pix0[4*(w0*j+i)+1]
|
|
|
|
b := pix0[4*(w0*j+i)+2]
|
|
|
|
a := pix0[4*(w0*j+i)+3]
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: r, G: g, B: b, A: a}
|
2018-12-15 21:09:43 +01:00
|
|
|
c := byte(i + w0*j)
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: c, G: c, B: c, A: c}
|
2018-12-15 11:51:17 +01:00
|
|
|
if got != want {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Errorf("at(%d, %d): got: %v, want: %v", i, j, got, want)
|
2018-12-15 21:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 17:28:16 +02:00
|
|
|
pix1 := make([]byte, 4*w1*h1)
|
2023-10-24 16:15:29 +02:00
|
|
|
if err := img1.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix1, image.Rect(0, 0, w1, h1)); err != nil {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-15 21:09:43 +01:00
|
|
|
for j := 0; j < h1; j++ {
|
|
|
|
for i := 0; i < w1; i++ {
|
2020-04-18 13:01:40 +02:00
|
|
|
r := pix1[4*(w1*j+i)]
|
|
|
|
g := pix1[4*(w1*j+i)+1]
|
|
|
|
b := pix1[4*(w1*j+i)+2]
|
|
|
|
a := pix1[4*(w1*j+i)+3]
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: r, G: g, B: b, A: a}
|
2018-12-15 21:09:43 +01:00
|
|
|
c := byte(i + w1*j)
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: c, G: c, B: c, A: c}
|
2018-12-15 21:09:43 +01:00
|
|
|
if got != want {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Errorf("at(%d, %d): got: %v, want: %v", i, j, got, want)
|
2018-12-15 11:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-16 07:13:35 +01:00
|
|
|
|
2022-08-07 20:24:46 +02:00
|
|
|
func TestWritePixelsAfterDrawTriangles(t *testing.T) {
|
2018-12-16 07:13:35 +01:00
|
|
|
const w, h = 256, 256
|
2022-06-07 16:57:56 +02:00
|
|
|
src := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer src.Deallocate()
|
2022-06-07 16:57:56 +02:00
|
|
|
dst := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer dst.Deallocate()
|
2018-12-16 07:13:35 +01:00
|
|
|
|
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for i := 0; i < w*h; i++ {
|
|
|
|
pix[4*i] = byte(i)
|
|
|
|
pix[4*i+1] = byte(i)
|
|
|
|
pix[4*i+2] = byte(i)
|
|
|
|
pix[4*i+3] = byte(i)
|
|
|
|
}
|
2023-04-27 17:55:10 +02:00
|
|
|
src.WritePixels(pix, image.Rect(0, 0, w, h))
|
2018-12-16 07:13:35 +01:00
|
|
|
|
2019-09-20 19:35:18 +02:00
|
|
|
vs := quadVertices(w, h, 0, 0, 1)
|
2018-12-16 07:13:35 +01:00
|
|
|
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{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-04-27 17:55:10 +02:00
|
|
|
dst.WritePixels(pix, image.Rect(0, 0, w, h))
|
2018-12-16 07:13:35 +01:00
|
|
|
|
2022-08-01 17:28:16 +02:00
|
|
|
pix = make([]byte, 4*w*h)
|
2023-10-24 16:15:29 +02:00
|
|
|
if err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, w, h)); err != nil {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-16 07:13:35 +01:00
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
2020-04-18 13:01:40 +02:00
|
|
|
r := pix[4*(w*j+i)]
|
|
|
|
g := pix[4*(w*j+i)+1]
|
|
|
|
b := pix[4*(w*j+i)+2]
|
|
|
|
a := pix[4*(w*j+i)+3]
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: r, G: g, B: b, A: a}
|
2018-12-16 07:13:35 +01:00
|
|
|
c := byte(i + w*j)
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: c, G: c, B: c, A: c}
|
2018-12-16 07:13:35 +01:00
|
|
|
if got != want {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Errorf("at(%d, %d): got %v, want: %v", i, j, got, want)
|
2018-12-16 07:13:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-02 18:39:36 +01:00
|
|
|
|
2019-06-25 15:47:34 +02:00
|
|
|
// Issue #887
|
|
|
|
func TestSmallImages(t *testing.T) {
|
|
|
|
const w, h = 4, 8
|
2022-06-07 16:57:56 +02:00
|
|
|
src := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer src.Deallocate()
|
2022-06-07 16:57:56 +02:00
|
|
|
dst := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer dst.Deallocate()
|
2019-06-25 15:47:34 +02:00
|
|
|
|
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for i := 0; i < w*h; i++ {
|
|
|
|
pix[4*i] = 0xff
|
|
|
|
pix[4*i+1] = 0xff
|
|
|
|
pix[4*i+2] = 0xff
|
|
|
|
pix[4*i+3] = 0xff
|
|
|
|
}
|
2023-04-27 17:55:10 +02:00
|
|
|
src.WritePixels(pix, image.Rect(0, 0, w, h))
|
2019-06-25 15:47:34 +02:00
|
|
|
|
2019-09-20 19:35:18 +02:00
|
|
|
vs := quadVertices(w, h, 0, 0, 1)
|
2019-06-25 15:47:34 +02:00
|
|
|
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{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2019-06-25 15:47:34 +02:00
|
|
|
|
2022-08-01 17:28:16 +02:00
|
|
|
pix = make([]byte, 4*w*h)
|
2023-10-24 16:15:29 +02:00
|
|
|
if err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, w, h)); err != nil {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-06-25 15:47:34 +02:00
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
2020-04-18 13:01:40 +02:00
|
|
|
r := pix[4*(w*j+i)]
|
|
|
|
a := pix[4*(w*j+i)+3]
|
2019-06-25 15:47:34 +02:00
|
|
|
if got, want := r, byte(0xff); got != want {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Errorf("at(%d, %d) red: got: %d, want: %d", i, j, got, want)
|
2019-06-25 15:47:34 +02:00
|
|
|
}
|
|
|
|
if got, want := a, byte(0xff); got != want {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Errorf("at(%d, %d) alpha: got: %d, want: %d", i, j, got, want)
|
2019-06-25 15:47:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 04:45:06 +02:00
|
|
|
// Issue #887
|
|
|
|
func TestLongImages(t *testing.T) {
|
|
|
|
const w, h = 1, 6
|
2022-06-07 16:57:56 +02:00
|
|
|
src := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer src.Deallocate()
|
2020-04-18 13:01:40 +02:00
|
|
|
|
|
|
|
const dstW, dstH = 256, 256
|
2022-06-07 16:57:56 +02:00
|
|
|
dst := atlas.NewImage(dstW, dstH, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer dst.Deallocate()
|
2019-06-26 04:45:06 +02:00
|
|
|
|
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for i := 0; i < w*h; i++ {
|
|
|
|
pix[4*i] = 0xff
|
|
|
|
pix[4*i+1] = 0xff
|
|
|
|
pix[4*i+2] = 0xff
|
|
|
|
pix[4*i+3] = 0xff
|
|
|
|
}
|
2023-04-27 17:55:10 +02:00
|
|
|
src.WritePixels(pix, image.Rect(0, 0, w, h))
|
2019-06-26 04:45:06 +02:00
|
|
|
|
|
|
|
const scale = 120
|
2019-09-20 19:35:18 +02:00
|
|
|
vs := quadVertices(w, h, 0, 0, scale)
|
2019-06-26 04:45:06 +02:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, dstW, dstH)
|
2023-11-04 10:09:47 +01:00
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2019-06-26 04:45:06 +02:00
|
|
|
|
2022-08-01 17:28:16 +02:00
|
|
|
pix = make([]byte, 4*dstW*dstH)
|
2023-10-24 16:15:29 +02:00
|
|
|
if err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, dstW, dstH)); err != nil {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-06-26 04:45:06 +02:00
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w*scale; i++ {
|
2020-04-18 13:01:40 +02:00
|
|
|
r := pix[4*(dstW*j+i)]
|
|
|
|
a := pix[4*(dstW*j+i)+3]
|
2019-06-26 04:45:06 +02:00
|
|
|
if got, want := r, byte(0xff); got != want {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Errorf("at(%d, %d) red: got: %d, want: %d", i, j, got, want)
|
2019-06-26 04:45:06 +02:00
|
|
|
}
|
|
|
|
if got, want := a, byte(0xff); got != want {
|
2020-04-18 13:01:40 +02:00
|
|
|
t.Errorf("at(%d, %d) alpha: got: %d, want: %d", i, j, got, want)
|
2019-06-26 04:45:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 07:06:22 +01:00
|
|
|
func TestDeallocateImmediately(t *testing.T) {
|
2024-01-13 11:58:28 +01:00
|
|
|
// This tests ClearPixels is called but WritePixels is not called.
|
2019-07-17 16:03:14 +02:00
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
img0 := atlas.NewImage(16, 16, atlas.ImageTypeRegular)
|
2023-02-23 09:21:17 +01:00
|
|
|
img0.EnsureIsolatedFromSourceForTesting(nil)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img0.Deallocate()
|
2019-07-17 16:03:14 +02:00
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
img1 := atlas.NewImage(16, 16, atlas.ImageTypeRegular)
|
2023-02-23 09:21:17 +01:00
|
|
|
img1.EnsureIsolatedFromSourceForTesting(nil)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img1.Deallocate()
|
2019-07-17 16:03:14 +02:00
|
|
|
|
|
|
|
// img0 and img1 should share the same backend in 99.9999% possibility.
|
|
|
|
}
|
|
|
|
|
2019-12-19 17:13:23 +01:00
|
|
|
// Issue #1028
|
|
|
|
func TestExtendWithBigImage(t *testing.T) {
|
2022-06-07 16:57:56 +02:00
|
|
|
img0 := atlas.NewImage(1, 1, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img0.Deallocate()
|
2019-12-19 17:13:23 +01:00
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
img0.WritePixels(make([]byte, 4*1*1), image.Rect(0, 0, 1, 1))
|
2019-12-19 17:13:23 +01:00
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
img1 := atlas.NewImage(minSourceImageSizeForTesting+1, minSourceImageSizeForTesting+1, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img1.Deallocate()
|
2019-12-19 17:13:23 +01:00
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
img1.WritePixels(make([]byte, 4*(minSourceImageSizeForTesting+1)*(minSourceImageSizeForTesting+1)), image.Rect(0, 0, minSourceImageSizeForTesting+1, minSourceImageSizeForTesting+1))
|
2019-12-19 17:13:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 10:09:23 +02:00
|
|
|
// Issue #1217
|
|
|
|
func TestMaxImageSize(t *testing.T) {
|
2022-06-09 17:14:04 +02:00
|
|
|
img0 := atlas.NewImage(1, 1, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img0.Deallocate()
|
2022-06-09 17:14:04 +02:00
|
|
|
paddingSize := img0.PaddingSizeForTesting()
|
|
|
|
|
2020-06-27 10:09:23 +02:00
|
|
|
// This tests that a too-big image is allocated correctly.
|
2022-06-09 17:14:04 +02:00
|
|
|
s := maxImageSizeForTesting - 2*paddingSize
|
|
|
|
img1 := atlas.NewImage(s, s, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img1.Deallocate()
|
2023-04-27 17:55:10 +02:00
|
|
|
img1.WritePixels(make([]byte, 4*s*s), image.Rect(0, 0, s, s))
|
2020-06-27 10:09:23 +02:00
|
|
|
}
|
|
|
|
|
2021-08-14 17:02:50 +02:00
|
|
|
// Issue #1217 (disabled)
|
|
|
|
func Disable_TestMinImageSize(t *testing.T) {
|
|
|
|
// The backend cannot be reset. If this is necessary, sync the state with the images (#1756).
|
|
|
|
// ResetBackendsForTesting()
|
2020-06-27 10:09:23 +02:00
|
|
|
|
|
|
|
// This tests that extending a backend works correctly.
|
|
|
|
// Though the image size is minimum size of the backend, extending the backend happens due to the paddings.
|
2023-02-23 09:21:17 +01:00
|
|
|
s := minSourceImageSizeForTesting
|
2022-06-07 16:57:56 +02:00
|
|
|
img := atlas.NewImage(s, s, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img.Deallocate()
|
2023-04-27 17:55:10 +02:00
|
|
|
img.WritePixels(make([]byte, 4*s*s), image.Rect(0, 0, s, s))
|
2020-06-27 10:09:23 +02:00
|
|
|
}
|
|
|
|
|
2022-06-10 07:30:18 +02:00
|
|
|
func TestMaxImageSizeJust(t *testing.T) {
|
|
|
|
s := maxImageSizeForTesting
|
2023-01-28 11:06:38 +01:00
|
|
|
// An unmanaged image never belongs to an atlas and doesn't have its paddings.
|
2022-06-10 07:30:18 +02:00
|
|
|
// TODO: Should we allow such this size for ImageTypeRegular?
|
|
|
|
img := atlas.NewImage(s, s, atlas.ImageTypeUnmanaged)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img.Deallocate()
|
2023-04-27 17:55:10 +02:00
|
|
|
img.WritePixels(make([]byte, 4*s*s), image.Rect(0, 0, s, s))
|
2022-06-10 07:30:18 +02:00
|
|
|
}
|
|
|
|
|
2022-06-10 07:25:08 +02:00
|
|
|
func TestMaxImageSizeExceeded(t *testing.T) {
|
|
|
|
// This tests that a too-big image is allocated correctly.
|
|
|
|
s := maxImageSizeForTesting
|
|
|
|
img := atlas.NewImage(s+1, s, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img.Deallocate()
|
2022-06-10 07:25:08 +02:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err == nil {
|
2022-08-07 20:24:46 +02:00
|
|
|
t.Errorf("WritePixels must panic but not")
|
2022-06-10 07:25:08 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
img.WritePixels(make([]byte, 4*(s+1)*s), image.Rect(0, 0, s+1, s))
|
2022-06-10 07:25:08 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 15:37:49 +01:00
|
|
|
// Issue #1421
|
2023-11-03 07:06:22 +01:00
|
|
|
func TestDeallocatedAndReputOnSourceBackend(t *testing.T) {
|
2020-11-12 15:37:49 +01:00
|
|
|
const size = 16
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
src := atlas.NewImage(size, size, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer src.Deallocate()
|
2022-06-07 16:57:56 +02:00
|
|
|
src2 := atlas.NewImage(size, size, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer src2.Deallocate()
|
2022-06-07 16:57:56 +02:00
|
|
|
dst := atlas.NewImage(size, size, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer dst.Deallocate()
|
2020-11-12 15:37:49 +01:00
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
// Use src as a render target so that src is not on an atlas.
|
2020-11-12 15:37:49 +01:00
|
|
|
vs := quadVertices(size, size, 0, 0, 1)
|
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, size, size)
|
2023-11-04 10:09:47 +01:00
|
|
|
src.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := src.IsOnSourceBackendForTesting(), false; got != want {
|
2020-11-12 15:37:49 +01:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use src as a render source.
|
2023-02-23 09:21:17 +01:00
|
|
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend/2; i++ {
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-03-02 11:04:20 +01:00
|
|
|
vs := quadVertices(size, size, 0, 0, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := src.IsOnSourceBackendForTesting(), false; got != want {
|
2020-11-12 15:37:49 +01:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 07:06:22 +01:00
|
|
|
// Before PutImagesOnSourceBackendForTesting, deallocate the image.
|
|
|
|
src.Deallocate()
|
2020-11-12 15:37:49 +01:00
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
// Confirm that PutImagesOnSourceBackendForTesting doesn't panic.
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2020-11-12 15:37:49 +01:00
|
|
|
}
|
|
|
|
|
2021-01-17 10:22:45 +01:00
|
|
|
// Issue #1456
|
2023-02-23 09:21:17 +01:00
|
|
|
func TestImageIsNotReputOnSourceBackendWithoutUsingAsSource(t *testing.T) {
|
2021-01-17 10:22:45 +01:00
|
|
|
const size = 16
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
src := atlas.NewImage(size, size, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer src.Deallocate()
|
2022-06-07 16:57:56 +02:00
|
|
|
src2 := atlas.NewImage(size, size, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer src2.Deallocate()
|
2022-06-07 16:57:56 +02:00
|
|
|
dst := atlas.NewImage(size, size, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer dst.Deallocate()
|
2021-01-17 10:22:45 +01:00
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
// Use src as a render target so that src is not on an atlas.
|
2021-01-17 10:22:45 +01:00
|
|
|
vs := quadVertices(size, size, 0, 0, 1)
|
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, size, size)
|
2021-01-17 10:22:45 +01:00
|
|
|
|
2023-08-06 08:39:26 +02:00
|
|
|
// Use src2 as a rendering target, and make src2 on a destination backend.
|
|
|
|
//
|
|
|
|
// Call DrawTriangles multiple times.
|
|
|
|
// The number of DrawTriangles doesn't matter as long as these are called in one frame.
|
|
|
|
for i := 0; i < 2; i++ {
|
2023-11-04 10:09:47 +01:00
|
|
|
src2.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-08-06 08:39:26 +02:00
|
|
|
}
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := src2.IsOnSourceBackendForTesting(), false; got != want {
|
2021-01-17 10:22:45 +01:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the count without using src2 as a rendering source.
|
2021-03-11 15:13:24 +01:00
|
|
|
// This should not affect whether src2 is on an atlas or not.
|
2023-02-23 09:21:17 +01:00
|
|
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend; i++ {
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := src2.IsOnSourceBackendForTesting(), false; got != want {
|
2021-01-17 10:22:45 +01:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the count with using src2 as a rendering source.
|
2023-02-23 09:21:17 +01:00
|
|
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend; i++ {
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-03-02 11:04:20 +01:00
|
|
|
vs := quadVertices(size, size, 0, 0, 1)
|
2023-11-04 10:09:47 +01:00
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := src2.IsOnSourceBackendForTesting(), false; got != want {
|
2021-01-17 10:22:45 +01:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-02-23 09:21:17 +01:00
|
|
|
if got, want := src2.IsOnSourceBackendForTesting(), true; got != want {
|
2021-01-17 10:22:45 +01:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 20:24:46 +02:00
|
|
|
func TestImageWritePixelsModify(t *testing.T) {
|
2022-06-11 16:44:29 +02:00
|
|
|
for _, typ := range []atlas.ImageType{atlas.ImageTypeRegular, atlas.ImageTypeVolatile, atlas.ImageTypeUnmanaged} {
|
|
|
|
const size = 16
|
|
|
|
img := atlas.NewImage(size, size, typ)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer img.Deallocate()
|
2022-06-11 16:44:29 +02:00
|
|
|
pix := make([]byte, 4*size*size)
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
pix[4*(i+j*size)] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+1] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+2] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+3] = byte(i + j)
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 17:55:10 +02:00
|
|
|
img.WritePixels(pix, image.Rect(0, 0, size, size))
|
2022-06-11 16:44:29 +02:00
|
|
|
|
2022-08-07 20:24:46 +02:00
|
|
|
// Modify pix after WritePixels.
|
2022-06-11 16:44:29 +02:00
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
pix[4*(i+j*size)] = 0
|
|
|
|
pix[4*(i+j*size)+1] = 0
|
|
|
|
pix[4*(i+j*size)+2] = 0
|
|
|
|
pix[4*(i+j*size)+3] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the pixels are the original ones.
|
2022-08-01 17:28:16 +02:00
|
|
|
pix = make([]byte, 4*size*size)
|
2023-10-24 16:15:29 +02:00
|
|
|
if err := img.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, size, size)); err != nil {
|
2022-06-11 16:44:29 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: byte(i + j), G: byte(i + j), B: byte(i + j), A: byte(i + j)}
|
2022-06-11 16:44:29 +02:00
|
|
|
r := pix[4*(size*j+i)]
|
|
|
|
g := pix[4*(size*j+i)+1]
|
|
|
|
b := pix[4*(size*j+i)+2]
|
|
|
|
a := pix[4*(size*j+i)+3]
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: r, G: g, B: b, A: a}
|
2022-06-11 16:44:29 +02:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("Type: %d, At(%d, %d): got: %v, want: %v", typ, i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 11:07:30 +01:00
|
|
|
func TestPowerOf2(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
In int
|
|
|
|
Out int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
In: 1023,
|
|
|
|
Out: 512,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: 1024,
|
|
|
|
Out: 1024,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: 1025,
|
|
|
|
Out: 1024,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: 10000,
|
|
|
|
Out: 8192,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: 16384,
|
|
|
|
Out: 16384,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: 1,
|
|
|
|
Out: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: 0,
|
|
|
|
Out: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: -1,
|
|
|
|
Out: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2023-01-02 12:17:44 +01:00
|
|
|
got := atlas.FloorPowerOf2(tc.In)
|
2023-01-02 11:07:30 +01:00
|
|
|
want := tc.Out
|
|
|
|
if got != want {
|
2023-01-02 12:17:44 +01:00
|
|
|
t.Errorf("packing.FloorPowerOf2(%d): got: %d, want: %d", tc.In, got, want)
|
2023-01-02 11:07:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 18:36:01 +02:00
|
|
|
func TestDestinationCountOverflow(t *testing.T) {
|
|
|
|
const w, h = 256, 256
|
|
|
|
src := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer src.Deallocate()
|
2023-08-18 18:36:01 +02:00
|
|
|
dst0 := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer dst0.Deallocate()
|
2023-08-18 18:36:01 +02:00
|
|
|
dst1 := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer dst1.Deallocate()
|
2023-08-18 18:36:01 +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-08-18 18:36:01 +02:00
|
|
|
|
|
|
|
// Use dst0 as a destination for a while.
|
|
|
|
for i := 0; i < 31; i++ {
|
2023-11-04 10:09:47 +01:00
|
|
|
dst0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-08-18 18:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use dst0 as a source for a while.
|
|
|
|
// As dst0 is used as a destination too many times (31 is a maximum), dst0's backend should never be a source backend.
|
|
|
|
for i := 0; i < 100; i++ {
|
2023-11-04 10:09:47 +01:00
|
|
|
dst1.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{dst0}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-08-18 18:36:01 +02:00
|
|
|
if dst0.IsOnSourceBackendForTesting() {
|
|
|
|
t.Errorf("dst0 cannot be on a source backend: %d", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 07:39:21 +02:00
|
|
|
// Issue #2729
|
|
|
|
func TestIteratingImagesToPutOnSourceBackend(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
|
|
|
src := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer src.Deallocate()
|
2023-08-23 07:39:21 +02:00
|
|
|
srcs := make([]*atlas.Image, 10)
|
|
|
|
for i := range srcs {
|
|
|
|
srcs[i] = atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer srcs[i].Deallocate()
|
2023-08-23 07:39:21 +02:00
|
|
|
}
|
|
|
|
dst := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
2023-11-03 07:06:22 +01:00
|
|
|
defer dst.Deallocate()
|
2023-08-23 07:39:21 +02:00
|
|
|
|
2023-12-17 14:42:34 +01:00
|
|
|
// Use srcs as destinations once.
|
2023-08-23 07:39:21 +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-08-23 07:39:21 +02:00
|
|
|
for _, img := range srcs {
|
2023-11-04 10:09:47 +01:00
|
|
|
img.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-08-23 07:39:21 +02:00
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-08-23 07:39:21 +02:00
|
|
|
|
|
|
|
// Use srcs as sources. This will register an image to imagesToPutOnSourceBackend.
|
|
|
|
// Check iterating the registered image works correctly.
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
for _, src := range srcs {
|
2023-11-04 10:09:47 +01:00
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-08-23 07:39:21 +02:00
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting())
|
2023-08-23 07:39:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 14:41:37 +01:00
|
|
|
func ensureGC() {
|
|
|
|
// Use a pointer to avoid tinyalloc. A tinyalloc-ed object's finalizer might not called immediately.
|
|
|
|
// See runtime/mfinal_test.go.
|
|
|
|
x := new(unsafe.Pointer)
|
|
|
|
ch := make(chan struct{})
|
|
|
|
runtime.SetFinalizer(x, func(*unsafe.Pointer) { close(ch) })
|
|
|
|
runtime.KeepAlive(x)
|
|
|
|
runtime.GC()
|
|
|
|
<-ch
|
|
|
|
|
|
|
|
// Add a little sleep to wait for other finalizers.
|
|
|
|
// TODO: Is there a better way?
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2024-01-29 12:38:17 +01:00
|
|
|
func TestGC(t *testing.T) {
|
|
|
|
img := atlas.NewImage(16, 16, atlas.ImageTypeRegular)
|
|
|
|
img.WritePixels(make([]byte, 4*16*16), image.Rect(0, 0, 16, 16))
|
2024-01-29 13:01:49 +01:00
|
|
|
|
|
|
|
// 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 13:01:49 +01:00
|
|
|
|
|
|
|
// Get the difference of the number of deferred functions before and after img is GCed.
|
|
|
|
c := atlas.DeferredFuncCountForTesting()
|
2024-01-29 12:52:38 +01:00
|
|
|
runtime.KeepAlive(img)
|
2024-02-24 14:41:37 +01:00
|
|
|
ensureGC()
|
2024-02-14 19:00:49 +01:00
|
|
|
|
|
|
|
diff := atlas.DeferredFuncCountForTesting() - c
|
2024-01-29 13:01:49 +01:00
|
|
|
if got, want := diff, 1; got != want {
|
2024-01-29 12:38:17 +01:00
|
|
|
t.Errorf("got: %d, want: %d", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
// TODO: Add tests to extend image on an atlas out of the main loop
|