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)
if c.dst.screen {
dst += " (screen)"
} else if c.dst.attribute != "" {
dst += " (" + c.dst.attribute + ")"
}
var srcstrs [graphics.ShaderSrcImageCount]string
@ -104,6 +106,8 @@ func (c *drawTrianglesCommand) String() string {
srcstrs[i] = fmt.Sprintf("%d", src.id)
if src.screen {
srcstrs[i] += " (screen)"
} else if src.attribute != "" {
srcstrs[i] += " (" + src.attribute + ")"
}
}
@ -335,14 +339,19 @@ func (c *disposeShaderCommand) NeedsSync() bool {
// newImageCommand represents a command to create an empty image with given width and height.
type newImageCommand struct {
result *Image
width int
height int
screen bool
result *Image
width int
height int
screen bool
attribute 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.

View File

@ -37,6 +37,9 @@ type Image struct {
internalHeight int
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.
//
// 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.
//
// 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{
width: width,
height: height,
screen: screenFramebuffer,
id: genNextImageID(),
width: width,
height: height,
screen: screenFramebuffer,
id: genNextImageID(),
attribute: attribute,
}
c := &newImageCommand{
result: i,
width: width,
height: height,
screen: screenFramebuffer,
result: i,
width: width,
height: height,
screen: screenFramebuffer,
attribute: attribute,
}
theCommandQueueManager.enqueueCommand(c)
return i

View File

@ -50,8 +50,8 @@ func quadVertices(w, h float32) []float32 {
func TestClear(t *testing.T) {
const w, h = 1024, 1024
src := graphicscommand.NewImage(w/2, h/2, false)
dst := graphicscommand.NewImage(w, h, false)
src := graphicscommand.NewImage(w/2, h/2, false, "")
dst := graphicscommand.NewImage(w, h, false, "")
vs := quadVertices(w/2, h/2)
is := graphics.QuadIndices()
@ -81,9 +81,9 @@ func TestClear(t *testing.T) {
func TestWritePixelsPartAfterDrawTriangles(t *testing.T) {
const w, h = 32, 32
clr := graphicscommand.NewImage(w, h, false)
src := graphicscommand.NewImage(w/2, h/2, false)
dst := graphicscommand.NewImage(w, h, false)
clr := graphicscommand.NewImage(w, h, false, "")
src := graphicscommand.NewImage(w/2, h/2, false, "")
dst := graphicscommand.NewImage(w, h, false, "")
vs := quadVertices(w/2, h/2)
is := graphics.QuadIndices()
dr := image.Rect(0, 0, w, h)
@ -101,8 +101,8 @@ func TestWritePixelsPartAfterDrawTriangles(t *testing.T) {
func TestShader(t *testing.T) {
const w, h = 16, 16
clr := graphicscommand.NewImage(w, h, false)
dst := graphicscommand.NewImage(w, h, false)
clr := graphicscommand.NewImage(w, h, false, "")
dst := graphicscommand.NewImage(w, h, false, "")
vs := quadVertices(w, h)
is := graphics.QuadIndices()
dr := image.Rect(0, 0, w, h)
@ -136,7 +136,7 @@ func TestShader(t *testing.T) {
// Issue #3036
func TestSuccessiveWritePixels(t *testing.T) {
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) {
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")
}
var attribute string
switch imageType {
case ImageTypeVolatile:
attribute = "volatile"
}
i := &Image{
image: graphicscommand.NewImage(width, height, imageType == ImageTypeScreen),
image: graphicscommand.NewImage(width, height, imageType == ImageTypeScreen, attribute),
width: width,
height: height,
imageType: imageType,
@ -566,7 +571,7 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
case ImageTypeScreen:
// The screen image should also be recreated because framebuffer might
// be changed.
i.image = graphicscommand.NewImage(w, h, true)
i.image = graphicscommand.NewImage(w, h, true, "")
i.basePixels.Dispose()
i.basePixels = Pixels{}
i.clearDrawTrianglesHistory()
@ -574,7 +579,7 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
i.staleRegions = i.staleRegions[:0]
return nil
case ImageTypeVolatile:
i.image = graphicscommand.NewImage(w, h, false)
i.image = graphicscommand.NewImage(w, h, false, "volatile")
iw, ih := i.image.InternalSize()
clearImage(i.image, image.Rect(0, 0, iw, ih))
return nil
@ -584,7 +589,7 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
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.
iw, ih := gimg.InternalSize()
clearImage(gimg, image.Rect(0, 0, iw, ih))