internal/mipmap: refactoring

This change replaces the member `volatile` with `imageType` to make
the code more explicit.

In the old code, it was not obvious whether a mipmap was used for a
screen image. Actually a mipmap was not used since `canSkipMipmap` is
always true for a screen image, but this was too tricky.
This commit is contained in:
Hajime Hoshi 2022-09-28 03:07:38 +09:00
parent 6b35ad4a88
commit 9492b4ecf5

View File

@ -26,22 +26,30 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir" "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
) )
func canUseMipmap(imageType atlas.ImageType) bool {
switch imageType {
case atlas.ImageTypeRegular, atlas.ImageTypeUnmanaged:
return true
}
return false
}
// Mipmap is a set of buffered.Image sorted by the order of mipmap level. // Mipmap is a set of buffered.Image sorted by the order of mipmap level.
// The level 0 image is a regular image and higher-level images are used for mipmap. // The level 0 image is a regular image and higher-level images are used for mipmap.
type Mipmap struct { type Mipmap struct {
width int width int
height int height int
volatile bool imageType atlas.ImageType
orig *buffered.Image orig *buffered.Image
imgs map[int]*buffered.Image imgs map[int]*buffered.Image
} }
func New(width, height int, imageType atlas.ImageType) *Mipmap { func New(width, height int, imageType atlas.ImageType) *Mipmap {
return &Mipmap{ return &Mipmap{
width: width, width: width,
height: height, height: height,
orig: buffered.NewImage(width, height, imageType), orig: buffered.NewImage(width, height, imageType),
volatile: imageType == atlas.ImageTypeVolatile, imageType: imageType,
} }
} }
@ -65,7 +73,7 @@ func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageCount]*Mipmap, vertices
level := 0 level := 0
// TODO: Do we need to check all the sources' states of being volatile? // TODO: Do we need to check all the sources' states of being volatile?
if !canSkipMipmap && srcs[0] != nil && !srcs[0].volatile { if !canSkipMipmap && srcs[0] != nil && canUseMipmap(srcs[0].imageType) {
level = math.MaxInt32 level = math.MaxInt32
for i := 0; i < len(indices)/3; i++ { for i := 0; i < len(indices)/3; i++ {
const n = graphics.VertexFloatCount const n = graphics.VertexFloatCount
@ -137,8 +145,8 @@ func (m *Mipmap) level(level int) *buffered.Image {
panic("mipmap: level must be non-zero at level") panic("mipmap: level must be non-zero at level")
} }
if m.volatile { if !canUseMipmap(m.imageType) {
panic("mipmap: mipmap images for a volatile image is not implemented yet") panic("mipmap: mipmap images for a volatile or a screen image is not implemented yet")
} }
if img, ok := m.imgs[level]; ok { if img, ok := m.imgs[level]; ok {
@ -182,11 +190,7 @@ func (m *Mipmap) level(level int) *buffered.Image {
return nil return nil
} }
t := atlas.ImageTypeRegular s := buffered.NewImage(w2, h2, m.imageType)
if m.volatile {
t = atlas.ImageTypeVolatile
}
s := buffered.NewImage(w2, h2, t)
dstRegion := graphicsdriver.Region{ dstRegion := graphicsdriver.Region{
X: 0, X: 0,