Fix D3D12 pipeline format mapping and add transparent material scene test
This commit is contained in:
@@ -15,6 +15,10 @@
|
||||
#include <unordered_map>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Components {
|
||||
class GameObject;
|
||||
} // namespace Components
|
||||
|
||||
namespace Resources {
|
||||
class Material;
|
||||
class Texture;
|
||||
@@ -38,6 +42,11 @@ public:
|
||||
const RenderSceneData& sceneData) override;
|
||||
|
||||
private:
|
||||
struct OwnedDescriptorSet {
|
||||
RHI::RHIDescriptorPool* pool = nullptr;
|
||||
RHI::RHIDescriptorSet* set = nullptr;
|
||||
};
|
||||
|
||||
struct PerObjectConstants {
|
||||
Math::Matrix4x4 projection = Math::Matrix4x4::Identity();
|
||||
Math::Matrix4x4 view = Math::Matrix4x4::Identity();
|
||||
@@ -50,6 +59,9 @@ private:
|
||||
RHI::RHIPipelineState* GetOrCreatePipelineState(
|
||||
const RenderContext& context,
|
||||
const Resources::Material* material);
|
||||
RHI::RHIDescriptorSet* GetOrCreatePerObjectSet(Core::uint64 objectId);
|
||||
RHI::RHIDescriptorSet* GetOrCreateTextureSet(RHI::RHIResourceView* textureView);
|
||||
void DestroyOwnedDescriptorSet(OwnedDescriptorSet& descriptorSet);
|
||||
|
||||
const Resources::Texture* ResolveTexture(const Resources::Material* material) const;
|
||||
RHI::RHIResourceView* ResolveTextureView(const VisibleRenderItem& visibleItem);
|
||||
@@ -64,14 +76,12 @@ private:
|
||||
|
||||
RenderResourceCache m_resourceCache;
|
||||
|
||||
RHI::RHIDescriptorPool* m_constantPool = nullptr;
|
||||
RHI::RHIDescriptorSet* m_constantSet = nullptr;
|
||||
RHI::RHIDescriptorPool* m_texturePool = nullptr;
|
||||
RHI::RHIDescriptorSet* m_textureSet = nullptr;
|
||||
RHI::RHIDescriptorPool* m_samplerPool = nullptr;
|
||||
RHI::RHIDescriptorSet* m_samplerSet = nullptr;
|
||||
RHI::RHIPipelineLayout* m_pipelineLayout = nullptr;
|
||||
std::unordered_map<Resources::MaterialRenderState, RHI::RHIPipelineState*, MaterialRenderStateHash> m_pipelineStates;
|
||||
std::unordered_map<Core::uint64, OwnedDescriptorSet> m_perObjectSets;
|
||||
std::unordered_map<RHI::RHIResourceView*, OwnedDescriptorSet> m_textureSets;
|
||||
RHI::RHISampler* m_sampler = nullptr;
|
||||
RHI::RHITexture* m_fallbackTexture = nullptr;
|
||||
RHI::RHIResourceView* m_fallbackTextureView = nullptr;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "XCEngine/RHI/D3D12/D3D12PipelineState.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Shader.h"
|
||||
#include "XCEngine/Debug/Logger.h"
|
||||
#include <cstring>
|
||||
|
||||
namespace XCEngine {
|
||||
@@ -191,9 +192,9 @@ bool D3D12PipelineState::CreateD3D12PSO() {
|
||||
|
||||
desc.NumRenderTargets = m_renderTargetCount;
|
||||
for (uint32_t i = 0; i < m_renderTargetCount && i < 8; ++i) {
|
||||
desc.RTVFormats[i] = static_cast<DXGI_FORMAT>(m_renderTargetFormats[i]);
|
||||
desc.RTVFormats[i] = ToD3D12(static_cast<Format>(m_renderTargetFormats[i]));
|
||||
}
|
||||
desc.DSVFormat = static_cast<DXGI_FORMAT>(m_depthStencilFormat);
|
||||
desc.DSVFormat = ToD3D12(static_cast<Format>(m_depthStencilFormat));
|
||||
desc.SampleDesc.Count = m_sampleCount;
|
||||
desc.SampleDesc.Quality = 0;
|
||||
desc.SampleMask = 0xffffffff;
|
||||
@@ -201,6 +202,9 @@ bool D3D12PipelineState::CreateD3D12PSO() {
|
||||
|
||||
HRESULT hr = m_device->CreateGraphicsPipelineState(&desc, IID_PPV_ARGS(&m_pipelineState));
|
||||
if (FAILED(hr)) {
|
||||
char errorMessage[256] = {};
|
||||
sprintf_s(errorMessage, "D3D12 CreateGraphicsPipelineState failed: hr=0x%08X", static_cast<unsigned int>(hr));
|
||||
Debug::Logger::Get().Error(Debug::LogCategory::Rendering, errorMessage);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#include "Rendering/Pipelines/BuiltinForwardPipeline.h"
|
||||
|
||||
#include "Components/GameObject.h"
|
||||
#include "Components/MeshFilterComponent.h"
|
||||
#include "Components/MeshRendererComponent.h"
|
||||
#include "RHI/RHICommandList.h"
|
||||
#include "Debug/Logger.h"
|
||||
#include "Rendering/RenderMaterialUtility.h"
|
||||
#include "Rendering/RenderSurface.h"
|
||||
#include "Resources/Material/Material.h"
|
||||
@@ -296,15 +298,6 @@ bool BuiltinForwardPipeline::EnsureInitialized(const RenderContext& context) {
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& context) {
|
||||
RHI::DescriptorPoolDesc constantPoolDesc = {};
|
||||
constantPoolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
|
||||
constantPoolDesc.descriptorCount = 1;
|
||||
constantPoolDesc.shaderVisible = false;
|
||||
m_constantPool = context.device->CreateDescriptorPool(constantPoolDesc);
|
||||
if (m_constantPool == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::DescriptorSetLayoutBinding constantBinding = {};
|
||||
constantBinding.binding = 0;
|
||||
constantBinding.type = static_cast<uint32_t>(RHI::DescriptorType::CBV);
|
||||
@@ -313,19 +306,6 @@ bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& contex
|
||||
RHI::DescriptorSetLayoutDesc constantLayout = {};
|
||||
constantLayout.bindings = &constantBinding;
|
||||
constantLayout.bindingCount = 1;
|
||||
m_constantSet = m_constantPool->AllocateSet(constantLayout);
|
||||
if (m_constantSet == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::DescriptorPoolDesc texturePoolDesc = {};
|
||||
texturePoolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
|
||||
texturePoolDesc.descriptorCount = 1;
|
||||
texturePoolDesc.shaderVisible = true;
|
||||
m_texturePool = context.device->CreateDescriptorPool(texturePoolDesc);
|
||||
if (m_texturePool == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::DescriptorSetLayoutBinding textureBinding = {};
|
||||
textureBinding.binding = 0;
|
||||
@@ -335,10 +315,6 @@ bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& contex
|
||||
RHI::DescriptorSetLayoutDesc textureLayout = {};
|
||||
textureLayout.bindings = &textureBinding;
|
||||
textureLayout.bindingCount = 1;
|
||||
m_textureSet = m_texturePool->AllocateSet(textureLayout);
|
||||
if (m_textureSet == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::DescriptorPoolDesc samplerPoolDesc = {};
|
||||
samplerPoolDesc.type = RHI::DescriptorHeapType::Sampler;
|
||||
@@ -447,6 +423,16 @@ void BuiltinForwardPipeline::DestroyPipelineResources() {
|
||||
}
|
||||
m_pipelineStates.clear();
|
||||
|
||||
for (auto& perObjectSetPair : m_perObjectSets) {
|
||||
DestroyOwnedDescriptorSet(perObjectSetPair.second);
|
||||
}
|
||||
m_perObjectSets.clear();
|
||||
|
||||
for (auto& textureSetPair : m_textureSets) {
|
||||
DestroyOwnedDescriptorSet(textureSetPair.second);
|
||||
}
|
||||
m_textureSets.clear();
|
||||
|
||||
if (m_fallbackTextureView != nullptr) {
|
||||
m_fallbackTextureView->Shutdown();
|
||||
delete m_fallbackTextureView;
|
||||
@@ -471,36 +457,12 @@ void BuiltinForwardPipeline::DestroyPipelineResources() {
|
||||
m_pipelineLayout = nullptr;
|
||||
}
|
||||
|
||||
if (m_constantSet != nullptr) {
|
||||
m_constantSet->Shutdown();
|
||||
delete m_constantSet;
|
||||
m_constantSet = nullptr;
|
||||
}
|
||||
|
||||
if (m_textureSet != nullptr) {
|
||||
m_textureSet->Shutdown();
|
||||
delete m_textureSet;
|
||||
m_textureSet = nullptr;
|
||||
}
|
||||
|
||||
if (m_samplerSet != nullptr) {
|
||||
m_samplerSet->Shutdown();
|
||||
delete m_samplerSet;
|
||||
m_samplerSet = nullptr;
|
||||
}
|
||||
|
||||
if (m_constantPool != nullptr) {
|
||||
m_constantPool->Shutdown();
|
||||
delete m_constantPool;
|
||||
m_constantPool = nullptr;
|
||||
}
|
||||
|
||||
if (m_texturePool != nullptr) {
|
||||
m_texturePool->Shutdown();
|
||||
delete m_texturePool;
|
||||
m_texturePool = nullptr;
|
||||
}
|
||||
|
||||
if (m_samplerPool != nullptr) {
|
||||
m_samplerPool->Shutdown();
|
||||
delete m_samplerPool;
|
||||
@@ -526,6 +488,9 @@ RHI::RHIPipelineState* BuiltinForwardPipeline::GetOrCreatePipelineState(
|
||||
CreatePipelineDesc(context.backendType, m_pipelineLayout, material);
|
||||
RHI::RHIPipelineState* pipelineState = context.device->CreatePipelineState(pipelineDesc);
|
||||
if (pipelineState == nullptr || !pipelineState->IsValid()) {
|
||||
Debug::Logger::Get().Error(
|
||||
Debug::LogCategory::Rendering,
|
||||
"BuiltinForwardPipeline failed to create pipeline state");
|
||||
if (pipelineState != nullptr) {
|
||||
pipelineState->Shutdown();
|
||||
delete pipelineState;
|
||||
@@ -537,6 +502,95 @@ RHI::RHIPipelineState* BuiltinForwardPipeline::GetOrCreatePipelineState(
|
||||
return pipelineState;
|
||||
}
|
||||
|
||||
RHI::RHIDescriptorSet* BuiltinForwardPipeline::GetOrCreatePerObjectSet(Core::uint64 objectId) {
|
||||
const auto existing = m_perObjectSets.find(objectId);
|
||||
if (existing != m_perObjectSets.end()) {
|
||||
return existing->second.set;
|
||||
}
|
||||
|
||||
RHI::DescriptorPoolDesc poolDesc = {};
|
||||
poolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
|
||||
poolDesc.descriptorCount = 1;
|
||||
poolDesc.shaderVisible = false;
|
||||
|
||||
OwnedDescriptorSet descriptorSet = {};
|
||||
descriptorSet.pool = m_device->CreateDescriptorPool(poolDesc);
|
||||
if (descriptorSet.pool == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
RHI::DescriptorSetLayoutBinding binding = {};
|
||||
binding.binding = 0;
|
||||
binding.type = static_cast<uint32_t>(RHI::DescriptorType::CBV);
|
||||
binding.count = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc layout = {};
|
||||
layout.bindings = &binding;
|
||||
layout.bindingCount = 1;
|
||||
descriptorSet.set = descriptorSet.pool->AllocateSet(layout);
|
||||
if (descriptorSet.set == nullptr) {
|
||||
DestroyOwnedDescriptorSet(descriptorSet);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto result = m_perObjectSets.emplace(objectId, descriptorSet);
|
||||
return result.first->second.set;
|
||||
}
|
||||
|
||||
RHI::RHIDescriptorSet* BuiltinForwardPipeline::GetOrCreateTextureSet(RHI::RHIResourceView* textureView) {
|
||||
if (textureView == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto existing = m_textureSets.find(textureView);
|
||||
if (existing != m_textureSets.end()) {
|
||||
return existing->second.set;
|
||||
}
|
||||
|
||||
RHI::DescriptorPoolDesc poolDesc = {};
|
||||
poolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
|
||||
poolDesc.descriptorCount = 1;
|
||||
poolDesc.shaderVisible = true;
|
||||
|
||||
OwnedDescriptorSet descriptorSet = {};
|
||||
descriptorSet.pool = m_device->CreateDescriptorPool(poolDesc);
|
||||
if (descriptorSet.pool == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
RHI::DescriptorSetLayoutBinding binding = {};
|
||||
binding.binding = 0;
|
||||
binding.type = static_cast<uint32_t>(RHI::DescriptorType::SRV);
|
||||
binding.count = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc layout = {};
|
||||
layout.bindings = &binding;
|
||||
layout.bindingCount = 1;
|
||||
descriptorSet.set = descriptorSet.pool->AllocateSet(layout);
|
||||
if (descriptorSet.set == nullptr) {
|
||||
DestroyOwnedDescriptorSet(descriptorSet);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
descriptorSet.set->Update(0, textureView);
|
||||
const auto result = m_textureSets.emplace(textureView, descriptorSet);
|
||||
return result.first->second.set;
|
||||
}
|
||||
|
||||
void BuiltinForwardPipeline::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;
|
||||
}
|
||||
}
|
||||
|
||||
const Resources::Texture* BuiltinForwardPipeline::ResolveTexture(const Resources::Material* material) const {
|
||||
return material != nullptr ? FindMaterialTexture(*material) : nullptr;
|
||||
}
|
||||
@@ -585,9 +639,15 @@ bool BuiltinForwardPipeline::DrawVisibleItem(
|
||||
return false;
|
||||
}
|
||||
|
||||
m_constantSet->WriteConstant(0, &constants, sizeof(constants));
|
||||
m_textureSet->Update(0, textureView);
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = { m_constantSet, m_textureSet, m_samplerSet };
|
||||
RHI::RHIDescriptorSet* constantSet = GetOrCreatePerObjectSet(
|
||||
visibleItem.gameObject != nullptr ? visibleItem.gameObject->GetID() : 0);
|
||||
RHI::RHIDescriptorSet* textureSet = GetOrCreateTextureSet(textureView);
|
||||
if (constantSet == nullptr || textureSet == nullptr || m_samplerSet == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
constantSet->WriteConstant(0, &constants, sizeof(constants));
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = { constantSet, textureSet, m_samplerSet };
|
||||
commandList->SetGraphicsDescriptorSets(kDescriptorFirstSet, 3, descriptorSets, m_pipelineLayout);
|
||||
|
||||
if (visibleItem.hasSection) {
|
||||
|
||||
@@ -4,3 +4,4 @@ project(XCEngine_RenderingIntegrationTests)
|
||||
|
||||
add_subdirectory(textured_quad_scene)
|
||||
add_subdirectory(backpack_scene)
|
||||
add_subdirectory(transparent_material_scene)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
project(rendering_integration_transparent_material_scene)
|
||||
|
||||
set(ENGINE_ROOT_DIR ${CMAKE_SOURCE_DIR}/engine)
|
||||
set(PACKAGE_DIR ${CMAKE_SOURCE_DIR}/tests/opengl/package)
|
||||
|
||||
get_filename_component(PROJECT_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../.. ABSOLUTE)
|
||||
|
||||
add_executable(rendering_integration_transparent_material_scene
|
||||
main.cpp
|
||||
${CMAKE_SOURCE_DIR}/tests/RHI/integration/fixtures/RHIIntegrationFixture.cpp
|
||||
${PACKAGE_DIR}/src/glad.c
|
||||
)
|
||||
|
||||
target_include_directories(rendering_integration_transparent_material_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_transparent_material_scene PRIVATE
|
||||
d3d12
|
||||
dxgi
|
||||
d3dcompiler
|
||||
winmm
|
||||
opengl32
|
||||
XCEngine
|
||||
GTest::gtest
|
||||
)
|
||||
|
||||
target_compile_definitions(rendering_integration_transparent_material_scene PRIVATE
|
||||
UNICODE
|
||||
_UNICODE
|
||||
XCENGINE_SUPPORT_OPENGL
|
||||
XCENGINE_SUPPORT_D3D12
|
||||
)
|
||||
|
||||
add_custom_command(TARGET rendering_integration_transparent_material_scene POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${CMAKE_SOURCE_DIR}/tests/RHI/integration/compare_ppm.py
|
||||
$<TARGET_FILE_DIR:rendering_integration_transparent_material_scene>/
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/GT.ppm
|
||||
$<TARGET_FILE_DIR:rendering_integration_transparent_material_scene>/GT.ppm
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${ENGINE_ROOT_DIR}/third_party/renderdoc/renderdoc.dll
|
||||
$<TARGET_FILE_DIR:rendering_integration_transparent_material_scene>/
|
||||
)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(rendering_integration_transparent_material_scene)
|
||||
631398
tests/Rendering/integration/transparent_material_scene/GT.ppm
Normal file
631398
tests/Rendering/integration/transparent_material_scene/GT.ppm
Normal file
File diff suppressed because it is too large
Load Diff
395
tests/Rendering/integration/transparent_material_scene/main.cpp
Normal file
395
tests/Rendering/integration/transparent_material_scene/main.cpp
Normal file
@@ -0,0 +1,395 @@
|
||||
#include <gtest/gtest.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/Quaternion.h>
|
||||
#include <XCEngine/Core/Math/Vector2.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/Material/Material.h>
|
||||
#include <XCEngine/Resources/Mesh/Mesh.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::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 = "transparent_material_scene_d3d12.ppm";
|
||||
constexpr const char* kOpenGLScreenshot = "transparent_material_scene_opengl.ppm";
|
||||
constexpr uint32_t kFrameWidth = 1280;
|
||||
constexpr uint32_t kFrameHeight = 720;
|
||||
|
||||
Mesh* CreateQuadMesh() {
|
||||
auto* mesh = new Mesh();
|
||||
IResource::ConstructParams params = {};
|
||||
params.name = "TransparentQuad";
|
||||
params.path = "Tests/Rendering/TransparentQuad.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* CreateSolidTexture(const char* name, const char* path, const unsigned char rgba[4]) {
|
||||
auto* texture = new Texture();
|
||||
IResource::ConstructParams params = {};
|
||||
params.name = name;
|
||||
params.path = path;
|
||||
params.guid = ResourceGUID::Generate(params.path);
|
||||
texture->Initialize(params);
|
||||
texture->Create(
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
XCEngine::Resources::TextureType::Texture2D,
|
||||
XCEngine::Resources::TextureFormat::RGBA8_UNORM,
|
||||
rgba,
|
||||
4);
|
||||
return texture;
|
||||
}
|
||||
|
||||
Material* CreateMaterial(
|
||||
const char* name,
|
||||
const char* path,
|
||||
Texture* texture,
|
||||
MaterialRenderQueue renderQueue,
|
||||
const MaterialRenderState& renderState) {
|
||||
auto* material = new Material();
|
||||
IResource::ConstructParams params = {};
|
||||
params.name = name;
|
||||
params.path = path;
|
||||
params.guid = ResourceGUID::Generate(params.path);
|
||||
material->Initialize(params);
|
||||
material->SetTexture("_BaseColorTexture", ResourceHandle<Texture>(texture));
|
||||
material->SetRenderQueue(renderQueue);
|
||||
material->SetRenderState(renderState);
|
||||
return material;
|
||||
}
|
||||
|
||||
GameObject* CreateQuadObject(
|
||||
Scene& scene,
|
||||
const char* name,
|
||||
Mesh* mesh,
|
||||
Material* material,
|
||||
const Vector3& position,
|
||||
const Vector3& scale,
|
||||
const Quaternion& rotation = Quaternion::Identity()) {
|
||||
GameObject* gameObject = scene.CreateGameObject(name);
|
||||
gameObject->GetTransform()->SetLocalPosition(position);
|
||||
gameObject->GetTransform()->SetLocalScale(scale);
|
||||
gameObject->GetTransform()->SetLocalRotation(rotation);
|
||||
|
||||
auto* meshFilter = gameObject->AddComponent<MeshFilterComponent>();
|
||||
auto* meshRenderer = gameObject->AddComponent<MeshRendererComponent>();
|
||||
meshFilter->SetMesh(ResourceHandle<Mesh>(mesh));
|
||||
meshRenderer->SetMaterial(0, ResourceHandle<Material>(material));
|
||||
return gameObject;
|
||||
}
|
||||
|
||||
const char* GetScreenshotFilename(RHIType backendType) {
|
||||
return backendType == RHIType::D3D12 ? kD3D12Screenshot : kOpenGLScreenshot;
|
||||
}
|
||||
|
||||
int GetComparisonThreshold(RHIType backendType) {
|
||||
(void)backendType;
|
||||
return 8;
|
||||
}
|
||||
|
||||
class TransparentMaterialSceneTest : 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<Texture*> mTextures;
|
||||
std::vector<Material*> mMaterials;
|
||||
RHITexture* mDepthTexture = nullptr;
|
||||
RHIResourceView* mDepthView = nullptr;
|
||||
Mesh* mMesh = nullptr;
|
||||
};
|
||||
|
||||
void TransparentMaterialSceneTest::SetUp() {
|
||||
RHIIntegrationFixture::SetUp();
|
||||
|
||||
mSceneRenderer = std::make_unique<SceneRenderer>();
|
||||
mScene = std::make_unique<Scene>("TransparentMaterialScene");
|
||||
mMesh = CreateQuadMesh();
|
||||
|
||||
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 TransparentMaterialSceneTest::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();
|
||||
|
||||
delete mMesh;
|
||||
mMesh = nullptr;
|
||||
|
||||
RHIIntegrationFixture::TearDown();
|
||||
}
|
||||
|
||||
void TransparentMaterialSceneTest::BuildScene() {
|
||||
ASSERT_NE(mScene, nullptr);
|
||||
ASSERT_NE(mMesh, nullptr);
|
||||
|
||||
GameObject* cameraObject = mScene->CreateGameObject("MainCamera");
|
||||
auto* camera = cameraObject->AddComponent<CameraComponent>();
|
||||
camera->SetPrimary(true);
|
||||
camera->SetProjectionType(CameraProjectionType::Perspective);
|
||||
camera->SetFieldOfView(45.0f);
|
||||
camera->SetNearClipPlane(0.1f);
|
||||
camera->SetFarClipPlane(10.0f);
|
||||
camera->SetClearColor(XCEngine::Math::Color(0.04f, 0.05f, 0.09f, 1.0f));
|
||||
|
||||
const unsigned char redPixel[4] = { 248, 80, 80, 160 };
|
||||
const unsigned char greenPixel[4] = { 88, 240, 136, 160 };
|
||||
|
||||
Texture* farTexture = CreateSolidTexture(
|
||||
"FarTransparentTexture",
|
||||
"Tests/Rendering/TransparentScene/far.texture",
|
||||
redPixel);
|
||||
Texture* nearTexture = CreateSolidTexture(
|
||||
"NearTransparentTexture",
|
||||
"Tests/Rendering/TransparentScene/near.texture",
|
||||
greenPixel);
|
||||
mTextures = { farTexture, nearTexture };
|
||||
|
||||
MaterialRenderState transparentState = {};
|
||||
transparentState.blendEnable = true;
|
||||
transparentState.srcBlend = MaterialBlendFactor::SrcAlpha;
|
||||
transparentState.dstBlend = MaterialBlendFactor::InvSrcAlpha;
|
||||
transparentState.srcBlendAlpha = MaterialBlendFactor::One;
|
||||
transparentState.dstBlendAlpha = MaterialBlendFactor::Zero;
|
||||
transparentState.depthTestEnable = true;
|
||||
transparentState.depthWriteEnable = false;
|
||||
transparentState.depthFunc = MaterialComparisonFunc::LessEqual;
|
||||
transparentState.cullMode = MaterialCullMode::None;
|
||||
Material* farMaterial = CreateMaterial(
|
||||
"FarTransparentMaterial",
|
||||
"Tests/Rendering/TransparentScene/far.mat",
|
||||
farTexture,
|
||||
MaterialRenderQueue::Transparent,
|
||||
transparentState);
|
||||
Material* nearMaterial = CreateMaterial(
|
||||
"NearTransparentMaterial",
|
||||
"Tests/Rendering/TransparentScene/near.mat",
|
||||
nearTexture,
|
||||
MaterialRenderQueue::Transparent,
|
||||
transparentState);
|
||||
mMaterials = { farMaterial, nearMaterial };
|
||||
|
||||
CreateQuadObject(
|
||||
*mScene,
|
||||
"FarTransparent",
|
||||
mMesh,
|
||||
farMaterial,
|
||||
Vector3(-0.14f, 0.0f, 3.25f),
|
||||
Vector3(0.92f, 0.92f, 1.0f),
|
||||
Quaternion::FromAxisAngle(Vector3::Up(), 0.62f));
|
||||
|
||||
CreateQuadObject(
|
||||
*mScene,
|
||||
"NearTransparent",
|
||||
mMesh,
|
||||
nearMaterial,
|
||||
Vector3(0.14f, 0.0f, 2.75f),
|
||||
Vector3(0.92f, 0.92f, 1.0f),
|
||||
Quaternion::FromAxisAngle(Vector3::Up(), -0.62f));
|
||||
}
|
||||
|
||||
RHIResourceView* TransparentMaterialSceneTest::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 TransparentMaterialSceneTest::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(TransparentMaterialSceneTest, RenderTransparentMaterialScene) {
|
||||
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, TransparentMaterialSceneTest, ::testing::Values(RHIType::D3D12));
|
||||
INSTANTIATE_TEST_SUITE_P(OpenGL, TransparentMaterialSceneTest, ::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();
|
||||
}
|
||||
Reference in New Issue
Block a user