yes i will change those value calls to something else later
This commit is contained in:
parent
83631f940e
commit
daad4cc907
85
src/main.cpp
85
src/main.cpp
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#define VK_ENABLE_BETA_EXTENSIONS
|
#define VK_ENABLE_BETA_EXTENSIONS
|
||||||
#define VULKAN_HPP_NO_CONSTRUCTORS
|
#define VULKAN_HPP_NO_CONSTRUCTORS
|
||||||
|
#define VULKAN_HPP_NO_EXCEPTIONS
|
||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
|
|
||||||
#include "util/types.h"
|
#include "util/types.h"
|
||||||
|
@ -152,7 +153,10 @@ class VulkanApp {
|
||||||
drawFrame();
|
drawFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
mDevice->waitIdle();
|
auto result = mDevice->waitIdle();
|
||||||
|
|
||||||
|
if (result != vk::Result::eSuccess)
|
||||||
|
throw std::runtime_error("Failed to wait for idle!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cleanupSwapChain() -> void {
|
fn cleanupSwapChain() -> void {
|
||||||
|
@ -177,7 +181,10 @@ class VulkanApp {
|
||||||
vkfw::waitEvents();
|
vkfw::waitEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
mDevice->waitIdle();
|
auto result = mDevice->waitIdle();
|
||||||
|
|
||||||
|
if (result != vk::Result::eSuccess)
|
||||||
|
throw std::runtime_error("Failed to wait for idle!");
|
||||||
|
|
||||||
cleanupSwapChain();
|
cleanupSwapChain();
|
||||||
|
|
||||||
|
@ -225,12 +232,8 @@ class VulkanApp {
|
||||||
for (const char* extension : extensions) fmt::println("\t{}", extension);
|
for (const char* extension : extensions) fmt::println("\t{}", extension);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
try {
|
mInstance = vk::createInstanceUnique(createInfo).value;
|
||||||
mInstance = vk::createInstanceUnique(createInfo);
|
mLoader = vk::DispatchLoaderDynamic(mInstance.get(), vkGetInstanceProcAddr);
|
||||||
mLoader = vk::DispatchLoaderDynamic(mInstance.get(), vkGetInstanceProcAddr);
|
|
||||||
} catch (const vk::SystemError& err) {
|
|
||||||
throw std::runtime_error("Failed to create instance: " + std::string(err.what()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setupDebugMessenger() -> void {
|
fn setupDebugMessenger() -> void {
|
||||||
|
@ -247,13 +250,14 @@ class VulkanApp {
|
||||||
.pfnUserCallback = debugCallback,
|
.pfnUserCallback = debugCallback,
|
||||||
};
|
};
|
||||||
|
|
||||||
mDebugMessenger = mInstance->createDebugUtilsMessengerEXTUnique(messengerCreateInfo, nullptr, mLoader);
|
mDebugMessenger =
|
||||||
|
mInstance->createDebugUtilsMessengerEXTUnique(messengerCreateInfo, nullptr, mLoader).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn createSurface() -> void { mSurface = vkfw::createWindowSurfaceUnique(mInstance.get(), mWindow.get()); }
|
fn createSurface() -> void { mSurface = vkfw::createWindowSurfaceUnique(mInstance.get(), mWindow.get()); }
|
||||||
|
|
||||||
fn pickPhysicalDevice() -> void {
|
fn pickPhysicalDevice() -> void {
|
||||||
std::vector<vk::PhysicalDevice> devices = mInstance->enumeratePhysicalDevices();
|
std::vector<vk::PhysicalDevice> devices = mInstance->enumeratePhysicalDevices().value;
|
||||||
|
|
||||||
if (devices.empty())
|
if (devices.empty())
|
||||||
throw std::runtime_error("Failed to find GPUs with Vulkan support!");
|
throw std::runtime_error("Failed to find GPUs with Vulkan support!");
|
||||||
|
@ -302,7 +306,7 @@ class VulkanApp {
|
||||||
.ppEnabledExtensionNames = deviceExtensions.data(),
|
.ppEnabledExtensionNames = deviceExtensions.data(),
|
||||||
.pEnabledFeatures = &deviceFeatures };
|
.pEnabledFeatures = &deviceFeatures };
|
||||||
|
|
||||||
mDevice = mPhysicalDevice.createDeviceUnique(createInfo);
|
mDevice = mPhysicalDevice.createDeviceUnique(createInfo).value;
|
||||||
|
|
||||||
mGraphicsQueue = mDevice->getQueue(indices.graphics_family.value(), 0);
|
mGraphicsQueue = mDevice->getQueue(indices.graphics_family.value(), 0);
|
||||||
mPresentQueue = mDevice->getQueue(indices.present_family.value(), 0);
|
mPresentQueue = mDevice->getQueue(indices.present_family.value(), 0);
|
||||||
|
@ -345,9 +349,9 @@ class VulkanApp {
|
||||||
.oldSwapchain = nullptr,
|
.oldSwapchain = nullptr,
|
||||||
};
|
};
|
||||||
|
|
||||||
mSwapChain = mDevice->createSwapchainKHRUnique(createInfo);
|
mSwapChain = mDevice->createSwapchainKHRUnique(createInfo).value;
|
||||||
|
|
||||||
mSwapChainImages = mDevice->getSwapchainImagesKHR(mSwapChain.get());
|
mSwapChainImages = mDevice->getSwapchainImagesKHR(mSwapChain.get()).value;
|
||||||
mSwapChainImageFormat = surfaceFormat.format;
|
mSwapChainImageFormat = surfaceFormat.format;
|
||||||
mSwapChainExtent = extent;
|
mSwapChainExtent = extent;
|
||||||
}
|
}
|
||||||
|
@ -371,7 +375,7 @@ class VulkanApp {
|
||||||
.layerCount = 1 },
|
.layerCount = 1 },
|
||||||
};
|
};
|
||||||
|
|
||||||
mSwapChainImageViews[i] = mDevice->createImageViewUnique(createInfo);
|
mSwapChainImageViews[i] = mDevice->createImageViewUnique(createInfo).value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -405,7 +409,7 @@ class VulkanApp {
|
||||||
.pSubpasses = &subpass,
|
.pSubpasses = &subpass,
|
||||||
};
|
};
|
||||||
|
|
||||||
mRenderPass = mDevice->createRenderPassUnique(renderPassInfo);
|
mRenderPass = mDevice->createRenderPassUnique(renderPassInfo).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn createGraphicsPipeline() -> void {
|
fn createGraphicsPipeline() -> void {
|
||||||
|
@ -487,7 +491,7 @@ class VulkanApp {
|
||||||
.pushConstantRangeCount = 0,
|
.pushConstantRangeCount = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
mPipelineLayout = mDevice->createPipelineLayoutUnique(pipelineLayoutInfo);
|
mPipelineLayout = mDevice->createPipelineLayoutUnique(pipelineLayoutInfo).value;
|
||||||
|
|
||||||
vk::GraphicsPipelineCreateInfo pipelineInfo {
|
vk::GraphicsPipelineCreateInfo pipelineInfo {
|
||||||
.stageCount = static_cast<u32>(shaderStages.size()),
|
.stageCount = static_cast<u32>(shaderStages.size()),
|
||||||
|
@ -520,7 +524,7 @@ class VulkanApp {
|
||||||
.layers = 1,
|
.layers = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
mSwapChainFramebuffers[i] = mDevice->createFramebufferUnique(framebufferInfo);
|
mSwapChainFramebuffers[i] = mDevice->createFramebufferUnique(framebufferInfo).value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -532,7 +536,7 @@ class VulkanApp {
|
||||||
.queueFamilyIndex = queueFamilyIndices.graphics_family.value(),
|
.queueFamilyIndex = queueFamilyIndices.graphics_family.value(),
|
||||||
};
|
};
|
||||||
|
|
||||||
mCommandPool = mDevice->createCommandPoolUnique(poolInfo);
|
mCommandPool = mDevice->createCommandPoolUnique(poolInfo).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn createCommandBuffers() -> void {
|
fn createCommandBuffers() -> void {
|
||||||
|
@ -543,13 +547,16 @@ class VulkanApp {
|
||||||
.commandBufferCount =
|
.commandBufferCount =
|
||||||
static_cast<u32>(mCommandBuffers.size()) };
|
static_cast<u32>(mCommandBuffers.size()) };
|
||||||
|
|
||||||
mCommandBuffers = mDevice->allocateCommandBuffersUnique(allocInfo);
|
mCommandBuffers = mDevice->allocateCommandBuffersUnique(allocInfo).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn recordCommandBuffer(vk::CommandBuffer commandBuffer, u32 imageIndex) -> void {
|
fn recordCommandBuffer(vk::CommandBuffer commandBuffer, u32 imageIndex) -> void {
|
||||||
vk::CommandBufferBeginInfo beginInfo {};
|
vk::CommandBufferBeginInfo beginInfo {};
|
||||||
|
|
||||||
commandBuffer.begin(beginInfo);
|
vk::Result beginResult = commandBuffer.begin(beginInfo);
|
||||||
|
|
||||||
|
if (beginResult != vk::Result::eSuccess)
|
||||||
|
throw std::runtime_error("Failed to begin command buffer!");
|
||||||
|
|
||||||
vk::ClearValue clearColor { .color = { .float32 = std::array<float, 4> { 0.0F, 0.0F, 0.0F, 1.0F } } };
|
vk::ClearValue clearColor { .color = { .float32 = std::array<float, 4> { 0.0F, 0.0F, 0.0F, 1.0F } } };
|
||||||
|
|
||||||
|
@ -583,7 +590,11 @@ class VulkanApp {
|
||||||
commandBuffer.draw(3, 1, 0, 0);
|
commandBuffer.draw(3, 1, 0, 0);
|
||||||
|
|
||||||
commandBuffer.endRenderPass();
|
commandBuffer.endRenderPass();
|
||||||
commandBuffer.end();
|
|
||||||
|
vk::Result endResult = commandBuffer.end();
|
||||||
|
|
||||||
|
if (endResult != vk::Result::eSuccess)
|
||||||
|
throw std::runtime_error("Failed to end command buffer!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn createSyncObjects() -> void {
|
fn createSyncObjects() -> void {
|
||||||
|
@ -595,9 +606,9 @@ class VulkanApp {
|
||||||
vk::FenceCreateInfo fenceInfo { .flags = vk::FenceCreateFlagBits::eSignaled };
|
vk::FenceCreateInfo fenceInfo { .flags = vk::FenceCreateFlagBits::eSignaled };
|
||||||
|
|
||||||
for (usize idx = 0; idx < MAX_FRAMES_IN_FLIGHT; idx++) {
|
for (usize idx = 0; idx < MAX_FRAMES_IN_FLIGHT; idx++) {
|
||||||
mImageAvailableSemaphores[idx] = mDevice->createSemaphoreUnique(semaphoreInfo);
|
mImageAvailableSemaphores[idx] = mDevice->createSemaphoreUnique(semaphoreInfo).value;
|
||||||
mRenderFinishedSemaphores[idx] = mDevice->createSemaphoreUnique(semaphoreInfo);
|
mRenderFinishedSemaphores[idx] = mDevice->createSemaphoreUnique(semaphoreInfo).value;
|
||||||
mInFlightFences[idx] = mDevice->createFenceUnique(fenceInfo);
|
mInFlightFences[idx] = mDevice->createFenceUnique(fenceInfo).value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -637,7 +648,10 @@ class VulkanApp {
|
||||||
.pSignalSemaphores = &mRenderFinishedSemaphores[mCurrentFrame].get(),
|
.pSignalSemaphores = &mRenderFinishedSemaphores[mCurrentFrame].get(),
|
||||||
};
|
};
|
||||||
|
|
||||||
mGraphicsQueue.submit(submitInfo, mInFlightFences[mCurrentFrame].get());
|
vk::Result submitResult = mGraphicsQueue.submit(submitInfo, mInFlightFences[mCurrentFrame].get());
|
||||||
|
|
||||||
|
if (submitResult != vk::Result::eSuccess)
|
||||||
|
throw std::runtime_error("Failed to submit draw command buffer!");
|
||||||
|
|
||||||
vk::PresentInfoKHR presentInfo {
|
vk::PresentInfoKHR presentInfo {
|
||||||
.waitSemaphoreCount = 1,
|
.waitSemaphoreCount = 1,
|
||||||
|
@ -649,11 +663,11 @@ class VulkanApp {
|
||||||
|
|
||||||
vk::Result presentResult = mPresentQueue.presentKHR(presentInfo);
|
vk::Result presentResult = mPresentQueue.presentKHR(presentInfo);
|
||||||
|
|
||||||
if (presentResult == vk::Result::eErrorOutOfDateKHR || presentResult == vk::Result::eSuboptimalKHR || mFramebufferResized) {
|
if (presentResult == vk::Result::eErrorOutOfDateKHR || presentResult == vk::Result::eSuboptimalKHR ||
|
||||||
|
mFramebufferResized) {
|
||||||
mFramebufferResized = false;
|
mFramebufferResized = false;
|
||||||
recreateSwapChain();
|
recreateSwapChain();
|
||||||
} else if (presentResult != vk::Result::eSuccess){
|
} else if (presentResult != vk::Result::eSuccess) {
|
||||||
std::cout << presentResult << '\n';
|
|
||||||
throw std::runtime_error("Failed to present swap chain image!");
|
throw std::runtime_error("Failed to present swap chain image!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -664,7 +678,7 @@ class VulkanApp {
|
||||||
vk::ShaderModuleCreateInfo createInfo { .codeSize = code.size(),
|
vk::ShaderModuleCreateInfo createInfo { .codeSize = code.size(),
|
||||||
.pCode = std::bit_cast<const u32*>(code.data()) };
|
.pCode = std::bit_cast<const u32*>(code.data()) };
|
||||||
|
|
||||||
return mDevice->createShaderModuleUnique(createInfo);
|
return mDevice->createShaderModuleUnique(createInfo).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fn chooseSwapSurfaceFormat(const std::vector<vk::SurfaceFormatKHR>& availableFormats
|
static fn chooseSwapSurfaceFormat(const std::vector<vk::SurfaceFormatKHR>& availableFormats
|
||||||
|
@ -706,9 +720,9 @@ class VulkanApp {
|
||||||
fn querySwapChainSupport(vk::PhysicalDevice device) -> SwapChainSupportDetails {
|
fn querySwapChainSupport(vk::PhysicalDevice device) -> SwapChainSupportDetails {
|
||||||
SwapChainSupportDetails details;
|
SwapChainSupportDetails details;
|
||||||
|
|
||||||
details.capabilities = device.getSurfaceCapabilitiesKHR(mSurface.get());
|
details.capabilities = device.getSurfaceCapabilitiesKHR(mSurface.get()).value;
|
||||||
details.formats = device.getSurfaceFormatsKHR(mSurface.get());
|
details.formats = device.getSurfaceFormatsKHR(mSurface.get()).value;
|
||||||
details.present_modes = device.getSurfacePresentModesKHR(mSurface.get());
|
details.present_modes = device.getSurfacePresentModesKHR(mSurface.get()).value;
|
||||||
|
|
||||||
return details;
|
return details;
|
||||||
}
|
}
|
||||||
|
@ -729,7 +743,8 @@ class VulkanApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
static fn checkDeviceExtensionSupport(vk::PhysicalDevice device) -> bool {
|
static fn checkDeviceExtensionSupport(vk::PhysicalDevice device) -> bool {
|
||||||
std::vector<vk::ExtensionProperties> availableExtensions = device.enumerateDeviceExtensionProperties();
|
std::vector<vk::ExtensionProperties> availableExtensions =
|
||||||
|
device.enumerateDeviceExtensionProperties().value;
|
||||||
|
|
||||||
std::set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
|
std::set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
|
||||||
|
|
||||||
|
@ -747,7 +762,7 @@ class VulkanApp {
|
||||||
if (queueFamilies[i].queueFlags & vk::QueueFlagBits::eGraphics)
|
if (queueFamilies[i].queueFlags & vk::QueueFlagBits::eGraphics)
|
||||||
indices.graphics_family = i;
|
indices.graphics_family = i;
|
||||||
|
|
||||||
vk::Bool32 presentSupport = device.getSurfaceSupportKHR(i, mSurface.get());
|
vk::Bool32 presentSupport = device.getSurfaceSupportKHR(i, mSurface.get()).value;
|
||||||
|
|
||||||
if (presentSupport)
|
if (presentSupport)
|
||||||
indices.present_family = i;
|
indices.present_family = i;
|
||||||
|
@ -771,7 +786,7 @@ class VulkanApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
static fn checkValidationLayerSupport() -> bool {
|
static fn checkValidationLayerSupport() -> bool {
|
||||||
std::vector<vk::LayerProperties> availableLayers = vk::enumerateInstanceLayerProperties();
|
std::vector<vk::LayerProperties> availableLayers = vk::enumerateInstanceLayerProperties().value;
|
||||||
|
|
||||||
for (const char* layerName : validationLayers) {
|
for (const char* layerName : validationLayers) {
|
||||||
bool layerFound = false;
|
bool layerFound = false;
|
||||||
|
|
Loading…
Reference in a new issue