Unify object id rendering with render passes

This commit is contained in:
2026-04-05 18:20:19 +08:00
parent a600e73fb2
commit 8ced88e847
7 changed files with 72 additions and 64 deletions

View File

@@ -8,6 +8,8 @@
#include "RHI/RHIPipelineState.h"
#include "Rendering/Detail/ShaderVariantUtils.h"
#include "Rendering/RenderMaterialUtility.h"
#include "Rendering/RenderSceneExtractor.h"
#include "Rendering/RenderSurface.h"
#include "Resources/BuiltinResources.h"
#include "Resources/Mesh/Mesh.h"
@@ -95,6 +97,10 @@ BuiltinObjectIdPass::~BuiltinObjectIdPass() {
Shutdown();
}
const char* BuiltinObjectIdPass::GetName() const {
return "BuiltinObjectIdPass";
}
RHI::InputLayoutDesc BuiltinObjectIdPass::BuildInputLayout() {
RHI::InputLayoutDesc inputLayout = {};
@@ -125,39 +131,42 @@ RHI::InputLayoutDesc BuiltinObjectIdPass::BuildInputLayout() {
return inputLayout;
}
bool BuiltinObjectIdPass::Render(
const RenderContext& context,
const RenderSurface& surface,
const RenderSceneData& sceneData) {
if (!context.IsValid()) {
bool BuiltinObjectIdPass::Initialize(const RenderContext& context) {
return EnsureInitialized(context);
}
bool BuiltinObjectIdPass::Execute(const RenderPassContext& context) {
if (!context.renderContext.IsValid()) {
return false;
}
const std::vector<RHI::RHIResourceView*>& colorAttachments = surface.GetColorAttachments();
if (colorAttachments.empty() || colorAttachments[0] == nullptr || surface.GetDepthAttachment() == nullptr) {
const std::vector<RHI::RHIResourceView*>& colorAttachments = context.surface.GetColorAttachments();
if (colorAttachments.empty() ||
colorAttachments[0] == nullptr ||
context.surface.GetDepthAttachment() == nullptr) {
return false;
}
const Math::RectInt renderArea = surface.GetRenderArea();
const Math::RectInt renderArea = context.surface.GetRenderArea();
if (renderArea.width <= 0 || renderArea.height <= 0) {
return false;
}
if (!EnsureInitialized(context)) {
if (!EnsureInitialized(context.renderContext)) {
return false;
}
RHI::RHICommandList* commandList = context.commandList;
RHI::RHICommandList* commandList = context.renderContext.commandList;
RHI::RHIResourceView* renderTarget = colorAttachments[0];
if (surface.IsAutoTransitionEnabled()) {
if (context.surface.IsAutoTransitionEnabled()) {
commandList->TransitionBarrier(
renderTarget,
surface.GetColorStateBefore(),
context.surface.GetColorStateBefore(),
RHI::ResourceStates::RenderTarget);
}
commandList->SetRenderTargets(1, &renderTarget, surface.GetDepthAttachment());
commandList->SetRenderTargets(1, &renderTarget, context.surface.GetDepthAttachment());
const RHI::Viewport viewport = {
static_cast<float>(renderArea.x),
@@ -179,21 +188,21 @@ bool BuiltinObjectIdPass::Render(
commandList->SetViewport(viewport);
commandList->SetScissorRect(scissorRect);
commandList->ClearRenderTarget(renderTarget, clearColor, 1, clearRects);
commandList->ClearDepthStencil(surface.GetDepthAttachment(), 1.0f, 0, 1, clearRects);
commandList->ClearDepthStencil(context.surface.GetDepthAttachment(), 1.0f, 0, 1, clearRects);
commandList->SetPrimitiveTopology(RHI::PrimitiveTopology::TriangleList);
commandList->SetPipelineState(m_pipelineState);
for (const VisibleRenderItem& visibleItem : sceneData.visibleItems) {
DrawVisibleItem(context, sceneData, visibleItem);
for (const VisibleRenderItem& visibleItem : context.sceneData.visibleItems) {
DrawVisibleItem(context.renderContext, context.sceneData, visibleItem);
}
commandList->EndRenderPass();
if (surface.IsAutoTransitionEnabled()) {
if (context.surface.IsAutoTransitionEnabled()) {
commandList->TransitionBarrier(
renderTarget,
RHI::ResourceStates::RenderTarget,
surface.GetColorStateAfter());
context.surface.GetColorStateAfter());
}
return true;