From 8d0a779ff4ecca0f38118e1e780726fa4221020f Mon Sep 17 00:00:00 2001 From: Wojciech Figat Date: Fri, 18 Mar 2022 17:55:35 +0100 Subject: [PATCH] Fix uploading volume texture data to GPU in D3D12 --- .../DirectX/DX12/UploadBufferDX12.cpp | 17 +++++++++-------- .../DirectX/DX12/UploadBufferDX12.h | 3 +-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Source/Engine/GraphicsDevice/DirectX/DX12/UploadBufferDX12.cpp b/Source/Engine/GraphicsDevice/DirectX/DX12/UploadBufferDX12.cpp index eaf4a4afb..7bba6a3ba 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX12/UploadBufferDX12.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX12/UploadBufferDX12.cpp @@ -90,29 +90,30 @@ bool UploadBufferDX12::UploadTexture(GPUContextDX12* context, ID3D12Resource* te _device->GetDevice()->GetCopyableFootprints(&resourceDesc, subresourceIndex, 1, 0, &footprint, &numRows, &rowPitchAligned, &mipSizeAligned); rowPitchAligned = footprint.Footprint.RowPitch; mipSizeAligned = rowPitchAligned * footprint.Footprint.Height; + const uint32 numSlices = resourceDesc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? Math::Max(1, resourceDesc.DepthOrArraySize >> mipIndex) : 1; + const uint64 sliceSizeAligned = numSlices * mipSizeAligned; // Allocate data - const DynamicAllocation allocation = Allocate(mipSizeAligned, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT); - if (allocation.Size != mipSizeAligned) + const DynamicAllocation allocation = Allocate(sliceSizeAligned, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT); + if (allocation.Size != sliceSizeAligned) return true; - // Check if can copy rows at once byte* ptr = (byte*)srcData; - ASSERT(srcSlicePitch <= mipSizeAligned); - if (srcRowPitch == rowPitchAligned) + ASSERT(srcSlicePitch <= sliceSizeAligned); + if (srcSlicePitch == sliceSizeAligned) { // Copy data at once Platform::MemoryCopy(allocation.CPUAddress, ptr, srcSlicePitch); } else { - // Use per row copy + // Copy data per-row byte* dst = static_cast(allocation.CPUAddress); ASSERT(srcRowPitch <= rowPitchAligned); - for (uint32 i = 0; i < numRows; i++) + const uint32 numCopies = numSlices * numRows; + for (uint32 i = 0; i < numCopies; i++) { Platform::MemoryCopy(dst, ptr, srcRowPitch); - dst += rowPitchAligned; ptr += srcRowPitch; } diff --git a/Source/Engine/GraphicsDevice/DirectX/DX12/UploadBufferDX12.h b/Source/Engine/GraphicsDevice/DirectX/DX12/UploadBufferDX12.h index 0e031d402..dfff8b203 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX12/UploadBufferDX12.h +++ b/Source/Engine/GraphicsDevice/DirectX/DX12/UploadBufferDX12.h @@ -141,9 +141,8 @@ struct DynamicAllocation } /// - /// Returns true if allocation is invalid + /// Returns true if allocation is invalid. /// - /// True if allocation in invalid bool IsInvalid() const { return CPUAddress == nullptr || Size == 0 || Page == nullptr;