Fix uploading volume texture data to GPU in D3D12

This commit is contained in:
Wojciech Figat
2022-03-18 17:55:35 +01:00
parent 8cca7f884b
commit 8d0a779ff4
2 changed files with 10 additions and 10 deletions

View File

@@ -90,29 +90,30 @@ bool UploadBufferDX12::UploadTexture(GPUContextDX12* context, ID3D12Resource* te
_device->GetDevice()->GetCopyableFootprints(&resourceDesc, subresourceIndex, 1, 0, &footprint, &numRows, &rowPitchAligned, &mipSizeAligned); _device->GetDevice()->GetCopyableFootprints(&resourceDesc, subresourceIndex, 1, 0, &footprint, &numRows, &rowPitchAligned, &mipSizeAligned);
rowPitchAligned = footprint.Footprint.RowPitch; rowPitchAligned = footprint.Footprint.RowPitch;
mipSizeAligned = rowPitchAligned * footprint.Footprint.Height; 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 // Allocate data
const DynamicAllocation allocation = Allocate(mipSizeAligned, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT); const DynamicAllocation allocation = Allocate(sliceSizeAligned, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
if (allocation.Size != mipSizeAligned) if (allocation.Size != sliceSizeAligned)
return true; return true;
// Check if can copy rows at once
byte* ptr = (byte*)srcData; byte* ptr = (byte*)srcData;
ASSERT(srcSlicePitch <= mipSizeAligned); ASSERT(srcSlicePitch <= sliceSizeAligned);
if (srcRowPitch == rowPitchAligned) if (srcSlicePitch == sliceSizeAligned)
{ {
// Copy data at once // Copy data at once
Platform::MemoryCopy(allocation.CPUAddress, ptr, srcSlicePitch); Platform::MemoryCopy(allocation.CPUAddress, ptr, srcSlicePitch);
} }
else else
{ {
// Use per row copy // Copy data per-row
byte* dst = static_cast<byte*>(allocation.CPUAddress); byte* dst = static_cast<byte*>(allocation.CPUAddress);
ASSERT(srcRowPitch <= rowPitchAligned); 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); Platform::MemoryCopy(dst, ptr, srcRowPitch);
dst += rowPitchAligned; dst += rowPitchAligned;
ptr += srcRowPitch; ptr += srcRowPitch;
} }

View File

@@ -141,9 +141,8 @@ struct DynamicAllocation
} }
/// <summary> /// <summary>
/// Returns true if allocation is invalid /// Returns true if allocation is invalid.
/// </summary> /// </summary>
/// <returns>True if allocation in invalid</returns>
bool IsInvalid() const bool IsInvalid() const
{ {
return CPUAddress == nullptr || Size == 0 || Page == nullptr; return CPUAddress == nullptr || Size == 0 || Page == nullptr;