all: unify terms for buffers and flushing

This commit is contained in:
Hajime Hoshi 2022-10-16 19:47:00 +09:00
parent 4d5b608d0c
commit 2855095ac9
7 changed files with 46 additions and 49 deletions

View File

@ -38,8 +38,8 @@ type Image struct {
original *Image
bounds image.Rectangle
// Do not add a 'cache' member that are resolved lazily.
// This tends to forget resolving the cache easily (#2362).
// Do not add a 'buffering' member that are resolved lazily.
// This tends to forget resolving the buffer easily (#2362).
}
var emptyImage *Image

View File

@ -95,7 +95,7 @@ func min(a, b int) int {
return b
}
func resolveDeferred() {
func flushDeferred() {
deferredM.Lock()
fs := deferred
deferred = nil
@ -568,8 +568,8 @@ func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte
defer backendsM.Unlock()
// In the tests, BeginFrame might not be called often and then images might not be disposed (#2292).
// To prevent memory leaks, resolve the deferred functions here.
resolveDeferred()
// To prevent memory leaks, flush the deferred functions here.
flushDeferred()
if i.backend == nil || i.backend.restorable == nil {
for i := range pixels {
@ -776,7 +776,7 @@ func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
return err
}
resolveDeferred()
flushDeferred()
if err := putImagesOnAtlas(graphicsDriver); err != nil {
return err
}

View File

@ -303,5 +303,3 @@ func TestWritePixelsAndModifyBeforeMain(t *testing.T) {
t.Errorf("got: %v, want: %v", got, want)
}
}
// TODO: Add tests for shaders and WritePixels to check resolvePendingPiexles works correctly.

View File

@ -249,11 +249,11 @@ func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics, endFrame bo
// FlushCommands flushes the command queue and present the screen if needed.
// If endFrame is true, the current screen might be used to present.
func FlushCommands(graphicsDriver graphicsdriver.Graphics, endFrame bool) error {
// Resolve unresolved images only when the frame ends.
// Resolving in tests might cause test flakiness on browsers (#2391).
// Flush image buffers only when the frame ends.
// Flushing them in tests might cause test flakiness on browsers (#2391).
// TODO: Investigate why.
if endFrame {
resolveImages()
flushImageBuffers()
}
return theCommandQueue.Flush(graphicsDriver, endFrame)

View File

@ -54,23 +54,22 @@ func genNextID() int {
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
// imagesWithBuffers is the set of an image with buffers.
var imagesWithBuffers []*Image
// addUnresolvedImage adds an image to the list of unresolved images.
func addUnresolvedImage(img *Image) {
unresolvedImages = append(unresolvedImages, img)
// addImageWithBuffer adds an image to the list of images with unflushed buffers.
func addImageWithBuffer(img *Image) {
imagesWithBuffers = append(imagesWithBuffers, 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
// flushImageBuffers flushes all the image buffers and send to the command queue.
// flushImageBuffers should be called before flushing commands.
func flushImageBuffers() {
for i, img := range imagesWithBuffers {
img.flushBufferedWritePixels()
imagesWithBuffers[i] = nil
}
unresolvedImages = unresolvedImages[:0]
imagesWithBuffers = imagesWithBuffers[:0]
}
// NewImage returns a new image.
@ -93,7 +92,7 @@ func NewImage(width, height int, screenFramebuffer bool) *Image {
return i
}
func (i *Image) resolveBufferedWritePixels() {
func (i *Image) flushBufferedWritePixels() {
if len(i.bufferedWP) == 0 {
return
}
@ -154,9 +153,9 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, offsets [g
if src.screen {
panic("graphicscommand: the screen image cannot be the rendering source")
}
src.resolveBufferedWritePixels()
src.flushBufferedWritePixels()
}
i.resolveBufferedWritePixels()
i.flushBufferedWritePixels()
theCommandQueue.EnqueueDrawTrianglesCommand(i, srcs, offsets, vertices, indices, blend, dstRegion, srcRegion, shader, uniforms, evenOdd)
}
@ -164,7 +163,7 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, offsets [g
// ReadPixels reads the image's pixels.
// ReadPixels returns an error when an error happens in the graphics driver.
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, buf []byte, x, y, width, height int) error {
i.resolveBufferedWritePixels()
i.flushBufferedWritePixels()
c := &readPixelsCommand{
img: i,
x: x,
@ -188,7 +187,7 @@ func (i *Image) WritePixels(pixels []byte, x, y, width, height int) {
Width: width,
Height: height,
})
addUnresolvedImage(i)
addImageWithBuffer(i)
}
func (i *Image) IsInvalidated(graphicsDriver graphicsdriver.Graphics) (bool, error) {

View File

@ -177,8 +177,8 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) error {
c.game.DrawScreen()
// The final screen is never used as the rendering source.
// Flush its cache here just in case.
c.screen.flushCacheIfNeeded()
// Flush its buffer here just in case.
c.screen.flushBufferIfNeeded()
return nil
}

View File

@ -35,7 +35,7 @@ type Image struct {
height int
volatile bool
dotsCache map[[2]int][4]byte
dotsBuffer map[[2]int][4]byte
}
func NewImage(width, height int, imageType atlas.ImageType) *Image {
@ -53,18 +53,18 @@ func (i *Image) MarkDisposed() {
}
i.mipmap.MarkDisposed()
i.mipmap = nil
i.dotsCache = nil
i.dotsBuffer = nil
}
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 1][2]float32, shader *Shader, uniforms [][]float32, evenOdd bool, canSkipMipmap bool) {
i.flushCacheIfNeeded()
i.flushBufferIfNeeded()
var srcMipmaps [graphics.ShaderImageCount]*mipmap.Mipmap
for i, src := range srcs {
if src == nil {
continue
}
src.flushCacheIfNeeded()
src.flushBufferIfNeeded()
srcMipmaps[i] = src.mipmap
}
@ -73,22 +73,22 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
func (i *Image) WritePixels(pix []byte, x, y, width, height int) {
if width == 1 && height == 1 {
if i.dotsCache == nil {
i.dotsCache = map[[2]int][4]byte{}
if i.dotsBuffer == nil {
i.dotsBuffer = map[[2]int][4]byte{}
}
var clr [4]byte
copy(clr[:], pix)
i.dotsCache[[2]int{x, y}] = clr
i.dotsBuffer[[2]int{x, y}] = clr
// One square requires 6 indices (= 2 triangles).
if len(i.dotsCache) >= graphics.IndicesCount/6 {
i.flushCacheIfNeeded()
if len(i.dotsBuffer) >= graphics.IndicesCount/6 {
i.flushBufferIfNeeded()
}
return
}
i.flushCacheIfNeeded()
i.flushBufferIfNeeded()
i.mipmap.WritePixels(pix, x, y, width, height)
}
@ -99,14 +99,14 @@ func (i *Image) ReadPixels(pixels []byte, x, y, width, height int) {
}
if width == 1 && height == 1 {
if c, ok := i.dotsCache[[2]int{x, y}]; ok {
if c, ok := i.dotsBuffer[[2]int{x, y}]; ok {
copy(pixels, c[:])
return
}
// Do not call flushCacheIfNeeded here. This would slow (image/draw).Draw.
// Do not call flushBufferIfNeeded here. This would slow (image/draw).Draw.
// See ebiten.TestImageDrawOver.
} else {
i.flushCacheIfNeeded()
i.flushBufferIfNeeded()
}
if err := theUI.readPixels(i.mipmap, pixels, x, y, width, height); err != nil {
@ -121,17 +121,17 @@ func (i *Image) DumpScreenshot(name string, blackbg bool) (string, error) {
return theUI.dumpScreenshot(i.mipmap, name, blackbg)
}
func (i *Image) flushCacheIfNeeded() {
if len(i.dotsCache) == 0 {
func (i *Image) flushBufferIfNeeded() {
if len(i.dotsBuffer) == 0 {
return
}
l := len(i.dotsCache)
l := len(i.dotsBuffer)
vs := graphics.Vertices(l * 4)
is := make([]uint16, l*6)
sx, sy := float32(1), float32(1)
var idx int
for p, c := range i.dotsCache {
for p, c := range i.dotsBuffer {
dx := float32(p[0])
dy := float32(p[1])
crf := float32(c[0]) / 0xff
@ -181,7 +181,7 @@ func (i *Image) flushCacheIfNeeded() {
idx++
}
i.dotsCache = nil
i.dotsBuffer = nil
srcs := [graphics.ShaderImageCount]*mipmap.Mipmap{emptyImage.mipmap}
dr := graphicsdriver.Region{