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"
|
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"
|
2023-07-31 17:11:35 +02: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"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2020-06-27 21:14:52 +02:00
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
2019-07-03 18:55:55 +02:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2021-06-19 11:52:05 +02:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2022-10-16 12:47:00 +02:00
|
|
|
func flushDeferred() {
|
2019-09-21 12:54:39 +02:00
|
|
|
deferredM.Lock()
|
|
|
|
fs := deferred
|
|
|
|
deferred = nil
|
|
|
|
deferredM.Unlock()
|
|
|
|
|
|
|
|
for _, f := range fs {
|
2019-08-12 12:37:20 +02:00
|
|
|
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
|
|
|
|
2023-08-06 09:03:57 +02:00
|
|
|
func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) {
|
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++
|
|
|
|
}
|
2023-08-18 18:19:15 +02:00
|
|
|
if int64(i.usedAsSourceCount) >= int64(baseCountToPutOnSourceBackend*(1<<uint(min(i.usedAsDestinationCount, 31)))) {
|
2023-08-06 09:03:57 +02:00
|
|
|
i.putOnSourceBackend(graphicsDriver)
|
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
|
|
|
}
|
|
|
|
|
2018-03-10 16:02:23 +01:00
|
|
|
type backend struct {
|
2021-03-11 15:13:24 +01:00
|
|
|
// restorable is an atlas on which there might be multiple images.
|
2018-03-03 10:51:52 +01:00
|
|
|
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%.
|
|
|
|
// 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) {
|
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
|
|
|
|
2022-11-11 13:15:28 +01: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
|
|
|
|
|
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
|
2023-08-06 06:27:28 +02:00
|
|
|
|
2019-08-12 13:48:26 +02:00
|
|
|
deferred []func()
|
2019-09-21 12:54:39 +02:00
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
// deferredM is a mutex for the slice operations. This must not be used for other usages.
|
2019-09-21 12:54:39 +02:00
|
|
|
deferredM sync.Mutex
|
2018-03-03 10:51:52 +01:00
|
|
|
)
|
|
|
|
|
2019-08-23 18:06:14 +02:00
|
|
|
func init() {
|
2019-08-25 17:32:30 +02:00
|
|
|
// Lock the mutex before a frame begins.
|
|
|
|
//
|
|
|
|
// In each frame, restoring images and resolving images happen respectively:
|
|
|
|
//
|
|
|
|
// [Restore -> Resolve] -> [Restore -> Resolve] -> ...
|
|
|
|
//
|
|
|
|
// Between each frame, any image operations are not permitted, or stale images would remain when restoring
|
|
|
|
// (#913).
|
2019-08-23 18:06:14 +02:00
|
|
|
backendsM.Lock()
|
2019-06-22 13:17:52 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
type ImageType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ImageTypeRegular ImageType = iota
|
|
|
|
ImageTypeScreen
|
|
|
|
ImageTypeVolatile
|
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
|
|
|
|
disposed bool
|
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
|
2023-02-23 09:21:17 +01:00
|
|
|
// a restorable image on an atlas by ensureIsolatedFromSource.
|
2019-05-11 16:16:05 +02:00
|
|
|
//
|
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.
|
2021-01-17 10:22:45 +01:00
|
|
|
usedAsSourceCount int
|
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) {
|
|
|
|
dst.dispose(false)
|
|
|
|
*dst = *i
|
|
|
|
|
|
|
|
// i is no longer available but Dispose must not be called
|
2023-01-28 11:06:38 +01:00
|
|
|
// since i and dst have the same values as 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)
|
|
|
|
graphics.QuadVertices(vs, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
|
|
|
|
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
|
|
|
|
2023-09-28 07:29:55 +02:00
|
|
|
newI.drawTriangles([graphics.ShaderImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, NearestFilterShader, nil, false, true)
|
2023-02-22 18:22:37 +01:00
|
|
|
newI.moveTo(i)
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2023-08-06 09:03:57 +02:00
|
|
|
func (i *Image) putOnSourceBackend(graphicsDriver graphicsdriver.Graphics) {
|
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)
|
|
|
|
graphics.QuadVertices(vs, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 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)
|
|
|
|
newI.drawTriangles([graphics.ShaderImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, NearestFilterShader, nil, false, true)
|
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
|
2023-09-28 07:29:55 +02:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, evenOdd bool) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
2021-02-26 04:56:22 +01:00
|
|
|
defer backendsM.Unlock()
|
2023-08-25 01:01:32 +02:00
|
|
|
i.drawTriangles(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, evenOdd, false)
|
2021-02-26 04:56:22 +01:00
|
|
|
}
|
2018-04-29 11:51:48 +02:00
|
|
|
|
2023-09-28 07:29:55 +02:00
|
|
|
func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, evenOdd bool, keepOnAtlas bool) {
|
2018-04-29 20:34:35 +02:00
|
|
|
if i.disposed {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: the drawing target image must not be disposed (DrawTriangles)")
|
2018-04-29 20:34:35 +02:00
|
|
|
}
|
2023-02-23 09:21:17 +01:00
|
|
|
|
|
|
|
backends := make([]*backend, 0, len(srcs))
|
|
|
|
for _, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
2021-02-26 04:56:22 +01:00
|
|
|
}
|
2023-08-18 20:59:03 +02:00
|
|
|
if src.disposed {
|
|
|
|
panic("atlas: the drawing source image must not be disposed (DrawTriangles)")
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2023-02-23 09:21:17 +01: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.
|
|
|
|
if src != nil && i.backend.restorable == src.backend.restorable {
|
|
|
|
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
|
|
|
|
}
|
2023-08-01 04:41:27 +02:00
|
|
|
if shader.unit() == shaderir.Texels {
|
2023-04-16 11:56:14 +02:00
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
var imgs [graphics.ShaderImageCount]*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
|
|
|
}
|
2022-10-02 16:49:07 +02:00
|
|
|
imgs[i] = src.backend.restorable
|
2020-05-26 15:26:55 +02:00
|
|
|
}
|
|
|
|
|
2023-08-25 01:01:32 +02:00
|
|
|
i.backend.restorable.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, evenOdd)
|
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-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) {
|
2018-04-29 20:34:35 +02:00
|
|
|
if i.disposed {
|
2022-09-13 15:39:29 +02:00
|
|
|
panic("atlas: the image must not be disposed at writePixels")
|
2018-04-29 20:34:35 +02:00
|
|
|
}
|
2020-11-05 18:10:03 +01:00
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
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 {
|
2023-04-27 17:12:42 +02:00
|
|
|
i.backend.restorable.WritePixels(nil, 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-07-31 17:11:35 +02:00
|
|
|
pix2 := graphicscommand.AllocBytes(len(pix))
|
2022-06-09 17:32:55 +02:00
|
|
|
copy(pix2, pix)
|
2023-04-27 17:12:42 +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
|
|
|
|
2023-07-31 17:11:35 +02:00
|
|
|
pixb := graphicscommand.AllocBytes(4 * r.Dx() * r.Dy())
|
2021-08-25 13:01:30 +02:00
|
|
|
|
2021-08-25 20:35:18 +02:00
|
|
|
// Clear the edges. pixb might not be zero-cleared.
|
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
|
|
|
}
|
2023-04-27 16:55:11 +02:00
|
|
|
rowPixels := 4 * r.Dx()
|
2021-08-25 20:35:18 +02:00
|
|
|
for i := 0; i < rowPixels; i++ {
|
2023-04-27 16:55:11 +02:00
|
|
|
pixb[rowPixels*(r.Dy()-1)+i] = 0
|
2021-08-25 20:35:18 +02:00
|
|
|
}
|
2023-04-27 16:55:11 +02:00
|
|
|
for j := 1; j < r.Dy(); j++ {
|
2023-04-27 02:02:09 +02:00
|
|
|
pixb[rowPixels*j-4] = 0
|
|
|
|
pixb[rowPixels*j-3] = 0
|
|
|
|
pixb[rowPixels*j-2] = 0
|
|
|
|
pixb[rowPixels*j-1] = 0
|
2021-08-25 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:01:30 +02:00
|
|
|
// Copy the content.
|
2023-04-27 17:12:42 +02:00
|
|
|
for j := 0; j < region.Dy(); j++ {
|
|
|
|
copy(pixb[4*j*r.Dx():], pix[4*j*region.Dx():4*(j+1)*region.Dx()])
|
2022-03-21 07:37:21 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
i.backend.restorable.WritePixels(pixb, r)
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) error {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
2020-04-18 13:01:40 +02:00
|
|
|
defer backendsM.Unlock()
|
|
|
|
|
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
|
|
|
|
2022-08-05 14:40:38 +02:00
|
|
|
if i.backend == nil || i.backend.restorable == nil {
|
|
|
|
for i := range pixels {
|
|
|
|
pixels[i] = 0
|
2020-04-18 13:01:40 +02:00
|
|
|
}
|
2022-08-05 14:40:38 +02:00
|
|
|
return nil
|
2018-04-29 11:51:48 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 16:55:11 +02:00
|
|
|
r := i.regionWithPadding()
|
2023-04-27 17:55:10 +02:00
|
|
|
return i.backend.restorable.ReadPixels(graphicsDriver, pixels, region.Add(r.Min))
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:54:39 +02:00
|
|
|
// MarkDisposed marks the image as disposed. The actual operation is deferred.
|
|
|
|
// MarkDisposed can be called from finalizers.
|
2019-08-12 13:09:17 +02:00
|
|
|
//
|
|
|
|
// A function from finalizer must not be blocked, but disposing operation can be blocked.
|
|
|
|
// Defer this operation until it becomes safe. (#913)
|
2019-09-21 12:54:39 +02:00
|
|
|
func (i *Image) MarkDisposed() {
|
2022-06-08 04:18:08 +02:00
|
|
|
// As MarkDisposed can be invoked from finalizers, backendsM should not be used.
|
2019-09-21 12:54:39 +02:00
|
|
|
deferredM.Lock()
|
2019-08-12 12:37:20 +02:00
|
|
|
deferred = append(deferred, func() {
|
|
|
|
i.dispose(true)
|
|
|
|
})
|
2019-09-21 12:54:39 +02:00
|
|
|
deferredM.Unlock()
|
2019-08-12 13:09:17 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 04:49:55 +02:00
|
|
|
func (i *Image) dispose(markDisposed bool) {
|
2018-03-10 15:14:59 +01:00
|
|
|
defer func() {
|
2018-05-03 04:49:55 +02:00
|
|
|
if markDisposed {
|
|
|
|
i.disposed = true
|
|
|
|
}
|
2018-04-08 19:18:46 +02:00
|
|
|
i.backend = nil
|
|
|
|
i.node = nil
|
2018-07-21 22:40:04 +02:00
|
|
|
if markDisposed {
|
|
|
|
runtime.SetFinalizer(i, nil)
|
|
|
|
}
|
2018-03-10 15:14:59 +01:00
|
|
|
}()
|
|
|
|
|
2021-01-17 10:22:45 +01:00
|
|
|
i.resetUsedAsSourceCount()
|
2020-11-12 15:37:49 +01:00
|
|
|
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.disposed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.backend == nil {
|
|
|
|
// Not allocated yet.
|
2018-04-25 18:51:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
if !i.isOnAtlas() {
|
2018-04-08 19:18:46 +02:00
|
|
|
i.backend.restorable.Dispose()
|
2018-03-10 15:14:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
i.backend.page.Free(i.node)
|
|
|
|
if !i.backend.page.IsEmpty() {
|
2018-04-05 17:36:15 +02:00
|
|
|
// As this part can be reused, this should be cleared explicitly.
|
2023-04-27 16:55:11 +02:00
|
|
|
r := i.regionWithPadding()
|
2023-04-27 17:12:42 +02:00
|
|
|
i.backend.restorable.ClearPixels(r)
|
2018-03-10 15:14:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +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 {
|
2023-02-23 09:21:17 +01:00
|
|
|
theBackends = append(theBackends[:idx], theBackends[idx+1:]...)
|
|
|
|
return
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-23 09:21:17 +01:00
|
|
|
|
|
|
|
panic("atlas: backend not found at an image being disposed")
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-21 12:54:39 +02:00
|
|
|
runtime.SetFinalizer(i, (*Image).MarkDisposed)
|
2019-09-21 12:15:49 +02:00
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
if i.imageType == ImageTypeScreen {
|
2023-02-23 09:21:17 +01:00
|
|
|
if asSource {
|
|
|
|
panic("atlas: a screen image cannot be created as a source")
|
|
|
|
}
|
2020-06-16 05:48:20 +02:00
|
|
|
// A screen image doesn't have a padding.
|
2019-08-24 17:43:26 +02:00
|
|
|
i.backend = &backend{
|
2022-06-07 16:45:35 +02:00
|
|
|
restorable: restorable.NewImage(i.width, i.height, restorable.ImageTypeScreen),
|
2019-08-24 17:43:26 +02:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2022-06-06 02:29:30 +02:00
|
|
|
typ := restorable.ImageTypeRegular
|
2022-06-07 16:57:56 +02:00
|
|
|
if i.imageType == ImageTypeVolatile {
|
2022-06-06 02:29:30 +02:00
|
|
|
typ = restorable.ImageTypeVolatile
|
|
|
|
}
|
2018-04-29 11:51:48 +02:00
|
|
|
i.backend = &backend{
|
2023-04-29 06:35:56 +02:00
|
|
|
restorable: restorable.NewImage(wp, hp, typ),
|
2023-02-23 09:21:17 +01:00
|
|
|
source: asSource && typ == restorable.ImageTypeRegular,
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-06-06 02:29:30 +02:00
|
|
|
typ := restorable.ImageTypeRegular
|
2022-06-07 16:57:56 +02:00
|
|
|
if i.imageType == ImageTypeVolatile {
|
2022-06-06 02:29:30 +02:00
|
|
|
typ = restorable.ImageTypeVolatile
|
|
|
|
}
|
2018-03-23 20:27:10 +01:00
|
|
|
b := &backend{
|
2023-02-22 16:44:56 +01:00
|
|
|
restorable: restorable.NewImage(width, height, typ),
|
|
|
|
page: packing.NewPage(width, height, maxSize),
|
2023-02-23 09:21:17 +01:00
|
|
|
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-04-27 02:02:09 +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-07-29 19:25:10 +02:00
|
|
|
func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) error {
|
2018-03-25 16:37:32 +02:00
|
|
|
backendsM.Lock()
|
2019-08-25 17:32:30 +02:00
|
|
|
|
2023-07-29 19:25:10 +02:00
|
|
|
if err := restorable.EndFrame(graphicsDriver, swapBuffersForGL); err != nil {
|
2022-10-16 08:16:38 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-06-25 19:33:14 +02:00
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
p2 := 1
|
|
|
|
for p2*2 <= x {
|
|
|
|
p2 *= 2
|
|
|
|
}
|
|
|
|
return p2
|
|
|
|
}
|
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
|
2019-08-25 17:32:30 +02:00
|
|
|
defer backendsM.Unlock()
|
2019-08-25 10:28:59 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
initOnce.Do(func() {
|
2022-03-19 15:55:14 +01: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 {
|
2023-01-02 12:17:44 +01: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-04-17 19:19:44 +02:00
|
|
|
// Restore images first before other image manipulations (#2075).
|
|
|
|
if err := restorable.RestoreIfNeeded(graphicsDriver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-16 12:47:00 +02:00
|
|
|
flushDeferred()
|
2023-08-06 09:03:57 +02:00
|
|
|
putImagesOnSourceBackend(graphicsDriver)
|
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()
|
2022-03-19 15:55:14 +01:00
|
|
|
return restorable.DumpImages(graphicsDriver, dir)
|
2018-04-28 14:49:00 +02:00
|
|
|
}
|