backup - needs fixing
[demo] / src / vulkan / vkutil.cc
index 698e6f8..25f3ad3 100644 (file)
@@ -1,3 +1,4 @@
+#include <gmath/gmath.h>
 #include <vulkan/vulkan.h>
 #include <stdio.h>
 #include <stdint.h>
 
 /* global variables */
 
-VkPipeline *vkgraphics_pipeline;
+VkPipeline *vkgparent_pipeline;
 VkFramebuffer *vkfbufs;
 VkRenderPass vkrpass;
-VkSwapchainKHR vkswapchain;
-VkImage *vkswapchain_images;
-VkImageView *vkswapchain_views;
-int vknum_swapchain_images;
-int vknext_swapchain_image;
 VkSurfaceKHR vksurface;
 VkInstance vkinst;
 VkPhysicalDevice vkpdev;
@@ -27,6 +23,14 @@ VkCommandPool vkcmdpool;
 VkCommandBuffer vkcmdbuf;      /* primary command buffer */
 int vkqfamily;
 
+VkSemaphore vk_img_avail_sema;
+VkSemaphore vk_rend_done_sema;
+VkSwapchainKHR vkswapchain;
+VkImage *vkswapchain_images;
+VkImageView *vkswapchain_views;
+int vknum_swapchain_images;
+int vk_curr_swapchain_image;
+
 /* static functions */
 static const char *get_device_name_str(VkPhysicalDeviceType type);
 static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags);
@@ -257,6 +261,10 @@ void vku_cleanup()
        if(vkinst) {
                vkDeviceWaitIdle(vkdev);
                vkDestroyCommandPool(vkdev, vkcmdpool, 0);
+
+               vkDestroySemaphore(vkdev, vk_img_avail_sema, 0);
+               vkDestroySemaphore(vkdev, vk_rend_done_sema, 0);
+
                vkDestroyDevice(vkdev, 0);
                vkDestroyInstance(vkinst, 0);
                vkinst = 0;
@@ -266,6 +274,25 @@ void vku_cleanup()
        phys_devices = 0;
 }
 
+bool vku_create_semaphores()
+{
+       VkSemaphoreCreateInfo sinf;
+       memset(&sinf, 0, sizeof sinf);
+
+       sinf.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
+       if(vkCreateSemaphore(vkdev, &sinf, 0, &vk_img_avail_sema) != VK_SUCCESS) {
+               fprintf(stderr, "Failed to create semaphore\n");
+               return false;
+       }
+
+       if(vkCreateSemaphore(vkdev, &sinf, 0, &vk_rend_done_sema) != VK_SUCCESS) {
+               fprintf(stderr, "Failed to create semaphore\n");
+               return false;
+       }
+
+       return true;
+}
+
 VkCommandBuffer vku_alloc_cmdbuf(VkCommandPool pool, VkCommandBufferLevel level)
 {
        VkCommandBuffer cmdbuf;
@@ -543,257 +570,6 @@ void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs,
        vkCmdCopyBuffer(cmdbuf, src, dest, 1, &copy);
 }
 
-/* paste
-
-static bool create_instance()
-{
-       uint32_t layer_count = 0;
-       std::vector<const char *> 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<layer_count; i++) {
-                       if(strcmp(layers[i].layerName, "VK_LAYER_LUNARG_standard_validation")) {
-                               enabled_layers.push_back(layers[i].layerName);
-                       }
-               }
-       }
-
-       uint32_t extensions_count = 0;
-       std::vector<const char *> 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<extensions_count; i++) {
-                       printf("Extension %u: %s %u.\n", i, extensions[i].extensionName, extensions[i].specVersion);
-                       //enable all the available extensions
-                       enabled_extensions.push_back(extensions[i].extensionName);
-                       enabled_extension_names.push_back(std::string(extensions[i].extensionName));
-               }
-       }
-
-       VkInstanceCreateInfo create_info;
-       memset(&create_info, 0, sizeof create_info);
-       create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
-
-       if(!enabled_layers.empty()) {
-               create_info.enabledLayerCount = enabled_layers.size();
-               create_info.ppEnabledLayerNames = enabled_layers.data();
-       }
-
-       if(!enabled_extensions.empty()) {
-               create_info.enabledExtensionCount = enabled_extensions.size();
-               create_info.ppEnabledExtensionNames = enabled_extensions.data();
-       }
-
-       if(vkCreateInstance(&create_info, 0, &inst) != VK_SUCCESS) {
-               fprintf(stderr, "Failed to create instance.\n");
-               return false;
-       }
-
-       if(!create_device())
-               return false;
-
-       return true;
-}
-
-static bool create_device()
-{
-       int qfam_idx = -1;
-       int pdev_idx = -1;
-
-       uint32_t dev_count;
-       if(vkEnumeratePhysicalDevices(inst, &dev_count, 0) != VK_SUCCESS) {
-               fprintf(stderr, "Failed to enumerate physical devices.\n");
-               return false;
-       }
-       printf("%u devices found.\n", (unsigned int)dev_count);
-
-       VkPhysicalDevice *phys_dev = (VkPhysicalDevice *)alloca(dev_count * sizeof *phys_dev);
-       vkEnumeratePhysicalDevices(inst, &dev_count, phys_dev);
-       VkPhysicalDeviceMemoryProperties memprop;
-
-       for(uint32_t i=0; i<dev_count; i++) {
-               VkPhysicalDeviceProperties dev_props;
-               vkGetPhysicalDeviceProperties(phys_dev[i], &dev_props);
-
-               //memory heaps:
-               vkGetPhysicalDeviceMemoryProperties(phys_dev[i], &memprop);
-               printf("\tNumber of heaps: %u\n", memprop.memoryHeapCount);
-               for(uint32_t j=0; j<memprop.memoryHeapCount; j++) {
-                       printf("\t\tHeap %u size: %lu\n", j, (unsigned long)memprop.memoryHeaps[j].size);
-                       printf("\t\tHeap %u flags: %s\n", j, heap_flags_str(memprop.memoryHeaps[j].flags));
-               }
-               //memory types
-               printf("\tMemory types: %u\n", memprop.memoryTypeCount);
-               for(uint32_t j=0; j<memprop.memoryTypeCount; j++) {
-                       printf("\t\tType %u heap index: %u\n", j, memprop.memoryTypes[j].heapIndex);
-                       printf("\t\tType %u flags: %s\n", j, memtype_flags_str(memprop.memoryTypes[j].propertyFlags));
-               }
-
-               //supported features
-               VkPhysicalDeviceFeatures features;
-               vkGetPhysicalDeviceFeatures(phys_dev[i], &features);
-
-               //queue families
-               uint32_t qfam_count;
-               vkGetPhysicalDeviceQueueFamilyProperties(phys_dev[i], &qfam_count, 0);
-               printf("\tQueue Families: %u\n", qfam_count);
-               VkQueueFamilyProperties *qfam_props = new VkQueueFamilyProperties[qfam_count];
-               vkGetPhysicalDeviceQueueFamilyProperties(phys_dev[i], &qfam_count, qfam_props);
-               for(uint32_t j=0; j<qfam_count; j++) {
-                       printf("\t\tFamily %u flags: %s\n", j, queue_flags_str(qfam_props[j].queueFlags));
-                       printf("\t\tFamily %u number of queues: %u\n", j, qfam_props[j].queueCount);
-
-                       if((qfam_props[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) && (pdev_idx == -1)) {
-                               pdev_idx = i;
-                               qfam_idx = j;
-                               num_queues = qfam_props[j].queueCount;
-                       }
-               }
-               delete [] qfam_props;
-       }
-
-       if(pdev_idx == -1) {
-               fprintf(stderr, "No suitable devices found.\n");
-               return false;
-       }
-
-       pdev = *(phys_dev + pdev_idx);
-       qfamily_idx = qfam_idx;
-
-               uint32_t layer_count;
-               if(vkEnumerateDeviceLayerProperties(pdev, &layer_count, 0) != VK_SUCCESS) {
-                       fprintf(stderr, "Failed to enumerate device layers.\n");
-                       return false;
-               }
-               if(layer_count > 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<layer_count; i++) {
-                               printf("Layer %u: %s (%u, %u)\n", i, layers[i].layerName,
-                                               layers[i].specVersion, layers[i].implementationVersion);
-                               printf("\tDesc: %s\n", layers[i].description);
-                       }
-               }
-
-       VkDeviceCreateInfo dev_info;
-       memset(&dev_info, 0, sizeof dev_info);
-       dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
-
-       VkDeviceQueueCreateInfo dev_qinfo;
-       memset(&dev_qinfo, 0, sizeof dev_qinfo);
-       dev_qinfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
-       dev_qinfo.queueFamilyIndex = qfam_idx;
-       dev_qinfo.queueCount = 1;
-
-       dev_info.queueCreateInfoCount = 1;
-       dev_info.pQueueCreateInfos = &dev_qinfo;
-
-       if(vkCreateDevice(pdev, &dev_info, 0, &device) != VK_SUCCESS) {
-               fprintf(stderr, "Failed to create logical device.\n");
-               return false;
-       }
-
-       vkGetPhysicalDeviceMemoryProperties(pdev, &memprop);
-       for(uint32_t j=0; j<memprop.memoryTypeCount; j++) {
-               if(memprop.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
-                       device_mem_idx = j;
-                       printf("Selected device memory index: %u\n", device_mem_idx);
-                       break;
-               }
-       }
-
-       return true;
-}
-
-static const char *dev_type_str(VkPhysicalDeviceType type)
-{
-       switch(type) {
-       case VK_PHYSICAL_DEVICE_TYPE_OTHER:
-               return "other";
-       case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
-               return "integrated GPU";
-       case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
-               return "discrete GPU";
-       case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
-               return "virtual GPU";
-       case VK_PHYSICAL_DEVICE_TYPE_CPU:
-               return "CPU";
-       default:
-               break;
-       }
-       return "unknown";
-}
-
-static const char *heap_flags_str(VkMemoryHeapFlags flags)
-{
-       static std::string str;
-       str.clear();
-       if(flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) {
-               str += "device-local ";
-       }
-       if(!str.empty())
-               str.pop_back();
-       return str.c_str();
-}
-
-static const char *memtype_flags_str(VkMemoryPropertyFlags flags)
-{
-       static std::string str;
-       str.clear();
-       if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
-               str += "device-local ";
-       }
-       if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
-               str += "host-visible ";
-       }
-       if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
-               str += "host-coherent ";
-       }
-       if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
-               str += "host-cached ";
-       }
-       if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
-               str += "lazily-allocated ";
-       }
-       if(!str.empty())
-               str.pop_back();
-       return str.c_str();
-}
-
-static const char *queue_flags_str(VkQueueFlags flags)
-{
-       static std::string str;
-       str.clear();
-       if(flags & VK_QUEUE_GRAPHICS_BIT)
-               str += "graphics ";
-       if(flags & VK_QUEUE_COMPUTE_BIT)
-               str += "compute ";
-       if(flags & VK_QUEUE_TRANSFER_BIT)
-               str += "transfer ";
-       if(flags & VK_QUEUE_SPARSE_BINDING_BIT)
-               str += "sparse-binding ";
-       if(!str.empty())
-               str.pop_back();
-       return str.c_str();
-}
-*/
-
 const char *vku_get_vulkan_error_str(VkResult error)
 {
        std::string errmsg;
@@ -839,7 +615,6 @@ const char *vku_get_vulkan_error_str(VkResult error)
                break;
        case VK_ERROR_FEATURE_NOT_PRESENT:
                errmsg = std::string("VK_ERROR_FEATURE_NOT_PRESENT");
-               break;
        case VK_ERROR_INCOMPATIBLE_DRIVER:
                errmsg = std::string("VK_ERROR_INCOMPATIBLE_DRIVER");
                break;
@@ -930,7 +705,17 @@ bool vku_create_graphics_pipeline(VkPipelineLayout *layout)
        msi.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
        msi.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
 
-       //TODO in progress
+       /* vertex input descriptions */
+       VkVertexInputBindingDescription vib;
+       memset(&vib, 0, sizeof vib);
+       vib.stride = sizeof(Vec3);
+       vib.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
+
+       /* input attr bindings */
+       VkVertexInputAttributeDescription via[2];
+       memset(&via, 0, sizeof via);
+       via[0].format = VK_FORMAT_R32G32B32A32_SFLOAT;
+
        return true;
 }