125 lines
4.0 KiB
C++
125 lines
4.0 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 {
|
|
// Initialize Dynamic Loader before any Vulkan calls
|
|
static vk::detail::DynamicLoader dl;
|
|
auto vkGetInstanceProcAddr = dl.getProcAddress<PFN_vkGetInstanceProcAddr>("vkGetInstanceProcAddr");
|
|
if (!vkGetInstanceProcAddr) {
|
|
has_gpu = false;
|
|
checked = true;
|
|
return has_gpu;
|
|
}
|
|
|
|
VULKAN_HPP_DEFAULT_DISPATCHER.init(vkGetInstanceProcAddr);
|
|
|
|
// 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;
|
|
|
|
// Initialize dispatcher with instance
|
|
VULKAN_HPP_DEFAULT_DISPATCHER.init(instance);
|
|
|
|
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
|