Add procedural skybox scene coverage

This commit is contained in:
2026-04-05 23:44:32 +08:00
parent be2013f3c4
commit 8151be0f45
27 changed files with 1092 additions and 5 deletions

View File

@@ -3,11 +3,13 @@
#include "Debug/Logger.h"
#include "Core/Asset/ResourceManager.h"
#include "RHI/RHICommandList.h"
#include "Rendering/Detail/ShaderVariantUtils.h"
#include "Rendering/Materials/RenderMaterialResolve.h"
#include "Rendering/RenderSurface.h"
#include "Resources/BuiltinResources.h"
#include <cstddef>
#include <cmath>
namespace XCEngine {
namespace Rendering {
@@ -40,6 +42,32 @@ private:
BuiltinForwardPipeline& m_pipeline;
};
class BuiltinForwardSkyboxPass final : public RenderPass {
public:
explicit BuiltinForwardSkyboxPass(BuiltinForwardPipeline& pipeline)
: m_pipeline(pipeline) {
}
const char* GetName() const override {
return "BuiltinForwardSkyboxPass";
}
bool Initialize(const RenderContext& context) override {
return m_pipeline.EnsureInitialized(context);
}
void Shutdown() override {
m_pipeline.DestroyPipelineResources();
}
bool Execute(const RenderPassContext& context) override {
return m_pipeline.ExecuteForwardSkyboxPass(context);
}
private:
BuiltinForwardPipeline& m_pipeline;
};
class BuiltinForwardTransparentPass final : public RenderPass {
public:
explicit BuiltinForwardTransparentPass(BuiltinForwardPipeline& pipeline)
@@ -75,10 +103,66 @@ bool IsDepthFormat(RHI::Format format) {
format == RHI::Format::D32_Float;
}
const Resources::ShaderPass* FindCompatibleSkyboxPass(
const Resources::Shader& shader,
Resources::ShaderBackend backend) {
const Resources::ShaderPass* skyboxPass = shader.FindPass("Skybox");
if (skyboxPass != nullptr &&
::XCEngine::Rendering::Detail::ShaderPassHasGraphicsVariants(shader, skyboxPass->name, backend)) {
return skyboxPass;
}
if (shader.GetPassCount() > 0 &&
::XCEngine::Rendering::Detail::ShaderPassHasGraphicsVariants(shader, shader.GetPasses()[0].name, backend)) {
return &shader.GetPasses()[0];
}
return nullptr;
}
RHI::GraphicsPipelineDesc CreateSkyboxPipelineDesc(
RHI::RHIType backendType,
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);
pipelineDesc.renderTargetCount = 1;
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.rasterizerState.fillMode = static_cast<uint32_t>(RHI::FillMode::Solid);
pipelineDesc.rasterizerState.cullMode = static_cast<uint32_t>(RHI::CullMode::None);
pipelineDesc.rasterizerState.frontFace = static_cast<uint32_t>(RHI::FrontFace::CounterClockwise);
pipelineDesc.rasterizerState.depthClipEnable = true;
pipelineDesc.blendState.blendEnable = false;
pipelineDesc.blendState.colorWriteMask = static_cast<uint8_t>(RHI::ColorWriteMask::All);
pipelineDesc.depthStencilState.depthTestEnable = true;
pipelineDesc.depthStencilState.depthWriteEnable = false;
pipelineDesc.depthStencilState.depthFunc = static_cast<uint32_t>(RHI::ComparisonFunc::LessEqual);
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;
}
} // namespace
BuiltinForwardPipeline::BuiltinForwardPipeline() {
m_passSequence.AddPass(std::make_unique<Detail::BuiltinForwardOpaquePass>(*this));
m_passSequence.AddPass(std::make_unique<Detail::BuiltinForwardSkyboxPass>(*this));
m_passSequence.AddPass(std::make_unique<Detail::BuiltinForwardTransparentPass>(*this));
}
@@ -145,6 +229,11 @@ bool BuiltinForwardPipeline::Render(
return m_passSequence.Execute(passContext);
}
bool BuiltinForwardPipeline::HasProceduralSkybox(const RenderSceneData& sceneData) const {
return sceneData.environment.HasProceduralSkybox() &&
sceneData.cameraData.perspectiveProjection;
}
bool BuiltinForwardPipeline::BeginForwardScenePass(const RenderPassContext& passContext) {
const RenderContext& context = passContext.renderContext;
const RenderSurface& surface = passContext.surface;
@@ -200,7 +289,8 @@ bool BuiltinForwardPipeline::BeginForwardScenePass(const RenderPassContext& pass
? surface.GetClearColorOverride()
: sceneData.cameraData.clearColor;
const float clearValues[4] = { clearColor.r, clearColor.g, clearColor.b, clearColor.a };
if (HasRenderClearFlag(sceneData.cameraData.clearFlags, RenderClearFlags::Color)) {
if (HasRenderClearFlag(sceneData.cameraData.clearFlags, RenderClearFlags::Color) &&
!HasProceduralSkybox(sceneData)) {
for (RHI::RHIResourceView* renderTarget : renderTargets) {
if (renderTarget != nullptr) {
commandList->ClearRenderTarget(renderTarget, clearValues, 1, clearRects);
@@ -256,6 +346,51 @@ bool BuiltinForwardPipeline::ExecuteForwardOpaquePass(const RenderPassContext& p
return DrawVisibleItems(context, sceneData, false);
}
bool BuiltinForwardPipeline::ExecuteForwardSkyboxPass(const RenderPassContext& passContext) {
const RenderContext& context = passContext.renderContext;
const RenderSurface& surface = passContext.surface;
const RenderSceneData& sceneData = passContext.sceneData;
if (!HasProceduralSkybox(sceneData)) {
return true;
}
if (surface.GetDepthAttachment() == nullptr) {
return true;
}
if (!EnsureSkyboxResources(context) ||
m_skyboxPipelineLayout == nullptr ||
m_skyboxPipelineState == nullptr ||
m_skyboxConstantSet == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline skybox pass failed to initialize runtime skybox resources");
return false;
}
SkyboxConstants constants = {};
constants.topColor = sceneData.environment.skybox.topColor.ToVector4();
constants.horizonColor = sceneData.environment.skybox.horizonColor.ToVector4();
constants.bottomColor = sceneData.environment.skybox.bottomColor.ToVector4();
constants.cameraRightAndTanHalfFov = Math::Vector4(
sceneData.cameraData.worldRight,
std::tan(sceneData.cameraData.verticalFovRadians * 0.5f));
constants.cameraUpAndAspect = Math::Vector4(
sceneData.cameraData.worldUp,
sceneData.cameraData.aspectRatio);
constants.cameraForwardAndUnused = Math::Vector4(sceneData.cameraData.worldForward, 0.0f);
m_skyboxConstantSet->WriteConstant(0, &constants, sizeof(constants));
RHI::RHICommandList* commandList = context.commandList;
commandList->SetPrimitiveTopology(RHI::PrimitiveTopology::TriangleList);
commandList->SetPipelineState(m_skyboxPipelineState);
RHI::RHIDescriptorSet* descriptorSets[] = { m_skyboxConstantSet };
commandList->SetGraphicsDescriptorSets(0, 1, descriptorSets, m_skyboxPipelineLayout);
commandList->Draw(3, 1, 0, 0);
return true;
}
bool BuiltinForwardPipeline::ExecuteForwardTransparentPass(const RenderPassContext& passContext) {
const RenderContext& context = passContext.renderContext;
const RenderSceneData& sceneData = passContext.sceneData;
@@ -345,6 +480,15 @@ bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& contex
return false;
}
m_builtinSkyboxShader = Resources::ResourceManager::Get().Load<Resources::Shader>(
Resources::GetBuiltinSkyboxShaderPath());
if (!m_builtinSkyboxShader.IsValid()) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline failed to load builtin skybox shader resource");
return false;
}
RHI::SamplerDesc samplerDesc = {};
samplerDesc.filter = static_cast<uint32_t>(RHI::FilterMode::Linear);
samplerDesc.addressU = static_cast<uint32_t>(RHI::TextureAddressMode::Clamp);
@@ -396,6 +540,123 @@ bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& contex
return true;
}
bool BuiltinForwardPipeline::EnsureSkyboxResources(const RenderContext& context) {
if (m_skyboxPipelineLayout != nullptr &&
m_skyboxPipelineState != nullptr &&
m_skyboxConstantPool != nullptr &&
m_skyboxConstantSet != nullptr) {
return true;
}
DestroySkyboxResources();
return CreateSkyboxResources(context);
}
bool BuiltinForwardPipeline::CreateSkyboxResources(const RenderContext& context) {
if (!context.IsValid() || !m_builtinSkyboxShader.IsValid()) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline skybox resources require a valid context and loaded shader");
return false;
}
const Resources::ShaderBackend backend = ::XCEngine::Rendering::Detail::ToShaderBackend(m_backendType);
const Resources::ShaderPass* skyboxPass =
FindCompatibleSkyboxPass(*m_builtinSkyboxShader.Get(), backend);
if (skyboxPass == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline could not resolve a valid skybox shader pass");
return false;
}
RHI::DescriptorSetLayoutBinding constantBinding = {};
constantBinding.binding = 0;
constantBinding.type = static_cast<uint32_t>(RHI::DescriptorType::CBV);
constantBinding.count = 1;
constantBinding.visibility = static_cast<uint32_t>(RHI::ShaderVisibility::All);
RHI::DescriptorSetLayoutDesc constantLayout = {};
constantLayout.bindings = &constantBinding;
constantLayout.bindingCount = 1;
RHI::RHIPipelineLayoutDesc pipelineLayoutDesc = {};
pipelineLayoutDesc.setLayouts = &constantLayout;
pipelineLayoutDesc.setLayoutCount = 1;
m_skyboxPipelineLayout = m_device->CreatePipelineLayout(pipelineLayoutDesc);
if (m_skyboxPipelineLayout == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline failed to create skybox pipeline layout");
DestroySkyboxResources();
return false;
}
RHI::DescriptorPoolDesc constantPoolDesc = {};
constantPoolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
constantPoolDesc.descriptorCount = 1;
constantPoolDesc.shaderVisible = false;
m_skyboxConstantPool = m_device->CreateDescriptorPool(constantPoolDesc);
if (m_skyboxConstantPool == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline failed to create skybox descriptor pool");
DestroySkyboxResources();
return false;
}
m_skyboxConstantSet = m_skyboxConstantPool->AllocateSet(constantLayout);
if (m_skyboxConstantSet == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline failed to allocate skybox descriptor set");
DestroySkyboxResources();
return false;
}
m_skyboxPipelineState = m_device->CreatePipelineState(
CreateSkyboxPipelineDesc(
m_backendType,
m_skyboxPipelineLayout,
*m_builtinSkyboxShader.Get(),
skyboxPass->name));
if (m_skyboxPipelineState == nullptr || !m_skyboxPipelineState->IsValid()) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinForwardPipeline failed to create skybox graphics pipeline state");
DestroySkyboxResources();
return false;
}
return true;
}
void BuiltinForwardPipeline::DestroySkyboxResources() {
if (m_skyboxPipelineState != nullptr) {
m_skyboxPipelineState->Shutdown();
delete m_skyboxPipelineState;
m_skyboxPipelineState = nullptr;
}
if (m_skyboxConstantSet != nullptr) {
m_skyboxConstantSet->Shutdown();
delete m_skyboxConstantSet;
m_skyboxConstantSet = nullptr;
}
if (m_skyboxConstantPool != nullptr) {
m_skyboxConstantPool->Shutdown();
delete m_skyboxConstantPool;
m_skyboxConstantPool = nullptr;
}
if (m_skyboxPipelineLayout != nullptr) {
m_skyboxPipelineLayout->Shutdown();
delete m_skyboxPipelineLayout;
m_skyboxPipelineLayout = nullptr;
}
}
void BuiltinForwardPipeline::DestroyPipelineResources() {
m_resourceCache.Shutdown();
@@ -423,6 +684,8 @@ void BuiltinForwardPipeline::DestroyPipelineResources() {
m_fallbackTextureView = nullptr;
}
DestroySkyboxResources();
if (m_fallbackTexture != nullptr) {
m_fallbackTexture->Shutdown();
delete m_fallbackTexture;
@@ -445,6 +708,7 @@ void BuiltinForwardPipeline::DestroyPipelineResources() {
m_initialized = false;
m_builtinForwardShader.Reset();
m_builtinUnlitShader.Reset();
m_builtinSkyboxShader.Reset();
}
} // namespace Pipelines