ID3D12GraphicsCommandList::CopyBufferRegion 方法 (d3d12.h)

将缓冲区的区域从一个资源复制到另一个资源。

语法

void CopyBufferRegion(
  [in] ID3D12Resource *pDstBuffer,
       UINT64         DstOffset,
  [in] ID3D12Resource *pSrcBuffer,
       UINT64         SrcOffset,
       UINT64         NumBytes
);

参数

[in] pDstBuffer

类型: ID3D12Resource*

指定目标 ID3D12Resource

DstOffset

类型: UINT64

指定 UINT64 偏移量 () 目标资源的字节数。

[in] pSrcBuffer

类型: ID3D12Resource*

指定源 ID3D12Resource

SrcOffset

类型: UINT64

指定 UINT64 偏移量 () 源资源中的字节数,以从中开始复制。

NumBytes

类型: UINT64

指定要复制的字节数。

返回值

备注

复制整个资源时,请考虑使用 CopyResource 方法,并使用此方法复制资源的区域。

CopyBufferRegion 可用于初始化为同一堆内存别名的资源。 有关更多详细信息,请参阅 CreatePlacedResource

示例

D3D12HelloTriangle 示例使用 ID3D12GraphicsCommandList::CopyBufferRegion,如下所示:

inline UINT64 UpdateSubresources(
    _In_ ID3D12GraphicsCommandList* pCmdList,
    _In_ ID3D12Resource* pDestinationResource,
    _In_ ID3D12Resource* pIntermediate,
    _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource,
    _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources,
    UINT64 RequiredSize,
    _In_reads_(NumSubresources) const D3D12_PLACED_SUBRESOURCE_FOOTPRINT* pLayouts,
    _In_reads_(NumSubresources) const UINT* pNumRows,
    _In_reads_(NumSubresources) const UINT64* pRowSizesInBytes,
    _In_reads_(NumSubresources) const D3D12_SUBRESOURCE_DATA* pSrcData)
{
    // Minor validation
    D3D12_RESOURCE_DESC IntermediateDesc = pIntermediate->GetDesc();
    D3D12_RESOURCE_DESC DestinationDesc = pDestinationResource->GetDesc();
    if (IntermediateDesc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || 
        IntermediateDesc.Width < RequiredSize + pLayouts[0].Offset || 
        RequiredSize > (SIZE_T)-1 || 
        (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER && 
            (FirstSubresource != 0 || NumSubresources != 1)))
    {
        return 0;
    }
    
    BYTE* pData;
    HRESULT hr = pIntermediate->Map(0, NULL, reinterpret_cast<void**>(&pData));
    if (FAILED(hr))
    {
        return 0;
    }
    
    for (UINT i = 0; i < NumSubresources; ++i)
    {
        if (pRowSizesInBytes[i] > (SIZE_T)-1) return 0;
        D3D12_MEMCPY_DEST DestData = { pData + pLayouts[i].Offset, pLayouts[i].Footprint.RowPitch, pLayouts[i].Footprint.RowPitch * pNumRows[i] };
        MemcpySubresource(&DestData, &pSrcData[i], (SIZE_T)pRowSizesInBytes[i], pNumRows[i], pLayouts[i].Footprint.Depth);
    }
    pIntermediate->Unmap(0, NULL);
    
    if (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)
    {
        CD3DX12_BOX SrcBox( UINT( pLayouts[0].Offset ), UINT( pLayouts[0].Offset + pLayouts[0].Footprint.Width ) );
        pCmdList->CopyBufferRegion(
            pDestinationResource, 0, pIntermediate, pLayouts[0].Offset, pLayouts[0].Footprint.Width);
    }
    else
    {
        for (UINT i = 0; i < NumSubresources; ++i)
        {
            CD3DX12_TEXTURE_COPY_LOCATION Dst(pDestinationResource, i + FirstSubresource);
            CD3DX12_TEXTURE_COPY_LOCATION Src(pIntermediate, pLayouts[i]);
            pCmdList->CopyTextureRegion(&Dst, 0, 0, 0, &Src, nullptr);
        }
    }
    return RequiredSize;
}

请参阅 D3D12 参考中的示例代码

要求

   
目标平台 Windows
标头 d3d12.h
Library D3d12.lib
DLL D3d12.dll

请参阅

CopyTextureRegion

ID3D12GraphicsCommandList