X-Git-Url: https://eleni.mutantstargoat.com/git/?p=demo;a=blobdiff_plain;f=src%2Fvulkan%2Fvkutil.cc;h=65467c563a014ecb24a56ca8028fd71db3c7efaa;hp=051ee4dc7174b101c6301ba2924046ec7a55f34e;hb=8fb0cca684e078cd2537070c53ad970ebbc2e9a7;hpb=9148ff7614b1dc22fd5d1dcf6d74e2fb4ff13706 diff --git a/src/vulkan/vkutil.cc b/src/vulkan/vkutil.cc index 051ee4d..65467c5 100644 --- a/src/vulkan/vkutil.cc +++ b/src/vulkan/vkutil.cc @@ -1,3 +1,4 @@ +#include #include #include #include @@ -9,19 +10,27 @@ #include "vkutil.h" /* global variables */ - -VkSwapchainKHR vkswapchain; -VkImage *vkswapchain_images; -int vknext_swapchain_image; -VkSurfaceKHR vksurface; +VkPhysicalDevice vk_physical; +VkDevice vk_device; +VkCommandPool vk_pool; +VkQueue vk_queue; +VkSurfaceKHR vk_surface; +VkSwapchainKHR vk_swapchain; + +VkPipeline *vkgparent_pipeline; +VkFramebuffer *vkfbufs; +VkRenderPass vkrpass; VkInstance vkinst; -VkPhysicalDevice vkpdev; -VkDevice vkdev; -VkQueue vkq; -VkCommandPool vkcmdpool; VkCommandBuffer vkcmdbuf; /* primary command buffer */ int vkqfamily; +VkSemaphore vk_img_avail_sema; +VkSemaphore vk_rend_done_sema; +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); @@ -46,10 +55,7 @@ bool vku_have_extension(const char *name) vkext_count = 0; vkEnumerateInstanceExtensionProperties(0, &vkext_count, 0); if(vkext_count) { - if(!(vkext = (VkExtensionProperties *)malloc(vkext_count * sizeof *vkext))) { - perror("failed to allocate instance extension list"); - return false; - } + vkext = new VkExtensionProperties[vkext_count]; vkEnumerateInstanceExtensionProperties(0, &vkext_count, vkext); printf("instance extensions:\n"); @@ -75,10 +81,7 @@ bool vku_have_device_extension(const char *name) vkdevext_count = 0; vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, 0); if(vkdevext_count) { - if(!(vkdevext = (VkExtensionProperties *)malloc(vkdevext_count * sizeof *vkdevext))) { - perror("failed to allocate device extension list"); - return false; - } + vkdevext = new VkExtensionProperties[vkdevext_count]; vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, vkdevext); printf("selected device extensions:\n"); @@ -137,7 +140,7 @@ bool vku_create_device() fprintf(stderr, "failed to enumerate vulkan physical devices\n"); return false; } - phys_devices = (VkPhysicalDevice *)malloc(num_devices * sizeof *phys_devices); + phys_devices = new VkPhysicalDevice[num_devices]; if(vkEnumeratePhysicalDevices(vkinst, &num_devices, phys_devices) != 0) { fprintf(stderr, "failed to enumerate vulkan physical devices\n"); return false; @@ -179,7 +182,7 @@ bool vku_create_device() if(qprop_count <= 0) { continue; } - qprop = (VkQueueFamilyProperties *)malloc(qprop_count * sizeof *qprop); + qprop = new VkQueueFamilyProperties[qprop_count]; vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, qprop); for(unsigned int j=0; jbuf) != 0) { + if(vkCreateBuffer(vk_device, &binfo, 0, &buf->buf) != 0) { fprintf(stderr, "failed to create %d byte buffer (usage: %x)\n", sz, usage); return 0; } @@ -421,8 +577,8 @@ struct vku_buffer *vku_create_buffer(int sz, unsigned int usage) void vku_destroy_buffer(struct vku_buffer *buf) { if(buf) { - vkDestroyBuffer(vkdev, buf->buf, 0); - free(buf); + vkDestroyBuffer(vk_device, buf->buf, 0); + delete buf; } } @@ -437,257 +593,6 @@ void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs, vkCmdCopyBuffer(cmdbuf, src, dest, 1, ©); } -/* paste - -static bool create_instance() -{ - 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 ds_enabled; +// ds_enabled.push_back(VK_DYNAMIC_STATE_VIEWPORT); + //ds_enabled.push_back(VK_DYNAMIC_STATE_SCISSOR); +// VkPipelineDynamicStateCreateInfo ds; +// ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; +// ds.pDynamicStates = ds_enabled.data(); +// ds.dynamicStateCount = static_cast(ds_enabled.size()); + + /* depth tests */ + VkPipelineDepthStencilStateCreateInfo dsi; + memset(&dsi, 0, sizeof dsi); + dsi.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; + dsi.depthTestEnable = VK_TRUE; + dsi.depthWriteEnable = VK_TRUE; + dsi.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; + dsi.back.failOp = VK_STENCIL_OP_KEEP; + dsi.back.passOp = VK_STENCIL_OP_KEEP; + dsi.back.compareOp = VK_COMPARE_OP_ALWAYS; + dsi.front = dsi.back; + + /* multisampling - must be set even if not used */ + VkPipelineMultisampleStateCreateInfo msi; + memset(&msi, 0, sizeof msi); + msi.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; + msi.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; + + /* 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; +} + static const char *get_device_name_str(VkPhysicalDeviceType type) { switch(type) {