2019-09-19 05:02:35 +02:00
|
|
|
// Copyright 2019 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 buffered
|
|
|
|
|
|
|
|
import (
|
2020-02-16 13:45:26 +01:00
|
|
|
"fmt"
|
2020-01-07 16:15:46 +01:00
|
|
|
"image"
|
2019-09-19 05:02:35 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/affine"
|
2021-03-11 15:13:24 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/driver"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2019-09-19 05:02:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Image struct {
|
2021-03-11 15:13:24 +01:00
|
|
|
img *atlas.Image
|
2019-10-11 20:09:43 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
|
2019-11-08 21:43:33 +01:00
|
|
|
pixels []byte
|
|
|
|
needsToResolvePixels bool
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func BeginFrame() error {
|
2021-03-11 15:13:24 +01:00
|
|
|
if err := atlas.BeginFrame(); err != nil {
|
2019-09-19 05:02:35 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-01-18 17:18:56 +01:00
|
|
|
return flushDelayedCommands()
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func EndFrame() error {
|
2021-03-11 15:13:24 +01:00
|
|
|
return atlas.EndFrame()
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 17:54:21 +02:00
|
|
|
func NewImage(width, height int) *Image {
|
2019-09-19 05:02:35 +02:00
|
|
|
i := &Image{}
|
2020-08-18 17:54:21 +02:00
|
|
|
i.initialize(width, height)
|
2020-02-16 11:24:26 +01:00
|
|
|
return i
|
|
|
|
}
|
2020-02-08 12:58:23 +01:00
|
|
|
|
2020-08-18 17:54:21 +02:00
|
|
|
func (i *Image) initialize(width, height int) {
|
2020-06-29 14:45:57 +02:00
|
|
|
if maybeCanAddDelayedCommand() {
|
2020-06-29 18:16:50 +02:00
|
|
|
if tryAddDelayedCommand(func() error {
|
2020-08-18 17:54:21 +02:00
|
|
|
i.initialize(width, height)
|
2020-06-29 14:45:57 +02:00
|
|
|
return nil
|
2020-06-29 18:16:50 +02:00
|
|
|
}) {
|
2020-06-29 14:45:57 +02:00
|
|
|
return
|
|
|
|
}
|
2019-09-26 20:12:21 +02:00
|
|
|
}
|
2021-03-11 15:13:24 +01:00
|
|
|
i.img = atlas.NewImage(width, height)
|
2019-10-11 20:09:43 +02:00
|
|
|
i.width = width
|
|
|
|
i.height = height
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2022-01-08 15:32:58 +01:00
|
|
|
func (i *Image) SetIndependent(independent bool) {
|
2022-01-08 15:21:38 +01:00
|
|
|
if maybeCanAddDelayedCommand() {
|
|
|
|
if tryAddDelayedCommand(func() error {
|
2022-01-08 15:32:58 +01:00
|
|
|
i.SetIndependent(independent)
|
2022-01-08 15:21:38 +01:00
|
|
|
return nil
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-01-08 15:32:58 +01:00
|
|
|
i.img.SetIndependent(independent)
|
2022-01-08 15:21:38 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 17:54:21 +02:00
|
|
|
func (i *Image) SetVolatile(volatile bool) {
|
|
|
|
if maybeCanAddDelayedCommand() {
|
|
|
|
if tryAddDelayedCommand(func() error {
|
|
|
|
i.SetVolatile(volatile)
|
|
|
|
return nil
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i.img.SetVolatile(volatile)
|
|
|
|
}
|
|
|
|
|
2019-09-19 05:02:35 +02:00
|
|
|
func NewScreenFramebufferImage(width, height int) *Image {
|
|
|
|
i := &Image{}
|
2020-02-16 11:24:26 +01:00
|
|
|
i.initializeAsScreenFramebuffer(width, height)
|
|
|
|
return i
|
|
|
|
}
|
2020-02-08 12:58:23 +01:00
|
|
|
|
2020-02-16 11:24:26 +01:00
|
|
|
func (i *Image) initializeAsScreenFramebuffer(width, height int) {
|
2020-06-29 14:45:57 +02:00
|
|
|
if maybeCanAddDelayedCommand() {
|
2020-06-29 18:16:50 +02:00
|
|
|
if tryAddDelayedCommand(func() error {
|
2020-06-29 14:45:57 +02:00
|
|
|
i.initializeAsScreenFramebuffer(width, height)
|
|
|
|
return nil
|
2020-06-29 18:16:50 +02:00
|
|
|
}) {
|
2020-06-29 14:45:57 +02:00
|
|
|
return
|
|
|
|
}
|
2019-09-26 20:12:21 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
i.img = atlas.NewScreenFramebufferImage(width, height)
|
2019-10-11 20:09:43 +02:00
|
|
|
i.width = width
|
|
|
|
i.height = height
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:09:43 +02:00
|
|
|
func (i *Image) invalidatePendingPixels() {
|
2019-11-08 21:43:33 +01:00
|
|
|
i.pixels = nil
|
2019-11-08 21:54:24 +01:00
|
|
|
i.needsToResolvePixels = false
|
2019-10-11 20:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) resolvePendingPixels(keepPendingPixels bool) {
|
2020-04-17 19:11:23 +02:00
|
|
|
if i.needsToResolvePixels {
|
|
|
|
i.img.ReplacePixels(i.pixels)
|
|
|
|
if !keepPendingPixels {
|
|
|
|
i.pixels = nil
|
|
|
|
}
|
|
|
|
i.needsToResolvePixels = false
|
2019-10-11 20:09:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 05:02:35 +02:00
|
|
|
func (i *Image) MarkDisposed() {
|
2020-06-29 14:45:57 +02:00
|
|
|
if maybeCanAddDelayedCommand() {
|
2020-06-29 18:16:50 +02:00
|
|
|
if tryAddDelayedCommand(func() error {
|
2020-06-29 14:45:57 +02:00
|
|
|
i.MarkDisposed()
|
|
|
|
return nil
|
2020-06-29 18:16:50 +02:00
|
|
|
}) {
|
2020-06-29 14:45:57 +02:00
|
|
|
return
|
|
|
|
}
|
2019-09-26 20:12:21 +02:00
|
|
|
}
|
2019-10-11 20:09:43 +02:00
|
|
|
i.invalidatePendingPixels()
|
2020-02-16 11:01:16 +01:00
|
|
|
i.img.MarkDisposed()
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:57:29 +02:00
|
|
|
func (img *Image) Pixels(x, y, width, height int) (pix []byte, err error) {
|
2020-06-16 20:19:31 +02:00
|
|
|
checkDelayedCommandsFlushed("Pixels")
|
2020-04-17 19:11:23 +02:00
|
|
|
|
2020-06-13 13:57:29 +02:00
|
|
|
if !image.Rect(x, y, x+width, y+height).In(image.Rect(0, 0, img.width, img.height)) {
|
|
|
|
return nil, fmt.Errorf("buffered: out of range")
|
2020-04-18 13:01:40 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:57:29 +02:00
|
|
|
pix = make([]byte, 4*width*height)
|
|
|
|
|
|
|
|
if img.pixels == nil {
|
|
|
|
pix, err := img.img.Pixels(0, 0, img.width, img.height)
|
2020-04-18 13:01:40 +02:00
|
|
|
if err != nil {
|
2020-06-13 13:57:29 +02:00
|
|
|
return nil, err
|
2020-04-18 13:01:40 +02:00
|
|
|
}
|
2020-06-13 13:57:29 +02:00
|
|
|
img.pixels = pix
|
2020-04-17 14:34:46 +02:00
|
|
|
}
|
2020-04-18 13:01:40 +02:00
|
|
|
|
2020-06-13 13:57:29 +02:00
|
|
|
for j := 0; j < height; j++ {
|
|
|
|
copy(pix[4*j*width:4*(j+1)*width], img.pixels[4*((j+y)*img.width+x):])
|
|
|
|
}
|
|
|
|
return pix, nil
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 09:09:25 +02:00
|
|
|
func (i *Image) DumpScreenshot(name string, blackbg bool) error {
|
2020-06-16 20:19:31 +02:00
|
|
|
checkDelayedCommandsFlushed("Dump")
|
2021-07-29 09:09:25 +02:00
|
|
|
return i.img.DumpScreenshot(name, blackbg)
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 13:45:26 +01:00
|
|
|
func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) error {
|
|
|
|
if l := 4 * width * height; len(pix) != l {
|
|
|
|
panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l))
|
|
|
|
}
|
|
|
|
|
2020-06-29 14:45:57 +02:00
|
|
|
if maybeCanAddDelayedCommand() {
|
2020-07-02 19:56:37 +02:00
|
|
|
copied := make([]byte, len(pix))
|
|
|
|
copy(copied, pix)
|
2020-06-29 18:16:50 +02:00
|
|
|
if tryAddDelayedCommand(func() error {
|
2020-07-02 19:56:37 +02:00
|
|
|
i.ReplacePixels(copied, x, y, width, height)
|
2020-06-29 14:45:57 +02:00
|
|
|
return nil
|
|
|
|
}) {
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-26 20:12:21 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 13:45:26 +01:00
|
|
|
if x == 0 && y == 0 && width == i.width && height == i.height {
|
|
|
|
i.invalidatePendingPixels()
|
2020-06-13 21:39:55 +02:00
|
|
|
|
2020-11-02 18:30:00 +01:00
|
|
|
// Call ReplacePixels immediately. Do not buffer the command.
|
|
|
|
// If a lot of new images are created but they are used at different timings,
|
|
|
|
// pixels are sent to GPU at different timings, which is very inefficient.
|
2020-06-27 19:43:38 +02:00
|
|
|
i.img.ReplacePixels(pix)
|
2020-02-16 13:45:26 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-16 14:16:27 +01:00
|
|
|
// TODO: Can we use (*restorable.Image).ReplacePixels?
|
2020-02-16 13:45:26 +01:00
|
|
|
if i.pixels == nil {
|
2020-04-18 13:01:40 +02:00
|
|
|
pix, err := i.img.Pixels(0, 0, i.width, i.height)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-02-16 13:45:26 +01:00
|
|
|
}
|
|
|
|
i.pixels = pix
|
|
|
|
}
|
2020-04-18 17:18:10 +02:00
|
|
|
i.replacePendingPixels(pix, x, y, width, height)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) replacePendingPixels(pix []byte, x, y, width, height int) {
|
2020-02-16 13:45:26 +01:00
|
|
|
for j := 0; j < height; j++ {
|
|
|
|
copy(i.pixels[4*((j+y)*i.width+x):], pix[4*j*width:4*(j+1)*width])
|
|
|
|
}
|
|
|
|
i.needsToResolvePixels = true
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 12:15:45 +01:00
|
|
|
// DrawTriangles draws the src image with the given vertices.
|
|
|
|
//
|
|
|
|
// Copying vertices and indices is the caller's responsibility.
|
2021-10-29 19:51:02 +02:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address, dstRegion, srcRegion driver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms []driver.Uniform, evenOdd bool) {
|
2020-07-17 18:09:58 +02:00
|
|
|
for _, src := range srcs {
|
2020-06-03 18:35:35 +02:00
|
|
|
if i == src {
|
2020-07-14 20:37:35 +02:00
|
|
|
panic("buffered: Image.DrawTriangles: source images must be different from the receiver")
|
2020-06-03 18:35:35 +02:00
|
|
|
}
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
2019-09-26 20:12:21 +02:00
|
|
|
|
2020-06-29 14:45:57 +02:00
|
|
|
if maybeCanAddDelayedCommand() {
|
2020-06-29 18:16:50 +02:00
|
|
|
if tryAddDelayedCommand(func() error {
|
2020-06-29 14:45:57 +02:00
|
|
|
// Arguments are not copied. Copying is the caller's responsibility.
|
2021-07-02 12:26:09 +02:00
|
|
|
i.DrawTriangles(srcs, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, shader, uniforms, evenOdd)
|
2020-06-29 14:45:57 +02:00
|
|
|
return nil
|
2020-06-29 18:16:50 +02:00
|
|
|
}) {
|
2020-06-29 14:45:57 +02:00
|
|
|
return
|
|
|
|
}
|
2019-09-26 20:12:21 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
var s *atlas.Shader
|
|
|
|
var imgs [graphics.ShaderImageNum]*atlas.Image
|
2020-09-20 22:36:47 +02:00
|
|
|
if shader == nil {
|
|
|
|
// Fast path for rendering without a shader (#1355).
|
|
|
|
img := srcs[0]
|
|
|
|
img.resolvePendingPixels(true)
|
|
|
|
imgs[0] = img.img
|
|
|
|
} else {
|
|
|
|
for i, img := range srcs {
|
|
|
|
if img == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
img.resolvePendingPixels(true)
|
|
|
|
imgs[i] = img.img
|
2020-07-17 18:09:58 +02:00
|
|
|
}
|
2020-09-20 22:36:47 +02:00
|
|
|
s = shader.shader
|
2020-07-05 18:55:46 +02:00
|
|
|
}
|
2020-09-20 22:36:47 +02:00
|
|
|
i.resolvePendingPixels(false)
|
2020-07-05 18:55:46 +02:00
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
i.img.DrawTriangles(imgs, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, s, uniforms, evenOdd)
|
2020-06-25 06:11:19 +02:00
|
|
|
i.invalidatePendingPixels()
|
2020-05-29 20:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Shader struct {
|
2021-03-11 15:13:24 +01:00
|
|
|
shader *atlas.Shader
|
2020-05-29 20:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewShader(program *shaderir.Program) *Shader {
|
|
|
|
return &Shader{
|
2021-03-11 15:13:24 +01:00
|
|
|
shader: atlas.NewShader(program),
|
2020-05-29 20:21:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) MarkDisposed() {
|
|
|
|
s.shader.MarkDisposed()
|
|
|
|
s.shader = nil
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|