diff options
| author | Miguel <m.i@gmx.at> | 2025-01-09 15:47:49 +0100 |
|---|---|---|
| committer | Miguel <m.i@gmx.at> | 2025-01-09 15:47:49 +0100 |
| commit | 2c41b643607f2f717824a035eafde5a89340fed5 (patch) | |
| tree | 4a7361f47914345e394a40ea7b6bb0b6b50e1584 | |
| parent | ef4538478075dada9161c1f0175ae7409f977603 (diff) | |
some cleanup
| -rw-r--r-- | Makefile | 15 | ||||
| -rw-r--r-- | main.cpp | 114 | ||||
| -rw-r--r-- | test.cpp | 10 |
3 files changed, 75 insertions, 64 deletions
@@ -1,11 +1,22 @@ CFLAGS = -std=c++17 -O2 -g LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + VulkanTest: main.cpp g++ $(CFLAGS) -o VulkanTest main.cpp $(LDFLAGS) -.PHONY: test clean -test: VulkanTest +shaders/frag.spv: shaders/shader.frag + glslc shaders/shader.frag -o shaders/frag.spv + +shaders/vert.spv: shaders/shader.vert + glslc shaders/shader.vert -o shaders/vert.spv + +.PHONY: run clean + +run: VulkanTest shaders/frag.spv shaders/vert.spv ./VulkanTest clean: rm -f VulkanTest + rm -f main.out + rm -f main.exe + rm -f shaders/*.spv @@ -32,25 +32,26 @@ public: private: + const uint32_t WIDTH = 800; + const uint32_t HEIGHT = 600; + #ifdef NDEBUG const bool enableValidationLayers = false; #else const bool enableValidationLayers = true; #endif - const uint32_t WIDTH = 800; - const uint32_t HEIGHT = 600; + const std::vector<const char*> validationLayers = { + "VK_LAYER_KHRONOS_validation" + }; const std::vector<const char*> deviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME }; - const std::vector<const char*> validationLayers = { - "VK_LAYER_KHRONOS_validation" - }; - + // READ FILE - HELPER static std::vector<char> readFile(const std::string& filename) { - std::ifstream file(filename, std::ios::ate | std::ios::binary); + std::ifstream file(filename, std::ios::ate | std::ios::binary); if (!file.is_open()) throw std::runtime_error("failed to open file!"); size_t fileSize = (size_t) file.tellg(); std::vector<char> buffer(fileSize); @@ -60,11 +61,26 @@ private: return buffer; } - bool quit; + // JUST LIST AVILABLE EXTENSIONS + static void listExt() { + uint32_t extensionCount = 0; + vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); + std::vector<VkExtensionProperties> extensionslist(extensionCount); + vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensionslist.data()); + for (const auto& extension : extensionslist) { + std::cout << "EXT: " << extension.extensionName << std::endl; + } + } - GLFWwindow* window; + // CHECK SUCCESS - HELPER + static void checkSucc(const std::string &str, VkResult res){ + if(res!=VK_SUCCESS) throw std::runtime_error("failed to: " + str); + } + bool quit=false; + GLFWwindow* window; VkInstance instance; + VkDebugUtilsMessengerEXT debugMessenger; VkSurfaceKHR surface; @@ -95,7 +111,7 @@ private: VkSemaphore renderFinishedSemaphore; VkFence inFlightFence; - void initWindow() { + void initWindow() { glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // no OpenGL glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // no resizing @@ -134,7 +150,7 @@ private: void initVulkan() { - createInstance(); // 01 Setup -> Instance + createInstance(); // 01 Setup -> Instance setupDebugMessenger(); // 02 Setup -> Validation Layers createSurface(); // 05 Presentation -> Window surface pickPhysicalDevice(); // 03 Setup -> Physical Device @@ -142,7 +158,7 @@ private: createSwapChain(); // 06 Presentation -> Swap Chain createImageViews(); // 07 Presentation -> Image Views createRenderPass(); // 11 Graphics Pipeline -> RenderPasses - createGraphicsPipeline(); // 08 Graphics Pipeline -> Introduction + createGraphicsPipeline(); // 08 Graphics Pipeline -> Introduction createFramebuffers(); // 12 Drawing -> Framebuffers createCommandPool(); // 13 Drawing -> Commmand Buffers createCommandBuffer(); // -- @@ -152,7 +168,7 @@ private: void createSyncObjects() { VkSemaphoreCreateInfo semaphoreInfo{}; semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; - + VkFenceCreateInfo fenceInfo{}; fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; @@ -188,7 +204,7 @@ private: vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline); - + VkViewport viewport{}; viewport.x = 0.0f; viewport.y = 0.0f; @@ -209,7 +225,7 @@ private: if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) { throw std::runtime_error("failed to record command buffer!"); } - } + } void createCommandBuffer() { VkCommandBufferAllocateInfo allocInfo{}; @@ -286,7 +302,7 @@ private: dependency.srcAccessMask = 0; dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; - + VkRenderPassCreateInfo renderPassInfo{}; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; renderPassInfo.attachmentCount = 1; @@ -295,7 +311,7 @@ private: renderPassInfo.pSubpasses = &subpass; renderPassInfo.dependencyCount = 1; renderPassInfo.pDependencies = &dependency; - + if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) { throw std::runtime_error("failed to create render pass!"); } @@ -323,7 +339,7 @@ private: fragShaderStageInfo.pName = "main"; VkPipelineShaderStageCreateInfo shaderStages[] = {vertShaderStageInfo, fragShaderStageInfo}; - + // 10 Graphics Pipeline -> Fixed Functions std::vector<VkDynamicState> dynamicStates = { VK_DYNAMIC_STATE_VIEWPORT, @@ -427,7 +443,7 @@ private: throw std::runtime_error("failed to create pipeline layout!"); } - // 12 Graphics Pipeline -> Conclusion + // 12 Graphics Pipeline -> Conclusion VkGraphicsPipelineCreateInfo pipelineInfo{}; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineInfo.stageCount = 2; @@ -488,7 +504,7 @@ private: if (vkCreateImageView(device, &createInfo, nullptr, &swapChainImageViews[i]) != VK_SUCCESS) { throw std::runtime_error("failed to create image views!"); } - } + } } void createSwapChain() { @@ -578,7 +594,7 @@ private: std::vector<VkDeviceQueueCreateInfo> queueCreateInfos; std::set<uint32_t> uniqueQueueFamilies = {indices.graphicsFamily.value(), indices.presentFamily.value()}; - + for (uint32_t queueFamily : uniqueQueueFamilies) { VkDeviceQueueCreateInfo queueCreateInfo{}; queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; @@ -627,8 +643,8 @@ private: vkGetPhysicalDeviceProperties(device, &deviceProperties); vkGetPhysicalDeviceFeatures(device, &deviceFeatures); - // return deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && deviceFeatures.geometryShader; - + // return deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && deviceFeatures.geometryShader; + QueueFamilyIndices indices = findQueueFamilies(device); bool extensionsSupported = checkDeviceExtensionSupport(device); @@ -650,7 +666,7 @@ private: std::set<std::string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end()); for (const auto& extension : availableExtensions) { - std::cout << "Available Device Extension: " << extension.extensionName << std::endl; + // std::cout << "Available Device Extension: " << extension.extensionName << std::endl; requiredExtensions.erase(extension.extensionName); } @@ -714,9 +730,7 @@ private: } void createInstance() { - if (enableValidationLayers && !checkValidationLayerSupport()) { - throw std::runtime_error("validation layers requested, but not available!"); - } + VkApplicationInfo appInfo{}; appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; appInfo.pApplicationName = "Hello Triangle"; @@ -725,22 +739,20 @@ private: 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; + VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo{}; auto extensions = getRequiredExtensions(); - createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size()); - createInfo.ppEnabledExtensionNames = extensions.data(); - if (enableValidationLayers) { - createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size()); - createInfo.ppEnabledLayerNames = validationLayers.data(); - } else { - createInfo.enabledLayerCount = 0; + if (enableValidationLayers && !checkValidationLayerSupport()) { + throw std::runtime_error("validation layers requested, but not available!"); } - VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo{}; + VkInstanceCreateInfo createInfo{}; + createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + createInfo.pApplicationInfo = &appInfo; + createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size()); + createInfo.ppEnabledExtensionNames = extensions.data(); + if (enableValidationLayers) { createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size()); createInfo.ppEnabledLayerNames = validationLayers.data(); @@ -751,35 +763,22 @@ private: createInfo.pNext = nullptr; } - if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) { - throw std::runtime_error("failed to create instance!"); - } - - uint32_t extensionCount = 0; - vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); - std::vector<VkExtensionProperties> extensionslist(extensionCount); - vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensionslist.data()); - std::cout << "available extensions:\n"; - for (const auto& extension : extensionslist) { - std::cout << '\t' << extension.extensionName << '\n'; - } + // Create Instance + checkSucc("CREATE INSTANCE", vkCreateInstance(&createInfo, nullptr, &instance)); } + // O(n^2) 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; - std::cout << layerName << std::endl; - break; - } + std::cout << "LAYER: " << layerProperties.layerName << std::endl; + if (strcmp(layerName, layerProperties.layerName) == 0) layerFound=true; } if (!layerFound) { @@ -955,8 +954,9 @@ private: if (enableValidationLayers) { DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr); } - vkDestroySurfaceKHR(instance, surface, nullptr); + vkDestroySurfaceKHR(instance, surface, nullptr); vkDestroyInstance(instance, nullptr); + glfwDestroyWindow(window); glfwTerminate(); } @@ -8,7 +8,7 @@ class C1 { -public: +public: C1() : name{"default"} { std::cout << "C1 default cons: " << name << std::endl; @@ -31,11 +31,11 @@ public: std::cout << "C1 move constructor: " << name << std::endl; other.name+=" (moved away)"; } - + C1& operator=(C1&& other) { std::cout << "C1 move called: " << other.name << " -> " << name << std::endl; name = other.name; - other.name+=" (moved away)"; + other.name+=" (moved away)"; return *this; } @@ -51,7 +51,7 @@ public: std::cout << "C1 says: " << name << std::endl; } -private: +private: std::string name; @@ -77,5 +77,5 @@ int main() { c.say(); return EXIT_SUCCESS; - + } |
