mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
internal/restorable: integrate Image functions into internal/atlas
Updates #805
This commit is contained in:
parent
6151fd313f
commit
59896e4447
@ -132,7 +132,7 @@ func (b *backend) extendIfNeeded(width, height int) {
|
||||
}
|
||||
|
||||
// Assume that the screen image is never extended.
|
||||
newImg := restorable.NewImage(width, height, false)
|
||||
newImg := newClearedImage(width, height, false)
|
||||
|
||||
// Use DrawTriangles instead of WritePixels because the image i might be stale and not have its pixels
|
||||
// information.
|
||||
@ -150,6 +150,44 @@ func (b *backend) extendIfNeeded(width, height int) {
|
||||
b.height = height
|
||||
}
|
||||
|
||||
// newClearedImage creates an emtpy image with the given size.
|
||||
//
|
||||
// Note that Dispose is not called automatically.
|
||||
func newClearedImage(width, height int, screen bool) *restorable.Image {
|
||||
i := &restorable.Image{
|
||||
Image: graphicscommand.NewImage(width, height, screen),
|
||||
}
|
||||
|
||||
// This needs to use 'InternalSize' to render the whole region, or edges are unexpectedly cleared on some
|
||||
// devices.
|
||||
iw, ih := i.Image.InternalSize()
|
||||
clearImage(i.Image, image.Rect(0, 0, iw, ih))
|
||||
return i
|
||||
}
|
||||
|
||||
func clearImage(i *graphicscommand.Image, region image.Rectangle) {
|
||||
vs := quadVertices(float32(region.Min.X), float32(region.Min.Y), float32(region.Max.X), float32(region.Max.Y), 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
is := graphics.QuadIndices()
|
||||
i.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendClear, region, [graphics.ShaderImageCount]image.Rectangle{}, restorable.ClearShader.Shader, nil, graphicsdriver.FillAll)
|
||||
}
|
||||
|
||||
func (b *backend) clearPixels(region image.Rectangle) {
|
||||
if region.Dx() <= 0 || region.Dy() <= 0 {
|
||||
panic("atlas: width/height must be positive")
|
||||
}
|
||||
clearImage(b.restorable.Image, region.Intersect(image.Rect(0, 0, b.width, b.height)))
|
||||
}
|
||||
|
||||
func (b *backend) writePixels(pixels *graphics.ManagedBytes, region image.Rectangle) {
|
||||
if region.Dx() <= 0 || region.Dy() <= 0 {
|
||||
panic("atlas: width/height must be positive")
|
||||
}
|
||||
if !region.In(image.Rect(0, 0, b.width, b.height)) {
|
||||
panic(fmt.Sprintf("atlas: out of range %v", region))
|
||||
}
|
||||
b.restorable.Image.WritePixels(pixels, region)
|
||||
}
|
||||
|
||||
var (
|
||||
// backendsM is a mutex for critical sections of the backend and packing.Node objects.
|
||||
backendsM sync.Mutex
|
||||
@ -518,7 +556,7 @@ func (i *Image) writePixels(pix []byte, region image.Rectangle) {
|
||||
region = region.Add(r.Min)
|
||||
|
||||
if pix == nil {
|
||||
i.backend.restorable.ClearPixels(region)
|
||||
i.backend.clearPixels(region)
|
||||
return
|
||||
}
|
||||
|
||||
@ -558,16 +596,6 @@ func (i *Image) writePixels(pix []byte, region image.Rectangle) {
|
||||
i.backend.writePixels(pixb, r)
|
||||
}
|
||||
|
||||
func (b *backend) writePixels(pixels *graphics.ManagedBytes, region image.Rectangle) {
|
||||
if region.Dx() <= 0 || region.Dy() <= 0 {
|
||||
panic("atlas: width/height must be positive")
|
||||
}
|
||||
if !region.In(image.Rect(0, 0, b.width, b.height)) {
|
||||
panic(fmt.Sprintf("atlas: out of range %v", region))
|
||||
}
|
||||
b.restorable.Image.WritePixels(pixels, region)
|
||||
}
|
||||
|
||||
// ReadPixels reads pixels on the given region to the given slice pixels.
|
||||
//
|
||||
// ReadPixels blocks until BeginFrame is called if necessary in order to ensure this is called in a frame (between BeginFrame and EndFrame).
|
||||
@ -653,7 +681,7 @@ func (i *Image) deallocate() {
|
||||
if !i.backend.page.IsEmpty() {
|
||||
// As this part can be reused, this should be cleared explicitly.
|
||||
r := i.regionWithPadding()
|
||||
i.backend.restorable.ClearPixels(r)
|
||||
i.backend.clearPixels(r)
|
||||
return
|
||||
}
|
||||
|
||||
@ -715,7 +743,7 @@ func (i *Image) allocate(forbiddenBackends []*backend, asSource bool) {
|
||||
}
|
||||
// A screen image doesn't have a padding.
|
||||
i.backend = &backend{
|
||||
restorable: restorable.NewImage(i.width, i.height, true),
|
||||
restorable: newClearedImage(i.width, i.height, true),
|
||||
width: i.width,
|
||||
height: i.height,
|
||||
}
|
||||
@ -732,7 +760,7 @@ func (i *Image) allocate(forbiddenBackends []*backend, asSource bool) {
|
||||
}
|
||||
|
||||
i.backend = &backend{
|
||||
restorable: restorable.NewImage(wp, hp, false),
|
||||
restorable: newClearedImage(wp, hp, false),
|
||||
width: wp,
|
||||
height: hp,
|
||||
source: asSource && i.imageType == ImageTypeRegular,
|
||||
@ -780,7 +808,7 @@ loop:
|
||||
}
|
||||
|
||||
b := &backend{
|
||||
restorable: restorable.NewImage(width, height, false),
|
||||
restorable: newClearedImage(width, height, false),
|
||||
width: width,
|
||||
height: height,
|
||||
page: packing.NewPage(width, height, maxSize),
|
||||
|
@ -15,11 +15,7 @@
|
||||
package restorable
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
||||
)
|
||||
|
||||
// Image represents an image.
|
||||
@ -28,50 +24,4 @@ type Image struct {
|
||||
// This member is exported on purpose.
|
||||
// TODO: Move the implementation to internal/atlas package (#805).
|
||||
Image *graphicscommand.Image
|
||||
|
||||
width int
|
||||
height int
|
||||
}
|
||||
|
||||
// NewImage creates an emtpy image with the given size.
|
||||
//
|
||||
// The returned image is cleared.
|
||||
//
|
||||
// Note that Dispose is not called automatically.
|
||||
func NewImage(width, height int, screen bool) *Image {
|
||||
i := &Image{
|
||||
Image: graphicscommand.NewImage(width, height, screen),
|
||||
width: width,
|
||||
height: height,
|
||||
}
|
||||
|
||||
// This needs to use 'InternalSize' to render the whole region, or edges are unexpectedly cleared on some
|
||||
// devices.
|
||||
iw, ih := i.Image.InternalSize()
|
||||
clearImage(i.Image, image.Rect(0, 0, iw, ih))
|
||||
return i
|
||||
}
|
||||
|
||||
// quadVertices returns vertices to render a quad. These values are passed to graphicscommand.Image.
|
||||
func quadVertices(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, cr, cg, cb, ca float32) []float32 {
|
||||
return []float32{
|
||||
dx0, dy0, sx0, sy0, cr, cg, cb, ca,
|
||||
dx1, dy0, sx1, sy0, cr, cg, cb, ca,
|
||||
dx0, dy1, sx0, sy1, cr, cg, cb, ca,
|
||||
dx1, dy1, sx1, sy1, cr, cg, cb, ca,
|
||||
}
|
||||
}
|
||||
|
||||
func clearImage(i *graphicscommand.Image, region image.Rectangle) {
|
||||
vs := quadVertices(float32(region.Min.X), float32(region.Min.Y), float32(region.Max.X), float32(region.Max.Y), 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
is := graphics.QuadIndices()
|
||||
i.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendClear, region, [graphics.ShaderImageCount]image.Rectangle{}, clearShader.Shader, nil, graphicsdriver.FillAll)
|
||||
}
|
||||
|
||||
// ClearPixels clears the specified region by WritePixels.
|
||||
func (i *Image) ClearPixels(region image.Rectangle) {
|
||||
if region.Dx() <= 0 || region.Dy() <= 0 {
|
||||
panic("restorable: width/height must be positive")
|
||||
}
|
||||
clearImage(i.Image, region.Intersect(image.Rect(0, 0, i.width, i.height)))
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ var (
|
||||
NearestFilterShaderIR *shaderir.Program
|
||||
LinearFilterShader *Shader
|
||||
LinearFilterShaderIR *shaderir.Program
|
||||
clearShader *Shader
|
||||
ClearShader *Shader
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -84,5 +84,5 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
|
||||
NearestFilterShader = NewShader(nearestIR)
|
||||
LinearFilterShaderIR = linearIR
|
||||
LinearFilterShader = NewShader(linearIR)
|
||||
clearShader = NewShader(clearIR)
|
||||
ClearShader = NewShader(clearIR)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user