internal/graphicscommand: add attributes to images

This commit is contained in:
Hajime Hoshi 2024-09-07 23:36:44 +09:00
parent 4fa8265c58
commit 1488e5e685
4 changed files with 45 additions and 26 deletions

View File

@ -93,6 +93,8 @@ func (c *drawTrianglesCommand) String() string {
dst := fmt.Sprintf("%d", c.dst.id) dst := fmt.Sprintf("%d", c.dst.id)
if c.dst.screen { if c.dst.screen {
dst += " (screen)" dst += " (screen)"
} else if c.dst.attribute != "" {
dst += " (" + c.dst.attribute + ")"
} }
var srcstrs [graphics.ShaderSrcImageCount]string var srcstrs [graphics.ShaderSrcImageCount]string
@ -104,6 +106,8 @@ func (c *drawTrianglesCommand) String() string {
srcstrs[i] = fmt.Sprintf("%d", src.id) srcstrs[i] = fmt.Sprintf("%d", src.id)
if src.screen { if src.screen {
srcstrs[i] += " (screen)" srcstrs[i] += " (screen)"
} else if src.attribute != "" {
srcstrs[i] += " (" + src.attribute + ")"
} }
} }
@ -339,10 +343,15 @@ type newImageCommand struct {
width int width int
height int height int
screen bool screen bool
attribute string
} }
func (c *newImageCommand) String() string { func (c *newImageCommand) String() string {
return fmt.Sprintf("new-image: result: %d, width: %d, height: %d, screen: %t", c.result.id, c.width, c.height, c.screen) str := fmt.Sprintf("new-image: result: %d, width: %d, height: %d, screen: %t", c.result.id, c.width, c.height, c.screen)
if c.attribute != "" {
str += ", attribute: " + c.attribute
}
return str
} }
// Exec executes a newImageCommand. // Exec executes a newImageCommand.

View File

@ -37,6 +37,9 @@ type Image struct {
internalHeight int internalHeight int
screen bool screen bool
// attribute is used only for logs.
attribute string
// id is an identifier for the image. This is used only when dumping the information. // id is an identifier for the image. This is used only when dumping the information.
// //
// This is duplicated with graphicsdriver.Image's ID, but this id is still necessary because this image might not // This is duplicated with graphicsdriver.Image's ID, but this id is still necessary because this image might not
@ -57,18 +60,20 @@ func genNextImageID() int {
// NewImage returns a new image. // NewImage returns a new image.
// //
// Note that the image is not initialized yet. // Note that the image is not initialized yet.
func NewImage(width, height int, screenFramebuffer bool) *Image { func NewImage(width, height int, screenFramebuffer bool, attribute string) *Image {
i := &Image{ i := &Image{
width: width, width: width,
height: height, height: height,
screen: screenFramebuffer, screen: screenFramebuffer,
id: genNextImageID(), id: genNextImageID(),
attribute: attribute,
} }
c := &newImageCommand{ c := &newImageCommand{
result: i, result: i,
width: width, width: width,
height: height, height: height,
screen: screenFramebuffer, screen: screenFramebuffer,
attribute: attribute,
} }
theCommandQueueManager.enqueueCommand(c) theCommandQueueManager.enqueueCommand(c)
return i return i

View File

@ -50,8 +50,8 @@ func quadVertices(w, h float32) []float32 {
func TestClear(t *testing.T) { func TestClear(t *testing.T) {
const w, h = 1024, 1024 const w, h = 1024, 1024
src := graphicscommand.NewImage(w/2, h/2, false) src := graphicscommand.NewImage(w/2, h/2, false, "")
dst := graphicscommand.NewImage(w, h, false) dst := graphicscommand.NewImage(w, h, false, "")
vs := quadVertices(w/2, h/2) vs := quadVertices(w/2, h/2)
is := graphics.QuadIndices() is := graphics.QuadIndices()
@ -81,9 +81,9 @@ func TestClear(t *testing.T) {
func TestWritePixelsPartAfterDrawTriangles(t *testing.T) { func TestWritePixelsPartAfterDrawTriangles(t *testing.T) {
const w, h = 32, 32 const w, h = 32, 32
clr := graphicscommand.NewImage(w, h, false) clr := graphicscommand.NewImage(w, h, false, "")
src := graphicscommand.NewImage(w/2, h/2, false) src := graphicscommand.NewImage(w/2, h/2, false, "")
dst := graphicscommand.NewImage(w, h, false) dst := graphicscommand.NewImage(w, h, false, "")
vs := quadVertices(w/2, h/2) vs := quadVertices(w/2, h/2)
is := graphics.QuadIndices() is := graphics.QuadIndices()
dr := image.Rect(0, 0, w, h) dr := image.Rect(0, 0, w, h)
@ -101,8 +101,8 @@ func TestWritePixelsPartAfterDrawTriangles(t *testing.T) {
func TestShader(t *testing.T) { func TestShader(t *testing.T) {
const w, h = 16, 16 const w, h = 16, 16
clr := graphicscommand.NewImage(w, h, false) clr := graphicscommand.NewImage(w, h, false, "")
dst := graphicscommand.NewImage(w, h, false) dst := graphicscommand.NewImage(w, h, false, "")
vs := quadVertices(w, h) vs := quadVertices(w, h)
is := graphics.QuadIndices() is := graphics.QuadIndices()
dr := image.Rect(0, 0, w, h) dr := image.Rect(0, 0, w, h)
@ -136,7 +136,7 @@ func TestShader(t *testing.T) {
// Issue #3036 // Issue #3036
func TestSuccessiveWritePixels(t *testing.T) { func TestSuccessiveWritePixels(t *testing.T) {
const w, h = 32, 32 const w, h = 32, 32
dst := graphicscommand.NewImage(w, h, false) dst := graphicscommand.NewImage(w, h, false, "")
dst.WritePixels(graphics.NewManagedBytes(4, func(bs []byte) { dst.WritePixels(graphics.NewManagedBytes(4, func(bs []byte) {
for i := range bs { for i := range bs {

View File

@ -156,8 +156,13 @@ func NewImage(width, height int, imageType ImageType) *Image {
panic("restorable: graphics driver must be ready at NewImage but not") panic("restorable: graphics driver must be ready at NewImage but not")
} }
var attribute string
switch imageType {
case ImageTypeVolatile:
attribute = "volatile"
}
i := &Image{ i := &Image{
image: graphicscommand.NewImage(width, height, imageType == ImageTypeScreen), image: graphicscommand.NewImage(width, height, imageType == ImageTypeScreen, attribute),
width: width, width: width,
height: height, height: height,
imageType: imageType, imageType: imageType,
@ -566,7 +571,7 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
case ImageTypeScreen: case ImageTypeScreen:
// The screen image should also be recreated because framebuffer might // The screen image should also be recreated because framebuffer might
// be changed. // be changed.
i.image = graphicscommand.NewImage(w, h, true) i.image = graphicscommand.NewImage(w, h, true, "")
i.basePixels.Dispose() i.basePixels.Dispose()
i.basePixels = Pixels{} i.basePixels = Pixels{}
i.clearDrawTrianglesHistory() i.clearDrawTrianglesHistory()
@ -574,7 +579,7 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
i.staleRegions = i.staleRegions[:0] i.staleRegions = i.staleRegions[:0]
return nil return nil
case ImageTypeVolatile: case ImageTypeVolatile:
i.image = graphicscommand.NewImage(w, h, false) i.image = graphicscommand.NewImage(w, h, false, "volatile")
iw, ih := i.image.InternalSize() iw, ih := i.image.InternalSize()
clearImage(i.image, image.Rect(0, 0, iw, ih)) clearImage(i.image, image.Rect(0, 0, iw, ih))
return nil return nil
@ -584,7 +589,7 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
panic("restorable: pixels must not be stale when restoring") panic("restorable: pixels must not be stale when restoring")
} }
gimg := graphicscommand.NewImage(w, h, false) gimg := graphicscommand.NewImage(w, h, false, "")
// Clear the image explicitly. // Clear the image explicitly.
iw, ih := gimg.InternalSize() iw, ih := gimg.InternalSize()
clearImage(gimg, image.Rect(0, 0, iw, ih)) clearImage(gimg, image.Rect(0, 0, iw, ih))