rendering: add alpha cutout integration coverage

This commit is contained in:
2026-04-06 23:09:45 +08:00
parent 620717f8b4
commit 83f316a91f
6 changed files with 506580 additions and 10 deletions

View File

@@ -1013,19 +1013,70 @@ std::vector<ShaderKeywordSet> BuildShaderKeywordVariantSets(
Containers::String BuildKeywordVariantSource(
const Containers::String& baseSource,
const ShaderKeywordSet& requiredKeywords) {
Containers::String variantSource;
if (requiredKeywords.enabledKeywords.Empty()) {
return baseSource;
}
std::string defineBlock;
for (const Containers::String& keyword : requiredKeywords.enabledKeywords) {
variantSource += "#define ";
variantSource += keyword;
variantSource += " 1\n";
defineBlock += "#define ";
defineBlock += ToStdString(keyword);
defineBlock += " 1\n";
}
if (!variantSource.Empty() && !baseSource.Empty()) {
variantSource += '\n';
if (baseSource.Empty()) {
return Containers::String(defineBlock.c_str());
}
variantSource += baseSource;
return variantSource;
const std::string sourceText = ToStdString(baseSource);
size_t lineStart = 0;
while (lineStart < sourceText.size()) {
const size_t lineEnd = sourceText.find_first_of("\r\n", lineStart);
const size_t lineLength =
lineEnd == std::string::npos ? sourceText.size() - lineStart : lineEnd - lineStart;
const std::string line = sourceText.substr(lineStart, lineLength);
const std::string trimmedLine = TrimCopy(line);
if (trimmedLine.empty() ||
trimmedLine.rfind("//", 0) == 0u ||
trimmedLine.rfind("/*", 0) == 0u ||
trimmedLine.rfind("*", 0) == 0u) {
if (lineEnd == std::string::npos) {
break;
}
lineStart = lineEnd + 1u;
if (sourceText[lineEnd] == '\r' &&
lineStart < sourceText.size() &&
sourceText[lineStart] == '\n') {
++lineStart;
}
continue;
}
if (trimmedLine.rfind("#version", 0) != 0u) {
break;
}
size_t insertionPos = lineEnd == std::string::npos ? sourceText.size() : lineEnd + 1u;
if (lineEnd != std::string::npos &&
sourceText[lineEnd] == '\r' &&
insertionPos < sourceText.size() &&
sourceText[insertionPos] == '\n') {
++insertionPos;
}
std::string variantSource = sourceText.substr(0, insertionPos);
variantSource += defineBlock;
variantSource += sourceText.substr(insertionPos);
return Containers::String(variantSource.c_str());
}
std::string variantSource = defineBlock;
variantSource += '\n';
variantSource += sourceText;
return Containers::String(variantSource.c_str());
}
bool TryParseShaderKeywordDeclarationPragma(

View File

@@ -20,3 +20,4 @@ add_subdirectory(skybox_scene)
add_subdirectory(post_process_scene)
add_subdirectory(camera_post_process_scene camera_post_process_scene_builtin)
add_subdirectory(final_color_scene)
add_subdirectory(alpha_cutout_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_alpha_cutout_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_alpha_cutout_scene
main.cpp
${CMAKE_SOURCE_DIR}/tests/RHI/integration/fixtures/RHIIntegrationFixture.cpp
${PACKAGE_DIR}/src/glad.c
)
target_include_directories(rendering_integration_alpha_cutout_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_alpha_cutout_scene PRIVATE
d3d12
dxgi
d3dcompiler
winmm
opengl32
XCEngine
GTest::gtest
)
if(TARGET Vulkan::Vulkan)
target_link_libraries(rendering_integration_alpha_cutout_scene PRIVATE Vulkan::Vulkan)
target_compile_definitions(rendering_integration_alpha_cutout_scene PRIVATE XCENGINE_SUPPORT_VULKAN)
endif()
target_compile_definitions(rendering_integration_alpha_cutout_scene PRIVATE
UNICODE
_UNICODE
XCENGINE_SUPPORT_OPENGL
XCENGINE_SUPPORT_D3D12
)
add_custom_command(TARGET rendering_integration_alpha_cutout_scene POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/tests/RHI/integration/compare_ppm.py
$<TARGET_FILE_DIR:rendering_integration_alpha_cutout_scene>/
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/GT.ppm
$<TARGET_FILE_DIR:rendering_integration_alpha_cutout_scene>/GT.ppm
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ENGINE_ROOT_DIR}/third_party/renderdoc/renderdoc.dll
$<TARGET_FILE_DIR:rendering_integration_alpha_cutout_scene>/
)
include(GoogleTest)
gtest_discover_tests(rendering_integration_alpha_cutout_scene)

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,577 @@
#include <gtest/gtest.h>
#include "../RenderingIntegrationImageAssert.h"
#include "../RenderingIntegrationMain.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/Asset/IResource.h>
#include <XCEngine/Core/Math/Bounds.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/Core/Math/Vector4.h>
#include <XCEngine/Rendering/Planning/CameraRenderRequest.h>
#include <XCEngine/Rendering/RenderContext.h>
#include <XCEngine/Rendering/RenderSurface.h>
#include <XCEngine/Rendering/Execution/SceneRenderer.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 <array>
#include <memory>
#include <vector>
using namespace RenderingIntegrationTestUtils;
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 = "alpha_cutout_scene_d3d12.ppm";
constexpr const char* kOpenGLScreenshot = "alpha_cutout_scene_opengl.ppm";
constexpr const char* kVulkanScreenshot = "alpha_cutout_scene_vulkan.ppm";
constexpr uint32_t kFrameWidth = 1280;
constexpr uint32_t kFrameHeight = 720;
constexpr float kAlphaCutoff = 0.5f;
void AppendQuadFace(
std::vector<StaticMeshVertex>& vertices,
std::vector<uint32_t>& indices,
const Vector3& v0,
const Vector3& v1,
const Vector3& v2,
const Vector3& v3,
const Vector3& normal) {
const uint32_t baseIndex = static_cast<uint32_t>(vertices.size());
StaticMeshVertex vertex = {};
vertex.normal = normal;
vertex.position = v0;
vertex.uv0 = Vector2(0.0f, 1.0f);
vertices.push_back(vertex);
vertex.position = v1;
vertex.uv0 = Vector2(1.0f, 1.0f);
vertices.push_back(vertex);
vertex.position = v2;
vertex.uv0 = Vector2(0.0f, 0.0f);
vertices.push_back(vertex);
vertex.position = v3;
vertex.uv0 = Vector2(1.0f, 0.0f);
vertices.push_back(vertex);
indices.push_back(baseIndex + 0);
indices.push_back(baseIndex + 2);
indices.push_back(baseIndex + 1);
indices.push_back(baseIndex + 1);
indices.push_back(baseIndex + 2);
indices.push_back(baseIndex + 3);
}
Mesh* CreateVerticalQuadMesh() {
auto* mesh = new Mesh();
IResource::ConstructParams params = {};
params.name = "AlphaCutoutVerticalQuad";
params.path = "Tests/Rendering/AlphaCutoutVerticalQuad.mesh";
params.guid = ResourceGUID::Generate(params.path);
mesh->Initialize(params);
StaticMeshVertex vertices[4] = {};
vertices[0].position = Vector3(-0.5f, -0.5f, 0.0f);
vertices[0].normal = Vector3::Back();
vertices[0].uv0 = Vector2(0.0f, 1.0f);
vertices[1].position = Vector3(-0.5f, 0.5f, 0.0f);
vertices[1].normal = Vector3::Back();
vertices[1].uv0 = Vector2(0.0f, 0.0f);
vertices[2].position = Vector3(0.5f, -0.5f, 0.0f);
vertices[2].normal = Vector3::Back();
vertices[2].uv0 = Vector2(1.0f, 1.0f);
vertices[3].position = Vector3(0.5f, 0.5f, 0.0f);
vertices[3].normal = Vector3::Back();
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::Normal | VertexAttribute::UV0);
mesh->SetIndexData(indices, sizeof(indices), 6, true);
const Bounds bounds(Vector3(0.0f, 0.0f, 0.0f), Vector3(1.0f, 1.0f, 0.02f));
mesh->SetBounds(bounds);
MeshSection section = {};
section.baseVertex = 0;
section.vertexCount = 4;
section.startIndex = 0;
section.indexCount = 6;
section.materialID = 0;
section.bounds = bounds;
mesh->AddSection(section);
return mesh;
}
Mesh* CreateGroundMesh() {
auto* mesh = new Mesh();
IResource::ConstructParams params = {};
params.name = "AlphaCutoutGround";
params.path = "Tests/Rendering/AlphaCutoutGround.mesh";
params.guid = ResourceGUID::Generate(params.path);
mesh->Initialize(params);
StaticMeshVertex vertices[4] = {};
vertices[0].position = Vector3(-0.5f, 0.0f, 0.0f);
vertices[0].normal = Vector3::Up();
vertices[0].uv0 = Vector2(0.0f, 1.0f);
vertices[1].position = Vector3(-0.5f, 0.0f, 1.0f);
vertices[1].normal = Vector3::Up();
vertices[1].uv0 = Vector2(0.0f, 0.0f);
vertices[2].position = Vector3(0.5f, 0.0f, 0.0f);
vertices[2].normal = Vector3::Up();
vertices[2].uv0 = Vector2(1.0f, 1.0f);
vertices[3].position = Vector3(0.5f, 0.0f, 1.0f);
vertices[3].normal = Vector3::Up();
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::Normal | VertexAttribute::UV0);
mesh->SetIndexData(indices, sizeof(indices), 6, true);
const Bounds bounds(Vector3(0.0f, 0.0f, 0.5f), Vector3(1.0f, 0.02f, 1.0f));
mesh->SetBounds(bounds);
MeshSection section = {};
section.baseVertex = 0;
section.vertexCount = 4;
section.startIndex = 0;
section.indexCount = 6;
section.materialID = 0;
section.bounds = bounds;
mesh->AddSection(section);
return mesh;
}
Texture* CreateCutoutTexture() {
auto* texture = new Texture();
IResource::ConstructParams params = {};
params.name = "AlphaCutoutTexture";
params.path = "Tests/Rendering/AlphaCutoutScene/cutout.texture";
params.guid = ResourceGUID::Generate(params.path);
texture->Initialize(params);
constexpr uint32_t kTextureSize = 16;
std::vector<unsigned char> pixels(static_cast<size_t>(kTextureSize) * kTextureSize * 4u, 0u);
for (uint32_t y = 0; y < kTextureSize; ++y) {
for (uint32_t x = 0; x < kTextureSize; ++x) {
const bool insideWindow = x >= 4u && x < 12u && y >= 4u && y < 12u;
const size_t pixelOffset = (static_cast<size_t>(y) * kTextureSize + x) * 4u;
pixels[pixelOffset + 0] = 235u;
pixels[pixelOffset + 1] = 72u;
pixels[pixelOffset + 2] = 54u;
pixels[pixelOffset + 3] = insideWindow ? 0u : 255u;
}
}
texture->Create(
kTextureSize,
kTextureSize,
1,
1,
XCEngine::Resources::TextureType::Texture2D,
TextureFormat::RGBA8_UNORM,
pixels.data(),
pixels.size());
return texture;
}
Material* CreateForwardLitMaterial(
const char* name,
const char* path,
const Vector4& baseColor,
Texture* texture = nullptr,
bool alphaTest = false) {
auto* material = new Material();
IResource::ConstructParams params = {};
params.name = name;
params.path = path;
params.guid = ResourceGUID::Generate(params.path);
material->Initialize(params);
material->SetShader(ResourceManager::Get().Load<Shader>(GetBuiltinForwardLitShaderPath()));
material->SetShaderPass("ForwardLit");
material->SetFloat4("_BaseColor", baseColor);
if (texture != nullptr) {
material->SetTexture("_MainTex", ResourceHandle<Texture>(texture));
}
MaterialRenderState renderState = material->GetRenderState();
renderState.depthFunc = MaterialComparisonFunc::LessEqual;
renderState.cullMode = MaterialCullMode::None;
material->SetRenderState(renderState);
if (alphaTest) {
material->SetFloat("_Cutoff", kAlphaCutoff);
material->EnableKeyword("XC_ALPHA_TEST");
material->SetRenderQueue(MaterialRenderQueue::AlphaTest);
} else {
material->SetRenderQueue(MaterialRenderQueue::Geometry);
}
return material;
}
GameObject* CreateRenderable(
Scene& scene,
const char* name,
Mesh* mesh,
Material* material,
const Vector3& position,
const Vector3& scale,
const Quaternion& rotation = Quaternion::Identity()) {
GameObject* object = scene.CreateGameObject(name);
object->GetTransform()->SetLocalPosition(position);
object->GetTransform()->SetLocalScale(scale);
object->GetTransform()->SetLocalRotation(rotation);
auto* meshFilter = object->AddComponent<MeshFilterComponent>();
auto* meshRenderer = object->AddComponent<MeshRendererComponent>();
meshFilter->SetMesh(ResourceHandle<Mesh>(mesh));
meshRenderer->SetMaterial(0, ResourceHandle<Material>(material));
return object;
}
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 : 12;
}
class AlphaCutoutSceneTest : public RHIIntegrationFixture {
protected:
void SetUp() override;
void TearDown() override;
void RenderFrame() override;
void AssertScenePixels(const char* screenshotFilename);
private:
void BuildScene();
RHIResourceView* GetCurrentBackBufferView();
std::unique_ptr<Scene> mScene;
std::unique_ptr<SceneRenderer> mSceneRenderer;
std::vector<RHIResourceView*> mBackBufferViews;
std::vector<Mesh*> mMeshes;
std::vector<Material*> mMaterials;
std::vector<Texture*> mTextures;
RHITexture* mDepthTexture = nullptr;
RHIResourceView* mDepthView = nullptr;
};
void AlphaCutoutSceneTest::SetUp() {
RHIIntegrationFixture::SetUp();
mSceneRenderer = std::make_unique<SceneRenderer>();
mScene = std::make_unique<Scene>("AlphaCutoutScene");
mMeshes.push_back(CreateVerticalQuadMesh());
mMeshes.push_back(CreateGroundMesh());
Texture* cutoutTexture = CreateCutoutTexture();
mTextures.push_back(cutoutTexture);
mMaterials.push_back(CreateForwardLitMaterial(
"AlphaCutoutCard",
"Tests/Rendering/AlphaCutoutScene/card.material",
Vector4::One(),
cutoutTexture,
true));
mMaterials.push_back(CreateForwardLitMaterial(
"AlphaCutoutBackdrop",
"Tests/Rendering/AlphaCutoutScene/backdrop.material",
Vector4(0.14f, 0.62f, 0.96f, 1.0f)));
mMaterials.push_back(CreateForwardLitMaterial(
"AlphaCutoutGround",
"Tests/Rendering/AlphaCutoutScene/ground.material",
Vector4(0.92f, 0.93f, 0.95f, 1.0f)));
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 AlphaCutoutSceneTest::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();
for (Mesh* mesh : mMeshes) {
delete mesh;
}
mMeshes.clear();
RHIIntegrationFixture::TearDown();
}
void AlphaCutoutSceneTest::BuildScene() {
ASSERT_NE(mScene, nullptr);
ASSERT_EQ(mMeshes.size(), 2u);
ASSERT_EQ(mMaterials.size(), 3u);
GameObject* cameraObject = mScene->CreateGameObject("MainCamera");
auto* camera = cameraObject->AddComponent<CameraComponent>();
camera->SetPrimary(true);
camera->SetProjectionType(CameraProjectionType::Perspective);
camera->SetFieldOfView(36.0f);
camera->SetNearClipPlane(0.1f);
camera->SetFarClipPlane(40.0f);
camera->SetClearColor(XCEngine::Math::Color(0.04f, 0.05f, 0.08f, 1.0f));
cameraObject->GetTransform()->SetLocalPosition(Vector3(0.0f, 1.15f, -1.95f));
cameraObject->GetTransform()->SetLocalRotation(
Quaternion::LookRotation(Vector3(0.0f, -0.07f, 1.0f).Normalized()));
GameObject* lightObject = mScene->CreateGameObject("MainDirectionalLight");
auto* light = lightObject->AddComponent<LightComponent>();
light->SetLightType(LightType::Directional);
light->SetColor(XCEngine::Math::Color(1.0f, 0.98f, 0.95f, 1.0f));
light->SetIntensity(2.2f);
light->SetCastsShadows(true);
lightObject->GetTransform()->SetLocalRotation(
Quaternion::LookRotation(Vector3(0.45f, -0.78f, 0.44f).Normalized()));
GameObject* ground = CreateRenderable(
*mScene,
"Ground",
mMeshes[1],
mMaterials[2],
Vector3(0.0f, -1.05f, 4.6f),
Vector3(8.8f, 1.0f, 10.0f));
auto* groundRenderer = ground->GetComponent<MeshRendererComponent>();
ASSERT_NE(groundRenderer, nullptr);
groundRenderer->SetCastShadows(false);
groundRenderer->SetReceiveShadows(true);
GameObject* backdrop = CreateRenderable(
*mScene,
"Backdrop",
mMeshes[0],
mMaterials[1],
Vector3(0.0f, 1.25f, 9.2f),
Vector3(5.2f, 4.1f, 1.0f));
auto* backdropRenderer = backdrop->GetComponent<MeshRendererComponent>();
ASSERT_NE(backdropRenderer, nullptr);
backdropRenderer->SetCastShadows(false);
backdropRenderer->SetReceiveShadows(false);
GameObject* card = CreateRenderable(
*mScene,
"CutoutCard",
mMeshes[0],
mMaterials[0],
Vector3(0.0f, 1.25f, 6.1f),
Vector3(2.4f, 3.2f, 1.0f));
auto* cardRenderer = card->GetComponent<MeshRendererComponent>();
ASSERT_NE(cardRenderer, nullptr);
cardRenderer->SetCastShadows(true);
cardRenderer->SetReceiveShadows(false);
}
RHIResourceView* AlphaCutoutSceneTest::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 AlphaCutoutSceneTest::RenderFrame() {
ASSERT_NE(mScene, nullptr);
ASSERT_NE(mSceneRenderer, nullptr);
RHICommandList* commandList = GetCommandList();
ASSERT_NE(commandList, nullptr);
commandList->Reset();
RenderSurface mainSurface(kFrameWidth, kFrameHeight);
mainSurface.SetColorAttachment(GetCurrentBackBufferView());
mainSurface.SetDepthAttachment(mDepthView);
RenderContext renderContext = {};
renderContext.device = GetDevice();
renderContext.commandList = commandList;
renderContext.commandQueue = GetCommandQueue();
renderContext.backendType = GetBackendType();
std::vector<CameraRenderRequest> requests =
mSceneRenderer->BuildRenderRequests(*mScene, nullptr, renderContext, mainSurface);
ASSERT_EQ(requests.size(), 1u);
ASSERT_TRUE(requests[0].directionalShadow.IsValid());
RenderSurface depthOnlySurface(kFrameWidth, kFrameHeight);
depthOnlySurface.SetDepthAttachment(mDepthView);
depthOnlySurface.SetRenderArea(requests[0].surface.GetRenderArea());
requests[0].depthOnly.surface = depthOnlySurface;
requests[0].depthOnly.clearFlags = RenderClearFlags::Depth;
requests[0].clearFlags = RenderClearFlags::Color;
ASSERT_TRUE(mSceneRenderer->Render(requests));
commandList->Close();
void* commandLists[] = { commandList };
GetCommandQueue()->ExecuteCommandLists(1, commandLists);
}
void AlphaCutoutSceneTest::AssertScenePixels(const char* screenshotFilename) {
const PpmImage image = LoadPpmImage(screenshotFilename);
ASSERT_GT(image.width, 0u);
ASSERT_GT(image.height, 0u);
ExpectPixelNear(image, 500, 220, { 255, 88, 65 }, 20, "card red frame");
ExpectPixelNear(image, 640, 260, { 44, 194, 255 }, 20, "cutout hole reveals blue backdrop");
ExpectPixelLuminanceAtMost(image, 900, 535, 420, "shadow border stays dark");
ExpectPixelLuminanceAtLeast(image, 800, 535, 700, "shadow hole stays lit");
}
TEST_P(AlphaCutoutSceneTest, RenderAlphaCutoutScene) {
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));
AssertScenePixels(screenshotFilename);
ASSERT_TRUE(CompareWithGoldenTemplate(
screenshotFilename,
"GT.ppm",
static_cast<float>(comparisonThreshold)));
break;
}
swapChain->Present(0, 0);
}
}
} // namespace
INSTANTIATE_TEST_SUITE_P(D3D12, AlphaCutoutSceneTest, ::testing::Values(RHIType::D3D12));
INSTANTIATE_TEST_SUITE_P(OpenGL, AlphaCutoutSceneTest, ::testing::Values(RHIType::OpenGL));
#if defined(XCENGINE_SUPPORT_VULKAN)
INSTANTIATE_TEST_SUITE_P(Vulkan, AlphaCutoutSceneTest, ::testing::Values(RHIType::Vulkan));
#endif
GTEST_API_ int main(int argc, char** argv) {
return RunRenderingIntegrationTestMain(argc, argv);
}

View File

@@ -551,11 +551,16 @@ TEST(ShaderLoader, LoadLegacyBackendSplitShaderAuthoringExpandsKeywordVariantsPe
ASSERT_NE(keywordOpenGLFragment, nullptr);
EXPECT_EQ(keywordOpenGLFragment->entryPoint, "main");
EXPECT_EQ(keywordOpenGLFragment->profile, "fs_4_30");
const std::string keywordOpenGLSource = keywordOpenGLFragment->sourceCode.CStr();
EXPECT_NE(
std::string(keywordOpenGLFragment->sourceCode.CStr()).find("#define XC_ALPHA_TEST 1"),
keywordOpenGLSource.find("#define XC_ALPHA_TEST 1"),
std::string::npos);
EXPECT_NE(keywordOpenGLSource.find("#version 430"), std::string::npos);
EXPECT_LT(
keywordOpenGLSource.find("#version 430"),
keywordOpenGLSource.find("#define XC_ALPHA_TEST 1"));
EXPECT_NE(
std::string(keywordOpenGLFragment->sourceCode.CStr()).find("LEGACY_KEYWORD_GL_PS"),
keywordOpenGLSource.find("LEGACY_KEYWORD_GL_PS"),
std::string::npos);
delete shader;
@@ -1464,6 +1469,33 @@ TEST(ShaderLoader, LoadBuiltinForwardLitShaderBuildsBackendVariants) {
std::string(alphaShadowD3D12Fragment->sourceCode.CStr()).find("gAlphaCutoffParams"),
std::string::npos);
const ShaderStageVariant* alphaShadowOpenGLFragment = shader->FindVariant(
"ForwardLit",
ShaderType::Fragment,
ShaderBackend::OpenGL,
alphaShadowKeywords);
ASSERT_NE(alphaShadowOpenGLFragment, nullptr);
const std::string alphaShadowOpenGLSource = alphaShadowOpenGLFragment->sourceCode.CStr();
EXPECT_NE(alphaShadowOpenGLSource.find("#version 430"), std::string::npos);
EXPECT_NE(alphaShadowOpenGLSource.find("#define XC_ALPHA_TEST 1"), std::string::npos);
EXPECT_LT(
alphaShadowOpenGLSource.find("#version 430"),
alphaShadowOpenGLSource.find("#define XC_ALPHA_TEST 1"));
EXPECT_NE(alphaShadowOpenGLSource.find("discard;"), std::string::npos);
const ShaderStageVariant* alphaShadowVulkanFragment = shader->FindVariant(
"ForwardLit",
ShaderType::Fragment,
ShaderBackend::Vulkan,
alphaShadowKeywords);
ASSERT_NE(alphaShadowVulkanFragment, nullptr);
const std::string alphaShadowVulkanSource = alphaShadowVulkanFragment->sourceCode.CStr();
EXPECT_NE(alphaShadowVulkanSource.find("#version 450"), std::string::npos);
EXPECT_NE(alphaShadowVulkanSource.find("#define XC_ALPHA_TEST 1"), std::string::npos);
EXPECT_LT(
alphaShadowVulkanSource.find("#version 450"),
alphaShadowVulkanSource.find("#define XC_ALPHA_TEST 1"));
const ShaderStageVariant* openglFragment = shader->FindVariant(
"ForwardLit",
ShaderType::Fragment,
@@ -1690,6 +1722,20 @@ TEST(ShaderLoader, LoadBuiltinDepthOnlyShaderBuildsBackendVariants) {
std::string(alphaD3D12Fragment->sourceCode.CStr()).find("gAlphaCutoffParams"),
std::string::npos);
const ShaderStageVariant* alphaOpenGLFragment = shader->FindVariant(
"DepthOnly",
ShaderType::Fragment,
ShaderBackend::OpenGL,
alphaKeywords);
ASSERT_NE(alphaOpenGLFragment, nullptr);
const std::string alphaOpenGLSource = alphaOpenGLFragment->sourceCode.CStr();
EXPECT_NE(alphaOpenGLSource.find("#version 430"), std::string::npos);
EXPECT_NE(alphaOpenGLSource.find("#define XC_ALPHA_TEST 1"), std::string::npos);
EXPECT_LT(
alphaOpenGLSource.find("#version 430"),
alphaOpenGLSource.find("#define XC_ALPHA_TEST 1"));
EXPECT_NE(alphaOpenGLSource.find("discard;"), std::string::npos);
delete shader;
}
@@ -1783,6 +1829,20 @@ TEST(ShaderLoader, LoadBuiltinShadowCasterShaderBuildsBackendVariants) {
std::string(alphaD3D12Fragment->sourceCode.CStr()).find("gAlphaCutoffParams"),
std::string::npos);
const ShaderStageVariant* alphaOpenGLFragment = shader->FindVariant(
"ShadowCaster",
ShaderType::Fragment,
ShaderBackend::OpenGL,
alphaKeywords);
ASSERT_NE(alphaOpenGLFragment, nullptr);
const std::string alphaOpenGLSource = alphaOpenGLFragment->sourceCode.CStr();
EXPECT_NE(alphaOpenGLSource.find("#version 430"), std::string::npos);
EXPECT_NE(alphaOpenGLSource.find("#define XC_ALPHA_TEST 1"), std::string::npos);
EXPECT_LT(
alphaOpenGLSource.find("#version 430"),
alphaOpenGLSource.find("#define XC_ALPHA_TEST 1"));
EXPECT_NE(alphaOpenGLSource.find("discard;"), std::string::npos);
delete shader;
}