Properly set up transitioning image layouts to pair well with dynamic rendering, refactor some code.

This commit is contained in:
Lillian Salehi 2024-11-05 05:50:51 -06:00
parent 7770063537
commit 4a8f6909a8
5 changed files with 670 additions and 548 deletions

View File

@ -15,34 +15,39 @@ namespace device_libs {
std::vector<VkPresentModeKHR> presentModes;
};
const std::vector<const char *> deviceExtensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
VK_KHR_SWAPCHAIN_EXTENSION_NAME};
SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) {
/* Swap chains are weird ngl, it's another one of those Vulkan platform agnosticity.
The swapchain is basically a wrapper for GDI+, DXGI, X11, Wayland, etc.
It lets us use the swap chain rather than create a different framebuffer
handler for every targeted platform. Swap chains handle the ownership
of buffers before sending them to the presentation engine. (still no
fucking clue how it works though) */
/* Swap chains are weird ngl, it's another one of those Vulkan platform
agnosticity. The swapchain is basically a wrapper for GDI+, DXGI, X11,
Wayland, etc. It lets us use the swap chain rather than create a different
framebuffer handler for every targeted platform. Swap chains handle the
ownership of buffers before sending them to the presentation engine. (still
no fucking clue how it works though) */
SwapChainSupportDetails details;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, Global::surface, &details.capabilities);
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, Global::surface,
&details.capabilities);
uint32_t formatCount;
vkGetPhysicalDeviceSurfaceFormatsKHR(device, Global::surface, &formatCount, nullptr);
vkGetPhysicalDeviceSurfaceFormatsKHR(device, Global::surface, &formatCount,
nullptr);
if (formatCount != 0) {
details.formats.resize(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(device, Global::surface, &formatCount, details.formats.data());
vkGetPhysicalDeviceSurfaceFormatsKHR(device, Global::surface, &formatCount,
details.formats.data());
}
uint32_t presentModeCount;
vkGetPhysicalDeviceSurfacePresentModesKHR(device, Global::surface, &presentModeCount, details.presentModes.data());
vkGetPhysicalDeviceSurfacePresentModesKHR(
device, Global::surface, &presentModeCount, details.presentModes.data());
if (presentModeCount != 0) {
details.presentModes.resize(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(device, Global::surface, &presentModeCount, details.presentModes.data());
vkGetPhysicalDeviceSurfacePresentModesKHR(device, Global::surface,
&presentModeCount,
details.presentModes.data());
}
return details;
@ -50,12 +55,15 @@ namespace device_libs {
bool checkDeviceExtensionSupport(VkPhysicalDevice device) {
uint32_t extensionCount;
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount,
nullptr);
std::vector<VkExtensionProperties> availableExtensions(extensionCount);
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount,
availableExtensions.data());
std::set<std::string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
std::set<std::string> requiredExtensions(deviceExtensions.begin(),
deviceExtensions.end());
for (const auto &extension : availableExtensions) {
requiredExtensions.erase(extension.extensionName);
@ -65,47 +73,57 @@ namespace device_libs {
}
bool isDeviceSuitable(VkPhysicalDevice device) {
// These two are simple, create a structure to hold the apiVersion, driverVersion, vendorID, deviceID and type, name, and a few other settings.
// These two are simple, create a structure to hold the apiVersion,
// driverVersion, vendorID, deviceID and type, name, and a few other settings.
// Then populate it by passing in the device and the structure reference.
vkGetPhysicalDeviceProperties(device, &deviceProperties);
// Similarly, we can pass in the device and a deviceFeatures struct, this is quite special, it holds a struct of optional features the GPU can perform.
// Some, like a geometry shader, and stereoscopic rendering (multiViewport) we want, so we dont return true without them.
// Similarly, we can pass in the device and a deviceFeatures struct, this is
// quite special, it holds a struct of optional features the GPU can perform.
// Some, like a geometry shader, and stereoscopic rendering (multiViewport) we
// want, so we dont return true without them.
VkPhysicalDeviceFeatures supportedFeatures;
vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
// We need to find a device that supports graphical operations, or else we cant do much with it! This function just runs over all the queueFamilies and sees if there
// is a queue family with the VK_QUEUE_GRAPHICS_BIT flipped!
// We need to find a device that supports graphical operations, or else we
// cant do much with it! This function just runs over all the queueFamilies
// and sees if there is a queue family with the VK_QUEUE_GRAPHICS_BIT flipped!
Global::QueueFamilyIndices indices = Global::findQueueFamilies(device);
bool extensionSupported = checkDeviceExtensionSupport(device);
bool swapChainAdequate = false;
if (extensionSupported) {
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
swapChainAdequate = !swapChainSupport.formats.empty() &&
!swapChainSupport.presentModes.empty();
}
return deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU
&& supportedFeatures.samplerAnisotropy
&& indices.isComplete()
&& extensionSupported
&& swapChainAdequate;
return deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
supportedFeatures.samplerAnisotropy && indices.isComplete() &&
extensionSupported && swapChainAdequate;
}
// -------------------------------------- Swap Chain Settings ----------------------------------------- //
VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats) {
// One of three settings we can set, Surface Format controls the color space and format.
// -------------------------------------- Swap Chain Settings
// ----------------------------------------- //
VkSurfaceFormatKHR chooseSwapSurfaceFormat(
const std::vector<VkSurfaceFormatKHR> &availableFormats) {
// One of three settings we can set, Surface Format controls the color space
// and format.
for (const auto &availableFormat : availableFormats) {
if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB &&
availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
// sRGB & 32bit BGRA
return availableFormat;
}
}
return availableFormats[0];
}
VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes) {
// The second of the three settings, arguably the most important, the presentation mode! This dictates how images are displayed.
// MAILBOX is basically equivalent to triple buffering, it avoids screen tearing with fairly low latency,
// However, it is not always supported, so in the case that it isn't, currently we will default to FIFO,
// This is most similarly to standard V-Sync.
VkPresentModeKHR chooseSwapPresentMode(
const std::vector<VkPresentModeKHR> &availablePresentModes) {
// The second of the three settings, arguably the most important, the
// presentation mode! This dictates how images are displayed. MAILBOX is
// basically equivalent to triple buffering, it avoids screen tearing with
// fairly low latency, However, it is not always supported, so in the case
// that it isn't, currently we will default to FIFO, This is most similarly to
// standard V-Sync.
for (const auto &availablePresentMode : availablePresentModes) {
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
return availablePresentMode;
@ -114,29 +132,37 @@ namespace device_libs {
return VK_PRESENT_MODE_FIFO_KHR;
}
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, GLFWwindow* window) {
// Swap Extent is just a fancy way of saying the resolution of the swap images to display.
// This is almost always going to equal the resolution of the window in pixels.
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities,
GLFWwindow *window) {
// Swap Extent is just a fancy way of saying the resolution of the swap images
// to display. This is almost always going to equal the resolution of the
// window in pixels.
// The max int32 value tells us that the window manager lets us change the windth and height to what we wish!
if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()) {
// The max int32 value tells us that the window manager lets us change the
// windth and height to what we wish!
if (capabilities.currentExtent.width !=
std::numeric_limits<uint32_t>::max()) {
return capabilities.currentExtent;
} else {
int width, height;
glfwGetFramebufferSize(window, &width, &height);
VkExtent2D actualExtent = {
static_cast<uint32_t>(width),
static_cast<uint32_t>(height)
};
// Clamp the image size to the minimum extent values specified by vulkan for our window manager.
actualExtent.width = std::clamp(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
actualExtent.height = std::clamp(actualExtent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
VkExtent2D actualExtent = {static_cast<uint32_t>(width),
static_cast<uint32_t>(height)};
// Clamp the image size to the minimum extent values specified by vulkan for
// our window manager.
actualExtent.width =
std::clamp(actualExtent.width, capabilities.minImageExtent.width,
capabilities.maxImageExtent.width);
actualExtent.height =
std::clamp(actualExtent.height, capabilities.minImageExtent.height,
capabilities.maxImageExtent.height);
return actualExtent;
}
}
// --------------------------------------- External Functions ----------------------------------------- //
// --------------------------------------- External Functions
// ----------------------------------------- //
void DeviceControl::pickPhysicalDevice(VkInstance &instance) {
uint32_t deviceCount = 0;
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
@ -144,12 +170,14 @@ namespace device_libs {
if (deviceCount == 0) {
throw std::runtime_error("Failed to find GPU's with Vulkan Support!!");
}
std::vector<VkPhysicalDevice> devices(deviceCount); // Direct Initialization is weird af, yo
std::vector<VkPhysicalDevice> devices(
deviceCount); // Direct Initialization is weird af, yo
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
for (const auto &device : devices) {
if (isDeviceSuitable(device)) {
//Once we have buttons or such, maybe ask the user or write a config file for which GPU to use?
// Once we have buttons or such, maybe ask the user or write a config file
// for which GPU to use?
Global::physicalDevice = device;
break;
}
@ -162,21 +190,23 @@ namespace device_libs {
vkDestroySurfaceKHR(instance, Global::surface, nullptr);
}
void DeviceControl::createSurface(VkInstance &instance, GLFWwindow *window) {
if(glfwCreateWindowSurface(instance, window, nullptr, &Global::surface) != VK_SUCCESS) {
if (glfwCreateWindowSurface(instance, window, nullptr, &Global::surface) !=
VK_SUCCESS) {
throw std::runtime_error("Failed to create window surface!!");
}
}
void DeviceControl::createLogicalDevice() {
// Describe how many queues we want for a single family (1) here, right now we are solely interested in graphics capabilites,
// but Compute Shaders, transfer ops, decode and encode operations can also queued with setup! We also assign each queue a priority.
// We do this by looping over all the queueFamilies and sorting them by indices to fill the queue at the end!
Global::QueueFamilyIndices indices = Global::findQueueFamilies(Global::physicalDevice);
// Describe how many queues we want for a single family (1) here, right now we
// are solely interested in graphics capabilites, but Compute Shaders,
// transfer ops, decode and encode operations can also queued with setup! We
// also assign each queue a priority. We do this by looping over all the
// queueFamilies and sorting them by indices to fill the queue at the end!
Global::QueueFamilyIndices indices =
Global::findQueueFamilies(Global::physicalDevice);
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
std::set<uint32_t> uniqueQueueFamilies = {
indices.graphicsFamily.value(),
indices.presentFamily.value()
};
std::set<uint32_t> uniqueQueueFamilies = {indices.graphicsFamily.value(),
indices.presentFamily.value()};
float queuePriority = 1.0f;
for (uint32_t queueFamily : uniqueQueueFamilies) {
@ -191,6 +221,7 @@ namespace device_libs {
VkPhysicalDeviceVulkan13Features features13{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES,
.pNext = nullptr,
.synchronization2 = true,
.dynamicRendering = true,
};
VkPhysicalDeviceFeatures featuresBase{
@ -207,28 +238,40 @@ namespace device_libs {
createDeviceInfo.pNext = &deviceFeatures;
createDeviceInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createDeviceInfo.pQueueCreateInfos = queueCreateInfos.data();
createDeviceInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
createDeviceInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
createDeviceInfo.queueCreateInfoCount =
static_cast<uint32_t>(queueCreateInfos.size());
createDeviceInfo.enabledExtensionCount =
static_cast<uint32_t>(deviceExtensions.size());
createDeviceInfo.ppEnabledExtensionNames = deviceExtensions.data();
if(vkCreateDevice(Global::physicalDevice, &createDeviceInfo, nullptr, &Global::device) != VK_SUCCESS) {
if (vkCreateDevice(Global::physicalDevice, &createDeviceInfo, nullptr,
&Global::device) != VK_SUCCESS) {
throw std::runtime_error("Failed to create logical device");
}
vkGetDeviceQueue(Global::device, indices.graphicsFamily.value(), 0, &Global::graphicsQueue);
vkGetDeviceQueue(Global::device, indices.presentFamily.value(), 0, &Global::presentQueue);
vkGetDeviceQueue(Global::device, indices.graphicsFamily.value(), 0,
&Global::graphicsQueue);
vkGetDeviceQueue(Global::device, indices.presentFamily.value(), 0,
&Global::presentQueue);
}
void DeviceControl::createSwapChain(GLFWwindow *window) {
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(Global::physicalDevice);
SwapChainSupportDetails swapChainSupport =
querySwapChainSupport(Global::physicalDevice);
VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
VkSurfaceFormatKHR surfaceFormat =
chooseSwapSurfaceFormat(swapChainSupport.formats);
VkPresentModeKHR presentMode =
chooseSwapPresentMode(swapChainSupport.presentModes);
VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities, window);
// Number of images to hold in the swap chain, 1 over the minimum guarantees we won't have to wait on the driver to complete
// internal operations before acquiring another image. Absolutely a TODO to determine the best amount to queue.
// Number of images to hold in the swap chain, 1 over the minimum guarantees
// we won't have to wait on the driver to complete internal operations before
// acquiring another image. Absolutely a TODO to determine the best amount to
// queue.
uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
// Make sure not to queue more than the max! 0 indicates that there is no maximum.
if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
// Make sure not to queue more than the max! 0 indicates that there is no
// maximum.
if (swapChainSupport.capabilities.maxImageCount > 0 &&
imageCount > swapChainSupport.capabilities.maxImageCount) {
imageCount = swapChainSupport.capabilities.maxImageCount;
}
@ -239,18 +282,24 @@ namespace device_libs {
createSwapChainInfo.imageFormat = surfaceFormat.format;
createSwapChainInfo.imageColorSpace = surfaceFormat.colorSpace;
createSwapChainInfo.imageExtent = extent;
// Image array layers is always 1 unless we are developing for VR (Spoiler: we are, we will use a build flag.)
// Image Usage specifies what operations you use the images for, COLOR_ATTACH means we render directly to them,
// if you wanted to render to separate images for things like post processing, you can use TRANSFER_DST and use a
// memory operation to transfer the image to a swap chain, this is also a TODO item eventually.
// Image array layers is always 1 unless we are developing for VR (Spoiler: we
// are, we will use a build flag.) Image Usage specifies what operations you
// use the images for, COLOR_ATTACH means we render directly to them, if you
// wanted to render to separate images for things like post processing, you
// can use TRANSFER_DST and use a memory operation to transfer the image to a
// swap chain, this is also a TODO item eventually.
createSwapChainInfo.imageArrayLayers = 1;
createSwapChainInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
// This handles swap chain images across multiple queue families, ie, if the graphics queue family is different from the present queue
Global::QueueFamilyIndices indices = Global::findQueueFamilies(Global::physicalDevice);
uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()};
// Usage across multiple queue families without explicit transfer of ownership if they are different queue families.
// Otherwise, no sharing without explicit handoffs, faster, but not easily supported with multiple families.
// This handles swap chain images across multiple queue families, ie, if the
// graphics queue family is different from the present queue
Global::QueueFamilyIndices indices =
Global::findQueueFamilies(Global::physicalDevice);
uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(),
indices.presentFamily.value()};
// Usage across multiple queue families without explicit transfer of ownership
// if they are different queue families. Otherwise, no sharing without
// explicit handoffs, faster, but not easily supported with multiple families.
// Presentation and Graphics families are usually merged on most hardware.
if (indices.graphicsFamily != indices.presentFamily) {
createSwapChainInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
@ -260,24 +309,30 @@ namespace device_libs {
createSwapChainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
}
// Transformation of image support.
createSwapChainInfo.preTransform = swapChainSupport.capabilities.currentTransform;
createSwapChainInfo.preTransform =
swapChainSupport.capabilities.currentTransform;
// Do NOT blend with other windows on the system.
createSwapChainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createSwapChainInfo.presentMode = presentMode;
// This is interesting, clip pixels that are obscured for performance, but that means you wont be able to read them reliably..
// I am curious if this would affect screen-space rendering techniques, may be something to note.
// This is interesting, clip pixels that are obscured for performance, but
// that means you wont be able to read them reliably.. I am curious if this
// would affect screen-space rendering techniques, may be something to note.
createSwapChainInfo.clipped = VK_TRUE;
// This is something that needs to be implemented later, operations like resizing the window invalidate the swap chain and
// require you to recreate it and reference the old one specified here, will revisit in a few days.
// This is something that needs to be implemented later, operations like
// resizing the window invalidate the swap chain and require you to recreate
// it and reference the old one specified here, will revisit in a few days.
// createSwapChainInfo.oldSwapchain = VK_NULL_HANDLE;
if(vkCreateSwapchainKHR(Global::device, &createSwapChainInfo, nullptr, &Global::swapChain) != VK_SUCCESS) {
if (vkCreateSwapchainKHR(Global::device, &createSwapChainInfo, nullptr,
&Global::swapChain) != VK_SUCCESS) {
throw std::runtime_error("Failed to create the swap chain!!");
}
vkGetSwapchainImagesKHR(Global::device, Global::swapChain, &imageCount, nullptr);
vkGetSwapchainImagesKHR(Global::device, Global::swapChain, &imageCount,
nullptr);
swapChainImages.resize(imageCount);
vkGetSwapchainImagesKHR(Global::device, Global::swapChain, &imageCount, swapChainImages.data());
vkGetSwapchainImagesKHR(Global::device, Global::swapChain, &imageCount,
swapChainImages.data());
swapChainImageFormat = surfaceFormat.format;
swapChainExtent = extent;
@ -285,7 +340,9 @@ namespace device_libs {
void DeviceControl::destroySwapChain() {
vkDestroySwapchainKHR(Global::device, Global::swapChain, nullptr);
}
VkImageView DeviceControl::createImageView(VkImage image, VkFormat format, VkImageAspectFlags flags, uint32_t mipLevels) {
VkImageView DeviceControl::createImageView(VkImage image, VkFormat format,
VkImageAspectFlags flags,
uint32_t mipLevels) {
// This defines the parameters of a newly created image object!
VkImageViewCreateInfo viewInfo{};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
@ -300,7 +357,8 @@ namespace device_libs {
viewInfo.subresourceRange.levelCount = mipLevels;
VkImageView imageView;
if (vkCreateImageView(Global::device, &viewInfo, nullptr, &imageView) != VK_SUCCESS) {
if (vkCreateImageView(Global::device, &viewInfo, nullptr, &imageView) !=
VK_SUCCESS) {
throw std::runtime_error("failed to create image view!");
}
@ -310,7 +368,8 @@ namespace device_libs {
Global::swapChainImageViews.resize(swapChainImages.size());
for (uint32_t i = 0; i < swapChainImages.size(); i++) {
Global::swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1);
Global::swapChainImageViews[i] = createImageView(
swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1);
}
}
void DeviceControl::destroyImageViews() {
@ -318,15 +377,12 @@ namespace device_libs {
vkDestroyImageView(Global::device, imageView, nullptr);
}
}
// --------------------------------------- Getters & Setters ------------------------------------------ //
VkFormat* DeviceControl::getImageFormat() {
return &swapChainImageFormat;
}
VkExtent2D DeviceControl::getSwapChainExtent() {
return swapChainExtent;
}
// --------------------------------------- Getters & Setters
// ------------------------------------------ //
VkFormat *DeviceControl::getImageFormat() { return &swapChainImageFormat; }
VkExtent2D DeviceControl::getSwapChainExtent() { return swapChainExtent; }
std::vector<VkImage> DeviceControl::getSwapChainImages() {
return swapChainImages;
}
}
} // namespace device_libs

View File

@ -7,10 +7,9 @@ VkInstance vulkaninstance;
void EntryApp::setFramebufferResized(bool setter) {
framebufferResized = setter;
}
bool EntryApp::getFramebufferResized() const {
return framebufferResized;
}
static void framebufferResizeCallback(GLFWwindow* window, int width, int height) {
bool EntryApp::getFramebufferResized() const { return framebufferResized; }
static void framebufferResizeCallback(GLFWwindow *window, int width,
int height) {
auto app = reinterpret_cast<EntryApp *>(glfwGetWindowUserPointer(window));
app->setFramebufferResized(true);
}
@ -21,7 +20,8 @@ void initWindow() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
// Settings for the window are set, create window reference.
Global::window = glfwCreateWindow(Global::WIDTH, Global::HEIGHT, "Trimgles :o", nullptr, nullptr);
Global::window = glfwCreateWindow(Global::WIDTH, Global::HEIGHT,
"Trimgles :o", nullptr, nullptr);
glfwSetWindowUserPointer(Global::window, &EntryApp::getInstance());
glfwSetFramebufferSizeCallback(Global::window, framebufferResizeCallback);
}
@ -31,31 +31,46 @@ void createInstance() {
// Set application info for the vulkan instance!
VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; // Tell vulkan that appInfo is a Application Info structure
appInfo.sType =
VK_STRUCTURE_TYPE_APPLICATION_INFO; // Tell vulkan that appInfo is a
// Application Info structure
appInfo.pApplicationName = "Triangle Test"; // Give the struct a name to use
appInfo.applicationVersion = VK_MAKE_VERSION(1,0,0); // Create a Major Minor Patch version number for the application!
appInfo.pEngineName = "Agnosia Engine"; // Give an internal name for the engine running
appInfo.engineVersion = VK_MAKE_VERSION(1,0,0); // Similar to the App version, give vulkan an *engine* version
appInfo.apiVersion = VK_API_VERSION_1_3; // Tell vulkan what the highest API version we will allow this program to run on
appInfo.applicationVersion = VK_MAKE_VERSION(
1, 0,
0); // Create a Major Minor Patch version number for the application!
appInfo.pEngineName =
"Agnosia Engine"; // Give an internal name for the engine running
appInfo.engineVersion = VK_MAKE_VERSION(
1, 0, 0); // Similar to the App version, give vulkan an *engine* version
appInfo.apiVersion =
VK_API_VERSION_1_3; // Tell vulkan what the highest API version we will
// allow this program to run on
// This gets a little weird, Vulkan is platform agnostic, so you need to figure out what extensions to interface with the current system are needed
// So, to figure out what extension codes and how many to use, feed the pointer into *glfwGetRequiredInstanceExtensions*, which will get the necessary extensions!
// From there, we can send that over to our createInfo Vulkan info struct to make it fully platform agnostic!
// This gets a little weird, Vulkan is platform agnostic, so you need to
// figure out what extensions to interface with the current system are needed
// So, to figure out what extension codes and how many to use, feed the
// pointer into *glfwGetRequiredInstanceExtensions*, which will get the
// necessary extensions! From there, we can send that over to our createInfo
// Vulkan info struct to make it fully platform agnostic!
uint32_t glfwExtensionCount = 0;
const char **glfwExtensions;
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
std::vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
std::vector<const char *> extensions(glfwExtensions,
glfwExtensions + glfwExtensionCount);
VkInstanceCreateInfo createInfo{}; // Define parameters of new vulkan instance
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; // Tell vulkan this is a info structure
createInfo.pApplicationInfo = &appInfo; // We just created a new appInfo structure, so we pass the pointer to it.
createInfo.sType =
VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; // Tell vulkan this is a info
// structure
createInfo.pApplicationInfo =
&appInfo; // We just created a new appInfo structure, so we pass the
// pointer to it.
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
createInfo.ppEnabledExtensionNames = extensions.data();
if (vkCreateInstance(&createInfo, nullptr, &vulkaninstance) != VK_SUCCESS) {
throw std::runtime_error("failed to create instance!");
}
}
void initVulkan() {
@ -94,12 +109,12 @@ void mainLoop() {
void cleanup() {
render_present::Render::cleanupSwapChain();
graphics_pipeline::Graphics::destroyGraphicsPipeline();
//graphics_pipeline::Graphics::destroyRenderPass();
buffers_libs::Buffers::destroyUniformBuffer();
buffers_libs::Buffers::destroyDescriptorPool();
texture_libs::Texture::destroyTextureSampler();
texture_libs::Texture::destroyTextureImage();
vkDestroyDescriptorSetLayout(Global::device, Global::descriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(Global::device, Global::descriptorSetLayout,
nullptr);
buffers_libs::Buffers::destroyBuffers();
render_present::Render::destroyFenceSemaphores();
graphics_pipeline::Graphics::destroyCommandPool();
@ -118,12 +133,8 @@ EntryApp& EntryApp::getInstance() {
}
EntryApp::EntryApp() : initialized(false), framebufferResized(false) {}
void EntryApp::initialize() {
initialized = true;
}
bool EntryApp::isInitialized() const {
return initialized;
}
void EntryApp::initialize() { initialized = true; }
bool EntryApp::isInitialized() const { return initialized; }
void EntryApp::run() {
initWindow();
@ -131,4 +142,3 @@ void EntryApp::run() {
mainLoop();
cleanup();
}

View File

@ -26,18 +26,23 @@ namespace Global {
std::vector<uint32_t> indices;
Global::QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
// First we feed in a integer we want to use to hold the number of queued items, that fills it, then we create that amount of default constructed *VkQueueFamilyProperties* structs.
// These store the flags, the amount of queued items in the family, and timestamp data. Queue families are simply group collections of tasks we want to get done.
// Next, we check the flags of the queueFamily item, use a bitwise and to see if they match, i.e. support graphical operations, then return that to notify that we have at least one family that supports VK_QUEUE_GRAPHICS_BIT.
// Which means this device supports graphical operations!
// We also do the same thing for window presentation, just check to see if its supported.
// First we feed in a integer we want to use to hold the number of queued
// items, that fills it, then we create that amount of default constructed
// *VkQueueFamilyProperties* structs. These store the flags, the amount of
// queued items in the family, and timestamp data. Queue families are simply
// group collections of tasks we want to get done. Next, we check the flags of
// the queueFamily item, use a bitwise and to see if they match, i.e. support
// graphical operations, then return that to notify that we have at least one
// family that supports VK_QUEUE_GRAPHICS_BIT. Which means this device
// supports graphical operations! We also do the same thing for window
// presentation, just check to see if its supported.
Global::QueueFamilyIndices indices;
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount,
queueFamilies.data());
int i = 0;
for (const auto &queueFamily : queueFamilies) {
@ -46,7 +51,8 @@ namespace Global {
}
VkBool32 presentSupport = false;
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, Global::surface, &presentSupport);
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, Global::surface,
&presentSupport);
if (presentSupport) {
indices.presentFamily = i;
}
@ -58,4 +64,4 @@ namespace Global {
}
return indices;
}
}
} // namespace Global

View File

@ -261,27 +261,35 @@ void Graphics::recordCommandBuffer(VkCommandBuffer commandBuffer,
if (vkBeginCommandBuffer(commandBuffer, &beginInfo) != VK_SUCCESS) {
throw std::runtime_error("failed to begin recording command buffer!");
}
const VkImageMemoryBarrier imageMemoryBarrier{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
const VkImageMemoryBarrier2 imageMemoryBarrier{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
.pNext = nullptr,
.srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
.srcAccessMask = 0,
.dstStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.newLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = device_libs::DeviceControl::getSwapChainImages()[imageIndex],
.subresourceRange = {
.subresourceRange =
{
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = texture_libs::Texture::getMipLevels(),
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
}};
},
};
const VkDependencyInfo dependencyInfo{
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
.pNext = nullptr,
.imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &imageMemoryBarrier,
};
vkCmdPipelineBarrier2(commandBuffer, &dependencyInfo);
vkCmdPipelineBarrier(commandBuffer,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0,
nullptr, 1, &imageMemoryBarrier
);
// ------------------- DYNAMIC RENDER INFO ---------------------- //
const VkRenderingAttachmentInfo colorAttachmentInfo{
@ -347,6 +355,36 @@ void Graphics::recordCommandBuffer(VkCommandBuffer commandBuffer,
vkCmdEndRendering(commandBuffer);
const VkImageMemoryBarrier2 prePresentImageBarrier{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
.pNext = nullptr,
.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
.srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT,
.dstStageMask = VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT,
.dstAccessMask = 0,
.oldLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = device_libs::DeviceControl::getSwapChainImages()[imageIndex],
.subresourceRange =
{
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
};
const VkDependencyInfo depInfo{
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
.pNext = nullptr,
.imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &prePresentImageBarrier,
};
vkCmdPipelineBarrier2(Global::commandBuffers[Global::currentFrame], &depInfo);
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
throw std::runtime_error("failed to record command buffer!");
}

View File

@ -1,8 +1,9 @@
#include "render.h"
#include "graphicspipeline.h"
#include "../devicelibrary.h"
#include "../entrypoint.h"
#include "graphicspipeline.h"
#include "render.h"
#include "texture.h"
#include <vulkan/vulkan_core.h>
namespace render_present {
std::vector<VkSemaphore> imageAvailableSemaphores;
@ -17,7 +18,8 @@ namespace render_present {
glfwWaitEvents();
}
vkDeviceWaitIdle(Global::device);
// Don't really wanna do this but I also don't want to create an extra class instance just to call the cleanup function.
// Don't really wanna do this but I also don't want to create an extra class
// instance just to call the cleanup function.
for (auto imageView : Global::swapChainImageViews) {
vkDestroyImageView(Global::device, imageView, nullptr);
@ -33,12 +35,15 @@ namespace render_present {
// record a comman d buffer which draws the scene onto that image
// submit the recorded command buffer and present the image!
void Render::drawFrame() {
vkWaitForFences(Global::device, 1, &inFlightFences[Global::currentFrame], VK_TRUE, UINT64_MAX);
vkWaitForFences(Global::device, 1, &inFlightFences[Global::currentFrame],
VK_TRUE, UINT64_MAX);
vkResetFences(Global::device, 1, &inFlightFences[Global::currentFrame]);
uint32_t imageIndex;
VkResult result = vkAcquireNextImageKHR(Global::device, Global::swapChain, UINT64_MAX, imageAvailableSemaphores[Global::currentFrame], VK_NULL_HANDLE, &imageIndex);
VkResult result =
vkAcquireNextImageKHR(Global::device, Global::swapChain, UINT64_MAX,
imageAvailableSemaphores[Global::currentFrame],
VK_NULL_HANDLE, &imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR) {
recreateSwapChain();
return;
@ -50,26 +55,31 @@ namespace render_present {
vkResetFences(Global::device, 1, &inFlightFences[Global::currentFrame]);
vkResetCommandBuffer(Global::commandBuffers[Global::currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
graphics_pipeline::Graphics::recordCommandBuffer(Global::commandBuffers[Global::currentFrame], imageIndex);
vkResetCommandBuffer(Global::commandBuffers[Global::currentFrame],
/*VkCommandBufferResetFlagBits*/ 0);
graphics_pipeline::Graphics::recordCommandBuffer(
Global::commandBuffers[Global::currentFrame], imageIndex);
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
VkSemaphore waitSemaphores[] = {imageAvailableSemaphores[Global::currentFrame]};
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
VkSemaphore waitSemaphores[] = {
imageAvailableSemaphores[Global::currentFrame]};
VkPipelineStageFlags waitStages[] = {
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &Global::commandBuffers[Global::currentFrame];
VkSemaphore signalSemaphores[] = {renderFinishedSemaphores[Global::currentFrame]};
VkSemaphore signalSemaphores[] = {
renderFinishedSemaphores[Global::currentFrame]};
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
if (vkQueueSubmit(Global::graphicsQueue, 1, &submitInfo, inFlightFences[Global::currentFrame]) != VK_SUCCESS) {
if (vkQueueSubmit(Global::graphicsQueue, 1, &submitInfo,
inFlightFences[Global::currentFrame]) != VK_SUCCESS) {
throw std::runtime_error("failed to submit draw command buffer!");
}
@ -82,35 +92,36 @@ namespace render_present {
VkSwapchainKHR swapChains[] = {Global::swapChain};
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
result = vkQueuePresentKHR(Global::presentQueue, &presentInfo);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || EntryApp::getInstance().getFramebufferResized()) {
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR ||
EntryApp::getInstance().getFramebufferResized()) {
EntryApp::getInstance().setFramebufferResized(false);
recreateSwapChain();
} else if (result != VK_SUCCESS) {
throw std::runtime_error("failed to present swap chain image!");
}
Global::currentFrame = (Global::currentFrame + 1) % Global::MAX_FRAMES_IN_FLIGHT;
Global::currentFrame =
(Global::currentFrame + 1) % Global::MAX_FRAMES_IN_FLIGHT;
}
#pragma info
// SEMAPHORES
// Synchronization of execution on the GPU in Vulkan is *explicit* The Order of ops is up to us to
// define the how we want things to run.
// Similarly, Semaphores are used to add order between queue ops. There are 2 kinds of Semaphores; binary, and timeline.
// We are using Binary semaphores, which can be signaled or unsignaled.
// Semaphores are initizalized unsignaled, the way we use them to order queue operations is by providing the same semaphore in one queue op and a wait in another.
// For example:
// VkCommandBuffer QueueOne, QueueTwo = ...
// VkSemaphore semaphore = ...
// enqueue QueueOne, Signal semaphore when done, start now.
// vkQueueSubmit(work: QueueOne, signal: semaphore, wait: none)
// enqueue QueueTwo, wait on semaphore to start
// vkQueueSubmit(
// Synchronization of execution on the GPU in Vulkan is *explicit* The Order of
// ops is up to us to define the how we want things to run. Similarly,
// Semaphores are used to add order between queue ops. There are 2 kinds of
// Semaphores; binary, and timeline. We are using Binary semaphores, which can
// be signaled or unsignaled. Semaphores are initizalized unsignaled, the way we
// use them to order queue operations is by providing the same semaphore in one
// queue op and a wait in another. For example: VkCommandBuffer QueueOne,
// QueueTwo = ... VkSemaphore semaphore = ... enqueue QueueOne, Signal semaphore
// when done, start now. vkQueueSubmit(work: QueueOne, signal: semaphore, wait:
// none) enqueue QueueTwo, wait on semaphore to start vkQueueSubmit(
// work: QueueTwo, signal: None, wait: semaphore)
// FENCES
// Fences are basically semaphores for the CPU! Otherwise known as the host. If the host needs to know when the GPU has finished a task, we use a fence.
// Fences are basically semaphores for the CPU! Otherwise known as the host. If
// the host needs to know when the GPU has finished a task, we use a fence.
// VkCommandBuffer cmndBuf = ...
// VkFence fence = ...
// Start work immediately, signal fence when done.
@ -132,14 +143,15 @@ namespace render_present {
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
for (size_t i = 0; i < Global::MAX_FRAMES_IN_FLIGHT; i++) {
if(vkCreateSemaphore(Global::device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
vkCreateSemaphore(Global::device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
vkCreateFence(Global::device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
if (vkCreateSemaphore(Global::device, &semaphoreInfo, nullptr,
&imageAvailableSemaphores[i]) != VK_SUCCESS ||
vkCreateSemaphore(Global::device, &semaphoreInfo, nullptr,
&renderFinishedSemaphores[i]) != VK_SUCCESS ||
vkCreateFence(Global::device, &fenceInfo, nullptr,
&inFlightFences[i]) != VK_SUCCESS) {
throw std::runtime_error("Failed to create semaphores!");
}
}
}
void Render::destroyFenceSemaphores() {
for (size_t i = 0; i < Global::MAX_FRAMES_IN_FLIGHT; i++) {
@ -159,4 +171,4 @@ namespace render_present {
vkDestroySwapchainKHR(Global::device, Global::swapChain, nullptr);
}
}
} // namespace render_present