Skip to content

Commit

Permalink
graphicsdriver/metal: Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Jun 26, 2020
1 parent 2bc1475 commit 00e8b70
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions internal/graphicsdriver/metal/graphics.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,15 +868,31 @@ func (i *Image) Pixels() ([]byte, error) {
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) {
g := i.graphics
if g.drawCalled {
g.flush(true, false)
g.drawCalled = false

// When mtl.TextureUsageRenderTarget is specified, synchronizing the texture memory before replacing
// a region seems necessary (#1213).
i.syncTexture()
if i.needsToSyncBeforeReplaceRegion(args) {
i.syncTexture()
}
}

g.t.Call(func() error {
Expand Down

0 comments on commit 00e8b70

Please sign in to comment.