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
|
|
|
|
|
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() {
|
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
|
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 {
|
2022-07-05 06:26:45 +02:00
|
|
|
return img.pixels[4*idx], img.pixels[4*idx+1], img.pixels[4*idx+2], img.pixels[4*idx+3], nil
|
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
|
|
|
|
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-07-05 06:26:45 +02:00
|
|
|
i.invalidatePixels()
|
|
|
|
i.img.ReplacePixels(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-07-12 18:46:02 +02:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 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
|
2022-07-12 18:46:02 +02:00
|
|
|
var imgs [graphics.ShaderImageCount]*atlas.Image
|
2020-09-20 22:36:47 +02:00
|
|
|
if shader == nil {
|
|
|
|
// Fast path for rendering without a shader (#1355).
|
|
|
|
img := srcs[0]
|
|
|
|
imgs[0] = img.img
|
|
|
|
} else {
|
|
|
|
for i, img := range srcs {
|
|
|
|
if img == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-21 07:27:21 +01:00
|
|
|
i.invalidatePixels()
|
2022-07-05 06:26:45 +02:00
|
|
|
i.img.DrawTriangles(imgs, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, s, 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() {
|
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
|
|
|
}
|