Add rendering post-process scene integration test

This commit is contained in:
2026-04-06 13:30:53 +08:00
parent 0804052d6f
commit 2b70a2e309
17 changed files with 1144 additions and 0 deletions

View File

@@ -455,6 +455,7 @@ add_library(XCEngine STATIC
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/Rendering/Passes/BuiltinDepthOnlyPass.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/Rendering/Passes/BuiltinShadowCasterPass.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/Rendering/Passes/BuiltinObjectIdPass.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/Rendering/Passes/BuiltinColorScalePostProcessPass.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/Rendering/Passes/BuiltinInfiniteGridPass.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/Rendering/Passes/BuiltinObjectIdOutlinePass.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/Rendering/Pipelines/BuiltinForwardPipeline.h
@@ -466,6 +467,7 @@ add_library(XCEngine STATIC
${CMAKE_CURRENT_SOURCE_DIR}/src/Rendering/Passes/BuiltinShadowCasterPass.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Rendering/Passes/BuiltinObjectIdPass.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Rendering/Passes/BuiltinObjectIdPassResources.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Rendering/Passes/BuiltinColorScalePostProcessPass.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Rendering/Passes/BuiltinInfiniteGridPass.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Rendering/Passes/BuiltinObjectIdOutlinePass.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Rendering/RenderSurface.cpp

View File

@@ -0,0 +1,16 @@
// XC_BUILTIN_COLOR_SCALE_POST_PROCESS_OPENGL_PS
#version 430
layout(binding = 0) uniform sampler2D uSourceColorTexture;
layout(std140, binding = 0) uniform PostProcessConstants {
vec4 gColorScale;
};
in vec2 vTexCoord;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = texture(uSourceColorTexture, vTexCoord) * gColorScale;
}

View File

@@ -0,0 +1,17 @@
// XC_BUILTIN_COLOR_SCALE_POST_PROCESS_VULKAN_PS
#version 450
layout(set = 0, binding = 0, std140) uniform PostProcessConstants {
vec4 gColorScale;
};
layout(set = 1, binding = 0) uniform texture2D uSourceColorTexture;
layout(set = 2, binding = 0) uniform sampler uLinearClampSampler;
layout(location = 0) in vec2 vTexCoord;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = texture(sampler2D(uSourceColorTexture, uLinearClampSampler), vTexCoord) * gColorScale;
}

View File

@@ -0,0 +1,16 @@
// XC_BUILTIN_COLOR_SCALE_POST_PROCESS_D3D12_PS
Texture2D gSourceColorTexture : register(t0);
SamplerState gLinearClampSampler : register(s0);
cbuffer PostProcessConstants : register(b0) {
float4 gColorScale;
};
struct PSInput {
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
float4 MainPS(PSInput input) : SV_TARGET {
return gSourceColorTexture.Sample(gLinearClampSampler, input.texcoord) * gColorScale;
}

View File

@@ -0,0 +1,28 @@
Shader "Builtin Color Scale Post Process"
{
Properties
{
_ColorScale ("Color Scale", Color) = (0.65,0.80,1.00,1.0)
}
SubShader
{
Pass
{
Name "ColorScale"
Tags { "LightMode" = "PostProcess" }
Resources
{
PostProcessConstants (ConstantBuffer, 0, 0)
SourceColorTexture (Texture2D, 1, 0)
LinearClampSampler (Sampler, 2, 0)
}
HLSLPROGRAM
#pragma vertex MainVS
#pragma fragment MainPS
#pragma backend D3D12 HLSL "color-scale-post-process.vs.hlsl" "color-scale-post-process.ps.hlsl" vs_5_0 ps_5_0
#pragma backend OpenGL GLSL "color-scale-post-process.vert.glsl" "color-scale-post-process.frag.glsl"
#pragma backend Vulkan GLSL "color-scale-post-process.vert.vk.glsl" "color-scale-post-process.frag.vk.glsl"
ENDHLSL
}
}
}

View File

@@ -0,0 +1,16 @@
// XC_BUILTIN_COLOR_SCALE_POST_PROCESS_OPENGL_VS
#version 430
out vec2 vTexCoord;
void main() {
const vec2 positions[3] = vec2[3](
vec2(-1.0, -1.0),
vec2(-1.0, 3.0),
vec2( 3.0, -1.0)
);
vec2 position = positions[gl_VertexID];
gl_Position = vec4(position, 1.0, 1.0);
vTexCoord = position * 0.5 + 0.5;
}

View File

@@ -0,0 +1,18 @@
// XC_BUILTIN_COLOR_SCALE_POST_PROCESS_VULKAN_VS
#version 450
layout(location = 0) out vec2 vTexCoord;
void main() {
const vec2 positions[3] = vec2[3](
vec2(-1.0, -1.0),
vec2(-1.0, 3.0),
vec2( 3.0, -1.0)
);
vec2 position = positions[gl_VertexIndex];
gl_Position = vec4(position, 1.0, 1.0);
vTexCoord = vec2(
position.x * 0.5 + 0.5,
1.0 - (position.y * 0.5 + 0.5));
}

View File

@@ -0,0 +1,22 @@
// XC_BUILTIN_COLOR_SCALE_POST_PROCESS_D3D12_VS
struct VSOutput {
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
VSOutput MainVS(uint vertexId : SV_VertexID) {
const float2 positions[3] = {
float2(-1.0f, -1.0f),
float2(-1.0f, 3.0f),
float2( 3.0f, -1.0f)
};
const float2 position = positions[vertexId];
VSOutput output;
output.position = float4(position, 1.0f, 1.0f);
output.texcoord = float2(
position.x * 0.5f + 0.5f,
1.0f - (position.y * 0.5f + 0.5f));
return output;
}

View File

@@ -0,0 +1,68 @@
#pragma once
#include <XCEngine/Core/Asset/ResourceHandle.h>
#include <XCEngine/Core/Containers/String.h>
#include <XCEngine/Core/Math/Vector4.h>
#include <XCEngine/Rendering/RenderPass.h>
#include <XCEngine/Resources/Shader/Shader.h>
#include <XCEngine/RHI/RHIEnums.h>
namespace XCEngine {
namespace RHI {
class RHIDescriptorPool;
class RHIDescriptorSet;
class RHIPipelineLayout;
class RHIPipelineState;
class RHISampler;
class RHIDevice;
} // namespace RHI
namespace Rendering {
namespace Passes {
class BuiltinColorScalePostProcessPass final : public RenderPass {
public:
struct OwnedDescriptorSet {
RHI::RHIDescriptorPool* pool = nullptr;
RHI::RHIDescriptorSet* set = nullptr;
};
explicit BuiltinColorScalePostProcessPass(
const Math::Vector4& colorScale = Math::Vector4(0.65f, 0.80f, 1.0f, 1.0f),
Containers::String shaderPath = {});
~BuiltinColorScalePostProcessPass() override;
const char* GetName() const override;
bool Execute(const RenderPassContext& context) override;
void Shutdown() override;
void SetColorScale(const Math::Vector4& colorScale);
const Math::Vector4& GetColorScale() const;
void SetShaderPath(const Containers::String& shaderPath);
const Containers::String& GetShaderPath() const;
private:
bool EnsureInitialized(const RenderContext& renderContext, RHI::Format renderTargetFormat);
bool CreateResources(const RenderContext& renderContext, RHI::Format renderTargetFormat);
void DestroyResources();
void DestroyOwnedDescriptorSet(OwnedDescriptorSet& descriptorSet);
Containers::String m_shaderPath;
Math::Vector4 m_colorScale = Math::Vector4(0.65f, 0.80f, 1.0f, 1.0f);
RHI::RHIDevice* m_device = nullptr;
RHI::RHIType m_backendType = RHI::RHIType::D3D12;
RHI::Format m_renderTargetFormat = RHI::Format::Unknown;
Resources::ResourceHandle<Resources::Shader> m_shader;
RHI::RHISampler* m_sampler = nullptr;
RHI::RHIPipelineLayout* m_pipelineLayout = nullptr;
RHI::RHIPipelineState* m_pipelineState = nullptr;
OwnedDescriptorSet m_constantsSet = {};
OwnedDescriptorSet m_textureSet = {};
OwnedDescriptorSet m_samplerSet = {};
RHI::RHIResourceView* m_boundSourceColorView = nullptr;
};
} // namespace Passes
} // namespace Rendering
} // namespace XCEngine

View File

@@ -29,6 +29,7 @@ Containers::String GetBuiltinDepthOnlyShaderPath();
Containers::String GetBuiltinShadowCasterShaderPath();
Containers::String GetBuiltinObjectIdShaderPath();
Containers::String GetBuiltinSkyboxShaderPath();
Containers::String GetBuiltinColorScalePostProcessShaderPath();
Containers::String GetBuiltinDefaultPrimitiveTexturePath();
bool TryParseBuiltinPrimitiveType(const Containers::String& path, BuiltinPrimitiveType& outPrimitiveType);

View File

@@ -0,0 +1,439 @@
#include "Rendering/Passes/BuiltinColorScalePostProcessPass.h"
#include "Core/Asset/ResourceManager.h"
#include "Debug/Logger.h"
#include "Rendering/Detail/ShaderVariantUtils.h"
#include "Rendering/RenderSurface.h"
#include "Resources/BuiltinResources.h"
#include "RHI/RHICommandList.h"
#include "RHI/RHIDescriptorPool.h"
#include "RHI/RHIDescriptorSet.h"
#include "RHI/RHIDevice.h"
#include "RHI/RHIPipelineLayout.h"
#include "RHI/RHIPipelineState.h"
#include "RHI/RHIResourceView.h"
#include "RHI/RHISampler.h"
#include <utility>
namespace XCEngine {
namespace Rendering {
namespace Passes {
namespace {
struct PostProcessConstants {
Math::Vector4 colorScale = Math::Vector4::One();
};
const Resources::ShaderPass* FindCompatiblePass(
const Resources::Shader& shader,
Resources::ShaderBackend backend) {
const Resources::ShaderPass* colorScalePass = shader.FindPass("ColorScale");
if (colorScalePass != nullptr &&
::XCEngine::Rendering::Detail::ShaderPassHasGraphicsVariants(shader, colorScalePass->name, backend)) {
return colorScalePass;
}
if (shader.GetPassCount() > 0 &&
::XCEngine::Rendering::Detail::ShaderPassHasGraphicsVariants(shader, shader.GetPasses()[0].name, backend)) {
return &shader.GetPasses()[0];
}
return nullptr;
}
RHI::GraphicsPipelineDesc CreatePipelineDesc(
RHI::RHIType backendType,
RHI::RHIPipelineLayout* pipelineLayout,
const Resources::Shader& shader,
const Containers::String& passName,
RHI::Format renderTargetFormat) {
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>(renderTargetFormat);
pipelineDesc.depthStencilFormat = static_cast<uint32_t>(RHI::Format::Unknown);
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.srcBlend = static_cast<uint32_t>(RHI::BlendFactor::One);
pipelineDesc.blendState.dstBlend = static_cast<uint32_t>(RHI::BlendFactor::Zero);
pipelineDesc.blendState.srcBlendAlpha = static_cast<uint32_t>(RHI::BlendFactor::One);
pipelineDesc.blendState.dstBlendAlpha = static_cast<uint32_t>(RHI::BlendFactor::Zero);
pipelineDesc.blendState.blendOp = static_cast<uint32_t>(RHI::BlendOp::Add);
pipelineDesc.blendState.blendOpAlpha = static_cast<uint32_t>(RHI::BlendOp::Add);
pipelineDesc.blendState.colorWriteMask = static_cast<uint8_t>(RHI::ColorWriteMask::All);
pipelineDesc.depthStencilState.depthTestEnable = false;
pipelineDesc.depthStencilState.depthWriteEnable = false;
pipelineDesc.depthStencilState.depthFunc = static_cast<uint32_t>(RHI::ComparisonFunc::Always);
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
BuiltinColorScalePostProcessPass::BuiltinColorScalePostProcessPass(
const Math::Vector4& colorScale,
Containers::String shaderPath)
: m_shaderPath(std::move(shaderPath))
, m_colorScale(colorScale) {
if (m_shaderPath.Empty()) {
m_shaderPath = Resources::GetBuiltinColorScalePostProcessShaderPath();
}
}
BuiltinColorScalePostProcessPass::~BuiltinColorScalePostProcessPass() {
Shutdown();
}
const char* BuiltinColorScalePostProcessPass::GetName() const {
return "BuiltinColorScalePostProcessPass";
}
bool BuiltinColorScalePostProcessPass::Execute(const RenderPassContext& context) {
if (!context.renderContext.IsValid() || context.sourceColorView == nullptr) {
return false;
}
const std::vector<RHI::RHIResourceView*>& colorAttachments = context.surface.GetColorAttachments();
if (colorAttachments.empty() || colorAttachments[0] == nullptr) {
return false;
}
const Math::RectInt renderArea = context.surface.GetRenderArea();
if (renderArea.width <= 0 || renderArea.height <= 0) {
return false;
}
const RHI::Format renderTargetFormat = colorAttachments[0]->GetFormat();
if (!EnsureInitialized(context.renderContext, renderTargetFormat)) {
return false;
}
PostProcessConstants constants = {};
constants.colorScale = m_colorScale;
m_constantsSet.set->WriteConstant(0, &constants, sizeof(constants));
if (m_boundSourceColorView != context.sourceColorView) {
m_textureSet.set->Update(0, context.sourceColorView);
m_boundSourceColorView = context.sourceColorView;
}
RHI::RHICommandList* commandList = context.renderContext.commandList;
RHI::RHIResourceView* renderTarget = colorAttachments[0];
if (context.surface.IsAutoTransitionEnabled()) {
commandList->TransitionBarrier(
renderTarget,
context.surface.GetColorStateBefore(),
RHI::ResourceStates::RenderTarget);
}
commandList->SetRenderTargets(1, &renderTarget, nullptr);
const RHI::Viewport viewport = {
static_cast<float>(renderArea.x),
static_cast<float>(renderArea.y),
static_cast<float>(renderArea.width),
static_cast<float>(renderArea.height),
0.0f,
1.0f
};
const RHI::Rect scissorRect = {
renderArea.x,
renderArea.y,
renderArea.x + renderArea.width,
renderArea.y + renderArea.height
};
commandList->SetViewport(viewport);
commandList->SetScissorRect(scissorRect);
commandList->SetPrimitiveTopology(RHI::PrimitiveTopology::TriangleList);
commandList->SetPipelineState(m_pipelineState);
RHI::RHIDescriptorSet* descriptorSets[] = {
m_constantsSet.set,
m_textureSet.set,
m_samplerSet.set
};
commandList->SetGraphicsDescriptorSets(0, 3, descriptorSets, m_pipelineLayout);
commandList->Draw(3, 1, 0, 0);
commandList->EndRenderPass();
if (context.surface.IsAutoTransitionEnabled()) {
commandList->TransitionBarrier(
renderTarget,
RHI::ResourceStates::RenderTarget,
context.surface.GetColorStateAfter());
}
return true;
}
void BuiltinColorScalePostProcessPass::Shutdown() {
DestroyResources();
}
void BuiltinColorScalePostProcessPass::SetColorScale(const Math::Vector4& colorScale) {
m_colorScale = colorScale;
}
const Math::Vector4& BuiltinColorScalePostProcessPass::GetColorScale() const {
return m_colorScale;
}
void BuiltinColorScalePostProcessPass::SetShaderPath(const Containers::String& shaderPath) {
if (m_shaderPath == shaderPath) {
return;
}
DestroyResources();
m_shaderPath = shaderPath;
}
const Containers::String& BuiltinColorScalePostProcessPass::GetShaderPath() const {
return m_shaderPath;
}
bool BuiltinColorScalePostProcessPass::EnsureInitialized(
const RenderContext& renderContext,
RHI::Format renderTargetFormat) {
if (m_device == renderContext.device &&
m_backendType == renderContext.backendType &&
m_renderTargetFormat == renderTargetFormat &&
m_pipelineLayout != nullptr &&
m_pipelineState != nullptr &&
m_sampler != nullptr &&
m_constantsSet.set != nullptr &&
m_textureSet.set != nullptr &&
m_samplerSet.set != nullptr) {
return true;
}
DestroyResources();
return CreateResources(renderContext, renderTargetFormat);
}
bool BuiltinColorScalePostProcessPass::CreateResources(
const RenderContext& renderContext,
RHI::Format renderTargetFormat) {
if (!renderContext.IsValid()) {
return false;
}
if (m_shaderPath.Empty()) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinColorScalePostProcessPass requires a shader path before resource creation");
return false;
}
m_shader = Resources::ResourceManager::Get().Load<Resources::Shader>(m_shaderPath);
if (!m_shader.IsValid()) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinColorScalePostProcessPass failed to load configured post-process shader resource");
DestroyResources();
return false;
}
const Resources::ShaderBackend backend = ::XCEngine::Rendering::Detail::ToShaderBackend(renderContext.backendType);
const Resources::ShaderPass* colorScalePass = FindCompatiblePass(*m_shader.Get(), backend);
if (colorScalePass == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinColorScalePostProcessPass could not resolve a valid ColorScale shader pass");
DestroyResources();
return false;
}
m_device = renderContext.device;
m_backendType = renderContext.backendType;
m_renderTargetFormat = renderTargetFormat;
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::DescriptorSetLayoutBinding textureBinding = {};
textureBinding.binding = 0;
textureBinding.type = static_cast<uint32_t>(RHI::DescriptorType::SRV);
textureBinding.count = 1;
textureBinding.visibility = static_cast<uint32_t>(RHI::ShaderVisibility::All);
RHI::DescriptorSetLayoutBinding samplerBinding = {};
samplerBinding.binding = 0;
samplerBinding.type = static_cast<uint32_t>(RHI::DescriptorType::Sampler);
samplerBinding.count = 1;
samplerBinding.visibility = static_cast<uint32_t>(RHI::ShaderVisibility::All);
RHI::DescriptorSetLayoutDesc constantLayout = {};
constantLayout.bindings = &constantBinding;
constantLayout.bindingCount = 1;
RHI::DescriptorSetLayoutDesc textureLayout = {};
textureLayout.bindings = &textureBinding;
textureLayout.bindingCount = 1;
RHI::DescriptorSetLayoutDesc samplerLayout = {};
samplerLayout.bindings = &samplerBinding;
samplerLayout.bindingCount = 1;
RHI::DescriptorSetLayoutDesc setLayouts[] = {
constantLayout,
textureLayout,
samplerLayout
};
RHI::RHIPipelineLayoutDesc pipelineLayoutDesc = {};
pipelineLayoutDesc.setLayouts = setLayouts;
pipelineLayoutDesc.setLayoutCount = 3;
m_pipelineLayout = m_device->CreatePipelineLayout(pipelineLayoutDesc);
if (m_pipelineLayout == nullptr) {
DestroyResources();
return false;
}
RHI::SamplerDesc samplerDesc = {};
samplerDesc.filter = static_cast<uint32_t>(RHI::FilterMode::Linear);
samplerDesc.addressU = static_cast<uint32_t>(RHI::TextureAddressMode::Clamp);
samplerDesc.addressV = static_cast<uint32_t>(RHI::TextureAddressMode::Clamp);
samplerDesc.addressW = static_cast<uint32_t>(RHI::TextureAddressMode::Clamp);
samplerDesc.mipLodBias = 0.0f;
samplerDesc.maxAnisotropy = 1;
samplerDesc.comparisonFunc = static_cast<uint32_t>(RHI::ComparisonFunc::Always);
samplerDesc.minLod = 0.0f;
samplerDesc.maxLod = 1000.0f;
m_sampler = m_device->CreateSampler(samplerDesc);
if (m_sampler == nullptr) {
DestroyResources();
return false;
}
auto createOwnedDescriptorSet =
[this](const RHI::DescriptorSetLayoutDesc& layout,
RHI::DescriptorHeapType heapType,
bool shaderVisible,
OwnedDescriptorSet& ownedSet) -> bool {
RHI::DescriptorPoolDesc poolDesc = {};
poolDesc.type = heapType;
poolDesc.descriptorCount = 1;
poolDesc.shaderVisible = shaderVisible;
ownedSet.pool = m_device->CreateDescriptorPool(poolDesc);
if (ownedSet.pool == nullptr) {
return false;
}
ownedSet.set = ownedSet.pool->AllocateSet(layout);
if (ownedSet.set == nullptr) {
DestroyOwnedDescriptorSet(ownedSet);
return false;
}
return true;
};
if (!createOwnedDescriptorSet(
constantLayout,
RHI::DescriptorHeapType::CBV_SRV_UAV,
false,
m_constantsSet) ||
!createOwnedDescriptorSet(
textureLayout,
RHI::DescriptorHeapType::CBV_SRV_UAV,
true,
m_textureSet) ||
!createOwnedDescriptorSet(
samplerLayout,
RHI::DescriptorHeapType::Sampler,
true,
m_samplerSet)) {
DestroyResources();
return false;
}
m_samplerSet.set->UpdateSampler(0, m_sampler);
m_pipelineState = m_device->CreatePipelineState(
CreatePipelineDesc(
m_backendType,
m_pipelineLayout,
*m_shader.Get(),
colorScalePass->name,
m_renderTargetFormat));
if (m_pipelineState == nullptr || !m_pipelineState->IsValid()) {
DestroyResources();
return false;
}
return true;
}
void BuiltinColorScalePostProcessPass::DestroyResources() {
m_boundSourceColorView = nullptr;
if (m_pipelineState != nullptr) {
m_pipelineState->Shutdown();
delete m_pipelineState;
m_pipelineState = nullptr;
}
DestroyOwnedDescriptorSet(m_samplerSet);
DestroyOwnedDescriptorSet(m_textureSet);
DestroyOwnedDescriptorSet(m_constantsSet);
if (m_pipelineLayout != nullptr) {
m_pipelineLayout->Shutdown();
delete m_pipelineLayout;
m_pipelineLayout = nullptr;
}
if (m_sampler != nullptr) {
m_sampler->Shutdown();
delete m_sampler;
m_sampler = nullptr;
}
m_shader.Reset();
m_device = nullptr;
m_backendType = RHI::RHIType::D3D12;
m_renderTargetFormat = RHI::Format::Unknown;
}
void BuiltinColorScalePostProcessPass::DestroyOwnedDescriptorSet(OwnedDescriptorSet& descriptorSet) {
if (descriptorSet.set != nullptr) {
descriptorSet.set->Shutdown();
delete descriptorSet.set;
descriptorSet.set = nullptr;
}
if (descriptorSet.pool != nullptr) {
descriptorSet.pool->Shutdown();
delete descriptorSet.pool;
descriptorSet.pool = nullptr;
}
}
} // namespace Passes
} // namespace Rendering
} // namespace XCEngine

View File

@@ -33,6 +33,8 @@ constexpr const char* kBuiltinDepthOnlyShaderPath = "builtin://shaders/depth-onl
constexpr const char* kBuiltinShadowCasterShaderPath = "builtin://shaders/shadow-caster";
constexpr const char* kBuiltinObjectIdShaderPath = "builtin://shaders/object-id";
constexpr const char* kBuiltinSkyboxShaderPath = "builtin://shaders/skybox";
constexpr const char* kBuiltinColorScalePostProcessShaderPath =
"builtin://shaders/color-scale-post-process";
constexpr const char* kBuiltinDefaultPrimitiveTexturePath = "builtin://textures/default-primitive-albedo";
constexpr float kPi = 3.14159265358979323846f;
@@ -55,6 +57,8 @@ constexpr const char* kBuiltinObjectIdShaderManifestRelativePath =
"engine/assets/builtin/shaders/object-id/object-id.shader";
constexpr const char* kBuiltinSkyboxShaderManifestRelativePath =
"engine/assets/builtin/shaders/skybox/skybox.shader";
constexpr const char* kBuiltinColorScalePostProcessShaderManifestRelativePath =
"engine/assets/builtin/shaders/color-scale-post-process/color-scale-post-process.shader";
Containers::String NormalizeBuiltinAssetPath(const std::filesystem::path& path) {
return Containers::String(path.lexically_normal().generic_string().c_str());
@@ -136,6 +140,9 @@ const char* GetBuiltinShaderManifestRelativePath(const Containers::String& built
if (builtinShaderPath == Containers::String(kBuiltinSkyboxShaderPath)) {
return kBuiltinSkyboxShaderManifestRelativePath;
}
if (builtinShaderPath == Containers::String(kBuiltinColorScalePostProcessShaderPath)) {
return kBuiltinColorScalePostProcessShaderManifestRelativePath;
}
return nullptr;
}
@@ -680,6 +687,10 @@ Shader* BuildBuiltinSkyboxShader(const Containers::String& path) {
return TryLoadBuiltinShaderFromManifest(path);
}
Shader* BuildBuiltinColorScalePostProcessShader(const Containers::String& path) {
return TryLoadBuiltinShaderFromManifest(path);
}
Material* BuildDefaultPrimitiveMaterial(const Containers::String& path) {
auto* material = new Material();
IResource::ConstructParams params;
@@ -802,6 +813,10 @@ Containers::String GetBuiltinSkyboxShaderPath() {
return Containers::String(kBuiltinSkyboxShaderPath);
}
Containers::String GetBuiltinColorScalePostProcessShaderPath() {
return Containers::String(kBuiltinColorScalePostProcessShaderPath);
}
Containers::String GetBuiltinDefaultPrimitiveTexturePath() {
return Containers::String(kBuiltinDefaultPrimitiveTexturePath);
}
@@ -905,6 +920,8 @@ LoadResult CreateBuiltinShaderResource(const Containers::String& path) {
shader = BuildBuiltinObjectIdShader(path);
} else if (path == GetBuiltinSkyboxShaderPath()) {
shader = BuildBuiltinSkyboxShader(path);
} else if (path == GetBuiltinColorScalePostProcessShaderPath()) {
shader = BuildBuiltinColorScalePostProcessShader(path);
} else {
return LoadResult(Containers::String("Unknown builtin shader: ") + path);
}

View File

@@ -25,6 +25,7 @@ add_custom_target(rendering_integration_tests
rendering_integration_material_state_scene
rendering_integration_offscreen_scene
rendering_integration_skybox_scene
rendering_integration_post_process_scene
)
add_custom_target(rendering_all_tests

View File

@@ -17,3 +17,4 @@ add_subdirectory(depth_sort_scene)
add_subdirectory(material_state_scene)
add_subdirectory(offscreen_scene)
add_subdirectory(skybox_scene)
add_subdirectory(post_process_scene)

View File

@@ -0,0 +1,63 @@
cmake_minimum_required(VERSION 3.15)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
project(rendering_integration_post_process_scene)
set(ENGINE_ROOT_DIR ${CMAKE_SOURCE_DIR}/engine)
set(PACKAGE_DIR ${CMAKE_SOURCE_DIR}/mvs/OpenGL/package)
get_filename_component(PROJECT_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../.. ABSOLUTE)
find_package(Vulkan QUIET)
add_executable(rendering_integration_post_process_scene
main.cpp
${CMAKE_SOURCE_DIR}/tests/RHI/integration/fixtures/RHIIntegrationFixture.cpp
${PACKAGE_DIR}/src/glad.c
)
target_include_directories(rendering_integration_post_process_scene PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/tests/RHI/integration/fixtures
${ENGINE_ROOT_DIR}/include
${PACKAGE_DIR}/include
${PROJECT_ROOT_DIR}/engine/src
)
target_link_libraries(rendering_integration_post_process_scene PRIVATE
d3d12
dxgi
d3dcompiler
winmm
opengl32
XCEngine
GTest::gtest
)
if(TARGET Vulkan::Vulkan)
target_link_libraries(rendering_integration_post_process_scene PRIVATE Vulkan::Vulkan)
target_compile_definitions(rendering_integration_post_process_scene PRIVATE XCENGINE_SUPPORT_VULKAN)
endif()
target_compile_definitions(rendering_integration_post_process_scene PRIVATE
UNICODE
_UNICODE
XCENGINE_SUPPORT_OPENGL
XCENGINE_SUPPORT_D3D12
)
add_custom_command(TARGET rendering_integration_post_process_scene POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/tests/RHI/integration/compare_ppm.py
$<TARGET_FILE_DIR:rendering_integration_post_process_scene>/
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/GT.ppm
$<TARGET_FILE_DIR:rendering_integration_post_process_scene>/GT.ppm
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ENGINE_ROOT_DIR}/third_party/renderdoc/renderdoc.dll
$<TARGET_FILE_DIR:rendering_integration_post_process_scene>/
)
include(GoogleTest)
gtest_discover_tests(rendering_integration_post_process_scene)

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,415 @@
#include <gtest/gtest.h>
#include "../RenderingIntegrationMain.h"
#include <XCEngine/Components/CameraComponent.h>
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/Components/MeshFilterComponent.h>
#include <XCEngine/Components/MeshRendererComponent.h>
#include <XCEngine/Core/Asset/IResource.h>
#include <XCEngine/Core/Math/Color.h>
#include <XCEngine/Core/Math/Vector2.h>
#include <XCEngine/Core/Math/Vector3.h>
#include <XCEngine/Rendering/Execution/CameraRenderer.h>
#include <XCEngine/Rendering/Passes/BuiltinColorScalePostProcessPass.h>
#include <XCEngine/Rendering/RenderContext.h>
#include <XCEngine/Rendering/RenderSurface.h>
#include <XCEngine/Resources/BuiltinResources.h>
#include <XCEngine/Resources/Material/Material.h>
#include <XCEngine/Resources/Mesh/Mesh.h>
#include <XCEngine/Resources/Shader/Shader.h>
#include <XCEngine/Resources/Texture/Texture.h>
#include <XCEngine/RHI/RHITexture.h>
#include <XCEngine/Scene/Scene.h>
#include "../../../RHI/integration/fixtures/RHIIntegrationFixture.h"
#include <memory>
#include <vector>
using namespace XCEngine::Components;
using namespace XCEngine::Math;
using namespace XCEngine::Rendering;
using namespace XCEngine::Resources;
using namespace XCEngine::RHI;
using namespace XCEngine::RHI::Integration;
namespace {
constexpr const char* kD3D12Screenshot = "post_process_scene_d3d12.ppm";
constexpr const char* kOpenGLScreenshot = "post_process_scene_opengl.ppm";
constexpr const char* kVulkanScreenshot = "post_process_scene_vulkan.ppm";
constexpr uint32_t kFrameWidth = 1280;
constexpr uint32_t kFrameHeight = 720;
Mesh* CreateQuadMesh() {
auto* mesh = new Mesh();
IResource::ConstructParams params = {};
params.name = "PostProcessQuad";
params.path = "Tests/Rendering/PostProcessQuad.mesh";
params.guid = ResourceGUID::Generate(params.path);
mesh->Initialize(params);
StaticMeshVertex vertices[4] = {};
vertices[0].position = Vector3(-1.0f, -1.0f, 0.0f);
vertices[0].uv0 = Vector2(0.0f, 1.0f);
vertices[1].position = Vector3(-1.0f, 1.0f, 0.0f);
vertices[1].uv0 = Vector2(0.0f, 0.0f);
vertices[2].position = Vector3(1.0f, -1.0f, 0.0f);
vertices[2].uv0 = Vector2(1.0f, 1.0f);
vertices[3].position = Vector3(1.0f, 1.0f, 0.0f);
vertices[3].uv0 = Vector2(1.0f, 0.0f);
const uint32_t indices[6] = { 0, 1, 2, 2, 1, 3 };
mesh->SetVertexData(
vertices,
sizeof(vertices),
4,
sizeof(StaticMeshVertex),
VertexAttribute::Position | VertexAttribute::UV0);
mesh->SetIndexData(indices, sizeof(indices), 6, true);
MeshSection section = {};
section.baseVertex = 0;
section.vertexCount = 4;
section.startIndex = 0;
section.indexCount = 6;
section.materialID = 0;
mesh->AddSection(section);
return mesh;
}
Texture* CreateCheckerTexture() {
auto* texture = new Texture();
IResource::ConstructParams params = {};
params.name = "PostProcessChecker";
params.path = "Tests/Rendering/PostProcessChecker.texture";
params.guid = ResourceGUID::Generate(params.path);
texture->Initialize(params);
const unsigned char pixels[16] = {
255, 96, 64, 255,
64, 255, 96, 255,
48, 96, 255, 255,
255, 240, 96, 255
};
texture->Create(
2,
2,
1,
1,
XCEngine::Resources::TextureType::Texture2D,
XCEngine::Resources::TextureFormat::RGBA8_UNORM,
pixels,
sizeof(pixels));
return texture;
}
Material* CreateQuadMaterial(Texture* texture) {
auto* material = new Material();
IResource::ConstructParams params = {};
params.name = "PostProcessQuadMaterial";
params.path = "Tests/Rendering/PostProcessQuad.material";
params.guid = ResourceGUID::Generate(params.path);
material->Initialize(params);
material->SetShader(ResourceManager::Get().Load<Shader>(GetBuiltinUnlitShaderPath()));
material->SetShaderPass("Unlit");
material->SetTexture("_MainTex", ResourceHandle<Texture>(texture));
return material;
}
const char* GetScreenshotFilename(RHIType backendType) {
switch (backendType) {
case RHIType::D3D12:
return kD3D12Screenshot;
case RHIType::Vulkan:
return kVulkanScreenshot;
case RHIType::OpenGL:
default:
return kOpenGLScreenshot;
}
}
int GetComparisonThreshold(RHIType backendType) {
return backendType == RHIType::D3D12 ? 0 : 6;
}
class PostProcessSceneTest : public RHIIntegrationFixture {
protected:
void SetUp() override;
void TearDown() override;
void RenderFrame() override;
private:
void BuildScene();
RHIResourceView* GetCurrentBackBufferView();
std::unique_ptr<Scene> mScene;
std::unique_ptr<CameraRenderer> mCameraRenderer;
RenderPassSequence mPostProcessPasses;
std::vector<RHIResourceView*> mBackBufferViews;
RHITexture* mSceneColorTexture = nullptr;
RHIResourceView* mSceneColorTargetView = nullptr;
RHIResourceView* mSceneColorShaderView = nullptr;
RHITexture* mDepthTexture = nullptr;
RHIResourceView* mDepthView = nullptr;
Mesh* mMesh = nullptr;
Material* mMaterial = nullptr;
Texture* mTexture = nullptr;
CameraComponent* mCamera = nullptr;
};
void PostProcessSceneTest::SetUp() {
RHIIntegrationFixture::SetUp();
mCameraRenderer = std::make_unique<CameraRenderer>();
mScene = std::make_unique<Scene>("PostProcessScene");
mMesh = CreateQuadMesh();
mTexture = CreateCheckerTexture();
mMaterial = CreateQuadMaterial(mTexture);
mPostProcessPasses.AddPass(std::make_unique<Passes::BuiltinColorScalePostProcessPass>(
Vector4(0.55f, 0.72f, 1.0f, 1.0f)));
BuildScene();
TextureDesc sceneColorDesc = {};
sceneColorDesc.width = kFrameWidth;
sceneColorDesc.height = kFrameHeight;
sceneColorDesc.depth = 1;
sceneColorDesc.mipLevels = 1;
sceneColorDesc.arraySize = 1;
sceneColorDesc.format = static_cast<uint32_t>(Format::R8G8B8A8_UNorm);
sceneColorDesc.textureType = static_cast<uint32_t>(XCEngine::RHI::TextureType::Texture2D);
sceneColorDesc.sampleCount = 1;
sceneColorDesc.sampleQuality = 0;
sceneColorDesc.flags = 0;
mSceneColorTexture = GetDevice()->CreateTexture(sceneColorDesc);
ASSERT_NE(mSceneColorTexture, nullptr);
ResourceViewDesc sceneColorViewDesc = {};
sceneColorViewDesc.format = static_cast<uint32_t>(Format::R8G8B8A8_UNorm);
sceneColorViewDesc.dimension = ResourceViewDimension::Texture2D;
sceneColorViewDesc.mipLevel = 0;
mSceneColorTargetView = GetDevice()->CreateRenderTargetView(mSceneColorTexture, sceneColorViewDesc);
ASSERT_NE(mSceneColorTargetView, nullptr);
mSceneColorShaderView = GetDevice()->CreateShaderResourceView(mSceneColorTexture, sceneColorViewDesc);
ASSERT_NE(mSceneColorShaderView, nullptr);
TextureDesc depthDesc = {};
depthDesc.width = kFrameWidth;
depthDesc.height = kFrameHeight;
depthDesc.depth = 1;
depthDesc.mipLevels = 1;
depthDesc.arraySize = 1;
depthDesc.format = static_cast<uint32_t>(Format::D24_UNorm_S8_UInt);
depthDesc.textureType = static_cast<uint32_t>(XCEngine::RHI::TextureType::Texture2D);
depthDesc.sampleCount = 1;
depthDesc.sampleQuality = 0;
depthDesc.flags = 0;
mDepthTexture = GetDevice()->CreateTexture(depthDesc);
ASSERT_NE(mDepthTexture, nullptr);
ResourceViewDesc depthViewDesc = {};
depthViewDesc.format = static_cast<uint32_t>(Format::D24_UNorm_S8_UInt);
depthViewDesc.dimension = ResourceViewDimension::Texture2D;
depthViewDesc.mipLevel = 0;
mDepthView = GetDevice()->CreateDepthStencilView(mDepthTexture, depthViewDesc);
ASSERT_NE(mDepthView, nullptr);
mBackBufferViews.resize(2, nullptr);
}
void PostProcessSceneTest::TearDown() {
if (GetCommandQueue() != nullptr) {
GetCommandQueue()->WaitForIdle();
}
mPostProcessPasses.Shutdown();
mCameraRenderer.reset();
if (mDepthView != nullptr) {
mDepthView->Shutdown();
delete mDepthView;
mDepthView = nullptr;
}
if (mDepthTexture != nullptr) {
mDepthTexture->Shutdown();
delete mDepthTexture;
mDepthTexture = nullptr;
}
if (mSceneColorShaderView != nullptr) {
mSceneColorShaderView->Shutdown();
delete mSceneColorShaderView;
mSceneColorShaderView = nullptr;
}
if (mSceneColorTargetView != nullptr) {
mSceneColorTargetView->Shutdown();
delete mSceneColorTargetView;
mSceneColorTargetView = nullptr;
}
if (mSceneColorTexture != nullptr) {
mSceneColorTexture->Shutdown();
delete mSceneColorTexture;
mSceneColorTexture = nullptr;
}
for (RHIResourceView*& backBufferView : mBackBufferViews) {
if (backBufferView != nullptr) {
backBufferView->Shutdown();
delete backBufferView;
backBufferView = nullptr;
}
}
mBackBufferViews.clear();
mScene.reset();
delete mMaterial;
mMaterial = nullptr;
delete mTexture;
mTexture = nullptr;
delete mMesh;
mMesh = nullptr;
mCamera = nullptr;
RHIIntegrationFixture::TearDown();
}
void PostProcessSceneTest::BuildScene() {
ASSERT_NE(mScene, nullptr);
GameObject* cameraObject = mScene->CreateGameObject("MainCamera");
mCamera = cameraObject->AddComponent<CameraComponent>();
ASSERT_NE(mCamera, nullptr);
mCamera->SetPrimary(true);
mCamera->SetFieldOfView(50.0f);
mCamera->SetNearClipPlane(0.1f);
mCamera->SetFarClipPlane(20.0f);
mCamera->SetClearColor(XCEngine::Math::Color(0.06f, 0.08f, 0.12f, 1.0f));
GameObject* quadObject = mScene->CreateGameObject("Quad");
quadObject->GetTransform()->SetLocalPosition(Vector3(0.0f, 0.0f, 3.0f));
quadObject->GetTransform()->SetLocalScale(Vector3(1.6f, 1.6f, 1.0f));
auto* meshFilter = quadObject->AddComponent<MeshFilterComponent>();
auto* meshRenderer = quadObject->AddComponent<MeshRendererComponent>();
meshFilter->SetMesh(ResourceHandle<Mesh>(mMesh));
meshRenderer->SetMaterial(0, ResourceHandle<Material>(mMaterial));
}
RHIResourceView* PostProcessSceneTest::GetCurrentBackBufferView() {
const int backBufferIndex = GetCurrentBackBufferIndex();
if (backBufferIndex < 0) {
return nullptr;
}
if (static_cast<size_t>(backBufferIndex) >= mBackBufferViews.size()) {
mBackBufferViews.resize(static_cast<size_t>(backBufferIndex) + 1, nullptr);
}
if (mBackBufferViews[backBufferIndex] == nullptr) {
ResourceViewDesc viewDesc = {};
viewDesc.format = static_cast<uint32_t>(Format::R8G8B8A8_UNorm);
viewDesc.dimension = ResourceViewDimension::Texture2D;
viewDesc.mipLevel = 0;
mBackBufferViews[backBufferIndex] = GetDevice()->CreateRenderTargetView(GetCurrentBackBuffer(), viewDesc);
}
return mBackBufferViews[backBufferIndex];
}
void PostProcessSceneTest::RenderFrame() {
ASSERT_NE(mScene, nullptr);
ASSERT_NE(mCameraRenderer, nullptr);
ASSERT_NE(mCamera, nullptr);
ASSERT_NE(mSceneColorTargetView, nullptr);
ASSERT_NE(mSceneColorShaderView, nullptr);
ASSERT_NE(mDepthView, nullptr);
RHICommandList* commandList = GetCommandList();
ASSERT_NE(commandList, nullptr);
RHIResourceView* backBufferView = GetCurrentBackBufferView();
ASSERT_NE(backBufferView, nullptr);
commandList->Reset();
RenderSurface mainSceneSurface(kFrameWidth, kFrameHeight);
mainSceneSurface.SetColorAttachment(mSceneColorTargetView);
mainSceneSurface.SetDepthAttachment(mDepthView);
mainSceneSurface.SetColorStateBefore(ResourceStates::Common);
mainSceneSurface.SetColorStateAfter(ResourceStates::PixelShaderResource);
RenderSurface finalSurface(kFrameWidth, kFrameHeight);
finalSurface.SetColorAttachment(backBufferView);
finalSurface.SetColorStateBefore(ResourceStates::Present);
finalSurface.SetColorStateAfter(ResourceStates::Present);
RenderContext renderContext = {};
renderContext.device = GetDevice();
renderContext.commandList = commandList;
renderContext.commandQueue = GetCommandQueue();
renderContext.backendType = GetBackendType();
CameraRenderRequest request = {};
request.scene = mScene.get();
request.camera = mCamera;
request.context = renderContext;
request.surface = finalSurface;
request.postProcess.sourceSurface = mainSceneSurface;
request.postProcess.sourceColorView = mSceneColorShaderView;
request.postProcess.destinationSurface = finalSurface;
request.postProcess.passes = &mPostProcessPasses;
ASSERT_TRUE(mCameraRenderer->Render(request));
commandList->Close();
void* commandLists[] = { commandList };
GetCommandQueue()->ExecuteCommandLists(1, commandLists);
}
TEST_P(PostProcessSceneTest, RenderPostProcessScene) {
RHICommandQueue* commandQueue = GetCommandQueue();
RHISwapChain* swapChain = GetSwapChain();
const int targetFrameCount = 30;
const char* screenshotFilename = GetScreenshotFilename(GetBackendType());
const int comparisonThreshold = GetComparisonThreshold(GetBackendType());
for (int frameCount = 0; frameCount <= targetFrameCount; ++frameCount) {
if (frameCount > 0) {
commandQueue->WaitForPreviousFrame();
}
BeginRender();
RenderFrame();
if (frameCount >= targetFrameCount) {
commandQueue->WaitForIdle();
ASSERT_TRUE(TakeScreenshot(screenshotFilename));
ASSERT_TRUE(CompareWithGoldenTemplate(
screenshotFilename,
"GT.ppm",
static_cast<float>(comparisonThreshold)));
break;
}
swapChain->Present(0, 0);
}
}
} // namespace
INSTANTIATE_TEST_SUITE_P(D3D12, PostProcessSceneTest, ::testing::Values(RHIType::D3D12));
INSTANTIATE_TEST_SUITE_P(OpenGL, PostProcessSceneTest, ::testing::Values(RHIType::OpenGL));
#if defined(XCENGINE_SUPPORT_VULKAN)
INSTANTIATE_TEST_SUITE_P(Vulkan, PostProcessSceneTest, ::testing::Values(RHIType::Vulkan));
#endif
GTEST_API_ int main(int argc, char** argv) {
return RunRenderingIntegrationTestMain(argc, argv);
}