refactor: route builtin forward pipeline through shader assets

This commit is contained in:
2026-04-02 19:00:48 +08:00
parent 86144416af
commit 9f7d8fd68d
8 changed files with 585 additions and 220 deletions

View File

@@ -3,15 +3,17 @@
#include "Components/GameObject.h"
#include "Components/MeshFilterComponent.h"
#include "Components/MeshRendererComponent.h"
#include "RHI/RHICommandList.h"
#include "Debug/Logger.h"
#include "Core/Asset/ResourceManager.h"
#include "RHI/RHICommandList.h"
#include "Rendering/RenderMaterialUtility.h"
#include "Rendering/RenderSurface.h"
#include "Resources/BuiltinResources.h"
#include "Resources/Material/Material.h"
#include "Resources/Shader/Shader.h"
#include "Resources/Texture/Texture.h"
#include <cstddef>
#include <cstring>
namespace XCEngine {
namespace Rendering {
@@ -51,189 +53,102 @@ namespace {
constexpr uint32_t kDescriptorFirstSet = 1;
constexpr uint32_t kDescriptorSetCount = 5;
const char kBuiltinForwardHlsl[] = R"(
Texture2D gBaseColorTexture : register(t1);
SamplerState gLinearSampler : register(s1);
cbuffer PerObjectConstants : register(b1) {
float4x4 gProjectionMatrix;
float4x4 gViewMatrix;
float4x4 gModelMatrix;
float4x4 gNormalMatrix;
float4 gMainLightDirectionAndIntensity;
float4 gMainLightColorAndFlags;
};
cbuffer MaterialConstants : register(b2) {
float4 gBaseColorFactor;
};
struct VSInput {
float3 position : POSITION;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
struct PSInput {
float4 position : SV_POSITION;
float3 normalWS : TEXCOORD0;
float2 texcoord : TEXCOORD1;
};
PSInput MainVS(VSInput input) {
PSInput output;
float4 positionWS = mul(gModelMatrix, float4(input.position, 1.0f));
float4 positionVS = mul(gViewMatrix, positionWS);
output.position = mul(gProjectionMatrix, positionVS);
output.normalWS = mul((float3x3)gNormalMatrix, input.normal);
output.texcoord = input.texcoord;
return output;
Resources::ShaderBackend ToShaderBackend(RHI::RHIType backendType) {
switch (backendType) {
case RHI::RHIType::D3D12:
return Resources::ShaderBackend::D3D12;
case RHI::RHIType::Vulkan:
return Resources::ShaderBackend::Vulkan;
case RHI::RHIType::OpenGL:
default:
return Resources::ShaderBackend::OpenGL;
}
}
float4 MainPS(PSInput input) : SV_TARGET {
float4 baseColor = gBaseColorTexture.Sample(gLinearSampler, input.texcoord) * gBaseColorFactor;
if (gMainLightColorAndFlags.a < 0.5f) {
return baseColor;
RHI::ShaderLanguage ToRHIShaderLanguage(Resources::ShaderLanguage language) {
switch (language) {
case Resources::ShaderLanguage::HLSL:
return RHI::ShaderLanguage::HLSL;
case Resources::ShaderLanguage::SPIRV:
return RHI::ShaderLanguage::SPIRV;
case Resources::ShaderLanguage::GLSL:
default:
return RHI::ShaderLanguage::GLSL;
}
}
std::wstring ToWideAscii(const Containers::String& value) {
std::wstring wide;
wide.reserve(value.Length());
for (size_t index = 0; index < value.Length(); ++index) {
wide.push_back(static_cast<wchar_t>(value[index]));
}
return wide;
}
bool ShaderPassHasGraphicsVariants(
const Resources::Shader& shader,
const Containers::String& passName,
Resources::ShaderBackend backend) {
return shader.FindVariant(passName, Resources::ShaderType::Vertex, backend) != nullptr &&
shader.FindVariant(passName, Resources::ShaderType::Fragment, backend) != nullptr;
}
const Resources::ShaderPass* FindForwardCompatiblePass(
const Resources::Shader& shader,
const Resources::Material* material,
Resources::ShaderBackend backend) {
if (material != nullptr && !material->GetShaderPass().Empty()) {
const Resources::ShaderPass* explicitPass = shader.FindPass(material->GetShaderPass());
if (explicitPass != nullptr &&
ShaderPassHasGraphicsVariants(shader, explicitPass->name, backend)) {
return explicitPass;
}
}
float3 normalWS = normalize(input.normalWS);
float3 directionToLightWS = normalize(gMainLightDirectionAndIntensity.xyz);
float diffuse = saturate(dot(normalWS, directionToLightWS));
float3 lighting = float3(0.28f, 0.28f, 0.28f) +
gMainLightColorAndFlags.rgb * (diffuse * gMainLightDirectionAndIntensity.w);
return float4(baseColor.rgb * lighting, baseColor.a);
}
)";
const char kBuiltinForwardVertexShader[] = R"(#version 430
layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec3 aNormal;
layout(location = 2) in vec2 aTexCoord;
layout(std140, binding = 1) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
mat4 gNormalMatrix;
vec4 gMainLightDirectionAndIntensity;
vec4 gMainLightColorAndFlags;
};
out vec3 vNormalWS;
out vec2 vTexCoord;
void main() {
vec4 positionWS = gModelMatrix * vec4(aPosition, 1.0);
vec4 positionVS = gViewMatrix * positionWS;
gl_Position = gProjectionMatrix * positionVS;
vNormalWS = mat3(gNormalMatrix) * aNormal;
vTexCoord = aTexCoord;
}
)";
const char kBuiltinForwardFragmentShader[] = R"(#version 430
layout(binding = 1) uniform sampler2D uBaseColorTexture;
layout(std140, binding = 1) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
mat4 gNormalMatrix;
vec4 gMainLightDirectionAndIntensity;
vec4 gMainLightColorAndFlags;
};
layout(std140, binding = 2) uniform MaterialConstants {
vec4 gBaseColorFactor;
};
in vec3 vNormalWS;
in vec2 vTexCoord;
layout(location = 0) out vec4 fragColor;
void main() {
vec4 baseColor = texture(uBaseColorTexture, vTexCoord) * gBaseColorFactor;
if (gMainLightColorAndFlags.w < 0.5) {
fragColor = baseColor;
return;
for (const Resources::ShaderPass& shaderPass : shader.GetPasses()) {
if (ShaderPassMatchesBuiltinPass(shaderPass, BuiltinMaterialPass::ForwardLit) &&
ShaderPassHasGraphicsVariants(shader, shaderPass.name, backend)) {
return &shaderPass;
}
}
vec3 normalWS = normalize(vNormalWS);
vec3 directionToLightWS = normalize(gMainLightDirectionAndIntensity.xyz);
float diffuse = max(dot(normalWS, directionToLightWS), 0.0);
vec3 lighting = vec3(0.28) +
gMainLightColorAndFlags.rgb * (diffuse * gMainLightDirectionAndIntensity.w);
fragColor = vec4(baseColor.rgb * lighting, baseColor.a);
}
)";
const char kBuiltinForwardVulkanVertexShader[] = R"(#version 450
layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec3 aNormal;
layout(location = 2) in vec2 aTexCoord;
layout(set = 1, binding = 0, std140) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
mat4 gNormalMatrix;
vec4 gMainLightDirectionAndIntensity;
vec4 gMainLightColorAndFlags;
};
layout(location = 0) out vec3 vNormalWS;
layout(location = 1) out vec2 vTexCoord;
void main() {
vec4 positionWS = gModelMatrix * vec4(aPosition, 1.0);
vec4 positionVS = gViewMatrix * positionWS;
gl_Position = gProjectionMatrix * positionVS;
vNormalWS = mat3(gNormalMatrix) * aNormal;
vTexCoord = aTexCoord;
}
)";
const char kBuiltinForwardVulkanFragmentShader[] = R"(#version 450
layout(set = 3, binding = 0) uniform texture2D uBaseColorTexture;
layout(set = 4, binding = 0) uniform sampler uLinearSampler;
layout(set = 1, binding = 0, std140) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
mat4 gNormalMatrix;
vec4 gMainLightDirectionAndIntensity;
vec4 gMainLightColorAndFlags;
};
layout(set = 2, binding = 0, std140) uniform MaterialConstants {
vec4 gBaseColorFactor;
};
layout(location = 0) in vec3 vNormalWS;
layout(location = 1) in vec2 vTexCoord;
layout(location = 0) out vec4 fragColor;
void main() {
vec4 baseColor = texture(sampler2D(uBaseColorTexture, uLinearSampler), vTexCoord) * gBaseColorFactor;
if (gMainLightColorAndFlags.w < 0.5) {
fragColor = baseColor;
return;
const Resources::ShaderPass* defaultPass = shader.FindPass("ForwardLit");
if (defaultPass != nullptr &&
ShaderPassHasGraphicsVariants(shader, defaultPass->name, backend)) {
return defaultPass;
}
vec3 normalWS = normalize(vNormalWS);
vec3 directionToLightWS = normalize(gMainLightDirectionAndIntensity.xyz);
float diffuse = max(dot(normalWS, directionToLightWS), 0.0);
vec3 lighting = vec3(0.28) +
gMainLightColorAndFlags.rgb * (diffuse * gMainLightDirectionAndIntensity.w);
fragColor = vec4(baseColor.rgb * lighting, baseColor.a);
defaultPass = shader.FindPass("Default");
if (defaultPass != nullptr &&
ShaderPassHasGraphicsVariants(shader, defaultPass->name, backend)) {
return defaultPass;
}
if (shader.GetPassCount() > 0 &&
ShaderPassHasGraphicsVariants(shader, shader.GetPasses()[0].name, backend)) {
return &shader.GetPasses()[0];
}
return nullptr;
}
)";
void ApplyShaderStageVariant(
const Resources::ShaderStageVariant& variant,
RHI::ShaderCompileDesc& compileDesc) {
compileDesc.source.assign(
variant.sourceCode.CStr(),
variant.sourceCode.CStr() + variant.sourceCode.Length());
compileDesc.sourceLanguage = ToRHIShaderLanguage(variant.language);
compileDesc.entryPoint = ToWideAscii(variant.entryPoint);
compileDesc.profile = ToWideAscii(variant.profile);
}
RHI::GraphicsPipelineDesc CreatePipelineDesc(
RHI::RHIType backendType,
RHI::RHIPipelineLayout* pipelineLayout,
const Resources::Shader& shader,
const Containers::String& passName,
const Resources::Material* material) {
RHI::GraphicsPipelineDesc pipelineDesc = {};
pipelineDesc.pipelineLayout = pipelineLayout;
@@ -246,44 +161,16 @@ RHI::GraphicsPipelineDesc CreatePipelineDesc(
pipelineDesc.inputLayout = BuiltinForwardPipeline::BuildInputLayout();
if (backendType == RHI::RHIType::D3D12) {
pipelineDesc.vertexShader.source.assign(
kBuiltinForwardHlsl,
kBuiltinForwardHlsl + std::strlen(kBuiltinForwardHlsl));
pipelineDesc.vertexShader.sourceLanguage = RHI::ShaderLanguage::HLSL;
pipelineDesc.vertexShader.entryPoint = L"MainVS";
pipelineDesc.vertexShader.profile = L"vs_5_0";
pipelineDesc.fragmentShader.source.assign(
kBuiltinForwardHlsl,
kBuiltinForwardHlsl + std::strlen(kBuiltinForwardHlsl));
pipelineDesc.fragmentShader.sourceLanguage = RHI::ShaderLanguage::HLSL;
pipelineDesc.fragmentShader.entryPoint = L"MainPS";
pipelineDesc.fragmentShader.profile = L"ps_5_0";
} else if (backendType == RHI::RHIType::Vulkan) {
pipelineDesc.vertexShader.source.assign(
kBuiltinForwardVulkanVertexShader,
kBuiltinForwardVulkanVertexShader + std::strlen(kBuiltinForwardVulkanVertexShader));
pipelineDesc.vertexShader.sourceLanguage = RHI::ShaderLanguage::GLSL;
pipelineDesc.vertexShader.profile = L"vs_4_50";
pipelineDesc.fragmentShader.source.assign(
kBuiltinForwardVulkanFragmentShader,
kBuiltinForwardVulkanFragmentShader + std::strlen(kBuiltinForwardVulkanFragmentShader));
pipelineDesc.fragmentShader.sourceLanguage = RHI::ShaderLanguage::GLSL;
pipelineDesc.fragmentShader.profile = L"fs_4_50";
} else {
pipelineDesc.vertexShader.source.assign(
kBuiltinForwardVertexShader,
kBuiltinForwardVertexShader + std::strlen(kBuiltinForwardVertexShader));
pipelineDesc.vertexShader.sourceLanguage = RHI::ShaderLanguage::GLSL;
pipelineDesc.vertexShader.profile = L"vs_4_30";
pipelineDesc.fragmentShader.source.assign(
kBuiltinForwardFragmentShader,
kBuiltinForwardFragmentShader + std::strlen(kBuiltinForwardFragmentShader));
pipelineDesc.fragmentShader.sourceLanguage = RHI::ShaderLanguage::GLSL;
pipelineDesc.fragmentShader.profile = L"fs_4_30";
const Resources::ShaderBackend backend = ToShaderBackend(backendType);
const Resources::ShaderStageVariant* vertexVariant =
shader.FindVariant(passName, Resources::ShaderType::Vertex, backend);
const Resources::ShaderStageVariant* fragmentVariant =
shader.FindVariant(passName, Resources::ShaderType::Fragment, backend);
if (vertexVariant != nullptr) {
ApplyShaderStageVariant(*vertexVariant, pipelineDesc.vertexShader);
}
if (fragmentVariant != nullptr) {
ApplyShaderStageVariant(*fragmentVariant, pipelineDesc.fragmentShader);
}
return pipelineDesc;
@@ -480,6 +367,15 @@ bool BuiltinForwardPipeline::EnsureInitialized(const RenderContext& context) {
}
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;
}
RHI::DescriptorSetLayoutBinding constantBinding = {};
constantBinding.binding = 0;
constantBinding.type = static_cast<uint32_t>(RHI::DescriptorType::CBV);
@@ -659,21 +555,67 @@ void BuiltinForwardPipeline::DestroyPipelineResources() {
m_device = nullptr;
m_initialized = false;
m_builtinForwardShader.Reset();
}
BuiltinForwardPipeline::ResolvedShaderPass BuiltinForwardPipeline::ResolveForwardShaderPass(
const Resources::Material* material) const {
ResolvedShaderPass resolved = {};
const Resources::ShaderBackend backend = ToShaderBackend(m_backendType);
if (material != nullptr && material->GetShader() != nullptr) {
const Resources::Shader* materialShader = material->GetShader();
if (const Resources::ShaderPass* shaderPass =
FindForwardCompatiblePass(*materialShader, material, backend)) {
resolved.shader = materialShader;
resolved.pass = shaderPass;
resolved.passName = shaderPass->name;
return resolved;
}
}
if (m_builtinForwardShader.IsValid()) {
const Resources::Shader* builtinShader = m_builtinForwardShader.Get();
if (const Resources::ShaderPass* shaderPass =
FindForwardCompatiblePass(*builtinShader, nullptr, backend)) {
resolved.shader = builtinShader;
resolved.pass = shaderPass;
resolved.passName = shaderPass->name;
}
}
return resolved;
}
RHI::RHIPipelineState* BuiltinForwardPipeline::GetOrCreatePipelineState(
const RenderContext& context,
const Resources::Material* material) {
const Resources::MaterialRenderState renderState =
material != nullptr ? material->GetRenderState() : Resources::MaterialRenderState();
const ResolvedShaderPass resolvedShaderPass = ResolveForwardShaderPass(material);
if (resolvedShaderPass.shader == nullptr || resolvedShaderPass.pass == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline could not resolve a valid ForwardLit shader pass");
return nullptr;
}
const auto existing = m_pipelineStates.find(renderState);
PipelineStateKey pipelineKey = {};
pipelineKey.renderState =
material != nullptr ? material->GetRenderState() : Resources::MaterialRenderState();
pipelineKey.shader = resolvedShaderPass.shader;
pipelineKey.passName = resolvedShaderPass.passName;
const auto existing = m_pipelineStates.find(pipelineKey);
if (existing != m_pipelineStates.end()) {
return existing->second;
}
const RHI::GraphicsPipelineDesc pipelineDesc =
CreatePipelineDesc(context.backendType, m_pipelineLayout, material);
CreatePipelineDesc(
context.backendType,
m_pipelineLayout,
*resolvedShaderPass.shader,
resolvedShaderPass.passName,
material);
RHI::RHIPipelineState* pipelineState = context.device->CreatePipelineState(pipelineDesc);
if (pipelineState == nullptr || !pipelineState->IsValid()) {
Debug::Logger::Get().Error(
@@ -686,7 +628,7 @@ RHI::RHIPipelineState* BuiltinForwardPipeline::GetOrCreatePipelineState(
return nullptr;
}
m_pipelineStates.emplace(renderState, pipelineState);
m_pipelineStates.emplace(pipelineKey, pipelineState);
return pipelineState;
}