147 lines
4.1 KiB
C
147 lines
4.1 KiB
C
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <vulkan/vulkan_core.h>
|
|
|
|
#define GLFW_INCLUDE_VULKAN
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#ifdef NDEBUG
|
|
const bool enableValidationLayers = false;
|
|
#else
|
|
const bool enableValidationLayers = true;
|
|
#endif
|
|
|
|
const char *validationLayers[] = {"VK_LAYER_KHRONOS_validation"};
|
|
size_t validationLayerCount =
|
|
sizeof(validationLayers) / sizeof(validationLayers[0]);
|
|
|
|
typedef struct Application {
|
|
GLFWwindow *window;
|
|
VkInstance instance;
|
|
} Application;
|
|
|
|
bool checkValidationLayerSupport() {
|
|
uint32_t layerCount;
|
|
vkEnumerateInstanceLayerProperties(&layerCount, NULL);
|
|
VkLayerProperties availableLayers[layerCount];
|
|
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers);
|
|
|
|
for (int i = 0; i < validationLayerCount; i++) {
|
|
bool layerFound = false;
|
|
for (int j = 0; j < layerCount; j++) {
|
|
if (strcmp(availableLayers[j].layerName, validationLayers[i]) == 0) {
|
|
layerFound = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!layerFound) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool verifyGlfwExtensionSupport(uint32_t extensionCount,
|
|
VkExtensionProperties *extentions,
|
|
uint32_t glfwExtensionCount,
|
|
const char **glfwExtensions) {
|
|
for (uint32_t i = 0; i < glfwExtensionCount; i++) {
|
|
bool layerFound = false;
|
|
for (uint32_t j = 0; j < extensionCount; j++) {
|
|
if (strcmp(glfwExtensions[i], extentions[j].extensionName) == 0) {
|
|
layerFound = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!layerFound) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void createInstance(Application *app) {
|
|
if (enableValidationLayers && !checkValidationLayerSupport()) {
|
|
fprintf(stderr, "Validation layers requested, but not available!");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
VkApplicationInfo appInfo = {
|
|
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
|
.pApplicationName = "Hello Triangle",
|
|
.applicationVersion = VK_MAKE_VERSION(1, 0, 0),
|
|
.pEngineName = "No Engine",
|
|
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
|
|
.apiVersion = VK_API_VERSION_1_0,
|
|
};
|
|
|
|
uint32_t glfwExtensionCount = 0;
|
|
const char **glfwExtensions;
|
|
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
|
|
|
VkInstanceCreateInfo createInfo = {
|
|
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
|
|
.pApplicationInfo = &appInfo,
|
|
.enabledExtensionCount = glfwExtensionCount,
|
|
.ppEnabledExtensionNames = glfwExtensions,
|
|
.enabledLayerCount = 0,
|
|
};
|
|
if (enableValidationLayers) {
|
|
createInfo.enabledLayerCount = validationLayerCount;
|
|
createInfo.ppEnabledLayerNames = validationLayers;
|
|
}
|
|
|
|
VkResult result = vkCreateInstance(&createInfo, NULL, &app->instance);
|
|
if (result != VK_SUCCESS) {
|
|
fprintf(stderr, "Failed to create vulkan instance\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
uint32_t extensionCount = 0;
|
|
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, NULL);
|
|
VkExtensionProperties extensions[extensionCount];
|
|
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, extensions);
|
|
|
|
if (!verifyGlfwExtensionSupport(extensionCount, extensions,
|
|
glfwExtensionCount, glfwExtensions)) {
|
|
fprintf(stderr, "Failed to find all required vulkan extentions for glfw\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
printf("Instance Extentions Available:\n");
|
|
for (uint i = 0; i < extensionCount; i++) {
|
|
printf("\t%s\n", extensions[i].extensionName);
|
|
}
|
|
}
|
|
|
|
void initWindow(Application *app) {
|
|
glfwInit();
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
|
app->window = glfwCreateWindow(800, 600, "Vulkan", NULL, NULL);
|
|
}
|
|
void initVulkan(Application *app) { createInstance(app); }
|
|
void mainLoop(Application *app) {
|
|
while (!glfwWindowShouldClose(app->window)) {
|
|
glfwPollEvents();
|
|
}
|
|
}
|
|
void cleanup(Application *app) {
|
|
vkDestroyInstance(app->instance, NULL);
|
|
glfwDestroyWindow(app->window);
|
|
glfwTerminate();
|
|
}
|
|
|
|
int main(void) {
|
|
Application app;
|
|
|
|
initWindow(&app);
|
|
initVulkan(&app);
|
|
mainLoop(&app);
|
|
cleanup(&app);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|