X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fvulkan%2Fvkutil.cc;h=04a9fe560170b76da7f52865c85ab3c3a45f2bcc;hb=f6ceb163227d00d7f97df1fc2dfbdd419c56277e;hp=65467c563a014ecb24a56ca8028fd71db3c7efaa;hpb=8fb0cca684e078cd2537070c53ad970ebbc2e9a7;p=demo diff --git a/src/vulkan/vkutil.cc b/src/vulkan/vkutil.cc index 65467c5..04a9fe5 100644 --- a/src/vulkan/vkutil.cc +++ b/src/vulkan/vkutil.cc @@ -7,29 +7,20 @@ #include #include +#include "allocator.h" #include "vkutil.h" /* global variables */ VkPhysicalDevice vk_physical; VkDevice vk_device; -VkCommandPool vk_pool; +VkInstance vk_instance; VkQueue vk_queue; +int vk_qfamily; +VkCommandPool vk_pool; VkSurfaceKHR vk_surface; VkSwapchainKHR vk_swapchain; - -VkPipeline *vkgparent_pipeline; -VkFramebuffer *vkfbufs; -VkRenderPass vkrpass; -VkInstance vkinst; -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; +VkRenderPass vk_renderpass; +VkDescriptorPool vk_dpool; /* static functions */ static const char *get_device_name_str(VkPhysicalDeviceType type); @@ -131,17 +122,17 @@ bool vku_create_device() inst_info.ppEnabledExtensionNames = ext_names; inst_info.enabledExtensionCount = sizeof ext_names / sizeof *ext_names; - if(vkCreateInstance(&inst_info, 0, &vkinst) != 0) { + if(vkCreateInstance(&inst_info, 0, &vk_instance) != 0) { fprintf(stderr, "failed to create vulkan instance\n"); return false; } printf("created vulkan instance\n"); - if(vkEnumeratePhysicalDevices(vkinst, &num_devices, 0) != 0) { + if(vkEnumeratePhysicalDevices(vk_instance, &num_devices, 0) != 0) { fprintf(stderr, "failed to enumerate vulkan physical devices\n"); return false; } phys_devices = new VkPhysicalDevice[num_devices]; - if(vkEnumeratePhysicalDevices(vkinst, &num_devices, phys_devices) != 0) { + if(vkEnumeratePhysicalDevices(vk_instance, &num_devices, phys_devices) != 0) { fprintf(stderr, "failed to enumerate vulkan physical devices\n"); return false; } @@ -233,7 +224,7 @@ bool vku_create_device() printf("created device %d\n", sel_dev); vk_physical = phys_devices[sel_dev]; - vkqfamily = sel_qfamily; + vk_qfamily = sel_qfamily; vkGetDeviceQueue(vk_device, sel_qfamily, 0, &vk_queue); @@ -248,51 +239,29 @@ bool vku_create_device() return false; } -/* if(!(vkcmdbuf = vku_alloc_cmdbuf(vk_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) { - fprintf(stderr, "failed to create primary command buffer\n"); - return false; - } */ + /* if(!(vkcmdbuf = vku_alloc_cmdbuf(vk_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) { + fprintf(stderr, "failed to create primary command buffer\n"); + return false; + } */ return true; } void vku_cleanup() { - if(vkinst) { + if(vk_instance) { vkDeviceWaitIdle(vk_device); vkDestroyCommandPool(vk_device, vk_pool, 0); - vkDestroySemaphore(vk_device, vk_img_avail_sema, 0); - vkDestroySemaphore(vk_device, vk_rend_done_sema, 0); - vkDestroyDevice(vk_device, 0); - vkDestroyInstance(vkinst, 0); - vkinst = 0; + vkDestroyInstance(vk_instance, 0); + vk_instance = 0; } delete [] phys_devices; phys_devices = 0; } -bool vku_create_semaphores() -{ - VkSemaphoreCreateInfo sinf; - memset(&sinf, 0, sizeof sinf); - - sinf.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; - if(vkCreateSemaphore(vk_device, &sinf, 0, &vk_img_avail_sema) != VK_SUCCESS) { - fprintf(stderr, "Failed to create semaphore\n"); - return false; - } - - if(vkCreateSemaphore(vk_device, &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; @@ -310,6 +279,24 @@ VkCommandBuffer vku_alloc_cmdbuf(VkCommandPool pool, VkCommandBufferLevel level) return cmdbuf; } +bool vku_alloc_cmdbufs(VkCommandPool pool, VkCommandBufferLevel level, unsigned int count, VkCommandBuffer *buf_array) +{ + VkCommandBufferAllocateInfo cinf; + memset(&cinf, 0, sizeof cinf); + + cinf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; + cinf.commandPool = vk_pool; + cinf.level = level; + cinf.commandBufferCount = count; + + if(vkAllocateCommandBuffers(vk_device, &cinf, buf_array) != VK_SUCCESS) { + fprintf(stderr, "Failed to allocate command buffer\n"); + return false; + } + + return true; +} + void vku_free_cmdbuf(VkCommandPool pool, VkCommandBuffer buf) { vkFreeCommandBuffers(vk_device, pool, 1, &buf); @@ -349,101 +336,39 @@ void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence) vkQueueSubmit(q, 1, &info, done_fence); } -VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n, - VkPresentModeKHR pmode, VkSwapchainKHR prev) +bool vku_get_surface_format(VkPhysicalDevice gpu, VkSurfaceKHR surface, VkSurfaceFormatKHR *sformat) { - VkSurfaceCapabilitiesKHR scap; - vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vk_physical, surf, &scap); - - VkPhysicalDeviceMemoryProperties mprops; - vkGetPhysicalDeviceMemoryProperties(vk_physical, &mprops); - - uint32_t pmcnt; - vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical, surf, &pmcnt, 0); - VkPresentModeKHR scpm = VK_PRESENT_MODE_IMMEDIATE_KHR; - VkExtent2D scext; - scext.width = xsz; - scext.height = ysz; - -// VkSurfaceTransformFlagBitsKHR pretransf; -// if(scap.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) { -// pretransf = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; -// } else { -// pretransf = scap.currentTransform; -// } + uint32_t fcount; - VkSwapchainKHR sc; - VkSwapchainCreateInfoKHR inf; - - VkSurfaceFormatKHR sformat; - sformat.format = VK_FORMAT_B8G8R8A8_UNORM; - - memset(&inf, 0, sizeof inf); - inf.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; - inf.surface = surf; - inf.minImageCount = n; - inf.imageFormat = sformat.format; /* TODO enumerate and choose */ - inf.imageColorSpace = sformat.colorSpace; - inf.imageExtent = scext; - inf.imageArrayLayers = 1; - inf.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - inf.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; /* XXX make this an option? */ - inf.preTransform = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR; - inf.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; - inf.presentMode = pmode; - inf.oldSwapchain = VK_NULL_HANDLE; - inf.clipped = VK_TRUE; - - if(vkCreateSwapchainKHR(vk_device, &inf, 0, &sc) != 0) { - return 0; + if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical, + vk_surface, &fcount, 0) != VK_SUCCESS) { + fprintf(stderr, "Failed to get format count for physical device.\n"); + return false; } - return sc; -} - -VkImage *vku_get_swapchain_images(VkSwapchainKHR sc, int *count) -{ - uint32_t nimg; - VkImage *images; - - if(vkGetSwapchainImagesKHR(vk_device, sc, &nimg, 0) != 0) { - return 0; + if(fcount == 0) { + fprintf(stderr, "No color formats were found.\n"); + return false; } - images = new VkImage[nimg]; - vkGetSwapchainImagesKHR(vk_device, sc, &nimg, images); - - if(count) *count = (int)nimg; - return images; -} -VkImageView *vku_create_image_views(VkImage *images, int count) -{ - VkImageView *iviews; - - iviews = new VkImageView[count]; - for(int i=0; iformat = VK_FORMAT_B8G8R8_UNORM; } - return iviews; + delete [] formats; + return true; } + int vku_get_next_image(VkSwapchainKHR sc) { uint32_t next; @@ -470,89 +395,6 @@ void vku_present(VkSwapchainKHR sc, int img_idx) vkQueuePresentKHR(vk_queue, &inf); } -bool vku_create_renderpass() -{ - VkAttachmentDescription attachments[2]; - memset(&attachments, 0, 2 * sizeof *attachments); - - /* color */ - attachments[0].format = VK_FORMAT_B8G8R8A8_UNORM; - attachments[0].samples = VK_SAMPLE_COUNT_1_BIT; - attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE; - attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachments[0].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; - attachments[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; - - /* depth */ - attachments[1].format = VK_FORMAT_D32_SFLOAT_S8_UINT; //TODO - attachments[1].samples = VK_SAMPLE_COUNT_1_BIT; - attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachments[1].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - attachments[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - - VkAttachmentReference color_ref; - color_ref.attachment = 0; - color_ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; - - VkAttachmentReference depth_ref; - depth_ref.attachment = 1; - depth_ref.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - - VkSubpassDescription subpass_desc; - memset(&subpass_desc, 0, sizeof subpass_desc); - - subpass_desc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; - subpass_desc.colorAttachmentCount = 1; - subpass_desc.pColorAttachments = &color_ref; - subpass_desc.pDepthStencilAttachment = &depth_ref; - - VkRenderPassCreateInfo inf; - memset(&inf, 0, sizeof inf); - - inf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; - inf.attachmentCount = 2; - inf.pAttachments = attachments; - inf.subpassCount = 1; - inf.pSubpasses = &subpass_desc; - - if(vkCreateRenderPass(vk_device, &inf, 0, &vkrpass) != VK_SUCCESS) { - return false; - } - - return true; -} - -bool vku_create_framebuffers(VkImageView *image_views, int count, int w, int h) -{ - delete vkfbufs; - vkfbufs = new VkFramebuffer[count]; - - VkFramebufferCreateInfo inf; - memset(&inf, 0, sizeof inf); - - inf.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; - inf.renderPass = vkrpass; - inf.attachmentCount = count; - inf.pAttachments = image_views; - inf.width = w; - inf.height = h; - inf.layers = 1; - - for(int i=0; ibuf, &mr); + + DevMemBlock block; + if(!vku_allocate(mr.size, &block)) + return 0; + + buf->mem_pool = block.dev_mem; + return buf; } void vku_destroy_buffer(struct vku_buffer *buf) { - if(buf) { - vkDestroyBuffer(vk_device, buf->buf, 0); - delete buf; +} + +bool vku_update_buffer(vku_buffer *buf, int size, void *data) +{ + uint8_t *pdata; + if(vkMapMemory(vk_device, buf->mem_pool, 0, size, 0, (void **)&pdata) != VK_SUCCESS) { + fprintf(stderr, "Failed to map memory.\n"); + return false; } + memcpy(pdata, data, size); + vkUnmapMemory(vk_device, buf->mem_pool); + + return true; } void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs, @@ -658,89 +518,6 @@ const char *vku_get_vulkan_error_str(VkResult error) return errmsg.c_str(); } -bool vku_create_graphics_pipeline(VkPipelineLayout *layout) -{ - VkGraphicsPipelineCreateInfo inf; - memset(&inf, 0, sizeof inf); - - inf.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; - inf.layout = *layout; - inf.renderPass = vkrpass; - - /* states */ - - /* how primitives are assembled */ - VkPipelineInputAssemblyStateCreateInfo ias; - memset(&ias, 0, sizeof ias); - ias.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; - ias.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; - - /* rasterisation */ - VkPipelineRasterizationStateCreateInfo rsi; - memset(&rsi, 0, sizeof rsi); - rsi.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; - rsi.polygonMode = VK_POLYGON_MODE_FILL; - rsi.cullMode = VK_CULL_MODE_BACK_BIT; - rsi.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; - rsi.lineWidth = 1.0f; - - /* blend factors */ - VkPipelineColorBlendAttachmentState bas[1]; - memset(&bas[0], 0, sizeof bas[0]); - - VkPipelineColorBlendStateCreateInfo cbs; - memset(&cbs, 0, sizeof cbs); - cbs.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; - cbs.attachmentCount = 1; - cbs.pAttachments = bas; - - /* number of viewport and scissors in this pipeline */ - VkPipelineViewportStateCreateInfo vs; - memset(&vs, 0, sizeof vs); - vs.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; - vs.viewportCount = 1; - vs.scissorCount = 1; - - /* dynamic states: that can be changed later */ -// std::vector 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) { @@ -838,3 +615,112 @@ static const char *get_mem_size_str(long sz) sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]); return str; } + +vku_descriptor *vku_create_descriptor(VkDescriptorType type, + VkFlags stage, int binding_point, + int size, int num_desc) +{ + vku_descriptor *desc = new vku_descriptor; + + desc->type = type; + desc->size = size; + desc->stage_flags = stage; + desc->binding_point = binding_point; + + VkDescriptorSetLayoutBinding dslb; + memset(&dslb, 0, sizeof dslb); + + dslb.binding = binding_point; + dslb.descriptorType = type; + dslb.descriptorCount = num_desc; + dslb.stageFlags = stage; + + VkDescriptorSetLayoutCreateInfo dslinf; + memset(&dslinf, 0, sizeof dslinf); + + dslinf.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + dslinf.bindingCount = 1; + dslinf.pBindings = &dslb; + + if(vkCreateDescriptorSetLayout(vk_device, &dslinf, 0, &desc->layout) != VK_SUCCESS) { + fprintf(stderr, "Failed to create vku_descriptor.\n"); + return 0; + } + + return desc; +} + +void vku_destroy_descriptor(vku_descriptor *desc) +{ + vkDestroyDescriptorSetLayout(vk_device, desc->layout, 0); + delete desc; +} + +bool vku_create_descriptor_pool(vku_descriptor **descriptors, int num_desc) +{ + if(vk_dpool) + vkDestroyDescriptorPool(vk_device, vk_dpool, 0); + + VkDescriptorPoolCreateInfo dpinf; + memset(&dpinf, 0, sizeof dpinf); + dpinf.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + dpinf.maxSets = 1; + + dpinf.poolSizeCount = num_desc; + + std::vector sizes; + for(int i=0; itype; + psz.descriptorCount = 1; + sizes.push_back(psz); + } + + dpinf.pPoolSizes = sizes.data(); + + if(vkCreateDescriptorPool(vk_device, &dpinf, 0, &vk_dpool) != VK_SUCCESS) { + fprintf(stderr, "Failed to create descriptor pool.\n"); + return false; + } + + return true; +} + +void vku_destroy_descriptor_pool() +{ + vkDestroyDescriptorPool(vk_device, vk_dpool, 0); +} + +bool vku_allocate_descriptor_sets(vku_descriptor **desc, int num_desc, VkDescriptorSet set) +{ + VkDescriptorSetAllocateInfo dainf; + memset(&dainf, 0, sizeof dainf); + + dainf.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + dainf.descriptorPool = vk_dpool; + dainf.descriptorSetCount = num_desc; + + std::vector layouts; + for(int i=0; ilayout); + } + + dainf.pSetLayouts = layouts.data(); + + if(vkAllocateDescriptorSets(vk_device, &dainf, &set) != VK_SUCCESS) { + fprintf(stderr, "Failed to allocate descriptor sets.\n"); + return false; + } + return true; +} + +void vku_free_descriptor_sets(VkDescriptorSet *sets, int num) +{ + vkFreeDescriptorSets(vk_device, vk_dpool, num, sets); +} + +bool vku_update_descriptor_sets(VkDescriptorSet *sets, int num_sets) +{ +// std::vector + return true; +}