Last active
August 22, 2024 11:30
-
-
Save CoffeeVampir3/d330744a78d7ee067fa4ed245b486ea2 to your computer and use it in GitHub Desktop.
CPP Module Example
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 | |
# Comment these out after building for the first time as you'll get an error if they're built already because reasons. | |
g++ -std=c++20 -fmodules-ts -xc++-system-header vector | |
g++ -std=c++20 -fmodules-ts -xc++-system-header iostream | |
g++ -std=c++20 -fmodules-ts -xc++-system-header stdexcept | |
g++ -std=c++20 -fmodules-ts -xc++-system-header cstdlib | |
g++ -std=c++20 -fmodules-ts -xc++-system-header cstring | |
# Build each time | |
g++ -std=c++20 -fmodules-ts -c VulkanValidation.cc | |
g++ -std=c++20 -fmodules-ts -c Vulkan.cc | |
g++ -std=c++20 -fmodules-ts -o VulkanTest Vulkan.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> | |
import Vulkan; | |
import <cstdlib>; | |
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 Vulkan; | |
import <stdexcept>; | |
import Validation; | |
#define GLFW_INCLUDE_VULKAN | |
#include <GLFW/glfw3.h> | |
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 (enableValidationLayers && !checkValidationLayerSupport()) { | |
throw std::runtime_error("validation layers requested, but not available!"); | |
} | |
if (enableValidationLayers) { | |
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size()); | |
createInfo.ppEnabledLayerNames = 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 Validation; | |
import <vector>; | |
import <cstring>; | |
import <cstdlib>; | |
#define GLFW_INCLUDE_VULKAN | |
#include <GLFW/glfw3.h> | |
export const std::vector<const char*> validationLayers = { | |
"VK_LAYER_KHRONOS_validation" | |
}; | |
#ifdef NDEBUG | |
export const bool enableValidationLayers = false; | |
#else | |
export const bool enableValidationLayers = true; | |
#endif | |
export 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