Split builtin forward pipeline into feature and internal modules
This commit is contained in:
@@ -1,676 +1,36 @@
|
||||
#include "Rendering/Pipelines/BuiltinForwardPipeline.h"
|
||||
|
||||
#include "Debug/Logger.h"
|
||||
#include "Core/Asset/ResourceManager.h"
|
||||
#include "RHI/RHICommandList.h"
|
||||
#include "Rendering/Internal/RenderSurfacePipelineUtils.h"
|
||||
#include "Rendering/Passes/BuiltinGaussianSplatPass.h"
|
||||
#include "Rendering/Materials/RenderMaterialResolve.h"
|
||||
#include "Rendering/Passes/BuiltinVolumetricPass.h"
|
||||
#include "Rendering/RenderSurface.h"
|
||||
#include "Resources/BuiltinResources.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include "Rendering/Pipelines/Internal/BuiltinForwardSceneSetup.h"
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Rendering {
|
||||
namespace Pipelines {
|
||||
namespace {
|
||||
|
||||
bool IsDepthFormat(RHI::Format format) {
|
||||
return format == RHI::Format::D24_UNorm_S8_UInt ||
|
||||
format == RHI::Format::D32_Float;
|
||||
}
|
||||
|
||||
bool ShouldSampleMainDirectionalShadowMap(const RenderSceneData& sceneData) {
|
||||
return sceneData.lighting.HasMainDirectionalShadow() &&
|
||||
sceneData.lighting.mainDirectionalShadow.shadowMap != nullptr &&
|
||||
IsDepthFormat(sceneData.lighting.mainDirectionalShadow.shadowMap->GetFormat());
|
||||
}
|
||||
|
||||
void TransitionMainDirectionalShadowForSampling(
|
||||
const RenderContext& context,
|
||||
const RenderSceneData& sceneData) {
|
||||
context.commandList->TransitionBarrier(
|
||||
sceneData.lighting.mainDirectionalShadow.shadowMap,
|
||||
RHI::ResourceStates::DepthWrite,
|
||||
RHI::ResourceStates::PixelShaderResource);
|
||||
}
|
||||
|
||||
void RestoreMainDirectionalShadowAfterSampling(
|
||||
const RenderContext& context,
|
||||
const RenderSceneData& sceneData) {
|
||||
context.commandList->TransitionBarrier(
|
||||
sceneData.lighting.mainDirectionalShadow.shadowMap,
|
||||
RHI::ResourceStates::PixelShaderResource,
|
||||
RHI::ResourceStates::DepthWrite);
|
||||
}
|
||||
|
||||
std::vector<RHI::RHIResourceView*> CollectSurfaceColorAttachments(const RenderSurface& surface) {
|
||||
std::vector<RHI::RHIResourceView*> renderTargets;
|
||||
const Core::uint32 colorAttachmentCount =
|
||||
::XCEngine::Rendering::Internal::ResolveSurfaceColorAttachmentCount(surface);
|
||||
renderTargets.reserve(colorAttachmentCount);
|
||||
for (Core::uint32 attachmentIndex = 0; attachmentIndex < colorAttachmentCount; ++attachmentIndex) {
|
||||
RHI::RHIResourceView* renderTarget = surface.GetColorAttachments()[attachmentIndex];
|
||||
if (renderTarget == nullptr) {
|
||||
break;
|
||||
}
|
||||
|
||||
renderTargets.push_back(renderTarget);
|
||||
}
|
||||
|
||||
return renderTargets;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
BuiltinForwardPipeline::BuiltinForwardPipeline() {
|
||||
m_gaussianSplatPass = std::make_unique<Passes::BuiltinGaussianSplatPass>();
|
||||
m_volumetricPass = std::make_unique<Passes::BuiltinVolumetricPass>();
|
||||
Internal::RegisterBuiltinForwardSceneFeatures(m_forwardSceneFeatureHost);
|
||||
}
|
||||
|
||||
BuiltinForwardPipeline::~BuiltinForwardPipeline() {
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
void BuiltinForwardPipeline::AddForwardSceneFeaturePass(
|
||||
std::unique_ptr<SceneRenderFeaturePass> featurePass) {
|
||||
m_forwardSceneFeatureHost.AddFeaturePass(std::move(featurePass));
|
||||
}
|
||||
|
||||
size_t BuiltinForwardPipeline::GetForwardSceneFeaturePassCount() const {
|
||||
return m_forwardSceneFeatureHost.GetFeaturePassCount();
|
||||
}
|
||||
|
||||
SceneRenderFeaturePass* BuiltinForwardPipeline::GetForwardSceneFeaturePass(size_t index) const {
|
||||
return m_forwardSceneFeatureHost.GetFeaturePass(index);
|
||||
}
|
||||
|
||||
std::unique_ptr<RenderPipeline> BuiltinForwardPipelineAsset::CreatePipeline() const {
|
||||
return std::make_unique<BuiltinForwardPipeline>();
|
||||
}
|
||||
|
||||
RHI::InputLayoutDesc BuiltinForwardPipeline::BuildInputLayout() {
|
||||
RHI::InputLayoutDesc inputLayout = {};
|
||||
|
||||
RHI::InputElementDesc position = {};
|
||||
position.semanticName = "POSITION";
|
||||
position.semanticIndex = 0;
|
||||
position.format = static_cast<uint32_t>(RHI::Format::R32G32B32_Float);
|
||||
position.inputSlot = 0;
|
||||
position.alignedByteOffset = 0;
|
||||
inputLayout.elements.push_back(position);
|
||||
|
||||
RHI::InputElementDesc normal = {};
|
||||
normal.semanticName = "NORMAL";
|
||||
normal.semanticIndex = 0;
|
||||
normal.format = static_cast<uint32_t>(RHI::Format::R32G32B32_Float);
|
||||
normal.inputSlot = 0;
|
||||
normal.alignedByteOffset = static_cast<uint32_t>(offsetof(Resources::StaticMeshVertex, normal));
|
||||
inputLayout.elements.push_back(normal);
|
||||
|
||||
RHI::InputElementDesc texcoord = {};
|
||||
texcoord.semanticName = "TEXCOORD";
|
||||
texcoord.semanticIndex = 0;
|
||||
texcoord.format = static_cast<uint32_t>(RHI::Format::R32G32_Float);
|
||||
texcoord.inputSlot = 0;
|
||||
texcoord.alignedByteOffset = static_cast<uint32_t>(offsetof(Resources::StaticMeshVertex, uv0));
|
||||
inputLayout.elements.push_back(texcoord);
|
||||
|
||||
RHI::InputElementDesc backTexcoord = {};
|
||||
backTexcoord.semanticName = "TEXCOORD";
|
||||
backTexcoord.semanticIndex = 1;
|
||||
backTexcoord.format = static_cast<uint32_t>(RHI::Format::R32G32_Float);
|
||||
backTexcoord.inputSlot = 0;
|
||||
backTexcoord.alignedByteOffset = static_cast<uint32_t>(offsetof(Resources::StaticMeshVertex, uv1));
|
||||
inputLayout.elements.push_back(backTexcoord);
|
||||
|
||||
RHI::InputElementDesc tangent = {};
|
||||
tangent.semanticName = "TEXCOORD";
|
||||
tangent.semanticIndex = 2;
|
||||
tangent.format = static_cast<uint32_t>(RHI::Format::R32G32B32_Float);
|
||||
tangent.inputSlot = 0;
|
||||
tangent.alignedByteOffset = static_cast<uint32_t>(offsetof(Resources::StaticMeshVertex, tangent));
|
||||
inputLayout.elements.push_back(tangent);
|
||||
|
||||
RHI::InputElementDesc bitangent = {};
|
||||
bitangent.semanticName = "TEXCOORD";
|
||||
bitangent.semanticIndex = 3;
|
||||
bitangent.format = static_cast<uint32_t>(RHI::Format::R32G32B32_Float);
|
||||
bitangent.inputSlot = 0;
|
||||
bitangent.alignedByteOffset = static_cast<uint32_t>(offsetof(Resources::StaticMeshVertex, bitangent));
|
||||
inputLayout.elements.push_back(bitangent);
|
||||
|
||||
RHI::InputElementDesc color = {};
|
||||
color.semanticName = "COLOR";
|
||||
color.semanticIndex = 0;
|
||||
color.format = static_cast<uint32_t>(RHI::Format::R32G32B32A32_Float);
|
||||
color.inputSlot = 0;
|
||||
color.alignedByteOffset = static_cast<uint32_t>(offsetof(Resources::StaticMeshVertex, color));
|
||||
inputLayout.elements.push_back(color);
|
||||
|
||||
return inputLayout;
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::Initialize(const RenderContext& context) {
|
||||
return EnsureInitialized(context) &&
|
||||
InitializeForwardSceneFeaturePasses(context);
|
||||
}
|
||||
|
||||
void BuiltinForwardPipeline::Shutdown() {
|
||||
ShutdownForwardSceneFeaturePasses();
|
||||
DestroyPipelineResources();
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::Render(
|
||||
const FrameExecutionContext& executionContext) {
|
||||
if (!Initialize(executionContext.renderContext)) {
|
||||
Debug::Logger::Get().Error(
|
||||
Debug::LogCategory::Rendering,
|
||||
"BuiltinForwardPipeline::Render failed: Initialize returned false");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!PrepareForwardSceneFeaturePasses(executionContext)) {
|
||||
Debug::Logger::Get().Error(
|
||||
Debug::LogCategory::Rendering,
|
||||
"BuiltinForwardPipeline::Render failed: PrepareForwardSceneFeaturePasses returned false");
|
||||
return false;
|
||||
}
|
||||
|
||||
const RenderPassContext passContext = BuildRenderPassContext(executionContext);
|
||||
|
||||
if (!BeginForwardScenePass(passContext)) {
|
||||
Debug::Logger::Get().Error(
|
||||
Debug::LogCategory::Rendering,
|
||||
"BuiltinForwardPipeline::Render failed: BeginForwardScenePass returned false");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool sampledDirectionalShadow =
|
||||
ShouldSampleMainDirectionalShadowMap(executionContext.sceneData);
|
||||
if (sampledDirectionalShadow) {
|
||||
TransitionMainDirectionalShadowForSampling(
|
||||
executionContext.renderContext,
|
||||
executionContext.sceneData);
|
||||
}
|
||||
|
||||
const bool renderResult = ExecuteForwardScene(executionContext);
|
||||
|
||||
if (sampledDirectionalShadow) {
|
||||
RestoreMainDirectionalShadowAfterSampling(
|
||||
executionContext.renderContext,
|
||||
executionContext.sceneData);
|
||||
}
|
||||
EndForwardScenePass(passContext);
|
||||
|
||||
return renderResult;
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::Render(
|
||||
const RenderContext& context,
|
||||
const RenderSurface& surface,
|
||||
const RenderSceneData& sceneData) {
|
||||
return Render(FrameExecutionContext(context, surface, sceneData));
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::BeginForwardScenePass(const RenderPassContext& passContext) {
|
||||
const RenderContext& context = passContext.renderContext;
|
||||
const RenderSurface& surface = passContext.surface;
|
||||
const RenderSceneData& sceneData = passContext.sceneData;
|
||||
RHI::RHIResourceView* depthAttachment = surface.GetDepthAttachment();
|
||||
|
||||
std::vector<RHI::RHIResourceView*> renderTargets = CollectSurfaceColorAttachments(surface);
|
||||
if (renderTargets.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const Math::RectInt renderArea = surface.GetRenderArea();
|
||||
if (renderArea.width <= 0 || renderArea.height <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::RHICommandList* commandList = context.commandList;
|
||||
if (surface.IsAutoTransitionEnabled()) {
|
||||
for (RHI::RHIResourceView* renderTarget : renderTargets) {
|
||||
if (renderTarget != nullptr) {
|
||||
commandList->TransitionBarrier(
|
||||
renderTarget,
|
||||
surface.GetColorStateBefore(),
|
||||
RHI::ResourceStates::RenderTarget);
|
||||
}
|
||||
}
|
||||
|
||||
if (depthAttachment != nullptr) {
|
||||
commandList->TransitionBarrier(
|
||||
depthAttachment,
|
||||
surface.GetDepthStateBefore(),
|
||||
RHI::ResourceStates::DepthWrite);
|
||||
}
|
||||
}
|
||||
|
||||
commandList->SetRenderTargets(
|
||||
static_cast<uint32_t>(renderTargets.size()),
|
||||
renderTargets.data(),
|
||||
depthAttachment);
|
||||
|
||||
const RHI::Viewport viewport = {
|
||||
static_cast<float>(renderArea.x),
|
||||
static_cast<float>(renderArea.y),
|
||||
static_cast<float>(renderArea.width),
|
||||
static_cast<float>(renderArea.height),
|
||||
0.0f,
|
||||
1.0f
|
||||
};
|
||||
const RHI::Rect scissorRect = {
|
||||
renderArea.x,
|
||||
renderArea.y,
|
||||
renderArea.x + renderArea.width,
|
||||
renderArea.y + renderArea.height
|
||||
};
|
||||
const RHI::Rect clearRects[] = { scissorRect };
|
||||
commandList->SetViewport(viewport);
|
||||
commandList->SetScissorRect(scissorRect);
|
||||
|
||||
const Math::Color clearColor = surface.HasClearColorOverride()
|
||||
? surface.GetClearColorOverride()
|
||||
: sceneData.cameraData.clearColor;
|
||||
const float clearValues[4] = { clearColor.r, clearColor.g, clearColor.b, clearColor.a };
|
||||
if (HasRenderClearFlag(sceneData.cameraData.clearFlags, RenderClearFlags::Color) &&
|
||||
!HasSkybox(sceneData)) {
|
||||
for (RHI::RHIResourceView* renderTarget : renderTargets) {
|
||||
if (renderTarget != nullptr) {
|
||||
commandList->ClearRenderTarget(renderTarget, clearValues, 1, clearRects);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (depthAttachment != nullptr &&
|
||||
HasRenderClearFlag(sceneData.cameraData.clearFlags, RenderClearFlags::Depth)) {
|
||||
commandList->ClearDepthStencil(depthAttachment, 1.0f, 0, 1, clearRects);
|
||||
}
|
||||
|
||||
commandList->SetPrimitiveTopology(RHI::PrimitiveTopology::TriangleList);
|
||||
return true;
|
||||
}
|
||||
|
||||
void BuiltinForwardPipeline::EndForwardScenePass(const RenderPassContext& passContext) {
|
||||
const RenderContext& context = passContext.renderContext;
|
||||
const RenderSurface& surface = passContext.surface;
|
||||
RHI::RHIResourceView* depthAttachment = surface.GetDepthAttachment();
|
||||
std::vector<RHI::RHIResourceView*> renderTargets = CollectSurfaceColorAttachments(surface);
|
||||
RHI::RHICommandList* commandList = context.commandList;
|
||||
|
||||
commandList->EndRenderPass();
|
||||
if (!surface.IsAutoTransitionEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (RHI::RHIResourceView* renderTarget : renderTargets) {
|
||||
if (renderTarget != nullptr) {
|
||||
commandList->TransitionBarrier(
|
||||
renderTarget,
|
||||
RHI::ResourceStates::RenderTarget,
|
||||
surface.GetColorStateAfter());
|
||||
}
|
||||
}
|
||||
|
||||
if (depthAttachment != nullptr) {
|
||||
commandList->TransitionBarrier(
|
||||
depthAttachment,
|
||||
RHI::ResourceStates::DepthWrite,
|
||||
surface.GetDepthStateAfter());
|
||||
}
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::ExecuteForwardOpaquePass(
|
||||
const ScenePhaseExecutionContext& executionContext) {
|
||||
return DrawVisibleItems(
|
||||
executionContext.frameContext,
|
||||
BuildDrawSettings(executionContext.scenePhase));
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::ExecuteForwardTransparentPass(
|
||||
const ScenePhaseExecutionContext& executionContext) {
|
||||
return DrawVisibleItems(
|
||||
executionContext.frameContext,
|
||||
BuildDrawSettings(executionContext.scenePhase));
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::EnsureInitialized(const RenderContext& context) {
|
||||
if (!context.IsValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_initialized &&
|
||||
m_device == context.device &&
|
||||
m_backendType == context.backendType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
DestroyPipelineResources();
|
||||
m_device = context.device;
|
||||
m_backendType = context.backendType;
|
||||
m_initialized = CreatePipelineResources(context);
|
||||
return m_initialized;
|
||||
}
|
||||
|
||||
BuiltinForwardPipeline::ForwardSceneFeaturePassArray
|
||||
BuiltinForwardPipeline::CollectForwardSceneFeaturePasses() const {
|
||||
return {
|
||||
m_gaussianSplatPass.get(),
|
||||
m_volumetricPass.get()
|
||||
};
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::InitializeForwardSceneFeaturePasses(const RenderContext& context) {
|
||||
for (SceneRenderFeaturePass* featurePass : CollectForwardSceneFeaturePasses()) {
|
||||
if (featurePass == nullptr || !featurePass->Initialize(context)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BuiltinForwardPipeline::ShutdownForwardSceneFeaturePasses() {
|
||||
for (SceneRenderFeaturePass* featurePass : CollectForwardSceneFeaturePasses()) {
|
||||
if (featurePass != nullptr) {
|
||||
featurePass->Shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::PrepareForwardSceneFeaturePasses(
|
||||
const FrameExecutionContext& executionContext) const {
|
||||
for (SceneRenderFeaturePass* featurePass : CollectForwardSceneFeaturePasses()) {
|
||||
if (featurePass == nullptr || !featurePass->IsActive(executionContext.sceneData)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!featurePass->Prepare(
|
||||
executionContext.renderContext,
|
||||
executionContext.sceneData)) {
|
||||
Debug::Logger::Get().Error(
|
||||
Debug::LogCategory::Rendering,
|
||||
(Containers::String("BuiltinForwardPipeline feature prepare failed: ") +
|
||||
featurePass->GetName()).CStr());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::ExecuteForwardSceneFeaturePasses(
|
||||
const ScenePhaseExecutionContext& executionContext) const {
|
||||
const RenderPassContext passContext = BuildRenderPassContext(executionContext);
|
||||
|
||||
for (SceneRenderFeaturePass* featurePass : CollectForwardSceneFeaturePasses()) {
|
||||
if (featurePass == nullptr ||
|
||||
!featurePass->IsActive(executionContext.frameContext.sceneData)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!featurePass->Execute(passContext)) {
|
||||
Debug::Logger::Get().Error(
|
||||
Debug::LogCategory::Rendering,
|
||||
(Containers::String("BuiltinForwardPipeline feature execute failed: ") +
|
||||
featurePass->GetName()).CStr());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ScenePhaseExecutionContext BuiltinForwardPipeline::BuildScenePhaseExecutionContext(
|
||||
const FrameExecutionContext& executionContext,
|
||||
ScenePhase scenePhase) const {
|
||||
return ScenePhaseExecutionContext(
|
||||
executionContext,
|
||||
scenePhase,
|
||||
ShouldSampleMainDirectionalShadowMap(executionContext.sceneData));
|
||||
}
|
||||
|
||||
DrawSettings BuiltinForwardPipeline::BuildDrawSettings(ScenePhase scenePhase) const {
|
||||
DrawSettings drawSettings = {};
|
||||
drawSettings.scenePhase = scenePhase;
|
||||
switch (scenePhase) {
|
||||
case ScenePhase::Opaque:
|
||||
drawSettings.rendererListType = RendererListType::Opaque;
|
||||
break;
|
||||
case ScenePhase::Transparent:
|
||||
drawSettings.rendererListType = RendererListType::Transparent;
|
||||
break;
|
||||
default:
|
||||
drawSettings.rendererListType = RendererListType::AllVisible;
|
||||
break;
|
||||
}
|
||||
|
||||
return drawSettings;
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::ExecuteScenePhase(
|
||||
const ScenePhaseExecutionContext& executionContext) {
|
||||
switch (executionContext.scenePhase) {
|
||||
case ScenePhase::Opaque:
|
||||
return ExecuteForwardOpaquePass(executionContext);
|
||||
case ScenePhase::Skybox:
|
||||
return ExecuteForwardSkyboxPass(BuildRenderPassContext(executionContext));
|
||||
case ScenePhase::Feature:
|
||||
return ExecuteForwardSceneFeaturePasses(executionContext);
|
||||
case ScenePhase::Transparent:
|
||||
return ExecuteForwardTransparentPass(executionContext);
|
||||
default:
|
||||
Debug::Logger::Get().Error(
|
||||
Debug::LogCategory::Rendering,
|
||||
(Containers::String("BuiltinForwardPipeline::ExecuteScenePhase does not support scene phase: ") +
|
||||
ToString(executionContext.scenePhase)).CStr());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::ExecuteForwardScene(
|
||||
const FrameExecutionContext& executionContext) {
|
||||
static constexpr ScenePhase kForwardScenePhases[] = {
|
||||
ScenePhase::Opaque,
|
||||
ScenePhase::Skybox,
|
||||
ScenePhase::Feature,
|
||||
ScenePhase::Transparent
|
||||
};
|
||||
|
||||
for (ScenePhase scenePhase : kForwardScenePhases) {
|
||||
const ScenePhaseExecutionContext scenePhaseExecutionContext =
|
||||
BuildScenePhaseExecutionContext(executionContext, scenePhase);
|
||||
if (!ExecuteScenePhase(scenePhaseExecutionContext)) {
|
||||
Debug::Logger::Get().Error(
|
||||
Debug::LogCategory::Rendering,
|
||||
(Containers::String("BuiltinForwardPipeline::ExecuteForwardScene failed during phase: ") +
|
||||
ToString(scenePhase)).CStr());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& context) {
|
||||
m_builtinForwardShader = Resources::ResourceManager::Get().Load<Resources::Shader>(
|
||||
Resources::GetBuiltinForwardLitShaderPath());
|
||||
if (!m_builtinForwardShader.IsValid()) {
|
||||
Debug::Logger::Get().Error(
|
||||
Debug::LogCategory::Rendering,
|
||||
"BuiltinForwardPipeline failed to load builtin forward shader resource");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_builtinUnlitShader = Resources::ResourceManager::Get().Load<Resources::Shader>(
|
||||
Resources::GetBuiltinUnlitShaderPath());
|
||||
if (!m_builtinUnlitShader.IsValid()) {
|
||||
Debug::Logger::Get().Error(
|
||||
Debug::LogCategory::Rendering,
|
||||
"BuiltinForwardPipeline failed to load builtin unlit shader resource");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_builtinSkyboxShader = Resources::ResourceManager::Get().Load<Resources::Shader>(
|
||||
Resources::GetBuiltinSkyboxShaderPath());
|
||||
if (!m_builtinSkyboxShader.IsValid()) {
|
||||
Debug::Logger::Get().Error(
|
||||
Debug::LogCategory::Rendering,
|
||||
"BuiltinForwardPipeline failed to load builtin skybox shader resource");
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::SamplerDesc samplerDesc = {};
|
||||
samplerDesc.filter = static_cast<uint32_t>(RHI::FilterMode::Linear);
|
||||
samplerDesc.addressU = static_cast<uint32_t>(RHI::TextureAddressMode::Clamp);
|
||||
samplerDesc.addressV = static_cast<uint32_t>(RHI::TextureAddressMode::Clamp);
|
||||
samplerDesc.addressW = static_cast<uint32_t>(RHI::TextureAddressMode::Clamp);
|
||||
samplerDesc.mipLodBias = 0.0f;
|
||||
samplerDesc.maxAnisotropy = 1;
|
||||
samplerDesc.comparisonFunc = static_cast<uint32_t>(RHI::ComparisonFunc::Always);
|
||||
samplerDesc.minLod = 0.0f;
|
||||
samplerDesc.maxLod = 1000.0f;
|
||||
m_sampler = context.device->CreateSampler(samplerDesc);
|
||||
if (m_sampler == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::SamplerDesc shadowSamplerDesc = samplerDesc;
|
||||
shadowSamplerDesc.filter = static_cast<uint32_t>(RHI::FilterMode::Point);
|
||||
m_shadowSampler = context.device->CreateSampler(shadowSamplerDesc);
|
||||
if (m_shadowSampler == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const unsigned char whitePixels2D[4] = {
|
||||
255, 255, 255, 255
|
||||
};
|
||||
RHI::TextureDesc fallback2DDesc = {};
|
||||
fallback2DDesc.width = 1;
|
||||
fallback2DDesc.height = 1;
|
||||
fallback2DDesc.depth = 1;
|
||||
fallback2DDesc.mipLevels = 1;
|
||||
fallback2DDesc.arraySize = 1;
|
||||
fallback2DDesc.format = static_cast<uint32_t>(RHI::Format::R8G8B8A8_UNorm);
|
||||
fallback2DDesc.textureType = static_cast<uint32_t>(RHI::TextureType::Texture2D);
|
||||
fallback2DDesc.sampleCount = 1;
|
||||
fallback2DDesc.sampleQuality = 0;
|
||||
fallback2DDesc.flags = 0;
|
||||
m_fallbackTexture2D = context.device->CreateTexture(
|
||||
fallback2DDesc,
|
||||
whitePixels2D,
|
||||
sizeof(whitePixels2D),
|
||||
sizeof(whitePixels2D));
|
||||
if (m_fallbackTexture2D == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::ResourceViewDesc fallback2DViewDesc = {};
|
||||
fallback2DViewDesc.format = static_cast<uint32_t>(RHI::Format::R8G8B8A8_UNorm);
|
||||
fallback2DViewDesc.dimension = RHI::ResourceViewDimension::Texture2D;
|
||||
fallback2DViewDesc.mipLevel = 0;
|
||||
m_fallbackTexture2DView = context.device->CreateShaderResourceView(m_fallbackTexture2D, fallback2DViewDesc);
|
||||
if (m_fallbackTexture2DView == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const unsigned char whitePixelsCube[6 * 4] = {
|
||||
255, 255, 255, 255,
|
||||
255, 255, 255, 255,
|
||||
255, 255, 255, 255,
|
||||
255, 255, 255, 255,
|
||||
255, 255, 255, 255,
|
||||
255, 255, 255, 255
|
||||
};
|
||||
RHI::TextureDesc fallbackCubeDesc = {};
|
||||
fallbackCubeDesc.width = 1;
|
||||
fallbackCubeDesc.height = 1;
|
||||
fallbackCubeDesc.depth = 1;
|
||||
fallbackCubeDesc.mipLevels = 1;
|
||||
fallbackCubeDesc.arraySize = 6;
|
||||
fallbackCubeDesc.format = static_cast<uint32_t>(RHI::Format::R8G8B8A8_UNorm);
|
||||
fallbackCubeDesc.textureType = static_cast<uint32_t>(RHI::TextureType::TextureCube);
|
||||
fallbackCubeDesc.sampleCount = 1;
|
||||
fallbackCubeDesc.sampleQuality = 0;
|
||||
fallbackCubeDesc.flags = 0;
|
||||
m_fallbackTextureCube = context.device->CreateTexture(
|
||||
fallbackCubeDesc,
|
||||
whitePixelsCube,
|
||||
sizeof(whitePixelsCube),
|
||||
4);
|
||||
if (m_fallbackTextureCube == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::ResourceViewDesc fallbackCubeViewDesc = {};
|
||||
fallbackCubeViewDesc.format = static_cast<uint32_t>(RHI::Format::R8G8B8A8_UNorm);
|
||||
fallbackCubeViewDesc.dimension = RHI::ResourceViewDimension::TextureCube;
|
||||
fallbackCubeViewDesc.mipLevel = 0;
|
||||
m_fallbackTextureCubeView = context.device->CreateShaderResourceView(m_fallbackTextureCube, fallbackCubeViewDesc);
|
||||
if (m_fallbackTextureCubeView == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BuiltinForwardPipeline::DestroyPipelineResources() {
|
||||
m_resourceCache.Shutdown();
|
||||
|
||||
for (auto& pipelinePair : m_pipelineStates) {
|
||||
if (pipelinePair.second != nullptr) {
|
||||
pipelinePair.second->Shutdown();
|
||||
delete pipelinePair.second;
|
||||
}
|
||||
}
|
||||
m_pipelineStates.clear();
|
||||
|
||||
for (auto& descriptorSetPair : m_dynamicDescriptorSets) {
|
||||
DestroyOwnedDescriptorSet(descriptorSetPair.second.descriptorSet);
|
||||
}
|
||||
m_dynamicDescriptorSets.clear();
|
||||
|
||||
for (auto& passLayoutPair : m_passResourceLayouts) {
|
||||
DestroyPassResourceLayout(passLayoutPair.second);
|
||||
}
|
||||
m_passResourceLayouts.clear();
|
||||
|
||||
DestroySkyboxResources();
|
||||
|
||||
if (m_fallbackTextureCubeView != nullptr) {
|
||||
m_fallbackTextureCubeView->Shutdown();
|
||||
delete m_fallbackTextureCubeView;
|
||||
m_fallbackTextureCubeView = nullptr;
|
||||
}
|
||||
|
||||
if (m_fallbackTextureCube != nullptr) {
|
||||
m_fallbackTextureCube->Shutdown();
|
||||
delete m_fallbackTextureCube;
|
||||
m_fallbackTextureCube = nullptr;
|
||||
}
|
||||
|
||||
if (m_fallbackTexture2DView != nullptr) {
|
||||
m_fallbackTexture2DView->Shutdown();
|
||||
delete m_fallbackTexture2DView;
|
||||
m_fallbackTexture2DView = nullptr;
|
||||
}
|
||||
|
||||
if (m_fallbackTexture2D != nullptr) {
|
||||
m_fallbackTexture2D->Shutdown();
|
||||
delete m_fallbackTexture2D;
|
||||
m_fallbackTexture2D = nullptr;
|
||||
}
|
||||
|
||||
if (m_sampler != nullptr) {
|
||||
m_sampler->Shutdown();
|
||||
delete m_sampler;
|
||||
m_sampler = nullptr;
|
||||
}
|
||||
|
||||
if (m_shadowSampler != nullptr) {
|
||||
m_shadowSampler->Shutdown();
|
||||
delete m_shadowSampler;
|
||||
m_shadowSampler = nullptr;
|
||||
}
|
||||
|
||||
m_device = nullptr;
|
||||
m_initialized = false;
|
||||
m_builtinForwardShader.Reset();
|
||||
m_builtinUnlitShader.Reset();
|
||||
m_builtinSkyboxShader.Reset();
|
||||
}
|
||||
|
||||
} // namespace Pipelines
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
|
||||
Reference in New Issue
Block a user