312 lines
9.9 KiB
C++
312 lines
9.9 KiB
C++
#define NOMINMAX
|
|
#include <windows.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "../RenderingIntegrationMain.h"
|
|
|
|
#include <XCEngine/Components/CameraComponent.h>
|
|
#include <XCEngine/Components/GameObject.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/Core/Math/Vector4.h>
|
|
#include <XCEngine/Rendering/Execution/SceneRenderer.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/Shader/Shader.h>
|
|
#include <XCEngine/Resources/Texture/Texture.h>
|
|
#include <XCEngine/Resources/Texture/TextureImportSettings.h>
|
|
#include <XCEngine/Resources/Texture/TextureLoader.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::Math;
|
|
using namespace XCEngine::Rendering;
|
|
using namespace XCEngine::Resources;
|
|
using namespace XCEngine::RHI;
|
|
using namespace XCEngine::RHI::Integration;
|
|
|
|
namespace {
|
|
|
|
constexpr const char* kD3D12Screenshot = "skybox_scene_d3d12.ppm";
|
|
constexpr const char* kOpenGLScreenshot = "skybox_scene_opengl.ppm";
|
|
constexpr const char* kVulkanScreenshot = "skybox_scene_vulkan.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;
|
|
}
|
|
|
|
Texture* LoadPanoramicSkyboxTexture(const std::filesystem::path& panoramaPath) {
|
|
TextureLoader loader;
|
|
TextureImportSettings settings;
|
|
settings.SetSRGB(true);
|
|
|
|
LoadResult loadResult = loader.Load(panoramaPath.string().c_str(), &settings);
|
|
if (!loadResult || loadResult.resource == nullptr) {
|
|
return nullptr;
|
|
}
|
|
|
|
auto* texture = static_cast<Texture*>(loadResult.resource);
|
|
if (texture == nullptr ||
|
|
texture->GetPixelData() == nullptr ||
|
|
texture->GetWidth() == 0 ||
|
|
texture->GetHeight() == 0) {
|
|
delete texture;
|
|
return nullptr;
|
|
}
|
|
|
|
return texture;
|
|
}
|
|
|
|
Material* CreateSkyboxMaterial(Texture* texture) {
|
|
auto* material = new Material();
|
|
IResource::ConstructParams params = {};
|
|
params.name = "SkyboxPanoramicMaterial";
|
|
params.path = "Tests/Rendering/SkyboxScene/skybox_panorama.mat";
|
|
params.guid = ResourceGUID::Generate(params.path);
|
|
material->Initialize(params);
|
|
material->SetShader(ResourceManager::Get().Load<Shader>(GetBuiltinSkyboxShaderPath()));
|
|
material->SetTexture("_MainTex", ResourceHandle<Texture>(texture));
|
|
material->SetFloat4("_Tint", Vector4(1.0f, 1.0f, 1.0f, 1.0f));
|
|
material->SetFloat("_Exposure", 1.0f);
|
|
material->SetFloat("_Rotation", -90.0f);
|
|
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) {
|
|
(void)backendType;
|
|
return 10;
|
|
}
|
|
|
|
class SkyboxSceneTest : 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<SceneRenderer> mSceneRenderer;
|
|
std::vector<RHIResourceView*> mBackBufferViews;
|
|
std::vector<Material*> mMaterials;
|
|
std::vector<Texture*> mTextures;
|
|
RHITexture* mDepthTexture = nullptr;
|
|
RHIResourceView* mDepthView = nullptr;
|
|
};
|
|
|
|
void SkyboxSceneTest::SetUp() {
|
|
RHIIntegrationFixture::SetUp();
|
|
|
|
mSceneRenderer = std::make_unique<SceneRenderer>();
|
|
mScene = std::make_unique<Scene>("SkyboxScene");
|
|
|
|
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 SkyboxSceneTest::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();
|
|
|
|
for (Material* material : mMaterials) {
|
|
delete material;
|
|
}
|
|
mMaterials.clear();
|
|
|
|
for (Texture* texture : mTextures) {
|
|
delete texture;
|
|
}
|
|
mTextures.clear();
|
|
RHIIntegrationFixture::TearDown();
|
|
}
|
|
|
|
void SkyboxSceneTest::BuildScene() {
|
|
ASSERT_NE(mScene, nullptr);
|
|
|
|
GameObject* cameraObject = mScene->CreateGameObject("MainCamera");
|
|
auto* camera = cameraObject->AddComponent<CameraComponent>();
|
|
camera->SetPrimary(true);
|
|
camera->SetProjectionType(CameraProjectionType::Perspective);
|
|
camera->SetFieldOfView(90.0f);
|
|
camera->SetNearClipPlane(0.1f);
|
|
camera->SetFarClipPlane(20.0f);
|
|
camera->SetClearColor(XCEngine::Math::Color(0.01f, 0.01f, 0.01f, 1.0f));
|
|
camera->SetSkyboxEnabled(true);
|
|
Texture* skyboxTexture = LoadPanoramicSkyboxTexture(ResolveRuntimePath("Res/skyboxes/sky.png"));
|
|
ASSERT_NE(skyboxTexture, nullptr);
|
|
Material* skyboxMaterial = CreateSkyboxMaterial(skyboxTexture);
|
|
ASSERT_NE(skyboxMaterial, nullptr);
|
|
camera->SetSkyboxMaterial(skyboxMaterial);
|
|
|
|
mMaterials = { skyboxMaterial };
|
|
mTextures = { skyboxTexture };
|
|
}
|
|
|
|
RHIResourceView* SkyboxSceneTest::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 SkyboxSceneTest::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(SkyboxSceneTest, RenderSkyboxScene) {
|
|
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, SkyboxSceneTest, ::testing::Values(RHIType::D3D12));
|
|
INSTANTIATE_TEST_SUITE_P(OpenGL, SkyboxSceneTest, ::testing::Values(RHIType::OpenGL));
|
|
#if defined(XCENGINE_SUPPORT_VULKAN)
|
|
INSTANTIATE_TEST_SUITE_P(Vulkan, SkyboxSceneTest, ::testing::Values(RHIType::Vulkan));
|
|
#endif
|
|
|
|
GTEST_API_ int main(int argc, char** argv) {
|
|
return RunRenderingIntegrationTestMain(argc, argv);
|
|
}
|