2018-12-16 09:00:55 +01: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 graphicscommand_test
|
|
|
|
|
|
|
|
import (
|
2022-10-02 11:19:26 +02:00
|
|
|
"fmt"
|
2023-04-27 17:00:51 +02:00
|
|
|
"image"
|
2018-12-16 09:00:55 +01:00
|
|
|
"image/color"
|
|
|
|
"testing"
|
|
|
|
|
2022-10-02 11:19:26 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2021-10-02 12:58:48 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
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"
|
2018-12-16 09:00:55 +01:00
|
|
|
)
|
|
|
|
|
2022-10-02 11:19:26 +02:00
|
|
|
var nearestFilterShader *graphicscommand.Shader
|
|
|
|
|
|
|
|
func init() {
|
2022-10-02 16:24:15 +02:00
|
|
|
ir, err := graphics.CompileShader([]byte(builtinshader.Shader(builtinshader.FilterNearest, builtinshader.AddressUnsafe, false)))
|
2022-10-02 11:19:26 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("graphicscommand: compiling the nearest shader failed: %v", err))
|
|
|
|
}
|
|
|
|
nearestFilterShader = graphicscommand.NewShader(ir)
|
|
|
|
}
|
|
|
|
|
2018-12-16 09:00:55 +01:00
|
|
|
func TestMain(m *testing.M) {
|
2020-05-24 13:15:07 +02:00
|
|
|
etesting.MainWithRunLoop(m)
|
2018-12-16 09:00:55 +01:00
|
|
|
}
|
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
func quadVertices(w, h float32) []float32 {
|
2019-09-20 21:13:32 +02:00
|
|
|
return []float32{
|
2020-07-02 16:06:58 +02:00
|
|
|
0, 0, 0, 0, 1, 1, 1, 1,
|
2023-04-16 11:56:14 +02:00
|
|
|
w, 0, w, 0, 1, 1, 1, 1,
|
|
|
|
0, w, 0, h, 1, 1, 1, 1,
|
|
|
|
w, h, w, h, 1, 1, 1, 1,
|
2019-09-20 21:13:32 +02:00
|
|
|
}
|
2019-06-21 04:09:48 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 13:46:51 +01:00
|
|
|
func TestClear(t *testing.T) {
|
2018-12-16 09:00:55 +01:00
|
|
|
const w, h = 1024, 1024
|
2022-06-06 02:21:11 +02:00
|
|
|
src := graphicscommand.NewImage(w/2, h/2, false)
|
|
|
|
dst := graphicscommand.NewImage(w, h, false)
|
2018-12-16 09:00:55 +01:00
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(w/2, h/2)
|
2018-12-16 09:00:55 +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]*graphicscommand.Image{src}, vs, is, graphicsdriver.BlendClear, dr, [graphics.ShaderImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll)
|
2018-12-16 09:00:55 +01:00
|
|
|
|
2022-02-27 12:11:27 +01:00
|
|
|
pix := make([]byte, 4*w*h)
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), []graphicsdriver.PixelsArgs{
|
2023-08-16 16:18:37 +02:00
|
|
|
{
|
|
|
|
Pixels: pix,
|
|
|
|
Region: image.Rect(0, 0, w, h),
|
|
|
|
},
|
|
|
|
}); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-16 09:00:55 +01:00
|
|
|
for j := 0; j < h/2; j++ {
|
|
|
|
for i := 0; i < w/2; i++ {
|
|
|
|
idx := 4 * (i + w*j)
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: pix[idx], G: pix[idx+1], B: pix[idx+2], A: pix[idx+3]}
|
2018-12-16 09:00:55 +01:00
|
|
|
want := color.RGBA{}
|
|
|
|
if got != want {
|
2019-04-22 16:12:03 +02:00
|
|
|
t.Errorf("dst.At(%d, %d) after DrawTriangles: got %v, want: %v", i, j, got, want)
|
2018-12-16 09:00:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 19:15:32 +01:00
|
|
|
|
2022-08-07 20:10:44 +02:00
|
|
|
func TestWritePixelsPartAfterDrawTriangles(t *testing.T) {
|
2019-01-05 19:15:32 +01:00
|
|
|
const w, h = 32, 32
|
2022-06-06 02:21:11 +02:00
|
|
|
clr := graphicscommand.NewImage(w, h, false)
|
|
|
|
src := graphicscommand.NewImage(w/2, h/2, false)
|
|
|
|
dst := graphicscommand.NewImage(w, h, false)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(w/2, h/2)
|
2019-01-05 19:15:32 +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]*graphicscommand.Image{clr}, vs, is, graphicsdriver.BlendClear, dr, [graphics.ShaderImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll)
|
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll)
|
2023-10-08 15:59:35 +02:00
|
|
|
bs := graphics.NewManagedBytes(4, func(bs []byte) {
|
|
|
|
for i := range bs {
|
|
|
|
bs[i] = 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
dst.WritePixels(bs, image.Rect(0, 0, 1, 1))
|
2020-08-15 08:11:56 +02:00
|
|
|
|
|
|
|
// TODO: Check the result.
|
2019-01-05 19:15:32 +01:00
|
|
|
}
|
2020-05-17 20:45:58 +02:00
|
|
|
|
|
|
|
func TestShader(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
2022-06-06 02:21:11 +02:00
|
|
|
clr := graphicscommand.NewImage(w, h, false)
|
|
|
|
dst := graphicscommand.NewImage(w, h, false)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(w, h)
|
2020-05-17 20:45:58 +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]*graphicscommand.Image{clr}, vs, is, graphicsdriver.BlendClear, dr, [graphics.ShaderImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll)
|
2020-05-17 20:45:58 +02:00
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
g := ui.Get().GraphicsDriverForTesting()
|
2022-03-21 09:48:47 +01:00
|
|
|
s := graphicscommand.NewShader(etesting.ShaderProgramFill(0xff, 0, 0, 0xff))
|
2023-11-04 10:09:47 +01:00
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, s, nil, graphicsdriver.FillAll)
|
2020-05-17 20:45:58 +02:00
|
|
|
|
2022-02-27 12:11:27 +01:00
|
|
|
pix := make([]byte, 4*w*h)
|
2023-08-16 16:18:37 +02:00
|
|
|
if err := dst.ReadPixels(g, []graphicsdriver.PixelsArgs{
|
|
|
|
{
|
|
|
|
Pixels: pix,
|
|
|
|
Region: image.Rect(0, 0, w, h),
|
|
|
|
},
|
|
|
|
}); err != nil {
|
2020-05-17 20:45:58 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
idx := 4 * (i + w*j)
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: pix[idx], G: pix[idx+1], B: pix[idx+2], A: pix[idx+3]}
|
|
|
|
want := color.RGBA{R: 0xff, A: 0xff}
|
2020-05-17 20:45:58 +02:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("dst.At(%d, %d) after DrawTriangles: got %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|