Vulkan hard :(
This commit is contained in:
319
src/main.c
Normal file
319
src/main.c
Normal file
@ -0,0 +1,319 @@
|
||||
#include "main.h"
|
||||
|
||||
int32_t main() {
|
||||
// Init variables
|
||||
int i;
|
||||
|
||||
//Prepare GLFW, get some info now that we pass into vulkan later.
|
||||
if(!glfwInit()) {
|
||||
printf("Failed to init glfw\n");
|
||||
return 1;
|
||||
}
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
||||
GLFWwindow *window = glfwCreateWindow(
|
||||
WINDOW_WIDTH, WINDOW_HEIGHT, "Vulkan window", NULL, NULL
|
||||
);
|
||||
uint32_t glfwExtensionCount = 0;
|
||||
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
||||
|
||||
//Prepare information about the app for Vulkan
|
||||
VkApplicationInfo appInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
||||
.pNext = NULL,
|
||||
.pApplicationName = "Step 1",
|
||||
.applicationVersion = 1,
|
||||
.pEngineName = NULL,
|
||||
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
|
||||
.apiVersion = VK_API_VERSION_1_0
|
||||
};
|
||||
|
||||
// Create the Vulkan instance
|
||||
VkInstanceCreateInfo createInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
|
||||
.pNext = NULL,
|
||||
.flags = 0,
|
||||
.ppEnabledLayerNames = NULL,
|
||||
.pApplicationInfo = &appInfo,
|
||||
.enabledExtensionCount = glfwExtensionCount,
|
||||
.ppEnabledExtensionNames = glfwExtensions,
|
||||
.enabledLayerCount = 0
|
||||
};
|
||||
VkInstance instance;
|
||||
VkResult result = vkCreateInstance(&createInfo, NULL, &instance);
|
||||
if(result != VK_SUCCESS) {
|
||||
printf("Failed to init vulkan\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create the rendering surface
|
||||
VkSurfaceKHR surface;
|
||||
if(glfwCreateWindowSurface(instance, window, NULL, &surface) != VK_SUCCESS) {
|
||||
printf("Failed to create window surface!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Get and print the extension support
|
||||
uint32_t extensionCount = 0;
|
||||
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, NULL);
|
||||
VkExtensionProperties *extensionProperties = malloc(sizeof(VkExtensionProperties) * extensionCount);
|
||||
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, extensionProperties);
|
||||
printf("Available extensions:\n");
|
||||
for(i = 0; i < extensionCount; i++) {
|
||||
printf(" %s\n", extensionProperties[i].extensionName);
|
||||
}
|
||||
|
||||
// Physical Device finding.
|
||||
uint32_t deviceCount = 0;
|
||||
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
||||
vkEnumeratePhysicalDevices(instance, &deviceCount, NULL);
|
||||
if(deviceCount == 0) {
|
||||
printf("No Vulkan GPU\n");
|
||||
return 1;
|
||||
}
|
||||
VkPhysicalDevice *devices = malloc(sizeof(VkPhysicalDevice) * deviceCount);
|
||||
|
||||
// Select a suitable device from that list of devices we just made.
|
||||
QueueFamilyIndices indices;
|
||||
SwapChainSupportDetails details;
|
||||
vkEnumeratePhysicalDevices(instance, &deviceCount, devices);
|
||||
for(i = 0; i < deviceCount; i++) {
|
||||
VkPhysicalDevice device = devices[i];
|
||||
VkPhysicalDeviceProperties deviceProperties;
|
||||
VkPhysicalDeviceFeatures deviceFeatures;
|
||||
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
||||
vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
|
||||
|
||||
// For this we select a DISCRETE GPU, not an internal.
|
||||
if(deviceProperties.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) continue;
|
||||
// Make sure it can run a geom shader
|
||||
if(!deviceFeatures.geometryShader) continue;
|
||||
|
||||
// This is something I don't really understand but I know we need it.
|
||||
// Follow the method for info.
|
||||
indices = findQueueFamilies(device);
|
||||
if(!indices.graphicsFamilyFound) continue;
|
||||
|
||||
// Supports Presenting?
|
||||
VkBool32 presentSupport = false;
|
||||
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
|
||||
if(!presentSupport) continue;
|
||||
indices.presentFamily = i;
|
||||
indices.presentFamilyFound = true;
|
||||
|
||||
// Now I'm trying to find out what extensions this device supports
|
||||
uint32_t extensionCount;
|
||||
vkEnumerateDeviceExtensionProperties(device, NULL, &extensionCount, NULL);
|
||||
VkExtensionProperties *availableExtensions = malloc(sizeof(VkExtensionProperties) * extensionCount);
|
||||
vkEnumerateDeviceExtensionProperties(device, NULL, &extensionCount, availableExtensions);
|
||||
|
||||
// Make sure it supports the things I need
|
||||
int countHas = 0;
|
||||
for(int j = 0; j < extensionCount; j++) {
|
||||
VkExtensionProperties prop = availableExtensions[j];
|
||||
if(strcmp(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0) {
|
||||
countHas++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if(countHas == 0) continue;
|
||||
|
||||
// Find out the capabilities of the swap chain.
|
||||
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
|
||||
|
||||
uint32_t formatCount, presentModeCount;
|
||||
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, NULL);
|
||||
if(formatCount == 0) continue;
|
||||
|
||||
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, NULL);
|
||||
if(presentModeCount == 0) continue;
|
||||
|
||||
details.formats = malloc(sizeof(VkSurfaceFormatKHR) * formatCount);
|
||||
details.presentModes = malloc(sizeof(VkPresentModeKHR) * presentModeCount);
|
||||
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats);
|
||||
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes);
|
||||
|
||||
// Determine the surface format
|
||||
details.surfaceFormat = details.formats[0];
|
||||
for(int j = 0; j < formatCount; j++) {
|
||||
VkSurfaceFormatKHR availableFormat = details.formats[j];
|
||||
if(availableFormat.format != VK_FORMAT_B8G8R8A8_SRGB) continue;
|
||||
if(availableFormat.colorSpace != VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) continue;
|
||||
details.surfaceFormat = availableFormat;
|
||||
break;
|
||||
}
|
||||
|
||||
// Choose the surface mode, prefer triple buffered, fallback to vsync
|
||||
details.presentMode = VK_PRESENT_MODE_FIFO_KHR;
|
||||
for(int j = 0; j < presentModeCount; j++) {
|
||||
VkPresentModeKHR availableMode = details.presentModes[j];
|
||||
if(availableMode != VK_PRESENT_MODE_MAILBOX_KHR) continue;
|
||||
details.presentMode = VK_PRESENT_MODE_MAILBOX_KHR;
|
||||
break;
|
||||
}
|
||||
|
||||
physicalDevice = device;
|
||||
break;
|
||||
}
|
||||
|
||||
// Did we find it?
|
||||
if(physicalDevice == VK_NULL_HANDLE) {
|
||||
printf("No Vulkan Device\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Swap Extent
|
||||
if (details.capabilities.currentExtent.width != UINT32_MAX) {
|
||||
details.extent = details.capabilities.currentExtent;
|
||||
} else {
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
|
||||
VkExtent2D actualExtent = {
|
||||
.width = width,
|
||||
.height = height
|
||||
};
|
||||
|
||||
actualExtent.width = MAX(
|
||||
details.capabilities.minImageExtent.width,
|
||||
MIN(details.capabilities.maxImageExtent.width, actualExtent.width)
|
||||
);
|
||||
actualExtent.height = MAX(
|
||||
details.capabilities.minImageExtent.height,
|
||||
MIN(details.capabilities.maxImageExtent.height, actualExtent.height)
|
||||
);
|
||||
|
||||
details.extent = actualExtent;
|
||||
}
|
||||
|
||||
uint32_t imageCount = details.capabilities.minImageCount + 1;
|
||||
if (details.capabilities.maxImageCount > 0 && imageCount > details.capabilities.maxImageCount) {
|
||||
imageCount = details.capabilities.maxImageCount;
|
||||
}
|
||||
|
||||
VkSwapchainCreateInfoKHR swapChainCreateInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
|
||||
.surface = surface,
|
||||
.minImageCount = imageCount,
|
||||
.imageFormat = details.surfaceFormat.format,
|
||||
.imageColorSpace = details.surfaceFormat.colorSpace,
|
||||
.imageExtent = details.extent,
|
||||
.imageArrayLayers = 1,
|
||||
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
||||
.preTransform = details.capabilities.currentTransform,
|
||||
.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
|
||||
.presentMode = details.presentMode,
|
||||
.clipped = VK_TRUE,
|
||||
.oldSwapchain = VK_NULL_HANDLE
|
||||
};
|
||||
|
||||
uint32_t queueFamilyIndices[2];
|
||||
queueFamilyIndices[0] = indices.graphicsFamily;
|
||||
queueFamilyIndices[1] = indices.presentFamily;
|
||||
if (indices.graphicsFamily != indices.presentFamily) {
|
||||
swapChainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
||||
swapChainCreateInfo.queueFamilyIndexCount = 2;
|
||||
swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
|
||||
} else {
|
||||
swapChainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
swapChainCreateInfo.queueFamilyIndexCount = 0; // Optional
|
||||
swapChainCreateInfo.pQueueFamilyIndices = NULL; // Optional
|
||||
}
|
||||
|
||||
// Now we create a LOGICAL device. I think this is a way of reserving the
|
||||
// queue from the indices and using it for our own needs.
|
||||
float queuePriority = 1.0f;
|
||||
VkDeviceQueueCreateInfo queueCreateInfo[DEV_QUEUE_COUNT];
|
||||
for(i = 0; i < DEV_QUEUE_COUNT; i++) {
|
||||
queueCreateInfo[i].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
||||
queueCreateInfo[i].queueFamilyIndex = i == 0 ? indices.graphicsFamily : indices.presentFamily;
|
||||
queueCreateInfo[i].queueCount = 1;
|
||||
queueCreateInfo[i].pNext = NULL;
|
||||
queueCreateInfo[i].pQueuePriorities = &queuePriority;// Related to threading
|
||||
}
|
||||
|
||||
VkPhysicalDeviceFeatures deviceFeatures = {
|
||||
.alphaToOne = VK_FALSE,
|
||||
};
|
||||
|
||||
char *logicalEnabledExtensions[1];
|
||||
logicalEnabledExtensions[0] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
|
||||
VkDeviceCreateInfo logicalCreateInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
||||
.pQueueCreateInfos = queueCreateInfo,
|
||||
.queueCreateInfoCount = DEV_QUEUE_COUNT,
|
||||
.pEnabledFeatures = &deviceFeatures,
|
||||
.enabledExtensionCount = 1,
|
||||
.ppEnabledExtensionNames = logicalEnabledExtensions,
|
||||
.enabledLayerCount = 0,
|
||||
.pNext = NULL
|
||||
};
|
||||
VkDevice device;
|
||||
if(vkCreateDevice(physicalDevice, &logicalCreateInfo, NULL, &device) != VK_SUCCESS) {
|
||||
printf("Failed to create logical device.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Get back our queues we made.
|
||||
VkQueue graphicsQueue, presentQueue;
|
||||
vkGetDeviceQueue(device, indices.graphicsFamily, 0, &graphicsQueue);
|
||||
vkGetDeviceQueue(device, indices.presentFamily, 0, &presentQueue);
|
||||
|
||||
VkSwapchainKHR swapChain;
|
||||
if(vkCreateSwapchainKHR(device, &swapChainCreateInfo, NULL, &swapChain) != VK_SUCCESS) {
|
||||
printf("Failed to create a swap chain\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
//Idk
|
||||
uint32_t imageCount;
|
||||
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, NULL);
|
||||
VkImage* swapChainImages = malloc(sizeof(VkImage) * imageCount);
|
||||
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages);
|
||||
|
||||
VkImageView> swapChainImageViews;
|
||||
|
||||
// Begin render loop
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
vkDestroySwapchainKHR(device, swapChain, NULL);
|
||||
vkDestroyDevice(device, NULL);
|
||||
vkDestroySurfaceKHR(instance, surface, NULL);
|
||||
vkDestroyInstance(instance, NULL);
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
|
||||
QueueFamilyIndices indices = {
|
||||
.graphicsFamily = 0,
|
||||
.graphicsFamilyFound = false,
|
||||
.presentFamily = 0,
|
||||
.presentFamilyFound = false
|
||||
};
|
||||
|
||||
// Basically what I understand is we're trying to find a specific kind of
|
||||
// queue from the capabilities of queues that the device supports. I think
|
||||
// that I will understand queues more later but for now we're just finding...
|
||||
uint32_t queueFamilyCount = 0;
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, NULL);
|
||||
|
||||
VkQueueFamilyProperties *properties = malloc(sizeof(VkQueueFamilyProperties) * queueFamilyCount);
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, properties);
|
||||
|
||||
for(int i = 0; i < queueFamilyCount; i++) {
|
||||
// ... A queue that has this particular bit flag.
|
||||
VkQueueFamilyProperties queueFamily = properties[i];
|
||||
|
||||
if(queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
|
||||
indices.graphicsFamily = i;
|
||||
indices.graphicsFamilyFound = true;
|
||||
}
|
||||
}
|
||||
|
||||
return indices;
|
||||
}
|
42
src/main.h
Normal file
42
src/main.h
Normal file
@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#define VK_USE_PLATFORM_WIN32_KHR
|
||||
#define GLFW_INCLUDE_VULKAN
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cglm/call.h>
|
||||
|
||||
#define WINDOW_WIDTH 800
|
||||
#define WINDOW_HEIGHT 600
|
||||
#define MIN(a,b) (((a)<(b))?(a):(b))
|
||||
#define MAX(a,b) (((a)>(b))?(a):(b))
|
||||
#define DEV_QUEUE_COUNT 2
|
||||
|
||||
typedef struct {
|
||||
uint32_t graphicsFamily;
|
||||
bool graphicsFamilyFound;
|
||||
|
||||
uint32_t presentFamily;
|
||||
bool presentFamilyFound;
|
||||
} QueueFamilyIndices;
|
||||
|
||||
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
|
||||
|
||||
typedef struct {
|
||||
VkSurfaceCapabilitiesKHR capabilities;
|
||||
VkSurfaceFormatKHR *formats;
|
||||
VkPresentModeKHR *presentModes;
|
||||
|
||||
VkSurfaceFormatKHR surfaceFormat;
|
||||
VkPresentModeKHR presentMode;
|
||||
VkExtent2D extent;
|
||||
} SwapChainSupportDetails;
|
||||
|
||||
/**
|
||||
* Entry of the program
|
||||
* @return 0 if success, anything else for failure.
|
||||
*/
|
||||
int32_t main();
|
Reference in New Issue
Block a user