graphicsdriver/metal: Optimization

This commit is contained in:
Hajime Hoshi 2020-06-27 04:08:00 +09:00
parent 2bc1475a15
commit 00e8b701c1

View File

@ -868,16 +868,32 @@ func (i *Image) Pixels() ([]byte, error) {
return b, nil return b, nil
} }
// needsToSyncBeforeReplaceRegion reports whether syncTexture is needed before calling ReplaceRegion.
//
// Before a part region is updated, the texture data needs to be loaded to CPU. Otherwise, the remaining region will
// be uninitialized (#1213).
func (i *Image) needsToSyncBeforeReplaceRegion(args []*driver.ReplacePixelsArgs) bool {
if len(args) == 0 {
return false
}
if len(args) > 1 {
return true
}
if a := args[0]; a.X == 0 && a.Y == 0 && a.Width == i.width && a.Height == i.height {
return false
}
return true
}
func (i *Image) ReplacePixels(args []*driver.ReplacePixelsArgs) { func (i *Image) ReplacePixels(args []*driver.ReplacePixelsArgs) {
g := i.graphics g := i.graphics
if g.drawCalled { if g.drawCalled {
g.flush(true, false) g.flush(true, false)
g.drawCalled = false g.drawCalled = false
if i.needsToSyncBeforeReplaceRegion(args) {
// When mtl.TextureUsageRenderTarget is specified, synchronizing the texture memory before replacing
// a region seems necessary (#1213).
i.syncTexture() i.syncTexture()
} }
}
g.t.Call(func() error { g.t.Call(func() error {
for _, a := range args { for _, a := range args {