2018-03-03 10:51:52 +01:00
|
|
|
// 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.
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
package atlas
|
2018-03-03 10:51:52 +01:00
|
|
|
|
|
|
|
import (
|
2019-02-07 09:19:24 +01:00
|
|
|
"fmt"
|
2021-07-29 09:09:25 +02:00
|
|
|
"image"
|
2023-08-06 06:46:41 +02:00
|
|
|
"math"
|
2024-02-24 18:06:52 +01:00
|
|
|
"math/bits"
|
2018-03-10 16:07:32 +01:00
|
|
|
"runtime"
|
2018-05-09 16:41:06 +02:00
|
|
|
"sync"
|
2018-03-10 15:27:16 +01:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2024-01-08 20:50:19 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/packing"
|
2024-09-06 04:15:54 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
|
2023-04-16 11:56:14 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2018-03-03 10:51:52 +01:00
|
|
|
)
|
|
|
|
|
2019-06-23 04:32:43 +02:00
|
|
|
var (
|
2023-02-23 09:21:17 +01:00
|
|
|
minSourceSize = 0
|
|
|
|
minDestinationSize = 0
|
|
|
|
maxSize = 0
|
2019-06-23 04:32:43 +02:00
|
|
|
)
|
|
|
|
|
2021-06-19 11:52:05 +02:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2024-03-26 04:59:31 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
// baseCountToPutOnSourceBackend represents the base time duration when the image can be put onto an atlas.
|
2023-01-28 11:06:38 +01:00
|
|
|
// Actual time duration is increased in an exponential way for each usage as a rendering target.
|
2023-02-23 09:21:17 +01:00
|
|
|
const baseCountToPutOnSourceBackend = 10
|
2021-03-09 18:29:56 +01:00
|
|
|
|
2024-04-29 16:34:37 +02:00
|
|
|
func putImagesOnSourceBackend() {
|
2023-08-06 06:27:28 +02:00
|
|
|
// The counter usedAsDestinationCount is updated at most once per frame (#2676).
|
2023-08-19 07:57:55 +02:00
|
|
|
imagesUsedAsDestination.forEach(func(i *Image) {
|
2023-08-18 20:25:17 +02:00
|
|
|
// This counter is not updated when the backend is created in this frame.
|
2023-08-18 22:20:39 +02:00
|
|
|
if !i.backendCreatedInThisFrame && i.usedAsDestinationCount < math.MaxInt {
|
2023-08-06 06:46:41 +02:00
|
|
|
i.usedAsDestinationCount++
|
|
|
|
}
|
2023-08-18 22:20:39 +02:00
|
|
|
i.backendCreatedInThisFrame = false
|
2023-08-19 07:57:55 +02:00
|
|
|
})
|
|
|
|
imagesUsedAsDestination.clear()
|
2023-08-06 06:27:28 +02:00
|
|
|
|
2023-08-19 07:57:55 +02:00
|
|
|
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()
|
2021-01-17 10:22:45 +01:00
|
|
|
i.usedAsSourceCount = 0
|
2019-05-11 16:16:05 +02:00
|
|
|
}
|
2023-08-19 07:57:55 +02:00
|
|
|
})
|
|
|
|
imagesToPutOnSourceBackend.clear()
|
2019-05-11 16:16:05 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-09-06 04:48:53 +02:00
|
|
|
// restorable is an atlas on which there might be multiple images.
|
|
|
|
restorable *restorable.Image
|
2018-03-15 17:21:33 +01:00
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
// 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
|
2023-02-23 09:21:17 +01:00
|
|
|
|
|
|
|
// source reports whether this backend is mainly used a rendering source, but this is not 100%.
|
2024-06-30 15:22:27 +02:00
|
|
|
//
|
2023-02-23 09:21:17 +01: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
|
2023-08-18 21:57:30 +02:00
|
|
|
|
|
|
|
// sourceInThisFrame reports whether this backend is used as a source in this frame.
|
|
|
|
// sourceInThisFrame is reset every frame.
|
|
|
|
sourceInThisFrame bool
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
func (b *backend) tryAlloc(width, height int) (*packing.Node, bool) {
|
2024-01-08 20:38:12 +01:00
|
|
|
if b.page == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2022-11-11 13:15:28 +01:00
|
|
|
n := b.page.Alloc(width, height)
|
|
|
|
if n == nil {
|
2023-01-28 11:06:38 +01:00
|
|
|
// The page can't be extended anymore. Return as failure.
|
2022-11-11 13:15:28 +01:00
|
|
|
return nil, false
|
2022-11-11 11:34:10 +01:00
|
|
|
}
|
2018-03-25 15:41:15 +02:00
|
|
|
|
2024-09-06 07:27:46 +02:00
|
|
|
b.restorable = b.restorable.Extend(b.page.Size())
|
2022-11-11 11:34:10 +01:00
|
|
|
|
2018-03-25 15:41:15 +02:00
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
|
2018-03-03 10:51:52 +01:00
|
|
|
var (
|
2018-03-10 16:28:30 +01:00
|
|
|
// backendsM is a mutex for critical sections of the backend and packing.Node objects.
|
|
|
|
backendsM sync.Mutex
|
|
|
|
|
2023-10-18 16:18:08 +02:00
|
|
|
// 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
|
|
|
|
|
2019-08-23 18:06:14 +02:00
|
|
|
initOnce sync.Once
|
2019-08-12 17:18:49 +02:00
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
// theBackends is a set of atlases.
|
2023-02-23 09:21:17 +01:00
|
|
|
theBackends []*backend
|
|
|
|
|
2023-08-19 07:57:55 +02:00
|
|
|
imagesToPutOnSourceBackend smallImageSet
|
2019-08-12 12:37:20 +02:00
|
|
|
|
2023-08-19 07:57:55 +02:00
|
|
|
imagesUsedAsDestination smallImageSet
|
2024-01-13 06:05:19 +01:00
|
|
|
|
2024-03-26 04:59:31 +01:00
|
|
|
deferred []func()
|
|
|
|
|
|
|
|
// deferredM is a mutex for the slice operations. This must not be used for other usages.
|
|
|
|
deferredM sync.Mutex
|
2018-03-03 10:51:52 +01:00
|
|
|
)
|
|
|
|
|
2024-06-30 15:22:27 +02:00
|
|
|
// ImageType represents the type of an image.
|
2022-06-07 16:57:56 +02:00
|
|
|
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).
|
2022-06-07 16:57:56 +02:00
|
|
|
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.
|
2022-06-07 16:57:56 +02:00
|
|
|
ImageTypeScreen
|
2024-06-30 15:22:27 +02:00
|
|
|
|
2024-09-06 08:23:40 +02:00
|
|
|
ImageTypeVolatile
|
|
|
|
|
2024-06-30 15:22:27 +02:00
|
|
|
// ImageTypeUnmanaged is an unmanaged image that is not on an atlas.
|
2022-06-09 19:11:08 +02:00
|
|
|
ImageTypeUnmanaged
|
2022-06-07 16:57:56 +02:00
|
|
|
)
|
|
|
|
|
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 {
|
2022-06-07 16:57:56 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
imageType ImageType
|
2018-04-29 11:51:48 +02:00
|
|
|
|
2023-08-18 22:20:39 +02:00
|
|
|
backend *backend
|
|
|
|
backendCreatedInThisFrame bool
|
2018-03-10 15:14:59 +01:00
|
|
|
|
2019-05-11 16:16:05 +02:00
|
|
|
node *packing.Node
|
|
|
|
|
2021-01-17 10:22:45 +01:00
|
|
|
// usedAsSourceCount represents how long the image is used as a rendering source and kept not modified with
|
|
|
|
// DrawTriangles.
|
2019-05-11 16:16:05 +02:00
|
|
|
// In the current implementation, if an image is being modified by DrawTriangles, the image is separated from
|
2024-09-06 04:48:53 +02:00
|
|
|
// a restorable image on an atlas by ensureIsolatedFromSource.
|
2019-05-11 16:16:05 +02:00
|
|
|
//
|
2024-06-30 15:38:25 +02:00
|
|
|
// The type is int64 instead of int to avoid overflow when comparing the limitation.
|
|
|
|
//
|
2021-01-17 10:22:45 +01:00
|
|
|
// usedAsSourceCount is increased if the image is used as a rendering source, or set to 0 if the image is
|
2019-05-11 16:16:05 +02:00
|
|
|
// modified.
|
|
|
|
//
|
2022-08-07 20:24:46 +02:00
|
|
|
// 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
|
2021-03-09 18:29:56 +01:00
|
|
|
|
2023-08-06 06:27:28 +02:00
|
|
|
// 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
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
//
|
2023-01-28 11:06:38 +01:00
|
|
|
// 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
|
|
|
|
|
2023-11-03 06:33:11 +01:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
func (i *Image) isOnAtlas() bool {
|
2018-06-25 18:59:12 +02:00
|
|
|
return i.node != nil
|
|
|
|
}
|
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
func (i *Image) isOnSourceBackend() bool {
|
|
|
|
if i.backend == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return i.backend.source
|
|
|
|
}
|
|
|
|
|
2021-01-17 10:22:45 +01:00
|
|
|
func (i *Image) resetUsedAsSourceCount() {
|
|
|
|
i.usedAsSourceCount = 0
|
2023-08-19 07:57:55 +02:00
|
|
|
imagesToPutOnSourceBackend.remove(i)
|
2020-11-05 18:10:03 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 17:14:04 +02:00
|
|
|
func (i *Image) paddingSize() int {
|
2022-06-09 17:32:55 +02:00
|
|
|
if i.imageType == ImageTypeRegular {
|
|
|
|
return 1
|
2022-06-09 17:14:04 +02:00
|
|
|
}
|
2022-06-09 17:32:55 +02:00
|
|
|
return 0
|
2022-06-09 17:14:04 +02:00
|
|
|
}
|
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
func (i *Image) ensureIsolatedFromSource(backends []*backend) {
|
2021-01-17 10:22:45 +01:00
|
|
|
i.resetUsedAsSourceCount()
|
2020-11-05 17:45:04 +01:00
|
|
|
|
2023-08-18 20:25:17 +02: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.
|
2023-08-19 07:57:55 +02:00
|
|
|
imagesUsedAsDestination.add(i)
|
2023-08-18 20:25:17 +02:00
|
|
|
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
2023-08-18 21:57:30 +02:00
|
|
|
// `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)
|
|
|
|
}
|
2023-02-23 09:21:17 +01:00
|
|
|
}
|
|
|
|
i.allocate(bs, false)
|
2023-08-18 22:20:39 +02:00
|
|
|
i.backendCreatedInThisFrame = true
|
2018-04-29 11:51:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
if !i.isOnAtlas() {
|
2018-03-10 15:14:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-02-22 18:22:37 +01:00
|
|
|
newI := NewImage(i.width, i.height, i.imageType)
|
2018-03-10 15:14:59 +01:00
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
// Call allocate explicitly in order to have an isolated backend from the specified backends.
|
2023-08-18 21:57:30 +02:00
|
|
|
// `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)
|
|
|
|
}
|
2023-02-23 09:21:17 +01:00
|
|
|
}
|
|
|
|
newI.allocate(bs, false)
|
2023-02-22 18:22:37 +01:00
|
|
|
|
|
|
|
w, h := float32(i.width), float32(i.height)
|
|
|
|
vs := make([]float32, 4*graphics.VertexFloatCount)
|
2024-08-11 19:42:36 +02:00
|
|
|
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, w, h, 0, 0, w, h, 1, 1, 1, 1)
|
2023-02-22 18:22:37 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, i.width, i.height)
|
2023-02-23 09:21:17 +01:00
|
|
|
|
2024-06-09 19:02:47 +02:00
|
|
|
newI.drawTriangles([graphics.ShaderSrcImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillRuleFillAll)
|
2023-02-22 18:22:37 +01:00
|
|
|
newI.moveTo(i)
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:34:37 +02:00
|
|
|
func (i *Image) putOnSourceBackend() {
|
2018-07-11 18:40:24 +02:00
|
|
|
if i.backend == nil {
|
2023-02-23 09:21:17 +01:00
|
|
|
i.allocate(nil, true)
|
2023-08-06 09:03:57 +02:00
|
|
|
return
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
if i.isOnSourceBackend() {
|
2023-08-06 09:03:57 +02:00
|
|
|
return
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
if !i.canBePutOnAtlas() {
|
2023-02-23 09:21:17 +01:00
|
|
|
panic("atlas: putOnSourceBackend cannot be called on a image that cannot be on an atlas")
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2023-02-23 09:21:17 +01:00
|
|
|
newI.allocate(nil, true)
|
2021-02-26 04:56:22 +01:00
|
|
|
|
2022-06-09 19:58:01 +02:00
|
|
|
w, h := float32(i.width), float32(i.height)
|
2022-12-02 13:04:03 +01:00
|
|
|
vs := make([]float32, 4*graphics.VertexFloatCount)
|
2024-08-11 19:42:36 +02:00
|
|
|
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, w, h, 0, 0, w, h, 1, 1, 1, 1)
|
2022-06-09 19:58:01 +02:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, i.width, i.height)
|
2024-06-09 19:02:47 +02:00
|
|
|
newI.drawTriangles([graphics.ShaderSrcImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillRuleFillAll)
|
2021-02-26 04:56:22 +01:00
|
|
|
|
2018-07-21 23:27:57 +02:00
|
|
|
newI.moveTo(i)
|
2021-01-17 10:22:45 +01:00
|
|
|
i.usedAsSourceCount = 0
|
2023-03-02 10:24:18 +01:00
|
|
|
|
|
|
|
if !i.isOnSourceBackend() {
|
|
|
|
panic("atlas: i must be on a source backend but not")
|
|
|
|
}
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 16:55:11 +02:00
|
|
|
func (i *Image) regionWithPadding() image.Rectangle {
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: backend must not be nil: not allocated yet?")
|
2018-04-29 11:51:48 +02:00
|
|
|
}
|
2021-03-11 15:13:24 +01:00
|
|
|
if !i.isOnAtlas() {
|
2023-04-27 16:55:11 +02:00
|
|
|
return image.Rect(0, 0, i.width+i.paddingSize(), i.height+i.paddingSize())
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
2023-04-27 16:55:11 +02:00
|
|
|
return i.node.Region()
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
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
|
2024-06-09 19:02:47 +02:00
|
|
|
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) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
2021-02-26 04:56:22 +01:00
|
|
|
defer backendsM.Unlock()
|
2023-10-18 16:18:08 +02:00
|
|
|
|
|
|
|
if !inFrame {
|
|
|
|
vs := make([]float32, len(vertices))
|
|
|
|
copy(vs, vertices)
|
2023-11-03 18:45:16 +01:00
|
|
|
is := make([]uint32, len(indices))
|
2023-10-18 16:18:08 +02:00
|
|
|
copy(is, indices)
|
|
|
|
us := make([]uint32, len(uniforms))
|
|
|
|
copy(us, uniforms)
|
|
|
|
|
2023-10-24 16:15:29 +02:00
|
|
|
appendDeferred(func() {
|
2023-11-04 10:09:47 +01:00
|
|
|
i.drawTriangles(srcs, vs, is, blend, dstRegion, srcRegions, shader, us, fillRule)
|
2023-10-18 16:18:08 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-04 10:09:47 +01:00
|
|
|
i.drawTriangles(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule)
|
2021-02-26 04:56:22 +01:00
|
|
|
}
|
2018-04-29 11:51:48 +02:00
|
|
|
|
2024-06-09 19:02:47 +02:00
|
|
|
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) {
|
2024-05-27 16:46:48 +02:00
|
|
|
backends := make([]*backend, 0, len(srcs))
|
2023-02-23 09:21:17 +01:00
|
|
|
for _, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
2021-02-26 04:56:22 +01:00
|
|
|
}
|
2023-02-23 09:21:17 +01:00
|
|
|
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.
|
2023-08-18 22:24:20 +02:00
|
|
|
// If the backend becomes the same as i's, i's backend will be changed at ensureIsolatedFromSource.
|
2023-02-23 09:21:17 +01:00
|
|
|
src.allocate(nil, true)
|
|
|
|
}
|
2024-05-27 16:46:48 +02:00
|
|
|
backends = append(backends, src.backend)
|
2023-08-18 21:57:30 +02:00
|
|
|
src.backend.sourceInThisFrame = true
|
2021-02-26 04:56:22 +01:00
|
|
|
}
|
2023-08-18 20:59:03 +02:00
|
|
|
|
2024-05-27 16:46:48 +02:00
|
|
|
i.ensureIsolatedFromSource(backends)
|
2018-03-10 15:48:10 +01:00
|
|
|
|
2020-07-17 18:09:58 +02:00
|
|
|
for _, src := range srcs {
|
2023-08-18 20:59:03 +02:00
|
|
|
// 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.
|
2024-09-06 04:48:53 +02:00
|
|
|
if src != nil && i.backend.restorable == src.backend.restorable {
|
2023-08-18 20:59:03 +02:00
|
|
|
panic("atlas: Image.DrawTriangles: source must be different from the receiver")
|
|
|
|
}
|
2018-03-10 15:48:10 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 16:55:11 +02:00
|
|
|
r := i.regionWithPadding()
|
2022-06-09 17:14:04 +02:00
|
|
|
// TODO: Check if dstRegion does not to violate the region.
|
2023-09-28 07:29:55 +02:00
|
|
|
dstRegion = dstRegion.Add(r.Min)
|
2022-06-09 17:14:04 +02:00
|
|
|
|
2023-09-28 07:29:55 +02:00
|
|
|
dx, dy := float32(r.Min.X), float32(r.Min.Y)
|
2020-11-07 11:14:06 +01:00
|
|
|
|
2020-06-03 18:35:35 +02:00
|
|
|
var oxf, oyf float32
|
2020-07-17 18:09:58 +02:00
|
|
|
if srcs[0] != nil {
|
2023-04-27 16:55:11 +02:00
|
|
|
r := srcs[0].regionWithPadding()
|
|
|
|
oxf, oyf = float32(r.Min.X), float32(r.Min.Y)
|
2021-09-22 17:19:23 +02:00
|
|
|
n := len(vertices)
|
2022-07-12 18:46:02 +02:00
|
|
|
for i := 0; i < n; i += graphics.VertexFloatCount {
|
2023-04-19 14:32:14 +02:00
|
|
|
vertices[i] += dx
|
|
|
|
vertices[i+1] += dy
|
2023-04-16 11:56:14 +02:00
|
|
|
vertices[i+2] += oxf
|
|
|
|
vertices[i+3] += oyf
|
|
|
|
}
|
2024-01-08 19:01:08 +01:00
|
|
|
if shader.ir.Unit == shaderir.Texels {
|
2024-09-06 07:25:20 +02:00
|
|
|
sw, sh := srcs[0].backend.restorable.InternalSize()
|
2023-04-16 11:56:14 +02:00
|
|
|
swf, shf := float32(sw), float32(sh)
|
|
|
|
for i := 0; i < n; i += graphics.VertexFloatCount {
|
|
|
|
vertices[i+2] /= swf
|
|
|
|
vertices[i+3] /= shf
|
|
|
|
}
|
2020-06-03 18:35:35 +02:00
|
|
|
}
|
2020-08-26 19:00:20 +02:00
|
|
|
} else {
|
2021-09-22 17:19:23 +02:00
|
|
|
n := len(vertices)
|
2022-07-12 18:46:02 +02:00
|
|
|
for i := 0; i < n; i += graphics.VertexFloatCount {
|
2023-04-19 14:32:14 +02:00
|
|
|
vertices[i] += dx
|
|
|
|
vertices[i+1] += dy
|
2020-08-26 19:00:20 +02:00
|
|
|
}
|
2019-09-20 17:41:53 +02:00
|
|
|
}
|
|
|
|
|
2023-08-25 01:01:32 +02:00
|
|
|
for i, src := range srcs {
|
2022-10-02 16:49:07 +02:00
|
|
|
if src == nil {
|
|
|
|
continue
|
2020-09-20 22:36:47 +02:00
|
|
|
}
|
2023-08-25 01:01:32 +02:00
|
|
|
|
|
|
|
// A source region can be deliberately empty when this is not needed in order to avoid unexpected
|
|
|
|
// performance issue (#1293).
|
2023-09-28 07:29:55 +02:00
|
|
|
if srcRegions[i].Empty() {
|
2023-08-25 01:01:32 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-27 16:55:11 +02:00
|
|
|
r := src.regionWithPadding()
|
2023-09-28 07:29:55 +02:00
|
|
|
srcRegions[i] = srcRegions[i].Add(r.Min)
|
2022-10-02 16:49:07 +02:00
|
|
|
}
|
2023-08-25 01:01:32 +02:00
|
|
|
|
2024-09-06 07:06:39 +02:00
|
|
|
var imgs [graphics.ShaderSrcImageCount]*restorable.Image
|
2022-10-02 16:49:07 +02:00
|
|
|
for i, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
2020-07-17 18:09:58 +02:00
|
|
|
}
|
2024-09-06 07:06:39 +02:00
|
|
|
imgs[i] = src.backend.restorable
|
2020-05-26 15:26:55 +02:00
|
|
|
}
|
|
|
|
|
2024-09-06 07:06:39 +02:00
|
|
|
i.backend.restorable.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader(), uniforms, fillRule)
|
2018-07-11 18:40:24 +02:00
|
|
|
|
2020-07-17 18:09:58 +02:00
|
|
|
for _, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-02-23 09:21:17 +01:00
|
|
|
if !src.isOnSourceBackend() && src.canBePutOnAtlas() {
|
2023-01-28 11:06:38 +01:00
|
|
|
// src might already registered, but assigning it again is not harmful.
|
2023-08-19 07:57:55 +02:00
|
|
|
imagesToPutOnSourceBackend.add(src)
|
2020-07-17 18:09:58 +02:00
|
|
|
}
|
2019-05-11 16:16:05 +02:00
|
|
|
}
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 20:24:46 +02:00
|
|
|
// WritePixels replaces the pixels on the image.
|
2023-04-27 17:55:10 +02:00
|
|
|
func (i *Image) WritePixels(pix []byte, region image.Rectangle) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2023-10-18 16:18:08 +02:00
|
|
|
|
|
|
|
if !inFrame {
|
|
|
|
copied := make([]byte, len(pix))
|
|
|
|
copy(copied, pix)
|
|
|
|
|
2023-10-24 16:15:29 +02:00
|
|
|
appendDeferred(func() {
|
2023-10-18 16:18:08 +02:00
|
|
|
i.writePixels(copied, region)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
i.writePixels(pix, region)
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
2018-03-10 16:28:30 +01:00
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
func (i *Image) writePixels(pix []byte, region image.Rectangle) {
|
|
|
|
if l := 4 * region.Dx() * region.Dy(); len(pix) != l {
|
2022-07-05 06:26:45 +02:00
|
|
|
panic(fmt.Sprintf("atlas: len(p) must be %d but %d", l, len(pix)))
|
|
|
|
}
|
|
|
|
|
2021-01-17 10:22:45 +01:00
|
|
|
i.resetUsedAsSourceCount()
|
2020-11-05 18:10:03 +01:00
|
|
|
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
2020-06-14 08:29:04 +02:00
|
|
|
if pix == nil {
|
2018-08-08 18:16:46 +02:00
|
|
|
return
|
|
|
|
}
|
2023-02-23 09:21:17 +01:00
|
|
|
// Allocate as a source as this image will likely be used as a source.
|
|
|
|
i.allocate(nil, true)
|
2018-04-29 11:51:48 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 16:55:11 +02:00
|
|
|
r := i.regionWithPadding()
|
2020-06-14 09:03:26 +02:00
|
|
|
|
2023-04-29 06:35:56 +02:00
|
|
|
if !region.Eq(image.Rect(0, 0, i.width, i.height)) || i.paddingSize() == 0 {
|
2023-04-27 17:12:42 +02:00
|
|
|
region = region.Add(r.Min)
|
2022-07-05 06:26:45 +02:00
|
|
|
|
|
|
|
if pix == nil {
|
2024-09-06 06:47:29 +02:00
|
|
|
i.backend.restorable.ClearPixels(region)
|
2022-07-05 06:26:45 +02:00
|
|
|
return
|
|
|
|
}
|
2020-06-14 09:03:26 +02:00
|
|
|
|
2022-06-11 16:44:29 +02:00
|
|
|
// Copy pixels in the case when pix is modified before the graphics command is executed.
|
2023-10-08 15:59:35 +02:00
|
|
|
pix2 := graphics.NewManagedBytes(len(pix), func(bs []byte) {
|
|
|
|
copy(bs, pix)
|
|
|
|
})
|
2024-09-06 07:02:41 +02:00
|
|
|
i.backend.restorable.WritePixels(pix2, region)
|
2022-06-09 17:32:55 +02:00
|
|
|
return
|
|
|
|
}
|
2022-06-09 17:14:04 +02:00
|
|
|
|
2022-03-21 07:37:21 +01:00
|
|
|
// TODO: These loops assume that paddingSize is 1.
|
2022-07-05 06:26:45 +02:00
|
|
|
// TODO: Is clearing edges explicitly really needed?
|
2022-06-09 17:14:04 +02:00
|
|
|
const paddingSize = 1
|
|
|
|
if paddingSize != i.paddingSize() {
|
2022-09-13 15:39:29 +02:00
|
|
|
panic(fmt.Sprintf("atlas: writePixels assumes the padding is always 1 but the actual padding was %d", i.paddingSize()))
|
2022-06-09 17:14:04 +02:00
|
|
|
}
|
2021-08-25 20:35:18 +02:00
|
|
|
|
2023-10-08 15:59:35 +02:00
|
|
|
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
|
|
|
|
}
|
2022-03-21 07:37:21 +01:00
|
|
|
|
2023-10-08 15:59:35 +02:00
|
|
|
// 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()])
|
|
|
|
}
|
|
|
|
})
|
2024-09-06 07:02:41 +02:00
|
|
|
i.backend.restorable.WritePixels(pixb, r)
|
2024-09-06 06:47:29 +02:00
|
|
|
}
|
|
|
|
|
2024-03-26 04:59:31 +01:00
|
|
|
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) (ok bool, err error) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
2020-04-18 13:01:40 +02:00
|
|
|
defer backendsM.Unlock()
|
|
|
|
|
2023-10-18 16:18:08 +02:00
|
|
|
if !inFrame {
|
2024-03-26 04:59:31 +01:00
|
|
|
// Not ready to read pixels. Try this later.
|
|
|
|
return false, nil
|
2023-10-18 16:18:08 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 16:54:54 +02:00
|
|
|
// In the tests, BeginFrame might not be called often and then images might not be disposed (#2292).
|
2022-10-16 12:47:00 +02:00
|
|
|
// To prevent memory leaks, flush the deferred functions here.
|
|
|
|
flushDeferred()
|
2022-09-01 16:54:54 +02:00
|
|
|
|
2024-09-06 04:48:53 +02:00
|
|
|
if i.backend == nil || i.backend.restorable == nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
for i := range pixels {
|
|
|
|
pixels[i] = 0
|
2020-04-18 13:01:40 +02:00
|
|
|
}
|
2024-09-06 04:48:53 +02:00
|
|
|
return true, nil
|
2018-04-29 11:51:48 +02:00
|
|
|
}
|
|
|
|
|
2024-09-06 07:17:47 +02:00
|
|
|
if err := i.backend.restorable.ReadPixels(graphicsDriver, pixels, region.Add(i.regionWithPadding().Min)); err != nil {
|
2024-09-06 04:48:53 +02:00
|
|
|
return false, err
|
2024-01-13 10:12:32 +01:00
|
|
|
}
|
2024-09-06 04:48:53 +02:00
|
|
|
return true, nil
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2023-11-03 07:06:22 +01:00
|
|
|
// 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()
|
|
|
|
|
2023-11-03 08:59:59 +01:00
|
|
|
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)
|
2019-08-12 13:09:17 +02:00
|
|
|
}
|
|
|
|
|
2023-11-03 06:09:44 +01:00
|
|
|
func (i *Image) deallocate() {
|
2018-03-10 15:14:59 +01:00
|
|
|
defer func() {
|
2018-04-08 19:18:46 +02:00
|
|
|
i.backend = nil
|
|
|
|
i.node = nil
|
2018-03-10 15:14:59 +01:00
|
|
|
}()
|
|
|
|
|
2021-01-17 10:22:45 +01:00
|
|
|
i.resetUsedAsSourceCount()
|
2023-11-03 06:09:44 +01:00
|
|
|
i.usedAsDestinationCount = 0
|
|
|
|
imagesUsedAsDestination.remove(i)
|
2020-11-12 15:37:49 +01:00
|
|
|
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
|
|
|
// Not allocated yet.
|
2018-04-25 18:51:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-30 15:51:05 +02:00
|
|
|
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()
|
2024-09-06 06:47:29 +02:00
|
|
|
i.backend.restorable.ClearPixels(r)
|
2024-06-30 15:51:05 +02:00
|
|
|
return
|
|
|
|
}
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
|
|
|
|
2024-09-06 07:25:20 +02:00
|
|
|
i.backend.restorable.Dispose()
|
2023-02-23 09:21:17 +01:00
|
|
|
|
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]
|
2023-02-23 09:21:17 +01:00
|
|
|
return
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-23 09:21:17 +01:00
|
|
|
|
2024-01-08 20:31:23 +01:00
|
|
|
panic("atlas: backend not found at an image being deallocated")
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
func NewImage(width, height int, imageType ImageType) *Image {
|
2019-08-23 18:06:14 +02:00
|
|
|
// Actual allocation is done lazily, and the lock is not needed.
|
2018-04-29 11:51:48 +02:00
|
|
|
return &Image{
|
2022-06-07 16:57:56 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
imageType: imageType,
|
2022-06-06 01:25:43 +02:00
|
|
|
}
|
2020-08-18 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
func (i *Image) canBePutOnAtlas() bool {
|
2023-02-23 09:21:17 +01:00
|
|
|
if minSourceSize == 0 || minDestinationSize == 0 || maxSize == 0 {
|
|
|
|
panic("atlas: min*Size or maxSize must be initialized")
|
2019-06-22 13:17:52 +02:00
|
|
|
}
|
2022-06-07 16:57:56 +02:00
|
|
|
if i.imageType != ImageTypeRegular {
|
2018-07-24 15:25:15 +02:00
|
|
|
return false
|
|
|
|
}
|
2023-04-27 02:02:09 +02:00
|
|
|
return i.width+i.paddingSize() <= maxSize && i.height+i.paddingSize() <= maxSize
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2024-01-29 12:38:17 +01:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
func (i *Image) allocate(forbiddenBackends []*backend, asSource bool) {
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend != nil {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: the image is already allocated")
|
2018-04-29 12:10:36 +02:00
|
|
|
}
|
|
|
|
|
2024-01-29 12:38:17 +01:00
|
|
|
runtime.SetFinalizer(i, (*Image).finalize)
|
2019-09-21 12:15:49 +02:00
|
|
|
|
2024-09-06 04:48:53 +02:00
|
|
|
if i.imageType == ImageTypeScreen {
|
|
|
|
if asSource {
|
|
|
|
panic("atlas: a screen image cannot be created as a source")
|
|
|
|
}
|
|
|
|
// A screen image doesn't have a padding.
|
|
|
|
i.backend = &backend{
|
2024-09-06 07:27:46 +02:00
|
|
|
restorable: restorable.NewImage(i.width, i.height, restorable.ImageTypeScreen),
|
2024-09-06 04:48:53 +02:00
|
|
|
}
|
|
|
|
theBackends = append(theBackends, i.backend)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-29 06:35:56 +02:00
|
|
|
wp := i.width + i.paddingSize()
|
|
|
|
hp := i.height + i.paddingSize()
|
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
if !i.canBePutOnAtlas() {
|
2023-04-29 06:35:56 +02:00
|
|
|
if wp > maxSize || hp > maxSize {
|
2022-06-10 07:25:08 +02:00
|
|
|
panic(fmt.Sprintf("atlas: the image being put on an atlas is too big: width: %d, height: %d", i.width, i.height))
|
|
|
|
}
|
|
|
|
|
2018-04-29 11:51:48 +02:00
|
|
|
i.backend = &backend{
|
2024-09-06 07:27:46 +02:00
|
|
|
restorable: restorable.NewImage(wp, hp, restorable.ImageTypeRegular),
|
2024-09-06 04:48:53 +02:00
|
|
|
source: asSource && i.imageType == ImageTypeRegular,
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
2024-01-08 20:38:12 +01:00
|
|
|
theBackends = append(theBackends, i.backend)
|
2018-04-29 11:51:48 +02:00
|
|
|
return
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
2018-03-10 15:14:59 +01:00
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
// Check if an existing backend is available.
|
|
|
|
loop:
|
2018-03-23 20:27:10 +01:00
|
|
|
for _, b := range theBackends {
|
2023-02-23 09:21:17 +01:00
|
|
|
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 {
|
2018-04-29 11:51:48 +02:00
|
|
|
i.backend = b
|
|
|
|
i.node = n
|
|
|
|
return
|
2018-03-10 17:30:24 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-22 16:44:56 +01:00
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
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 {
|
2023-02-22 16:44:56 +01:00
|
|
|
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 {
|
2023-02-22 16:44:56 +01:00
|
|
|
if height == maxSize {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic(fmt.Sprintf("atlas: the image being put on an atlas is too big: width: %d, height: %d", i.width, i.height))
|
2018-03-25 15:41:15 +02:00
|
|
|
}
|
2023-02-22 16:44:56 +01:00
|
|
|
height *= 2
|
2018-03-25 15:41:15 +02:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:27:10 +01:00
|
|
|
b := &backend{
|
2024-09-06 07:27:46 +02:00
|
|
|
restorable: restorable.NewImage(width, height, restorable.ImageTypeRegular),
|
2024-09-06 04:48:53 +02:00
|
|
|
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)
|
2018-03-09 08:02:57 +01:00
|
|
|
|
2023-04-29 06:35:56 +02:00
|
|
|
n := b.page.Alloc(wp, hp)
|
2018-03-03 10:51:52 +01:00
|
|
|
if n == nil {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: Alloc result must not be nil at allocate")
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
2018-04-29 11:51:48 +02:00
|
|
|
i.backend = b
|
|
|
|
i.node = n
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
2018-03-10 15:48:10 +01:00
|
|
|
|
2022-08-31 06:10:41 +02: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()
|
|
|
|
|
2023-10-18 16:18:08 +02:00
|
|
|
if !inFrame {
|
2023-10-18 19:04:21 +02:00
|
|
|
panic("atlas: DumpScreenshots must be called in between BeginFrame and EndFrame")
|
2023-10-18 16:18:08 +02:00
|
|
|
}
|
|
|
|
|
2024-09-06 07:25:20 +02:00
|
|
|
return i.backend.restorable.Dump(graphicsDriver, path, blackbg, image.Rect(0, 0, i.width, i.height))
|
2019-07-19 18:42:19 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 17:18:26 +02:00
|
|
|
func EndFrame() error {
|
2018-03-25 16:37:32 +02:00
|
|
|
backendsM.Lock()
|
2023-10-18 16:18:08 +02:00
|
|
|
defer backendsM.Unlock()
|
|
|
|
defer func() {
|
2023-10-18 19:04:21 +02:00
|
|
|
inFrame = false
|
2023-10-18 16:18:08 +02:00
|
|
|
}()
|
2019-08-25 17:32:30 +02:00
|
|
|
|
2023-10-18 19:04:21 +02:00
|
|
|
if !inFrame {
|
|
|
|
panic("atlas: inFrame must be true in EndFrame")
|
|
|
|
}
|
|
|
|
|
2023-08-18 21:57:30 +02:00
|
|
|
for _, b := range theBackends {
|
|
|
|
b.sourceInThisFrame = false
|
2023-02-23 09:21:17 +01:00
|
|
|
}
|
|
|
|
|
2022-10-16 08:16:38 +02:00
|
|
|
return nil
|
2018-03-25 16:37:32 +02:00
|
|
|
}
|
|
|
|
|
2023-12-23 11:17:50 +01:00
|
|
|
func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error {
|
2023-10-23 17:18:26 +02:00
|
|
|
func() {
|
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
|
|
|
|
if inFrame {
|
|
|
|
panic("atlas: inFrame must be false in SwapBuffer")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-09-06 07:33:48 +02:00
|
|
|
if err := restorable.SwapBuffers(graphicsDriver); err != nil {
|
2023-10-23 17:18:26 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-02 12:17:44 +01:00
|
|
|
func floorPowerOf2(x int) int {
|
2023-01-02 11:07:30 +01:00
|
|
|
if x <= 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2024-02-24 18:06:52 +01:00
|
|
|
return 1 << (bits.Len(uint(x)) - 1)
|
2023-01-02 11:07:30 +01:00
|
|
|
}
|
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
|
2023-10-18 16:18:08 +02:00
|
|
|
backendsM.Lock()
|
2019-08-25 17:32:30 +02:00
|
|
|
defer backendsM.Unlock()
|
2023-10-18 19:04:21 +02:00
|
|
|
|
|
|
|
if inFrame {
|
|
|
|
panic("atlas: inFrame must be false in BeginFrame")
|
|
|
|
}
|
|
|
|
|
2023-10-18 16:18:08 +02:00
|
|
|
inFrame = true
|
2019-08-25 10:28:59 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
initOnce.Do(func() {
|
2024-09-06 07:29:37 +02:00
|
|
|
err = restorable.InitializeGraphicsDriverState(graphicsDriver)
|
2019-08-25 10:32:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(theBackends) != 0 {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: all the images must be not on an atlas before the game starts")
|
2019-08-25 10:32:32 +02:00
|
|
|
}
|
2022-06-10 07:25:08 +02:00
|
|
|
|
2023-02-23 09:21:17 +01:00
|
|
|
// min*Size and maxSize can already be set for testings.
|
|
|
|
if minSourceSize == 0 {
|
|
|
|
minSourceSize = 1024
|
|
|
|
}
|
|
|
|
if minDestinationSize == 0 {
|
|
|
|
minDestinationSize = 16
|
2022-06-10 07:25:08 +02:00
|
|
|
}
|
|
|
|
if maxSize == 0 {
|
2024-09-06 07:29:37 +02:00
|
|
|
maxSize = floorPowerOf2(restorable.MaxImageSize(graphicsDriver))
|
2022-06-10 07:25:08 +02:00
|
|
|
}
|
2019-08-25 10:28:59 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-16 12:47:00 +02:00
|
|
|
flushDeferred()
|
2024-04-29 16:34:37 +02:00
|
|
|
putImagesOnSourceBackend()
|
2022-02-13 19:45:56 +01:00
|
|
|
|
2022-04-17 19:19:44 +02:00
|
|
|
return nil
|
2018-03-25 16:37:32 +02:00
|
|
|
}
|
2018-04-05 18:12:08 +02:00
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, error) {
|
2018-04-28 14:49:00 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2023-10-18 19:04:21 +02:00
|
|
|
|
|
|
|
if !inFrame {
|
|
|
|
panic("atlas: DumpImages must be called in between BeginFrame and EndFrame")
|
|
|
|
}
|
|
|
|
|
2024-01-08 20:50:19 +01:00
|
|
|
images := make([]*graphicscommand.Image, 0, len(theBackends))
|
|
|
|
for _, backend := range theBackends {
|
2024-09-06 04:48:53 +02:00
|
|
|
if backend.restorable == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
images = append(images, backend.restorable.Image)
|
2024-01-08 20:50:19 +01:00
|
|
|
}
|
|
|
|
return graphicscommand.DumpImages(images, graphicsDriver, dir)
|
2018-04-28 14:49:00 +02:00
|
|
|
}
|