From ccddd4ee204811f19b4afef4cff17a881ad35b76 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 19 Sep 2022 00:16:53 +0900 Subject: [PATCH] internal/graphicsdriver/directx: refactoring: make the lifetime of uploadingStagingBuffer more explicit --- .../directx/graphics_windows.go | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/internal/graphicsdriver/directx/graphics_windows.go b/internal/graphicsdriver/directx/graphics_windows.go index 7cab25e42..da78081e7 100644 --- a/internal/graphicsdriver/directx/graphics_windows.go +++ b/internal/graphicsdriver/directx/graphics_windows.go @@ -1499,17 +1499,19 @@ func (*Image) IsInvalidated() bool { return false } -func (i *Image) ensureUploadingStagingBuffer(size uint64) (*_ID3D12Resource, error) { - if i.uploadingStagingBuffer != nil && i.uploadingStagingBuffer.GetDesc().Width < size { - i.uploadingStagingBuffer.Release() - i.uploadingStagingBuffer = nil - } +func (i *Image) ensureUploadingStagingBuffer() (*_ID3D12Resource, error) { + // Unlike readingStagingBuffer, uploadingStagingBuffer's lifetime is not determined and cannot recreate easily. + // Then, keep using the same uploadingStagingBuffer for one image. if i.uploadingStagingBuffer != nil { return i.uploadingStagingBuffer, nil } - usb, err := createBuffer(i.graphics.device, size, _D3D12_HEAP_TYPE_UPLOAD) + iw, ih := graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height) + alignedWidth := align(4 * iw) + size := alignedWidth * ih + + usb, err := createBuffer(i.graphics.device, uint64(size), _D3D12_HEAP_TYPE_UPLOAD) if err != nil { return nil, err } @@ -1617,9 +1619,7 @@ func (i *Image) WritePixels(args []*graphicsdriver.WritePixelsArgs) error { return err } - iw, ih := graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height) - alignedWidth := align(4 * iw) - usb, err := i.ensureUploadingStagingBuffer(uint64(alignedWidth * ih)) + usb, err := i.ensureUploadingStagingBuffer() if err != nil { return err } @@ -1635,6 +1635,9 @@ func (i *Image) WritePixels(args []*graphicsdriver.WritePixelsArgs) error { i.graphics.needFlushCopyCommandList = true + iw, ih := graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height) + alignedWidth := align(4 * iw) + var srcBytes []byte h := (*reflect.SliceHeader)(unsafe.Pointer(&srcBytes)) h.Data = uintptr(m)