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"
|
2022-04-03 19:15:33 +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
|
2022-03-20 20:11:36 +01:00
|
|
|
mask []byte
|
2019-11-08 21:43:33 +01:00
|
|
|
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
|
|
|
|
}
|
2022-06-24 18:42:40 +02:00
|
|
|
flushDelayedCommands()
|
|
|
|
return nil
|
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
|
|
|
}
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
func NewImage(width, height int, imageType atlas.ImageType) *Image {
|
2022-03-20 10:01:27 +01:00
|
|
|
i := &Image{
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
2022-06-07 16:57:56 +02:00
|
|
|
i.initialize(imageType)
|
2020-02-16 11:24:26 +01:00
|
|
|
return i
|
|
|
|
}
|
2020-02-08 12:58:23 +01:00
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
func (i *Image) initialize(imageType atlas.ImageType) {
|
2020-06-29 14:45:57 +02:00
|
|
|
if maybeCanAddDelayedCommand() {
|
2022-06-24 18:42:40 +02:00
|
|
|
if tryAddDelayedCommand(func() {
|
2022-06-07 16:57:56 +02:00
|
|
|
i.initialize(imageType)
|
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-06-07 16:57:56 +02:00
|
|
|
i.img = atlas.NewImage(i.width, i.height, imageType)
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 07:27:21 +01:00
|
|
|
func (i *Image) invalidatePixels() {
|
2019-11-08 21:43:33 +01:00
|
|
|
i.pixels = nil
|
2022-03-20 20:11:36 +01:00
|
|
|
i.mask = 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) {
|
2022-03-20 20:36:32 +01:00
|
|
|
if !i.needsToResolvePixels {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-20 20:11:36 +01:00
|
|
|
i.img.ReplacePixels(i.pixels, i.mask)
|
2022-03-21 07:27:21 +01:00
|
|
|
if !keepPendingPixels {
|
2022-03-20 20:36:32 +01:00
|
|
|
i.pixels = nil
|
2022-03-20 20:11:36 +01:00
|
|
|
i.mask = nil
|
2019-10-11 20:09:43 +02:00
|
|
|
}
|
2022-03-20 20:36:32 +01:00
|
|
|
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() {
|
2022-06-24 18:42:40 +02:00
|
|
|
if tryAddDelayedCommand(func() {
|
2020-06-29 14:45:57 +02:00
|
|
|
i.MarkDisposed()
|
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-21 07:27:21 +01:00
|
|
|
i.invalidatePixels()
|
2020-02-16 11:01:16 +01:00
|
|
|
i.img.MarkDisposed()
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
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 20:11:36 +01:00
|
|
|
idx := (y*img.width + x)
|
|
|
|
if img.pixels != nil {
|
|
|
|
if img.mask == nil {
|
|
|
|
return img.pixels[4*idx], img.pixels[4*idx+1], img.pixels[4*idx+2], img.pixels[4*idx+3], nil
|
|
|
|
}
|
|
|
|
if img.mask[idx/8]<<(idx%8)&1 != 0 {
|
|
|
|
return img.pixels[4*idx], img.pixels[4*idx+1], img.pixels[4*idx+2], img.pixels[4*idx+3], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
img.resolvePendingPixels(false)
|
2020-04-17 14:34:46 +02:00
|
|
|
}
|
2020-04-18 13:01:40 +02:00
|
|
|
|
2022-03-20 20:11:36 +01:00
|
|
|
pix, err := img.img.Pixels(graphicsDriver)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, 0, 0, err
|
|
|
|
}
|
|
|
|
img.pixels = pix
|
|
|
|
// When pixels represents the whole pixels, the mask is not needed.
|
|
|
|
img.mask = nil
|
|
|
|
return img.pixels[4*idx], img.pixels[4*idx+1], img.pixels[4*idx+2], img.pixels[4*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-21 07:19:06 +01:00
|
|
|
func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) {
|
|
|
|
if l := 4 * width * 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)
|
2022-06-24 18:42:40 +02:00
|
|
|
if tryAddDelayedCommand(func() {
|
2022-03-21 07:19:06 +01:00
|
|
|
i.ReplacePixels(copied, x, y, width, height)
|
2022-03-19 19:51:57 +01:00
|
|
|
}) {
|
2022-03-20 08:44:11 +01:00
|
|
|
return
|
2022-03-19 19:51:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 07:19:06 +01:00
|
|
|
if x == 0 && y == 0 && width == i.width && height == i.height {
|
2022-03-21 07:27:21 +01:00
|
|
|
i.invalidatePixels()
|
2022-03-21 07:19:06 +01:00
|
|
|
i.img.ReplacePixels(pix, nil)
|
|
|
|
return
|
2022-03-20 10:01:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 07:19:06 +01:00
|
|
|
// TODO: If width/height is big enough, ReplacePixels can be called instead of replacePendingPixels.
|
|
|
|
// Check if this is efficient.
|
2019-09-26 20:12:21 +02:00
|
|
|
|
2022-03-20 10:01:27 +01:00
|
|
|
i.replacePendingPixels(pix, x, y, width, height)
|
2020-04-18 17:18:10 +02:00
|
|
|
}
|
|
|
|
|
2022-03-20 20:11:36 +01:00
|
|
|
func (img *Image) replacePendingPixels(pix []byte, x, y, width, height int) {
|
|
|
|
if img.pixels == nil {
|
|
|
|
img.pixels = make([]byte, 4*img.width*img.height)
|
|
|
|
if img.mask == nil {
|
|
|
|
img.mask = make([]byte, (img.width*img.height-1)/8+1)
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 10:01:27 +01:00
|
|
|
for j := 0; j < height; j++ {
|
2022-03-20 20:11:36 +01:00
|
|
|
copy(img.pixels[4*((j+y)*img.width+x):], pix[4*j*width:4*(j+1)*width])
|
|
|
|
}
|
|
|
|
|
2022-03-21 07:20:52 +01:00
|
|
|
// A mask is created only when partial regions are replaced by replacePendingPixels.
|
2022-03-20 20:11:36 +01:00
|
|
|
if img.mask != nil {
|
|
|
|
for j := 0; j < height; j++ {
|
|
|
|
for i := 0; i < width; i++ {
|
|
|
|
idx := (y+j)*img.width + x + i
|
|
|
|
img.mask[idx/8] |= 1 << (idx % 8)
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 10:01:27 +01:00
|
|
|
}
|
2022-03-20 20:11:36 +01:00
|
|
|
|
|
|
|
img.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-04-03 19:15:33 +02: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() {
|
2022-06-24 18:42:40 +02:00
|
|
|
if tryAddDelayedCommand(func() {
|
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 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)
|
2022-03-21 07:27:21 +01:00
|
|
|
i.invalidatePixels()
|
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
|
|
|
}
|
|
|
|
|
2022-04-03 19:15:33 +02:00
|
|
|
func NewShader(ir *shaderir.Program) *Shader {
|
2022-06-24 18:30:02 +02:00
|
|
|
s := &Shader{}
|
|
|
|
s.initialize(ir)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) initialize(ir *shaderir.Program) {
|
|
|
|
if maybeCanAddDelayedCommand() {
|
2022-06-24 18:42:40 +02:00
|
|
|
if tryAddDelayedCommand(func() {
|
2022-06-24 18:30:02 +02:00
|
|
|
s.initialize(ir)
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
2020-05-29 20:21:45 +02:00
|
|
|
}
|
2022-06-24 18:30:02 +02:00
|
|
|
s.shader = atlas.NewShader(ir)
|
2020-05-29 20:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) MarkDisposed() {
|
2022-06-24 18:30:02 +02:00
|
|
|
if maybeCanAddDelayedCommand() {
|
2022-06-24 18:42:40 +02:00
|
|
|
if tryAddDelayedCommand(func() {
|
2022-06-24 18:30:02 +02:00
|
|
|
s.MarkDisposed()
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 20:21:45 +02:00
|
|
|
s.shader.MarkDisposed()
|
|
|
|
s.shader = nil
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|