refactor: route builtin object-id pass through shader assets

This commit is contained in:
2026-04-02 19:17:22 +08:00
parent 9f7d8fd68d
commit 11fb8f3585
8 changed files with 390 additions and 161 deletions

View File

@@ -1,13 +1,17 @@
#include "Rendering/Passes/BuiltinObjectIdPass.h"
#include "Components/GameObject.h"
#include "Core/Asset/ResourceManager.h"
#include "Debug/Logger.h"
#include "RHI/RHICommandList.h"
#include "RHI/RHIPipelineLayout.h"
#include "RHI/RHIPipelineState.h"
#include "Rendering/Detail/ShaderVariantUtils.h"
#include "Rendering/RenderMaterialUtility.h"
#include "Resources/BuiltinResources.h"
#include "Resources/Mesh/Mesh.h"
#include <cstddef>
#include <cstring>
namespace XCEngine {
namespace Rendering {
@@ -15,83 +19,41 @@ namespace Passes {
namespace {
const char kBuiltinObjectIdHlsl[] = R"(
cbuffer PerObjectConstants : register(b0) {
float4x4 gProjectionMatrix;
float4x4 gViewMatrix;
float4x4 gModelMatrix;
float4 gObjectIdColor;
};
const Resources::ShaderPass* FindObjectIdCompatiblePass(
const Resources::Shader& shader,
Resources::ShaderBackend backend) {
for (const Resources::ShaderPass& shaderPass : shader.GetPasses()) {
if (ShaderPassMatchesBuiltinPass(shaderPass, BuiltinMaterialPass::ObjectId) &&
::XCEngine::Rendering::Detail::ShaderPassHasGraphicsVariants(shader, shaderPass.name, backend)) {
return &shaderPass;
}
}
struct VSInput {
float3 position : POSITION;
};
const Resources::ShaderPass* objectIdPass = shader.FindPass("ObjectId");
if (objectIdPass != nullptr &&
::XCEngine::Rendering::Detail::ShaderPassHasGraphicsVariants(shader, objectIdPass->name, backend)) {
return objectIdPass;
}
struct PSInput {
float4 position : SV_POSITION;
};
const Resources::ShaderPass* editorObjectIdPass = shader.FindPass("EditorObjectId");
if (editorObjectIdPass != nullptr &&
::XCEngine::Rendering::Detail::ShaderPassHasGraphicsVariants(shader, editorObjectIdPass->name, backend)) {
return editorObjectIdPass;
}
PSInput MainVS(VSInput input) {
PSInput output;
float4 positionWS = mul(gModelMatrix, float4(input.position, 1.0));
float4 positionVS = mul(gViewMatrix, positionWS);
output.position = mul(gProjectionMatrix, positionVS);
return output;
}
if (shader.GetPassCount() > 0 &&
::XCEngine::Rendering::Detail::ShaderPassHasGraphicsVariants(shader, shader.GetPasses()[0].name, backend)) {
return &shader.GetPasses()[0];
}
float4 MainPS(PSInput input) : SV_TARGET {
return gObjectIdColor;
}
)";
const char kBuiltinObjectIdVertexShader[] = R"(#version 430
layout(location = 0) in vec3 aPosition;
layout(std140, binding = 0) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
vec4 gObjectIdColor;
};
void main() {
vec4 positionWS = gModelMatrix * vec4(aPosition, 1.0);
vec4 positionVS = gViewMatrix * positionWS;
gl_Position = gProjectionMatrix * positionVS;
}
)";
const char kBuiltinObjectIdFragmentShader[] = R"(#version 430
layout(std140, binding = 0) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
vec4 gObjectIdColor;
};
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = gObjectIdColor;
}
)";
RHI::InputLayoutDesc 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 = static_cast<uint32_t>(offsetof(Resources::StaticMeshVertex, position));
inputLayout.elements.push_back(position);
return inputLayout;
return nullptr;
}
RHI::GraphicsPipelineDesc CreatePipelineDesc(
RHI::RHIType backendType,
RHI::RHIPipelineLayout* pipelineLayout) {
RHI::RHIPipelineLayout* pipelineLayout,
const Resources::Shader& shader,
const Containers::String& passName) {
RHI::GraphicsPipelineDesc pipelineDesc = {};
pipelineDesc.pipelineLayout = pipelineLayout;
pipelineDesc.topologyType = static_cast<uint32_t>(RHI::PrimitiveTopologyType::Triangle);
@@ -99,7 +61,7 @@ RHI::GraphicsPipelineDesc CreatePipelineDesc(
pipelineDesc.renderTargetFormats[0] = static_cast<uint32_t>(RHI::Format::R8G8B8A8_UNorm);
pipelineDesc.depthStencilFormat = static_cast<uint32_t>(RHI::Format::D24_UNorm_S8_UInt);
pipelineDesc.sampleCount = 1;
pipelineDesc.inputLayout = BuildInputLayout();
pipelineDesc.inputLayout = BuiltinObjectIdPass::BuildInputLayout();
pipelineDesc.rasterizerState.fillMode = static_cast<uint32_t>(RHI::FillMode::Solid);
pipelineDesc.rasterizerState.cullMode = static_cast<uint32_t>(RHI::CullMode::None);
@@ -113,32 +75,14 @@ RHI::GraphicsPipelineDesc CreatePipelineDesc(
pipelineDesc.depthStencilState.depthWriteEnable = false;
pipelineDesc.depthStencilState.depthFunc = static_cast<uint32_t>(RHI::ComparisonFunc::LessEqual);
if (backendType == RHI::RHIType::D3D12) {
pipelineDesc.vertexShader.source.assign(
kBuiltinObjectIdHlsl,
kBuiltinObjectIdHlsl + std::strlen(kBuiltinObjectIdHlsl));
pipelineDesc.vertexShader.sourceLanguage = RHI::ShaderLanguage::HLSL;
pipelineDesc.vertexShader.entryPoint = L"MainVS";
pipelineDesc.vertexShader.profile = L"vs_5_0";
pipelineDesc.fragmentShader.source.assign(
kBuiltinObjectIdHlsl,
kBuiltinObjectIdHlsl + std::strlen(kBuiltinObjectIdHlsl));
pipelineDesc.fragmentShader.sourceLanguage = RHI::ShaderLanguage::HLSL;
pipelineDesc.fragmentShader.entryPoint = L"MainPS";
pipelineDesc.fragmentShader.profile = L"ps_5_0";
} else {
pipelineDesc.vertexShader.source.assign(
kBuiltinObjectIdVertexShader,
kBuiltinObjectIdVertexShader + std::strlen(kBuiltinObjectIdVertexShader));
pipelineDesc.vertexShader.sourceLanguage = RHI::ShaderLanguage::GLSL;
pipelineDesc.vertexShader.profile = L"vs_4_30";
pipelineDesc.fragmentShader.source.assign(
kBuiltinObjectIdFragmentShader,
kBuiltinObjectIdFragmentShader + std::strlen(kBuiltinObjectIdFragmentShader));
pipelineDesc.fragmentShader.sourceLanguage = RHI::ShaderLanguage::GLSL;
pipelineDesc.fragmentShader.profile = L"fs_4_30";
const Resources::ShaderBackend backend = ::XCEngine::Rendering::Detail::ToShaderBackend(backendType);
if (const Resources::ShaderStageVariant* vertexVariant =
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);
}
return pipelineDesc;
@@ -150,6 +94,19 @@ BuiltinObjectIdPass::~BuiltinObjectIdPass() {
Shutdown();
}
RHI::InputLayoutDesc BuiltinObjectIdPass::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 = static_cast<uint32_t>(offsetof(Resources::StaticMeshVertex, position));
inputLayout.elements.push_back(position);
return inputLayout;
}
bool BuiltinObjectIdPass::Render(
const RenderContext& context,
const RenderSurface& surface,
@@ -244,6 +201,26 @@ bool BuiltinObjectIdPass::EnsureInitialized(const RenderContext& context) {
bool BuiltinObjectIdPass::CreateResources(const RenderContext& context) {
m_device = context.device;
m_backendType = context.backendType;
m_builtinObjectIdShader = Resources::ResourceManager::Get().Load<Resources::Shader>(
Resources::GetBuiltinObjectIdShaderPath());
if (!m_builtinObjectIdShader.IsValid()) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinObjectIdPass failed to load builtin object-id shader resource");
DestroyResources();
return false;
}
const Resources::ShaderBackend backend = ::XCEngine::Rendering::Detail::ToShaderBackend(m_backendType);
const Resources::ShaderPass* objectIdPass =
FindObjectIdCompatiblePass(*m_builtinObjectIdShader.Get(), backend);
if (objectIdPass == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinObjectIdPass could not resolve a valid ObjectId shader pass");
DestroyResources();
return false;
}
RHI::DescriptorSetLayoutBinding constantBinding = {};
constantBinding.binding = 0;
@@ -263,7 +240,12 @@ bool BuiltinObjectIdPass::CreateResources(const RenderContext& context) {
return false;
}
m_pipelineState = m_device->CreatePipelineState(CreatePipelineDesc(m_backendType, m_pipelineLayout));
m_pipelineState = m_device->CreatePipelineState(
CreatePipelineDesc(
m_backendType,
m_pipelineLayout,
*m_builtinObjectIdShader.Get(),
objectIdPass->name));
if (m_pipelineState == nullptr || !m_pipelineState->IsValid()) {
if (m_pipelineState != nullptr) {
m_pipelineState->Shutdown();
@@ -299,6 +281,7 @@ void BuiltinObjectIdPass::DestroyResources() {
m_device = nullptr;
m_backendType = RHI::RHIType::D3D12;
m_builtinObjectIdShader.Reset();
}
RHI::RHIDescriptorSet* BuiltinObjectIdPass::GetOrCreatePerObjectSet(uint64_t objectId) {