ebiten/internal/buffered/image.go

217 lines
5.8 KiB
Go
Raw Normal View History

// 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 (
"fmt"
2023-04-27 17:55:10 +02:00
"image"
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
type Image struct {
img *atlas.Image
width int
height int
// pixels is valid only when restorable.AlwaysReadPixelsFromGPU() returns true.
pixels []byte
}
func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
if err := atlas.BeginFrame(graphicsDriver); err != nil {
return err
}
2022-06-24 18:42:40 +02:00
flushDelayedCommands()
return nil
}
func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) error {
return atlas.EndFrame(graphicsDriver, swapBuffersForGL)
}
func NewImage(width, height int, imageType atlas.ImageType) *Image {
return &Image{
width: width,
height: height,
img: atlas.NewImage(width, height, imageType),
}
}
func (i *Image) invalidatePixels() {
i.pixels = nil
}
func (i *Image) MarkDisposed() {
if maybeCanAddDelayedCommand() {
2022-06-24 18:42:40 +02:00
if tryAddDelayedCommand(func() {
i.markDisposedImpl()
2020-06-29 18:16:50 +02:00
}) {
return
}
}
i.markDisposedImpl()
}
func (i *Image) markDisposedImpl() {
i.invalidatePixels()
i.img.MarkDisposed()
}
2023-04-27 17:55:10 +02:00
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) error {
checkDelayedCommandsFlushed("ReadPixels")
// If restorable.AlwaysReadPixelsFromGPU() returns false, the pixel data is cached in the restorable package.
if !restorable.AlwaysReadPixelsFromGPU() {
2023-04-27 17:55:10 +02:00
if err := i.img.ReadPixels(graphicsDriver, pixels, region); err != nil {
return err
}
return nil
}
if i.pixels == nil {
pix := make([]byte, 4*i.width*i.height)
2023-04-27 17:55:10 +02:00
if err := i.img.ReadPixels(graphicsDriver, pix, image.Rect(0, 0, i.width, i.height)); err != nil {
return err
}
i.pixels = pix
}
2023-04-27 17:55:10 +02:00
lineWidth := 4 * region.Dx()
for j := 0; j < region.Dy(); j++ {
dstX := 4 * j * region.Dx()
srcX := 4 * ((region.Min.Y+j)*i.width + region.Min.X)
copy(pixels[dstX:dstX+lineWidth], i.pixels[srcX:srcX+lineWidth])
}
return nil
}
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) (string, error) {
checkDelayedCommandsFlushed("Dump")
return i.img.DumpScreenshot(graphicsDriver, name, blackbg)
}
// WritePixels replaces the pixels at the specified region.
2023-04-27 17:55:10 +02:00
func (i *Image) WritePixels(pix []byte, region image.Rectangle) {
if l := 4 * region.Dx() * region.Dy(); len(pix) != l {
panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l))
}
if maybeCanAddDelayedCommand() {
copied := make([]byte, len(pix))
copy(copied, pix)
2022-06-24 18:42:40 +02:00
if tryAddDelayedCommand(func() {
2023-04-27 17:55:10 +02:00
i.writePixelsImpl(copied, region)
}) {
2022-03-20 08:44:11 +01:00
return
}
}
2023-04-27 17:55:10 +02:00
i.writePixelsImpl(pix, region)
}
2023-04-27 17:55:10 +02:00
func (i *Image) writePixelsImpl(pix []byte, region image.Rectangle) {
i.invalidatePixels()
2023-04-27 17:55:10 +02:00
i.img.WritePixels(pix, region)
}
// DrawTriangles draws the src image with the given vertices.
//
// Copying vertices and indices is the caller's responsibility.
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, evenOdd bool) {
for _, src := range srcs {
if i == src {
panic("buffered: Image.DrawTriangles: source images must be different from the receiver")
}
}
if maybeCanAddDelayedCommand() {
vs := make([]float32, len(vertices))
copy(vs, vertices)
is := make([]uint16, len(indices))
copy(is, indices)
us := make([]uint32, len(uniforms))
copy(us, uniforms)
2022-06-24 18:42:40 +02:00
if tryAddDelayedCommand(func() {
i.drawTrianglesImpl(srcs, vs, is, blend, dstRegion, srcRegions, shader, us, evenOdd)
2020-06-29 18:16:50 +02:00
}) {
return
}
}
i.drawTrianglesImpl(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, evenOdd)
}
func (i *Image) drawTrianglesImpl(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, evenOdd bool) {
var imgs [graphics.ShaderImageCount]*atlas.Image
for i, img := range srcs {
if img == nil {
continue
}
imgs[i] = img.img
}
i.invalidatePixels()
i.img.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, evenOdd)
2020-05-29 20:21:45 +02:00
}
type Shader struct {
shader *atlas.Shader
2020-05-29 20:21:45 +02:00
}
func NewShader(ir *shaderir.Program) *Shader {
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() {
s.initializeImpl(ir)
}) {
return
}
2020-05-29 20:21:45 +02:00
}
s.initializeImpl(ir)
}
func (s *Shader) initializeImpl(ir *shaderir.Program) {
s.shader = atlas.NewShader(ir)
2020-05-29 20:21:45 +02:00
}
func (s *Shader) MarkDisposed() {
if maybeCanAddDelayedCommand() {
2022-06-24 18:42:40 +02:00
if tryAddDelayedCommand(func() {
s.markDisposedImpl()
}) {
return
}
}
s.markDisposedImpl()
}
func (s *Shader) markDisposedImpl() {
2020-05-29 20:21:45 +02:00
s.shader.MarkDisposed()
s.shader = nil
}
var (
NearestFilterShader = &Shader{shader: atlas.NearestFilterShader}
LinearFilterShader = &Shader{shader: atlas.LinearFilterShader}
)