updated ebiten version from 2.7.9 to 2.9.9
This commit is contained in:
+140
-225
@@ -22,11 +22,10 @@ import (
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/debug"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/packing"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
||||
)
|
||||
|
||||
@@ -36,30 +35,6 @@ var (
|
||||
maxSize = 0
|
||||
)
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// quadVertices returns vertices to render a quad. These values are passed to graphicscommand.Image.
|
||||
func quadVertices(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, cr, cg, cb, ca float32) []float32 {
|
||||
return []float32{
|
||||
dx0, dy0, sx0, sy0, cr, cg, cb, ca,
|
||||
dx1, dy0, sx1, sy0, cr, cg, cb, ca,
|
||||
dx0, dy1, sx0, sy1, cr, cg, cb, ca,
|
||||
dx1, dy1, sx1, sy1, cr, cg, cb, ca,
|
||||
}
|
||||
}
|
||||
|
||||
func appendDeferred(f func()) {
|
||||
deferredM.Lock()
|
||||
defer deferredM.Unlock()
|
||||
@@ -81,9 +56,14 @@ func flushDeferred() {
|
||||
// Actual time duration is increased in an exponential way for each usage as a rendering target.
|
||||
const baseCountToPutOnSourceBackend = 10
|
||||
|
||||
func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) {
|
||||
func putImagesOnSourceBackend() {
|
||||
// The counter usedAsDestinationCount is updated at most once per frame (#2676).
|
||||
imagesUsedAsDestination.forEach(func(i *Image) {
|
||||
// i.backend can be nil after deallocate is called.
|
||||
if i.backend == nil {
|
||||
i.usedAsDestinationCount = 0
|
||||
return
|
||||
}
|
||||
// This counter is not updated when the backend is created in this frame.
|
||||
if !i.backendCreatedInThisFrame && i.usedAsDestinationCount < math.MaxInt {
|
||||
i.usedAsDestinationCount++
|
||||
@@ -93,29 +73,34 @@ func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) {
|
||||
imagesUsedAsDestination.clear()
|
||||
|
||||
imagesToPutOnSourceBackend.forEach(func(i *Image) {
|
||||
// i.backend can be nil after deallocate is called.
|
||||
if i.backend == nil {
|
||||
i.usedAsSourceCount = 0
|
||||
return
|
||||
}
|
||||
if i.usedAsSourceCount < math.MaxInt {
|
||||
i.usedAsSourceCount++
|
||||
}
|
||||
if int64(i.usedAsSourceCount) >= int64(baseCountToPutOnSourceBackend*(1<<uint(min(i.usedAsDestinationCount, 31)))) {
|
||||
i.putOnSourceBackend(graphicsDriver)
|
||||
if i.usedAsSourceCount >= baseCountToPutOnSourceBackend*(1<<uint(min(i.usedAsDestinationCount, 31))) {
|
||||
i.putOnSourceBackend()
|
||||
i.usedAsSourceCount = 0
|
||||
}
|
||||
})
|
||||
imagesToPutOnSourceBackend.clear()
|
||||
}
|
||||
|
||||
// backend is a big texture atlas that can have multiple images.
|
||||
// backend is a texture in GPU.
|
||||
type backend struct {
|
||||
// image is an atlas on which there might be multiple images.
|
||||
image *graphicscommand.Image
|
||||
|
||||
width int
|
||||
height int
|
||||
// restorable is an atlas on which there might be multiple images.
|
||||
restorable *restorable.Image
|
||||
|
||||
// page is an atlas map. Each part is called a node.
|
||||
// If page is nil, the backend's image is isolated and not on an atlas.
|
||||
page *packing.Page
|
||||
|
||||
// source reports whether this backend is mainly used a rendering source, but this is not 100%.
|
||||
//
|
||||
// If a non-source (destination) image is used as a source many times,
|
||||
// the image's backend might be turned into a source backend to optimize draw calls.
|
||||
source bool
|
||||
@@ -135,73 +120,11 @@ func (b *backend) tryAlloc(width, height int) (*packing.Node, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
b.extendIfNeeded(b.page.Size())
|
||||
b.restorable = b.restorable.Extend(b.page.Size())
|
||||
|
||||
return n, true
|
||||
}
|
||||
|
||||
// extendIfNeeded extends the image by the given size if necessary.
|
||||
// extendIfNeeded creates a new image with the given size and copies the pixels of the given source image.
|
||||
// extendIfNeeded disposes an old image after its call when a new image is created.
|
||||
func (b *backend) extendIfNeeded(width, height int) {
|
||||
if b.width >= width && b.height >= height {
|
||||
return
|
||||
}
|
||||
|
||||
// Assume that the screen image is never extended.
|
||||
newImg := newClearedImage(width, height, false)
|
||||
|
||||
// Use DrawTriangles instead of WritePixels because the image i might be stale and not have its pixels
|
||||
// information.
|
||||
srcs := [graphics.ShaderImageCount]*graphicscommand.Image{b.image}
|
||||
sw, sh := b.image.InternalSize()
|
||||
vs := quadVertices(0, 0, float32(sw), float32(sh), 0, 0, float32(sw), float32(sh), 1, 1, 1, 1)
|
||||
is := graphics.QuadIndices()
|
||||
dr := image.Rect(0, 0, sw, sh)
|
||||
newImg.DrawTriangles(srcs, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, NearestFilterShader.ensureShader(), nil, graphicsdriver.FillAll)
|
||||
b.image.Dispose()
|
||||
|
||||
b.image = newImg
|
||||
b.width = width
|
||||
b.height = height
|
||||
}
|
||||
|
||||
// newClearedImage creates an emtpy image with the given size.
|
||||
//
|
||||
// Note that Dispose is not called automatically.
|
||||
func newClearedImage(width, height int, screen bool) *graphicscommand.Image {
|
||||
i := graphicscommand.NewImage(width, height, screen)
|
||||
|
||||
// This needs to use 'InternalSize' to render the whole region, or edges are unexpectedly cleared on some
|
||||
// devices.
|
||||
iw, ih := i.InternalSize()
|
||||
clearImage(i, image.Rect(0, 0, iw, ih))
|
||||
return i
|
||||
}
|
||||
|
||||
func clearImage(i *graphicscommand.Image, region image.Rectangle) {
|
||||
vs := quadVertices(float32(region.Min.X), float32(region.Min.Y), float32(region.Max.X), float32(region.Max.Y), 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
is := graphics.QuadIndices()
|
||||
i.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendClear, region, [graphics.ShaderImageCount]image.Rectangle{}, clearShader.ensureShader(), nil, graphicsdriver.FillAll)
|
||||
}
|
||||
|
||||
func (b *backend) clearPixels(region image.Rectangle) {
|
||||
if region.Dx() <= 0 || region.Dy() <= 0 {
|
||||
panic("atlas: width/height must be positive")
|
||||
}
|
||||
clearImage(b.image, region.Intersect(image.Rect(0, 0, b.width, b.height)))
|
||||
}
|
||||
|
||||
func (b *backend) writePixels(pixels *graphics.ManagedBytes, region image.Rectangle) {
|
||||
if region.Dx() <= 0 || region.Dy() <= 0 {
|
||||
panic("atlas: width/height must be positive")
|
||||
}
|
||||
if !region.In(image.Rect(0, 0, b.width, b.height)) {
|
||||
panic(fmt.Sprintf("atlas: out of range %v", region))
|
||||
}
|
||||
b.image.WritePixels(pixels, region)
|
||||
}
|
||||
|
||||
var (
|
||||
// backendsM is a mutex for critical sections of the backend and packing.Node objects.
|
||||
backendsM sync.Mutex
|
||||
@@ -215,11 +138,9 @@ var (
|
||||
// theBackends is a set of atlases.
|
||||
theBackends []*backend
|
||||
|
||||
imagesToPutOnSourceBackend smallImageSet
|
||||
imagesToPutOnSourceBackend imageSmallSet
|
||||
|
||||
imagesUsedAsDestination smallImageSet
|
||||
|
||||
graphicsDriverInitialized bool
|
||||
imagesUsedAsDestination imageSmallSet
|
||||
|
||||
deferred []func()
|
||||
|
||||
@@ -227,17 +148,33 @@ var (
|
||||
deferredM sync.Mutex
|
||||
)
|
||||
|
||||
// ImageType represents the type of an image.
|
||||
type ImageType int
|
||||
|
||||
const (
|
||||
// ImageTypeRegular is a regular image, that can be on a big texture atlas (backend).
|
||||
ImageTypeRegular ImageType = iota
|
||||
|
||||
// ImageTypeScreen is a screen image that is not on an atlas.
|
||||
// A screen image is also unmanaged.
|
||||
ImageTypeScreen
|
||||
|
||||
// ImageTypeVolatile is a volatile image that is cleared every frame.
|
||||
// A volatile image is also unmanaged.
|
||||
ImageTypeVolatile
|
||||
|
||||
// ImageTypeUnmanaged is an unmanaged image that is not on an atlas.
|
||||
ImageTypeUnmanaged
|
||||
)
|
||||
|
||||
// Image is a rectangle pixel set that might be on an atlas.
|
||||
type Image struct {
|
||||
*imageImpl
|
||||
|
||||
cleanup runtime.Cleanup
|
||||
}
|
||||
|
||||
type imageImpl struct {
|
||||
width int
|
||||
height int
|
||||
imageType ImageType
|
||||
@@ -250,13 +187,15 @@ type Image struct {
|
||||
// usedAsSourceCount represents how long the image is used as a rendering source and kept not modified with
|
||||
// DrawTriangles.
|
||||
// In the current implementation, if an image is being modified by DrawTriangles, the image is separated from
|
||||
// a graphicscommand.Image on an atlas by ensureIsolatedFromSource.
|
||||
// a restorable image on an atlas by ensureIsolatedFromSource.
|
||||
//
|
||||
// The type is int64 instead of int to avoid overflow when comparing the limitation.
|
||||
//
|
||||
// usedAsSourceCount is increased if the image is used as a rendering source, or set to 0 if the image is
|
||||
// modified.
|
||||
//
|
||||
// WritePixels doesn't affect this value since WritePixels can be done on images on an atlas.
|
||||
usedAsSourceCount int
|
||||
usedAsSourceCount int64
|
||||
|
||||
// usedAsDestinationCount represents how many times an image is used as a rendering destination at DrawTriangles.
|
||||
// usedAsDestinationCount affects the calculation when to put the image onto a texture atlas again.
|
||||
@@ -270,15 +209,21 @@ type Image struct {
|
||||
//
|
||||
// moveTo is similar to C++'s move semantics.
|
||||
func (i *Image) moveTo(dst *Image) {
|
||||
dst.deallocate()
|
||||
*dst = *i
|
||||
dst.deallocateImpl()
|
||||
dst.cleanup.Stop()
|
||||
|
||||
impl := *i.imageImpl
|
||||
dst.imageImpl = &impl
|
||||
if dst.backend != nil {
|
||||
dst.cleanup = runtime.AddCleanup(dst, (*imageImpl).cleanup, dst.imageImpl)
|
||||
}
|
||||
|
||||
// i is no longer available but the finalizer must not be called
|
||||
// since i and dst share the same backend and the same node.
|
||||
runtime.SetFinalizer(i, nil)
|
||||
i.cleanup.Stop()
|
||||
}
|
||||
|
||||
func (i *Image) isOnAtlas() bool {
|
||||
func (i *imageImpl) isOnAtlas() bool {
|
||||
return i.node != nil
|
||||
}
|
||||
|
||||
@@ -294,7 +239,7 @@ func (i *Image) resetUsedAsSourceCount() {
|
||||
imagesToPutOnSourceBackend.remove(i)
|
||||
}
|
||||
|
||||
func (i *Image) paddingSize() int {
|
||||
func (i *imageImpl) paddingSize() int {
|
||||
if i.imageType == ImageTypeRegular {
|
||||
return 1
|
||||
}
|
||||
@@ -351,15 +296,16 @@ func (i *Image) ensureIsolatedFromSource(backends []*backend) {
|
||||
|
||||
w, h := float32(i.width), float32(i.height)
|
||||
vs := make([]float32, 4*graphics.VertexFloatCount)
|
||||
graphics.QuadVertices(vs, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
|
||||
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, w, h, 0, 0, w, h, 1, 1, 1, 1)
|
||||
is := graphics.QuadIndices()
|
||||
dr := image.Rect(0, 0, i.width, i.height)
|
||||
sr := image.Rect(0, 0, i.width, i.height)
|
||||
|
||||
newI.drawTriangles([graphics.ShaderImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillAll)
|
||||
newI.drawTriangles([graphics.ShaderSrcImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, NearestFilterShader, nil, graphicsdriver.FillRuleFillAll, restorable.HintOverwriteDstRegion)
|
||||
newI.moveTo(i)
|
||||
}
|
||||
|
||||
func (i *Image) putOnSourceBackend(graphicsDriver graphicsdriver.Graphics) {
|
||||
func (i *Image) putOnSourceBackend() {
|
||||
if i.backend == nil {
|
||||
i.allocate(nil, true)
|
||||
return
|
||||
@@ -382,10 +328,11 @@ func (i *Image) putOnSourceBackend(graphicsDriver graphicsdriver.Graphics) {
|
||||
|
||||
w, h := float32(i.width), float32(i.height)
|
||||
vs := make([]float32, 4*graphics.VertexFloatCount)
|
||||
graphics.QuadVertices(vs, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
|
||||
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, w, h, 0, 0, w, h, 1, 1, 1, 1)
|
||||
is := graphics.QuadIndices()
|
||||
dr := image.Rect(0, 0, i.width, i.height)
|
||||
newI.drawTriangles([graphics.ShaderImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillAll)
|
||||
sr := image.Rect(0, 0, i.width, i.height)
|
||||
newI.drawTriangles([graphics.ShaderSrcImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, NearestFilterShader, nil, graphicsdriver.FillRuleFillAll, restorable.HintOverwriteDstRegion)
|
||||
|
||||
newI.moveTo(i)
|
||||
i.usedAsSourceCount = 0
|
||||
@@ -395,7 +342,7 @@ func (i *Image) putOnSourceBackend(graphicsDriver graphicsdriver.Graphics) {
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Image) regionWithPadding() image.Rectangle {
|
||||
func (i *imageImpl) regionWithPadding() image.Rectangle {
|
||||
if i.backend == nil {
|
||||
panic("atlas: backend must not be nil: not allocated yet?")
|
||||
}
|
||||
@@ -417,7 +364,7 @@ func (i *Image) regionWithPadding() image.Rectangle {
|
||||
// 5: Color G
|
||||
// 6: Color B
|
||||
// 7: Color Y
|
||||
func (i *Image) DrawTriangles(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) {
|
||||
func (i *Image) DrawTriangles(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 restorable.Hint) {
|
||||
backendsM.Lock()
|
||||
defer backendsM.Unlock()
|
||||
|
||||
@@ -430,19 +377,15 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
|
||||
copy(us, uniforms)
|
||||
|
||||
appendDeferred(func() {
|
||||
i.drawTriangles(srcs, vs, is, blend, dstRegion, srcRegions, shader, us, fillRule)
|
||||
i.drawTriangles(srcs, vs, is, blend, dstRegion, srcRegions, shader, us, fillRule, hint)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
i.drawTriangles(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule)
|
||||
i.drawTriangles(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule, hint)
|
||||
}
|
||||
|
||||
func (i *Image) drawTriangles(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 len(vertices) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
func (i *Image) drawTriangles(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 restorable.Hint) {
|
||||
backends := make([]*backend, 0, len(srcs))
|
||||
for _, src := range srcs {
|
||||
if src == nil {
|
||||
@@ -462,7 +405,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
|
||||
for _, src := range srcs {
|
||||
// Compare i and source images after ensuring i is not on an atlas, or
|
||||
// i and a source image might share the same atlas even though i != src.
|
||||
if src != nil && i.backend.image == src.backend.image {
|
||||
if src != nil && i.backend.restorable == src.backend.restorable {
|
||||
panic("atlas: Image.DrawTriangles: source must be different from the receiver")
|
||||
}
|
||||
}
|
||||
@@ -484,8 +427,8 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
|
||||
vertices[i+2] += oxf
|
||||
vertices[i+3] += oyf
|
||||
}
|
||||
if shader.ir.Unit == shaderir.Texels {
|
||||
sw, sh := srcs[0].backend.image.InternalSize()
|
||||
if shader.unit == shaderir.Texels {
|
||||
sw, sh := srcs[0].backend.restorable.InternalSize()
|
||||
swf, shf := float32(sw), float32(sh)
|
||||
for i := 0; i < n; i += graphics.VertexFloatCount {
|
||||
vertices[i+2] /= swf
|
||||
@@ -500,6 +443,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
|
||||
}
|
||||
}
|
||||
|
||||
var imgs [graphics.ShaderSrcImageCount]*restorable.Image
|
||||
for i, src := range srcs {
|
||||
if src == nil {
|
||||
continue
|
||||
@@ -507,33 +451,19 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
|
||||
|
||||
// A source region can be deliberately empty when this is not needed in order to avoid unexpected
|
||||
// performance issue (#1293).
|
||||
if srcRegions[i].Empty() {
|
||||
continue
|
||||
}
|
||||
|
||||
r := src.regionWithPadding()
|
||||
srcRegions[i] = srcRegions[i].Add(r.Min)
|
||||
}
|
||||
|
||||
var imgs [graphics.ShaderImageCount]*graphicscommand.Image
|
||||
for i, src := range srcs {
|
||||
if src == nil {
|
||||
continue
|
||||
}
|
||||
imgs[i] = src.backend.image
|
||||
}
|
||||
|
||||
i.backend.image.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader(), uniforms, fillRule)
|
||||
|
||||
for _, src := range srcs {
|
||||
if src == nil {
|
||||
continue
|
||||
// TODO: This should no longer be needed but is kept just in case. Remove this later.
|
||||
if !srcRegions[i].Empty() {
|
||||
r := src.regionWithPadding()
|
||||
srcRegions[i] = srcRegions[i].Add(r.Min)
|
||||
}
|
||||
imgs[i] = src.backend.restorable
|
||||
if !src.isOnSourceBackend() && src.canBePutOnAtlas() {
|
||||
// src might already registered, but assigning it again is not harmful.
|
||||
imagesToPutOnSourceBackend.add(src)
|
||||
}
|
||||
}
|
||||
|
||||
i.backend.restorable.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader(), uniforms, fillRule, hint)
|
||||
}
|
||||
|
||||
// WritePixels replaces the pixels on the image.
|
||||
@@ -575,7 +505,7 @@ func (i *Image) writePixels(pix []byte, region image.Rectangle) {
|
||||
region = region.Add(r.Min)
|
||||
|
||||
if pix == nil {
|
||||
i.backend.clearPixels(region)
|
||||
i.backend.restorable.ClearPixels(region)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -583,7 +513,7 @@ func (i *Image) writePixels(pix []byte, region image.Rectangle) {
|
||||
pix2 := graphics.NewManagedBytes(len(pix), func(bs []byte) {
|
||||
copy(bs, pix)
|
||||
})
|
||||
i.backend.writePixels(pix2, region)
|
||||
i.backend.restorable.WritePixels(pix2, region)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -612,7 +542,7 @@ func (i *Image) writePixels(pix []byte, region image.Rectangle) {
|
||||
copy(bs[4*j*r.Dx():], pix[4*j*region.Dx():4*(j+1)*region.Dx()])
|
||||
}
|
||||
})
|
||||
i.backend.writePixels(pixb, r)
|
||||
i.backend.restorable.WritePixels(pixb, r)
|
||||
}
|
||||
|
||||
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) (ok bool, err error) {
|
||||
@@ -628,58 +558,45 @@ func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte
|
||||
// To prevent memory leaks, flush the deferred functions here.
|
||||
flushDeferred()
|
||||
|
||||
if err := i.readPixels(graphicsDriver, pixels, region); err != nil {
|
||||
if i.backend == nil || i.backend.restorable == nil {
|
||||
for i := range pixels {
|
||||
pixels[i] = 0
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if err := i.backend.restorable.ReadPixels(graphicsDriver, pixels, region.Add(i.regionWithPadding().Min)); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (i *Image) readPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) error {
|
||||
if i.backend == nil || i.backend.image == nil {
|
||||
for i := range pixels {
|
||||
pixels[i] = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := i.backend.image.ReadPixels(graphicsDriver, []graphicsdriver.PixelsArgs{
|
||||
{
|
||||
Pixels: pixels,
|
||||
Region: region.Add(i.regionWithPadding().Min),
|
||||
},
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Deallocate deallocates the internal state.
|
||||
// Even after this call, the image is still available as a new cleared image.
|
||||
func (i *Image) Deallocate() {
|
||||
i.cleanup.Stop()
|
||||
|
||||
backendsM.Lock()
|
||||
defer backendsM.Unlock()
|
||||
|
||||
if !inFrame {
|
||||
appendDeferred(func() {
|
||||
i.deallocate()
|
||||
runtime.SetFinalizer(i, nil)
|
||||
i.deallocateImpl()
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
i.deallocate()
|
||||
runtime.SetFinalizer(i, nil)
|
||||
i.deallocateImpl()
|
||||
}
|
||||
|
||||
func (i *Image) deallocate() {
|
||||
func (i *imageImpl) deallocateImpl() {
|
||||
defer func() {
|
||||
i.backend = nil
|
||||
i.node = nil
|
||||
}()
|
||||
|
||||
i.resetUsedAsSourceCount()
|
||||
i.usedAsSourceCount = 0
|
||||
i.usedAsDestinationCount = 0
|
||||
imagesUsedAsDestination.remove(i)
|
||||
|
||||
if i.backend == nil {
|
||||
// Not allocated yet.
|
||||
@@ -691,13 +608,12 @@ func (i *Image) deallocate() {
|
||||
if !i.backend.page.IsEmpty() {
|
||||
// As this part can be reused, this should be cleared explicitly.
|
||||
r := i.regionWithPadding()
|
||||
i.backend.clearPixels(r)
|
||||
i.backend.restorable.ClearPixels(r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
i.backend.image.Dispose()
|
||||
i.backend.image = nil
|
||||
i.backend.restorable.Dispose()
|
||||
|
||||
for idx, sh := range theBackends {
|
||||
if sh == i.backend {
|
||||
@@ -714,9 +630,11 @@ func (i *Image) deallocate() {
|
||||
func NewImage(width, height int, imageType ImageType) *Image {
|
||||
// Actual allocation is done lazily, and the lock is not needed.
|
||||
return &Image{
|
||||
width: width,
|
||||
height: height,
|
||||
imageType: imageType,
|
||||
imageImpl: &imageImpl{
|
||||
width: width,
|
||||
height: height,
|
||||
imageType: imageType,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -730,25 +648,20 @@ func (i *Image) canBePutOnAtlas() bool {
|
||||
return i.width+i.paddingSize() <= maxSize && i.height+i.paddingSize() <= maxSize
|
||||
}
|
||||
|
||||
func (i *Image) finalize() {
|
||||
func (i *imageImpl) cleanup() {
|
||||
// A function from finalizer must not be blocked, but disposing operation can be blocked.
|
||||
// Defer this operation until it becomes safe. (#913)
|
||||
appendDeferred(func() {
|
||||
i.deallocate()
|
||||
runtime.SetFinalizer(i, nil)
|
||||
i.deallocateImpl()
|
||||
})
|
||||
}
|
||||
|
||||
func (i *Image) allocate(forbiddenBackends []*backend, asSource bool) {
|
||||
if !graphicsDriverInitialized {
|
||||
panic("atlas: graphics driver must be ready at allocate but not")
|
||||
}
|
||||
|
||||
if i.backend != nil {
|
||||
panic("atlas: the image is already allocated")
|
||||
}
|
||||
|
||||
runtime.SetFinalizer(i, (*Image).finalize)
|
||||
i.cleanup = runtime.AddCleanup(i, (*imageImpl).cleanup, i.imageImpl)
|
||||
|
||||
if i.imageType == ImageTypeScreen {
|
||||
if asSource {
|
||||
@@ -756,9 +669,7 @@ func (i *Image) allocate(forbiddenBackends []*backend, asSource bool) {
|
||||
}
|
||||
// A screen image doesn't have a padding.
|
||||
i.backend = &backend{
|
||||
image: newClearedImage(i.width, i.height, true),
|
||||
width: i.width,
|
||||
height: i.height,
|
||||
restorable: restorable.NewImage(i.width, i.height, restorable.ImageTypeScreen),
|
||||
}
|
||||
theBackends = append(theBackends, i.backend)
|
||||
return
|
||||
@@ -772,11 +683,13 @@ func (i *Image) allocate(forbiddenBackends []*backend, asSource bool) {
|
||||
panic(fmt.Sprintf("atlas: the image being put on an atlas is too big: width: %d, height: %d", i.width, i.height))
|
||||
}
|
||||
|
||||
typ := restorable.ImageTypeRegular
|
||||
if i.imageType == ImageTypeVolatile {
|
||||
typ = restorable.ImageTypeVolatile
|
||||
}
|
||||
i.backend = &backend{
|
||||
image: newClearedImage(wp, hp, false),
|
||||
width: wp,
|
||||
height: hp,
|
||||
source: asSource && i.imageType == ImageTypeRegular,
|
||||
restorable: restorable.NewImage(wp, hp, typ),
|
||||
source: asSource && typ == restorable.ImageTypeRegular,
|
||||
}
|
||||
theBackends = append(theBackends, i.backend)
|
||||
return
|
||||
@@ -820,12 +733,14 @@ loop:
|
||||
height *= 2
|
||||
}
|
||||
|
||||
typ := restorable.ImageTypeRegular
|
||||
if i.imageType == ImageTypeVolatile {
|
||||
typ = restorable.ImageTypeVolatile
|
||||
}
|
||||
b := &backend{
|
||||
image: newClearedImage(width, height, false),
|
||||
width: width,
|
||||
height: height,
|
||||
page: packing.NewPage(width, height, maxSize),
|
||||
source: asSource,
|
||||
restorable: restorable.NewImage(width, height, typ),
|
||||
page: packing.NewPage(width, height, maxSize),
|
||||
source: asSource,
|
||||
}
|
||||
theBackends = append(theBackends, b)
|
||||
|
||||
@@ -845,7 +760,7 @@ func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, path stri
|
||||
panic("atlas: DumpScreenshots must be called in between BeginFrame and EndFrame")
|
||||
}
|
||||
|
||||
return i.backend.image.Dump(graphicsDriver, path, blackbg, image.Rect(0, 0, i.width, i.height))
|
||||
return i.backend.restorable.Dump(graphicsDriver, path, blackbg, image.Rect(0, 0, i.width, i.height))
|
||||
}
|
||||
|
||||
func EndFrame() error {
|
||||
@@ -876,18 +791,7 @@ func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error {
|
||||
}
|
||||
}()
|
||||
|
||||
if debug.IsDebug {
|
||||
debug.Logf("Internal image sizes:\n")
|
||||
imgs := make([]*graphicscommand.Image, 0, len(theBackends))
|
||||
for _, backend := range theBackends {
|
||||
if backend.image == nil {
|
||||
continue
|
||||
}
|
||||
imgs = append(imgs, backend.image)
|
||||
}
|
||||
graphicscommand.LogImagesInfo(imgs)
|
||||
}
|
||||
if err := graphicscommand.FlushCommands(graphicsDriver, true); err != nil {
|
||||
if err := restorable.SwapBuffers(graphicsDriver); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -912,7 +816,7 @@ func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
|
||||
|
||||
var err error
|
||||
initOnce.Do(func() {
|
||||
err = graphicscommand.InitializeGraphicsDriverState(graphicsDriver)
|
||||
err = restorable.InitializeGraphicsDriverState(graphicsDriver)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -928,17 +832,20 @@ func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
|
||||
minDestinationSize = 16
|
||||
}
|
||||
if maxSize == 0 {
|
||||
maxSize = floorPowerOf2(graphicscommand.MaxImageSize(graphicsDriver))
|
||||
maxSize = floorPowerOf2(restorable.MaxImageSize(graphicsDriver))
|
||||
}
|
||||
|
||||
graphicsDriverInitialized = true
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Restore images first before other image manipulations (#2075).
|
||||
if err := restorable.RestoreIfNeeded(graphicsDriver); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
flushDeferred()
|
||||
putImagesOnSourceBackend(graphicsDriver)
|
||||
putImagesOnSourceBackend()
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -951,12 +858,20 @@ func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, err
|
||||
panic("atlas: DumpImages must be called in between BeginFrame and EndFrame")
|
||||
}
|
||||
|
||||
images := make([]*graphicscommand.Image, 0, len(theBackends))
|
||||
for _, backend := range theBackends {
|
||||
if backend.image == nil {
|
||||
return restorable.DumpImages(graphicsDriver, dir)
|
||||
}
|
||||
|
||||
func TotalGPUImageMemoryUsageInBytes() int64 {
|
||||
backendsM.Lock()
|
||||
defer backendsM.Unlock()
|
||||
|
||||
var sum int64
|
||||
for _, b := range theBackends {
|
||||
if b.restorable == nil {
|
||||
continue
|
||||
}
|
||||
images = append(images, backend.image)
|
||||
w, h := b.restorable.InternalSize()
|
||||
sum += 4 * int64(w) * int64(h)
|
||||
}
|
||||
return graphicscommand.DumpImages(images, graphicsDriver, dir)
|
||||
return sum
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user