Add directional shadow integration coverage

This commit is contained in:
2026-04-05 00:03:22 +08:00
parent 96a44da2cb
commit 2ddbe24b82
13 changed files with 803 additions and 94 deletions

View File

@@ -145,10 +145,13 @@ private:
ComPtr<ID3D12DescriptorHeap> m_rtvHeap;
ComPtr<ID3D12DescriptorHeap> m_shaderVisibleCbvSrvUavHeap;
ComPtr<ID3D12DescriptorHeap> m_shaderVisibleSamplerHeap;
std::vector<ComPtr<ID3D12DescriptorHeap>> m_retiredShaderVisibleHeaps;
ID3D12Device* m_device = nullptr;
CommandQueueType m_type;
uint32_t m_shaderVisibleCbvSrvUavHeapCapacity = 0;
uint32_t m_shaderVisibleSamplerHeapCapacity = 0;
uint32_t m_shaderVisibleCbvSrvUavCursor = 0;
uint32_t m_shaderVisibleSamplerCursor = 0;
std::unordered_map<ID3D12Resource*, ResourceStates> m_resourceStateMap;
std::vector<ID3D12Resource*> m_trackedResources;

View File

@@ -16,6 +16,7 @@ namespace Rendering {
class RenderSurface;
class RenderPipelineAsset;
struct RenderContext;
class CameraRenderer {
public:
@@ -43,7 +44,12 @@ public:
bool Render(const CameraRenderRequest& request);
private:
struct DirectionalShadowSurfaceResources;
void ResetPipeline(std::unique_ptr<RenderPipeline> pipeline);
DirectionalShadowSurfaceResources* AcquireDirectionalShadowSurface(
const RenderContext& context,
const DirectionalShadowRenderPlan& plan);
RenderSceneExtractor m_sceneExtractor;
std::shared_ptr<const RenderPipelineAsset> m_pipelineAsset;
@@ -51,6 +57,7 @@ private:
std::unique_ptr<ObjectIdPass> m_objectIdPass;
std::unique_ptr<RenderPass> m_depthOnlyPass;
std::unique_ptr<RenderPass> m_shadowCasterPass;
std::unique_ptr<DirectionalShadowSurfaceResources> m_directionalShadowSurface;
};
} // namespace Rendering

View File

@@ -125,8 +125,11 @@ void D3D12CommandList::Shutdown() {
m_rtvHeap.Reset();
m_shaderVisibleCbvSrvUavHeap.Reset();
m_shaderVisibleSamplerHeap.Reset();
m_retiredShaderVisibleHeaps.clear();
m_shaderVisibleCbvSrvUavHeapCapacity = 0;
m_shaderVisibleSamplerHeapCapacity = 0;
m_shaderVisibleCbvSrvUavCursor = 0;
m_shaderVisibleSamplerCursor = 0;
m_resourceStateMap.clear();
m_trackedResources.clear();
m_currentShader = nullptr;
@@ -142,6 +145,9 @@ void D3D12CommandList::Reset() {
m_currentRootSignature = nullptr;
m_currentPipelineLayout = nullptr;
m_currentDescriptorHeap = nullptr;
m_retiredShaderVisibleHeaps.clear();
m_shaderVisibleCbvSrvUavCursor = 0;
m_shaderVisibleSamplerCursor = 0;
m_resourceStateMap.clear();
m_trackedResources.clear();
m_boundRenderTargets.clear();
@@ -250,17 +256,22 @@ void D3D12CommandList::SetGraphicsDescriptorSets(
auto ensureShaderVisibleHeap =
[this](D3D12_DESCRIPTOR_HEAP_TYPE heapType,
uint32_t requiredDescriptors,
uint32_t currentCursor,
ComPtr<ID3D12DescriptorHeap>& heap,
uint32_t& capacity) -> bool {
if (requiredDescriptors == 0) {
return true;
}
if (heap != nullptr && capacity >= requiredDescriptors) {
const uint32_t requiredCapacity = currentCursor + requiredDescriptors;
if (heap != nullptr && capacity >= requiredCapacity) {
return true;
}
const uint32_t newCapacity = std::max<uint32_t>(requiredDescriptors, capacity > 0 ? capacity * 2u : 32u);
const uint32_t baselineCapacity =
heapType == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER ? 256u : 4096u;
const uint32_t grownCapacity = capacity > 0 ? capacity * 2u : baselineCapacity;
const uint32_t newCapacity = std::max<uint32_t>(requiredCapacity, std::max<uint32_t>(grownCapacity, baselineCapacity));
D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
heapDesc.Type = heapType;
heapDesc.NumDescriptors = newCapacity;
@@ -272,6 +283,9 @@ void D3D12CommandList::SetGraphicsDescriptorSets(
return false;
}
if (heap != nullptr) {
m_retiredShaderVisibleHeaps.push_back(heap);
}
heap = newHeap;
capacity = newCapacity;
return true;
@@ -280,11 +294,13 @@ void D3D12CommandList::SetGraphicsDescriptorSets(
if (!ensureShaderVisibleHeap(
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
requiredCbvSrvUavDescriptors,
m_shaderVisibleCbvSrvUavCursor,
m_shaderVisibleCbvSrvUavHeap,
m_shaderVisibleCbvSrvUavHeapCapacity) ||
!ensureShaderVisibleHeap(
D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
requiredSamplerDescriptors,
m_shaderVisibleSamplerCursor,
m_shaderVisibleSamplerHeap,
m_shaderVisibleSamplerHeapCapacity)) {
return;
@@ -302,8 +318,8 @@ void D3D12CommandList::SetGraphicsDescriptorSets(
SetDescriptorHeaps(static_cast<uint32_t>(descriptorHeaps.size()), descriptorHeaps.data());
}
uint32_t nextCbvSrvUavDescriptor = 0;
uint32_t nextSamplerDescriptor = 0;
uint32_t nextCbvSrvUavDescriptor = m_shaderVisibleCbvSrvUavCursor;
uint32_t nextSamplerDescriptor = m_shaderVisibleSamplerCursor;
for (uint32_t i = 0; i < count; ++i) {
if (descriptorSets[i] == nullptr) {
@@ -434,6 +450,9 @@ void D3D12CommandList::SetGraphicsDescriptorSets(
}
}
}
m_shaderVisibleCbvSrvUavCursor = nextCbvSrvUavDescriptor;
m_shaderVisibleSamplerCursor = nextSamplerDescriptor;
}
void D3D12CommandList::SetComputeDescriptorSets(
@@ -470,17 +489,22 @@ void D3D12CommandList::SetComputeDescriptorSets(
auto ensureShaderVisibleHeap =
[this](D3D12_DESCRIPTOR_HEAP_TYPE heapType,
uint32_t requiredDescriptors,
uint32_t currentCursor,
ComPtr<ID3D12DescriptorHeap>& heap,
uint32_t& capacity) -> bool {
if (requiredDescriptors == 0) {
return true;
}
if (heap != nullptr && capacity >= requiredDescriptors) {
const uint32_t requiredCapacity = currentCursor + requiredDescriptors;
if (heap != nullptr && capacity >= requiredCapacity) {
return true;
}
const uint32_t newCapacity = std::max<uint32_t>(requiredDescriptors, capacity > 0 ? capacity * 2u : 32u);
const uint32_t baselineCapacity =
heapType == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER ? 256u : 4096u;
const uint32_t grownCapacity = capacity > 0 ? capacity * 2u : baselineCapacity;
const uint32_t newCapacity = std::max<uint32_t>(requiredCapacity, std::max<uint32_t>(grownCapacity, baselineCapacity));
D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
heapDesc.Type = heapType;
heapDesc.NumDescriptors = newCapacity;
@@ -492,6 +516,9 @@ void D3D12CommandList::SetComputeDescriptorSets(
return false;
}
if (heap != nullptr) {
m_retiredShaderVisibleHeaps.push_back(heap);
}
heap = newHeap;
capacity = newCapacity;
return true;
@@ -500,11 +527,13 @@ void D3D12CommandList::SetComputeDescriptorSets(
if (!ensureShaderVisibleHeap(
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
requiredCbvSrvUavDescriptors,
m_shaderVisibleCbvSrvUavCursor,
m_shaderVisibleCbvSrvUavHeap,
m_shaderVisibleCbvSrvUavHeapCapacity) ||
!ensureShaderVisibleHeap(
D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
requiredSamplerDescriptors,
m_shaderVisibleSamplerCursor,
m_shaderVisibleSamplerHeap,
m_shaderVisibleSamplerHeapCapacity)) {
return;
@@ -522,8 +551,8 @@ void D3D12CommandList::SetComputeDescriptorSets(
SetDescriptorHeaps(static_cast<uint32_t>(descriptorHeaps.size()), descriptorHeaps.data());
}
uint32_t nextCbvSrvUavDescriptor = 0;
uint32_t nextSamplerDescriptor = 0;
uint32_t nextCbvSrvUavDescriptor = m_shaderVisibleCbvSrvUavCursor;
uint32_t nextSamplerDescriptor = m_shaderVisibleSamplerCursor;
for (uint32_t i = 0; i < count; ++i) {
if (descriptorSets[i] == nullptr) {
@@ -654,6 +683,9 @@ void D3D12CommandList::SetComputeDescriptorSets(
}
}
}
m_shaderVisibleCbvSrvUavCursor = nextCbvSrvUavDescriptor;
m_shaderVisibleSamplerCursor = nextSamplerDescriptor;
}
void D3D12CommandList::TransitionBarrierInternal(ID3D12Resource* resource, ResourceStates stateBefore, ResourceStates stateAfter, uint32_t subresource) {

View File

@@ -7,10 +7,48 @@
#include <d3d12.h>
#include <dxgi.h>
#include <stdio.h>
#include <vector>
namespace XCEngine {
namespace RHI {
namespace {
void DumpD3D12InfoQueueMessages(ID3D12Device* device) {
if (device == nullptr) {
return;
}
ID3D12InfoQueue* infoQueue = nullptr;
if (FAILED(device->QueryInterface(IID_PPV_ARGS(&infoQueue))) || infoQueue == nullptr) {
return;
}
const UINT64 messageCount = infoQueue->GetNumStoredMessagesAllowedByRetrievalFilter();
const UINT64 startIndex = messageCount > 12 ? messageCount - 12 : 0;
for (UINT64 messageIndex = startIndex; messageIndex < messageCount; ++messageIndex) {
SIZE_T messageLength = 0;
if (FAILED(infoQueue->GetMessage(messageIndex, nullptr, &messageLength)) || messageLength == 0) {
continue;
}
std::vector<uint8_t> messageStorage(messageLength);
auto* message = reinterpret_cast<D3D12_MESSAGE*>(messageStorage.data());
if (FAILED(infoQueue->GetMessage(messageIndex, message, &messageLength)) ||
message->pDescription == nullptr) {
continue;
}
XCEngine::Debug::Logger::Get().Error(
XCEngine::Debug::LogCategory::Rendering,
message->pDescription);
}
infoQueue->Release();
}
} // namespace
bool D3D12Screenshot::Capture(ID3D12Device* device,
ID3D12CommandQueue* commandQueue,
ID3D12Resource* renderTarget,
@@ -156,7 +194,21 @@ bool D3D12Screenshot::CopyToReadbackAndSave(ID3D12Device* device,
ID3D12Fence* fence = nullptr;
hr = device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));
if (FAILED(hr)) {
XCEngine::Debug::Logger::Get().Error(XCEngine::Debug::LogCategory::Rendering, "Screenshot: CreateFence failed");
char message[256];
sprintf_s(message, sizeof(message), "Screenshot: CreateFence failed hr=0x%08X", static_cast<unsigned int>(hr));
XCEngine::Debug::Logger::Get().Error(XCEngine::Debug::LogCategory::Rendering, message);
DumpD3D12InfoQueueMessages(device);
if (hr == DXGI_ERROR_DEVICE_REMOVED ||
hr == DXGI_ERROR_DEVICE_HUNG ||
hr == DXGI_ERROR_DEVICE_RESET) {
const HRESULT removedReason = device->GetDeviceRemovedReason();
sprintf_s(
message,
sizeof(message),
"Screenshot: Device removed reason after CreateFence failure: 0x%08X",
static_cast<unsigned int>(removedReason));
XCEngine::Debug::Logger::Get().Error(XCEngine::Debug::LogCategory::Rendering, message);
}
cmdList->Release();
cmdAlloc->Release();
readbackBuffer->Release();
@@ -234,4 +286,4 @@ bool D3D12Screenshot::Capture(RHIDevice* device, RHISwapChain* swapChain, const
}
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -14,22 +14,32 @@
namespace XCEngine {
namespace Rendering {
namespace {
struct OwnedShadowSurfaceResources {
struct CameraRenderer::DirectionalShadowSurfaceResources {
RHI::RHIDevice* device = nullptr;
uint32_t width = 0;
uint32_t height = 0;
RHI::RHITexture* depthTexture = nullptr;
RHI::RHIResourceView* depthView = nullptr;
RHI::RHIResourceView* shaderView = nullptr;
RenderSurface surface = {};
OwnedShadowSurfaceResources() = default;
OwnedShadowSurfaceResources(const OwnedShadowSurfaceResources&) = delete;
OwnedShadowSurfaceResources& operator=(const OwnedShadowSurfaceResources&) = delete;
DirectionalShadowSurfaceResources() = default;
DirectionalShadowSurfaceResources(const DirectionalShadowSurfaceResources&) = delete;
DirectionalShadowSurfaceResources& operator=(const DirectionalShadowSurfaceResources&) = delete;
~OwnedShadowSurfaceResources() {
~DirectionalShadowSurfaceResources() {
Reset();
}
bool Matches(const RenderContext& context, const DirectionalShadowRenderPlan& plan) const {
return device == context.device &&
width == plan.mapWidth &&
height == plan.mapHeight &&
depthTexture != nullptr &&
depthView != nullptr &&
shaderView != nullptr;
}
void Reset() {
if (shaderView != nullptr) {
shaderView->Shutdown();
@@ -49,10 +59,15 @@ struct OwnedShadowSurfaceResources {
depthTexture = nullptr;
}
device = nullptr;
width = 0;
height = 0;
surface = RenderSurface();
}
};
namespace {
bool InitializePassSequence(
RenderPassSequence* sequence,
const RenderContext& context,
@@ -150,67 +165,6 @@ bool ExecuteScenePassRequest(
return pass->Execute(passContext);
}
bool CreateDirectionalShadowSurface(
const RenderContext& context,
const DirectionalShadowRenderPlan& plan,
OwnedShadowSurfaceResources& outResources) {
if (!context.IsValid() || !plan.IsValid()) {
return false;
}
RHI::TextureDesc depthDesc = {};
depthDesc.width = plan.mapWidth;
depthDesc.height = plan.mapHeight;
depthDesc.depth = 1;
depthDesc.mipLevels = 1;
depthDesc.arraySize = 1;
depthDesc.format = static_cast<uint32_t>(RHI::Format::D24_UNorm_S8_UInt);
depthDesc.textureType = static_cast<uint32_t>(RHI::TextureType::Texture2D);
depthDesc.sampleCount = 1;
depthDesc.sampleQuality = 0;
depthDesc.flags = 0;
RHI::RHITexture* depthTexture = context.device->CreateTexture(depthDesc);
if (depthTexture == nullptr) {
return false;
}
RHI::ResourceViewDesc depthViewDesc = {};
depthViewDesc.format = static_cast<uint32_t>(RHI::Format::D24_UNorm_S8_UInt);
depthViewDesc.dimension = RHI::ResourceViewDimension::Texture2D;
depthViewDesc.mipLevel = 0;
RHI::RHIResourceView* depthView =
context.device->CreateDepthStencilView(depthTexture, depthViewDesc);
if (depthView == nullptr) {
depthTexture->Shutdown();
delete depthTexture;
return false;
}
RHI::ResourceViewDesc shaderViewDesc = {};
shaderViewDesc.format = depthDesc.format;
shaderViewDesc.dimension = RHI::ResourceViewDimension::Texture2D;
shaderViewDesc.mipLevel = 0;
RHI::RHIResourceView* shaderView =
context.device->CreateShaderResourceView(depthTexture, shaderViewDesc);
if (shaderView == nullptr) {
depthView->Shutdown();
delete depthView;
depthTexture->Shutdown();
delete depthTexture;
return false;
}
outResources.Reset();
outResources.depthTexture = depthTexture;
outResources.depthView = depthView;
outResources.shaderView = shaderView;
outResources.surface = RenderSurface(plan.mapWidth, plan.mapHeight);
outResources.surface.SetDepthAttachment(depthView);
return true;
}
RenderDirectionalShadowData BuildDirectionalShadowData(
const DirectionalShadowRenderPlan& plan,
RHI::RHIResourceView* shadowMapView) {
@@ -343,6 +297,75 @@ void CameraRenderer::ResetPipeline(std::unique_ptr<RenderPipeline> pipeline) {
}
}
CameraRenderer::DirectionalShadowSurfaceResources* CameraRenderer::AcquireDirectionalShadowSurface(
const RenderContext& context,
const DirectionalShadowRenderPlan& plan) {
if (!context.IsValid() || !plan.IsValid()) {
return nullptr;
}
if (m_directionalShadowSurface == nullptr) {
m_directionalShadowSurface = std::make_unique<DirectionalShadowSurfaceResources>();
}
if (!m_directionalShadowSurface->Matches(context, plan)) {
RHI::TextureDesc depthDesc = {};
depthDesc.width = plan.mapWidth;
depthDesc.height = plan.mapHeight;
depthDesc.depth = 1;
depthDesc.mipLevels = 1;
depthDesc.arraySize = 1;
depthDesc.format = static_cast<uint32_t>(RHI::Format::D24_UNorm_S8_UInt);
depthDesc.textureType = static_cast<uint32_t>(RHI::TextureType::Texture2D);
depthDesc.sampleCount = 1;
depthDesc.sampleQuality = 0;
depthDesc.flags = 0;
RHI::RHITexture* depthTexture = context.device->CreateTexture(depthDesc);
if (depthTexture == nullptr) {
return nullptr;
}
RHI::ResourceViewDesc depthViewDesc = {};
depthViewDesc.format = static_cast<uint32_t>(RHI::Format::D24_UNorm_S8_UInt);
depthViewDesc.dimension = RHI::ResourceViewDimension::Texture2D;
depthViewDesc.mipLevel = 0;
RHI::RHIResourceView* depthView =
context.device->CreateDepthStencilView(depthTexture, depthViewDesc);
if (depthView == nullptr) {
depthTexture->Shutdown();
delete depthTexture;
return nullptr;
}
RHI::ResourceViewDesc shaderViewDesc = {};
shaderViewDesc.format = depthDesc.format;
shaderViewDesc.dimension = RHI::ResourceViewDimension::Texture2D;
shaderViewDesc.mipLevel = 0;
RHI::RHIResourceView* shaderView =
context.device->CreateShaderResourceView(depthTexture, shaderViewDesc);
if (shaderView == nullptr) {
depthView->Shutdown();
delete depthView;
depthTexture->Shutdown();
delete depthTexture;
return nullptr;
}
m_directionalShadowSurface->Reset();
m_directionalShadowSurface->device = context.device;
m_directionalShadowSurface->width = plan.mapWidth;
m_directionalShadowSurface->height = plan.mapHeight;
m_directionalShadowSurface->depthTexture = depthTexture;
m_directionalShadowSurface->depthView = depthView;
m_directionalShadowSurface->shaderView = shaderView;
m_directionalShadowSurface->surface = RenderSurface(plan.mapWidth, plan.mapHeight);
m_directionalShadowSurface->surface.SetDepthAttachment(depthView);
}
return m_directionalShadowSurface.get();
}
bool CameraRenderer::Render(
const CameraRenderRequest& request) {
if (!request.IsValid() || m_pipeline == nullptr) {
@@ -363,20 +386,20 @@ bool CameraRenderer::Render(
}
ShadowCasterRenderRequest resolvedShadowCaster = request.shadowCaster;
OwnedShadowSurfaceResources ownedShadowSurface;
DirectionalShadowSurfaceResources* directionalShadowSurface = nullptr;
if (resolvedShadowCaster.IsRequested()) {
if (!resolvedShadowCaster.IsValid()) {
return false;
}
} else if (request.directionalShadow.IsValid()) {
if (!CreateDirectionalShadowSurface(
request.context,
request.directionalShadow,
ownedShadowSurface)) {
directionalShadowSurface = AcquireDirectionalShadowSurface(
request.context,
request.directionalShadow);
if (directionalShadowSurface == nullptr) {
return false;
}
resolvedShadowCaster.surface = ownedShadowSurface.surface;
resolvedShadowCaster.surface = directionalShadowSurface->surface;
if (!resolvedShadowCaster.hasCameraDataOverride) {
resolvedShadowCaster.hasCameraDataOverride = true;
resolvedShadowCaster.cameraDataOverride = request.directionalShadow.cameraData;
@@ -393,7 +416,8 @@ bool CameraRenderer::Render(
}
if (request.directionalShadow.IsValid()) {
RHI::RHIResourceView* shadowMapView = ownedShadowSurface.shaderView;
RHI::RHIResourceView* shadowMapView =
directionalShadowSurface != nullptr ? directionalShadowSurface->shaderView : nullptr;
sceneData.lighting.mainDirectionalShadow =
BuildDirectionalShadowData(request.directionalShadow, shadowMapView);
}

View File

@@ -101,9 +101,11 @@ RHI::GraphicsPipelineDesc CreatePipelineDesc(
shader.FindVariant(passName, Resources::ShaderType::Vertex, backend)) {
::XCEngine::Rendering::Detail::ApplyShaderStageVariant(*vertexVariant, pipelineDesc.vertexShader);
}
if (const Resources::ShaderStageVariant* fragmentVariant =
shader.FindVariant(passName, Resources::ShaderType::Fragment, backend)) {
::XCEngine::Rendering::Detail::ApplyShaderStageVariant(*fragmentVariant, pipelineDesc.fragmentShader);
if (pipelineDesc.renderTargetCount > 0) {
if (const Resources::ShaderStageVariant* fragmentVariant =
shader.FindVariant(passName, Resources::ShaderType::Fragment, backend)) {
::XCEngine::Rendering::Detail::ApplyShaderStageVariant(*fragmentVariant, pipelineDesc.fragmentShader);
}
}
return pipelineDesc;