internal/restorable: remove drawTrianglesHistoryItem

Updates #805
This commit is contained in:
Hajime Hoshi 2024-01-09 02:38:31 +09:00
parent 5a36b4bb48
commit afe3f7a8ff

View File

@ -77,19 +77,6 @@ func (p *Pixels) Dispose() {
p.pixelsRecords.dispose() p.pixelsRecords.dispose()
} }
// drawTrianglesHistoryItem is an item for history of draw-image commands.
type drawTrianglesHistoryItem struct {
images [graphics.ShaderImageCount]*Image
vertices []float32
indices []uint32
blend graphicsdriver.Blend
dstRegion image.Rectangle
srcRegions [graphics.ShaderImageCount]image.Rectangle
shader *Shader
uniforms []uint32
fillRule graphicsdriver.FillRule
}
type ImageType int type ImageType int
const ( const (
@ -117,10 +104,6 @@ type Image struct {
basePixels Pixels basePixels Pixels
// drawTrianglesHistory is a set of draw-image commands.
// TODO: This should be merged with the similar command queue in package graphics (#433).
drawTrianglesHistory []*drawTrianglesHistoryItem
// stale indicates whether the image needs to be synced with GPU as soon as possible. // stale indicates whether the image needs to be synced with GPU as soon as possible.
stale bool stale bool
@ -209,11 +192,6 @@ func clearImage(i *graphicscommand.Image, region image.Rectangle) {
i.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendClear, region, [graphics.ShaderImageCount]image.Rectangle{}, clearShader.shader, nil, graphicsdriver.FillAll) i.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendClear, region, [graphics.ShaderImageCount]image.Rectangle{}, clearShader.shader, nil, graphicsdriver.FillAll)
} }
// BasePixelsForTesting returns the image's basePixels for testing.
func (i *Image) BasePixelsForTesting() *Pixels {
return &i.basePixels
}
// makeStale makes the image stale. // makeStale makes the image stale.
func (i *Image) makeStale(rect image.Rectangle) { func (i *Image) makeStale(rect image.Rectangle) {
i.stale = true i.stale = true
@ -224,7 +202,6 @@ func (i *Image) makeStale(rect image.Rectangle) {
} }
var addedRegions []image.Rectangle var addedRegions []image.Rectangle
i.appendRegionsForDrawTriangles(&addedRegions)
if !rect.Empty() { if !rect.Empty() {
appendRegionRemovingDuplicates(&addedRegions, rect) appendRegionRemovingDuplicates(&addedRegions, rect)
} }
@ -233,8 +210,6 @@ func (i *Image) makeStale(rect image.Rectangle) {
appendRegionRemovingDuplicates(&i.staleRegions, rect) appendRegionRemovingDuplicates(&i.staleRegions, rect)
} }
i.clearDrawTrianglesHistory()
// Clear pixels to save memory. // Clear pixels to save memory.
for _, r := range addedRegions { for _, r := range addedRegions {
i.basePixels.Clear(r) i.basePixels.Clear(r)
@ -312,49 +287,8 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
i.image.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule) i.image.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule)
} }
// appendDrawTrianglesHistory appends a draw-image history item to the image.
func (i *Image) appendDrawTrianglesHistory(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) {
if i.stale || !i.needsRestoring() {
panic("restorable: an image must not be stale or need restoring at appendDrawTrianglesHistory")
}
if alwaysReadPixelsFromGPU() {
panic("restorable: appendDrawTrianglesHistory must not be called when AlwaysReadPixelsFromGPU() returns true")
}
// TODO: Would it be possible to merge draw image history items?
const maxDrawTrianglesHistoryCount = 1024
if len(i.drawTrianglesHistory)+1 > maxDrawTrianglesHistoryCount {
i.makeStale(dstRegion)
return
}
// All images must be resolved and not stale each after frame.
// So we don't have to care if image is stale or not here.
vs := make([]float32, len(vertices))
copy(vs, vertices)
is := make([]uint32, len(indices))
copy(is, indices)
us := make([]uint32, len(uniforms))
copy(us, uniforms)
item := &drawTrianglesHistoryItem{
images: srcs,
vertices: vs,
indices: is,
blend: blend,
dstRegion: dstRegion,
srcRegions: srcRegions,
shader: shader,
uniforms: us,
fillRule: fillRule,
}
i.drawTrianglesHistory = append(i.drawTrianglesHistory, item)
}
func (i *Image) readPixelsFromGPUIfNeeded(graphicsDriver graphicsdriver.Graphics) error { func (i *Image) readPixelsFromGPUIfNeeded(graphicsDriver graphicsdriver.Graphics) error {
if len(i.drawTrianglesHistory) > 0 || i.stale { if i.stale {
if err := i.readPixelsFromGPU(graphicsDriver); err != nil { if err := i.readPixelsFromGPU(graphicsDriver); err != nil {
return err return err
} }
@ -413,7 +347,6 @@ func (i *Image) readPixelsFromGPU(graphicsDriver graphicsdriver.Graphics) error
if i.stale { if i.stale {
rs = i.staleRegions rs = i.staleRegions
} else { } else {
i.appendRegionsForDrawTriangles(&i.regionsCache)
defer func() { defer func() {
i.regionsCache = i.regionsCache[:0] i.regionsCache = i.regionsCache[:0]
}() }()
@ -453,7 +386,6 @@ func (i *Image) readPixelsFromGPU(graphicsDriver graphicsdriver.Graphics) error
i.basePixels.AddOrReplace(bs, a.Region) i.basePixels.AddOrReplace(bs, a.Region)
} }
i.clearDrawTrianglesHistory()
i.stale = false i.stale = false
i.staleRegions = i.staleRegions[:0] i.staleRegions = i.staleRegions[:0]
return nil return nil
@ -461,49 +393,23 @@ func (i *Image) readPixelsFromGPU(graphicsDriver graphicsdriver.Graphics) error
// dependsOn reports whether the image depends on target. // dependsOn reports whether the image depends on target.
func (i *Image) dependsOn(target *Image) bool { func (i *Image) dependsOn(target *Image) bool {
for _, c := range i.drawTrianglesHistory {
for _, img := range c.images {
if img == nil {
continue
}
if img == target {
return true
}
}
}
return false return false
} }
// dependsOnShader reports whether the image depends on shader. // dependsOnShader reports whether the image depends on shader.
func (i *Image) dependsOnShader(shader *Shader) bool { func (i *Image) dependsOnShader(shader *Shader) bool {
for _, c := range i.drawTrianglesHistory {
if c.shader == shader {
return true
}
}
return false return false
} }
// dependingImages returns all images that is depended on the image. // dependingImages returns all images that is depended on the image.
func (i *Image) dependingImages() map[*Image]struct{} { func (i *Image) dependingImages() map[*Image]struct{} {
r := map[*Image]struct{}{} r := map[*Image]struct{}{}
for _, c := range i.drawTrianglesHistory {
for _, img := range c.images {
if img == nil {
continue
}
r[img] = struct{}{}
}
}
return r return r
} }
// hasDependency returns a boolean value indicating whether the image depends on another image. // hasDependency returns a boolean value indicating whether the image depends on another image.
func (i *Image) hasDependency() bool { func (i *Image) hasDependency() bool {
if i.stale { return false
return false
}
return len(i.drawTrianglesHistory) > 0
} }
// Restore restores *graphicscommand.Image from the pixels using its state. // Restore restores *graphicscommand.Image from the pixels using its state.
@ -518,7 +424,6 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
i.image = graphicscommand.NewImage(w, h, true) i.image = graphicscommand.NewImage(w, h, true)
i.basePixels.Dispose() i.basePixels.Dispose()
i.basePixels = Pixels{} i.basePixels = Pixels{}
i.clearDrawTrianglesHistory()
i.stale = false i.stale = false
i.staleRegions = i.staleRegions[:0] i.staleRegions = i.staleRegions[:0]
return nil return nil
@ -540,62 +445,7 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
i.basePixels.Apply(gimg) i.basePixels.Apply(gimg)
for _, c := range i.drawTrianglesHistory {
var imgs [graphics.ShaderImageCount]*graphicscommand.Image
for i, img := range c.images {
if img == nil {
continue
}
if img.hasDependency() {
panic("restorable: all dependencies must be already resolved but not")
}
imgs[i] = img.image
}
gimg.DrawTriangles(imgs, c.vertices, c.indices, c.blend, c.dstRegion, c.srcRegions, c.shader.shader, c.uniforms, c.fillRule)
}
// In order to clear the draw-triangles history, read pixels from GPU.
if len(i.drawTrianglesHistory) > 0 {
i.appendRegionsForDrawTriangles(&i.regionsCache)
defer func() {
i.regionsCache = i.regionsCache[:0]
}()
args := make([]graphicsdriver.PixelsArgs, 0, len(i.regionsCache))
for _, r := range i.regionsCache {
if r.Empty() {
continue
}
if i.pixelsCache == nil {
i.pixelsCache = map[image.Rectangle][]byte{}
}
pix, ok := i.pixelsCache[r]
if !ok {
pix = make([]byte, 4*r.Dx()*r.Dy())
i.pixelsCache[r] = pix
}
args = append(args, graphicsdriver.PixelsArgs{
Pixels: pix,
Region: r,
})
}
if err := gimg.ReadPixels(graphicsDriver, args); err != nil {
return err
}
for _, a := range args {
bs := graphics.NewManagedBytes(len(a.Pixels), func(bs []byte) {
copy(bs, a.Pixels)
})
i.basePixels.AddOrReplace(bs, a.Region)
}
}
i.image = gimg i.image = gimg
i.clearDrawTrianglesHistory()
i.stale = false i.stale = false
i.staleRegions = i.staleRegions[:0] i.staleRegions = i.staleRegions[:0]
return nil return nil
@ -611,7 +461,6 @@ func (i *Image) Dispose() {
i.basePixels.Dispose() i.basePixels.Dispose()
i.basePixels = Pixels{} i.basePixels = Pixels{}
i.pixelsCache = nil i.pixelsCache = nil
i.clearDrawTrianglesHistory()
i.stale = false i.stale = false
i.staleRegions = i.staleRegions[:0] i.staleRegions = i.staleRegions[:0]
} }
@ -620,27 +469,10 @@ func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackb
return i.image.Dump(graphicsDriver, path, blackbg, rect) return i.image.Dump(graphicsDriver, path, blackbg, rect)
} }
func (i *Image) clearDrawTrianglesHistory() {
// Clear the items explicitly, or the references might remain (#1803).
for idx := range i.drawTrianglesHistory {
i.drawTrianglesHistory[idx] = nil
}
i.drawTrianglesHistory = i.drawTrianglesHistory[:0]
}
func (i *Image) InternalSize() (int, int) { func (i *Image) InternalSize() (int, int) {
return i.image.InternalSize() return i.image.InternalSize()
} }
func (i *Image) appendRegionsForDrawTriangles(regions *[]image.Rectangle) {
for _, d := range i.drawTrianglesHistory {
if d.dstRegion.Empty() {
continue
}
appendRegionRemovingDuplicates(regions, d.dstRegion)
}
}
// appendRegionRemovingDuplicates adds a region to a given list of regions, // appendRegionRemovingDuplicates adds a region to a given list of regions,
// but removes any duplicate between the newly added region and any existing regions. // but removes any duplicate between the newly added region and any existing regions.
// //