From 0755523776739155dcea581569923bf90656c868 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 25 Feb 2023 02:43:58 +0900 Subject: [PATCH] internal/restorable: bug fix: pixel info was unexpectedly lost --- internal/restorable/image.go | 2 +- internal/restorable/images_test.go | 33 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 8f76ff653..129642181 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -494,9 +494,9 @@ func (i *Image) makeStaleIfDependingOnShader(shader *Shader) { // readPixelsFromGPU reads the pixels from GPU and resolves the image's 'stale' state. func (i *Image) readPixelsFromGPU(graphicsDriver graphicsdriver.Graphics) error { - i.basePixels = Pixels{} var r image.Rectangle if i.stale { + i.basePixels = Pixels{} r = i.staleRegion } else { for _, d := range i.drawTrianglesHistory { diff --git a/internal/restorable/images_test.go b/internal/restorable/images_test.go index 2d742aad6..55302ff09 100644 --- a/internal/restorable/images_test.go +++ b/internal/restorable/images_test.go @@ -1129,3 +1129,36 @@ func TestDrawTrianglesAndReadPixels(t *testing.T) { t.Errorf("got: %v, want: %v", got, want) } } + +func TestWritePixelsAndDrawTriangles(t *testing.T) { + src := restorable.NewImage(1, 1, restorable.ImageTypeRegular) + dst := restorable.NewImage(2, 1, restorable.ImageTypeRegular) + + src.WritePixels([]byte{0x80, 0x80, 0x80, 0x80}, 0, 0, 1, 1) + + // Call WritePixels first. + dst.WritePixels([]byte{0x40, 0x40, 0x40, 0x40}, 0, 0, 1, 1) + + // Call DrawTriangles at a different region second. + vs := quadVertices(src, 1, 1, 1, 0) + is := graphics.QuadIndices() + dr := graphicsdriver.Region{ + X: 1, + Y: 0, + Width: 1, + Height: 1, + } + dst.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{src}, [graphics.ShaderImageCount - 1][2]float32{}, vs, is, graphicsdriver.BlendSourceOver, dr, graphicsdriver.Region{}, restorable.NearestFilterShader, nil, false) + + // Get the pixels. + pix := make([]byte, 4*2*1) + if err := dst.ReadPixels(ui.GraphicsDriverForTesting(), pix, 0, 0, 2, 1); err != nil { + t.Fatal(err) + } + if got, want := (color.RGBA{R: pix[0], G: pix[1], B: pix[2], A: pix[3]}), (color.RGBA{R: 0x40, G: 0x40, B: 0x40, A: 0x40}); !sameColors(got, want, 1) { + t.Errorf("got: %v, want: %v", got, want) + } + if got, want := (color.RGBA{R: pix[4], G: pix[5], B: pix[6], A: pix[7]}), (color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0x80}); !sameColors(got, want, 1) { + t.Errorf("got: %v, want: %v", got, want) + } +}