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"
|
2022-08-05 16:57:56 +02:00
|
|
|
"image"
|
2019-09-19 05:02:35 +02:00
|
|
|
|
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
|
|
|
|
|
2022-07-05 06:26:45 +02:00
|
|
|
pixels []byte
|
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() {
|
2023-02-21 07:51:46 +01:00
|
|
|
i.initializeImpl(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
|
|
|
}
|
2023-02-21 07:51:46 +01:00
|
|
|
i.initializeImpl(imageType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) initializeImpl(imageType atlas.ImageType) {
|
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
|
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() {
|
2023-02-21 07:51:46 +01:00
|
|
|
i.markDisposedImpl()
|
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
|
|
|
}
|
2023-02-21 07:51:46 +01:00
|
|
|
i.markDisposedImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) markDisposedImpl() {
|
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-08-05 16:57:56 +02:00
|
|
|
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, x, y, width, height int) error {
|
|
|
|
checkDelayedCommandsFlushed("ReadPixels")
|
2020-04-17 19:11:23 +02:00
|
|
|
|
2022-08-05 16:57:56 +02:00
|
|
|
r := image.Rect(x, y, x+width, y+height).Intersect(image.Rect(0, 0, i.width, i.height))
|
|
|
|
if r.Empty() {
|
|
|
|
for i := range pixels {
|
|
|
|
pixels[i] = 0
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-17 14:34:46 +02:00
|
|
|
}
|
2020-04-18 13:01:40 +02:00
|
|
|
|
2022-08-05 16:57:56 +02:00
|
|
|
if i.pixels == nil {
|
|
|
|
pix := make([]byte, 4*i.width*i.height)
|
|
|
|
if err := i.img.ReadPixels(graphicsDriver, pix); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i.pixels = pix
|
2022-03-20 20:11:36 +01:00
|
|
|
}
|
2022-08-05 16:57:56 +02:00
|
|
|
|
|
|
|
dstBaseX := r.Min.X - x
|
|
|
|
dstBaseY := r.Min.Y - y
|
|
|
|
srcBaseX := r.Min.X
|
|
|
|
srcBaseY := r.Min.Y
|
|
|
|
lineWidth := 4 * r.Dx()
|
|
|
|
for j := 0; j < r.Dy(); j++ {
|
|
|
|
dstX := 4 * ((dstBaseY+j)*width + dstBaseX)
|
|
|
|
srcX := 4 * ((srcBaseY+j)*i.width + srcBaseX)
|
|
|
|
copy(pixels[dstX:dstX+lineWidth], i.pixels[srcX:srcX+lineWidth])
|
|
|
|
}
|
|
|
|
return nil
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) (string, 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-08-07 20:29:12 +02:00
|
|
|
// WritePixels replaces the pixels at the specified region.
|
|
|
|
func (i *Image) WritePixels(pix []byte, x, y, width, height int) {
|
2022-03-21 07:19:06 +01:00
|
|
|
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() {
|
2023-02-21 07:51:46 +01:00
|
|
|
i.writePixelsImpl(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
|
|
|
}
|
|
|
|
}
|
2023-02-21 07:51:46 +01:00
|
|
|
i.writePixelsImpl(pix, x, y, width, height)
|
|
|
|
}
|
2022-03-19 19:51:57 +01:00
|
|
|
|
2023-02-21 07:51:46 +01:00
|
|
|
func (i *Image) writePixelsImpl(pix []byte, x, y, width, height int) {
|
2022-07-05 06:26:45 +02:00
|
|
|
i.invalidatePixels()
|
2022-08-07 20:24:46 +02:00
|
|
|
i.img.WritePixels(pix, x, y, width, height)
|
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-12-03 08:11:26 +01:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 1][2]float32, shader *Shader, uniforms []uint32, 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-12-02 13:04:03 +01:00
|
|
|
vs := make([]float32, len(vertices))
|
|
|
|
copy(vs, vertices)
|
|
|
|
is := make([]uint16, len(indices))
|
|
|
|
copy(is, indices)
|
2022-12-03 13:47:10 +01:00
|
|
|
us := make([]uint32, len(uniforms))
|
|
|
|
copy(us, uniforms)
|
2022-06-24 18:42:40 +02:00
|
|
|
if tryAddDelayedCommand(func() {
|
2023-02-21 07:51:46 +01:00
|
|
|
i.drawTrianglesImpl(srcs, vs, is, blend, dstRegion, srcRegion, subimageOffsets, shader, us, 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
|
|
|
}
|
2023-02-21 07:51:46 +01:00
|
|
|
i.drawTrianglesImpl(srcs, vertices, indices, blend, dstRegion, srcRegion, subimageOffsets, shader, uniforms, evenOdd)
|
|
|
|
}
|
2019-09-26 20:12:21 +02:00
|
|
|
|
2023-02-21 07:51:46 +01:00
|
|
|
func (i *Image) drawTrianglesImpl(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 1][2]float32, shader *Shader, uniforms []uint32, evenOdd bool) {
|
2022-07-12 18:46:02 +02:00
|
|
|
var imgs [graphics.ShaderImageCount]*atlas.Image
|
2022-10-02 16:49:07 +02:00
|
|
|
for i, img := range srcs {
|
|
|
|
if img == nil {
|
|
|
|
continue
|
2020-07-17 18:09:58 +02:00
|
|
|
}
|
2022-10-02 16:49:07 +02:00
|
|
|
imgs[i] = img.img
|
2020-07-05 18:55:46 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 07:27:21 +01:00
|
|
|
i.invalidatePixels()
|
2022-10-15 11:58:56 +02:00
|
|
|
i.img.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegion, subimageOffsets, shader.shader, uniforms, evenOdd)
|
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() {
|
2023-02-21 07:51:46 +01:00
|
|
|
s.initializeImpl(ir)
|
2022-06-24 18:30:02 +02:00
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
2020-05-29 20:21:45 +02:00
|
|
|
}
|
2023-02-21 07:51:46 +01:00
|
|
|
s.initializeImpl(ir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) initializeImpl(ir *shaderir.Program) {
|
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() {
|
2023-02-21 07:51:46 +01:00
|
|
|
s.markDisposedImpl()
|
2022-06-24 18:30:02 +02:00
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 07:51:46 +01:00
|
|
|
s.markDisposedImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) markDisposedImpl() {
|
2020-05-29 20:21:45 +02:00
|
|
|
s.shader.MarkDisposed()
|
|
|
|
s.shader = nil
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
2022-10-02 11:19:26 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
NearestFilterShader = &Shader{shader: atlas.NearestFilterShader}
|
|
|
|
LinearFilterShader = &Shader{shader: atlas.LinearFilterShader}
|
|
|
|
)
|