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"
|
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/graphics"
|
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/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
|
|
|
}
|
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
|
|
|
|
if err := atlas.BeginFrame(graphicsDriver); 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
|
|
|
}
|
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func EndFrame(graphicsDriver graphicsdriver.Graphics) error {
|
|
|
|
return atlas.EndFrame(graphicsDriver)
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 17:54:21 +02:00
|
|
|
func NewImage(width, height int) *Image {
|
2022-03-20 10:01:27 +01:00
|
|
|
i := &Image{
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
|
|
|
i.initialize()
|
2020-02-16 11:24:26 +01:00
|
|
|
return i
|
|
|
|
}
|
2020-02-08 12:58:23 +01:00
|
|
|
|
2022-03-20 10:01:27 +01:00
|
|
|
func (i *Image) initialize() {
|
2020-06-29 14:45:57 +02:00
|
|
|
if maybeCanAddDelayedCommand() {
|
2020-06-29 18:16:50 +02:00
|
|
|
if tryAddDelayedCommand(func() error {
|
2022-03-20 10:01:27 +01:00
|
|
|
i.initialize()
|
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
|
|
|
}
|
2022-03-20 10:01:27 +01:00
|
|
|
i.img = atlas.NewImage(i.width, i.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 {
|
2022-03-20 10:07:29 +01:00
|
|
|
i.img.ReplacePixels(i.pixels)
|
2020-04-17 19:11:23 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-20 11:03:41 +01:00
|
|
|
func (i *Image) ensurePixels(graphicsDriver graphicsdriver.Graphics) error {
|
|
|
|
if i.pixels != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pix, err := i.img.Pixels(graphicsDriver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i.pixels = pix
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-20 10:38:29 +01:00
|
|
|
func (img *Image) At(graphicsDriver graphicsdriver.Graphics, x, y int) (r, g, b, a byte, err error) {
|
|
|
|
checkDelayedCommandsFlushed("At")
|
2020-04-17 19:11:23 +02:00
|
|
|
|
2022-03-20 11:03:41 +01:00
|
|
|
if err := img.ensurePixels(graphicsDriver); err != nil {
|
|
|
|
return 0, 0, 0, 0, err
|
2020-04-17 14:34:46 +02:00
|
|
|
}
|
2020-04-18 13:01:40 +02:00
|
|
|
|
2022-03-20 10:38:29 +01:00
|
|
|
idx := 4 * (y*img.width + x)
|
|
|
|
return img.pixels[idx], img.pixels[idx+1], img.pixels[idx+2], img.pixels[idx+3], nil
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) error {
|
2020-06-16 20:19:31 +02:00
|
|
|
checkDelayedCommandsFlushed("Dump")
|
2022-03-19 15:55:14 +01:00
|
|
|
return i.img.DumpScreenshot(graphicsDriver, name, blackbg)
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2022-03-20 08:44:11 +01:00
|
|
|
// ReplacePixels replaces the pixels at the specified region.
|
2022-03-19 20:20:23 +01:00
|
|
|
// This call is not accumulated and send one draw call to replace pixels.
|
2022-03-20 10:01:27 +01:00
|
|
|
func (i *Image) ReplacePixels(pix []byte) {
|
|
|
|
if l := 4 * i.width * i.height; len(pix) != l {
|
2020-02-16 13:45:26 +01:00
|
|
|
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 {
|
2022-03-20 10:01:27 +01:00
|
|
|
i.ReplacePixels(copied)
|
2022-03-19 19:51:57 +01:00
|
|
|
return nil
|
|
|
|
}) {
|
2022-03-20 08:44:11 +01:00
|
|
|
return
|
2022-03-19 19:51:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i.invalidatePendingPixels()
|
|
|
|
|
2022-03-20 10:07:29 +01:00
|
|
|
i.img.ReplacePixels(pix)
|
2022-03-19 19:51:57 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 10:01:27 +01:00
|
|
|
// ReplacePartial replaces the pixel at the specified partial region.
|
2022-03-19 20:20:23 +01:00
|
|
|
// This call might be accumulated and send one draw call to replace pixels for the accumulated calls.
|
2022-03-20 10:01:27 +01:00
|
|
|
func (i *Image) ReplacePartialPixels(graphicsDriver graphicsdriver.Graphics, 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))
|
|
|
|
}
|
|
|
|
|
2022-03-19 19:51:57 +01:00
|
|
|
if maybeCanAddDelayedCommand() {
|
|
|
|
if tryAddDelayedCommand(func() error {
|
2022-03-20 10:01:27 +01:00
|
|
|
copied := make([]byte, len(pix))
|
|
|
|
copy(copied, pix)
|
|
|
|
i.ReplacePartialPixels(graphicsDriver, 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
|
|
|
}
|
|
|
|
|
2022-03-20 11:03:41 +01:00
|
|
|
if err := i.ensurePixels(graphicsDriver); err != nil {
|
|
|
|
return err
|
2020-02-16 13:45:26 +01:00
|
|
|
}
|
2022-03-20 11:03:41 +01:00
|
|
|
|
2022-03-20 10:01:27 +01:00
|
|
|
i.replacePendingPixels(pix, x, y, width, height)
|
2020-04-18 17:18:10 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-20 10:01:27 +01:00
|
|
|
func (i *Image) replacePendingPixels(pix []byte, x, y, width, height int) {
|
|
|
|
for j := 0; j < height; j++ {
|
|
|
|
copy(i.pixels[4*((j+y)*i.width+x):], pix[4*j*width:4*(j+1)*width])
|
|
|
|
}
|
2020-02-16 13:45:26 +01:00
|
|
|
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.
|
2022-03-12 19:42:10 +01:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms [][]float32, 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
|
|
|
}
|