mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-13 04:22:05 +01:00
internal/atlas: refactoring
This commit is contained in:
parent
32ed22f91c
commit
98cb77d94f
@ -22,8 +22,8 @@ const (
|
|||||||
BaseCountToPutOnSourceBackend = baseCountToPutOnSourceBackend
|
BaseCountToPutOnSourceBackend = baseCountToPutOnSourceBackend
|
||||||
)
|
)
|
||||||
|
|
||||||
func PutImagesOnSourceBackendForTesting(graphicsDriver graphicsdriver.Graphics) error {
|
func PutImagesOnSourceBackendForTesting(graphicsDriver graphicsdriver.Graphics) {
|
||||||
return putImagesOnSourceBackend(graphicsDriver)
|
putImagesOnSourceBackend(graphicsDriver)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -64,7 +64,7 @@ func flushDeferred() {
|
|||||||
// Actual time duration is increased in an exponential way for each usage as a rendering target.
|
// Actual time duration is increased in an exponential way for each usage as a rendering target.
|
||||||
const baseCountToPutOnSourceBackend = 10
|
const baseCountToPutOnSourceBackend = 10
|
||||||
|
|
||||||
func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) error {
|
func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) {
|
||||||
// The counter usedAsDestinationCount is updated at most once per frame (#2676).
|
// The counter usedAsDestinationCount is updated at most once per frame (#2676).
|
||||||
for i := range imagesUsedAsDestination {
|
for i := range imagesUsedAsDestination {
|
||||||
if i.usedAsDestinationCount < math.MaxInt {
|
if i.usedAsDestinationCount < math.MaxInt {
|
||||||
@ -78,9 +78,7 @@ func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) error {
|
|||||||
i.usedAsSourceCount++
|
i.usedAsSourceCount++
|
||||||
}
|
}
|
||||||
if i.usedAsSourceCount >= baseCountToPutOnSourceBackend*(1<<uint(min(i.usedAsDestinationCount, 31))) {
|
if i.usedAsSourceCount >= baseCountToPutOnSourceBackend*(1<<uint(min(i.usedAsDestinationCount, 31))) {
|
||||||
if err := i.putOnSourceBackend(graphicsDriver); err != nil {
|
i.putOnSourceBackend(graphicsDriver)
|
||||||
return err
|
|
||||||
}
|
|
||||||
i.usedAsSourceCount = 0
|
i.usedAsSourceCount = 0
|
||||||
}
|
}
|
||||||
delete(imagesToPutOnSourceBackend, i)
|
delete(imagesToPutOnSourceBackend, i)
|
||||||
@ -89,7 +87,6 @@ func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) error {
|
|||||||
for i := range imagesBackendJustCreated {
|
for i := range imagesBackendJustCreated {
|
||||||
delete(imagesBackendJustCreated, i)
|
delete(imagesBackendJustCreated, i)
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type backend struct {
|
type backend struct {
|
||||||
@ -295,14 +292,14 @@ func (i *Image) ensureIsolatedFromSource(backends []*backend) {
|
|||||||
newI.moveTo(i)
|
newI.moveTo(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) putOnSourceBackend(graphicsDriver graphicsdriver.Graphics) error {
|
func (i *Image) putOnSourceBackend(graphicsDriver graphicsdriver.Graphics) {
|
||||||
if i.backend == nil {
|
if i.backend == nil {
|
||||||
i.allocate(nil, true)
|
i.allocate(nil, true)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if i.isOnSourceBackend() {
|
if i.isOnSourceBackend() {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !i.canBePutOnAtlas() {
|
if !i.canBePutOnAtlas() {
|
||||||
@ -334,8 +331,6 @@ func (i *Image) putOnSourceBackend(graphicsDriver graphicsdriver.Graphics) error
|
|||||||
if !i.isOnSourceBackend() {
|
if !i.isOnSourceBackend() {
|
||||||
panic("atlas: i must be on a source backend but not")
|
panic("atlas: i must be on a source backend but not")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) regionWithPadding() image.Rectangle {
|
func (i *Image) regionWithPadding() image.Rectangle {
|
||||||
@ -817,9 +812,7 @@ func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
flushDeferred()
|
flushDeferred()
|
||||||
if err := putImagesOnSourceBackend(graphicsDriver); err != nil {
|
putImagesOnSourceBackend(graphicsDriver)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -210,9 +210,7 @@ func TestReputOnSourceBackend(t *testing.T) {
|
|||||||
// Use the doubled count since img1 was on a texture atlas and became an isolated image once.
|
// Use the doubled count since img1 was on a texture atlas and became an isolated image once.
|
||||||
// Then, img1 requires longer time to recover to be on a texture atlas again.
|
// Then, img1 requires longer time to recover to be on a texture atlas again.
|
||||||
for i := 0; i < atlas.BaseCountToPutOnSourceBackend*2; i++ {
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend*2; i++ {
|
||||||
if err := atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting()); err != nil {
|
atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
vs := quadVertices(size, size, 0, 0, 1)
|
vs := quadVertices(size, size, 0, 0, 1)
|
||||||
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
||||||
if got, want := img1.IsOnSourceBackendForTesting(), false; got != want {
|
if got, want := img1.IsOnSourceBackendForTesting(), false; got != want {
|
||||||
@ -220,17 +218,13 @@ func TestReputOnSourceBackend(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Finally, img1 is on a source backend.
|
// Finally, img1 is on a source backend.
|
||||||
if err := atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting()); err != nil {
|
atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
vs := quadVertices(size, size, 0, 0, 1)
|
vs := quadVertices(size, size, 0, 0, 1)
|
||||||
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
||||||
if got, want := img1.IsOnSourceBackendForTesting(), true; got != want {
|
if got, want := img1.IsOnSourceBackendForTesting(), true; got != want {
|
||||||
t.Errorf("got: %v, want: %v", got, want)
|
t.Errorf("got: %v, want: %v", got, want)
|
||||||
}
|
}
|
||||||
if err := atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting()); err != nil {
|
atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pix = make([]byte, 4*size*size)
|
pix = make([]byte, 4*size*size)
|
||||||
if err := img1.ReadPixels(ui.GraphicsDriverForTesting(), pix, image.Rect(0, 0, size, size)); err != nil {
|
if err := img1.ReadPixels(ui.GraphicsDriverForTesting(), pix, image.Rect(0, 0, size, size)); err != nil {
|
||||||
@ -286,9 +280,7 @@ func TestReputOnSourceBackend(t *testing.T) {
|
|||||||
// Use img1 as a render source, but call WritePixels.
|
// Use img1 as a render source, but call WritePixels.
|
||||||
// Now use 4x count as img1 became an isolated image again.
|
// Now use 4x count as img1 became an isolated image again.
|
||||||
for i := 0; i < atlas.BaseCountToPutOnSourceBackend*4; i++ {
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend*4; i++ {
|
||||||
if err := atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting()); err != nil {
|
atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
img1.WritePixels(make([]byte, 4*size*size), image.Rect(0, 0, size, size))
|
img1.WritePixels(make([]byte, 4*size*size), image.Rect(0, 0, size, size))
|
||||||
vs := quadVertices(size, size, 0, 0, 1)
|
vs := quadVertices(size, size, 0, 0, 1)
|
||||||
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
||||||
@ -296,9 +288,7 @@ func TestReputOnSourceBackend(t *testing.T) {
|
|||||||
t.Errorf("got: %v, want: %v", got, want)
|
t.Errorf("got: %v, want: %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting()); err != nil {
|
atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// img1 is not on an atlas due to WritePixels.
|
// img1 is not on an atlas due to WritePixels.
|
||||||
vs = quadVertices(size, size, 0, 0, 1)
|
vs = quadVertices(size, size, 0, 0, 1)
|
||||||
@ -309,9 +299,7 @@ func TestReputOnSourceBackend(t *testing.T) {
|
|||||||
|
|
||||||
// Use img3 as a render source. As img3 is volatile, img3 is never on an atlas.
|
// Use img3 as a render source. As img3 is volatile, img3 is never on an atlas.
|
||||||
for i := 0; i < atlas.BaseCountToPutOnSourceBackend*2; i++ {
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend*2; i++ {
|
||||||
if err := atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting()); err != nil {
|
atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
vs := quadVertices(size, size, 0, 0, 1)
|
vs := quadVertices(size, size, 0, 0, 1)
|
||||||
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
||||||
if got, want := img3.IsOnSourceBackendForTesting(), false; got != want {
|
if got, want := img3.IsOnSourceBackendForTesting(), false; got != want {
|
||||||
@ -633,9 +621,7 @@ func TestDisposedAndReputOnSourceBackend(t *testing.T) {
|
|||||||
|
|
||||||
// Use src as a render source.
|
// Use src as a render source.
|
||||||
for i := 0; i < atlas.BaseCountToPutOnSourceBackend/2; i++ {
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend/2; i++ {
|
||||||
if err := atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting()); err != nil {
|
atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
vs := quadVertices(size, size, 0, 0, 1)
|
vs := quadVertices(size, size, 0, 0, 1)
|
||||||
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
||||||
if got, want := src.IsOnSourceBackendForTesting(), false; got != want {
|
if got, want := src.IsOnSourceBackendForTesting(), false; got != want {
|
||||||
@ -650,9 +636,7 @@ func TestDisposedAndReputOnSourceBackend(t *testing.T) {
|
|||||||
atlas.FlushDeferredForTesting()
|
atlas.FlushDeferredForTesting()
|
||||||
|
|
||||||
// Confirm that PutImagesOnSourceBackendForTesting doesn't panic.
|
// Confirm that PutImagesOnSourceBackendForTesting doesn't panic.
|
||||||
if err := atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting()); err != nil {
|
atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue #1456
|
// Issue #1456
|
||||||
@ -690,9 +674,7 @@ func TestImageIsNotReputOnSourceBackendWithoutUsingAsSource(t *testing.T) {
|
|||||||
// Update the count without using src2 as a rendering source.
|
// Update the count without using src2 as a rendering source.
|
||||||
// This should not affect whether src2 is on an atlas or not.
|
// This should not affect whether src2 is on an atlas or not.
|
||||||
for i := 0; i < atlas.BaseCountToPutOnSourceBackend; i++ {
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend; i++ {
|
||||||
if err := atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting()); err != nil {
|
atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got, want := src2.IsOnSourceBackendForTesting(), false; got != want {
|
if got, want := src2.IsOnSourceBackendForTesting(), false; got != want {
|
||||||
t.Errorf("got: %v, want: %v", got, want)
|
t.Errorf("got: %v, want: %v", got, want)
|
||||||
}
|
}
|
||||||
@ -700,9 +682,7 @@ func TestImageIsNotReputOnSourceBackendWithoutUsingAsSource(t *testing.T) {
|
|||||||
|
|
||||||
// Update the count with using src2 as a rendering source.
|
// Update the count with using src2 as a rendering source.
|
||||||
for i := 0; i < atlas.BaseCountToPutOnSourceBackend; i++ {
|
for i := 0; i < atlas.BaseCountToPutOnSourceBackend; i++ {
|
||||||
if err := atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting()); err != nil {
|
atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
vs := quadVertices(size, size, 0, 0, 1)
|
vs := quadVertices(size, size, 0, 0, 1)
|
||||||
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src2}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src2}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, atlas.NearestFilterShader, nil, false)
|
||||||
if got, want := src2.IsOnSourceBackendForTesting(), false; got != want {
|
if got, want := src2.IsOnSourceBackendForTesting(), false; got != want {
|
||||||
@ -710,9 +690,7 @@ func TestImageIsNotReputOnSourceBackendWithoutUsingAsSource(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting()); err != nil {
|
atlas.PutImagesOnSourceBackendForTesting(ui.GraphicsDriverForTesting())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got, want := src2.IsOnSourceBackendForTesting(), true; got != want {
|
if got, want := src2.IsOnSourceBackendForTesting(), true; got != want {
|
||||||
t.Errorf("got: %v, want: %v", got, want)
|
t.Errorf("got: %v, want: %v", got, want)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user