Compare commits

...

2 Commits

Author SHA1 Message Date
Hajime Hoshi
cbecbe2bad internal/mipmap: refactoring 2024-09-12 15:55:27 +09:00
Hajime Hoshi
6389db14a2 internal/mipmap: reuse internal mipmap images whenever possible
Closes #3093
2024-09-12 15:30:31 +09:00

View File

@ -41,7 +41,12 @@ type Mipmap struct {
height int height int
imageType atlas.ImageType imageType atlas.ImageType
orig *buffered.Image orig *buffered.Image
imgs map[int]*buffered.Image imgs map[int]imageWithDirtyFlag
}
type imageWithDirtyFlag struct {
img *buffered.Image
dirty bool
} }
func New(width, height int, imageType atlas.ImageType) *Mipmap { func New(width, height int, imageType atlas.ImageType) *Mipmap {
@ -59,7 +64,14 @@ func (m *Mipmap) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name str
func (m *Mipmap) WritePixels(pix []byte, region image.Rectangle) { func (m *Mipmap) WritePixels(pix []byte, region image.Rectangle) {
m.orig.WritePixels(pix, region) m.orig.WritePixels(pix, region)
m.deallocateMipmaps() m.markDirty()
}
func (m *Mipmap) markDirty() {
for i, img := range m.imgs {
img.dirty = true
m.imgs[i] = img
}
} }
func (m *Mipmap) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) (ok bool, err error) { func (m *Mipmap) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) (ok bool, err error) {
@ -125,14 +137,17 @@ func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Mipmap, verti
} }
m.orig.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule, hint) m.orig.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule, hint)
m.deallocateMipmaps() m.markDirty()
} }
func (m *Mipmap) setImg(level int, img *buffered.Image) { func (m *Mipmap) setImg(level int, img *buffered.Image) {
if m.imgs == nil { if m.imgs == nil {
m.imgs = map[int]*buffered.Image{} m.imgs = map[int]imageWithDirtyFlag{}
}
m.imgs[level] = imageWithDirtyFlag{
img: img,
dirty: false,
} }
m.imgs[level] = img
} }
func (m *Mipmap) level(level int) *buffered.Image { func (m *Mipmap) level(level int) *buffered.Image {
@ -144,28 +159,33 @@ func (m *Mipmap) level(level int) *buffered.Image {
panic("mipmap: mipmap images for a screen image is not implemented yet") panic("mipmap: mipmap images for a screen image is not implemented yet")
} }
if img, ok := m.imgs[level]; ok { img, ok := m.imgs[level]
return img if ok && !img.dirty {
return img.img
} }
var w, h int
var src *buffered.Image var src *buffered.Image
vs := make([]float32, 4*graphics.VertexFloatCount) vs := make([]float32, 4*graphics.VertexFloatCount)
switch { switch {
case level == 1: case level == 1:
src = m.orig src = m.orig
graphics.QuadVerticesFromSrcAndMatrix(vs, 0, 0, float32(m.width), float32(m.height), 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1) w = m.width
h = m.height
case level > 1: case level > 1:
src = m.level(level - 1) src = m.level(level - 1)
if src == nil { if src == nil {
m.setImg(level, nil) m.setImg(level, nil)
return nil return nil
} }
w := sizeForLevel(m.width, level-1) w = sizeForLevel(m.width, level-1)
h := sizeForLevel(m.height, level-1) h = sizeForLevel(m.height, level-1)
graphics.QuadVerticesFromSrcAndMatrix(vs, 0, 0, float32(w), float32(h), 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
default: default:
panic(fmt.Sprintf("mipmap: invalid level: %d", level)) panic(fmt.Sprintf("mipmap: invalid level: %d", level))
} }
graphics.QuadVerticesFromSrcAndMatrix(vs, 0, 0, float32(w), float32(h), 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
is := graphics.QuadIndices() is := graphics.QuadIndices()
w2 := sizeForLevel(m.width, level) w2 := sizeForLevel(m.width, level)
@ -182,16 +202,20 @@ func (m *Mipmap) level(level int) *buffered.Image {
return nil return nil
} }
s := buffered.NewImage(w2, h2, m.imageType) var s *buffered.Image
if img.img != nil {
// As s is overwritten, this doesn't have to be cleared.
s = img.img
} else {
s = buffered.NewImage(w2, h2, m.imageType)
}
dstRegion := image.Rect(0, 0, w2, h2) dstRegion := image.Rect(0, 0, w2, h2)
w := sizeForLevel(m.width, level-1)
h := sizeForLevel(m.height, level-1)
srcRegion := image.Rect(0, 0, w, h) srcRegion := image.Rect(0, 0, w, h)
s.DrawTriangles([graphics.ShaderSrcImageCount]*buffered.Image{src}, vs, is, graphicsdriver.BlendCopy, dstRegion, [graphics.ShaderSrcImageCount]image.Rectangle{srcRegion}, atlas.LinearFilterShader, nil, graphicsdriver.FillRuleFillAll, restorable.HintOverwriteDstRegion) s.DrawTriangles([graphics.ShaderSrcImageCount]*buffered.Image{src}, vs, is, graphicsdriver.BlendCopy, dstRegion, [graphics.ShaderSrcImageCount]image.Rectangle{srcRegion}, atlas.LinearFilterShader, nil, graphicsdriver.FillRuleFillAll, restorable.HintOverwriteDstRegion)
m.setImg(level, s) m.setImg(level, s)
return m.imgs[level] return m.imgs[level].img
} }
func sizeForLevel(x int, level int) int { func sizeForLevel(x int, level int) int {
@ -205,19 +229,16 @@ func sizeForLevel(x int, level int) int {
} }
func (m *Mipmap) Deallocate() { func (m *Mipmap) Deallocate() {
m.deallocateMipmaps()
m.orig.Deallocate()
}
func (m *Mipmap) deallocateMipmaps() {
for _, img := range m.imgs { for _, img := range m.imgs {
if img != nil { if img.img == nil {
img.Deallocate() continue
} }
img.img.Deallocate()
} }
for k := range m.imgs { for k := range m.imgs {
delete(m.imgs, k) delete(m.imgs, k)
} }
m.orig.Deallocate()
} }
// mipmapLevel returns an appropriate mipmap level for the given distance. // mipmapLevel returns an appropriate mipmap level for the given distance.