Extract builtin forward main-scene graph builder

This commit is contained in:
2026-04-15 01:07:59 +08:00
parent f6fb396a41
commit 65b3078c7f
5 changed files with 262 additions and 206 deletions

View File

@@ -1,15 +1,10 @@
#include "Rendering/Pipelines/BuiltinForwardPipeline.h"
#include "Debug/Logger.h"
#include "Rendering/Graph/RenderGraph.h"
#include "Rendering/Internal/RenderPassGraphUtils.h"
#include "Rendering/Pipelines/Internal/BuiltinForwardSceneSetup.h"
#include "RHI/RHICommandList.h"
#include "Rendering/Pipelines/Internal/BuiltinForwardMainSceneGraphBuilder.h"
#include "Rendering/Internal/RenderSurfacePipelineUtils.h"
#include "Rendering/RenderSurface.h"
#include <memory>
#include <string>
#include "RHI/RHICommandList.h"
namespace XCEngine {
namespace Rendering {
@@ -56,30 +51,6 @@ std::vector<RHI::RHIResourceView*> CollectSurfaceColorAttachments(const RenderSu
return renderTargets;
}
RenderSurface BuildGraphManagedForwardSceneSurface(const RenderSurface& templateSurface) {
RenderSurface surface = templateSurface;
surface.SetAutoTransitionEnabled(false);
return surface;
}
struct ForwardSceneGraphExecutionState {
bool initialized = false;
};
Containers::String BuildForwardScenePhasePassName(
const Containers::String& baseName,
ScenePhase scenePhase) {
std::string name = baseName.CStr();
name += '.';
name += ToString(scenePhase);
return Containers::String(name.c_str());
}
bool ScenePhaseSamplesMainDirectionalShadow(ScenePhase scenePhase) {
return scenePhase == ScenePhase::Opaque ||
scenePhase == ScenePhase::Transparent;
}
} // namespace
bool BuiltinForwardPipeline::ShouldSampleMainDirectionalShadowMap(const RenderSceneData& sceneData) {
@@ -94,181 +65,7 @@ bool BuiltinForwardPipeline::SupportsMainSceneRenderGraph() const {
bool BuiltinForwardPipeline::RecordMainSceneRenderGraph(
const RenderPipelineMainSceneRenderGraphContext& context) {
const Containers::String passName = context.passName;
const RenderContext renderContext = context.renderContext;
const std::shared_ptr<const RenderSceneData> sceneData =
std::make_shared<RenderSceneData>(context.sceneData);
const RenderSurface surfaceTemplate =
BuildGraphManagedForwardSceneSurface(context.surfaceTemplate);
const bool hasSourceSurface = context.sourceSurface != nullptr;
const RenderSurface sourceSurface =
hasSourceSurface ? *context.sourceSurface : RenderSurface();
RHI::RHIResourceView* const sourceColorView = context.sourceColorView;
const RHI::ResourceStates sourceColorState = context.sourceColorState;
const std::vector<RenderGraphTextureHandle> colorTargets = context.colorTargets;
const RenderGraphTextureHandle depthTarget = context.depthTarget;
const CameraFrameRenderGraphResources* const frameResources =
TryGetCameraFrameRenderGraphResources(context.blackboard);
const RenderGraphTextureHandle mainDirectionalShadowTexture =
frameResources != nullptr
? frameResources->mainDirectionalShadow
: RenderGraphTextureHandle{};
bool* const executionSucceeded = context.executionSucceeded;
const std::shared_ptr<ForwardSceneGraphExecutionState> graphExecutionState =
std::make_shared<ForwardSceneGraphExecutionState>();
const SceneRenderFeaturePassBeginCallback beginRecordedPass =
[this,
graphExecutionState,
renderContext,
sceneData,
hasSourceSurface,
sourceSurface,
sourceColorView,
sourceColorState,
executionSucceeded](
const RenderPassContext& passContext,
bool clearAttachments) -> bool {
if (executionSucceeded != nullptr && !(*executionSucceeded)) {
return false;
}
if (!graphExecutionState->initialized) {
if (!Initialize(renderContext)) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline::RecordMainSceneRenderGraph failed during execution: Initialize returned false");
if (executionSucceeded != nullptr) {
*executionSucceeded = false;
}
return false;
}
const FrameExecutionContext executionContext(
renderContext,
passContext.surface,
*sceneData,
hasSourceSurface ? &sourceSurface : nullptr,
sourceColorView,
sourceColorState);
if (!m_forwardSceneFeatureHost.Prepare(executionContext)) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline::RecordMainSceneRenderGraph failed during execution: SceneRenderFeatureHost::Prepare returned false");
if (executionSucceeded != nullptr) {
*executionSucceeded = false;
}
return false;
}
graphExecutionState->initialized = true;
}
if (!BeginForwardScenePass(passContext, clearAttachments)) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline::RecordMainSceneRenderGraph failed during execution: BeginForwardScenePass returned false");
if (executionSucceeded != nullptr) {
*executionSucceeded = false;
}
return false;
}
return true;
};
const SceneRenderFeaturePassEndCallback endRecordedPass =
[this](const RenderPassContext& passContext) {
EndForwardScenePass(passContext);
};
bool clearAttachments = true;
for (const Pipelines::Internal::ForwardSceneStep& step : Pipelines::Internal::GetBuiltinForwardSceneSteps()) {
if (step.type == Pipelines::Internal::ForwardSceneStepType::InjectionPoint) {
const SceneRenderFeaturePassRenderGraphContext featureContext = {
context.graphBuilder,
passName,
renderContext,
*sceneData,
surfaceTemplate,
hasSourceSurface ? &sourceSurface : nullptr,
sourceColorView,
sourceColorState,
{},
colorTargets,
depthTarget,
clearAttachments,
executionSucceeded,
beginRecordedPass,
endRecordedPass,
context.blackboard
};
bool recordedAnyPass = false;
if (!m_forwardSceneFeatureHost.Record(
featureContext,
step.injectionPoint,
&recordedAnyPass)) {
return false;
}
if (recordedAnyPass) {
clearAttachments = false;
}
continue;
}
const Containers::String phasePassName =
BuildForwardScenePhasePassName(passName, step.scenePhase);
const RenderPassGraphBeginCallback beginPhasePass =
[beginRecordedPass, clearAttachments](const RenderPassContext& passContext) {
return beginRecordedPass(passContext, clearAttachments);
};
const std::vector<RenderGraphTextureHandle> additionalReadTextures =
ScenePhaseSamplesMainDirectionalShadow(step.scenePhase) &&
mainDirectionalShadowTexture.IsValid()
? std::vector<RenderGraphTextureHandle>{ mainDirectionalShadowTexture }
: std::vector<RenderGraphTextureHandle>{};
const RenderPassRenderGraphContext phaseContext = {
context.graphBuilder,
phasePassName,
renderContext,
*sceneData,
surfaceTemplate,
hasSourceSurface ? &sourceSurface : nullptr,
sourceColorView,
sourceColorState,
{},
colorTargets,
depthTarget,
executionSucceeded,
beginPhasePass,
endRecordedPass,
context.blackboard
};
if (!::XCEngine::Rendering::Internal::RecordCallbackRasterRenderPass(
phaseContext,
{
false,
true,
depthTarget.IsValid()
},
[this, scenePhase = step.scenePhase](const RenderPassContext& passContext) {
const FrameExecutionContext executionContext(
passContext.renderContext,
passContext.surface,
passContext.sceneData,
passContext.sourceSurface,
passContext.sourceColorView,
passContext.sourceColorState);
const ScenePhaseExecutionContext scenePhaseExecutionContext =
BuildScenePhaseExecutionContext(executionContext, scenePhase);
return ExecuteBuiltinScenePhase(scenePhaseExecutionContext);
},
additionalReadTextures)) {
return false;
}
clearAttachments = false;
}
return true;
return Internal::BuiltinForwardMainSceneGraphBuilder::Record(*this, context);
}
bool BuiltinForwardPipeline::Render(