Function updateTextureSubresources

Synopsis

#include <Source/Falcor/Core/API/CopyContext.h>

void updateTextureSubresources(const Texture *pTexture, uint32_t firstSubresource, uint32_t subresourceCount, const void *pData, const uint3 &offset=uint3(0), const uint3 &size=uint3(-1))

Description

No description yet.

Source

Lines 74-143 in Source/Falcor/Core/API/D3D12/D3D12CopyContext.cpp. Line 151 in Source/Falcor/Core/API/CopyContext.h.

void CopyContext::updateTextureSubresources(const Texture* pTexture, uint32_t firstSubresource, uint32_t subresourceCount, const void* pData, const uint3& offset, const uint3& size)
{
    bool copyRegion = (offset != uint3(0)) || (size != uint3(-1));
    assert(subresourceCount == 1 || (copyRegion == false));
    mCommandsPending = true;
    uint32_t arraySize = (pTexture->getType() == Texture::Type::TextureCube) ? pTexture->getArraySize() * 6 : pTexture->getArraySize();
    assert(firstSubresource + subresourceCount <= arraySize * pTexture->getMipCount());
    // Get the footprint
    D3D12_RESOURCE_DESC texDesc = pTexture->getApiHandle()->GetDesc();
    std::vector<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> footprint(subresourceCount);
    std::vector<uint32_t> rowCount(subresourceCount);
    std::vector<uint64_t> rowSize(subresourceCount);
    uint64_t bufferSize;
    if (copyRegion)
    {
        footprint[0].Offset = 0;
        footprint[0].Footprint.Format = getDxgiFormat(pTexture->getFormat());
        uint32_t mipLevel = pTexture->getSubresourceMipLevel(firstSubresource);
        footprint[0].Footprint.Width = (size.x == -1) ? pTexture->getWidth(mipLevel) - offset.x : size.x;
        footprint[0].Footprint.Height = (size.y == -1) ? pTexture->getHeight(mipLevel) - offset.y : size.y;
        footprint[0].Footprint.Depth = (size.z == -1) ? pTexture->getDepth(mipLevel) - offset.z : size.z;
        footprint[0].Footprint.RowPitch = align_to(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT, footprint[0].Footprint.Width * getFormatBytesPerBlock(pTexture->getFormat()));
        rowCount[0] = footprint[0].Footprint.Height;
        rowSize[0] = footprint[0].Footprint.RowPitch;
        bufferSize = rowSize[0] * rowCount[0] * footprint[0].Footprint.Depth;
    }
    else
    {
        ID3D12Device* pDevice = gpDevice->getApiHandle();
        pDevice->GetCopyableFootprints(&texDesc, firstSubresource, subresourceCount, 0, footprint.data(), rowCount.data(), rowSize.data(), &bufferSize);
    }
    // Allocate a buffer on the upload heap
    Buffer::SharedPtr pBuffer = Buffer::create(bufferSize, Buffer::BindFlags::None, Buffer::CpuAccess::Write, nullptr);
    // Map the buffer
    uint8_t* pDst = (uint8_t*)pBuffer->map(Buffer::MapType::WriteDiscard);
    ID3D12ResourcePtr pResource = pBuffer->getApiHandle();
    // Get the offset from the beginning of the resource
    uint64_t vaOffset = pBuffer->getGpuAddressOffset();
    resourceBarrier(pTexture, Resource::State::CopyDest);
    const uint8_t* pSrc = (uint8_t*)pData;
    for (uint32_t s = 0; s < subresourceCount; s++)
    {
        uint32_t physicalWidth = footprint[s].Footprint.Width / getFormatWidthCompressionRatio(pTexture->getFormat());
        uint32_t physicalHeight = footprint[s].Footprint.Height / getFormatHeightCompressionRatio(pTexture->getFormat());
        D3D12_SUBRESOURCE_DATA src;
        src.pData = pSrc;
        src.RowPitch = physicalWidth * getFormatBytesPerBlock(pTexture->getFormat());
        src.SlicePitch = src.RowPitch * physicalHeight;
        copySubresourceData(src, footprint[s], pDst, rowSize[s], rowCount[s]);
        pSrc = (uint8_t*)pSrc + footprint[s].Footprint.Depth * src.SlicePitch;
        // Dispatch a command
        footprint[s].Offset += vaOffset;
        uint32_t subresource = s + firstSubresource;
        D3D12_TEXTURE_COPY_LOCATION dstLoc = { pTexture->getApiHandle(), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, subresource };
        D3D12_TEXTURE_COPY_LOCATION srcLoc = { pResource, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, footprint[s] };
        mpLowLevelData->getCommandList()->CopyTextureRegion(&dstLoc, offset.x, offset.y, offset.z, &srcLoc, nullptr);
    }
    pBuffer->unmap();
}





Add Discussion as Guest

Log in to DocsForge