#include <string>
#include <vector>
+#include "allocator.h"
#include "vkutil.h"
/* global variables */
-
-VkPipeline *vkgparent_pipeline;
-VkFramebuffer *vkfbufs;
-VkRenderPass vkrpass;
-VkSurfaceKHR vksurface;
-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;
-VkSwapchainKHR vkswapchain;
-VkImage *vkswapchain_images;
-VkImageView *vkswapchain_views;
-int vknum_swapchain_images;
-int vk_curr_swapchain_image;
+VkPhysicalDevice vk_physical;
+VkDevice vk_device;
+VkInstance vk_instance;
+VkQueue vk_queue;
+int vk_qfamily;
+VkCommandPool vk_pool;
+VkSurfaceKHR vk_surface;
+VkSwapchainKHR vk_swapchain;
+VkRenderPass vk_renderpass;
+VkDescriptorPool vk_dpool;
/* static functions */
static const char *get_device_name_str(VkPhysicalDeviceType type);
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;
}
if(sel_dev < 0 || sel_qfamily < 0) {
fprintf(stderr, "failed to find any device with a graphics-capable command queue\n");
- vkDestroyDevice(vkdev, 0);
+ vkDestroyDevice(vk_device, 0);
return false;
}
dev_info.enabledExtensionCount = sizeof devext_names / sizeof *devext_names;
dev_info.ppEnabledExtensionNames = devext_names;
- if(vkCreateDevice(phys_devices[sel_dev], &dev_info, 0, &vkdev) != 0) {
+ if(vkCreateDevice(phys_devices[sel_dev], &dev_info, 0, &vk_device) != 0) {
fprintf(stderr, "failed to create device %d\n", sel_dev);
return false;
}
printf("created device %d\n", sel_dev);
- vkpdev = phys_devices[sel_dev];
- vkqfamily = sel_qfamily;
+ vk_physical = phys_devices[sel_dev];
+ vk_qfamily = sel_qfamily;
- vkGetDeviceQueue(vkdev, sel_qfamily, 0, &vkq);
+ vkGetDeviceQueue(vk_device, sel_qfamily, 0, &vk_queue);
/* create command buffer pool */
memset(&cmdpool_info, 0, sizeof cmdpool_info);
cmdpool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
cmdpool_info.queueFamilyIndex = sel_qfamily;
- if(vkCreateCommandPool(vkdev, &cmdpool_info, 0, &vkcmdpool) != 0) {
- fprintf(stderr, "failed to get command quque!\n");
+ if(vkCreateCommandPool(vk_device, &cmdpool_info, 0, &vk_pool) != 0) {
+ fprintf(stderr, "failed to get command queue!\n");
return false;
}
- if(!(vkcmdbuf = vku_alloc_cmdbuf(vkcmdpool, 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) {
- vkDeviceWaitIdle(vkdev);
- vkDestroyCommandPool(vkdev, vkcmdpool, 0);
-
- vkDestroySemaphore(vkdev, vk_img_avail_sema, 0);
- vkDestroySemaphore(vkdev, vk_rend_done_sema, 0);
+ if(vk_instance) {
+ vkDeviceWaitIdle(vk_device);
+ vkDestroyCommandPool(vk_device, vk_pool, 0);
- vkDestroyDevice(vkdev, 0);
- vkDestroyInstance(vkinst, 0);
- vkinst = 0;
+ vkDestroyDevice(vk_device, 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(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;
inf.level = level;
inf.commandBufferCount = 1;
- if(vkAllocateCommandBuffers(vkdev, &inf, &cmdbuf) != 0) {
+ if(vkAllocateCommandBuffers(vk_device, &inf, &cmdbuf) != 0) {
return 0;
}
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(vkdev, pool, 1, &buf);
+ vkFreeCommandBuffers(vk_device, pool, 1, &buf);
}
void vku_begin_cmdbuf(VkCommandBuffer buf, unsigned int flags)
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)
{
- VkSwapchainKHR sc;
- VkSwapchainCreateInfoKHR inf;
+ uint32_t fcount;
- memset(&inf, 0, sizeof inf);
- inf.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
- inf.surface = surf;
- inf.minImageCount = n;
- inf.imageFormat = VK_FORMAT_B8G8R8A8_UNORM; /* TODO enumerate and choose */
- inf.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
- inf.imageExtent.width = xsz;
- inf.imageExtent.height = ysz;
- 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 = prev;
-
- if(vkCreateSwapchainKHR(vkdev, &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(vkdev, sc, &nimg, 0) != 0) {
- return 0;
+ if(fcount == 0) {
+ fprintf(stderr, "No color formats were found.\n");
+ return false;
}
- images = new VkImage[nimg];
- vkGetSwapchainImagesKHR(vkdev, 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; i<count; i++) {
- VkImageViewCreateInfo inf;
- memset(&inf, 0, sizeof inf);
+ VkSurfaceFormatKHR *formats = new VkSurfaceFormatKHR[fcount];
+ if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical, vk_surface,
+ &fcount, formats) != VK_SUCCESS) {
+ delete [] formats;
+ fprintf(stderr, "Failed to get surface formats.\n");
+ return false;
+ }
- inf.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
- inf.image = images[i];
- inf.viewType = VK_IMAGE_VIEW_TYPE_2D;
- inf.format = VK_FORMAT_B8G8R8A8_UNORM; //TODO
- inf.components.r = inf.components.g = inf.components.b =
- inf.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
- inf.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
- inf.subresourceRange.levelCount = 1;
- inf.subresourceRange.layerCount = 1;
+ *sformat = formats[0];
- if(vkCreateImageView(vkdev, &inf, 0, iviews) != 0) {
- fprintf(stderr, "Failed to create image views.\n");
- delete iviews;
- return 0;
- }
+ if((fcount == 1) && (formats[0].format == VK_FORMAT_UNDEFINED)) {
+ sformat->format = VK_FORMAT_B8G8R8_UNORM;
}
- return iviews;
+ delete [] formats;
+ return true;
}
+
int vku_get_next_image(VkSwapchainKHR sc)
{
uint32_t next;
- if(vkAcquireNextImageKHR(vkdev, sc, UINT64_MAX, 0, 0, &next) != 0) {
+ if(vkAcquireNextImageKHR(vk_device, sc, UINT64_MAX, 0, 0, &next) != 0) {
return -1;
}
return (int)next;
inf.pImageIndices = &index;
inf.pResults = &res;
- vkQueuePresentKHR(vkq, &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(vkdev, &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; i<count; i++) {
- if(vkCreateFramebuffer(vkdev, &inf, 0, &vkfbufs[i]) != VK_SUCCESS) {
- fprintf(stderr, "Failed to create framebuffer for image view: %d\n", i);
- delete vkfbufs;
- return false;
- }
- }
- return true;
+ vkQueuePresentKHR(vk_queue, &inf);
}
struct vku_buffer *vku_create_buffer(int sz, unsigned int usage)
binfo.usage = usage;
binfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
- if(vkCreateBuffer(vkdev, &binfo, 0, &buf->buf) != 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;
}
- // TODO back with memory
+
+ VkMemoryRequirements mr;
+ vkGetBufferMemoryRequirements(vk_device, buf->buf, &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(vkdev, buf->buf, 0);
+ //TODO change when the allocator changes
+ vku_free(buf->mem_pool);
+
+ 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,
VkBuffer src, int soffs, int size)
{
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<VkDynamicState> 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<uint32_t>(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)
{
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<VkDescriptorPoolSize> sizes;
+ for(int i=0; i<num_desc; i++) {
+ VkDescriptorPoolSize psz;
+ psz.type = descriptors[i]->type;
+ 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<VkDescriptorSetLayout> layouts;
+ for(int i=0; i<num_desc; i++) {
+ layouts.push_back(desc[i]->layout);
+ }
+
+ 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;
+}
+
+VkPipelineCache vku_create_pipeline_cache()
+{
+ VkPipelineCacheCreateInfo cinf;
+ memset(&cinf, 0, sizeof cinf);
+ cinf.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
+
+ VkPipelineCache cache;
+ if(vkCreatePipelineCache(vk_device, &cinf, 0, &cache) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create pipeline cache.\n");
+ return 0;
+ }
+ return cache;
+}
+
+void vku_destroy_pipeline_cache(VkPipelineCache cache)
+{
+ vkDestroyPipelineCache(vk_device, cache, 0);
+}
+
+void vku_pl_init_shader_stage_state_info(VkPipelineShaderStageCreateInfo *ssinf,
+ VkShaderStageFlagBits stage, VkShaderModule sm)
+{
+ memset(ssinf, 0, sizeof *ssinf);
+ ssinf->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
+ ssinf->stage = stage;
+ ssinf->module = sm;
+ ssinf->pName = "main";
+}
+
+void vku_pl_init_input_asm_state_info(VkPipelineInputAssemblyStateCreateInfo *iasinf)
+{
+ memset(iasinf, 0, sizeof *iasinf);
+ iasinf->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
+ iasinf->topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
+}
+
+void vku_pl_init_viewport_state_info(VkPipelineViewportStateCreateInfo *vsinf,
+ VkViewport *viewports, int num_viewports, VkRect2D *scissors,
+ int num_scissors)
+{
+ memset(vsinf, 0, sizeof *vsinf);
+ vsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
+ vsinf->viewportCount = num_viewports;
+ vsinf->scissorCount = num_scissors;
+ vsinf->pViewports = viewports;
+ vsinf->pScissors = scissors;
+}
+
+void vku_pl_init_rasterization_state_info(VkPipelineRasterizationStateCreateInfo *rsinf)
+{
+ memset(rsinf, 0, sizeof *rsinf);
+ rsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
+ rsinf->polygonMode = VK_POLYGON_MODE_FILL;
+ rsinf->cullMode = VK_CULL_MODE_FRONT_BIT;
+ rsinf->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
+}
+
+void vku_pl_init_multisample_state_info(VkPipelineMultisampleStateCreateInfo *msinf)
+{
+ memset(msinf, 0, sizeof *msinf);
+ msinf->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
+ msinf->rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
+}
+
+void vku_pl_init_depth_stencil_state_info(VkPipelineDepthStencilStateCreateInfo *dsinf,
+ bool enable)
+{
+ memset(dsinf, 0, sizeof *dsinf);
+ dsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
+ dsinf->depthTestEnable = enable ? VK_TRUE : VK_FALSE;
+}
+
+void vku_pl_init_color_blend_state_info(VkPipelineColorBlendStateCreateInfo *cbsinf,
+ bool enable)
+{
+ memset(cbsinf, 0, sizeof *cbsinf);
+ cbsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
+ //TODO
+}