Last active
August 22, 2024 10:45
-
-
Save CoffeeVampir3/84ce144a61169d4c180c99ddf8eb6d6c to your computer and use it in GitHub Desktop.
y tho
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
g++ -std=c++20 -fmodules-ts -c VulkanValidation.cc | |
g++ -std=c++20 -fmodules-ts -c VulkanInstance.cc | |
g++ -std=c++20 -fmodules-ts -o VulkanTest VulkanInstance.o VulkanValidation.o main.cc -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define GLFW_INCLUDE_VULKAN | |
#include <GLFW/glfw3.h> | |
#include <iostream> | |
#include <stdexcept> | |
#include <cstring> | |
#include <cstdlib> | |
#include <optional> | |
import VulkanInstance; | |
const uint32_t WIDTH = 800; | |
const uint32_t HEIGHT = 600; | |
int main() { | |
// GLFW WINDOW | |
glfwInit(); | |
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); | |
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); | |
auto window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); | |
auto instance = Vulkan::CreateInstance(); | |
// PRIMARY LOOP | |
while (!glfwWindowShouldClose(window)) { | |
glfwPollEvents(); | |
} | |
// CLEANUP | |
vkDestroyInstance(instance, nullptr); | |
glfwDestroyWindow(window); | |
glfwTerminate(); | |
return EXIT_SUCCESS; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export module VulkanInstance; | |
#define GLFW_INCLUDE_VULKAN | |
#include <GLFW/glfw3.h> | |
import VulkanValidation; | |
export namespace Vulkan { | |
auto CreateInstance() { | |
VkInstance instance; | |
VkApplicationInfo appInfo{}; | |
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; | |
appInfo.pApplicationName = "Hello Triangle"; | |
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); | |
appInfo.pEngineName = "No Engine"; | |
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); | |
appInfo.apiVersion = VK_API_VERSION_1_0; | |
VkInstanceCreateInfo createInfo{}; | |
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; | |
createInfo.pApplicationInfo = &appInfo; | |
uint32_t glfwExtensionCount = 0; | |
const char** glfwExtensions; | |
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); | |
createInfo.enabledExtensionCount = glfwExtensionCount; | |
createInfo.ppEnabledExtensionNames = glfwExtensions; | |
createInfo.enabledLayerCount = 0; | |
VkResult result = vkCreateInstance(&createInfo, nullptr, &instance); | |
if (Vulkan::enableValidationLayers) { | |
createInfo.enabledLayerCount = static_cast<uint32_t>(Vulkan::validationLayers.size()); | |
createInfo.ppEnabledLayerNames = Vulkan::validationLayers.data(); | |
} else { | |
createInfo.enabledLayerCount = 0; | |
} | |
return instance; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export module VulkanValidation; | |
#define GLFW_INCLUDE_VULKAN | |
#include <GLFW/glfw3.h> | |
#include <vector> | |
export namespace Vulkan { | |
const std::vector<const char*> validationLayers = { | |
"VK_LAYER_KHRONOS_validation" | |
}; | |
#ifdef NDEBUG | |
const bool enableValidationLayers = false; | |
#else | |
const bool enableValidationLayers = true; | |
#endif | |
bool checkValidationLayerSupport() { | |
uint32_t layerCount; | |
vkEnumerateInstanceLayerProperties(&layerCount, nullptr); | |
std::vector<VkLayerProperties> availableLayers(layerCount); | |
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data()); | |
for (const char* layerName : validationLayers) { | |
bool layerFound = false; | |
for (const auto& layerProperties : availableLayers) { | |
if (strcmp(layerName, layerProperties.layerName) == 0) { | |
layerFound = true; | |
break; | |
} | |
} | |
if (!layerFound) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment