MSDN

Microsoft Docs

CreateUnorderedAccessView

Synopsis
Parameters
Return Value
Remarks
Example
See Also

Syntax

void CreateUnorderedAccessView(
    ID3D12Resource* pResource,
    ID3D12Resource* pCounterResource,
    const D3D12_UNORDERED_ACCESS_VIEW_DESC* pDesc,
    D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor);

Parameters

NameDescription
pResourcePointer to the resource that will be accessed as an unordered access view.
pCounterResourceOptional; pointer to a buffer resource that holds a counter for the view. May be nullptr.
pDescPointer to a D3D12_UNORDERED_ACCESS_VIEW_DESC that describes the view. If nullptr, the view is inferred from the resource.
DestDescriptorThe CPU handle that identifies the destination descriptor in a descriptor heap where the view will be stored.

Return Value

This method does not return a value. Errors are reported via the device’s debug layer or ID3D12Device::GetDeviceRemovedReason.

Remarks

  • The resource must have been created with the D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS flag.
  • If a counter resource is supplied, it must be created with the D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS flag and a format of DXGI_FORMAT_R32_UINT.
  • The descriptor heap that receives DestDescriptor must be a D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV heap.
  • When creating a UAV for a typed resource, the format in pDesc must match the resource’s format unless DXGI_FORMAT_UNKNOWN is used.

Example

// Assume device, commandList and descriptorHeap have been created.

ID3D12Resource* texture = nullptr;
D3D12_RESOURCE_DESC texDesc = {};
texDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
texDesc.Width = 1024;
texDesc.Height = 1024;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.MipLevels = 1;
texDesc.SampleDesc.Count = 1;
texDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;

device->CreateCommittedResource(
    &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
    D3D12_HEAP_FLAG_NONE,
    &texDesc,
    D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
    nullptr,
    IID_PPV_ARGS(&texture));

D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
uavDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;

CD3DX12_CPU_DESCRIPTOR_HANDLE uavHandle(descriptorHeap->GetCPUDescriptorHandleForHeapStart(), 0, device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
device->CreateUnorderedAccessView(texture, nullptr, &uavDesc, uavHandle);

// Now the UAV can be bound to the pipeline.
commandList->SetDescriptorHeaps(1, &descriptorHeap);
commandList->SetComputeRootDescriptorTable(0, descriptorHeap->GetGPUDescriptorHandleForHeapStart());