2020-05-24 17:46:23 +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.
|
|
|
|
|
|
|
|
package restorable_test
|
|
|
|
|
|
|
|
import (
|
2023-04-27 17:12:42 +02:00
|
|
|
"image"
|
2020-05-24 17:46:23 +02:00
|
|
|
"image/color"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
2021-10-02 12:58:48 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
|
2020-10-03 19:35:13 +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"
|
2020-05-24 17:46:23 +02:00
|
|
|
)
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
func clearImage(img *restorable.Image, w, h int) {
|
2022-06-06 02:29:30 +02:00
|
|
|
emptyImage := restorable.NewImage(3, 3, restorable.ImageTypeRegular)
|
2021-09-08 20:58:58 +02:00
|
|
|
defer emptyImage.Dispose()
|
|
|
|
|
2020-11-07 18:26:51 +01:00
|
|
|
dx0 := float32(0)
|
|
|
|
dy0 := float32(0)
|
|
|
|
dx1 := float32(w)
|
|
|
|
dy1 := float32(h)
|
|
|
|
sx0 := float32(1)
|
|
|
|
sy0 := float32(1)
|
|
|
|
sx1 := float32(2)
|
|
|
|
sy1 := float32(2)
|
|
|
|
vs := []float32{
|
|
|
|
dx0, dy0, sx0, sy0, 0, 0, 0, 0,
|
|
|
|
dx1, dy0, sx1, sy0, 0, 0, 0, 0,
|
|
|
|
dx0, dy1, sx0, sy1, 0, 0, 0, 0,
|
|
|
|
dx1, dy1, sx1, sy1, 0, 0, 0, 0,
|
|
|
|
}
|
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
2023-11-04 11:02:40 +01:00
|
|
|
img.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{emptyImage}, vs, is, graphicsdriver.BlendClear, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2020-11-07 18:26:51 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:46:23 +02:00
|
|
|
func TestShader(t *testing.T) {
|
2022-06-06 02:29:30 +02:00
|
|
|
img := restorable.NewImage(1, 1, restorable.ImageTypeRegular)
|
2020-05-24 17:46:23 +02:00
|
|
|
defer img.Dispose()
|
|
|
|
|
2022-03-21 09:48:47 +01:00
|
|
|
s := restorable.NewShader(etesting.ShaderProgramFill(0xff, 0, 0, 0xff))
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, 1, 1)
|
2023-11-04 11:02:40 +01:00
|
|
|
img.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{}, quadVertices(1, 1, 0, 0), graphics.QuadIndices(), graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s, nil, false)
|
2020-05-24 17:46:23 +02:00
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.Get().GraphicsDriverForTesting()); err != nil {
|
2020-05-24 17:46:23 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.Get().GraphicsDriverForTesting()); err != nil {
|
2020-05-24 17:46:23 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: 0xff, A: 0xff}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(img.BasePixelsForTesting(), 0, 0, 1, 1)
|
2020-05-24 17:46:23 +02:00
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("got %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2020-05-24 10:14:37 +02:00
|
|
|
|
|
|
|
func TestShaderChain(t *testing.T) {
|
|
|
|
const num = 10
|
2021-10-02 12:58:48 +02:00
|
|
|
imgs := []*restorable.Image{}
|
2020-05-24 10:14:37 +02:00
|
|
|
for i := 0; i < num; i++ {
|
2022-06-06 02:29:30 +02:00
|
|
|
img := restorable.NewImage(1, 1, restorable.ImageTypeRegular)
|
2020-05-24 10:14:37 +02:00
|
|
|
defer img.Dispose()
|
|
|
|
imgs = append(imgs, img)
|
|
|
|
}
|
|
|
|
|
2023-10-08 15:59:35 +02:00
|
|
|
imgs[0].WritePixels(bytesToManagedBytes([]byte{0xff, 0, 0, 0xff}), image.Rect(0, 0, 1, 1))
|
2020-05-24 10:14:37 +02:00
|
|
|
|
2022-03-21 09:48:47 +01:00
|
|
|
s := restorable.NewShader(etesting.ShaderProgramImages(1))
|
2020-05-24 10:14:37 +02:00
|
|
|
for i := 0; i < num-1; i++ {
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, 1, 1)
|
2023-11-04 11:02:40 +01:00
|
|
|
imgs[i+1].DrawTriangles([graphics.ShaderImageCount]*restorable.Image{imgs[i]}, quadVertices(1, 1, 0, 0), graphics.QuadIndices(), graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s, nil, false)
|
2020-05-24 10:14:37 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.Get().GraphicsDriverForTesting()); err != nil {
|
2020-05-24 10:14:37 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.Get().GraphicsDriverForTesting()); err != nil {
|
2020-05-24 10:14:37 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, img := range imgs {
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: 0xff, A: 0xff}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(img.BasePixelsForTesting(), 0, 0, 1, 1)
|
2020-05-24 10:14:37 +02:00
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("%d: got %v, want %v", i, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-25 15:49:41 +02:00
|
|
|
|
|
|
|
func TestShaderMultipleSources(t *testing.T) {
|
2022-07-12 18:46:02 +02:00
|
|
|
var srcs [graphics.ShaderImageCount]*restorable.Image
|
2020-05-25 15:49:41 +02:00
|
|
|
for i := range srcs {
|
2022-06-06 02:29:30 +02:00
|
|
|
srcs[i] = restorable.NewImage(1, 1, restorable.ImageTypeRegular)
|
2020-05-25 15:49:41 +02:00
|
|
|
}
|
2023-10-08 15:59:35 +02:00
|
|
|
srcs[0].WritePixels(bytesToManagedBytes([]byte{0x40, 0, 0, 0xff}), image.Rect(0, 0, 1, 1))
|
|
|
|
srcs[1].WritePixels(bytesToManagedBytes([]byte{0, 0x80, 0, 0xff}), image.Rect(0, 0, 1, 1))
|
|
|
|
srcs[2].WritePixels(bytesToManagedBytes([]byte{0, 0, 0xc0, 0xff}), image.Rect(0, 0, 1, 1))
|
2020-05-25 15:49:41 +02:00
|
|
|
|
2022-06-06 02:29:30 +02:00
|
|
|
dst := restorable.NewImage(1, 1, restorable.ImageTypeRegular)
|
2020-05-25 15:49:41 +02:00
|
|
|
|
2022-03-21 09:48:47 +01:00
|
|
|
s := restorable.NewShader(etesting.ShaderProgramImages(3))
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, 1, 1)
|
2023-11-04 11:02:40 +01:00
|
|
|
dst.DrawTriangles(srcs, quadVertices(1, 1, 0, 0), graphics.QuadIndices(), graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s, nil, false)
|
2020-07-18 12:56:22 +02:00
|
|
|
|
|
|
|
// Clear one of the sources after DrawTriangles. dst should not be affected.
|
2020-11-07 18:26:51 +01:00
|
|
|
clearImage(srcs[0], 1, 1)
|
2020-07-18 12:56:22 +02:00
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.Get().GraphicsDriverForTesting()); err != nil {
|
2020-07-18 12:56:22 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.Get().GraphicsDriverForTesting()); err != nil {
|
2020-07-18 12:56:22 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: 0x40, G: 0x80, B: 0xc0, A: 0xff}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(dst.BasePixelsForTesting(), 0, 0, 1, 1)
|
2020-07-18 12:56:22 +02:00
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("got %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShaderMultipleSourcesOnOneTexture(t *testing.T) {
|
2022-06-06 02:29:30 +02:00
|
|
|
src := restorable.NewImage(3, 1, restorable.ImageTypeRegular)
|
2023-10-08 15:59:35 +02:00
|
|
|
src.WritePixels(bytesToManagedBytes([]byte{
|
2020-07-18 12:56:22 +02:00
|
|
|
0x40, 0, 0, 0xff,
|
|
|
|
0, 0x80, 0, 0xff,
|
|
|
|
0, 0, 0xc0, 0xff,
|
2023-10-08 15:59:35 +02:00
|
|
|
}), image.Rect(0, 0, 3, 1))
|
2022-07-12 18:46:02 +02:00
|
|
|
srcs := [graphics.ShaderImageCount]*restorable.Image{src, src, src}
|
2020-07-18 12:56:22 +02:00
|
|
|
|
2022-06-06 02:29:30 +02:00
|
|
|
dst := restorable.NewImage(1, 1, restorable.ImageTypeRegular)
|
2020-07-18 12:56:22 +02:00
|
|
|
|
2022-03-21 09:48:47 +01:00
|
|
|
s := restorable.NewShader(etesting.ShaderProgramImages(3))
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, 1, 1)
|
|
|
|
srcRegions := [graphics.ShaderImageCount]image.Rectangle{
|
|
|
|
image.Rect(0, 0, 1, 1),
|
|
|
|
image.Rect(1, 0, 2, 1),
|
|
|
|
image.Rect(2, 0, 3, 1),
|
2023-08-25 01:01:32 +02:00
|
|
|
}
|
2023-11-04 11:02:40 +01:00
|
|
|
dst.DrawTriangles(srcs, quadVertices(1, 1, 0, 0), graphics.QuadIndices(), graphicsdriver.BlendCopy, dr, srcRegions, s, nil, false)
|
2020-05-25 15:49:41 +02:00
|
|
|
|
|
|
|
// Clear one of the sources after DrawTriangles. dst should not be affected.
|
2020-11-07 18:26:51 +01:00
|
|
|
clearImage(srcs[0], 3, 1)
|
2020-05-25 15:49:41 +02:00
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.Get().GraphicsDriverForTesting()); err != nil {
|
2020-05-25 15:49:41 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.Get().GraphicsDriverForTesting()); err != nil {
|
2020-05-25 15:49:41 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: 0x40, G: 0x80, B: 0xc0, A: 0xff}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(dst.BasePixelsForTesting(), 0, 0, 1, 1)
|
2020-05-25 15:49:41 +02:00
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("got %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2020-05-25 17:20:08 +02:00
|
|
|
|
|
|
|
func TestShaderDispose(t *testing.T) {
|
2022-06-06 02:29:30 +02:00
|
|
|
img := restorable.NewImage(1, 1, restorable.ImageTypeRegular)
|
2020-05-25 17:20:08 +02:00
|
|
|
defer img.Dispose()
|
|
|
|
|
2022-03-21 09:48:47 +01:00
|
|
|
s := restorable.NewShader(etesting.ShaderProgramFill(0xff, 0, 0, 0xff))
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, 1, 1)
|
2023-11-04 11:02:40 +01:00
|
|
|
img.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{}, quadVertices(1, 1, 0, 0), graphics.QuadIndices(), graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s, nil, false)
|
2020-05-25 17:20:08 +02:00
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
// Dispose the shader. This should invalidate all the images using this shader i.e., all the images become
|
2020-05-25 17:27:57 +02:00
|
|
|
// stale.
|
2020-05-25 17:20:08 +02:00
|
|
|
s.Dispose()
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.Get().GraphicsDriverForTesting()); err != nil {
|
2020-05-25 17:20:08 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.Get().GraphicsDriverForTesting()); err != nil {
|
2020-05-25 17:20:08 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: 0xff, A: 0xff}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(img.BasePixelsForTesting(), 0, 0, 1, 1)
|
2020-05-25 17:20:08 +02:00
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("got %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|