From 0c5fa3525b2c8151bf83a215eee992c257d6fa28 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Mon, 2 Oct 2017 18:52:26 +0300 Subject: [PATCH] quick backup - everything is going to be changed on vulkan side. --- external/gph-math | 2 +- external/libimago | 2 +- src/main.cc | 7 +- src/opengl/opengl.cc | 1 + src/opengl/opengl.h | 6 +- src/opengl/texture-gl.h | 2 +- src/vulkan/vk.cc | 393 ++++++++++++++++++++++++++++++++++++++++++++++- src/vulkan/vk.h | 7 +- 8 files changed, 400 insertions(+), 20 deletions(-) diff --git a/external/gph-math b/external/gph-math index 54ac4cb..9f7489b 160000 --- a/external/gph-math +++ b/external/gph-math @@ -1 +1 @@ -Subproject commit 54ac4cbc3b08744e570a53e7f1a8ba862ea87b80 +Subproject commit 9f7489b288f64a5a8e95a0d2ce67594e7fed950b diff --git a/external/libimago b/external/libimago index 2228b92..6d5b91d 160000 --- a/external/libimago +++ b/external/libimago @@ -1 +1 @@ -Subproject commit 2228b925b87ee5458bd59611c11fc98c051446cc +Subproject commit 6d5b91de61e8710324852a356d1b7704fc22596c diff --git a/src/main.cc b/src/main.cc index 69721c1..6b1050b 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,4 +1,5 @@ #include +#include #include #include @@ -80,7 +81,7 @@ int main(int argc, char **argv) { Gfx_API api; - for(int i=0; i +#include #include #include "gfxapi.h" diff --git a/src/opengl/opengl.h b/src/opengl/opengl.h index 1a9a86f..7b4d11d 100644 --- a/src/opengl/opengl.h +++ b/src/opengl/opengl.h @@ -1,11 +1,7 @@ #ifndef OPENGL_H_ #define OPENGL_H_ -#include - bool init_opengl(); void cleanup_opengl(); -GLFWwindow *create_opengl_window(); -void clear_gl(); -#endif // OPENGL_H_ +#endif // OPENGL_H_ \ No newline at end of file diff --git a/src/opengl/texture-gl.h b/src/opengl/texture-gl.h index 0b495ed..748b4e7 100644 --- a/src/opengl/texture-gl.h +++ b/src/opengl/texture-gl.h @@ -6,7 +6,7 @@ class TextureGL : public Texture { private: unsigned int tex; - GLenum target; + unsigned int target; virtual void update() override; public: diff --git a/src/vulkan/vk.cc b/src/vulkan/vk.cc index 24941b5..105caba 100644 --- a/src/vulkan/vk.cc +++ b/src/vulkan/vk.cc @@ -1,20 +1,403 @@ -#include "vulkan/vk.h" -extern GLFWwindow win; +#define GLFW_INCLUDE_VULKAN +#include + +#include +#include +#include + +#include +#include + +#include "gfxapi.h" + +/* global variables */ +extern GLFWwindow *win; +extern int win_w; +extern int win_h; + +/* static variables */ +static std::vector enabled_extension_names; +static VkInstance inst; +static VkDevice device; +static VkPhysicalDevice pdev; +static VkSurfaceKHR surface; +static uint32_t device_mem_idx; +static uint32_t num_queues; +static uint32_t qfamily_idx; + +static const char *print_vulkan_error(VkResult error); +static const char *dev_type_str(VkPhysicalDeviceType type); +static const char *heap_flags_str(VkMemoryHeapFlags flags); +static const char *memtype_flags_str(VkMemoryPropertyFlags flags); +static const char *queue_flags_str(VkQueueFlags flags); + +/* static fumctions */ +static bool create_instance(); +static bool create_device(); bool init_vulkan() { + if(!glfwInit()) { + fprintf(stderr, "Failed to initialize GLFW.\n"); + return false; + } + + if(!glfwVulkanSupported()) { + fprintf(stderr, "No Vulkan support on the device.\n"); + return false; + } + + if(!create_instance()) { + fprintf(stderr, "Failed to enable validation.\n"); + return false; + } + + if(!glfwGetPhysicalDevicePresentationSupport(inst, pdev, qfamily_idx)) { + fprintf(stderr, "Presentation support not found.\n"); + return false; + } + + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + if(!(win = glfwCreateWindow(win_w, win_h, "vkcow", 0, 0))) { + fprintf(stderr, "Failed to create window.\n"); + return false; + } + + if(VkResult err = glfwCreateWindowSurface(inst, win, 0, &surface)) { + fprintf(stderr, "Failed to create KHR surface: %s\n", print_vulkan_error(err)); + return false; + } + return true; } void cleanup_vulkan() { + //TODOs according to the book: + // 1- make sure all threads have been terminated (when I add threads) + // 2- destroy objects in *reverse* order + + vkDestroySurfaceKHR(inst, surface, 0); + + if(vkDeviceWaitIdle(device) == VK_SUCCESS) { + vkDestroyDevice(device, 0); + vkDestroyInstance(inst, 0); + } +} + +static bool create_instance() +{ + /* enable layers */ + uint32_t layer_count = 0; + std::vector enabled_layers; + + if(vkEnumerateInstanceLayerProperties(&layer_count, 0) != VK_SUCCESS) { + fprintf(stderr, "Failed to query layer properties.\n"); + return false; + } + + if(layer_count > 0) { + VkLayerProperties *layers = (VkLayerProperties *)alloca(layer_count * sizeof *layers); + vkEnumerateInstanceLayerProperties(&layer_count, layers); + for(uint32_t i=0; i enabled_extensions; + + if(vkEnumerateInstanceExtensionProperties(0, &extensions_count, 0) != VK_SUCCESS) { + fprintf(stderr, "Failed to enumerate instance extension properties\n"); + return false; + } + + if(extensions_count > 0) { + VkExtensionProperties *extensions = (VkExtensionProperties *)alloca(extensions_count * sizeof *extensions); + vkEnumerateInstanceExtensionProperties(0, &extensions_count, extensions); + + for(uint32_t i=0; i 0) { + VkLayerProperties *layers = (VkLayerProperties*)alloca(layer_count * sizeof *layers); + vkEnumerateDeviceLayerProperties(pdev, &layer_count, layers); + printf("%u layers found.\n", layer_count); + for(uint32_t i=0; i +#include bool init_vulkan(); void cleanup_vulkan(); -GLFWwindow *create_vulkan_window(); -void clbk_clear_vk(); -#endif // VK_H_ +#endif // VK_H_ \ No newline at end of file -- 1.7.10.4