2

首先,我对描述符范围的理解是,我可以指定着色器可能使用的多个缓冲区(在我的情况下为常量缓冲区),对吗?如果不是,那么这就是我的误解所在,剩下的问题将毫无意义。

假设我想在我的顶点着色器中传递几个常量缓冲区

// Vertex.hlsl

float value0 : register(b0)
float value1 : register(b1)
...

无论出于何种原因,我想使用描述符范围来指定 b0 和 b1。我填写了一个 D3D12_DESCRIPTOR_RANGE:

D3D12_DESCRIPTOR_RANGE range;
range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
range.NumDescriptors = 2;    
range.BaseShaderRegister = 0;
range.RegisterSpace = 0;
range.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;

然后我继续将其推入根参数

D3D12_ROOT_PARAMETER param;  
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
param.DescriptorTable.NumDescriptorRanges = 1; 
param.DescriptorTable.pDescriptorRanges = ⦥
param.ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;

根参数进入我的签名描述

D3D12_ROOT_SIGNATURE_DESC1 signatureDesc;
signatureDesc.NumParameters = 1;
signatureDesc.pParameters = &param;
signatureDesc.NumStaticSamplers = 0;
signatureDesc.pStaticSamplers = nullptr;
D3D12_ROOT_SIGNATURE_FLAGS = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;

在此之后创建我的根签名等等。我还为 2 个描述符创建了一个堆

D3D12_DESCRIPTOR_HEAP_DESC heapDescCbv;
heapDescCbv.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
heapDescCbv.NumDescriptors = 2;
heapDescCbv.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
heapDescCbv.NodeMask = 0;
ThrowIfFailed(m_device->CreateDescriptorHeap(&heapDescCbv, IID_PPV_ARGS(&m_cbvHeap)));

然后我映射了各自的 ID3D12Resource 以获得两个指针,以便我可以将我的值存储到它们。

void D3D12App::AllocateConstantBuffer(SIZE_T index, size_t dataSize, ID3D12Resource** buffer, void** mappedPtr)
{
    D3D12_HEAP_PROPERTIES heapProp;
    heapProp.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
    heapProp.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
    heapProp.CreationNodeMask = 1;
    heapProp.VisibleNodeMask = 1;
    heapProp.Type = D3D12_HEAP_TYPE_UPLOAD;
    D3D12_RESOURCE_DESC resDesc;
    resDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
    resDesc.Alignment = 0;
    resDesc.Width = (dataSize + 255) & ~255;
    resDesc.Height = 1;
    resDesc.DepthOrArraySize = 1;
    resDesc.MipLevels = 1;
    resDesc.Format = DXGI_FORMAT_UNKNOWN;
    resDesc.SampleDesc.Count = 1;
    resDesc.SampleDesc.Quality = 0;
    resDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
    resDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
    ThrowIfFailed(m_device->CreateCommittedResource(&heapProp, D3D12_HEAP_FLAG_NONE,
        &resDesc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(buffer)));

    D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc;
    cbvDesc.BufferLocation = (*buffer)->GetGPUVirtualAddress();
    cbvDesc.SizeInBytes = (dataSize + 255) & ~255;
    auto cbvHandle = m_cbvHeap->GetCPUDescriptorHandleForHeapStart();
    cbvHandle.ptr += index * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
    m_device->CreateConstantBufferView(&cbvDesc, cbvHandle);

    D3D12_RANGE readRange;
    readRange.Begin = 0;
    readRange.End = 0;
    ThrowIfFailed((*buffer)->Map(0, &readRange, mappedPtr));
}

AllocateConstantBuffer(0, sizeof(m_value0), &m_value0Resource, reinterpret_cast<void**>&m_constPtrvalue0));
AllocateConstantBuffer(1, sizeof(m_value1), &m_value1Resource, reinterpret_cast<void**>&m_constPtrvalue1));

问题是当我想将它们送入管道时。渲染时,我使用了

auto cbvHandle = m_cbvHeap->GetGPUDescriptorHandleForHeapStart();
m_commandList->SetGraphicsRootDescriptorTable(0, cbvHandle);

我得到的结果是只有 register(b0) 得到了正确的值,而 register(b1) 保持未初始化。我做错了什么?

4

1 回答 1

0

好的,我让它工作了。原来我需要稍微改变一下着色器:

cbuffer a : register(b0) { float value0; }
cbuffer b : register(b1) { float value1; };

不过,这给了我另一个问题,根据这个链接: https ://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-constants

缓冲区名称 a 和 b 应该是可选的,但是当我尝试着色器无法编译时。我想这是一个不同的问题。

于 2019-12-21T04:33:31.180 回答