import std;
import vulkan_raii;
class VulkanApplication {
public:
VulkanApplication() {
// Initialize crash handler
crash_handler::initialize("MyVulkanApp", "crash_logs");
// Initialize Vulkan with debugging and robustness
initialize_vulkan();
}
void run() {
// Main application loop
while (!should_close()) {
try {
update();
render();
} catch (const vk::SystemError& e) {
// Handle recoverable Vulkan errors
std::cerr << "Vulkan error: " << e.what() << std::endl;
if (!recover_from_error()) {
break;
}
}
}
cleanup();
}
private:
void initialize_vulkan() {
// Create instance with validation layers in debug builds
#ifdef _DEBUG
enable_validation_layers = true;
#else
enable_validation_layers = false;
#endif
// Create instance
instance = create_instance();
// Set up debug messenger if validation is enabled
if (enable_validation_layers) {
debug_messenger = create_debug_messenger(instance);
}
// Select physical device
physical_device = select_physical_device(instance);
// Check for robustness support
has_robustness2 = check_robustness2_support(physical_device);
// Create logical device with robustness if available
device = create_device(physical_device);
// Name Vulkan objects for debugging
if (enable_validation_layers) {
debug_utils::set_name(device, *device, "Main Device");
// Name other objects as they're created
}
// Initialize other Vulkan resources
// ...
}
void render() {
// Begin frame
auto cmd_buffer = begin_frame();
// Label command buffer regions for debugging
if (enable_validation_layers) {
vk::DebugUtilsLabelEXT label_info{};
label_info.setPLabelName("Main Render Pass");
label_info.setColor(std::array<float, 4>{0.0f, 1.0f, 0.0f, 1.0f});
cmd_buffer.beginDebugUtilsLabelEXT(label_info);
}
// Record rendering commands
// ...
// End debug label
if (enable_validation_layers) {
cmd_buffer.endDebugUtilsLabelEXT();
}
// End frame
end_frame(cmd_buffer);
// Capture frame with RenderDoc if requested
if (capture_next_frame) {
if (renderdoc_api) {
renderdoc_api->TriggerCapture();
}
capture_next_frame = false;
}
}
// Vulkan objects
vk::raii::Context context;
vk::raii::Instance instance{nullptr};
vk::raii::DebugUtilsMessengerEXT debug_messenger{nullptr};
vk::raii::PhysicalDevice physical_device{nullptr};
vk::raii::Device device{nullptr};
// Flags
bool enable_validation_layers = false;
bool has_robustness2 = false;
bool capture_next_frame = false;
// RenderDoc API
RENDERDOC_API_1_4_1* renderdoc_api = nullptr;
};