internal/restorable: remove draw-triangles history items at WritePixels

This commit is contained in:
Hajime Hoshi 2024-09-08 18:17:23 +09:00
parent f32648b144
commit bbe3cba110

View File

@ -295,9 +295,9 @@ func (i *Image) WritePixels(pixels *graphics.ManagedBytes, region image.Rectangl
if region.Eq(image.Rect(0, 0, w, h)) { if region.Eq(image.Rect(0, 0, w, h)) {
if pixels != nil { if pixels != nil {
// Clone a ManagedBytes as the package graphicscommand has a different lifetime management. // Clone a ManagedBytes as the package graphicscommand has a different lifetime management.
i.basePixels.AddOrReplace(pixels.Clone(), image.Rect(0, 0, w, h)) i.basePixels.AddOrReplace(pixels.Clone(), region)
} else { } else {
i.basePixels.Clear(image.Rect(0, 0, w, h)) i.basePixels.Clear(region)
} }
i.clearDrawTrianglesHistory() i.clearDrawTrianglesHistory()
i.stale = false i.stale = false
@ -311,6 +311,8 @@ func (i *Image) WritePixels(pixels *graphics.ManagedBytes, region image.Rectangl
return return
} }
i.removeDrawTrianglesHistoryItems(region)
// Records for DrawTriangles cannot come before records for WritePixels. // Records for DrawTriangles cannot come before records for WritePixels.
if len(i.drawTrianglesHistory) > 0 { if len(i.drawTrianglesHistory) > 0 {
i.makeStale(region) i.makeStale(region)
@ -400,19 +402,11 @@ func (i *Image) areStaleRegionsIncludedIn(r image.Rectangle) bool {
return true return true
} }
// appendDrawTrianglesHistory appends a draw-image history item to the image. // removeDrawTrianglesHistoryItems removes draw-image history items whose destination regions are in the given region.
func (i *Image) appendDrawTrianglesHistory(srcs [graphics.ShaderSrcImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule, hint Hint) { // This is useful when the destination region is overwritten soon later.
if i.stale || !i.needsRestoration() { func (i *Image) removeDrawTrianglesHistoryItems(region image.Rectangle) {
panic("restorable: an image must not be stale or need restoration at appendDrawTrianglesHistory")
}
if AlwaysReadPixelsFromGPU() {
panic("restorable: appendDrawTrianglesHistory must not be called when AlwaysReadPixelsFromGPU() returns true")
}
// If the command overwrites the destination region, remove the history items that are in the region.
if hint == HintOverwriteDstRegion {
for idx, c := range i.drawTrianglesHistory { for idx, c := range i.drawTrianglesHistory {
if c.dstRegion.In(dstRegion) { if c.dstRegion.In(region) {
i.drawTrianglesHistory[idx] = nil i.drawTrianglesHistory[idx] = nil
} }
} }
@ -427,6 +421,20 @@ func (i *Image) appendDrawTrianglesHistory(srcs [graphics.ShaderSrcImageCount]*I
i.drawTrianglesHistory = i.drawTrianglesHistory[:n] i.drawTrianglesHistory = i.drawTrianglesHistory[:n]
} }
// appendDrawTrianglesHistory appends a draw-image history item to the image.
func (i *Image) appendDrawTrianglesHistory(srcs [graphics.ShaderSrcImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule, hint Hint) {
if i.stale || !i.needsRestoration() {
panic("restorable: an image must not be stale or need restoration at appendDrawTrianglesHistory")
}
if AlwaysReadPixelsFromGPU() {
panic("restorable: appendDrawTrianglesHistory must not be called when AlwaysReadPixelsFromGPU() returns true")
}
// If the command overwrites the destination region, remove the history items that are in the region.
if hint == HintOverwriteDstRegion {
i.removeDrawTrianglesHistoryItems(dstRegion)
}
// TODO: Would it be possible to merge draw image history items? // TODO: Would it be possible to merge draw image history items?
const maxDrawTrianglesHistoryCount = 1024 const maxDrawTrianglesHistoryCount = 1024
if len(i.drawTrianglesHistory)+1 > maxDrawTrianglesHistoryCount { if len(i.drawTrianglesHistory)+1 > maxDrawTrianglesHistoryCount {