Moved functions and the like to appropriate enough file structure for now

This commit is contained in:
Warwick New 2025-01-26 12:34:48 +00:00
parent 5ce5da188a
commit 2c6c2b2592
5 changed files with 60 additions and 44 deletions

View file

@ -1,13 +1,17 @@
#include "vulkan_wrapper.h" #include "vulkan_wrapper.h"
#include "window.h"
#include <stdlib.h> #include <stdlib.h>
int main(void) { int main(void) {
Application app = {0}; VulkanApp app = {0};
initWindow(&app); window_init(&app);
initVulkan(&app); vulkan_init(&app);
mainLoop(&app);
cleanup(&app); mainLoop(&app); // TODO: pull this out of vulkan_wrapper
vulkan_deinit(&app);
window_deinit(&app);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View file

@ -43,7 +43,6 @@ size_t validationLayerCount =
static bool framebufferResized; // initialised to 0 static bool framebufferResized; // initialised to 0
typedef struct SwapChainSupportDetails { typedef struct SwapChainSupportDetails {
VkSurfaceCapabilitiesKHR capabilities; VkSurfaceCapabilitiesKHR capabilities;
uint32_t numFormats; uint32_t numFormats;
@ -106,7 +105,7 @@ void populateDebugMessengerCreateInfo(
createInfo->pfnUserCallback = debugCallback; createInfo->pfnUserCallback = debugCallback;
} }
void createInstance(Application *app) { void createInstance(VulkanApp *app) {
if (enableValidationLayers && !checkValidationLayerSupport()) { if (enableValidationLayers && !checkValidationLayerSupport()) {
fprintf(stderr, "Validation layers requested, but not available!\n"); fprintf(stderr, "Validation layers requested, but not available!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -178,20 +177,10 @@ void createInstance(Application *app) {
} }
} }
static void framebufferResizeCallback(GLFWwindow *window, int width, void framebufferResizeCallback(GLFWwindow *window, int width, int height) {
int height) {
framebufferResized = true; framebufferResized = true;
} }
void initWindow(Application *app) {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
app->window = glfwCreateWindow(800, 600, "Vulkan", NULL, NULL);
glfwSetFramebufferSizeCallback(app->window, framebufferResizeCallback);
}
VkResult CreateDebugUtilsMessengerEXT( VkResult CreateDebugUtilsMessengerEXT(
VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo, VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
const VkAllocationCallbacks *pAllocator, const VkAllocationCallbacks *pAllocator,
@ -206,7 +195,7 @@ VkResult CreateDebugUtilsMessengerEXT(
} }
} }
void setupDebugMessenger(Application *app) { void setupDebugMessenger(VulkanApp *app) {
if (!enableValidationLayers) { if (!enableValidationLayers) {
return; return;
} }
@ -401,7 +390,7 @@ bool isDeviceSuitable(VkPhysicalDevice device, VkSurfaceKHR *surface) {
return completeIndeces && extensionsSupported && swapChainAdequate; return completeIndeces && extensionsSupported && swapChainAdequate;
} }
void pickPhysicalDevice(Application *app) { void pickPhysicalDevice(VulkanApp *app) {
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
uint32_t physicalDeviceCount; uint32_t physicalDeviceCount;
@ -431,7 +420,7 @@ void pickPhysicalDevice(Application *app) {
app->physicalDevice = physicalDevice; app->physicalDevice = physicalDevice;
} }
void createLogicalDevice(Application *app) { void createLogicalDevice(VulkanApp *app) {
QueueFamilyIndices indices = QueueFamilyIndices indices =
findQueueFamilies(app->physicalDevice, &app->surface); findQueueFamilies(app->physicalDevice, &app->surface);
@ -499,14 +488,14 @@ void createLogicalDevice(Application *app) {
vkGetDeviceQueue(app->device, indices.presentFamily, 0, &app->presentQueue); vkGetDeviceQueue(app->device, indices.presentFamily, 0, &app->presentQueue);
} }
void createSurface(Application *app) { void createSurface(VulkanApp *app) {
if (glfwCreateWindowSurface(app->instance, app->window, NULL, if (glfwCreateWindowSurface(app->instance, app->window, NULL,
&app->surface) != VK_SUCCESS) { &app->surface) != VK_SUCCESS) {
fprintf(stderr, "Error creating vulkan surface in glfw window."); fprintf(stderr, "Error creating vulkan surface in glfw window.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
void createSwapChain(Application *app) { void createSwapChain(VulkanApp *app) {
SwapChainSupportDetails swapChainSupport = SwapChainSupportDetails swapChainSupport =
querySwapchainSupport(app->physicalDevice, &app->surface); querySwapchainSupport(app->physicalDevice, &app->surface);
VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat( VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(
@ -572,7 +561,7 @@ void createSwapChain(Application *app) {
free(swapChainSupport.formats); free(swapChainSupport.formats);
} }
void createImageViews(Application *app) { void createImageViews(VulkanApp *app) {
app->swapChainImageViewCount = app->swapChainImageCount; app->swapChainImageViewCount = app->swapChainImageCount;
app->swapChainImageViews = app->swapChainImageViews =
malloc(app->swapChainImageViewCount * sizeof(VkImageView)); malloc(app->swapChainImageViewCount * sizeof(VkImageView));
@ -659,7 +648,7 @@ VkShaderModule createShaderModule(VkDevice device, ShaderBuffer code) {
return shaderModule; return shaderModule;
} }
void createGraphicsPipeline(Application *app) { void createGraphicsPipeline(VulkanApp *app) {
ShaderBuffer vertShaderCode = loadShaderIntoBuffer("shaders/shader.vert.spv"); ShaderBuffer vertShaderCode = loadShaderIntoBuffer("shaders/shader.vert.spv");
ShaderBuffer fragShaderCode = loadShaderIntoBuffer("shaders/shader.frag.spv"); ShaderBuffer fragShaderCode = loadShaderIntoBuffer("shaders/shader.frag.spv");
@ -812,7 +801,7 @@ void createGraphicsPipeline(Application *app) {
vkDestroyShaderModule(app->device, vertShaderModule, NULL); vkDestroyShaderModule(app->device, vertShaderModule, NULL);
} }
void createRenderPass(Application *app) { void createRenderPass(VulkanApp *app) {
VkAttachmentDescription colorAttachment = {0}; VkAttachmentDescription colorAttachment = {0};
colorAttachment.format = app->swapChainImageFormat; colorAttachment.format = app->swapChainImageFormat;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT; colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
@ -856,7 +845,7 @@ void createRenderPass(Application *app) {
} }
} }
void createFramebuffers(Application *app) { void createFramebuffers(VulkanApp *app) {
app->swapChainFramebufferCount = app->swapChainImageViewCount; app->swapChainFramebufferCount = app->swapChainImageViewCount;
app->swapChainFramebuffers = app->swapChainFramebuffers =
malloc(app->swapChainFramebufferCount * sizeof(VkFramebuffer)); malloc(app->swapChainFramebufferCount * sizeof(VkFramebuffer));
@ -881,7 +870,7 @@ void createFramebuffers(Application *app) {
} }
} }
void CreateCommandPool(Application *app) { void CreateCommandPool(VulkanApp *app) {
QueueFamilyIndices queueFamilyIndices = QueueFamilyIndices queueFamilyIndices =
findQueueFamilies(app->physicalDevice, &app->surface); findQueueFamilies(app->physicalDevice, &app->surface);
@ -897,7 +886,7 @@ void CreateCommandPool(Application *app) {
} }
} }
void recordCommandBuffer(Application *app, VkCommandBuffer commandBuffer, void recordCommandBuffer(VulkanApp *app, VkCommandBuffer commandBuffer,
uint32_t imageIndex) { uint32_t imageIndex) {
VkCommandBufferBeginInfo beginInfo = {0}; VkCommandBufferBeginInfo beginInfo = {0};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
@ -953,7 +942,7 @@ void recordCommandBuffer(Application *app, VkCommandBuffer commandBuffer,
} }
} }
void createCommandBuffer(Application *app) { void createCommandBuffer(VulkanApp *app) {
VkCommandBufferAllocateInfo allocInfo = {0}; VkCommandBufferAllocateInfo allocInfo = {0};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = app->commandPool; allocInfo.commandPool = app->commandPool;
@ -967,7 +956,7 @@ void createCommandBuffer(Application *app) {
} }
} }
void createSyncObjects(Application *app) { void createSyncObjects(VulkanApp *app) {
VkSemaphoreCreateInfo semaphoreInfo = {0}; VkSemaphoreCreateInfo semaphoreInfo = {0};
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
@ -988,7 +977,7 @@ void createSyncObjects(Application *app) {
} }
} }
void cleanupSwapChain(Application *app) { void cleanupSwapChain(VulkanApp *app) {
for (size_t i = 0; i < app->swapChainFramebufferCount; i++) { for (size_t i = 0; i < app->swapChainFramebufferCount; i++) {
vkDestroyFramebuffer(app->device, app->swapChainFramebuffers[i], NULL); vkDestroyFramebuffer(app->device, app->swapChainFramebuffers[i], NULL);
@ -1005,7 +994,7 @@ void cleanupSwapChain(Application *app) {
vkDestroySwapchainKHR(app->device, app->swapChain, NULL); vkDestroySwapchainKHR(app->device, app->swapChain, NULL);
} }
void recreateSwapChain(Application *app) { void recreateSwapChain(VulkanApp *app) {
int width = 0, height = 0; int width = 0, height = 0;
glfwGetFramebufferSize(app->window, &width, &height); glfwGetFramebufferSize(app->window, &width, &height);
while (width == 0 || height == 0) { while (width == 0 || height == 0) {
@ -1021,7 +1010,7 @@ void recreateSwapChain(Application *app) {
createFramebuffers(app); createFramebuffers(app);
} }
void initVulkan(Application *app) { void vulkan_init(VulkanApp *app) {
createInstance(app); createInstance(app);
setupDebugMessenger(app); setupDebugMessenger(app);
createSurface(app); createSurface(app);
@ -1037,7 +1026,7 @@ void initVulkan(Application *app) {
createSyncObjects(app); createSyncObjects(app);
} }
void drawFrame(Application *app) { void drawFrame(VulkanApp *app) {
vkWaitForFences(app->device, 1, &app->inFlightFences[app->currentFrame], vkWaitForFences(app->device, 1, &app->inFlightFences[app->currentFrame],
VK_TRUE, UINT64_MAX); VK_TRUE, UINT64_MAX);
@ -1106,7 +1095,7 @@ void drawFrame(Application *app) {
app->currentFrame = (app->currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; app->currentFrame = (app->currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
} }
void mainLoop(Application *app) { void mainLoop(VulkanApp *app) {
while (!glfwWindowShouldClose(app->window)) { while (!glfwWindowShouldClose(app->window)) {
glfwPollEvents(); glfwPollEvents();
drawFrame(app); drawFrame(app);
@ -1115,7 +1104,7 @@ void mainLoop(Application *app) {
vkDeviceWaitIdle(app->device); vkDeviceWaitIdle(app->device);
} }
void cleanup(Application *app) { void vulkan_deinit(VulkanApp *app) {
cleanupSwapChain(app); cleanupSwapChain(app);
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
@ -1136,6 +1125,4 @@ void cleanup(Application *app) {
} }
vkDestroySurfaceKHR(app->instance, app->surface, NULL); vkDestroySurfaceKHR(app->instance, app->surface, NULL);
vkDestroyInstance(app->instance, NULL); vkDestroyInstance(app->instance, NULL);
glfwDestroyWindow(app->window);
glfwTerminate();
} }

View file

@ -34,9 +34,10 @@ typedef struct {
VkSemaphore renderFinishedSemaphore[MAX_FRAMES_IN_FLIGHT]; VkSemaphore renderFinishedSemaphore[MAX_FRAMES_IN_FLIGHT];
VkFence inFlightFences[MAX_FRAMES_IN_FLIGHT]; VkFence inFlightFences[MAX_FRAMES_IN_FLIGHT];
uint32_t currentFrame; // initialised to 0 uint32_t currentFrame; // initialised to 0
} Application; } VulkanApp;
void initWindow(Application *app); void vulkan_init(VulkanApp *app);
void initVulkan(Application *app); void mainLoop(VulkanApp *app);
void mainLoop(Application *app); void vulkan_deinit(VulkanApp *app);
void cleanup(Application *app);
void framebufferResizeCallback(GLFWwindow *window, int width, int height);

View file

@ -0,0 +1,18 @@
#include "window.h"
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
void window_init(VulkanApp *app) {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
app->window = glfwCreateWindow(800, 600, "Vulkan", NULL, NULL);
glfwSetFramebufferSizeCallback(app->window, framebufferResizeCallback);
}
void window_deinit(VulkanApp *app) {
glfwDestroyWindow(app->window);
glfwTerminate();
}

View file

@ -1 +1,7 @@
#pragma once #pragma once
#include "vulkan_wrapper.h"
void window_init(VulkanApp *app);
void window_deinit(VulkanApp *app);