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"
|
2023-04-27 17:55:10 +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"
|
2024-09-06 08:50:08 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
|
2019-09-19 05:02:35 +02:00
|
|
|
)
|
|
|
|
|
2024-01-15 15:45:00 +01:00
|
|
|
var whiteImage *Image
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
whiteImage = NewImage(3, 3, atlas.ImageTypeRegular)
|
|
|
|
pix := make([]byte, 4*3*3)
|
|
|
|
for i := range pix {
|
|
|
|
pix[i] = 0xff
|
|
|
|
}
|
|
|
|
whiteImage.WritePixels(pix, image.Rect(0, 0, 3, 3))
|
|
|
|
}
|
|
|
|
|
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
|
2024-01-15 15:45:00 +01:00
|
|
|
|
|
|
|
// dotsBuffer is a buffer for drawing a lot of dots.
|
|
|
|
// An entry in this map is the primary data of pixels for ReadPixels.
|
|
|
|
dotsBuffer map[image.Point][4]byte
|
|
|
|
|
|
|
|
// pixels is cached pixels for ReadPixels.
|
|
|
|
// pixels might be out of sync with GPU.
|
|
|
|
// The data of pixels is the secondary data of pixels for ReadPixels.
|
2024-09-06 08:50:08 +02:00
|
|
|
//
|
|
|
|
// pixels is always nil when restorable.AlwaysReadPixelsFromGPU() returns false.
|
2022-07-05 06:26:45 +02:00
|
|
|
pixels []byte
|
2024-01-15 15:45:00 +01:00
|
|
|
|
|
|
|
// pixelsUnsynced represents whether the pixels in CPU and GPU are not synced.
|
|
|
|
pixelsUnsynced bool
|
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 {
|
2023-10-18 15:30:01 +02:00
|
|
|
return &Image{
|
2024-01-15 15:45:00 +01:00
|
|
|
img: atlas.NewImage(width, height, imageType),
|
2022-03-20 10:01:27 +01:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2023-11-03 07:06:22 +01:00
|
|
|
func (i *Image) Deallocate() {
|
|
|
|
i.img.Deallocate()
|
2024-01-15 15:45:00 +01:00
|
|
|
i.dotsBuffer = nil
|
|
|
|
i.pixels = nil
|
|
|
|
i.pixelsUnsynced = false
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2024-03-26 04:59:31 +01:00
|
|
|
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) (bool, error) {
|
2024-01-15 15:45:00 +01:00
|
|
|
if region.Dx() == 1 && region.Dy() == 1 {
|
|
|
|
if c, ok := i.dotsBuffer[region.Min]; ok {
|
|
|
|
copy(pixels, c[:])
|
2024-03-26 04:59:31 +01:00
|
|
|
return true, nil
|
2024-01-15 15:45:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 08:50:08 +02:00
|
|
|
// If restorable.AlwaysReadPixelsFromGPU() returns false, the pixel data is cached in the restorable package.
|
|
|
|
if !restorable.AlwaysReadPixelsFromGPU() {
|
|
|
|
i.syncPixelsIfNeeded()
|
|
|
|
ok, err := i.img.ReadPixels(graphicsDriver, pixels, region)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not call syncPixelsIfNeeded here. This would slow (image/draw).Draw.
|
|
|
|
// See ebiten.TestImageDrawOver.
|
|
|
|
|
2022-08-05 16:57:56 +02:00
|
|
|
if i.pixels == nil {
|
|
|
|
pix := make([]byte, 4*i.width*i.height)
|
2024-03-26 04:59:31 +01:00
|
|
|
ok, err := i.img.ReadPixels(graphicsDriver, pix, image.Rect(0, 0, i.width, i.height))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return false, nil
|
2022-08-05 16:57:56 +02:00
|
|
|
}
|
|
|
|
i.pixels = pix
|
2022-03-20 20:11:36 +01:00
|
|
|
}
|
2022-08-05 16:57:56 +02:00
|
|
|
|
2024-01-15 15:45:00 +01:00
|
|
|
if len(i.dotsBuffer) > 0 {
|
|
|
|
for pos, clr := range i.dotsBuffer {
|
|
|
|
idx := 4 * (pos.Y*i.width + pos.X)
|
|
|
|
i.pixels[idx] = clr[0]
|
|
|
|
i.pixels[idx+1] = clr[1]
|
|
|
|
i.pixels[idx+2] = clr[2]
|
|
|
|
i.pixels[idx+3] = clr[3]
|
|
|
|
delete(i.dotsBuffer, pos)
|
|
|
|
}
|
|
|
|
i.pixelsUnsynced = true
|
|
|
|
}
|
|
|
|
|
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)
|
2022-08-05 16:57:56 +02:00
|
|
|
copy(pixels[dstX:dstX+lineWidth], i.pixels[srcX:srcX+lineWidth])
|
|
|
|
}
|
2024-01-15 15:45:00 +01:00
|
|
|
|
2024-03-26 04:59:31 +01:00
|
|
|
return true, 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) {
|
2024-01-15 15:45:00 +01:00
|
|
|
i.syncPixelsIfNeeded()
|
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.
|
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 {
|
2020-02-16 13:45:26 +01:00
|
|
|
panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l))
|
|
|
|
}
|
2024-01-15 15:45:00 +01:00
|
|
|
|
|
|
|
// Writing one pixel is a special case.
|
2024-01-16 20:16:32 +01:00
|
|
|
// Do not write pixels in GPU, as (image/draw).Image's functions might call WritePixels with pixels one by one.
|
2024-01-15 15:45:00 +01:00
|
|
|
if region.Dx() == 1 && region.Dy() == 1 {
|
2024-01-16 16:00:31 +01:00
|
|
|
// If i.pixels exists, update this instead of adding an entry to dotsBuffer.
|
|
|
|
if i.pixels != nil {
|
|
|
|
idx := 4 * (region.Min.Y*i.width + region.Min.X)
|
|
|
|
i.pixels[idx] = pix[0]
|
|
|
|
i.pixels[idx+1] = pix[1]
|
|
|
|
i.pixels[idx+2] = pix[2]
|
|
|
|
i.pixels[idx+3] = pix[3]
|
|
|
|
i.pixelsUnsynced = true
|
|
|
|
delete(i.dotsBuffer, region.Min)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-15 15:45:00 +01:00
|
|
|
if i.dotsBuffer == nil {
|
|
|
|
i.dotsBuffer = map[image.Point][4]byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var clr [4]byte
|
|
|
|
copy(clr[:], pix)
|
|
|
|
i.dotsBuffer[region.Min] = clr
|
|
|
|
|
|
|
|
if len(i.dotsBuffer) >= 10000 {
|
|
|
|
i.syncPixelsIfNeeded()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If i.pixels is not nil, this indicates ReadPixels is called and might be called again later.
|
|
|
|
// Keep and update the pixels data in this case.
|
|
|
|
if i.pixels != nil {
|
|
|
|
lineWidth := 4 * region.Dx()
|
|
|
|
for j := 0; j < region.Dy(); j++ {
|
|
|
|
dstX := 4 * ((region.Min.Y+j)*i.width + region.Min.X)
|
|
|
|
srcX := 4 * j * region.Dx()
|
|
|
|
copy(i.pixels[dstX:dstX+lineWidth], pix[srcX:srcX+lineWidth])
|
|
|
|
}
|
|
|
|
// pixelsUnsynced can NOT be set false as the outside pixels of the region is not written by WritePixels here.
|
2024-01-16 04:48:47 +01:00
|
|
|
// See the test TestUnsyncedPixels.
|
2024-01-15 15:45:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Even if i.pixels is nil, do not create a pixel cache.
|
|
|
|
// It is in theroy possible to copy the argument pixels, but this tends to consume a lot of memory.
|
|
|
|
// Avoid this unless ReadPixels is called.
|
|
|
|
|
|
|
|
// Remove entries in the dots buffer that are overwritten by this WritePixels call.
|
|
|
|
for pos := range i.dotsBuffer {
|
|
|
|
if !pos.In(region) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(i.dotsBuffer, pos)
|
|
|
|
}
|
2020-02-16 13:45:26 +01:00
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
i.img.WritePixels(pix, region)
|
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.
|
2024-09-08 04:42:02 +02:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *atlas.Shader, uniforms []uint32, fillRule graphicsdriver.FillRule, hint restorable.Hint) {
|
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
|
|
|
}
|
2024-01-15 15:45:00 +01:00
|
|
|
if src != nil {
|
|
|
|
// src's pixels have to be synced between CPU and GPU,
|
|
|
|
// but doesn't have to be cleared since src is not modified in this function.
|
|
|
|
src.syncPixelsIfNeeded()
|
|
|
|
}
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
2024-01-15 15:45:00 +01:00
|
|
|
|
|
|
|
i.syncPixelsIfNeeded()
|
2019-09-26 20:12:21 +02:00
|
|
|
|
2024-06-09 19:02:47 +02:00
|
|
|
var imgs [graphics.ShaderSrcImageCount]*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
|
|
|
}
|
|
|
|
|
2024-09-08 04:42:02 +02:00
|
|
|
i.img.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule, hint)
|
2024-01-15 15:45:00 +01:00
|
|
|
|
|
|
|
// After rendering, the pixel cache is no longer valid.
|
|
|
|
i.pixels = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// syncPixelsIfNeeded syncs the pixels between CPU and GPU.
|
2024-01-16 20:16:32 +01:00
|
|
|
// After syncPixelsIfNeeded, dotsBuffer is cleared, but pixels might remain.
|
2024-01-15 15:45:00 +01:00
|
|
|
func (i *Image) syncPixelsIfNeeded() {
|
2024-01-16 16:20:12 +01:00
|
|
|
if i.pixelsUnsynced {
|
2024-01-15 15:45:00 +01:00
|
|
|
// If this image already has pixels, use WritePixels instead of DrawTriangles for efficiency.
|
|
|
|
for pos, clr := range i.dotsBuffer {
|
|
|
|
idx := 4 * (pos.Y*i.width + pos.X)
|
|
|
|
i.pixels[idx] = clr[0]
|
|
|
|
i.pixels[idx+1] = clr[1]
|
|
|
|
i.pixels[idx+2] = clr[2]
|
|
|
|
i.pixels[idx+3] = clr[3]
|
|
|
|
delete(i.dotsBuffer, pos)
|
|
|
|
}
|
|
|
|
i.img.WritePixels(i.pixels, image.Rect(0, 0, i.width, i.height))
|
|
|
|
i.pixelsUnsynced = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(i.dotsBuffer) == 0 {
|
2024-01-16 16:20:12 +01:00
|
|
|
return
|
2024-01-15 15:45:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
l := len(i.dotsBuffer)
|
|
|
|
vs := make([]float32, l*4*graphics.VertexFloatCount)
|
|
|
|
is := make([]uint32, l*6)
|
|
|
|
sx, sy := float32(1), float32(1)
|
|
|
|
var idx int
|
|
|
|
for p, c := range i.dotsBuffer {
|
|
|
|
dx := float32(p.X)
|
|
|
|
dy := float32(p.Y)
|
|
|
|
crf := float32(c[0]) / 0xff
|
|
|
|
cgf := float32(c[1]) / 0xff
|
|
|
|
cbf := float32(c[2]) / 0xff
|
|
|
|
caf := float32(c[3]) / 0xff
|
|
|
|
|
2024-08-11 15:13:49 +02:00
|
|
|
vidx := 4 * idx
|
|
|
|
iidx := 6 * idx
|
|
|
|
|
|
|
|
vs[graphics.VertexFloatCount*vidx] = dx
|
|
|
|
vs[graphics.VertexFloatCount*vidx+1] = dy
|
|
|
|
vs[graphics.VertexFloatCount*vidx+2] = sx
|
|
|
|
vs[graphics.VertexFloatCount*vidx+3] = sy
|
|
|
|
vs[graphics.VertexFloatCount*vidx+4] = crf
|
|
|
|
vs[graphics.VertexFloatCount*vidx+5] = cgf
|
|
|
|
vs[graphics.VertexFloatCount*vidx+6] = cbf
|
|
|
|
vs[graphics.VertexFloatCount*vidx+7] = caf
|
|
|
|
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+1)] = dx + 1
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+1)+1] = dy
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+1)+2] = sx + 1
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+1)+3] = sy
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+1)+4] = crf
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+1)+5] = cgf
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+1)+6] = cbf
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+1)+7] = caf
|
|
|
|
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+2)] = dx
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+2)+1] = dy + 1
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+2)+2] = sx
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+2)+3] = sy + 1
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+2)+4] = crf
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+2)+5] = cgf
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+2)+6] = cbf
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+2)+7] = caf
|
|
|
|
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+3)] = dx + 1
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+3)+1] = dy + 1
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+3)+2] = sx + 1
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+3)+3] = sy + 1
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+3)+4] = crf
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+3)+5] = cgf
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+3)+6] = cbf
|
|
|
|
vs[graphics.VertexFloatCount*(vidx+3)+7] = caf
|
|
|
|
|
|
|
|
is[iidx] = uint32(vidx)
|
|
|
|
is[iidx+1] = uint32(vidx + 1)
|
|
|
|
is[iidx+2] = uint32(vidx + 2)
|
|
|
|
is[iidx+3] = uint32(vidx + 1)
|
|
|
|
is[iidx+4] = uint32(vidx + 2)
|
|
|
|
is[iidx+5] = uint32(vidx + 3)
|
2024-01-15 15:45:00 +01:00
|
|
|
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
|
2024-06-09 19:02:47 +02:00
|
|
|
srcs := [graphics.ShaderSrcImageCount]*atlas.Image{whiteImage.img}
|
2024-01-15 15:45:00 +01:00
|
|
|
dr := image.Rect(0, 0, i.width, i.height)
|
2024-09-08 14:41:20 +02:00
|
|
|
sr := image.Rect(0, 0, whiteImage.width, whiteImage.height)
|
2024-01-15 15:45:00 +01:00
|
|
|
blend := graphicsdriver.BlendCopy
|
2024-09-08 14:41:20 +02:00
|
|
|
i.img.DrawTriangles(srcs, vs, is, blend, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil, graphicsdriver.FillRuleFillAll, restorable.HintNone)
|
2024-01-15 15:45:00 +01:00
|
|
|
|
2024-09-11 17:58:15 +02:00
|
|
|
clear(i.dotsBuffer)
|
2020-05-29 20:21:45 +02:00
|
|
|
}
|