vulkan-test/src/main.cpp

1871 lines
66 KiB
C++
Raw Normal View History

2024-10-12 15:39:20 -04:00
// Time measurements
#include <chrono>
2024-10-12 15:39:20 -04:00
// String formatting
2024-09-30 00:57:13 -04:00
#include <fmt/format.h>
2024-10-14 23:32:52 -04:00
// Unordered sets
#include <unordered_set>
2024-09-25 23:03:56 -04:00
2024-10-12 15:39:20 -04:00
// Make depth go from 0 to 1 instead of -1 to 1
2024-10-11 19:09:37 -04:00
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
2024-10-12 15:39:20 -04:00
// Provides various math-related data structures
2024-10-11 19:09:37 -04:00
#include <glm/glm.hpp>
2024-10-12 15:39:20 -04:00
// Used to load models
2024-10-11 20:04:28 -04:00
#define TINYOBJLOADER_IMPLEMENTATION
#include <tiny_obj_loader.h>
2024-10-12 15:39:20 -04:00
// Dynamic function loading
2024-10-06 12:26:44 -04:00
#define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
2024-10-12 15:39:20 -04:00
// Use the beta extensions
2024-09-30 21:21:38 -04:00
#define VK_ENABLE_BETA_EXTENSIONS
2024-10-12 15:39:20 -04:00
// Use {} instead of ()
2024-09-29 23:02:04 -04:00
#define VULKAN_HPP_NO_CONSTRUCTORS
2024-10-12 15:39:20 -04:00
// Vulkan itself
2024-09-29 23:02:04 -04:00
#include <vulkan/vulkan.hpp>
2024-10-12 15:39:20 -04:00
// Needed for dynamic function loading to work
2024-10-06 12:26:44 -04:00
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
2024-10-12 15:39:20 -04:00
// Type aliases
2024-09-28 19:38:13 -04:00
#include "util/types.h"
2024-10-12 15:39:20 -04:00
// STB image helper
2024-10-12 00:07:13 -04:00
#include "util/unique_image.h"
2024-10-05 23:08:12 -04:00
2024-10-12 15:39:20 -04:00
// Vertex class
#include "util/vertex.h"
// Use {} instead of ()
#define VKFW_NO_STRUCT_CONSTRUCTORS
// GLFW C++ wrapper
#include "vkfw.hpp"
2024-09-25 23:03:56 -04:00
2024-10-12 15:39:20 -04:00
// Initial width and height
constexpr i32 WIDTH = 800;
constexpr i32 HEIGHT = 600;
2024-09-25 23:03:56 -04:00
2024-10-12 15:39:20 -04:00
// Paths for the model and texture to render
2024-10-11 20:04:28 -04:00
constexpr const char* MODEL_PATH = "models/viking_room.obj";
constexpr const char* TEXTURE_PATH = "textures/viking_room.png";
2024-10-12 15:39:20 -04:00
// Paths for the shaders
constexpr const char* FRAGMENT_SHADER_PATH = "shaders/frag.spv";
constexpr const char* VERTEX_SHADER_PATH = "shaders/vert.spv";
2024-10-05 23:08:12 -04:00
2024-10-12 15:39:20 -04:00
// Maximum number of frames to be worked on at the same time
constexpr i32 MAX_FRAMES_IN_FLIGHT = 2;
2024-10-14 23:32:52 -04:00
// Enable validation layers only in debug mode
#ifndef NDEBUG
constexpr std::array<const char*, 1> validationLayers = { "VK_LAYER_KHRONOS_validation" };
#endif
2024-10-12 15:39:20 -04:00
// macOS requires the portability extension to be enabled
#ifdef __APPLE__
constexpr std::array<const char*, 2> deviceExtensions = { vk::KHRSwapchainExtensionName,
vk::KHRPortabilitySubsetExtensionName };
#else
constexpr std::array<const char*, 1> deviceExtensions = { vk::KHRSwapchainExtensionName };
#endif
2024-09-29 23:11:12 -04:00
class VulkanApp {
2024-09-25 23:03:56 -04:00
public:
2024-09-30 00:57:13 -04:00
fn run() -> void {
2024-10-12 15:39:20 -04:00
// Create window
2024-09-25 23:03:56 -04:00
initWindow();
2024-10-12 15:39:20 -04:00
// Setup vulkan
2024-09-25 23:03:56 -04:00
initVulkan();
2024-10-12 15:39:20 -04:00
// Render loop
2024-09-25 23:03:56 -04:00
mainLoop();
}
private:
2024-10-12 19:05:31 -04:00
// GLFW instance
2024-10-12 15:39:20 -04:00
vkfw::UniqueInstance mVKFWInstance;
2024-10-12 19:05:31 -04:00
// GLFW window
vkfw::UniqueWindow mWindow;
2024-09-28 18:13:24 -04:00
2024-10-12 19:05:31 -04:00
// Vulkan instance
2024-09-29 23:02:04 -04:00
vk::UniqueInstance mInstance;
2024-09-25 23:03:56 -04:00
2024-10-12 19:05:31 -04:00
// Debug messenger
2024-10-06 12:26:44 -04:00
vk::UniqueDebugUtilsMessengerEXT mDebugMessenger;
2024-10-12 19:05:31 -04:00
// Window surface
vk::UniqueSurfaceKHR mSurface;
2024-09-30 00:31:08 -04:00
2024-10-12 19:05:31 -04:00
// Represents the physical device
vk::PhysicalDevice mPhysicalDevice;
// Number of MSAA samples
2024-10-12 15:39:20 -04:00
vk::SampleCountFlagBits mMsaaSamples;
2024-10-12 19:05:31 -04:00
// Represents the logical device
vk::UniqueDevice mDevice;
2024-09-30 00:57:13 -04:00
vk::Queue mGraphicsQueue;
vk::Queue mPresentQueue;
2024-10-01 18:54:41 -04:00
vk::UniqueSwapchainKHR mSwapChain;
std::vector<vk::Image> mSwapChainImages;
vk::Format mSwapChainImageFormat;
vk::Extent2D mSwapChainExtent;
std::vector<vk::UniqueImageView> mSwapChainImageViews;
std::vector<vk::UniqueFramebuffer> mSwapChainFramebuffers;
2024-10-01 16:57:40 -04:00
vk::UniqueRenderPass mRenderPass;
vk::UniqueDescriptorSetLayout mDescriptorSetLayout;
vk::UniquePipelineLayout mPipelineLayout;
vk::UniquePipeline mGraphicsPipeline;
2024-10-01 18:30:31 -04:00
vk::UniqueCommandPool mCommandPool;
2024-10-01 18:54:41 -04:00
2024-10-11 21:14:36 -04:00
vk::UniqueImage mColorImage;
vk::UniqueDeviceMemory mColorImageMemory;
vk::UniqueImageView mColorImageView;
2024-10-11 19:09:37 -04:00
vk::UniqueImage mDepthImage;
vk::UniqueDeviceMemory mDepthImageMemory;
vk::UniqueImageView mDepthImageView;
2024-10-11 20:50:23 -04:00
u32 mMipLevels;
2024-10-11 00:42:58 -04:00
vk::UniqueImage mTextureImage;
vk::UniqueDeviceMemory mTextureImageMemory;
vk::UniqueImageView mTextureImageView;
vk::UniqueSampler mTextureSampler;
2024-10-11 20:04:28 -04:00
std::vector<Vertex> mVertices;
std::vector<u32> mIndices;
vk::UniqueBuffer mVertexBuffer;
vk::UniqueDeviceMemory mVertexBufferMemory;
2024-10-10 18:51:20 -04:00
vk::UniqueBuffer mIndexBuffer;
vk::UniqueDeviceMemory mIndexBufferMemory;
std::vector<vk::UniqueBuffer> mUniformBuffers;
std::vector<vk::UniqueDeviceMemory> mUniformBuffersMemory;
std::vector<void*> mUniformBuffersMapped;
vk::UniqueDescriptorPool mDescriptorPool;
std::vector<vk::DescriptorSet> mDescriptorSets;
std::vector<vk::UniqueCommandBuffer> mCommandBuffers;
std::vector<vk::UniqueSemaphore> mImageAvailableSemaphores;
std::vector<vk::UniqueSemaphore> mRenderFinishedSemaphores;
std::vector<vk::UniqueFence> mInFlightFences;
2024-10-05 23:08:12 -04:00
bool mFramebufferResized = false;
u32 mCurrentFrame = 0;
2024-10-01 18:33:05 -04:00
2024-09-30 00:31:08 -04:00
struct QueueFamilyIndices {
std::optional<u32> graphics_family;
2024-09-30 00:57:13 -04:00
std::optional<u32> present_family;
2024-09-30 00:31:08 -04:00
2024-09-30 00:57:13 -04:00
fn isComplete() -> bool { return graphics_family.has_value() && present_family.has_value(); }
2024-09-30 00:31:08 -04:00
};
2024-09-28 21:55:26 -04:00
struct SwapChainSupportDetails {
vk::SurfaceCapabilitiesKHR capabilities;
std::vector<vk::SurfaceFormatKHR> formats;
std::vector<vk::PresentModeKHR> present_modes;
};
struct UniformBufferObject {
alignas(16) glm::mat4 model;
alignas(16) glm::mat4 view;
alignas(16) glm::mat4 proj;
};
2024-10-14 23:32:52 -04:00
// Read a file into a vector of chars
2024-10-01 18:30:31 -04:00
static fn readFile(const std::string& filename) -> std::vector<char> {
2024-10-14 23:32:52 -04:00
// Open the file in binary mode
std::ifstream file(filename, std::ios::binary);
2024-10-01 18:30:31 -04:00
2024-10-14 23:32:52 -04:00
// Make sure the file was opened
if (!file)
throw std::runtime_error("Failed to open file: " + filename);
2024-10-01 18:30:31 -04:00
2024-10-14 23:32:52 -04:00
// Read the contents of the file into a vector
std::vector<char> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
2024-10-01 18:30:31 -04:00
return buffer;
}
2024-10-14 23:32:52 -04:00
// Create a GLFW window
2024-09-30 00:57:13 -04:00
fn initWindow() -> void {
2024-10-14 23:32:52 -04:00
// Initialize GLFW
2024-10-12 15:39:20 -04:00
mVKFWInstance = vkfw::initUnique();
2024-09-25 23:03:56 -04:00
2024-10-14 23:32:52 -04:00
// Set window hints
vkfw::WindowHints hints {
.clientAPI = vkfw::ClientAPI::eNone, // No client API (Vulkan)
};
2024-09-30 16:46:17 -04:00
2024-10-14 23:32:52 -04:00
// Create the window
mWindow = vkfw::createWindowUnique(WIDTH, HEIGHT, "Vulkan", hints);
2024-10-14 23:32:52 -04:00
// Set the window user pointer to this class
2024-10-05 23:08:12 -04:00
mWindow->setUserPointer(this);
2024-10-14 23:32:52 -04:00
// Set the framebuffer resize callback
2024-10-12 15:39:20 -04:00
mWindow->callbacks()->on_window_resize =
2024-10-14 23:32:52 -04:00
[](const vkfw::Window& window, usize /*width*/, usize /*height*/) -> void {
// Set the framebuffer resized flag
2024-10-12 15:39:20 -04:00
std::bit_cast<VulkanApp*>(window.getUserPointer())->mFramebufferResized = true;
};
2024-09-25 23:03:56 -04:00
}
2024-10-14 23:32:52 -04:00
// Initialize Vulkan
2024-09-30 00:57:13 -04:00
fn initVulkan() -> void {
2024-09-28 18:13:24 -04:00
createInstance();
setupDebugMessenger();
2024-09-30 00:57:13 -04:00
createSurface();
2024-09-30 00:31:08 -04:00
pickPhysicalDevice();
2024-09-30 00:57:13 -04:00
createLogicalDevice();
2024-10-01 16:57:40 -04:00
createSwapChain();
2024-10-01 17:06:14 -04:00
createImageViews();
2024-10-01 18:30:31 -04:00
createRenderPass();
createDescriptorSetLayout();
2024-10-01 18:30:31 -04:00
createGraphicsPipeline();
2024-10-01 18:54:41 -04:00
createCommandPool();
2024-10-11 21:14:36 -04:00
createColorResources();
2024-10-11 19:09:37 -04:00
createDepthResources();
createFramebuffers();
2024-10-11 00:42:58 -04:00
createTextureImage();
createTextureImageView();
createTextureSampler();
2024-10-11 20:04:28 -04:00
loadModel();
createVertexBuffer();
2024-10-10 18:51:20 -04:00
createIndexBuffer();
createUniformBuffers();
createDescriptorPool();
createDescriptorSets();
2024-10-05 23:08:12 -04:00
createCommandBuffers();
2024-10-01 18:54:41 -04:00
createSyncObjects();
2024-09-29 23:02:04 -04:00
}
2024-09-28 14:54:39 -04:00
2024-10-14 23:32:52 -04:00
// Main loop
2024-09-30 00:57:13 -04:00
fn mainLoop() -> void {
2024-10-14 23:32:52 -04:00
// While the window is open,
2024-10-01 18:54:41 -04:00
while (!mWindow->shouldClose()) {
2024-10-14 23:32:52 -04:00
// Poll for events
2024-10-10 21:36:42 -04:00
vkfw::pollEvents();
2024-10-14 23:32:52 -04:00
// Draw a frame
2024-10-01 18:54:41 -04:00
drawFrame();
}
2024-10-14 23:32:52 -04:00
// Wait for the device to finish
2024-10-06 18:14:15 -04:00
mDevice->waitIdle();
2024-09-28 14:54:39 -04:00
}
2024-10-14 23:32:52 -04:00
// Clean up the swap chain
2024-10-05 23:08:12 -04:00
fn cleanupSwapChain() -> void {
2024-10-11 19:09:37 -04:00
for (vk::UniqueFramebuffer& mSwapChainFramebuffer : mSwapChainFramebuffers) mSwapChainFramebuffer.reset();
for (vk::UniqueImageView& mSwapChainImageView : mSwapChainImageViews) mSwapChainImageView.reset();
2024-10-05 23:08:12 -04:00
mSwapChain.reset();
}
2024-10-14 23:32:52 -04:00
// Recreate the swap chain
2024-10-05 23:08:12 -04:00
fn recreateSwapChain() -> void {
2024-10-14 23:32:52 -04:00
// Get the new width and height
auto [width, height] = mWindow->getFramebufferSize();
2024-10-05 23:08:12 -04:00
2024-10-14 23:32:52 -04:00
// If the width or height are 0, wait for events
2024-10-05 23:08:12 -04:00
while (width == 0 || height == 0) {
std::tie(width, height) = mWindow->getFramebufferSize();
vkfw::waitEvents();
}
2024-10-14 23:32:52 -04:00
// Wait for the device to finish
2024-10-06 18:14:15 -04:00
mDevice->waitIdle();
2024-10-05 23:08:12 -04:00
2024-10-14 23:32:52 -04:00
// Clean up the swap chain
2024-10-05 23:08:12 -04:00
cleanupSwapChain();
2024-10-14 23:32:52 -04:00
// Create a new swap chain
2024-10-05 23:08:12 -04:00
createSwapChain();
createImageViews();
2024-10-11 21:14:36 -04:00
createColorResources();
2024-10-11 19:09:37 -04:00
createDepthResources();
2024-10-05 23:08:12 -04:00
createFramebuffers();
}
2024-10-14 23:32:52 -04:00
// Create a Vulkan instance
2024-09-25 23:03:56 -04:00
fn createInstance() -> void {
2024-10-14 23:32:52 -04:00
#ifndef NDEBUG
// Make sure validation layers are supported
if (!checkValidationLayerSupport())
2024-09-30 21:21:38 -04:00
throw std::runtime_error("Validation layers requested, but not available!");
2024-10-14 23:32:52 -04:00
#endif
// Application metadata
vk::ApplicationInfo appInfo {
.pApplicationName = "Hello Triangle",
.applicationVersion = 1,
.pEngineName = "No Engine",
.engineVersion = 1,
.apiVersion = vk::ApiVersion12,
};
2024-09-25 23:03:56 -04:00
2024-10-14 23:32:52 -04:00
// Get the required extensions
std::span<const char*> extensionsSpan = vkfw::getRequiredInstanceExtensions();
2024-09-25 23:03:56 -04:00
2024-10-14 23:32:52 -04:00
// Convert the span to a vector
std::vector extensions(extensionsSpan.begin(), extensionsSpan.end());
#ifndef NDEBUG
// Add the debug utils extension
extensions.emplace_back(vk::EXTDebugUtilsExtensionName);
#endif
2024-09-26 19:56:19 -04:00
#ifdef __APPLE__
2024-10-14 23:32:52 -04:00
// Add the portability extension
2024-10-01 14:15:39 -04:00
extensions.emplace_back(vk::KHRPortabilityEnumerationExtensionName);
2024-10-14 23:32:52 -04:00
// Technically deprecated but Vulkan complains if I don't include it for macOS,
// so instead of using the vk::KHRPortabilitySubsetExtensionName, I just use
2024-10-01 14:15:39 -04:00
// the direct string.
extensions.emplace_back("VK_KHR_get_physical_device_properties2");
2024-09-30 21:21:38 -04:00
#endif
2024-09-30 00:31:08 -04:00
2024-10-14 23:32:52 -04:00
// Create the instance
2024-09-29 23:05:15 -04:00
vk::InstanceCreateInfo createInfo {
2024-10-01 16:57:40 -04:00
#ifdef __APPLE__
2024-10-14 23:32:52 -04:00
// Enable the portability extension
2024-10-01 16:57:40 -04:00
.flags = vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR,
#endif
2024-10-14 23:32:52 -04:00
.pApplicationInfo = &appInfo,
#ifdef NDEBUG
.enabledLayerCount = 0,
.ppEnabledLayerNames = nullptr,
#else
.enabledLayerCount = static_cast<u32>(validationLayers.size()),
.ppEnabledLayerNames = validationLayers.data(),
#endif
.enabledExtensionCount = static_cast<u32>(extensions.size()),
2024-09-30 00:31:08 -04:00
.ppEnabledExtensionNames = extensions.data()
2024-09-29 23:05:15 -04:00
};
2024-09-26 17:18:45 -04:00
2024-09-30 00:31:08 -04:00
#ifndef NDEBUG
2024-09-29 23:02:04 -04:00
fmt::println("Available extensions:");
for (const char* extension : extensions) fmt::println("\t{}", extension);
2024-09-30 00:31:08 -04:00
#endif
2024-09-25 23:03:56 -04:00
2024-10-14 23:32:52 -04:00
// Create the instance
2024-10-06 18:14:15 -04:00
mInstance = vk::createInstanceUnique(createInfo);
2024-10-06 17:34:25 -04:00
2024-10-14 23:32:52 -04:00
// Load the instance functions
2024-10-06 12:26:44 -04:00
VULKAN_HPP_DEFAULT_DISPATCHER.init(mInstance.get());
2024-09-28 14:54:39 -04:00
}
2024-10-14 23:32:52 -04:00
// Create the debug messenger
2024-09-30 00:57:13 -04:00
fn setupDebugMessenger() -> void {
2024-10-14 23:32:52 -04:00
#ifdef NDEBUG
return;
#endif
2024-09-28 18:13:24 -04:00
2024-09-30 00:31:08 -04:00
vk::DebugUtilsMessengerCreateInfoEXT messengerCreateInfo {
.messageSeverity = vk::DebugUtilsMessageSeverityFlagBitsEXT::eVerbose |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eError,
.messageType = vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral |
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation |
vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance,
2024-10-12 15:39:20 -04:00
.pfnUserCallback = debugCallback
2024-09-30 00:31:08 -04:00
};
2024-09-28 18:13:24 -04:00
2024-10-06 18:14:15 -04:00
mDebugMessenger = mInstance->createDebugUtilsMessengerEXTUnique(messengerCreateInfo, nullptr);
2024-09-30 00:31:08 -04:00
}
2024-09-28 18:13:24 -04:00
2024-10-14 23:32:52 -04:00
// Create the GLFW window surface
2024-09-30 16:46:17 -04:00
fn createSurface() -> void { mSurface = vkfw::createWindowSurfaceUnique(mInstance.get(), mWindow.get()); }
2024-09-30 00:57:13 -04:00
2024-10-14 23:32:52 -04:00
// Choose the best-suited physical device
2024-09-30 00:31:08 -04:00
fn pickPhysicalDevice() -> void {
2024-10-14 23:32:52 -04:00
// Get all physical devices
2024-10-06 18:14:15 -04:00
std::vector<vk::PhysicalDevice> devices = mInstance->enumeratePhysicalDevices();
2024-09-30 00:31:08 -04:00
2024-10-14 23:32:52 -04:00
// Make sure there are supported devices
2024-10-06 18:14:15 -04:00
if (devices.empty())
2024-09-30 00:31:08 -04:00
throw std::runtime_error("Failed to find GPUs with Vulkan support!");
#ifndef NDEBUG
fmt::println("Available devices:");
#endif
2024-10-14 23:32:52 -04:00
// For each device,
2024-10-06 18:14:15 -04:00
for (const vk::PhysicalDevice& device : devices) {
2024-09-30 00:31:08 -04:00
#ifndef NDEBUG
vk::PhysicalDeviceProperties properties = device.getProperties();
2024-09-30 19:49:44 -04:00
fmt::println("\t{}", properties.deviceName.data());
2024-09-30 00:31:08 -04:00
#endif
2024-10-14 23:32:52 -04:00
// Set the first suitable device as the physical device
2024-09-30 00:31:08 -04:00
if (isDeviceSuitable(device)) {
mPhysicalDevice = device;
2024-10-11 21:14:36 -04:00
mMsaaSamples = getMaxUsableSampleCount();
2024-09-30 00:31:08 -04:00
break;
}
2024-09-28 18:13:24 -04:00
}
2024-10-14 23:32:52 -04:00
// If no suitable device was found, throw an error
2024-09-30 00:31:08 -04:00
if (!mPhysicalDevice)
throw std::runtime_error("Failed to find a suitable GPU!");
}
2024-10-14 23:32:52 -04:00
// Create the logical device
2024-09-30 00:57:13 -04:00
fn createLogicalDevice() -> void {
2024-10-14 23:32:52 -04:00
// Get the queue families
2024-10-10 18:51:20 -04:00
QueueFamilyIndices qfIndices = findQueueFamilies(mPhysicalDevice);
2024-09-30 00:57:13 -04:00
std::vector<vk::DeviceQueueCreateInfo> queueCreateInfos;
2024-10-14 23:32:52 -04:00
std::set<u32> uniqueQueueFamilies = {
qfIndices.graphics_family.value(),
qfIndices.present_family.value(),
};
// Set the queue priority
2024-09-30 00:57:13 -04:00
f32 queuePriority = 1.0F;
2024-10-14 23:32:52 -04:00
// For each unique queue family, create a new queue
2024-09-30 00:57:13 -04:00
for (u32 queueFamily : uniqueQueueFamilies) {
2024-10-14 23:32:52 -04:00
vk::DeviceQueueCreateInfo queueCreateInfo {
.queueFamilyIndex = queueFamily,
.queueCount = 1,
.pQueuePriorities = &queuePriority,
};
2024-09-30 00:57:13 -04:00
2024-10-01 14:15:39 -04:00
queueCreateInfos.emplace_back(queueCreateInfo);
2024-09-30 00:57:13 -04:00
}
2024-10-14 23:32:52 -04:00
// Enable anisotropic filtering
2024-10-11 00:42:58 -04:00
vk::PhysicalDeviceFeatures deviceFeatures {
.samplerAnisotropy = vk::True,
};
2024-09-30 00:57:13 -04:00
2024-10-14 23:32:52 -04:00
vk::DeviceCreateInfo createInfo {
.queueCreateInfoCount = static_cast<u32>(queueCreateInfos.size()),
.pQueueCreateInfos = queueCreateInfos.data(),
.enabledExtensionCount = static_cast<u32>(deviceExtensions.size()),
.ppEnabledExtensionNames = deviceExtensions.data(),
.pEnabledFeatures = &deviceFeatures,
};
2024-09-30 00:57:13 -04:00
2024-10-14 23:32:52 -04:00
// Create the logical device and set the graphics and present queues
2024-10-06 18:14:15 -04:00
mDevice = mPhysicalDevice.createDeviceUnique(createInfo);
2024-10-10 18:51:20 -04:00
mGraphicsQueue = mDevice->getQueue(qfIndices.graphics_family.value(), 0);
mPresentQueue = mDevice->getQueue(qfIndices.present_family.value(), 0);
2024-09-30 00:57:13 -04:00
}
2024-10-01 16:57:40 -04:00
fn createSwapChain() -> void {
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(mPhysicalDevice);
vk::SurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
vk::PresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.present_modes);
vk::Extent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
u32 imageCount = swapChainSupport.capabilities.minImageCount + 1;
if (swapChainSupport.capabilities.maxImageCount > 0 &&
imageCount > swapChainSupport.capabilities.maxImageCount)
imageCount = swapChainSupport.capabilities.maxImageCount;
2024-10-10 18:51:20 -04:00
QueueFamilyIndices qfIndices = findQueueFamilies(mPhysicalDevice);
2024-10-14 23:32:52 -04:00
std::array<u32, 2> queueFamilyIndices = {
qfIndices.graphics_family.value(),
qfIndices.present_family.value(),
};
2024-10-01 16:57:40 -04:00
vk::SwapchainCreateInfoKHR createInfo {
.surface = mSurface.get(),
.minImageCount = imageCount,
.imageFormat = surfaceFormat.format,
.imageColorSpace = surfaceFormat.colorSpace,
.imageExtent = extent,
.imageArrayLayers = 1,
.imageUsage = vk::ImageUsageFlagBits::eColorAttachment,
2024-10-10 18:51:20 -04:00
.imageSharingMode = qfIndices.graphics_family != qfIndices.present_family ? vk::SharingMode::eConcurrent
: vk::SharingMode::eExclusive,
.queueFamilyIndexCount =
static_cast<u32>(qfIndices.graphics_family != qfIndices.present_family ? 2 : 0),
2024-10-01 16:57:40 -04:00
.pQueueFamilyIndices =
2024-10-10 18:51:20 -04:00
qfIndices.graphics_family != qfIndices.present_family ? queueFamilyIndices.data() : nullptr,
2024-10-01 16:57:40 -04:00
.preTransform = swapChainSupport.capabilities.currentTransform,
.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque,
.presentMode = presentMode,
2024-10-01 18:30:31 -04:00
.clipped = vk::True,
2024-10-01 16:57:40 -04:00
.oldSwapchain = nullptr,
};
2024-10-06 18:14:15 -04:00
mSwapChain = mDevice->createSwapchainKHRUnique(createInfo);
2024-10-06 17:34:25 -04:00
2024-10-06 18:14:15 -04:00
mSwapChainImages = mDevice->getSwapchainImagesKHR(mSwapChain.get());
2024-10-01 16:57:40 -04:00
mSwapChainImageFormat = surfaceFormat.format;
mSwapChainExtent = extent;
}
2024-10-01 17:06:14 -04:00
fn createImageViews() -> void {
mSwapChainImageViews.resize(mSwapChainImages.size());
2024-10-11 19:09:37 -04:00
for (u32 i = 0; i < mSwapChainImages.size(); i++)
mSwapChainImageViews[i] =
2024-10-11 20:50:23 -04:00
createImageView(mSwapChainImages[i], mSwapChainImageFormat, vk::ImageAspectFlagBits::eColor, 1);
}
2024-10-01 18:30:31 -04:00
fn createRenderPass() -> void {
2024-10-14 23:32:52 -04:00
vk::AttachmentDescription colorAttachment {
.format = mSwapChainImageFormat,
.samples = mMsaaSamples,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eStore,
.stencilLoadOp = vk::AttachmentLoadOp::eDontCare,
.stencilStoreOp = vk::AttachmentStoreOp::eDontCare,
.initialLayout = vk::ImageLayout::eUndefined,
.finalLayout = vk::ImageLayout::eColorAttachmentOptimal,
};
vk::AttachmentDescription depthAttachment {
.format = findDepthFormat(),
.samples = mMsaaSamples,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eDontCare,
.stencilLoadOp = vk::AttachmentLoadOp::eDontCare,
.stencilStoreOp = vk::AttachmentStoreOp::eDontCare,
.initialLayout = vk::ImageLayout::eUndefined,
.finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
};
vk::AttachmentDescription colorAttachmentResolve {
.format = mSwapChainImageFormat,
.samples = vk::SampleCountFlagBits::e1,
.loadOp = vk::AttachmentLoadOp::eDontCare,
.storeOp = vk::AttachmentStoreOp::eStore,
.stencilLoadOp = vk::AttachmentLoadOp::eDontCare,
.stencilStoreOp = vk::AttachmentStoreOp::eDontCare,
.initialLayout = vk::ImageLayout::eUndefined,
.finalLayout = vk::ImageLayout::ePresentSrcKHR,
};
vk::AttachmentReference colorAttachmentRef {
.attachment = 0,
.layout = vk::ImageLayout::eColorAttachmentOptimal,
};
vk::AttachmentReference depthAttachmentRef {
.attachment = 1,
.layout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
};
vk::AttachmentReference colorAttachmentResolveRef {
.attachment = 2,
.layout = vk::ImageLayout::eColorAttachmentOptimal,
};
vk::SubpassDescription subpass {
.pipelineBindPoint = vk::PipelineBindPoint::eGraphics,
.colorAttachmentCount = 1,
.pColorAttachments = &colorAttachmentRef,
.pResolveAttachments = &colorAttachmentResolveRef,
.pDepthStencilAttachment = &depthAttachmentRef,
};
vk::SubpassDependency dependency {
.srcSubpass = vk::SubpassExternal,
.dstSubpass = {},
.srcStageMask =
vk::PipelineStageFlagBits::eColorAttachmentOutput | vk::PipelineStageFlagBits::eEarlyFragmentTests,
.dstStageMask =
vk::PipelineStageFlagBits::eColorAttachmentOutput | vk::PipelineStageFlagBits::eEarlyFragmentTests,
.srcAccessMask = {},
.dstAccessMask =
vk::AccessFlagBits::eColorAttachmentWrite | vk::AccessFlagBits::eDepthStencilAttachmentWrite,
};
std::array<vk::AttachmentDescription, 3> attachments = {
colorAttachment,
depthAttachment,
colorAttachmentResolve,
};
vk::RenderPassCreateInfo renderPassInfo {
.attachmentCount = static_cast<u32>(attachments.size()),
.pAttachments = attachments.data(),
.subpassCount = 1,
.pSubpasses = &subpass,
.dependencyCount = 1,
.pDependencies = &dependency,
};
2024-10-01 18:30:31 -04:00
2024-10-06 18:14:15 -04:00
mRenderPass = mDevice->createRenderPassUnique(renderPassInfo);
2024-10-01 18:30:31 -04:00
}
fn createDescriptorSetLayout() -> void {
vk::DescriptorSetLayoutBinding uboLayoutBinding {
.binding = 0,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eVertex,
.pImmutableSamplers = nullptr,
};
2024-10-11 00:42:58 -04:00
vk::DescriptorSetLayoutBinding samplerLayoutBinding {
.binding = 1,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eFragment,
.pImmutableSamplers = nullptr,
};
std::array<vk::DescriptorSetLayoutBinding, 2> bindings = { uboLayoutBinding, samplerLayoutBinding };
vk::DescriptorSetLayoutCreateInfo layoutInfo {
2024-10-11 00:42:58 -04:00
.bindingCount = static_cast<u32>(bindings.size()),
.pBindings = bindings.data(),
};
mDescriptorSetLayout = mDevice->createDescriptorSetLayoutUnique(layoutInfo);
}
2024-10-01 18:30:31 -04:00
fn createGraphicsPipeline() -> void {
2024-10-12 15:39:20 -04:00
std::vector<char> vertShaderCode = readFile(VERTEX_SHADER_PATH);
std::vector<char> fragShaderCode = readFile(FRAGMENT_SHADER_PATH);
2024-10-01 18:30:31 -04:00
vk::UniqueShaderModule vertShaderModule = createShaderModule(vertShaderCode);
vk::UniqueShaderModule fragShaderModule = createShaderModule(fragShaderCode);
2024-10-14 23:32:52 -04:00
vk::PipelineShaderStageCreateInfo vertShaderStageInfo {
.stage = vk::ShaderStageFlagBits::eVertex,
.module = vertShaderModule.get(),
.pName = "main",
};
2024-10-01 18:30:31 -04:00
2024-10-14 23:32:52 -04:00
vk::PipelineShaderStageCreateInfo fragShaderStageInfo {
.stage = vk::ShaderStageFlagBits::eFragment,
.module = fragShaderModule.get(),
.pName = "main",
};
2024-10-01 18:30:31 -04:00
2024-10-14 23:32:52 -04:00
std::array<vk::PipelineShaderStageCreateInfo, 2> shaderStages = {
vertShaderStageInfo,
fragShaderStageInfo,
};
2024-10-01 18:30:31 -04:00
vk::VertexInputBindingDescription bindingDescription = Vertex::getBindingDescription();
2024-10-11 00:42:58 -04:00
std::array<vk::VertexInputAttributeDescription, 3> attributeDescriptions =
Vertex::getAttributeDescriptions();
2024-10-01 18:30:31 -04:00
vk::PipelineVertexInputStateCreateInfo vertexInputInfo {
.vertexBindingDescriptionCount = 1,
.pVertexBindingDescriptions = &bindingDescription,
.vertexAttributeDescriptionCount = static_cast<u32>(attributeDescriptions.size()),
2024-10-14 23:32:52 -04:00
.pVertexAttributeDescriptions = attributeDescriptions.data(),
2024-10-01 18:30:31 -04:00
};
2024-10-14 23:32:52 -04:00
vk::PipelineInputAssemblyStateCreateInfo inputAssembly {
.topology = vk::PrimitiveTopology::eTriangleList,
.primitiveRestartEnable = vk::False,
};
2024-10-01 18:30:31 -04:00
2024-10-14 23:32:52 -04:00
vk::PipelineViewportStateCreateInfo viewportState {
.viewportCount = 1,
.scissorCount = 1,
};
2024-10-01 18:30:31 -04:00
2024-10-14 23:32:52 -04:00
vk::PipelineRasterizationStateCreateInfo rasterizer {
.depthClampEnable = vk::False,
.rasterizerDiscardEnable = vk::False,
.polygonMode = vk::PolygonMode::eFill,
.cullMode = vk::CullModeFlagBits::eBack,
.frontFace = vk::FrontFace::eCounterClockwise,
.depthBiasEnable = vk::False,
.lineWidth = 1.0F,
};
2024-10-01 18:30:31 -04:00
2024-10-14 23:32:52 -04:00
vk::PipelineMultisampleStateCreateInfo multisampling {
.rasterizationSamples = mMsaaSamples,
.sampleShadingEnable = vk::False,
};
2024-10-11 19:09:37 -04:00
2024-10-14 23:32:52 -04:00
vk::PipelineDepthStencilStateCreateInfo depthStencil {
.depthTestEnable = vk::True,
.depthWriteEnable = vk::True,
.depthCompareOp = vk::CompareOp::eLess,
.depthBoundsTestEnable = vk::False,
.stencilTestEnable = vk::False,
};
2024-10-01 18:30:31 -04:00
vk::PipelineColorBlendAttachmentState colorBlendAttachment {
.blendEnable = vk::False,
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
2024-10-14 23:32:52 -04:00
vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA,
2024-10-01 18:30:31 -04:00
};
vk::PipelineColorBlendStateCreateInfo colorBlending {
.logicOpEnable = vk::False,
.logicOp = vk::LogicOp::eCopy,
.attachmentCount = 1,
.pAttachments = &colorBlendAttachment,
2024-10-14 23:32:52 -04:00
.blendConstants = std::array<float, 4> { 0.0F, 0.0F, 0.0F, 0.0F },
2024-10-01 18:30:31 -04:00
};
2024-10-01 18:30:31 -04:00
std::vector<vk::DynamicState> dynamicStates = { vk::DynamicState::eViewport, vk::DynamicState::eScissor };
2024-10-14 23:32:52 -04:00
vk::PipelineDynamicStateCreateInfo dynamicState {
.dynamicStateCount = static_cast<u32>(dynamicStates.size()),
.pDynamicStates = dynamicStates.data(),
};
2024-10-01 18:30:31 -04:00
2024-10-14 23:32:52 -04:00
vk::PipelineLayoutCreateInfo pipelineLayoutInfo {
.setLayoutCount = 1,
.pSetLayouts = &mDescriptorSetLayout.get(),
};
2024-10-01 18:30:31 -04:00
2024-10-06 18:14:15 -04:00
mPipelineLayout = mDevice->createPipelineLayoutUnique(pipelineLayoutInfo);
2024-10-01 18:30:31 -04:00
2024-10-14 23:32:52 -04:00
vk::GraphicsPipelineCreateInfo pipelineInfo {
.stageCount = static_cast<u32>(shaderStages.size()),
.pStages = shaderStages.data(),
.pVertexInputState = &vertexInputInfo,
.pInputAssemblyState = &inputAssembly,
.pViewportState = &viewportState,
.pRasterizationState = &rasterizer,
.pMultisampleState = &multisampling,
.pDepthStencilState = &depthStencil,
.pColorBlendState = &colorBlending,
.pDynamicState = &dynamicState,
.layout = mPipelineLayout.get(),
.renderPass = mRenderPass.get(),
.subpass = 0,
};
2024-10-01 18:30:31 -04:00
2024-10-06 17:34:25 -04:00
vk::Result graphicsPipelineResult = vk::Result::eSuccess;
vk::UniquePipeline graphicsPipelineValue;
std::tie(graphicsPipelineResult, graphicsPipelineValue) =
mDevice->createGraphicsPipelineUnique(nullptr, pipelineInfo).asTuple();
if (graphicsPipelineResult != vk::Result::eSuccess)
throw std::runtime_error("Failed to create graphics pipeline!");
mGraphicsPipeline = std::move(graphicsPipelineValue);
2024-10-01 18:30:31 -04:00
}
2024-10-01 18:33:05 -04:00
fn createFramebuffers() -> void {
mSwapChainFramebuffers.resize(mSwapChainImageViews.size());
for (usize i = 0; i < mSwapChainImageViews.size(); i++) {
2024-10-11 21:14:36 -04:00
std::array<vk::ImageView, 3> attachments = { mColorImageView.get(),
mDepthImageView.get(),
mSwapChainImageViews[i].get() };
2024-10-11 19:09:37 -04:00
vk::FramebufferCreateInfo framebufferInfo { .renderPass = mRenderPass.get(),
.attachmentCount = static_cast<u32>(attachments.size()),
.pAttachments = attachments.data(),
.width = mSwapChainExtent.width,
.height = mSwapChainExtent.height,
.layers = 1 };
2024-10-01 18:33:05 -04:00
2024-10-06 18:14:15 -04:00
mSwapChainFramebuffers[i] = mDevice->createFramebufferUnique(framebufferInfo);
2024-10-01 18:33:05 -04:00
}
}
2024-10-01 18:54:41 -04:00
fn createCommandPool() -> void {
QueueFamilyIndices queueFamilyIndices = findQueueFamilies(mPhysicalDevice);
2024-10-12 15:39:20 -04:00
vk::CommandPoolCreateInfo poolInfo { .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer,
.queueFamilyIndex = queueFamilyIndices.graphics_family.value() };
2024-10-01 18:54:41 -04:00
2024-10-06 18:14:15 -04:00
mCommandPool = mDevice->createCommandPoolUnique(poolInfo);
2024-10-01 18:54:41 -04:00
}
2024-10-11 21:14:36 -04:00
fn createColorResources() -> void {
vk::Format colorFormat = mSwapChainImageFormat;
2024-10-14 23:32:52 -04:00
std::tie(mColorImage, mColorImageMemory) = createImage(
2024-10-11 21:14:36 -04:00
mSwapChainExtent.width,
mSwapChainExtent.height,
1,
mMsaaSamples,
colorFormat,
vk::ImageTiling::eOptimal,
vk::ImageUsageFlagBits::eTransientAttachment | vk::ImageUsageFlagBits::eColorAttachment,
2024-10-14 23:32:52 -04:00
vk::MemoryPropertyFlagBits::eDeviceLocal
2024-10-11 21:14:36 -04:00
);
mColorImageView = createImageView(mColorImage.get(), colorFormat, vk::ImageAspectFlagBits::eColor, 1);
}
2024-10-11 19:09:37 -04:00
fn createDepthResources() -> void {
vk::Format depthFormat = findDepthFormat();
2024-10-14 23:32:52 -04:00
std::tie(mDepthImage, mDepthImageMemory) = createImage(
2024-10-11 19:09:37 -04:00
mSwapChainExtent.width,
mSwapChainExtent.height,
2024-10-11 20:50:23 -04:00
1,
2024-10-11 21:14:36 -04:00
mMsaaSamples,
2024-10-11 19:09:37 -04:00
depthFormat,
vk::ImageTiling::eOptimal,
vk::ImageUsageFlagBits::eDepthStencilAttachment,
2024-10-14 23:32:52 -04:00
vk::MemoryPropertyFlagBits::eDeviceLocal
2024-10-11 19:09:37 -04:00
);
2024-10-11 20:50:23 -04:00
mDepthImageView = createImageView(mDepthImage.get(), depthFormat, vk::ImageAspectFlagBits::eDepth, 1);
2024-10-11 19:09:37 -04:00
}
fn findSupportedFormat(
const std::vector<vk::Format>& candidates,
2024-10-14 23:32:52 -04:00
const vk::ImageTiling& tiling,
const vk::FormatFeatureFlags& features
2024-10-11 19:09:37 -04:00
) -> vk::Format {
for (vk::Format format : candidates) {
vk::FormatProperties props = mPhysicalDevice.getFormatProperties(format);
2024-10-14 23:32:52 -04:00
2024-10-11 19:09:37 -04:00
if (tiling == vk::ImageTiling::eLinear && (props.linearTilingFeatures & features) == features)
return format;
2024-10-14 23:32:52 -04:00
2024-10-11 19:09:37 -04:00
if (tiling == vk::ImageTiling::eOptimal && (props.optimalTilingFeatures & features) == features)
return format;
}
throw std::runtime_error("Failed to find supported format!");
}
fn findDepthFormat() -> vk::Format {
return findSupportedFormat(
{ vk::Format::eD32Sfloat, vk::Format::eD32SfloatS8Uint, vk::Format::eD24UnormS8Uint },
vk::ImageTiling::eOptimal,
vk::FormatFeatureFlagBits::eDepthStencilAttachment
);
}
static fn hasStencilComponent(vk::Format format) {
return format == vk::Format::eD32SfloatS8Uint || format == vk::Format::eD24UnormS8Uint;
}
2024-10-11 00:42:58 -04:00
fn createTextureImage() -> void {
2024-10-12 00:07:13 -04:00
stb::UniqueImage image(TEXTURE_PATH);
2024-10-11 20:50:23 -04:00
2024-10-12 00:07:13 -04:00
u8* pixels = image.getData();
i32 texWidth = image.getWidth(), texHeight = image.getHeight();
2024-10-11 00:42:58 -04:00
vk::DeviceSize imageSize =
static_cast<vk::DeviceSize>(texWidth) * static_cast<vk::DeviceSize>(texHeight) * 4;
2024-10-11 20:50:23 -04:00
mMipLevels = static_cast<u32>(std::floor(std::log2(std::max(texWidth, texHeight)))) + 1;
2024-10-11 00:42:58 -04:00
if (!pixels)
throw std::runtime_error("Failed to load texture image!");
vk::UniqueBuffer stagingBuffer;
vk::UniqueDeviceMemory stagingBufferMemory;
2024-10-14 23:32:52 -04:00
std::tie(stagingBuffer, stagingBufferMemory) = createBuffer(
2024-10-11 00:42:58 -04:00
imageSize,
vk::BufferUsageFlagBits::eTransferSrc,
2024-10-14 23:32:52 -04:00
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent
2024-10-11 00:42:58 -04:00
);
copyData(stagingBufferMemory.get(), imageSize, pixels);
2024-10-14 23:32:52 -04:00
std::tie(mTextureImage, mTextureImageMemory) = createImage(
2024-10-11 00:42:58 -04:00
static_cast<u32>(texWidth),
static_cast<u32>(texHeight),
2024-10-11 20:50:23 -04:00
mMipLevels,
2024-10-11 21:14:36 -04:00
vk::SampleCountFlagBits::e1,
2024-10-11 00:42:58 -04:00
vk::Format::eR8G8B8A8Srgb,
vk::ImageTiling::eOptimal,
2024-10-11 20:50:23 -04:00
vk::ImageUsageFlagBits::eTransferSrc | vk::ImageUsageFlagBits::eTransferDst |
vk::ImageUsageFlagBits::eSampled,
2024-10-14 23:32:52 -04:00
vk::MemoryPropertyFlagBits::eDeviceLocal
2024-10-11 00:42:58 -04:00
);
transitionImageLayout(
2024-10-11 20:50:23 -04:00
mTextureImage.get(), vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, mMipLevels
2024-10-11 00:42:58 -04:00
);
copyBufferToImage(
stagingBuffer.get(), mTextureImage.get(), static_cast<u32>(texWidth), static_cast<u32>(texHeight)
);
2024-10-11 20:50:23 -04:00
generateMipmaps(mTextureImage.get(), vk::Format::eR8G8B8A8Srgb, texWidth, texHeight, mMipLevels);
}
fn generateMipmaps(vk::Image image, vk::Format imageFormat, i32 texWidth, i32 texHeight, u32 mipLevels)
-> void {
vk::FormatProperties formatProperties = mPhysicalDevice.getFormatProperties(imageFormat);
if (!(formatProperties.optimalTilingFeatures & vk::FormatFeatureFlagBits::eSampledImageFilterLinear))
throw std::runtime_error("Texture image format does not support linear blitting!");
2024-10-14 23:32:52 -04:00
vk::CommandBuffer commandBuffer = beginSingleTimeCommands();
2024-10-11 20:50:23 -04:00
vk::ImageMemoryBarrier barrier {
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
.image = image,
.subresourceRange = { .aspectMask = vk::ImageAspectFlagBits::eColor,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1 }
};
i32 mipWidth = texWidth;
i32 mipHeight = texHeight;
for (u32 i = 1; i < mipLevels; i++) {
barrier.subresourceRange.baseMipLevel = i - 1;
barrier.oldLayout = vk::ImageLayout::eTransferDstOptimal;
barrier.newLayout = vk::ImageLayout::eTransferSrcOptimal;
barrier.srcAccessMask = vk::AccessFlagBits::eTransferWrite;
barrier.dstAccessMask = vk::AccessFlagBits::eTransferRead;
2024-10-14 23:32:52 -04:00
commandBuffer.pipelineBarrier(
2024-10-11 20:50:23 -04:00
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTransfer,
{},
nullptr,
nullptr,
barrier
);
vk::ImageBlit blit {
.srcSubresource = { .aspectMask = vk::ImageAspectFlagBits::eColor,
.mipLevel = i - 1,
.baseArrayLayer = 0,
.layerCount = 1 },
2024-10-11 22:34:15 -04:00
.srcOffsets = std::array<vk::Offset3D, 2> { { { .x = 0, .y = 0, .z = 0 },
{ .x = mipWidth, .y = mipHeight, .z = 1 } } },
2024-10-11 20:50:23 -04:00
.dstSubresource = { .aspectMask = vk::ImageAspectFlagBits::eColor,
.mipLevel = i,
.baseArrayLayer = 0,
.layerCount = 1 },
2024-10-12 15:39:20 -04:00
.dstOffsets = std::array<vk::Offset3D, 2> { vk::Offset3D {
.x = 0,
.y = 0,
.z = 0,
}, vk::Offset3D {
.x = mipWidth > 1 ? mipWidth / 2 : 1,
.y = mipHeight > 1 ? mipHeight / 2 : 1,
.z = 1,
} }
2024-10-11 20:50:23 -04:00
};
2024-10-14 23:32:52 -04:00
commandBuffer.blitImage(
2024-10-11 20:50:23 -04:00
image,
vk::ImageLayout::eTransferSrcOptimal,
image,
vk::ImageLayout::eTransferDstOptimal,
blit,
vk::Filter::eLinear
);
barrier.oldLayout = vk::ImageLayout::eTransferSrcOptimal;
barrier.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
barrier.srcAccessMask = vk::AccessFlagBits::eTransferRead;
barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead;
2024-10-14 23:32:52 -04:00
commandBuffer.pipelineBarrier(
2024-10-11 20:50:23 -04:00
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eFragmentShader,
{},
nullptr,
nullptr,
barrier
);
if (mipWidth > 1)
mipWidth /= 2;
if (mipHeight > 1)
mipHeight /= 2;
}
barrier.subresourceRange.baseMipLevel = mMipLevels - 1;
barrier.oldLayout = vk::ImageLayout::eTransferDstOptimal;
barrier.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
barrier.srcAccessMask = vk::AccessFlagBits::eTransferWrite;
barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead;
2024-10-14 23:32:52 -04:00
commandBuffer.pipelineBarrier(
2024-10-11 20:50:23 -04:00
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eFragmentShader,
{},
nullptr,
nullptr,
barrier
2024-10-11 00:42:58 -04:00
);
2024-10-11 20:50:23 -04:00
2024-10-14 23:32:52 -04:00
endSingleTimeCommands(commandBuffer);
2024-10-11 00:42:58 -04:00
}
2024-10-11 21:14:36 -04:00
fn getMaxUsableSampleCount() -> vk::SampleCountFlagBits {
vk::PhysicalDeviceProperties physicalDeviceProperties = mPhysicalDevice.getProperties();
vk::SampleCountFlags counts = physicalDeviceProperties.limits.framebufferColorSampleCounts &
physicalDeviceProperties.limits.framebufferDepthSampleCounts;
2024-10-14 23:32:52 -04:00
// Define an array of sample counts in descending order
const std::array<vk::SampleCountFlagBits, 7> sampleCounts = {
vk::SampleCountFlagBits::e64, vk::SampleCountFlagBits::e32, vk::SampleCountFlagBits::e16,
vk::SampleCountFlagBits::e8, vk::SampleCountFlagBits::e4, vk::SampleCountFlagBits::e2,
vk::SampleCountFlagBits::e1,
};
// Loop through the array and return the first supported sample count
for (const vk::SampleCountFlagBits& count : sampleCounts)
if (counts & count)
return count;
2024-10-11 21:14:36 -04:00
2024-10-14 23:32:52 -04:00
// Return e1 if no other sample count is supported
2024-10-11 21:14:36 -04:00
return vk::SampleCountFlagBits::e1;
}
2024-10-11 00:42:58 -04:00
fn createTextureImageView() -> void {
2024-10-11 20:50:23 -04:00
mTextureImageView = createImageView(
mTextureImage.get(), vk::Format::eR8G8B8A8Srgb, vk::ImageAspectFlagBits::eColor, mMipLevels
);
2024-10-11 00:42:58 -04:00
}
fn createTextureSampler() -> void {
vk::PhysicalDeviceProperties properties = mPhysicalDevice.getProperties();
vk::SamplerCreateInfo samplerInfo {
.magFilter = vk::Filter::eLinear,
.minFilter = vk::Filter::eLinear,
.mipmapMode = vk::SamplerMipmapMode::eLinear,
.addressModeU = vk::SamplerAddressMode::eRepeat,
.addressModeV = vk::SamplerAddressMode::eRepeat,
.addressModeW = vk::SamplerAddressMode::eRepeat,
.mipLodBias = 0.0F,
.anisotropyEnable = vk::False,
.maxAnisotropy = properties.limits.maxSamplerAnisotropy,
.compareEnable = vk::False,
.compareOp = vk::CompareOp::eAlways,
.minLod = 0.0F,
2024-10-11 20:50:23 -04:00
.maxLod = static_cast<f32>(mMipLevels),
2024-10-11 00:42:58 -04:00
.borderColor = vk::BorderColor::eIntOpaqueBlack,
.unnormalizedCoordinates = vk::False,
};
mTextureSampler = mDevice->createSamplerUnique(samplerInfo);
}
2024-10-14 23:32:52 -04:00
fn createImageView(
const vk::Image& image,
const vk::Format& format,
const vk::ImageAspectFlags& aspectFlags,
const u32& mipLevels
) -> vk::UniqueImageView {
2024-10-11 00:42:58 -04:00
vk::ImageViewCreateInfo viewInfo {
.image = image, .viewType = vk::ImageViewType::e2D, .format = format, .subresourceRange = {
2024-10-14 23:32:52 -04:00
.aspectMask = aspectFlags,
.baseMipLevel = 0,
.levelCount = mipLevels,
.baseArrayLayer = 0,
.layerCount = 1,
2024-10-11 00:42:58 -04:00
}
};
return mDevice->createImageViewUnique(viewInfo);
}
fn createImage(
2024-10-14 23:32:52 -04:00
const u32& width,
const u32& height,
const u32& mipLevels,
const vk::SampleCountFlagBits& numSamples,
const vk::Format& format,
const vk::ImageTiling& tiling,
const vk::ImageUsageFlags& usage,
const vk::MemoryPropertyFlags& properties
) -> std::pair<vk::UniqueImage, vk::UniqueDeviceMemory> {
// Define the image creation info
2024-10-11 00:42:58 -04:00
vk::ImageCreateInfo imageInfo {
.imageType = vk::ImageType::e2D,
.format = format,
.extent = { .width = width, .height = height, .depth = 1 },
2024-10-11 20:50:23 -04:00
.mipLevels = mipLevels,
2024-10-11 00:42:58 -04:00
.arrayLayers = 1,
2024-10-11 21:14:36 -04:00
.samples = numSamples,
2024-10-11 00:42:58 -04:00
.tiling = tiling,
.usage = usage,
.sharingMode = vk::SharingMode::eExclusive,
.initialLayout = vk::ImageLayout::eUndefined,
};
2024-10-14 23:32:52 -04:00
// Create the image
vk::UniqueImage image = mDevice->createImageUnique(imageInfo);
2024-10-11 00:42:58 -04:00
2024-10-14 23:32:52 -04:00
// Get the memory requirements for the image
2024-10-11 00:42:58 -04:00
vk::MemoryRequirements memRequirements = mDevice->getImageMemoryRequirements(image.get());
2024-10-14 23:32:52 -04:00
// Memory allocation info
2024-10-11 00:42:58 -04:00
vk::MemoryAllocateInfo allocInfo {
.allocationSize = memRequirements.size,
.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties),
};
2024-10-14 23:32:52 -04:00
// Allocate memory
vk::UniqueDeviceMemory imageMemory = mDevice->allocateMemoryUnique(allocInfo);
2024-10-11 00:42:58 -04:00
2024-10-14 23:32:52 -04:00
// Bind the allocated memory to the image
2024-10-11 00:42:58 -04:00
mDevice->bindImageMemory(image.get(), imageMemory.get(), 0);
2024-10-14 23:32:52 -04:00
// Return the unique image
return { std::move(image), std::move(imageMemory) };
2024-10-11 00:42:58 -04:00
}
fn transitionImageLayout(
2024-10-14 23:32:52 -04:00
const vk::Image& image,
const vk::ImageLayout& oldLayout,
const vk::ImageLayout& newLayout,
const u32& mipLevels
2024-10-11 00:42:58 -04:00
) -> void {
2024-10-14 23:32:52 -04:00
vk::CommandBuffer commandBuffer = beginSingleTimeCommands();
2024-10-11 00:42:58 -04:00
vk::ImageMemoryBarrier barrier {
.oldLayout = oldLayout,
.newLayout = newLayout,
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
.image = image,
2024-10-14 23:32:52 -04:00
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = mipLevels,
.baseArrayLayer = 0,
.layerCount = 1,
},
2024-10-11 00:42:58 -04:00
};
vk::PipelineStageFlags sourceStage;
vk::PipelineStageFlags destinationStage;
if (oldLayout == vk::ImageLayout::eUndefined && newLayout == vk::ImageLayout::eTransferDstOptimal) {
barrier.srcAccessMask = {};
barrier.dstAccessMask = vk::AccessFlagBits::eTransferWrite;
sourceStage = vk::PipelineStageFlagBits::eTopOfPipe;
destinationStage = vk::PipelineStageFlagBits::eTransfer;
} else if (oldLayout == vk::ImageLayout::eTransferDstOptimal &&
newLayout == vk::ImageLayout::eShaderReadOnlyOptimal) {
barrier.srcAccessMask = vk::AccessFlagBits::eTransferWrite;
barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead;
sourceStage = vk::PipelineStageFlagBits::eTransfer;
destinationStage = vk::PipelineStageFlagBits::eFragmentShader;
} else {
throw std::invalid_argument("Unsupported layout transition!");
}
2024-10-14 23:32:52 -04:00
commandBuffer.pipelineBarrier(sourceStage, destinationStage, {}, {}, {}, barrier);
2024-10-11 00:42:58 -04:00
2024-10-14 23:32:52 -04:00
endSingleTimeCommands(commandBuffer);
2024-10-11 00:42:58 -04:00
}
2024-10-14 23:32:52 -04:00
fn copyBufferToImage(const vk::Buffer& buffer, const vk::Image& image, const u32& width, const u32& height)
-> void {
vk::CommandBuffer commandBuffer = beginSingleTimeCommands();
2024-10-11 00:42:58 -04:00
vk::BufferImageCopy region {
.bufferOffset = 0,
.bufferRowLength = 0,
.bufferImageHeight = 0,
.imageSubresource = { .aspectMask = vk::ImageAspectFlagBits::eColor,
.mipLevel = 0,
.baseArrayLayer = 0,
.layerCount = 1 },
2024-10-11 22:34:15 -04:00
.imageOffset = { .x = 0, .y = 0, .z = 0 },
.imageExtent = { .width = width, .height = height, .depth = 1 },
2024-10-11 00:42:58 -04:00
};
2024-10-14 23:32:52 -04:00
commandBuffer.copyBufferToImage(buffer, image, vk::ImageLayout::eTransferDstOptimal, 1, &region);
2024-10-11 00:42:58 -04:00
2024-10-14 23:32:52 -04:00
endSingleTimeCommands(commandBuffer);
2024-10-11 00:42:58 -04:00
}
2024-10-11 20:04:28 -04:00
fn loadModel() -> void {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn, err;
if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, MODEL_PATH))
throw std::runtime_error(warn + err);
std::unordered_map<Vertex, u32> uniqueVertices {};
for (const tinyobj::shape_t& shape : shapes) {
for (const tinyobj::index_t& index : shape.mesh.indices) {
Vertex vertex {
.pos = {
2024-10-11 22:34:15 -04:00
attrib.vertices[static_cast<u32>((3 * index.vertex_index) + 0)],
attrib.vertices[static_cast<u32>((3 * index.vertex_index) + 1)],
attrib.vertices[static_cast<u32>((3 * index.vertex_index) + 2)],
2024-10-11 20:04:28 -04:00
},
.color = { 1.0F, 1.0F, 1.0F },
.tex_coord = {
2024-10-11 22:34:15 -04:00
attrib.texcoords[static_cast<u32>((2 * index.texcoord_index) + 0)],
1.0F - attrib.texcoords[static_cast<u32>((2 * index.texcoord_index) + 1)],
2024-10-14 23:32:52 -04:00
},
2024-10-11 20:04:28 -04:00
};
if (!uniqueVertices.contains(vertex)) {
uniqueVertices[vertex] = static_cast<u32>(mVertices.size());
mVertices.push_back(vertex);
}
mIndices.push_back(uniqueVertices[vertex]);
}
}
}
fn createVertexBuffer() -> void {
2024-10-11 20:04:28 -04:00
vk::DeviceSize bufferSize = sizeof(mVertices[0]) * mVertices.size();
2024-10-10 16:04:46 -04:00
vk::UniqueBuffer stagingBuffer;
vk::UniqueDeviceMemory stagingBufferMemory;
2024-10-14 23:32:52 -04:00
std::tie(stagingBuffer, stagingBufferMemory) = createBuffer(
2024-10-10 16:04:46 -04:00
bufferSize,
vk::BufferUsageFlagBits::eTransferSrc,
2024-10-14 23:32:52 -04:00
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent
2024-10-10 16:04:46 -04:00
);
2024-10-11 20:04:28 -04:00
copyData(stagingBufferMemory.get(), bufferSize, mVertices.data());
2024-10-10 16:04:46 -04:00
2024-10-14 23:32:52 -04:00
std::tie(mVertexBuffer, mVertexBufferMemory) = createBuffer(
2024-10-10 16:04:46 -04:00
bufferSize,
vk::BufferUsageFlagBits::eVertexBuffer | vk::BufferUsageFlagBits::eTransferDst,
2024-10-14 23:32:52 -04:00
vk::MemoryPropertyFlagBits::eDeviceLocal
2024-10-10 16:04:46 -04:00
);
copyBuffer(stagingBuffer.get(), mVertexBuffer.get(), bufferSize);
stagingBuffer.reset();
stagingBufferMemory.reset();
}
2024-10-10 18:51:20 -04:00
fn createIndexBuffer() -> void {
2024-10-11 20:04:28 -04:00
vk::DeviceSize bufferSize = sizeof(mIndices[0]) * mIndices.size();
2024-10-10 18:51:20 -04:00
vk::UniqueBuffer stagingBuffer;
vk::UniqueDeviceMemory stagingBufferMemory;
2024-10-14 23:32:52 -04:00
std::tie(stagingBuffer, stagingBufferMemory) = createBuffer(
2024-10-10 18:51:20 -04:00
bufferSize,
vk::BufferUsageFlagBits::eTransferSrc,
2024-10-14 23:32:52 -04:00
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent
2024-10-10 18:51:20 -04:00
);
2024-10-11 20:04:28 -04:00
copyData(stagingBufferMemory.get(), bufferSize, mIndices.data());
2024-10-10 18:51:20 -04:00
2024-10-14 23:32:52 -04:00
std::tie(mIndexBuffer, mIndexBufferMemory) = createBuffer(
2024-10-10 18:51:20 -04:00
bufferSize,
vk::BufferUsageFlagBits::eIndexBuffer | vk::BufferUsageFlagBits::eTransferDst,
2024-10-14 23:32:52 -04:00
vk::MemoryPropertyFlagBits::eDeviceLocal
2024-10-10 18:51:20 -04:00
);
copyBuffer(stagingBuffer.get(), mIndexBuffer.get(), bufferSize);
stagingBuffer.reset();
stagingBufferMemory.reset();
}
fn createUniformBuffers() -> void {
vk::DeviceSize bufferSize = sizeof(UniformBufferObject);
mUniformBuffers.resize(MAX_FRAMES_IN_FLIGHT);
mUniformBuffersMemory.resize(MAX_FRAMES_IN_FLIGHT);
mUniformBuffersMapped.resize(MAX_FRAMES_IN_FLIGHT);
for (usize i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
2024-10-14 23:32:52 -04:00
std::tie(mUniformBuffers[i], mUniformBuffersMemory[i]) = createBuffer(
bufferSize,
vk::BufferUsageFlagBits::eUniformBuffer,
2024-10-14 23:32:52 -04:00
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent
);
mUniformBuffersMapped[i] = mDevice->mapMemory(mUniformBuffersMemory[i].get(), 0, bufferSize);
}
}
fn createDescriptorPool() -> void {
2024-10-11 00:42:58 -04:00
std::array<vk::DescriptorPoolSize, 2> poolSizes = {
2024-10-14 23:32:52 -04:00
vk::DescriptorPoolSize {
.type = vk::DescriptorType::eUniformBuffer,
.descriptorCount = MAX_FRAMES_IN_FLIGHT,
},
vk::DescriptorPoolSize {
.type = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = MAX_FRAMES_IN_FLIGHT,
},
};
vk::DescriptorPoolCreateInfo poolInfo {
2024-10-11 00:42:58 -04:00
.maxSets = MAX_FRAMES_IN_FLIGHT,
.poolSizeCount = static_cast<u32>(poolSizes.size()),
.pPoolSizes = poolSizes.data(),
};
mDescriptorPool = mDevice->createDescriptorPoolUnique(poolInfo);
}
fn createDescriptorSets() -> void {
std::vector<vk::DescriptorSetLayout> layouts(MAX_FRAMES_IN_FLIGHT, mDescriptorSetLayout.get());
vk::DescriptorSetAllocateInfo allocInfo {
.descriptorPool = mDescriptorPool.get(),
.descriptorSetCount = static_cast<u32>(MAX_FRAMES_IN_FLIGHT),
.pSetLayouts = layouts.data(),
};
mDescriptorSets = mDevice->allocateDescriptorSets(allocInfo);
for (usize i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
vk::DescriptorBufferInfo bufferInfo {
.buffer = mUniformBuffers[i].get(),
.offset = 0,
.range = sizeof(UniformBufferObject),
};
2024-10-11 00:42:58 -04:00
vk::DescriptorImageInfo imageInfo {
.sampler = mTextureSampler.get(),
.imageView = mTextureImageView.get(),
.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
};
std::array<vk::WriteDescriptorSet, 2> descriptorWrites = {
2024-10-14 23:32:52 -04:00
vk::WriteDescriptorSet {
.dstSet = mDescriptorSets[i],
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.pBufferInfo = &bufferInfo,
},
vk::WriteDescriptorSet {
.dstSet = mDescriptorSets[i],
.dstBinding = 1,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.pImageInfo = &imageInfo,
},
};
2024-10-11 00:42:58 -04:00
mDevice->updateDescriptorSets(descriptorWrites, {});
}
}
2024-10-10 16:04:46 -04:00
fn createBuffer(
2024-10-14 23:32:52 -04:00
const vk::DeviceSize& deviceSize,
const vk::BufferUsageFlags& bufferUsageFlags,
const vk::MemoryPropertyFlags& memoryPropertyFlags
) -> std::pair<vk::UniqueBuffer, vk::UniqueDeviceMemory> {
vk::BufferCreateInfo bufferInfo {
2024-10-10 16:04:46 -04:00
.size = deviceSize,
.usage = bufferUsageFlags,
.sharingMode = vk::SharingMode::eExclusive,
};
2024-10-14 23:32:52 -04:00
// Create the buffer
vk::UniqueBuffer buffer = mDevice->createBufferUnique(bufferInfo);
2024-10-14 23:32:52 -04:00
// Get the memory requirements for the buffer
2024-10-10 16:04:46 -04:00
vk::MemoryRequirements memRequirements = mDevice->getBufferMemoryRequirements(buffer.get());
2024-10-14 23:32:52 -04:00
// Memory allocation info
vk::MemoryAllocateInfo allocInfo {
.allocationSize = memRequirements.size,
2024-10-10 16:04:46 -04:00
.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, memoryPropertyFlags),
};
2024-10-14 23:32:52 -04:00
// Allocate memory
vk::UniqueDeviceMemory bufferMemory = mDevice->allocateMemoryUnique(allocInfo);
2024-10-10 16:04:46 -04:00
2024-10-14 23:32:52 -04:00
// Bind the allocated memory to the buffer
2024-10-10 16:04:46 -04:00
mDevice->bindBufferMemory(buffer.get(), bufferMemory.get(), 0);
2024-10-14 23:32:52 -04:00
// Return both the unique buffer and its associated memory
return { std::move(buffer), std::move(bufferMemory) };
2024-10-10 16:04:46 -04:00
}
2024-10-14 23:32:52 -04:00
// Create the command buffers
fn beginSingleTimeCommands() -> vk::CommandBuffer {
// Define the command buffer allocation info
2024-10-10 16:04:46 -04:00
vk::CommandBufferAllocateInfo allocInfo {
.commandPool = mCommandPool.get(),
.level = vk::CommandBufferLevel::ePrimary,
.commandBufferCount = 1,
};
2024-10-14 23:32:52 -04:00
// Allocate the command buffer
vk::CommandBuffer commandBuffer = mDevice->allocateCommandBuffers(allocInfo)[0];
2024-10-10 16:04:46 -04:00
2024-10-14 23:32:52 -04:00
// Define the command buffer begin info
2024-10-10 16:04:46 -04:00
vk::CommandBufferBeginInfo beginInfo { .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit };
2024-10-14 23:32:52 -04:00
// Begin the command buffer
commandBuffer.begin(beginInfo);
2024-10-10 16:04:46 -04:00
2024-10-11 00:42:58 -04:00
return commandBuffer;
}
2024-10-10 16:04:46 -04:00
2024-10-14 23:32:52 -04:00
// End the single time command buffer
fn endSingleTimeCommands(const vk::CommandBuffer& commandBuffer) -> void {
// End the command buffer
commandBuffer.end();
2024-10-14 23:32:52 -04:00
// Define the submit info
vk::SubmitInfo submitInfo { .commandBufferCount = 1, .pCommandBuffers = &commandBuffer };
2024-10-14 23:32:52 -04:00
// Submit the command buffer
2024-10-10 16:04:46 -04:00
mGraphicsQueue.submit(submitInfo, nullptr);
2024-10-14 23:32:52 -04:00
// Wait for the queue to finish
2024-10-10 16:04:46 -04:00
mGraphicsQueue.waitIdle();
2024-10-14 23:32:52 -04:00
// Free the command buffer
mDevice->freeCommandBuffers(mCommandPool.get(), commandBuffer);
}
2024-10-14 23:32:52 -04:00
// Copy data from src to dst
fn copyData(const vk::DeviceMemory& stagingBufferMemory, const vk::DeviceSize& bufferSize, const void* src)
-> void {
// Map the memory
2024-10-11 00:42:58 -04:00
void* data = mDevice->mapMemory(stagingBufferMemory, 0, bufferSize);
2024-10-14 23:32:52 -04:00
// Copy the data with memcpy - memcpy(dst, src, size)
2024-10-11 00:42:58 -04:00
memcpy(data, src, static_cast<usize>(bufferSize));
2024-10-14 23:32:52 -04:00
// Unmap the memory
2024-10-11 00:42:58 -04:00
mDevice->unmapMemory(stagingBufferMemory);
}
2024-10-14 23:32:52 -04:00
// Copy buffer from src to dst
fn copyBuffer(const vk::Buffer& srcBuffer, const vk::Buffer& dstBuffer, const vk::DeviceSize& deviceSize)
-> void {
// Begin a single time command buffer
vk::CommandBuffer commandBuffer = beginSingleTimeCommands();
2024-10-11 00:42:58 -04:00
2024-10-14 23:32:52 -04:00
// Define the copy region
2024-10-11 00:42:58 -04:00
vk::BufferCopy copyRegion { .size = deviceSize };
2024-10-14 23:32:52 -04:00
// Copy the buffer
commandBuffer.copyBuffer(srcBuffer, dstBuffer, 1, &copyRegion);
2024-10-11 00:42:58 -04:00
2024-10-14 23:32:52 -04:00
// End the single time command buffer
endSingleTimeCommands(commandBuffer);
2024-10-11 00:42:58 -04:00
}
2024-10-14 23:32:52 -04:00
// Find the memory type of the physical device
fn findMemoryType(const u32& typeFilter, const vk::MemoryPropertyFlags& properties) -> u32 {
// Get the memory properties of the physical device
vk::PhysicalDeviceMemoryProperties memProperties = mPhysicalDevice.getMemoryProperties();
2024-10-14 23:32:52 -04:00
// Loop through the memory types and find the one that matches the filter
for (u32 idx = 0; idx < memProperties.memoryTypeCount; idx++)
if ((typeFilter & (1 << idx)) &&
(memProperties.memoryTypes.at(idx).propertyFlags & properties) == properties)
return idx;
2024-10-14 23:32:52 -04:00
// Throw an error if no suitable memory type is found
throw std::runtime_error("Failed to find a suitable memory type!");
}
2024-10-14 23:32:52 -04:00
// Create the command buffers
2024-10-05 23:08:12 -04:00
fn createCommandBuffers() -> void {
2024-10-14 23:32:52 -04:00
// Resize the command buffers to hold the maximum number of frames in flight
2024-10-05 23:08:12 -04:00
mCommandBuffers.resize(MAX_FRAMES_IN_FLIGHT);
2024-10-14 23:32:52 -04:00
// Define the command buffer allocation info
vk::CommandBufferAllocateInfo allocInfo {
.commandPool = mCommandPool.get(),
.level = vk::CommandBufferLevel::ePrimary,
.commandBufferCount = static_cast<u32>(mCommandBuffers.size()),
};
2024-10-01 18:54:41 -04:00
2024-10-14 23:32:52 -04:00
// Allocate the command buffers
2024-10-06 18:14:15 -04:00
mCommandBuffers = mDevice->allocateCommandBuffersUnique(allocInfo);
2024-10-01 18:54:41 -04:00
}
2024-10-14 23:32:52 -04:00
// Record the command buffer
fn recordCommandBuffer(const vk::CommandBuffer& commandBuffer, const u32& imageIndex) -> void {
// Define the command buffer begin info
2024-10-01 18:54:41 -04:00
vk::CommandBufferBeginInfo beginInfo {};
2024-10-14 23:32:52 -04:00
// Begin the command buffer
2024-10-06 18:14:15 -04:00
commandBuffer.begin(beginInfo);
2024-10-01 18:54:41 -04:00
2024-10-14 23:32:52 -04:00
// Define the render pass begin info
2024-10-11 19:09:37 -04:00
std::array<vk::ClearValue, 2> clearValues {
2024-10-14 23:32:52 -04:00
// Set the color buffer to black
vk::ClearValue { .color = { .uint32 = std::array<u32, 4> { 0, 0, 0, 255 } } },
// Set the depth buffer to 1.0F
vk::ClearValue { .depthStencil = { .depth = 1.0F, .stencil = 0 } },
2024-10-11 19:09:37 -04:00
};
2024-10-01 18:54:41 -04:00
2024-10-14 23:32:52 -04:00
// Define the render pass info
2024-10-01 18:54:41 -04:00
vk::RenderPassBeginInfo renderPassInfo {
2024-10-14 23:32:52 -04:00
// Render pass itself
.renderPass = mRenderPass.get(),
// Current framebuffer
.framebuffer = mSwapChainFramebuffers[imageIndex].get(),
// Render area (entire framebuffer)
.renderArea = { .offset = { .x = 0, .y = 0 }, .extent = mSwapChainExtent },
// Clear values for the attachments
2024-10-11 19:09:37 -04:00
.clearValueCount = static_cast<u32>(clearValues.size()),
2024-10-14 23:32:52 -04:00
.pClearValues = clearValues.data(),
2024-10-01 18:54:41 -04:00
};
2024-10-14 23:32:52 -04:00
// Begin the render pass
2024-10-01 18:54:41 -04:00
commandBuffer.beginRenderPass(renderPassInfo, vk::SubpassContents::eInline);
2024-10-14 23:32:52 -04:00
// Bind the graphics pipeline to the command buffer
2024-10-01 18:54:41 -04:00
commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, mGraphicsPipeline.get());
2024-10-14 23:32:52 -04:00
// Create the viewport and scissor.
// If the scissor is smaller than the
// viewport, then it will be clipped.
2024-10-01 18:54:41 -04:00
vk::Viewport viewport {
.x = 0.0F,
.y = 0.0F,
.width = static_cast<f32>(mSwapChainExtent.width),
.height = static_cast<f32>(mSwapChainExtent.height),
.minDepth = 0.0F,
.maxDepth = 1.0F,
};
2024-10-14 23:32:52 -04:00
// Create the scissor
2024-10-01 18:54:41 -04:00
vk::Rect2D scissor {
2024-10-11 22:34:15 -04:00
.offset = { .x = 0, .y = 0 },
2024-10-01 18:54:41 -04:00
.extent = mSwapChainExtent,
};
2024-10-14 23:32:52 -04:00
// Set the viewport and scissor
2024-10-01 19:00:31 -04:00
commandBuffer.setViewport(0, viewport);
2024-10-01 18:54:41 -04:00
commandBuffer.setScissor(0, scissor);
2024-10-14 23:32:52 -04:00
// Bind the vertex buffer
commandBuffer.bindVertexBuffers(0, mVertexBuffer.get(), { 0 });
2024-10-14 23:32:52 -04:00
// Bind the index buffer
2024-10-11 20:04:28 -04:00
commandBuffer.bindIndexBuffer(mIndexBuffer.get(), 0, vk::IndexType::eUint32);
2024-10-10 18:51:20 -04:00
2024-10-14 23:32:52 -04:00
// Bind the descriptor sets
commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics,
mPipelineLayout.get(),
0,
1,
&mDescriptorSets[mCurrentFrame],
0,
nullptr
);
2024-10-14 23:32:52 -04:00
// Draw the indexed vertices
2024-10-11 20:04:28 -04:00
commandBuffer.drawIndexed(static_cast<u32>(mIndices.size()), 1, 0, 0, 0);
2024-10-01 18:54:41 -04:00
2024-10-14 23:32:52 -04:00
// End the render pass
2024-10-01 18:54:41 -04:00
commandBuffer.endRenderPass();
2024-10-14 23:32:52 -04:00
// End the command buffer
2024-10-06 18:14:15 -04:00
commandBuffer.end();
2024-10-01 18:54:41 -04:00
}
2024-10-14 23:32:52 -04:00
// Create the semaphores and fences
2024-10-01 18:54:41 -04:00
fn createSyncObjects() -> void {
2024-10-14 23:32:52 -04:00
// Resize the vectors to hold the maximum number of frames in flight
2024-10-05 23:08:12 -04:00
mImageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
mRenderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
mInFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
2024-10-14 23:32:52 -04:00
// Create the semaphore and fence info
2024-10-01 18:54:41 -04:00
vk::SemaphoreCreateInfo semaphoreInfo {};
vk::FenceCreateInfo fenceInfo { .flags = vk::FenceCreateFlagBits::eSignaled };
2024-10-14 23:32:52 -04:00
// Loop through the maximum number of frames in flight and create the semaphores and fences
2024-10-05 23:08:12 -04:00
for (usize idx = 0; idx < MAX_FRAMES_IN_FLIGHT; idx++) {
2024-10-06 18:14:15 -04:00
mImageAvailableSemaphores[idx] = mDevice->createSemaphoreUnique(semaphoreInfo);
mRenderFinishedSemaphores[idx] = mDevice->createSemaphoreUnique(semaphoreInfo);
mInFlightFences[idx] = mDevice->createFenceUnique(fenceInfo);
2024-10-05 23:08:12 -04:00
}
2024-10-01 18:54:41 -04:00
}
2024-10-14 23:32:52 -04:00
fn updateUniformBuffer(const u32& currentImage) -> void {
using namespace std::chrono;
using time_point = high_resolution_clock::time_point;
static time_point StartTime = high_resolution_clock::now();
2024-10-14 23:32:52 -04:00
time_point currentTime = high_resolution_clock::now();
f32 time = duration<f32, seconds::period>(currentTime - StartTime).count();
UniformBufferObject ubo {
.model = glm::rotate(glm::mat4(1.0F), time * glm::radians(90.0F), glm::vec3(0.0F, 0.0F, 1.0F)),
.view =
glm::lookAt(glm::vec3(2.0F, 2.0F, 2.0F), glm::vec3(0.0F, 0.0F, 0.0F), glm::vec3(0.0F, 0.0F, 1.0F)),
.proj = glm::perspective(
glm::radians(45.0F),
static_cast<f32>(mSwapChainExtent.width) / static_cast<f32>(mSwapChainExtent.height),
0.1F,
10.0F
)
};
// Flip the Y axis, because glm was designed for OpenGL
ubo.proj[1][1] *= -1;
memcpy(mUniformBuffersMapped[currentImage], &ubo, sizeof(ubo));
}
2024-10-01 18:54:41 -04:00
fn drawFrame() -> void {
2024-10-06 18:34:42 -04:00
try {
vk::Result result =
mDevice->waitForFences(mInFlightFences[mCurrentFrame].get(), vk::Bool32(vk::True), UINT64_MAX);
2024-10-01 18:54:41 -04:00
2024-10-06 18:34:42 -04:00
if (result != vk::Result::eSuccess)
throw std::runtime_error("Failed to wait for fences!");
2024-10-01 18:54:41 -04:00
2024-10-06 18:34:42 -04:00
vk::Result imageIndexResult = vk::Result::eSuccess;
u32 imageIndexValue = 0;
2024-10-06 17:34:25 -04:00
2024-10-06 18:34:42 -04:00
std::tie(imageIndexResult, imageIndexValue) = mDevice->acquireNextImageKHR(
mSwapChain.get(), UINT64_MAX, mImageAvailableSemaphores[mCurrentFrame].get(), nullptr
);
2024-10-05 23:08:12 -04:00
2024-10-06 18:34:42 -04:00
if (imageIndexResult == vk::Result::eErrorOutOfDateKHR) {
recreateSwapChain();
return;
}
2024-10-05 23:08:12 -04:00
2024-10-06 18:34:42 -04:00
if (imageIndexResult != vk::Result::eSuccess && imageIndexResult != vk::Result::eSuboptimalKHR)
throw std::runtime_error("Failed to acquire swap chain image!");
2024-10-01 18:54:41 -04:00
updateUniformBuffer(mCurrentFrame);
2024-10-06 18:34:42 -04:00
mDevice->resetFences(mInFlightFences[mCurrentFrame].get());
2024-10-01 18:54:41 -04:00
2024-10-06 18:34:42 -04:00
mCommandBuffers[mCurrentFrame]->reset(vk::CommandBufferResetFlagBits::eReleaseResources);
recordCommandBuffer(mCommandBuffers[mCurrentFrame].get(), imageIndexValue);
2024-10-01 18:54:41 -04:00
2024-10-06 18:34:42 -04:00
std::array<vk::PipelineStageFlags, 1> waitStages = {
vk::PipelineStageFlagBits::eColorAttachmentOutput
};
2024-10-01 18:54:41 -04:00
2024-10-06 18:34:42 -04:00
vk::SubmitInfo submitInfo {
.waitSemaphoreCount = 1,
.pWaitSemaphores = &mImageAvailableSemaphores[mCurrentFrame].get(),
.pWaitDstStageMask = waitStages.data(),
.commandBufferCount = 1,
.pCommandBuffers = &mCommandBuffers[mCurrentFrame].get(),
.signalSemaphoreCount = 1,
.pSignalSemaphores = &mRenderFinishedSemaphores[mCurrentFrame].get(),
};
2024-10-01 18:54:41 -04:00
2024-10-06 18:34:42 -04:00
mGraphicsQueue.submit(submitInfo, mInFlightFences[mCurrentFrame].get());
2024-10-01 18:54:41 -04:00
2024-10-06 18:34:42 -04:00
vk::PresentInfoKHR presentInfo {
.waitSemaphoreCount = 1,
.pWaitSemaphores = &mRenderFinishedSemaphores[mCurrentFrame].get(),
.swapchainCount = 1,
.pSwapchains = &mSwapChain.get(),
.pImageIndices = &imageIndexValue,
};
2024-10-01 18:54:41 -04:00
2024-10-06 18:14:15 -04:00
vk::Result presentResult = mPresentQueue.presentKHR(presentInfo);
if (presentResult == vk::Result::eErrorOutOfDateKHR || presentResult == vk::Result::eSuboptimalKHR ||
mFramebufferResized) {
mFramebufferResized = false;
recreateSwapChain();
2024-10-07 16:04:49 -04:00
} else if (presentResult != vk::Result::eSuccess)
2024-10-06 18:14:15 -04:00
throw std::runtime_error("Failed to present swap chain image!");
mCurrentFrame = (mCurrentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
2024-10-07 16:04:49 -04:00
} catch (vk::OutOfDateKHRError& /*err*/) {
2024-10-05 23:08:12 -04:00
mFramebufferResized = false;
recreateSwapChain();
2024-10-06 18:14:15 -04:00
return;
2024-10-05 23:08:12 -04:00
}
2024-10-01 18:54:41 -04:00
}
2024-10-01 18:30:31 -04:00
fn createShaderModule(const std::vector<char>& code) -> vk::UniqueShaderModule {
2024-10-14 23:32:52 -04:00
vk::ShaderModuleCreateInfo createInfo {
.codeSize = code.size(),
.pCode = std::bit_cast<const u32*>(code.data()),
};
2024-10-01 18:30:31 -04:00
2024-10-06 18:14:15 -04:00
return mDevice->createShaderModuleUnique(createInfo);
2024-10-01 18:30:31 -04:00
}
2024-10-14 23:32:52 -04:00
// Choose the color format for the swap chain
2024-10-05 23:08:12 -04:00
static fn chooseSwapSurfaceFormat(const std::vector<vk::SurfaceFormatKHR>& availableFormats
) -> vk::SurfaceFormatKHR {
2024-10-14 23:32:52 -04:00
// If SRGB is available, use it
for (const vk::SurfaceFormatKHR& availableFormat : availableFormats)
2024-10-01 16:57:40 -04:00
if (availableFormat.format == vk::Format::eB8G8R8A8Srgb &&
availableFormat.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear)
return availableFormat;
2024-10-14 23:32:52 -04:00
// Otherwise, use the first available format
2024-10-01 16:57:40 -04:00
return availableFormats[0];
}
2024-10-14 23:32:52 -04:00
// Choose the swap chain presentation mode
2024-10-05 23:08:12 -04:00
static fn chooseSwapPresentMode(const std::vector<vk::PresentModeKHR>& availablePresentModes
) -> vk::PresentModeKHR {
2024-10-14 23:32:52 -04:00
// Check if mailbox mode is available (adaptive sync)
for (const vk::PresentModeKHR& availablePresentMode : availablePresentModes)
2024-10-01 16:57:40 -04:00
if (availablePresentMode == vk::PresentModeKHR::eMailbox)
return availablePresentMode;
2024-10-14 23:32:52 -04:00
// If mailbox mode is not available, use FIFO mode (vsync)
2024-10-01 16:57:40 -04:00
return vk::PresentModeKHR::eFifo;
}
2024-10-14 23:32:52 -04:00
// Choose the swap chain extent (resolution)
fn chooseSwapExtent(const vk::SurfaceCapabilitiesKHR& capabilities) -> vk::Extent2D {
// If the resolution is not UINT32_MAX, return it
// Otherwise, we need to set the resolution manually
2024-10-01 16:57:40 -04:00
if (capabilities.currentExtent.width != UINT32_MAX)
return capabilities.currentExtent;
2024-10-14 23:32:52 -04:00
// Get the window's resolution
2024-10-01 16:57:40 -04:00
u32 width = 0, height = 0;
std::tie(width, height) = mWindow->getFramebufferSize();
2024-10-14 23:32:52 -04:00
// Return the resolution clamped to the supported range
return {
.width = std::clamp(width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width),
.height = std::clamp(height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height),
};
2024-10-01 16:57:40 -04:00
}
2024-10-14 23:32:52 -04:00
// Check if the swap chain is adequate
fn querySwapChainSupport(const vk::PhysicalDevice& device) -> SwapChainSupportDetails {
return {
.capabilities = device.getSurfaceCapabilitiesKHR(mSurface.get()),
.formats = device.getSurfaceFormatsKHR(mSurface.get()),
.present_modes = device.getSurfacePresentModesKHR(mSurface.get()),
};
2024-10-01 17:06:14 -04:00
}
2024-10-14 23:32:52 -04:00
// Check if the device is suitable for the application
fn isDeviceSuitable(const vk::PhysicalDevice& device) -> bool {
// Get the queue families that support the required operations
2024-10-10 18:51:20 -04:00
QueueFamilyIndices qfIndices = findQueueFamilies(device);
2024-10-01 17:06:14 -04:00
2024-10-14 23:32:52 -04:00
// Check if the device supports the required extensions
2024-10-01 17:06:14 -04:00
bool extensionsSupported = checkDeviceExtensionSupport(device);
bool swapChainAdequate = false;
if (extensionsSupported) {
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
2024-10-14 23:32:52 -04:00
// Check if the swap chain is adequate (make sure it has
// at least one supported format and presentation mode)
2024-10-01 17:06:14 -04:00
swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.present_modes.empty();
}
2024-10-14 23:32:52 -04:00
// Check if the device supports the required features
2024-10-11 00:42:58 -04:00
vk::PhysicalDeviceFeatures supportedFeatures = device.getFeatures();
2024-10-14 23:32:52 -04:00
// If the device supports everything required, return true
2024-10-11 00:42:58 -04:00
return qfIndices.isComplete() && extensionsSupported && swapChainAdequate &&
supportedFeatures.samplerAnisotropy;
2024-10-01 17:06:14 -04:00
}
2024-10-14 23:32:52 -04:00
// Check if the device supports the required extensions
static fn checkDeviceExtensionSupport(const vk::PhysicalDevice& device) -> bool {
// Get the available extensions
2024-10-06 18:14:15 -04:00
std::vector<vk::ExtensionProperties> availableExtensions = device.enumerateDeviceExtensionProperties();
2024-10-01 17:06:14 -04:00
2024-10-14 23:32:52 -04:00
// Create a set of required extension names
2024-10-01 17:06:14 -04:00
std::set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
2024-10-14 23:32:52 -04:00
// Remove each required extension from the set of available extensions
2024-10-06 18:14:15 -04:00
for (const vk::ExtensionProperties& extension : availableExtensions)
2024-10-06 17:34:25 -04:00
requiredExtensions.erase(extension.extensionName);
2024-10-01 17:06:14 -04:00
2024-10-14 23:32:52 -04:00
// If the set is empty, all required extensions are supported
2024-10-01 17:06:14 -04:00
return requiredExtensions.empty();
}
2024-10-14 23:32:52 -04:00
// Find the queue families that support the required operations
fn findQueueFamilies(const vk::PhysicalDevice& device) -> QueueFamilyIndices {
// Create a struct to store the queue family indices
2024-10-10 18:51:20 -04:00
QueueFamilyIndices qfIndices;
2024-10-01 17:06:14 -04:00
2024-10-14 23:32:52 -04:00
// Get the queue family properties
2024-10-01 17:06:14 -04:00
std::vector<vk::QueueFamilyProperties> queueFamilies = device.getQueueFamilyProperties();
2024-10-14 23:32:52 -04:00
// For every queue family,
2024-10-01 17:06:14 -04:00
for (u32 i = 0; i < queueFamilies.size(); i++) {
2024-10-14 23:32:52 -04:00
// Check if the queue family supports the required operations
2024-10-01 17:06:14 -04:00
if (queueFamilies[i].queueFlags & vk::QueueFlagBits::eGraphics)
2024-10-10 18:51:20 -04:00
qfIndices.graphics_family = i;
2024-10-01 17:06:14 -04:00
2024-10-14 23:32:52 -04:00
// Check if the queue family supports presentation
2024-10-06 18:14:15 -04:00
vk::Bool32 queuePresentSupport = device.getSurfaceSupportKHR(i, mSurface.get());
2024-10-01 17:06:14 -04:00
2024-10-14 23:32:52 -04:00
// If the queue family supports presentation, set the present family index
2024-10-06 18:14:15 -04:00
if (queuePresentSupport)
2024-10-10 18:51:20 -04:00
qfIndices.present_family = i;
2024-10-01 17:06:14 -04:00
2024-10-14 23:32:52 -04:00
// If the queue family supports both operations, we're done
2024-10-10 18:51:20 -04:00
if (qfIndices.isComplete())
2024-10-01 17:06:14 -04:00
break;
}
2024-10-10 18:51:20 -04:00
return qfIndices;
2024-10-01 17:06:14 -04:00
}
2024-10-14 23:32:52 -04:00
// Check if all requested layers are available
2024-09-30 00:31:08 -04:00
static fn checkValidationLayerSupport() -> bool {
2024-10-06 18:14:15 -04:00
std::vector<vk::LayerProperties> availableLayers = vk::enumerateInstanceLayerProperties();
2024-09-30 00:31:08 -04:00
2024-10-14 23:32:52 -04:00
// Create a set of available layer names
std::unordered_set<std::string_view> availableLayerNames;
for (const vk::LayerProperties& layerProperties : availableLayers)
availableLayerNames.emplace(layerProperties.layerName);
2024-09-30 00:31:08 -04:00
2024-10-14 23:32:52 -04:00
// Check if each validation layer is available
for (const std::string_view layerName : validationLayers)
if (availableLayerNames.find(layerName) == availableLayerNames.end())
return false; // Layer not found
2024-09-30 00:31:08 -04:00
2024-10-14 23:32:52 -04:00
return true; // All validation layers are available
2024-09-30 00:31:08 -04:00
}
2024-10-14 23:32:52 -04:00
// Callback function used to report validation layer messages
2024-09-28 18:13:24 -04:00
static VKAPI_ATTR fn VKAPI_CALL debugCallback(
2024-10-14 23:32:52 -04:00
VkDebugUtilsMessageSeverityFlagBitsEXT /*messageSeverity*/, // Severity: verbose, info, warning, error
VkDebugUtilsMessageTypeFlagsEXT /*messageType*/, // Type: general, validation, performance
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, // Message details
void* /*pUserData*/ // Optional user data
2024-09-30 00:57:13 -04:00
) -> vk::Bool32 {
2024-10-14 23:32:52 -04:00
// Print the message to the console
// Because pCallbackData already gives the message severity
// and type, we don't need to print them, so they're unused.
2024-09-29 23:02:04 -04:00
fmt::println("Validation layer: {}", pCallbackData->pMessage);
2024-09-28 18:13:24 -04:00
2024-09-30 21:34:25 -04:00
return vk::False;
2024-09-25 23:03:56 -04:00
}
};
2024-09-28 21:55:26 -04:00
fn main() -> i32 {
2024-10-14 23:32:52 -04:00
// Initialize dynamic function dispatcher
2024-10-12 15:39:20 -04:00
VULKAN_HPP_DEFAULT_DISPATCHER.init();
2024-10-06 12:26:44 -04:00
2024-10-14 23:32:52 -04:00
// Create an app instance
2024-09-29 23:11:12 -04:00
VulkanApp app;
2024-09-25 23:03:56 -04:00
try {
app.run();
} catch (const std::exception& e) {
2024-10-05 23:08:12 -04:00
fmt::println("{}", e.what());
2024-09-25 23:03:56 -04:00
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}