Fix RHI constant binding and add sphere test

This commit is contained in:
2026-03-26 01:23:29 +08:00
parent c5605c2a32
commit 39edb0b497
17 changed files with 959 additions and 35 deletions

View File

@@ -33,6 +33,7 @@ public:
GPUDescriptorHandle GetGPUDescriptorHandle(uint32_t index);
uint32_t GetDescriptorCount() const override;
DescriptorHeapType GetType() const override;
bool IsShaderVisible() const { return m_shaderVisible; }
uint32_t GetDescriptorSize() const { return m_descriptorSize; }
D3D12_CPU_DESCRIPTOR_HANDLE GetCPUDescriptorHandleForHeapStart() const;

View File

@@ -1,6 +1,7 @@
#pragma once
#include <d3d12.h>
#include <memory>
#include <wrl/client.h>
#include <vector>
@@ -13,6 +14,7 @@ namespace XCEngine {
namespace RHI {
class D3D12DescriptorHeap;
class D3D12Buffer;
class D3D12DescriptorSet : public RHIDescriptorSet {
public:
@@ -41,6 +43,10 @@ public:
uint32_t GetOffset() const { return m_offset; }
uint32_t GetCount() const { return m_count; }
D3D12DescriptorHeap* GetHeap() const { return m_heap; }
bool HasBindingType(DescriptorType type) const;
uint32_t GetFirstBindingOfType(DescriptorType type) const;
bool UploadConstantBuffer();
D3D12_GPU_VIRTUAL_ADDRESS GetConstantBufferGPUAddress() const;
private:
D3D12DescriptorHeap* m_heap;
@@ -50,7 +56,9 @@ private:
DescriptorSetLayoutBinding* m_bindings;
std::vector<uint8_t> m_constantBufferData;
bool m_constantBufferDirty = false;
std::unique_ptr<D3D12Buffer> m_constantBuffer;
uint64_t m_constantBufferCapacity = 0;
};
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -29,12 +29,14 @@ public:
uint32_t GetRootParameterIndex(uint32_t shaderRegister) const;
bool HasRootParameter(uint32_t shaderRegister) const;
const RHIPipelineLayoutDesc& GetDesc() const { return m_desc; }
private:
bool InitializeInternal(D3D12Device* device, const RHIPipelineLayoutDesc& desc);
ComPtr<ID3D12RootSignature> m_rootSignature;
D3D12Device* m_device;
RHIPipelineLayoutDesc m_desc = {};
std::unordered_map<uint32_t, uint32_t> m_registerToRootIndex;
std::vector<D3D12_ROOT_PARAMETER> m_rootParameters;
std::vector<D3D12_DESCRIPTOR_RANGE> m_descriptorRanges;

View File

@@ -13,6 +13,20 @@
namespace XCEngine {
namespace RHI {
namespace {
bool HasDescriptorTableBindings(const D3D12DescriptorSet* descriptorSet) {
return descriptorSet != nullptr &&
(descriptorSet->HasBindingType(DescriptorType::SRV) ||
descriptorSet->HasBindingType(DescriptorType::UAV));
}
bool HasSamplerBindings(const D3D12DescriptorSet* descriptorSet) {
return descriptorSet != nullptr && descriptorSet->HasBindingType(DescriptorType::Sampler);
}
} // namespace
D3D12CommandList::D3D12CommandList()
: m_type(CommandQueueType::Direct)
, m_currentTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST)
@@ -164,7 +178,8 @@ void D3D12CommandList::SetGraphicsDescriptorSets(
D3D12DescriptorSet* d3d12Set = static_cast<D3D12DescriptorSet*>(descriptorSets[i]);
D3D12DescriptorHeap* heap = d3d12Set->GetHeap();
if (heap != nullptr) {
if (heap != nullptr && heap->IsShaderVisible() &&
(HasDescriptorTableBindings(d3d12Set) || HasSamplerBindings(d3d12Set))) {
ID3D12DescriptorHeap* nativeHeap = heap->GetDescriptorHeap();
if (nativeHeap != nullptr &&
std::find(descriptorHeaps.begin(), descriptorHeaps.end(), nativeHeap) == descriptorHeaps.end()) {
@@ -183,17 +198,28 @@ void D3D12CommandList::SetGraphicsDescriptorSets(
}
D3D12DescriptorSet* d3d12Set = static_cast<D3D12DescriptorSet*>(descriptorSets[i]);
D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle = d3d12Set->GetGPUHandle();
uint32_t rootIndex = firstSet + i;
if (d3d12Set->GetHeap() != nullptr) {
if (d3d12Set->GetHeap()->GetType() == DescriptorHeapType::CBV_SRV_UAV && d3d12Layout->HasRootParameter(100)) {
rootIndex = d3d12Layout->GetRootParameterIndex(100);
} else if (d3d12Set->GetHeap()->GetType() == DescriptorHeapType::Sampler && d3d12Layout->HasRootParameter(200)) {
rootIndex = d3d12Layout->GetRootParameterIndex(200);
if (d3d12Set->HasBindingType(DescriptorType::CBV)) {
const uint32_t cbvBinding = d3d12Set->GetFirstBindingOfType(DescriptorType::CBV);
if (cbvBinding != UINT32_MAX &&
d3d12Layout->HasRootParameter(cbvBinding) &&
d3d12Set->UploadConstantBuffer()) {
SetGraphicsRootConstantBufferView(
d3d12Layout->GetRootParameterIndex(cbvBinding),
d3d12Set->GetConstantBufferGPUAddress());
}
}
SetGraphicsRootDescriptorTable(rootIndex, gpuHandle);
D3D12DescriptorHeap* heap = d3d12Set->GetHeap();
if (heap == nullptr || !heap->IsShaderVisible()) {
continue;
}
D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle = d3d12Set->GetGPUHandle();
if (heap->GetType() == DescriptorHeapType::CBV_SRV_UAV && HasDescriptorTableBindings(d3d12Set) && d3d12Layout->HasRootParameter(100)) {
SetGraphicsRootDescriptorTable(d3d12Layout->GetRootParameterIndex(100), gpuHandle);
} else if (heap->GetType() == DescriptorHeapType::Sampler && HasSamplerBindings(d3d12Set) && d3d12Layout->HasRootParameter(200)) {
SetGraphicsRootDescriptorTable(d3d12Layout->GetRootParameterIndex(200), gpuHandle);
}
}
}
@@ -219,7 +245,8 @@ void D3D12CommandList::SetComputeDescriptorSets(
D3D12DescriptorSet* d3d12Set = static_cast<D3D12DescriptorSet*>(descriptorSets[i]);
D3D12DescriptorHeap* heap = d3d12Set->GetHeap();
if (heap != nullptr) {
if (heap != nullptr && heap->IsShaderVisible() &&
(HasDescriptorTableBindings(d3d12Set) || HasSamplerBindings(d3d12Set))) {
ID3D12DescriptorHeap* nativeHeap = heap->GetDescriptorHeap();
if (nativeHeap != nullptr &&
std::find(descriptorHeaps.begin(), descriptorHeaps.end(), nativeHeap) == descriptorHeaps.end()) {
@@ -238,17 +265,28 @@ void D3D12CommandList::SetComputeDescriptorSets(
}
D3D12DescriptorSet* d3d12Set = static_cast<D3D12DescriptorSet*>(descriptorSets[i]);
D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle = d3d12Set->GetGPUHandle();
uint32_t rootIndex = firstSet + i;
if (d3d12Set->GetHeap() != nullptr) {
if (d3d12Set->GetHeap()->GetType() == DescriptorHeapType::CBV_SRV_UAV && d3d12Layout->HasRootParameter(100)) {
rootIndex = d3d12Layout->GetRootParameterIndex(100);
} else if (d3d12Set->GetHeap()->GetType() == DescriptorHeapType::Sampler && d3d12Layout->HasRootParameter(200)) {
rootIndex = d3d12Layout->GetRootParameterIndex(200);
if (d3d12Set->HasBindingType(DescriptorType::CBV)) {
const uint32_t cbvBinding = d3d12Set->GetFirstBindingOfType(DescriptorType::CBV);
if (cbvBinding != UINT32_MAX &&
d3d12Layout->HasRootParameter(cbvBinding) &&
d3d12Set->UploadConstantBuffer()) {
m_commandList->SetComputeRootConstantBufferView(
d3d12Layout->GetRootParameterIndex(cbvBinding),
d3d12Set->GetConstantBufferGPUAddress());
}
}
m_commandList->SetComputeRootDescriptorTable(rootIndex, gpuHandle);
D3D12DescriptorHeap* heap = d3d12Set->GetHeap();
if (heap == nullptr || !heap->IsShaderVisible()) {
continue;
}
D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle = d3d12Set->GetGPUHandle();
if (heap->GetType() == DescriptorHeapType::CBV_SRV_UAV && HasDescriptorTableBindings(d3d12Set) && d3d12Layout->HasRootParameter(100)) {
m_commandList->SetComputeRootDescriptorTable(d3d12Layout->GetRootParameterIndex(100), gpuHandle);
} else if (heap->GetType() == DescriptorHeapType::Sampler && HasSamplerBindings(d3d12Set) && d3d12Layout->HasRootParameter(200)) {
m_commandList->SetComputeRootDescriptorTable(d3d12Layout->GetRootParameterIndex(200), gpuHandle);
}
}
}

View File

@@ -1,4 +1,5 @@
#include "XCEngine/RHI/D3D12/D3D12DescriptorSet.h"
#include "XCEngine/RHI/D3D12/D3D12Buffer.h"
#include "XCEngine/RHI/D3D12/D3D12DescriptorHeap.h"
#include "XCEngine/RHI/D3D12/D3D12ResourceView.h"
#include "XCEngine/RHI/D3D12/D3D12Sampler.h"
@@ -6,6 +7,15 @@
namespace XCEngine {
namespace RHI {
namespace {
uint64_t AlignConstantBufferSize(size_t size) {
const uint64_t minSize = size > 0 ? static_cast<uint64_t>(size) : 1ull;
return (minSize + 255ull) & ~255ull;
}
} // namespace
D3D12DescriptorSet::D3D12DescriptorSet()
: m_heap(nullptr)
, m_offset(0)
@@ -39,6 +49,8 @@ void D3D12DescriptorSet::Shutdown() {
m_offset = 0;
m_count = 0;
m_bindingCount = 0;
m_constantBuffer.reset();
m_constantBufferCapacity = 0;
if (m_bindings != nullptr) {
delete[] m_bindings;
m_bindings = nullptr;
@@ -118,6 +130,7 @@ D3D12_GPU_DESCRIPTOR_HANDLE D3D12DescriptorSet::GetGPUHandle(uint32_t index) con
}
void D3D12DescriptorSet::WriteConstant(uint32_t binding, const void* data, size_t size, size_t offset) {
(void)binding;
size_t requiredSize = offset + size;
if (m_constantBufferData.size() < requiredSize) {
m_constantBufferData.resize(requiredSize);
@@ -126,5 +139,58 @@ void D3D12DescriptorSet::WriteConstant(uint32_t binding, const void* data, size_
m_constantBufferDirty = true;
}
bool D3D12DescriptorSet::HasBindingType(DescriptorType type) const {
for (uint32_t i = 0; i < m_bindingCount; ++i) {
if (static_cast<DescriptorType>(m_bindings[i].type) == type) {
return true;
}
}
return false;
}
uint32_t D3D12DescriptorSet::GetFirstBindingOfType(DescriptorType type) const {
for (uint32_t i = 0; i < m_bindingCount; ++i) {
if (static_cast<DescriptorType>(m_bindings[i].type) == type) {
return m_bindings[i].binding;
}
}
return UINT32_MAX;
}
bool D3D12DescriptorSet::UploadConstantBuffer() {
if (!HasBindingType(DescriptorType::CBV) || m_heap == nullptr || m_heap->GetDevice() == nullptr) {
return false;
}
const uint64_t alignedSize = AlignConstantBufferSize(m_constantBufferData.size());
if (!m_constantBuffer || m_constantBufferCapacity < alignedSize) {
auto constantBuffer = std::make_unique<D3D12Buffer>();
if (!constantBuffer->Initialize(
m_heap->GetDevice(),
alignedSize,
D3D12_RESOURCE_STATE_GENERIC_READ,
D3D12_HEAP_TYPE_UPLOAD)) {
return false;
}
constantBuffer->SetBufferType(BufferType::Constant);
constantBuffer->SetStride(static_cast<uint32_t>(alignedSize));
m_constantBuffer = std::move(constantBuffer);
m_constantBufferCapacity = alignedSize;
m_constantBufferDirty = true;
}
if (m_constantBufferDirty && !m_constantBufferData.empty()) {
m_constantBuffer->SetData(m_constantBufferData.data(), m_constantBufferData.size());
m_constantBufferDirty = false;
}
return m_constantBuffer != nullptr;
}
D3D12_GPU_VIRTUAL_ADDRESS D3D12DescriptorSet::GetConstantBufferGPUAddress() const {
return m_constantBuffer ? m_constantBuffer->GetGPUVirtualAddress() : 0;
}
} // namespace RHI
} // namespace XCEngine

View File

@@ -22,13 +22,14 @@ bool D3D12PipelineLayout::InitializeInternal(D3D12Device* device, const RHIPipel
}
m_device = device;
m_desc = desc;
m_rootParameters.clear();
m_descriptorRanges.clear();
m_registerToRootIndex.clear();
const uint32_t rootParameterCount =
(desc.constantBufferCount > 0 ? 1u : 0u) +
desc.constantBufferCount +
(desc.textureCount > 0 ? 1u : 0u) +
(desc.samplerCount > 0 ? 1u : 0u);
const uint32_t descriptorRangeCount =
@@ -40,13 +41,10 @@ bool D3D12PipelineLayout::InitializeInternal(D3D12Device* device, const RHIPipel
uint32_t rootIndex = 0;
if (desc.constantBufferCount > 0) {
D3D12_ROOT_PARAMETER param = D3D12RootSignature::Create32BitConstants(
0, desc.constantBufferCount * 16, ShaderVisibility::All, 0);
for (uint32_t i = 0; i < desc.constantBufferCount; ++i) {
D3D12_ROOT_PARAMETER param = D3D12RootSignature::CreateCBV(i, ShaderVisibility::All, 0);
m_rootParameters.push_back(param);
for (uint32_t i = 0; i < desc.constantBufferCount; ++i) {
m_registerToRootIndex[i] = rootIndex;
}
m_registerToRootIndex[i] = rootIndex;
rootIndex++;
}
@@ -112,6 +110,7 @@ bool D3D12PipelineLayout::InitializeInternal(D3D12Device* device, const RHIPipel
void D3D12PipelineLayout::Shutdown() {
m_rootSignature.Reset();
m_desc = {};
m_rootParameters.clear();
m_descriptorRanges.clear();
m_registerToRootIndex.clear();

View File

@@ -140,10 +140,22 @@ void OpenGLDescriptorSet::Bind() {
}
glBindBuffer(GL_UNIFORM_BUFFER, m_constantBuffer);
glBufferData(GL_UNIFORM_BUFFER, m_constantBufferData.size(), m_constantBufferData.data(), GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, m_constantBuffer);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
m_constantBufferDirty = false;
}
if (m_constantBuffer != 0) {
for (const auto& binding : m_bindings) {
if (binding.type != DescriptorType::CBV) {
continue;
}
for (uint32_t i = 0; i < binding.count; ++i) {
glBindBufferBase(GL_UNIFORM_BUFFER, binding.binding + i, m_constantBuffer);
}
}
}
for (size_t i = 0; i < m_bindings.size(); ++i) {
const auto& binding = m_bindings[i];
@@ -170,6 +182,12 @@ void OpenGLDescriptorSet::Unbind() {
for (size_t i = 0; i < m_bindings.size(); ++i) {
const auto& binding = m_bindings[i];
if (binding.type == DescriptorType::CBV) {
for (uint32_t j = 0; j < binding.count; ++j) {
glBindBufferBase(GL_UNIFORM_BUFFER, binding.binding + j, 0);
}
}
for (size_t j = 0; j < binding.textureUnits.size(); ++j) {
uint32_t unit = binding.textureUnits[j];