#include <string.h>
#include <stdint.h>
#include <stdarg.h>
+#include <errno.h>
+#include <assert.h>
#include <vulkan/vulkan.h>
#include "vk.h"
#include "util.h"
static int choose_phys_dev(void);
static int create_device(void);
static int create_swapchain(void);
+static int create_default_cmdbuf(void);
static int choose_pixfmt(void);
static int eval_pdev_score(VkPhysicalDevice dev);
static VkImage *vksc_img;
static VkExtent2D vksc_extent;
static VkImageView *vksc_view;
+static VkCommandBuffer *vksc_cmdbuf; /* default command buffers (vksc_numimg) */
static VkLayerProperties *inst_layers;
static VkExtensionProperties *inst_ext, *dev_ext;
if(create_swapchain() == -1) return -1;
+ if(!vksc_cmdbuf) {
+ if(create_default_cmdbuf() == -1) return -1;
+ }
+
/* TODO create depth/stencil buffers as needed (initflags) */
return 0;
}
+int vk_num_swap_images(void)
+{
+ return vksc_numimg;
+}
+
+VkImageView vk_swap_image(int idx)
+{
+ return vksc_view[idx];
+}
+
+int vk_next_swap_image(VkSemaphore sem)
+{
+ uint32_t idx;
+ if(vkAcquireNextImageKHR(vkdev, vksc, UINT64_MAX, sem, 0, &idx) != 0) {
+ return -1;
+ }
+ return (int)idx;
+}
+
+int vk_submit(VkQueue q, VkCommandBuffer cmdbuf, VkSemaphore semwait, VkSemaphore semsig)
+{
+ /* TODO: investigate if we need to expose the wait stage */
+ VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
+ VkSubmitInfo sinf = {0};
+ sinf.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
+ sinf.waitSemaphoreCount = semwait ? 1 : 0;
+ sinf.pWaitSemaphores = &semwait;
+ sinf.pWaitDstStageMask = &wait_stage;
+ sinf.commandBufferCount = 1;
+ sinf.pCommandBuffers = &cmdbuf;
+ sinf.signalSemaphoreCount = semsig ? 1 : 0;
+ sinf.pSignalSemaphores = &semsig;
+
+ if(vkQueueSubmit(q, 1, &sinf, 0) != 0) {
+ fprintf(stderr, "failed to submit command buffer\n");
+ return -1;
+ }
+ return 0;
+}
+
+int vk_present(VkQueue q, int imgid, VkSemaphore semwait)
+{
+ VkPresentInfoKHR pinf = {0};
+
+ pinf.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
+ pinf.waitSemaphoreCount = semwait ? 1 : 0;
+ pinf.pWaitSemaphores = &semwait;
+ pinf.swapchainCount = 1;
+ pinf.pSwapchains = &vksc;
+ pinf.pImageIndices = (unsigned int*)&imgid;
+
+ if(vkQueuePresentKHR(q, &pinf) != 0) {
+ fprintf(stderr, "present failed\n");
+ return -1;
+ }
+ return 0;
+}
+
int vk_find_qfamily(unsigned int flags)
{
int i, famidx = -1;
return vk_create_cmdbuf_fam(qfam, level);
}
+VkCommandBuffer vk_get_cmdbuf(int imgid)
+{
+ if(imgid < 0 || imgid >= vksc_numimg) {
+ fprintf(stderr, "vk_get_cmdbuf: invalid id %d, swap chain has %d images\n",
+ imgid, vksc_numimg);
+ return 0;
+ }
+ return vksc_cmdbuf[imgid];
+}
+
int vk_create_rpass(void)
{
int i;
att[i].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
att[i].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
}
+ /*
att[zidx].format = r->zfmt;
att[zidx].samples = 1;
att[zidx].loadOp = r->clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
att[zidx].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
att[zidx].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
att[zidx].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
+ */
for(i=0; i<r->num_colbuf; i++) {
catref[i].attachment = i;
catref[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
}
+ /*
zatref.attachment = zidx;
zatref.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
+ */
memset(&subpass, 0, sizeof subpass);
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = r->num_colbuf;
subpass.pColorAttachments = catref;
- subpass.pDepthStencilAttachment = &zatref;
+ subpass.pDepthStencilAttachment = 0;//&zatref;
memset(&pinf, 0, sizeof pinf);
pinf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
- pinf.attachmentCount = r->num_colbuf + 1;
+ pinf.attachmentCount = r->num_colbuf;// + 1;
pinf.pAttachments = att;
pinf.subpassCount = 1;
pinf.pSubpasses = &subpass;
if(!framebufs) {
framebufs = darr_alloc(0, sizeof *framebufs);
- darr_push(framebufs, &framebuf); /* add dummy rpass */
+ darr_push(framebufs, &framebuf); /* add dummy framebuffer */
}
for(i=1; i<darr_size(framebufs); i++) {
void vk_fb_size(int fb, int x, int y)
{
- framebufs[fb].width = x;
- framebufs[fb].height = y;
- framebufs[fb].vkobj_valid = 0;
+ if(x != framebufs[fb].width || y != framebufs[fb].height) {
+ framebufs[fb].width = x;
+ framebufs[fb].height = y;
+ framebufs[fb].vkobj_valid = 0;
+ }
}
void vk_fb_rpass(int fb, int rpass)
fbinf.pAttachments = f->imgv;
fbinf.width = f->width;
fbinf.height = f->height;
+ fbinf.layers = 1;
if(vkCreateFramebuffer(vkdev, &fbinf, 0, &f->vkobj) != 0) {
fprintf(stderr, "vk_fb: failed to create framebuffer\n");
}
+VkShaderModule vk_load_shader(const char *fname)
+{
+ FILE *fp;
+ long sz;
+ void *buf;
+ VkShaderModuleCreateInfo sinf;
+ VkShaderModule sdr;
+
+ if(!(fp = fopen(fname, "rb"))) {
+ fprintf(stderr, "failed to open shader: %s: %s\n", fname, strerror(errno));
+ return 0;
+ }
+ fseek(fp, 0, SEEK_END);
+ sz = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ buf = alloca(sz);
+ if(fread(buf, 1, sz, fp) < sz) {
+ fprintf(stderr, "unexpected EOF while reading shader: %s\n", fname);
+ fclose(fp);
+ return 0;
+ }
+ fclose(fp);
+
+ memset(&sinf, 0, sizeof sinf);
+ sinf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
+ sinf.codeSize = sz;
+ sinf.pCode = buf;
+
+ if(vkCreateShaderModule(vkdev, &sinf, 0, &sdr) != 0) {
+ fprintf(stderr, "failed to create shader from %s\n", fname);
+ return 0;
+ }
+ return sdr;
+}
+
+void vk_free_shader(VkShaderModule sdr)
+{
+ vkDestroyShaderModule(vkdev, sdr, 0);
+}
+
+VkSemaphore vk_create_sem(void)
+{
+ VkSemaphore sem;
+ VkSemaphoreCreateInfo sinf = {0};
+
+ sinf.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
+ if(vkCreateSemaphore(vkdev, &sinf, 0, &sem) != 0) {
+ return 0;
+ }
+ return sem;
+}
+
+void vk_free_sem(VkSemaphore sem)
+{
+ vkDestroySemaphore(vkdev, sem, 0);
+}
+
+
+void vk_rect(VkRect2D *r, int x, int y, int w, int h)
+{
+ r->offset.x = x;
+ r->offset.y = y;
+ r->extent.width = w;
+ r->extent.height = h;
+}
+
+
#define ARRSZ(arr) (sizeof arr / sizeof *arr)
static const char *known_layer_list[] = {
+ "VK_LAYER_KHRONOS_validation",
"VK_LAYER_GOOGLE_threading",
"VK_LAYER_LUNARG_parameter_validation",
"VK_LAYER_LUNARG_object_tracker",
return 0;
}
+static int create_default_cmdbuf(void)
+{
+ int i, qfam;
+ VkCommandPool cmdpool;
+ VkCommandBufferAllocateInfo cbinf = {0};
+
+ assert(!vksc_cmdbuf);
+
+ if((qfam = vk_find_qfamily(VKQ_GFX | VKQ_PRESENT)) == -1) {
+ fprintf(stderr, "failed to find a gfx|present capable queue family\n");
+ return -1;
+ }
+ if(!(cmdpool = find_cmdpool(qfam))) {
+ fprintf(stderr, "failed to find usable command pool for default command buffers\n");
+ return -1;
+ }
+ if(!(vksc_cmdbuf = malloc(vksc_numimg * sizeof *vksc_cmdbuf))) {
+ fprintf(stderr, "failed to allocate %d command buffers for the swap-chain\n", vksc_numimg);
+ return -1;
+ }
+
+ cbinf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
+ cbinf.commandPool = cmdpool;
+ cbinf.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
+ cbinf.commandBufferCount = vksc_numimg;
+
+ if(vkAllocateCommandBuffers(vkdev, &cbinf, vksc_cmdbuf) != 0) {
+ fprintf(stderr, "failed to create %d command buffers for the swap-chain\n", vksc_numimg);
+ return -1;
+ }
+ return 0;
+}
static int eval_pdev_score(VkPhysicalDevice dev)
{