internal/graphicsdriver/directx: refactoring: make the lifetime of uploadingStagingBuffer more explicit

This commit is contained in:
Hajime Hoshi 2022-09-19 00:16:53 +09:00
parent 8e40c2ce7e
commit ccddd4ee20

View File

@ -1499,17 +1499,19 @@ func (*Image) IsInvalidated() bool {
return false return false
} }
func (i *Image) ensureUploadingStagingBuffer(size uint64) (*_ID3D12Resource, error) { func (i *Image) ensureUploadingStagingBuffer() (*_ID3D12Resource, error) {
if i.uploadingStagingBuffer != nil && i.uploadingStagingBuffer.GetDesc().Width < size { // Unlike readingStagingBuffer, uploadingStagingBuffer's lifetime is not determined and cannot recreate easily.
i.uploadingStagingBuffer.Release() // Then, keep using the same uploadingStagingBuffer for one image.
i.uploadingStagingBuffer = nil
}
if i.uploadingStagingBuffer != nil { if i.uploadingStagingBuffer != nil {
return 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 { if err != nil {
return nil, err return nil, err
} }
@ -1617,9 +1619,7 @@ func (i *Image) WritePixels(args []*graphicsdriver.WritePixelsArgs) error {
return err return err
} }
iw, ih := graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height) usb, err := i.ensureUploadingStagingBuffer()
alignedWidth := align(4 * iw)
usb, err := i.ensureUploadingStagingBuffer(uint64(alignedWidth * ih))
if err != nil { if err != nil {
return err return err
} }
@ -1635,6 +1635,9 @@ func (i *Image) WritePixels(args []*graphicsdriver.WritePixelsArgs) error {
i.graphics.needFlushCopyCommandList = true i.graphics.needFlushCopyCommandList = true
iw, ih := graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height)
alignedWidth := align(4 * iw)
var srcBytes []byte var srcBytes []byte
h := (*reflect.SliceHeader)(unsafe.Pointer(&srcBytes)) h := (*reflect.SliceHeader)(unsafe.Pointer(&srcBytes))
h.Data = uintptr(m) h.Data = uintptr(m)