From 64e32d4ee197a89aa1611c5876fe1f04c7ca9a48 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Tue, 29 Oct 2024 22:33:43 +0100 Subject: [PATCH] [d3d11] Clean up InitHostVisibleTexture Also fixes a potential crash if pSysMem of any initial data structs is null. --- src/d3d11/d3d11_initializer.cpp | 47 ++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/d3d11/d3d11_initializer.cpp b/src/d3d11/d3d11_initializer.cpp index 7cff6ecb..0f046d8c 100644 --- a/src/d3d11/d3d11_initializer.cpp +++ b/src/d3d11/d3d11_initializer.cpp @@ -229,38 +229,47 @@ namespace dxvk { D3D11CommonTexture* pTexture, const D3D11_SUBRESOURCE_DATA* pInitialData) { Rc image = pTexture->GetImage(); + auto formatInfo = image->formatInfo(); + + for (uint32_t layer = 0; layer < pTexture->Desc()->ArraySize; layer++) { + for (uint32_t level = 0; level < pTexture->Desc()->MipLevels; level++) { + uint32_t subresourceIndex = D3D11CalcSubresource(level, layer, pTexture->Desc()->MipLevels); - for (uint32_t layer = 0; layer < image->info().numLayers; layer++) { - for (uint32_t level = 0; level < image->info().mipLevels; level++) { VkImageSubresource subresource; - subresource.aspectMask = image->formatInfo()->aspectMask; + subresource.aspectMask = formatInfo->aspectMask; subresource.mipLevel = level; subresource.arrayLayer = layer; VkExtent3D blockCount = util::computeBlockCount( - image->mipLevelExtent(level), - image->formatInfo()->blockSize); + image->mipLevelExtent(level), formatInfo->blockSize); - VkSubresourceLayout layout = image->querySubresourceLayout(subresource); + auto layout = pTexture->GetSubresourceLayout( + subresource.aspectMask, subresourceIndex); - auto initialData = pInitialData - ? &pInitialData[D3D11CalcSubresource(level, layer, image->info().mipLevels)] - : nullptr; + if (pInitialData && pInitialData[subresourceIndex].pSysMem) { + const auto& initialData = pInitialData[subresourceIndex]; - for (uint32_t z = 0; z < blockCount.depth; z++) { - for (uint32_t y = 0; y < blockCount.height; y++) { - auto size = blockCount.width * image->formatInfo()->elementSize; - auto dst = image->mapPtr(layout.offset + y * layout.rowPitch + z * layout.depthPitch); + for (uint32_t z = 0; z < blockCount.depth; z++) { + for (uint32_t y = 0; y < blockCount.height; y++) { + auto size = blockCount.width * formatInfo->elementSize; + + auto dst = pTexture->GetMapPtr(subresourceIndex, layout.Offset + + y * layout.RowPitch + + z * layout.DepthPitch); + + auto src = reinterpret_cast(initialData.pSysMem) + + y * initialData.SysMemPitch + + z * initialData.SysMemSlicePitch; - if (initialData) { - auto src = reinterpret_cast(initialData->pSysMem) - + y * initialData->SysMemPitch - + z * initialData->SysMemSlicePitch; std::memcpy(dst, src, size); - } else { - std::memset(dst, 0, size); + + if (size < layout.RowPitch) + std::memset(reinterpret_cast(dst) + size, 0, layout.RowPitch - size); } } + } else { + void* dst = pTexture->GetMapPtr(subresourceIndex, layout.Offset); + std::memset(dst, 0, layout.Size); } } }