refactor(rendering): add override helpers for shared recording contexts

This commit is contained in:
2026-04-15 13:01:04 +08:00
parent 8aee665d32
commit ea2e44976a
5 changed files with 128 additions and 34 deletions

View File

@@ -22,24 +22,53 @@ struct RenderGraphRecordingContext {
RenderGraphBlackboard* blackboard = nullptr;
};
struct RenderGraphRecordingContextOverrides {
const Containers::String* passName = nullptr;
const RenderSurface* surface = nullptr;
bool overrideSourceSurface = false;
const RenderSurface* sourceSurface = nullptr;
bool overrideSourceColorView = false;
RHI::RHIResourceView* sourceColorView = nullptr;
bool overrideSourceColorState = false;
RHI::ResourceStates sourceColorState = RHI::ResourceStates::Common;
bool overrideSourceColorTexture = false;
RenderGraphTextureHandle sourceColorTexture = {};
bool overrideColorTargets = false;
std::vector<RenderGraphTextureHandle> colorTargets = {};
bool overrideDepthTarget = false;
RenderGraphTextureHandle depthTarget = {};
};
inline RenderGraphRecordingContext CloneRenderGraphRecordingContext(
const RenderGraphRecordingContext& context,
const Containers::String& passName) {
return {
const RenderGraphRecordingContextOverrides& overrides) {
RenderGraphRecordingContext clone = {
context.graphBuilder,
passName,
overrides.passName != nullptr ? *overrides.passName : context.passName,
context.renderContext,
context.sceneData,
context.surface,
context.sourceSurface,
context.sourceColorView,
context.sourceColorState,
context.sourceColorTexture,
context.colorTargets,
context.depthTarget,
overrides.surface != nullptr ? *overrides.surface : context.surface,
overrides.overrideSourceSurface ? overrides.sourceSurface : context.sourceSurface,
overrides.overrideSourceColorView ? overrides.sourceColorView : context.sourceColorView,
overrides.overrideSourceColorState ? overrides.sourceColorState : context.sourceColorState,
overrides.overrideSourceColorTexture ? overrides.sourceColorTexture : context.sourceColorTexture,
overrides.overrideColorTargets ? overrides.colorTargets : context.colorTargets,
overrides.overrideDepthTarget ? overrides.depthTarget : context.depthTarget,
context.executionSucceeded,
context.blackboard
};
return clone;
}
inline RenderGraphRecordingContext CloneRenderGraphRecordingContext(
const RenderGraphRecordingContext& context,
const Containers::String& passName) {
const RenderGraphRecordingContextOverrides overrides = {
&passName
};
return CloneRenderGraphRecordingContext(
context,
overrides);
}
inline RenderGraphRecordingContext BuildRenderGraphRecordingContext(

View File

@@ -384,17 +384,19 @@ RenderGraphRecordingContext BuildCameraFrameStageGraphRecordingContext(
const CameraFrameRenderGraphSourceBinding& sourceBinding,
std::vector<RenderGraphTextureHandle> colorTargets,
RenderGraphTextureHandle depthTarget) {
RenderGraphRecordingContext recordingContext =
CloneRenderGraphRecordingContext(
BuildCameraFrameStageGraphRecordingContext(
context,
stageState,
sceneData,
sourceBinding),
passName);
recordingContext.colorTargets = std::move(colorTargets);
recordingContext.depthTarget = depthTarget;
return recordingContext;
RenderGraphRecordingContextOverrides overrides = {};
overrides.passName = &passName;
overrides.overrideColorTargets = true;
overrides.colorTargets = std::move(colorTargets);
overrides.overrideDepthTarget = true;
overrides.depthTarget = depthTarget;
return CloneRenderGraphRecordingContext(
BuildCameraFrameStageGraphRecordingContext(
context,
stageState,
sceneData,
sourceBinding),
overrides);
}
RenderGraphRecordingContext BuildCameraFrameStageGraphRecordingContext(

View File

@@ -37,15 +37,19 @@ bool ScenePhaseSamplesMainDirectionalShadow(ScenePhase scenePhase) {
bool BuiltinForwardMainSceneGraphBuilder::Record(
BuiltinForwardPipeline& pipeline,
const RenderPipelineMainSceneRenderGraphContext& context) {
RenderGraphRecordingContext recordingContext =
BuildRenderGraphRecordingContext(context);
recordingContext.surface =
const RenderSurface graphManagedSurface =
BuildGraphManagedForwardSceneSurface(context.surfaceTemplate);
const bool hasSourceSurface = recordingContext.sourceSurface != nullptr;
const bool hasSourceSurface = context.sourceSurface != nullptr;
const RenderSurface sourceSurface =
hasSourceSurface ? *recordingContext.sourceSurface : RenderSurface();
recordingContext.sourceSurface = hasSourceSurface ? &sourceSurface : nullptr;
hasSourceSurface ? *context.sourceSurface : RenderSurface();
RenderGraphRecordingContextOverrides overrides = {};
overrides.surface = &graphManagedSurface;
overrides.overrideSourceSurface = true;
overrides.sourceSurface = hasSourceSurface ? &sourceSurface : nullptr;
RenderGraphRecordingContext recordingContext =
CloneRenderGraphRecordingContext(
BuildRenderGraphRecordingContext(context),
overrides);
const CameraFrameRenderGraphResources* const frameResources =
TryGetCameraFrameRenderGraphResources(recordingContext.blackboard);

View File

@@ -18,15 +18,21 @@ bool SceneRenderFeaturePass::RecordRenderGraph(
[](RenderGraphTextureHandle handle) {
return handle.IsValid();
});
RenderGraphRecordingContext recordingContext =
const RenderGraphRecordingContext baseRecordingContext =
BuildRenderGraphRecordingContext(context);
const RenderSurface sourceSurfaceTemplate =
recordingContext.sourceSurface != nullptr
? *recordingContext.sourceSurface
baseRecordingContext.sourceSurface != nullptr
? *baseRecordingContext.sourceSurface
: (usesSourceColor ? context.surface : RenderSurface());
if (recordingContext.sourceSurface == nullptr && usesSourceColor) {
recordingContext.sourceSurface = &sourceSurfaceTemplate;
}
RenderGraphRecordingContextOverrides overrides = {};
overrides.overrideSourceSurface =
baseRecordingContext.sourceSurface == nullptr && usesSourceColor;
overrides.sourceSurface =
overrides.overrideSourceSurface ? &sourceSurfaceTemplate : nullptr;
const RenderGraphRecordingContext recordingContext =
CloneRenderGraphRecordingContext(
baseRecordingContext,
overrides);
const RenderPassGraphBeginCallback beginPassCallback =
context.beginPassCallback

View File

@@ -225,6 +225,59 @@ TEST(RenderGraphRecordingContext_Test, BuildsPipelineMainSceneContextFromSharedR
EXPECT_EQ(pipelineContext.blackboard, &blackboard);
}
TEST(RenderGraphRecordingContext_Test, CloneOverridesSelectedRecordingContextFieldsOnly) {
RenderGraph graph = {};
RenderGraphBuilder builder(graph);
RenderGraphBlackboard blackboard = {};
RenderContext renderContext = {};
RenderSceneData sceneData = {};
RenderSurface surface(800u, 600u);
RenderSurface sourceSurface(400u, 300u);
bool executionSucceeded = false;
const RenderGraphRecordingContext commonContext =
BuildCommonContext(
builder,
blackboard,
renderContext,
sceneData,
surface,
sourceSurface,
executionSucceeded);
const XCEngine::Containers::String overriddenPassName("Recording.Override");
RenderSurface overriddenSurface(1024u, 576u);
RenderGraphRecordingContextOverrides overrides = {};
overrides.passName = &overriddenPassName;
overrides.surface = &overriddenSurface;
overrides.overrideSourceSurface = true;
overrides.sourceSurface = nullptr;
overrides.overrideColorTargets = true;
overrides.colorTargets = { { 10u } };
overrides.overrideDepthTarget = true;
overrides.depthTarget = { 11u };
const RenderGraphRecordingContext overriddenContext =
CloneRenderGraphRecordingContext(
commonContext,
overrides);
EXPECT_EQ(overriddenContext.passName, overriddenPassName);
EXPECT_EQ(overriddenContext.surface.GetWidth(), 1024u);
EXPECT_EQ(overriddenContext.surface.GetHeight(), 576u);
EXPECT_EQ(overriddenContext.sourceSurface, nullptr);
EXPECT_EQ(
overriddenContext.sourceColorView,
reinterpret_cast<XCEngine::RHI::RHIResourceView*>(0x1234));
EXPECT_EQ(
overriddenContext.sourceColorState,
XCEngine::RHI::ResourceStates::PixelShaderResource);
ASSERT_EQ(overriddenContext.colorTargets.size(), 1u);
EXPECT_EQ(overriddenContext.colorTargets[0].index, 10u);
EXPECT_EQ(overriddenContext.depthTarget.index, 11u);
EXPECT_EQ(overriddenContext.executionSucceeded, &executionSucceeded);
EXPECT_EQ(overriddenContext.blackboard, &blackboard);
}
TEST(RenderGraphRecordingContext_Test, BuildsSharedRecordingDataFromPipelineMainSceneContext) {
RenderGraph graph = {};
RenderGraphBuilder builder(graph);