ebiten/internal/atlas/image.go

938 lines
25 KiB
Go
Raw Normal View History

// Copyright 2018 The Ebiten 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.
package atlas
import (
2019-02-07 09:19:24 +01:00
"fmt"
"image"
2023-08-06 06:46:41 +02:00
"math"
"math/bits"
2018-03-10 16:07:32 +01:00
"runtime"
"sync"
"github.com/hajimehoshi/ebiten/v2/internal/debug"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2/internal/packing"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
2019-06-23 04:32:43 +02:00
var (
minSourceSize = 0
minDestinationSize = 0
maxSize = 0
2019-06-23 04:32:43 +02:00
)
func min(a, b int) int {
if a < b {
return a
}
return b
}
func appendDeferred(f func()) {
deferredM.Lock()
defer deferredM.Unlock()
deferred = append(deferred, f)
}
func flushDeferred() {
deferredM.Lock()
fs := deferred
deferred = nil
deferredM.Unlock()
for _, f := range fs {
f()
}
}
// baseCountToPutOnSourceBackend represents the base time duration when the image can be put onto an atlas.
// Actual time duration is increased in an exponential way for each usage as a rendering target.
const baseCountToPutOnSourceBackend = 10
2024-04-29 16:34:37 +02:00
func putImagesOnSourceBackend() {
// The counter usedAsDestinationCount is updated at most once per frame (#2676).
imagesUsedAsDestination.forEach(func(i *Image) {
// This counter is not updated when the backend is created in this frame.
if !i.backendCreatedInThisFrame && i.usedAsDestinationCount < math.MaxInt {
2023-08-06 06:46:41 +02:00
i.usedAsDestinationCount++
}
i.backendCreatedInThisFrame = false
})
imagesUsedAsDestination.clear()
imagesToPutOnSourceBackend.forEach(func(i *Image) {
2023-08-06 06:46:41 +02:00
if i.usedAsSourceCount < math.MaxInt {
i.usedAsSourceCount++
}
2024-06-30 15:38:25 +02:00
if i.usedAsSourceCount >= baseCountToPutOnSourceBackend*(1<<uint(min(i.usedAsDestinationCount, 31))) {
2024-04-29 16:34:37 +02:00
i.putOnSourceBackend()
i.usedAsSourceCount = 0
}
})
imagesToPutOnSourceBackend.clear()
}
2024-06-30 15:22:27 +02:00
// backend is a big texture atlas that can have multiple images.
// backend is a texture in GPU.
2018-03-10 16:02:23 +01:00
type backend struct {
// image is an atlas on which there might be multiple images.
image *graphicscommand.Image
2018-03-15 17:21:33 +01:00
width int
height int
// 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.
2018-03-15 17:21:33 +01:00
page *packing.Page
// source reports whether this backend is mainly used a rendering source, but this is not 100%.
2024-06-30 15:22:27 +02:00
//
// 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
// sourceInThisFrame reports whether this backend is used as a source in this frame.
// sourceInThisFrame is reset every frame.
sourceInThisFrame bool
}
func (b *backend) tryAlloc(width, height int) (*packing.Node, bool) {
if b.page == nil {
return nil, false
}
n := b.page.Alloc(width, height)
if n == nil {
// The page can't be extended anymore. Return as failure.
return nil, false
}
b.extendIfNeeded(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)
srcs := [graphics.ShaderSrcImageCount]*graphicscommand.Image{b.image}
sw, sh := b.image.InternalSize()
vs := make([]float32, 4*graphics.VertexFloatCount)
graphics.QuadVerticesFromDstAndSrc(vs, 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.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader.ensureShader(), nil, graphicsdriver.FillRuleFillAll)
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 := make([]float32, 4*graphics.VertexFloatCount)
graphics.QuadVerticesFromDstAndSrc(vs, 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.ShaderSrcImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendClear, region, [graphics.ShaderSrcImageCount]image.Rectangle{}, clearShader.ensureShader(), nil, graphicsdriver.FillRuleFillAll)
}
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
// inFrame indicates whether the current state is in between BeginFrame and EndFrame or not.
// If inFrame is false, function calls on an image should be deferred until the next BeginFrame.
inFrame bool
initOnce sync.Once
// theBackends is a set of atlases.
theBackends []*backend
imagesToPutOnSourceBackend smallImageSet
imagesUsedAsDestination smallImageSet
graphicsDriverInitialized bool
deferred []func()
// deferredM is a mutex for the slice operations. This must not be used for other usages.
deferredM sync.Mutex
)
2024-06-30 15:22:27 +02:00
// ImageType represents the type of an image.
type ImageType int
const (
2024-06-30 15:22:27 +02:00
// ImageTypeRegular is a regular image, that can be on a big texture atlas (backend).
ImageTypeRegular ImageType = iota
2024-06-30 15:22:27 +02:00
// ImageTypeScreen is a screen image that is not on an atlas.
// A screen image is also unmanaged.
ImageTypeScreen
2024-06-30 15:22:27 +02:00
// ImageTypeUnmanaged is an unmanaged image that is not on an atlas.
ImageTypeUnmanaged
)
2022-01-08 15:33:59 +01:00
// Image is a rectangle pixel set that might be on an atlas.
2018-03-10 16:05:06 +01:00
type Image struct {
width int
height int
imageType ImageType
backend *backend
backendCreatedInThisFrame bool
node *packing.Node
// 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.
//
2024-06-30 15:38:25 +02:00
// 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.
2024-06-30 15:38:25 +02:00
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.
//
// usedAsDestinationCount is never reset.
usedAsDestinationCount int
}
2021-03-11 16:26:38 +01:00
// moveTo moves its content to the given image dst.
// After moveTo is called, the image i is no longer available.
//
// moveTo is similar to C++'s move semantics.
2018-07-21 23:27:57 +02:00
func (i *Image) moveTo(dst *Image) {
2023-11-03 06:09:44 +01:00
dst.deallocate()
2018-07-21 23:27:57 +02:00
*dst = *i
// i is no longer available but the finalizer must not be called
// since i and dst share the same backend and the same node.
2018-07-21 23:27:57 +02:00
runtime.SetFinalizer(i, nil)
}
func (i *Image) isOnAtlas() bool {
2018-06-25 18:59:12 +02:00
return i.node != nil
}
func (i *Image) isOnSourceBackend() bool {
if i.backend == nil {
return false
}
return i.backend.source
}
func (i *Image) resetUsedAsSourceCount() {
i.usedAsSourceCount = 0
imagesToPutOnSourceBackend.remove(i)
}
func (i *Image) paddingSize() int {
if i.imageType == ImageTypeRegular {
return 1
}
return 0
}
func (i *Image) ensureIsolatedFromSource(backends []*backend) {
i.resetUsedAsSourceCount()
2020-11-05 17:45:04 +01:00
// imagesUsedAsDestination affects the counter usedAsDestination.
// The larger this counter is, the harder it is for the image to be transferred to the source backend.
imagesUsedAsDestination.add(i)
if i.backend == nil {
// `sourceInThisFrame` of `backends` should be true, so `backends` should be in `bs`.
var bs []*backend
for _, b := range theBackends {
if b.sourceInThisFrame {
bs = append(bs, b)
}
}
i.allocate(bs, false)
i.backendCreatedInThisFrame = true
return
}
if !i.isOnAtlas() {
return
}
// Check if i has the same backend as the given backends.
var needsIsolation bool
for _, b := range backends {
if i.backend == b {
needsIsolation = true
break
}
}
if !needsIsolation {
return
}
newI := NewImage(i.width, i.height, i.imageType)
// Call allocate explicitly in order to have an isolated backend from the specified backends.
// `sourceInThisFrame` of `backends` should be true, so `backends` should be in `bs`.
bs := []*backend{i.backend}
for _, b := range theBackends {
if b.sourceInThisFrame {
bs = append(bs, b)
}
}
newI.allocate(bs, false)
w, h := float32(i.width), float32(i.height)
vs := make([]float32, 4*graphics.VertexFloatCount)
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.ShaderSrcImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillRuleFillAll)
newI.moveTo(i)
}
2024-04-29 16:34:37 +02:00
func (i *Image) putOnSourceBackend() {
if i.backend == nil {
i.allocate(nil, true)
2023-08-06 09:03:57 +02:00
return
}
if i.isOnSourceBackend() {
2023-08-06 09:03:57 +02:00
return
}
if !i.canBePutOnAtlas() {
panic("atlas: putOnSourceBackend cannot be called on a image that cannot be on an atlas")
}
2022-06-09 19:38:30 +02:00
if i.imageType != ImageTypeRegular {
panic(fmt.Sprintf("atlas: the image type must be ImageTypeRegular but %d", i.imageType))
}
newI := NewImage(i.width, i.height, ImageTypeRegular)
newI.allocate(nil, true)
w, h := float32(i.width), float32(i.height)
vs := make([]float32, 4*graphics.VertexFloatCount)
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.ShaderSrcImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillRuleFillAll)
2018-07-21 23:27:57 +02:00
newI.moveTo(i)
i.usedAsSourceCount = 0
if !i.isOnSourceBackend() {
panic("atlas: i must be on a source backend but not")
}
}
func (i *Image) regionWithPadding() image.Rectangle {
if i.backend == nil {
panic("atlas: backend must not be nil: not allocated yet?")
}
if !i.isOnAtlas() {
return image.Rect(0, 0, i.width+i.paddingSize(), i.height+i.paddingSize())
}
return i.node.Region()
}
2019-09-20 19:35:18 +02:00
// DrawTriangles draws triangles with the given image.
//
// The vertex floats are:
//
2022-08-03 13:48:02 +02:00
// 0: Destination X in pixels
// 1: Destination Y in pixels
// 2: Source X in pixels (the upper-left is (0, 0))
// 3: Source Y in pixels
// 4: Color R [0.0-1.0]
// 5: Color G
// 6: Color B
// 7: Color Y
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) {
backendsM.Lock()
defer backendsM.Unlock()
if !inFrame {
vs := make([]float32, len(vertices))
copy(vs, vertices)
is := make([]uint32, len(indices))
copy(is, indices)
us := make([]uint32, len(uniforms))
copy(us, uniforms)
appendDeferred(func() {
i.drawTriangles(srcs, vs, is, blend, dstRegion, srcRegions, shader, us, fillRule)
})
return
}
i.drawTriangles(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, 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) {
if len(vertices) == 0 {
return
}
// This slice is not escaped to the heap. This can be checked by `go build -gcflags=-m`.
backends := make([]*backend, 0, len(srcs))
for _, src := range srcs {
if src == nil {
continue
}
if src.backend == nil {
// It is possible to spcify i.backend as a forbidden backend, but this might prevent a good allocation for a source image.
// If the backend becomes the same as i's, i's backend will be changed at ensureIsolatedFromSource.
src.allocate(nil, true)
}
backends = append(backends, src.backend)
src.backend.sourceInThisFrame = true
}
i.ensureIsolatedFromSource(backends)
2018-03-10 15:48:10 +01:00
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 {
panic("atlas: Image.DrawTriangles: source must be different from the receiver")
}
2018-03-10 15:48:10 +01:00
}
r := i.regionWithPadding()
// TODO: Check if dstRegion does not to violate the region.
dstRegion = dstRegion.Add(r.Min)
dx, dy := float32(r.Min.X), float32(r.Min.Y)
var oxf, oyf float32
if srcs[0] != nil {
r := srcs[0].regionWithPadding()
oxf, oyf = float32(r.Min.X), float32(r.Min.Y)
n := len(vertices)
for i := 0; i < n; i += graphics.VertexFloatCount {
vertices[i] += dx
vertices[i+1] += dy
vertices[i+2] += oxf
vertices[i+3] += oyf
}
if shader.ir.Unit == shaderir.Texels {
sw, sh := srcs[0].backend.image.InternalSize()
swf, shf := float32(sw), float32(sh)
for i := 0; i < n; i += graphics.VertexFloatCount {
vertices[i+2] /= swf
vertices[i+3] /= shf
}
}
} else {
n := len(vertices)
for i := 0; i < n; i += graphics.VertexFloatCount {
vertices[i] += dx
vertices[i+1] += dy
}
}
for i, src := range srcs {
if src == nil {
continue
}
// 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.ShaderSrcImageCount]*graphicscommand.Image
for i, src := range srcs {
if src == nil {
continue
}
imgs[i] = src.backend.image
2020-05-26 15:26:55 +02:00
}
i.backend.image.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader(), uniforms, fillRule)
for _, src := range srcs {
if src == nil {
continue
}
if !src.isOnSourceBackend() && src.canBePutOnAtlas() {
// src might already registered, but assigning it again is not harmful.
imagesToPutOnSourceBackend.add(src)
}
}
}
// WritePixels replaces the pixels on the image.
2023-04-27 17:55:10 +02:00
func (i *Image) WritePixels(pix []byte, region image.Rectangle) {
backendsM.Lock()
defer backendsM.Unlock()
if !inFrame {
copied := make([]byte, len(pix))
copy(copied, pix)
appendDeferred(func() {
i.writePixels(copied, region)
})
return
}
2023-04-27 17:55:10 +02:00
i.writePixels(pix, region)
}
func (i *Image) writePixels(pix []byte, region image.Rectangle) {
if l := 4 * region.Dx() * region.Dy(); len(pix) != l {
panic(fmt.Sprintf("atlas: len(p) must be %d but %d", l, len(pix)))
}
i.resetUsedAsSourceCount()
if i.backend == nil {
2020-06-14 08:29:04 +02:00
if pix == nil {
return
}
// Allocate as a source as this image will likely be used as a source.
i.allocate(nil, true)
}
r := i.regionWithPadding()
2023-04-29 06:35:56 +02:00
if !region.Eq(image.Rect(0, 0, i.width, i.height)) || i.paddingSize() == 0 {
region = region.Add(r.Min)
if pix == nil {
i.backend.clearPixels(region)
return
}
// Copy pixels in the case when pix is modified before the graphics command is executed.
pix2 := graphics.NewManagedBytes(len(pix), func(bs []byte) {
copy(bs, pix)
})
i.backend.writePixels(pix2, region)
return
}
// TODO: These loops assume that paddingSize is 1.
// TODO: Is clearing edges explicitly really needed?
const paddingSize = 1
if paddingSize != i.paddingSize() {
panic(fmt.Sprintf("atlas: writePixels assumes the padding is always 1 but the actual padding was %d", i.paddingSize()))
}
pixb := graphics.NewManagedBytes(4*r.Dx()*r.Dy(), func(bs []byte) {
// Clear the edges. bs might not be zero-cleared.
rowPixels := 4 * r.Dx()
for i := 0; i < rowPixels; i++ {
bs[rowPixels*(r.Dy()-1)+i] = 0
}
for j := 1; j < r.Dy(); j++ {
bs[rowPixels*j-4] = 0
bs[rowPixels*j-3] = 0
bs[rowPixels*j-2] = 0
bs[rowPixels*j-1] = 0
}
// Copy the content.
for j := 0; j < region.Dy(); j++ {
copy(bs[4*j*r.Dx():], pix[4*j*region.Dx():4*(j+1)*region.Dx()])
}
})
i.backend.writePixels(pixb, r)
}
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) (ok bool, err error) {
backendsM.Lock()
defer backendsM.Unlock()
if !inFrame {
// Not ready to read pixels. Try this later.
return false, nil
}
// In the tests, BeginFrame might not be called often and then images might not be disposed (#2292).
// To prevent memory leaks, flush the deferred functions here.
flushDeferred()
if err := i.readPixels(graphicsDriver, pixels, region); 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 {
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() {
2023-11-03 06:52:19 +01:00
backendsM.Lock()
defer backendsM.Unlock()
if !inFrame {
appendDeferred(func() {
i.deallocate()
runtime.SetFinalizer(i, nil)
})
return
}
2023-11-03 06:52:19 +01:00
i.deallocate()
runtime.SetFinalizer(i, nil)
}
2023-11-03 06:09:44 +01:00
func (i *Image) deallocate() {
defer func() {
2018-04-08 19:18:46 +02:00
i.backend = nil
i.node = nil
}()
i.resetUsedAsSourceCount()
2023-11-03 06:09:44 +01:00
i.usedAsDestinationCount = 0
imagesUsedAsDestination.remove(i)
if i.backend == nil {
// Not allocated yet.
return
}
if i.isOnAtlas() {
i.backend.page.Free(i.node)
if !i.backend.page.IsEmpty() {
// As this part can be reused, this should be cleared explicitly.
r := i.regionWithPadding()
i.backend.clearPixels(r)
return
}
}
i.backend.image.Dispose()
i.backend.image = nil
2018-04-08 19:18:46 +02:00
for idx, sh := range theBackends {
if sh == i.backend {
2024-01-08 20:31:23 +01:00
copy(theBackends[idx:], theBackends[idx+1:])
theBackends[len(theBackends)-1] = nil
theBackends = theBackends[:len(theBackends)-1]
return
}
}
2024-01-08 20:31:23 +01:00
panic("atlas: backend not found at an image being deallocated")
}
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,
}
}
func (i *Image) canBePutOnAtlas() bool {
if minSourceSize == 0 || minDestinationSize == 0 || maxSize == 0 {
panic("atlas: min*Size or maxSize must be initialized")
}
if i.imageType != ImageTypeRegular {
return false
}
return i.width+i.paddingSize() <= maxSize && i.height+i.paddingSize() <= maxSize
}
func (i *Image) finalize() {
// 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)
})
}
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")
2018-04-29 12:10:36 +02:00
}
runtime.SetFinalizer(i, (*Image).finalize)
2019-09-21 12:15:49 +02:00
2023-04-29 06:35:56 +02:00
wp := i.width + i.paddingSize()
hp := i.height + i.paddingSize()
if !i.canBePutOnAtlas() {
2023-04-29 06:35:56 +02:00
if wp > maxSize || hp > maxSize {
panic(fmt.Sprintf("atlas: the image being put on an atlas is too big: width: %d, height: %d", i.width, i.height))
}
i.backend = &backend{
2024-06-30 12:21:42 +02:00
image: newClearedImage(wp, hp, i.imageType == ImageTypeScreen),
width: wp,
height: hp,
source: asSource && i.imageType == ImageTypeRegular,
}
theBackends = append(theBackends, i.backend)
return
}
// Check if an existing backend is available.
loop:
2018-03-23 20:27:10 +01:00
for _, b := range theBackends {
if b.source != asSource {
continue
}
for _, bb := range forbiddenBackends {
if b == bb {
continue loop
}
}
2023-04-29 06:35:56 +02:00
if n, ok := b.tryAlloc(wp, hp); ok {
i.backend = b
i.node = n
return
}
}
var width, height int
if asSource {
width, height = minSourceSize, minSourceSize
} else {
width, height = minDestinationSize, minDestinationSize
}
2023-04-29 06:35:56 +02:00
for wp > width {
if width == maxSize {
panic(fmt.Sprintf("atlas: the image being put on an atlas is too big: width: %d, height: %d", i.width, i.height))
}
width *= 2
}
2023-04-29 06:35:56 +02:00
for hp > height {
if height == maxSize {
panic(fmt.Sprintf("atlas: the image being put on an atlas is too big: width: %d, height: %d", i.width, i.height))
}
height *= 2
}
2018-03-23 20:27:10 +01:00
b := &backend{
image: newClearedImage(width, height, false),
width: width,
height: height,
page: packing.NewPage(width, height, maxSize),
source: asSource,
2018-03-06 18:18:08 +01:00
}
2018-03-23 20:27:10 +01:00
theBackends = append(theBackends, b)
2023-04-29 06:35:56 +02:00
n := b.page.Alloc(wp, hp)
if n == nil {
panic("atlas: Alloc result must not be nil at allocate")
}
i.backend = b
i.node = n
}
2018-03-10 15:48:10 +01:00
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool) (string, error) {
2019-07-19 18:42:19 +02:00
backendsM.Lock()
defer backendsM.Unlock()
if !inFrame {
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))
2019-07-19 18:42:19 +02:00
}
func EndFrame() error {
backendsM.Lock()
defer backendsM.Unlock()
defer func() {
inFrame = false
}()
2019-08-25 17:32:30 +02:00
if !inFrame {
panic("atlas: inFrame must be true in EndFrame")
}
for _, b := range theBackends {
b.sourceInThisFrame = false
}
return nil
}
func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error {
func() {
backendsM.Lock()
defer backendsM.Unlock()
if inFrame {
panic("atlas: inFrame must be false in SwapBuffer")
}
}()
if debug.IsDebug {
debug.FrameLogf("Internal image sizes:\n")
imgs := make([]*graphicscommand.Image, 0, len(theBackends))
for _, backend := range theBackends {
imgs = append(imgs, backend.image)
}
graphicscommand.LogImagesInfo(imgs)
}
if err := graphicscommand.FlushCommands(graphicsDriver, true); err != nil {
return err
}
return nil
}
func floorPowerOf2(x int) int {
if x <= 0 {
return 0
}
return 1 << (bits.Len(uint(x)) - 1)
}
func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
backendsM.Lock()
2019-08-25 17:32:30 +02:00
defer backendsM.Unlock()
if inFrame {
panic("atlas: inFrame must be false in BeginFrame")
}
inFrame = true
var err error
initOnce.Do(func() {
err = graphicscommand.InitializeGraphicsDriverState(graphicsDriver)
if err != nil {
return
}
if len(theBackends) != 0 {
panic("atlas: all the images must be not on an atlas before the game starts")
}
// min*Size and maxSize can already be set for testings.
if minSourceSize == 0 {
minSourceSize = 1024
}
if minDestinationSize == 0 {
minDestinationSize = 16
}
if maxSize == 0 {
maxSize = floorPowerOf2(graphicscommand.MaxImageSize(graphicsDriver))
}
graphicsDriverInitialized = true
})
if err != nil {
return err
}
flushDeferred()
2024-04-29 16:34:37 +02:00
putImagesOnSourceBackend()
return nil
}
func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, error) {
backendsM.Lock()
defer backendsM.Unlock()
if !inFrame {
panic("atlas: DumpImages must be called in between BeginFrame and EndFrame")
}
images := make([]*graphicscommand.Image, 0, len(theBackends))
for _, backend := range theBackends {
images = append(images, backend.image)
}
return graphicscommand.DumpImages(images, graphicsDriver, dir)
}