Fix D3D12 pipeline format mapping and add transparent material scene test

This commit is contained in:
2026-03-27 13:01:17 +08:00
parent 79e7452245
commit 134a80b334
7 changed files with 631983 additions and 59 deletions

View File

@@ -15,6 +15,10 @@
#include <unordered_map>
namespace XCEngine {
namespace Components {
class GameObject;
} // namespace Components
namespace Resources {
class Material;
class Texture;
@@ -38,6 +42,11 @@ public:
const RenderSceneData& sceneData) override;
private:
struct OwnedDescriptorSet {
RHI::RHIDescriptorPool* pool = nullptr;
RHI::RHIDescriptorSet* set = nullptr;
};
struct PerObjectConstants {
Math::Matrix4x4 projection = Math::Matrix4x4::Identity();
Math::Matrix4x4 view = Math::Matrix4x4::Identity();
@@ -50,6 +59,9 @@ private:
RHI::RHIPipelineState* GetOrCreatePipelineState(
const RenderContext& context,
const Resources::Material* material);
RHI::RHIDescriptorSet* GetOrCreatePerObjectSet(Core::uint64 objectId);
RHI::RHIDescriptorSet* GetOrCreateTextureSet(RHI::RHIResourceView* textureView);
void DestroyOwnedDescriptorSet(OwnedDescriptorSet& descriptorSet);
const Resources::Texture* ResolveTexture(const Resources::Material* material) const;
RHI::RHIResourceView* ResolveTextureView(const VisibleRenderItem& visibleItem);
@@ -64,14 +76,12 @@ private:
RenderResourceCache m_resourceCache;
RHI::RHIDescriptorPool* m_constantPool = nullptr;
RHI::RHIDescriptorSet* m_constantSet = nullptr;
RHI::RHIDescriptorPool* m_texturePool = nullptr;
RHI::RHIDescriptorSet* m_textureSet = nullptr;
RHI::RHIDescriptorPool* m_samplerPool = nullptr;
RHI::RHIDescriptorSet* m_samplerSet = nullptr;
RHI::RHIPipelineLayout* m_pipelineLayout = nullptr;
std::unordered_map<Resources::MaterialRenderState, RHI::RHIPipelineState*, MaterialRenderStateHash> m_pipelineStates;
std::unordered_map<Core::uint64, OwnedDescriptorSet> m_perObjectSets;
std::unordered_map<RHI::RHIResourceView*, OwnedDescriptorSet> m_textureSets;
RHI::RHISampler* m_sampler = nullptr;
RHI::RHITexture* m_fallbackTexture = nullptr;
RHI::RHIResourceView* m_fallbackTextureView = nullptr;

View File

@@ -1,5 +1,6 @@
#include "XCEngine/RHI/D3D12/D3D12PipelineState.h"
#include "XCEngine/RHI/D3D12/D3D12Shader.h"
#include "XCEngine/Debug/Logger.h"
#include <cstring>
namespace XCEngine {
@@ -191,9 +192,9 @@ bool D3D12PipelineState::CreateD3D12PSO() {
desc.NumRenderTargets = m_renderTargetCount;
for (uint32_t i = 0; i < m_renderTargetCount && i < 8; ++i) {
desc.RTVFormats[i] = static_cast<DXGI_FORMAT>(m_renderTargetFormats[i]);
desc.RTVFormats[i] = ToD3D12(static_cast<Format>(m_renderTargetFormats[i]));
}
desc.DSVFormat = static_cast<DXGI_FORMAT>(m_depthStencilFormat);
desc.DSVFormat = ToD3D12(static_cast<Format>(m_depthStencilFormat));
desc.SampleDesc.Count = m_sampleCount;
desc.SampleDesc.Quality = 0;
desc.SampleMask = 0xffffffff;
@@ -201,6 +202,9 @@ bool D3D12PipelineState::CreateD3D12PSO() {
HRESULT hr = m_device->CreateGraphicsPipelineState(&desc, IID_PPV_ARGS(&m_pipelineState));
if (FAILED(hr)) {
char errorMessage[256] = {};
sprintf_s(errorMessage, "D3D12 CreateGraphicsPipelineState failed: hr=0x%08X", static_cast<unsigned int>(hr));
Debug::Logger::Get().Error(Debug::LogCategory::Rendering, errorMessage);
return false;
}

View File

@@ -1,8 +1,10 @@
#include "Rendering/Pipelines/BuiltinForwardPipeline.h"
#include "Components/GameObject.h"
#include "Components/MeshFilterComponent.h"
#include "Components/MeshRendererComponent.h"
#include "RHI/RHICommandList.h"
#include "Debug/Logger.h"
#include "Rendering/RenderMaterialUtility.h"
#include "Rendering/RenderSurface.h"
#include "Resources/Material/Material.h"
@@ -296,15 +298,6 @@ bool BuiltinForwardPipeline::EnsureInitialized(const RenderContext& context) {
}
bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& context) {
RHI::DescriptorPoolDesc constantPoolDesc = {};
constantPoolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
constantPoolDesc.descriptorCount = 1;
constantPoolDesc.shaderVisible = false;
m_constantPool = context.device->CreateDescriptorPool(constantPoolDesc);
if (m_constantPool == nullptr) {
return false;
}
RHI::DescriptorSetLayoutBinding constantBinding = {};
constantBinding.binding = 0;
constantBinding.type = static_cast<uint32_t>(RHI::DescriptorType::CBV);
@@ -313,19 +306,6 @@ bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& contex
RHI::DescriptorSetLayoutDesc constantLayout = {};
constantLayout.bindings = &constantBinding;
constantLayout.bindingCount = 1;
m_constantSet = m_constantPool->AllocateSet(constantLayout);
if (m_constantSet == nullptr) {
return false;
}
RHI::DescriptorPoolDesc texturePoolDesc = {};
texturePoolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
texturePoolDesc.descriptorCount = 1;
texturePoolDesc.shaderVisible = true;
m_texturePool = context.device->CreateDescriptorPool(texturePoolDesc);
if (m_texturePool == nullptr) {
return false;
}
RHI::DescriptorSetLayoutBinding textureBinding = {};
textureBinding.binding = 0;
@@ -335,10 +315,6 @@ bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& contex
RHI::DescriptorSetLayoutDesc textureLayout = {};
textureLayout.bindings = &textureBinding;
textureLayout.bindingCount = 1;
m_textureSet = m_texturePool->AllocateSet(textureLayout);
if (m_textureSet == nullptr) {
return false;
}
RHI::DescriptorPoolDesc samplerPoolDesc = {};
samplerPoolDesc.type = RHI::DescriptorHeapType::Sampler;
@@ -447,6 +423,16 @@ void BuiltinForwardPipeline::DestroyPipelineResources() {
}
m_pipelineStates.clear();
for (auto& perObjectSetPair : m_perObjectSets) {
DestroyOwnedDescriptorSet(perObjectSetPair.second);
}
m_perObjectSets.clear();
for (auto& textureSetPair : m_textureSets) {
DestroyOwnedDescriptorSet(textureSetPair.second);
}
m_textureSets.clear();
if (m_fallbackTextureView != nullptr) {
m_fallbackTextureView->Shutdown();
delete m_fallbackTextureView;
@@ -471,36 +457,12 @@ void BuiltinForwardPipeline::DestroyPipelineResources() {
m_pipelineLayout = nullptr;
}
if (m_constantSet != nullptr) {
m_constantSet->Shutdown();
delete m_constantSet;
m_constantSet = nullptr;
}
if (m_textureSet != nullptr) {
m_textureSet->Shutdown();
delete m_textureSet;
m_textureSet = nullptr;
}
if (m_samplerSet != nullptr) {
m_samplerSet->Shutdown();
delete m_samplerSet;
m_samplerSet = nullptr;
}
if (m_constantPool != nullptr) {
m_constantPool->Shutdown();
delete m_constantPool;
m_constantPool = nullptr;
}
if (m_texturePool != nullptr) {
m_texturePool->Shutdown();
delete m_texturePool;
m_texturePool = nullptr;
}
if (m_samplerPool != nullptr) {
m_samplerPool->Shutdown();
delete m_samplerPool;
@@ -526,6 +488,9 @@ RHI::RHIPipelineState* BuiltinForwardPipeline::GetOrCreatePipelineState(
CreatePipelineDesc(context.backendType, m_pipelineLayout, material);
RHI::RHIPipelineState* pipelineState = context.device->CreatePipelineState(pipelineDesc);
if (pipelineState == nullptr || !pipelineState->IsValid()) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline failed to create pipeline state");
if (pipelineState != nullptr) {
pipelineState->Shutdown();
delete pipelineState;
@@ -537,6 +502,95 @@ RHI::RHIPipelineState* BuiltinForwardPipeline::GetOrCreatePipelineState(
return pipelineState;
}
RHI::RHIDescriptorSet* BuiltinForwardPipeline::GetOrCreatePerObjectSet(Core::uint64 objectId) {
const auto existing = m_perObjectSets.find(objectId);
if (existing != m_perObjectSets.end()) {
return existing->second.set;
}
RHI::DescriptorPoolDesc poolDesc = {};
poolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
poolDesc.descriptorCount = 1;
poolDesc.shaderVisible = false;
OwnedDescriptorSet descriptorSet = {};
descriptorSet.pool = m_device->CreateDescriptorPool(poolDesc);
if (descriptorSet.pool == nullptr) {
return nullptr;
}
RHI::DescriptorSetLayoutBinding binding = {};
binding.binding = 0;
binding.type = static_cast<uint32_t>(RHI::DescriptorType::CBV);
binding.count = 1;
RHI::DescriptorSetLayoutDesc layout = {};
layout.bindings = &binding;
layout.bindingCount = 1;
descriptorSet.set = descriptorSet.pool->AllocateSet(layout);
if (descriptorSet.set == nullptr) {
DestroyOwnedDescriptorSet(descriptorSet);
return nullptr;
}
const auto result = m_perObjectSets.emplace(objectId, descriptorSet);
return result.first->second.set;
}
RHI::RHIDescriptorSet* BuiltinForwardPipeline::GetOrCreateTextureSet(RHI::RHIResourceView* textureView) {
if (textureView == nullptr) {
return nullptr;
}
const auto existing = m_textureSets.find(textureView);
if (existing != m_textureSets.end()) {
return existing->second.set;
}
RHI::DescriptorPoolDesc poolDesc = {};
poolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
poolDesc.descriptorCount = 1;
poolDesc.shaderVisible = true;
OwnedDescriptorSet descriptorSet = {};
descriptorSet.pool = m_device->CreateDescriptorPool(poolDesc);
if (descriptorSet.pool == nullptr) {
return nullptr;
}
RHI::DescriptorSetLayoutBinding binding = {};
binding.binding = 0;
binding.type = static_cast<uint32_t>(RHI::DescriptorType::SRV);
binding.count = 1;
RHI::DescriptorSetLayoutDesc layout = {};
layout.bindings = &binding;
layout.bindingCount = 1;
descriptorSet.set = descriptorSet.pool->AllocateSet(layout);
if (descriptorSet.set == nullptr) {
DestroyOwnedDescriptorSet(descriptorSet);
return nullptr;
}
descriptorSet.set->Update(0, textureView);
const auto result = m_textureSets.emplace(textureView, descriptorSet);
return result.first->second.set;
}
void BuiltinForwardPipeline::DestroyOwnedDescriptorSet(OwnedDescriptorSet& descriptorSet) {
if (descriptorSet.set != nullptr) {
descriptorSet.set->Shutdown();
delete descriptorSet.set;
descriptorSet.set = nullptr;
}
if (descriptorSet.pool != nullptr) {
descriptorSet.pool->Shutdown();
delete descriptorSet.pool;
descriptorSet.pool = nullptr;
}
}
const Resources::Texture* BuiltinForwardPipeline::ResolveTexture(const Resources::Material* material) const {
return material != nullptr ? FindMaterialTexture(*material) : nullptr;
}
@@ -585,9 +639,15 @@ bool BuiltinForwardPipeline::DrawVisibleItem(
return false;
}
m_constantSet->WriteConstant(0, &constants, sizeof(constants));
m_textureSet->Update(0, textureView);
RHI::RHIDescriptorSet* descriptorSets[] = { m_constantSet, m_textureSet, m_samplerSet };
RHI::RHIDescriptorSet* constantSet = GetOrCreatePerObjectSet(
visibleItem.gameObject != nullptr ? visibleItem.gameObject->GetID() : 0);
RHI::RHIDescriptorSet* textureSet = GetOrCreateTextureSet(textureView);
if (constantSet == nullptr || textureSet == nullptr || m_samplerSet == nullptr) {
return false;
}
constantSet->WriteConstant(0, &constants, sizeof(constants));
RHI::RHIDescriptorSet* descriptorSets[] = { constantSet, textureSet, m_samplerSet };
commandList->SetGraphicsDescriptorSets(kDescriptorFirstSet, 3, descriptorSets, m_pipelineLayout);
if (visibleItem.hasSection) {