Files
mirage/tests/utils/vulkan_test_base.cpp
daiqingshuang 2f7d23bf26 Add Vulkan compute scheduler tests and supporting utilities
- Implemented ComputeSchedulerTest to validate device management, task scheduling, and resource cleanup for the compute scheduler.
- Created mock_window class for testing Vulkan surface creation, supporting both headless and windowed modes.
- Added shader_loader utility for loading SPIR-V shaders from files and providing minimal embedded shaders for testing.
- Introduced test_helpers for creating and destroying shader modules, validating device features, and finding memory types.
- Established VulkanTestBase and VulkanGPUTest classes to streamline Vulkan test setup and teardown processes.
2025-11-23 11:32:01 +08:00

110 lines
3.4 KiB
C++

#include "vulkan_test_base.h"
#include <cstdlib>
#include <iostream>
namespace mirage::render::vulkan::test {
void VulkanTestBase::SetUp() {
// Base class setup
}
void VulkanTestBase::TearDown() {
// Base class teardown
}
bool VulkanTestBase::HasGPU() {
static bool checked = false;
static bool has_gpu = false;
if (!checked) {
try {
// Try to create a minimal Vulkan instance
vk::ApplicationInfo app_info{};
app_info.apiVersion = VK_API_VERSION_1_3;
vk::InstanceCreateInfo create_info{};
create_info.pApplicationInfo = &app_info;
auto instance_result = vk::createInstance(create_info);
if (instance_result.result == vk::Result::eSuccess) {
auto instance = instance_result.value;
auto devices_result = instance.enumeratePhysicalDevices();
has_gpu = devices_result.result == vk::Result::eSuccess && !devices_result.value.empty();
instance.destroy();
}
} catch (...) {
has_gpu = false;
}
checked = true;
}
return has_gpu;
}
bool VulkanTestBase::IsCI() {
// Check common CI environment variables
const char* ci_env = std::getenv("CI");
const char* github_actions = std::getenv("GITHUB_ACTIONS");
const char* gitlab_ci = std::getenv("GITLAB_CI");
const char* jenkins = std::getenv("JENKINS_HOME");
return (ci_env != nullptr) || (github_actions != nullptr) ||
(gitlab_ci != nullptr) || (jenkins != nullptr);
}
std::string VulkanTestBase::GetTestName() const {
const ::testing::TestInfo* test_info = ::testing::UnitTest::GetInstance()->current_test_info();
return std::string(test_info->test_suite_name()) + "." + std::string(test_info->name());
}
void VulkanGPUTest::SetUp() {
VulkanTestBase::SetUp();
// Skip test if no GPU available
if (!HasGPU()) {
GTEST_SKIP() << "No GPU available, skipping test: " << GetTestName();
return;
}
// Create Vulkan context
render_context::create_info ctx_info{};
ctx_info.app_name = "Mirage Vulkan Tests";
ctx_info.enable_validation = false; // Disable validation in tests for performance
auto ctx_result = render_context::create(ctx_info);
if (!ctx_result.has_value()) {
GTEST_SKIP() << "Failed to create Vulkan context: " << vk::to_string(ctx_result.error());
return;
}
context_ = std::make_unique<render_context>(std::move(ctx_result.value()));
// Create device manager
device_mgr_ = std::make_unique<device_manager>(context_->get_instance());
// Select primary device
auto device_result = device_mgr_->select_primary_device();
if (!device_result.has_value()) {
GTEST_SKIP() << "Failed to select primary device: " << vk::to_string(device_result.error());
return;
}
physical_device_ = device_result.value();
// Create logical device
auto logical_result = logical_device::create(physical_device_);
if (!logical_result.has_value()) {
GTEST_SKIP() << "Failed to create logical device: " << vk::to_string(logical_result.error());
return;
}
device_ = std::make_unique<logical_device>(std::move(logical_result.value()));
}
void VulkanGPUTest::TearDown() {
// Clean up in reverse order
device_.reset();
device_mgr_.reset();
context_.reset();
VulkanTestBase::TearDown();
}
} // namespace mirage::render::vulkan::test