From c11134ad6e8033a19606eab40fdc2b58886b93a7 Mon Sep 17 00:00:00 2001 From: Alexey Panteleev Date: Mon, 9 Sep 2024 12:56:41 -0700 Subject: [PATCH] Fixed some numeric overflow bugs when working with very large textures. --- src/d3d12/d3d12-texture.cpp | 2 +- src/vulkan/vulkan-staging-texture.cpp | 8 ++++---- src/vulkan/vulkan-texture.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/d3d12/d3d12-texture.cpp b/src/d3d12/d3d12-texture.cpp index 85a4e1f..2445202 100644 --- a/src/d3d12/d3d12-texture.cpp +++ b/src/d3d12/d3d12-texture.cpp @@ -1178,7 +1178,7 @@ namespace nvrhi::d3d12 { for (uint32_t row = 0; row < numRows; row++) { - void* destAddress = (char*)cpuVA + footprint.Footprint.RowPitch * (row + depthSlice * numRows); + void* destAddress = (char*)cpuVA + uint64_t(footprint.Footprint.RowPitch) * uint64_t(row + depthSlice * numRows); const void* srcAddress = (const char*)data + rowPitch * row + depthPitch * depthSlice; memcpy(destAddress, srcAddress, std::min(rowPitch, rowSizeInBytes)); } diff --git a/src/vulkan/vulkan-staging-texture.cpp b/src/vulkan/vulkan-staging-texture.cpp index c49ebde..cac8205 100644 --- a/src/vulkan/vulkan-staging-texture.cpp +++ b/src/vulkan/vulkan-staging-texture.cpp @@ -39,10 +39,10 @@ namespace nvrhi::vulkan { const FormatInfo& formatInfo = getFormatInfo(desc.format); - auto wInBlocks = std::max(((desc.width >> mipLevel) + formatInfo.blockSize - 1) / formatInfo.blockSize, 1u); - auto hInBlocks = std::max(((desc.height >> mipLevel) + formatInfo.blockSize - 1) / formatInfo.blockSize, 1u); + uint32_t wInBlocks = std::max(((desc.width >> mipLevel) + formatInfo.blockSize - 1) / formatInfo.blockSize, 1u); + uint32_t hInBlocks = std::max(((desc.height >> mipLevel) + formatInfo.blockSize - 1) / formatInfo.blockSize, 1u); - auto blockPitchBytes = wInBlocks * formatInfo.bytesPerBlock; + size_t blockPitchBytes = wInBlocks * formatInfo.bytesPerBlock; return blockPitchBytes * hInBlocks; } @@ -118,7 +118,7 @@ namespace nvrhi::vulkan tex->populateSliceRegions(); BufferDesc bufDesc; - bufDesc.byteSize = uint32_t(tex->getBufferSize()); + bufDesc.byteSize = tex->getBufferSize(); assert(bufDesc.byteSize > 0); bufDesc.debugName = desc.debugName; bufDesc.cpuAccess = cpuAccess; diff --git a/src/vulkan/vulkan-texture.cpp b/src/vulkan/vulkan-texture.cpp index ce0b2a5..bd52773 100644 --- a/src/vulkan/vulkan-texture.cpp +++ b/src/vulkan/vulkan-texture.cpp @@ -482,7 +482,7 @@ namespace nvrhi::vulkan uint32_t deviceNumCols = (mipWidth + formatInfo.blockSize - 1) / formatInfo.blockSize; uint32_t deviceNumRows = (mipHeight + formatInfo.blockSize - 1) / formatInfo.blockSize; uint32_t deviceRowPitch = deviceNumCols * formatInfo.bytesPerBlock; - uint32_t deviceMemSize = deviceRowPitch * deviceNumRows * mipDepth; + uint64_t deviceMemSize = uint64_t(deviceRowPitch) * uint64_t(deviceNumRows) * mipDepth; Buffer* uploadBuffer; uint64_t uploadOffset;