feat: add basic forward lighting coverage

This commit is contained in:
2026-04-01 00:41:56 +08:00
parent ac2b7c1fa2
commit 3373119eee
10 changed files with 31741 additions and 7 deletions

View File

@@ -59,6 +59,9 @@ private:
Math::Matrix4x4 projection = Math::Matrix4x4::Identity();
Math::Matrix4x4 view = Math::Matrix4x4::Identity();
Math::Matrix4x4 model = Math::Matrix4x4::Identity();
Math::Matrix4x4 normalMatrix = Math::Matrix4x4::Identity();
Math::Vector4 mainLightDirectionAndIntensity = Math::Vector4::Zero();
Math::Vector4 mainLightColorAndFlags = Math::Vector4::Zero();
};
bool EnsureInitialized(const RenderContext& context);

View File

@@ -16,9 +16,25 @@ class Scene;
namespace Rendering {
struct RenderDirectionalLightData {
bool enabled = false;
Math::Vector3 direction = Math::Vector3::Back();
float intensity = 1.0f;
Math::Color color = Math::Color::White();
};
struct RenderLightingData {
RenderDirectionalLightData mainDirectionalLight;
bool HasMainDirectionalLight() const {
return mainDirectionalLight.enabled;
}
};
struct RenderSceneData {
Components::CameraComponent* camera = nullptr;
RenderCameraData cameraData;
RenderLightingData lighting;
std::vector<VisibleRenderItem> visibleItems;
bool HasCamera() const { return camera != nullptr; }
@@ -45,6 +61,9 @@ private:
const Components::CameraComponent& camera,
uint32_t viewportWidth,
uint32_t viewportHeight) const;
void ExtractLighting(
const Components::Scene& scene,
RenderLightingData& lightingData) const;
void ExtractVisibleItems(
Components::GameObject* gameObject,
const Math::Vector3& cameraPosition,

View File

@@ -59,16 +59,21 @@ cbuffer PerObjectConstants : register(b1) {
float4x4 gProjectionMatrix;
float4x4 gViewMatrix;
float4x4 gModelMatrix;
float4x4 gNormalMatrix;
float4 gMainLightDirectionAndIntensity;
float4 gMainLightColorAndFlags;
};
struct VSInput {
float3 position : POSITION;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
struct PSInput {
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
float3 normalWS : TEXCOORD0;
float2 texcoord : TEXCOORD1;
};
PSInput MainVS(VSInput input) {
@@ -76,31 +81,48 @@ PSInput MainVS(VSInput input) {
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;
}
float4 MainPS(PSInput input) : SV_TARGET {
return gBaseColorTexture.Sample(gLinearSampler, input.texcoord);
float4 baseColor = gBaseColorTexture.Sample(gLinearSampler, input.texcoord);
if (gMainLightColorAndFlags.a < 0.5f) {
return baseColor;
}
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 vec2 aTexCoord;
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;
}
)";
@@ -108,12 +130,33 @@ void main() {
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;
};
in vec3 vNormalWS;
in vec2 vTexCoord;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = texture(uBaseColorTexture, vTexCoord);
vec4 baseColor = texture(uBaseColorTexture, vTexCoord);
if (gMainLightColorAndFlags.w < 0.5) {
fragColor = baseColor;
return;
}
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);
}
)";
@@ -204,6 +247,14 @@ RHI::InputLayoutDesc BuiltinForwardPipeline::BuildInputLayout() {
position.alignedByteOffset = 0;
inputLayout.elements.push_back(position);
RHI::InputElementDesc normal = {};
normal.semanticName = "NORMAL";
normal.semanticIndex = 0;
normal.format = static_cast<uint32_t>(RHI::Format::R32G32B32_Float);
normal.inputSlot = 0;
normal.alignedByteOffset = static_cast<uint32_t>(offsetof(Resources::StaticMeshVertex, normal));
inputLayout.elements.push_back(normal);
RHI::InputElementDesc texcoord = {};
texcoord.semanticName = "TEXCOORD";
texcoord.semanticIndex = 0;
@@ -689,7 +740,22 @@ bool BuiltinForwardPipeline::DrawVisibleItem(
const PerObjectConstants constants = {
sceneData.cameraData.projection,
sceneData.cameraData.view,
visibleItem.localToWorld.Transpose()
visibleItem.localToWorld.Transpose(),
visibleItem.localToWorld.Inverse(),
sceneData.lighting.HasMainDirectionalLight()
? Math::Vector4(
sceneData.lighting.mainDirectionalLight.direction.x,
sceneData.lighting.mainDirectionalLight.direction.y,
sceneData.lighting.mainDirectionalLight.direction.z,
sceneData.lighting.mainDirectionalLight.intensity)
: Math::Vector4::Zero(),
sceneData.lighting.HasMainDirectionalLight()
? Math::Vector4(
sceneData.lighting.mainDirectionalLight.color.r,
sceneData.lighting.mainDirectionalLight.color.g,
sceneData.lighting.mainDirectionalLight.color.b,
1.0f)
: Math::Vector4::Zero()
};
RHI::RHIResourceView* textureView = ResolveTextureView(visibleItem);

View File

@@ -2,6 +2,7 @@
#include "Components/CameraComponent.h"
#include "Components/GameObject.h"
#include "Components/LightComponent.h"
#include "Components/MeshFilterComponent.h"
#include "Components/MeshRendererComponent.h"
#include "Components/TransformComponent.h"
@@ -22,6 +23,13 @@ bool IsUsableCamera(const Components::CameraComponent* camera) {
camera->GetGameObject()->IsActiveInHierarchy();
}
bool IsUsableLight(const Components::LightComponent* light) {
return light != nullptr &&
light->IsEnabled() &&
light->GetGameObject() != nullptr &&
light->GetGameObject()->IsActiveInHierarchy();
}
bool CompareVisibleItems(const VisibleRenderItem& lhs, const VisibleRenderItem& rhs) {
if (lhs.renderQueue != rhs.renderQueue) {
return lhs.renderQueue < rhs.renderQueue;
@@ -66,6 +74,7 @@ RenderSceneData RenderSceneExtractor::Extract(
sceneData.visibleItems.begin(),
sceneData.visibleItems.end(),
CompareVisibleItems);
ExtractLighting(scene, sceneData.lighting);
return sceneData;
}
@@ -93,6 +102,7 @@ RenderSceneData RenderSceneExtractor::ExtractForCamera(
sceneData.visibleItems.begin(),
sceneData.visibleItems.end(),
CompareVisibleItems);
ExtractLighting(scene, sceneData.lighting);
return sceneData;
}
@@ -171,6 +181,46 @@ RenderCameraData RenderSceneExtractor::BuildCameraData(
return cameraData;
}
void RenderSceneExtractor::ExtractLighting(
const Components::Scene& scene,
RenderLightingData& lightingData) const {
const std::vector<Components::LightComponent*> lights =
scene.FindObjectsOfType<Components::LightComponent>();
Components::LightComponent* mainDirectionalLight = nullptr;
for (Components::LightComponent* light : lights) {
if (!IsUsableLight(light) ||
light->GetLightType() != Components::LightType::Directional) {
continue;
}
if (mainDirectionalLight == nullptr ||
light->GetIntensity() > mainDirectionalLight->GetIntensity()) {
mainDirectionalLight = light;
}
}
if (mainDirectionalLight == nullptr) {
lightingData = {};
return;
}
RenderDirectionalLightData lightData;
lightData.enabled = true;
lightData.intensity = mainDirectionalLight->GetIntensity();
lightData.color = mainDirectionalLight->GetColor();
Math::Vector3 direction = mainDirectionalLight->transform().GetForward() * -1.0f;
if (direction.SqrMagnitude() <= Math::EPSILON) {
direction = Math::Vector3::Back();
} else {
direction = direction.Normalized();
}
lightData.direction = direction;
lightingData.mainDirectionalLight = lightData;
}
void RenderSceneExtractor::ExtractVisibleItems(
Components::GameObject* gameObject,
const Math::Vector3& cameraPosition,

View File

@@ -4,6 +4,7 @@ project(XCEngine_RenderingIntegrationTests)
add_subdirectory(textured_quad_scene)
add_subdirectory(backpack_scene)
add_subdirectory(backpack_lit_scene)
add_subdirectory(transparent_material_scene)
add_subdirectory(cull_material_scene)
add_subdirectory(depth_sort_scene)

View File

@@ -0,0 +1,62 @@
cmake_minimum_required(VERSION 3.15)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
project(rendering_integration_backpack_lit_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)
add_executable(rendering_integration_backpack_lit_scene
main.cpp
${CMAKE_SOURCE_DIR}/tests/RHI/integration/fixtures/RHIIntegrationFixture.cpp
${PACKAGE_DIR}/src/glad.c
)
target_include_directories(rendering_integration_backpack_lit_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_backpack_lit_scene PRIVATE
d3d12
dxgi
d3dcompiler
winmm
opengl32
XCEngine
GTest::gtest
)
target_compile_definitions(rendering_integration_backpack_lit_scene PRIVATE
UNICODE
_UNICODE
XCENGINE_SUPPORT_OPENGL
XCENGINE_SUPPORT_D3D12
)
add_custom_command(TARGET rendering_integration_backpack_lit_scene POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/tests/RHI/integration/backpack/Res
$<TARGET_FILE_DIR:rendering_integration_backpack_lit_scene>/Res
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/tests/RHI/integration/compare_ppm.py
$<TARGET_FILE_DIR:rendering_integration_backpack_lit_scene>/
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/GT.ppm
$<TARGET_FILE_DIR:rendering_integration_backpack_lit_scene>/GT.ppm
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ENGINE_ROOT_DIR}/third_party/renderdoc/renderdoc.dll
$<TARGET_FILE_DIR:rendering_integration_backpack_lit_scene>/
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ENGINE_ROOT_DIR}/third_party/assimp/bin/assimp-vc143-mt.dll
$<TARGET_FILE_DIR:rendering_integration_backpack_lit_scene>/
)
include(GoogleTest)
gtest_discover_tests(rendering_integration_backpack_lit_scene)

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,319 @@
#define NOMINMAX
#include <windows.h>
#include <gtest/gtest.h>
#include <XCEngine/Components/CameraComponent.h>
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/Components/LightComponent.h>
#include <XCEngine/Components/MeshFilterComponent.h>
#include <XCEngine/Components/MeshRendererComponent.h>
#include <XCEngine/Core/Math/Color.h>
#include <XCEngine/Core/Math/Quaternion.h>
#include <XCEngine/Core/Math/Vector3.h>
#include <XCEngine/Debug/ConsoleLogSink.h>
#include <XCEngine/Debug/Logger.h>
#include <XCEngine/Rendering/RenderContext.h>
#include <XCEngine/Rendering/RenderSurface.h>
#include <XCEngine/Rendering/SceneRenderer.h>
#include <XCEngine/Resources/Mesh/Mesh.h>
#include <XCEngine/Resources/Mesh/MeshImportSettings.h>
#include <XCEngine/Resources/Mesh/MeshLoader.h>
#include <XCEngine/RHI/RHITexture.h>
#include <XCEngine/Scene/Scene.h>
#include "../../../RHI/integration/fixtures/RHIIntegrationFixture.h"
#include <algorithm>
#include <filesystem>
#include <memory>
#include <vector>
using namespace XCEngine::Components;
using namespace XCEngine::Debug;
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 = "backpack_lit_scene_d3d12.ppm";
constexpr const char* kOpenGLScreenshot = "backpack_lit_scene_opengl.ppm";
constexpr uint32_t kFrameWidth = 1280;
constexpr uint32_t kFrameHeight = 720;
std::filesystem::path GetExecutableDirectory() {
char exePath[MAX_PATH] = {};
const DWORD length = GetModuleFileNameA(nullptr, exePath, MAX_PATH);
if (length == 0 || length >= MAX_PATH) {
return std::filesystem::current_path();
}
return std::filesystem::path(exePath).parent_path();
}
std::filesystem::path ResolveRuntimePath(const char* relativePath) {
return GetExecutableDirectory() / relativePath;
}
const char* GetScreenshotFilename(RHIType backendType) {
return backendType == RHIType::D3D12 ? kD3D12Screenshot : kOpenGLScreenshot;
}
int GetComparisonThreshold(RHIType backendType) {
return backendType == RHIType::OpenGL ? 10 : 10;
}
class BackpackLitSceneTest : public RHIIntegrationFixture {
protected:
void SetUp() override;
void TearDown() override;
void RenderFrame() override;
private:
void LoadBackpackMesh();
void BuildScene();
RHIResourceView* GetCurrentBackBufferView();
std::unique_ptr<Scene> mScene;
std::unique_ptr<SceneRenderer> mSceneRenderer;
std::vector<RHIResourceView*> mBackBufferViews;
RHITexture* mDepthTexture = nullptr;
RHIResourceView* mDepthView = nullptr;
Mesh* mMesh = nullptr;
};
void BackpackLitSceneTest::SetUp() {
RHIIntegrationFixture::SetUp();
mSceneRenderer = std::make_unique<SceneRenderer>();
mScene = std::make_unique<Scene>("BackpackLitScene");
LoadBackpackMesh();
BuildScene();
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 BackpackLitSceneTest::TearDown() {
mSceneRenderer.reset();
if (mDepthView != nullptr) {
mDepthView->Shutdown();
delete mDepthView;
mDepthView = nullptr;
}
if (mDepthTexture != nullptr) {
mDepthTexture->Shutdown();
delete mDepthTexture;
mDepthTexture = nullptr;
}
for (RHIResourceView*& backBufferView : mBackBufferViews) {
if (backBufferView != nullptr) {
backBufferView->Shutdown();
delete backBufferView;
backBufferView = nullptr;
}
}
mBackBufferViews.clear();
mScene.reset();
if (mMesh != nullptr) {
for (size_t materialIndex = 0; materialIndex < mMesh->GetMaterials().Size(); ++materialIndex) {
delete mMesh->GetMaterials()[materialIndex];
}
for (size_t textureIndex = 0; textureIndex < mMesh->GetTextures().Size(); ++textureIndex) {
delete mMesh->GetTextures()[textureIndex];
}
delete mMesh;
mMesh = nullptr;
}
RHIIntegrationFixture::TearDown();
}
void BackpackLitSceneTest::LoadBackpackMesh() {
const std::filesystem::path meshPath = ResolveRuntimePath("Res/models/backpack/backpack.obj");
MeshLoader loader;
MeshImportSettings settings;
LoadResult result = loader.Load(meshPath.string().c_str(), &settings);
ASSERT_TRUE(result);
ASSERT_NE(result.resource, nullptr);
mMesh = static_cast<Mesh*>(result.resource);
ASSERT_NE(mMesh, nullptr);
ASSERT_TRUE(mMesh->IsValid());
ASSERT_GT(mMesh->GetVertexCount(), 0u);
ASSERT_GT(mMesh->GetSections().Size(), 0u);
}
void BackpackLitSceneTest::BuildScene() {
ASSERT_NE(mScene, nullptr);
ASSERT_NE(mMesh, nullptr);
GameObject* cameraObject = mScene->CreateGameObject("MainCamera");
auto* camera = cameraObject->AddComponent<CameraComponent>();
camera->SetPrimary(true);
camera->SetFieldOfView(45.0f);
camera->SetNearClipPlane(0.1f);
camera->SetFarClipPlane(100.0f);
camera->SetClearColor(XCEngine::Math::Color(0.03f, 0.03f, 0.05f, 1.0f));
GameObject* mainLightObject = mScene->CreateGameObject("MainDirectionalLight");
auto* mainLight = mainLightObject->AddComponent<LightComponent>();
mainLight->SetLightType(LightType::Directional);
mainLight->SetColor(XCEngine::Math::Color(1.0f, 1.0f, 0.97f, 1.0f));
mainLight->SetIntensity(1.8f);
mainLightObject->GetTransform()->SetLocalRotation(
Quaternion::LookRotation(Vector3(-0.15f, -0.75f, -0.65f).Normalized()));
const Vector3 boundsMin = mMesh->GetBounds().GetMin();
const Vector3 boundsMax = mMesh->GetBounds().GetMax();
const Vector3 center(
(boundsMin.x + boundsMax.x) * 0.5f,
(boundsMin.y + boundsMax.y) * 0.5f,
(boundsMin.z + boundsMax.z) * 0.5f);
const float sizeX = std::max(boundsMax.x - boundsMin.x, 0.001f);
const float sizeY = std::max(boundsMax.y - boundsMin.y, 0.001f);
const float sizeZ = std::max(boundsMax.z - boundsMin.z, 0.001f);
const float maxExtent = std::max(sizeX, std::max(sizeY, sizeZ));
const float uniformScale = 1.8f / maxExtent;
GameObject* root = mScene->CreateGameObject("BackpackRoot");
root->GetTransform()->SetLocalPosition(Vector3(0.0f, 0.08f, 3.0f));
GameObject* rotateY = mScene->CreateGameObject("BackpackRotateY");
rotateY->SetParent(root, false);
rotateY->GetTransform()->SetLocalRotation(Quaternion::FromAxisAngle(Vector3::Up(), -0.35f));
GameObject* rotateX = mScene->CreateGameObject("BackpackRotateX");
rotateX->SetParent(rotateY, false);
rotateX->GetTransform()->SetLocalRotation(Quaternion::FromAxisAngle(Vector3::Right(), -0.18f));
GameObject* scale = mScene->CreateGameObject("BackpackScale");
scale->SetParent(rotateX, false);
scale->GetTransform()->SetLocalScale(Vector3(uniformScale, uniformScale, uniformScale));
GameObject* meshObject = mScene->CreateGameObject("BackpackMesh");
meshObject->SetParent(scale, false);
meshObject->GetTransform()->SetLocalPosition(Vector3(-center.x, -center.y, -center.z));
auto* meshFilter = meshObject->AddComponent<MeshFilterComponent>();
auto* meshRenderer = meshObject->AddComponent<MeshRendererComponent>();
meshFilter->SetMesh(ResourceHandle<Mesh>(mMesh));
meshRenderer->ClearMaterials();
}
RHIResourceView* BackpackLitSceneTest::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 BackpackLitSceneTest::RenderFrame() {
ASSERT_NE(mScene, nullptr);
ASSERT_NE(mSceneRenderer, nullptr);
RHICommandList* commandList = GetCommandList();
ASSERT_NE(commandList, nullptr);
commandList->Reset();
RenderSurface surface(kFrameWidth, kFrameHeight);
surface.SetColorAttachment(GetCurrentBackBufferView());
surface.SetDepthAttachment(mDepthView);
RenderContext renderContext = {};
renderContext.device = GetDevice();
renderContext.commandList = commandList;
renderContext.commandQueue = GetCommandQueue();
renderContext.backendType = GetBackendType();
ASSERT_TRUE(mSceneRenderer->Render(*mScene, nullptr, renderContext, surface));
commandList->Close();
void* commandLists[] = { commandList };
GetCommandQueue()->ExecuteCommandLists(1, commandLists);
}
TEST_P(BackpackLitSceneTest, RenderBackpackLitScene) {
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, BackpackLitSceneTest, ::testing::Values(RHIType::D3D12));
INSTANTIATE_TEST_SUITE_P(OpenGL, BackpackLitSceneTest, ::testing::Values(RHIType::OpenGL));
GTEST_API_ int main(int argc, char** argv) {
Logger::Get().Initialize();
Logger::Get().AddSink(std::make_unique<ConsoleLogSink>());
Logger::Get().SetMinimumLevel(LogLevel::Debug);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -11,7 +11,7 @@ using namespace XCEngine::RHI;
TEST(BuiltinForwardPipeline_Test, UsesFloat3PositionInputLayoutForStaticMeshVertices) {
const InputLayoutDesc inputLayout = BuiltinForwardPipeline::BuildInputLayout();
ASSERT_EQ(inputLayout.elements.size(), 2u);
ASSERT_EQ(inputLayout.elements.size(), 3u);
const InputElementDesc& position = inputLayout.elements[0];
EXPECT_EQ(position.semanticName, "POSITION");
@@ -20,7 +20,14 @@ TEST(BuiltinForwardPipeline_Test, UsesFloat3PositionInputLayoutForStaticMeshVert
EXPECT_EQ(position.inputSlot, 0u);
EXPECT_EQ(position.alignedByteOffset, 0u);
const InputElementDesc& texcoord = inputLayout.elements[1];
const InputElementDesc& normal = inputLayout.elements[1];
EXPECT_EQ(normal.semanticName, "NORMAL");
EXPECT_EQ(normal.semanticIndex, 0u);
EXPECT_EQ(normal.format, static_cast<uint32_t>(Format::R32G32B32_Float));
EXPECT_EQ(normal.inputSlot, 0u);
EXPECT_EQ(normal.alignedByteOffset, static_cast<uint32_t>(offsetof(StaticMeshVertex, normal)));
const InputElementDesc& texcoord = inputLayout.elements[2];
EXPECT_EQ(texcoord.semanticName, "TEXCOORD");
EXPECT_EQ(texcoord.semanticIndex, 0u);
EXPECT_EQ(texcoord.format, static_cast<uint32_t>(Format::R32G32_Float));

View File

@@ -2,9 +2,12 @@
#include <XCEngine/Components/CameraComponent.h>
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/Components/LightComponent.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/Quaternion.h>
#include <XCEngine/Core/Math/Vector3.h>
#include <XCEngine/Rendering/RenderMaterialUtility.h>
#include <XCEngine/Rendering/RenderSceneExtractor.h>
@@ -137,6 +140,46 @@ TEST(RenderSceneExtractor_Test, OverrideCameraTakesPriority) {
EXPECT_EQ(sceneData.cameraData.viewportHeight, 480u);
}
TEST(RenderSceneExtractor_Test, ExtractsBrightestDirectionalLightAsMainLight) {
Scene scene("LightingScene");
GameObject* cameraObject = scene.CreateGameObject("Camera");
auto* camera = cameraObject->AddComponent<CameraComponent>();
camera->SetPrimary(true);
GameObject* fillLightObject = scene.CreateGameObject("FillLight");
auto* fillLight = fillLightObject->AddComponent<LightComponent>();
fillLight->SetLightType(LightType::Directional);
fillLight->SetColor(Color(0.2f, 0.4f, 0.8f, 1.0f));
fillLight->SetIntensity(0.5f);
GameObject* pointLightObject = scene.CreateGameObject("PointLight");
auto* pointLight = pointLightObject->AddComponent<LightComponent>();
pointLight->SetLightType(LightType::Point);
pointLight->SetIntensity(10.0f);
GameObject* mainLightObject = scene.CreateGameObject("MainLight");
auto* mainLight = mainLightObject->AddComponent<LightComponent>();
mainLight->SetLightType(LightType::Directional);
mainLight->SetColor(Color(1.0f, 0.8f, 0.6f, 1.0f));
mainLight->SetIntensity(2.5f);
mainLightObject->GetTransform()->SetLocalRotation(
Quaternion::LookRotation(Vector3(-0.3f, -1.0f, -0.2f).Normalized()));
RenderSceneExtractor extractor;
const RenderSceneData sceneData = extractor.Extract(scene, nullptr, 800, 600);
ASSERT_TRUE(sceneData.HasCamera());
ASSERT_TRUE(sceneData.lighting.HasMainDirectionalLight());
EXPECT_FLOAT_EQ(sceneData.lighting.mainDirectionalLight.intensity, 2.5f);
EXPECT_EQ(sceneData.lighting.mainDirectionalLight.color.r, 1.0f);
EXPECT_EQ(sceneData.lighting.mainDirectionalLight.color.g, 0.8f);
EXPECT_EQ(sceneData.lighting.mainDirectionalLight.color.b, 0.6f);
EXPECT_EQ(
sceneData.lighting.mainDirectionalLight.direction,
mainLightObject->GetTransform()->GetForward().Normalized() * -1.0f);
}
TEST(RenderSceneExtractor_Test, ExtractsSectionLevelVisibleItemsAndSortsByRenderQueue) {
Scene scene("SectionScene");