mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-15 07:27:32 +01:00
af9bd6a282
This parameter was not important and actually not used with usual shaders. However, now this information will be important for the restorable package optimization later. There was a performance issue that was caused by srcRegions, but now this should not happen thanks to FilterUniformVariables. Updates #1293
117 lines
4.5 KiB
Go
117 lines
4.5 KiB
Go
// 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 (
|
|
"image"
|
|
"image/color"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
|
|
etesting "github.com/hajimehoshi/ebiten/v2/internal/testing"
|
|
"github.com/hajimehoshi/ebiten/v2/internal/ui"
|
|
)
|
|
|
|
func TestShaderFillTwice(t *testing.T) {
|
|
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)
|
|
g := ui.Get().GraphicsDriverForTesting()
|
|
s0 := atlas.NewShader(etesting.ShaderProgramFill(0xff, 0xff, 0xff, 0xff), "")
|
|
dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, s0, nil, graphicsdriver.FillRuleFillAll, restorable.HintNone)
|
|
|
|
// Vertices must be recreated (#1755)
|
|
vs = quadVertices(w, h, 0, 0, 1)
|
|
s1 := atlas.NewShader(etesting.ShaderProgramFill(0x80, 0x80, 0x80, 0xff), "")
|
|
dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, s1, nil, graphicsdriver.FillRuleFillAll, restorable.HintNone)
|
|
|
|
pix := make([]byte, 4*w*h)
|
|
ok, err := dst.ReadPixels(g, pix, image.Rect(0, 0, w, h))
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !ok {
|
|
t.Fatal("ReadPixels failed")
|
|
}
|
|
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 {
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestImageDrawTwice(t *testing.T) {
|
|
const w, h = 1, 1
|
|
|
|
dst := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
|
src0 := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
|
src0.WritePixels([]byte{0xff, 0xff, 0xff, 0xff}, image.Rect(0, 0, w, h))
|
|
src1 := atlas.NewImage(w, h, atlas.ImageTypeRegular)
|
|
src1.WritePixels([]byte{0x80, 0x80, 0x80, 0xff}, image.Rect(0, 0, w, h))
|
|
|
|
vs := quadVertices(w, h, 0, 0, 1)
|
|
is := graphics.QuadIndices()
|
|
dr := image.Rect(0, 0, w, h)
|
|
sr := image.Rect(0, 0, w, h)
|
|
dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src0}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil, graphicsdriver.FillRuleFillAll, restorable.HintNone)
|
|
|
|
// Vertices must be recreated (#1755)
|
|
vs = quadVertices(w, h, 0, 0, 1)
|
|
dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil, graphicsdriver.FillRuleFillAll, restorable.HintNone)
|
|
|
|
pix := make([]byte, 4*w*h)
|
|
ok, err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, w, h))
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !ok {
|
|
t.Fatal("ReadPixels failed")
|
|
}
|
|
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 {
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
}
|
|
}
|
|
|
|
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.ShaderSrcImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, s, nil, graphicsdriver.FillRuleFillAll, restorable.HintNone)
|
|
|
|
// Ensure other objects are GCed, as GC appends deferred functions for collected objects.
|
|
ensureGC()
|
|
|
|
// Get the difference of the number of deferred functions before and after s is GCed.
|
|
c := atlas.DeferredFuncCountForTesting()
|
|
runtime.KeepAlive(s)
|
|
ensureGC()
|
|
|
|
diff := atlas.DeferredFuncCountForTesting() - c
|
|
if got, want := diff, 1; got != want {
|
|
t.Errorf("got: %d, want: %d", got, want)
|
|
}
|
|
}
|