mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-12 22:17:26 +01:00
internal/graphicscommand: bug fix: resolve unsent WritePixels commands
(*Image).WritePixels doens't send a command to the queue immediately but caches commands internally. However, the package atlas assumed that pixel data was sent to the cache every end of a frame. Then, byte slices for pixels were corrupted. This change fixes the issue by resolving all the images when flushing commands. Closes #2390
This commit is contained in:
parent
aa81a6cc7d
commit
27cb149475
@ -35,6 +35,8 @@ type temporaryBytes struct {
|
|||||||
pixels []byte
|
pixels []byte
|
||||||
pos int
|
pos int
|
||||||
notFullyUsedTime int
|
notFullyUsedTime int
|
||||||
|
|
||||||
|
m sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var theTemporaryBytes temporaryBytes
|
var theTemporaryBytes temporaryBytes
|
||||||
@ -738,9 +740,12 @@ func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, path stri
|
|||||||
func EndFrame(graphicsDriver graphicsdriver.Graphics) error {
|
func EndFrame(graphicsDriver graphicsdriver.Graphics) error {
|
||||||
backendsM.Lock()
|
backendsM.Lock()
|
||||||
|
|
||||||
theTemporaryBytes.resetAtFrameEnd()
|
if err := restorable.ResolveStaleImages(graphicsDriver, true); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return restorable.ResolveStaleImages(graphicsDriver, true)
|
theTemporaryBytes.resetAtFrameEnd()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
|
func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
|
||||||
|
@ -249,6 +249,8 @@ func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics, endFrame bo
|
|||||||
// FlushCommands flushes the command queue and present the screen if needed.
|
// FlushCommands flushes the command queue and present the screen if needed.
|
||||||
// If endFrame is true, the current screen might be used to present.
|
// If endFrame is true, the current screen might be used to present.
|
||||||
func FlushCommands(graphicsDriver graphicsdriver.Graphics, endFrame bool) error {
|
func FlushCommands(graphicsDriver graphicsdriver.Graphics, endFrame bool) error {
|
||||||
|
resolveImages()
|
||||||
|
|
||||||
return theCommandQueue.Flush(graphicsDriver, endFrame)
|
return theCommandQueue.Flush(graphicsDriver, endFrame)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ type Image struct {
|
|||||||
// have its graphicsdriver.Image.
|
// have its graphicsdriver.Image.
|
||||||
id int
|
id int
|
||||||
|
|
||||||
bufferedRP []*graphicsdriver.WritePixelsArgs
|
bufferedWP []*graphicsdriver.WritePixelsArgs
|
||||||
}
|
}
|
||||||
|
|
||||||
var nextID = 1
|
var nextID = 1
|
||||||
@ -54,6 +54,25 @@ func genNextID() int {
|
|||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// unresolvedImages is the set of unresolved images.
|
||||||
|
// An unresolved image is an image that might have an state unsent to the command queue yet.
|
||||||
|
var unresolvedImages []*Image
|
||||||
|
|
||||||
|
// addUnresolvedImage adds an image to the list of unresolved images.
|
||||||
|
func addUnresolvedImage(img *Image) {
|
||||||
|
unresolvedImages = append(unresolvedImages, img)
|
||||||
|
}
|
||||||
|
|
||||||
|
// resolveImages resolves all the image states unsent to the command queue.
|
||||||
|
// resolveImages should be called before flushing commands.
|
||||||
|
func resolveImages() {
|
||||||
|
for i, img := range unresolvedImages {
|
||||||
|
img.resolveBufferedWritePixels()
|
||||||
|
unresolvedImages[i] = nil
|
||||||
|
}
|
||||||
|
unresolvedImages = unresolvedImages[:0]
|
||||||
|
}
|
||||||
|
|
||||||
// NewImage returns a new image.
|
// NewImage returns a new image.
|
||||||
//
|
//
|
||||||
// Note that the image is not initialized yet.
|
// Note that the image is not initialized yet.
|
||||||
@ -75,15 +94,15 @@ func NewImage(width, height int, screenFramebuffer bool) *Image {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) resolveBufferedWritePixels() {
|
func (i *Image) resolveBufferedWritePixels() {
|
||||||
if len(i.bufferedRP) == 0 {
|
if len(i.bufferedWP) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c := &writePixelsCommand{
|
c := &writePixelsCommand{
|
||||||
dst: i,
|
dst: i,
|
||||||
args: i.bufferedRP,
|
args: i.bufferedWP,
|
||||||
}
|
}
|
||||||
theCommandQueue.Enqueue(c)
|
theCommandQueue.Enqueue(c)
|
||||||
i.bufferedRP = nil
|
i.bufferedWP = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) Dispose() {
|
func (i *Image) Dispose() {
|
||||||
@ -162,13 +181,14 @@ func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, buf []byte, x
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) WritePixels(pixels []byte, x, y, width, height int) {
|
func (i *Image) WritePixels(pixels []byte, x, y, width, height int) {
|
||||||
i.bufferedRP = append(i.bufferedRP, &graphicsdriver.WritePixelsArgs{
|
i.bufferedWP = append(i.bufferedWP, &graphicsdriver.WritePixelsArgs{
|
||||||
Pixels: pixels,
|
Pixels: pixels,
|
||||||
X: x,
|
X: x,
|
||||||
Y: y,
|
Y: y,
|
||||||
Width: width,
|
Width: width,
|
||||||
Height: height,
|
Height: height,
|
||||||
})
|
})
|
||||||
|
addUnresolvedImage(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) IsInvalidated(graphicsDriver graphicsdriver.Graphics) (bool, error) {
|
func (i *Image) IsInvalidated(graphicsDriver graphicsdriver.Graphics) (bool, error) {
|
||||||
|
85
internal/processtest/testdata/issue2390.go
vendored
Normal file
85
internal/processtest/testdata/issue2390.go
vendored
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
// Copyright 2022 The Ebitengine 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.
|
||||||
|
|
||||||
|
//go:build ignore
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Game struct {
|
||||||
|
images []*ebiten.Image
|
||||||
|
imageCreated chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Update() error {
|
||||||
|
if g.imageCreated == nil {
|
||||||
|
g.imageCreated = make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
op := &ebiten.NewImageFromImageOptions{
|
||||||
|
Unmanaged: true,
|
||||||
|
}
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
i := i
|
||||||
|
img := image.NewRGBA(image.Rect(0, 0, 512, 512))
|
||||||
|
for j := 0; j < len(img.Pix)/4; j++ {
|
||||||
|
img.Pix[4*j] = byte(0x60 * i)
|
||||||
|
img.Pix[4*j+1] = byte(0x60 * i)
|
||||||
|
img.Pix[4*j+2] = byte(0x60 * i)
|
||||||
|
img.Pix[4*j+3] = 0xff
|
||||||
|
}
|
||||||
|
g.images = append(g.images, ebiten.NewImageFromImageWithOptions(img, op))
|
||||||
|
}
|
||||||
|
close(g.imageCreated)
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-g.imageCreated:
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, img := range g.images {
|
||||||
|
got := img.At(0, 0).(color.RGBA)
|
||||||
|
want := color.RGBA{byte(0x60 * i), byte(0x60 * i), byte(0x60 * i), 0xff}
|
||||||
|
if got != want {
|
||||||
|
panic(fmt.Sprintf("got: %v, want: %v", got, want))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ebiten.Termination
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Draw(screen *ebiten.Image) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||||
|
return 640, 480
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := ebiten.RunGame(&Game{}); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user