foo
[vktest3] / src / vk.c
index 6887460..a22b7a3 100644 (file)
--- a/src/vk.c
+++ b/src/vk.c
@@ -43,9 +43,31 @@ struct framebuf {
        VkFramebuffer vkobj;
 };
 
+struct pipeline {
+       int used;
+       int vport[4], scissor[4];
+       VkShaderModule sdr[VKSDR_MAX];
+       VkPrimitiveTopology prim;
+       VkPolygonMode polymode;
+       VkCullModeFlags cull;
+       VkFrontFace     frontface;
+       VkColorComponentFlags colorwr;
+       int zbuf, depthwr;
+       int stencil, stencilwr;
+       VkStencilOp sfail, szfail, szpass;
+       VkCompareOp sop;
+       unsigned int sref, smask;
+       int blend;
+       VkBlendFactor srcblend, dstblend, srcblend_a, dstblend_a;
+
+       int vkobj_valid;
+       VkPipeline vkobj;
+};
+
 
 static struct rpass *rpasses;
 static struct framebuf *framebufs;
+static struct pipeline *pipelines;
 
 
 static int create_instance(void);
@@ -66,6 +88,8 @@ static int initflags;
 static struct {
        unsigned int flags;
        int count;
+       int qfam;
+       VkCommandPool cmdpool;
 } initq[MAX_INIT_QUEUE];
 static int num_initq;
 
@@ -101,6 +125,15 @@ void vk_init_xwin(Display *d, Window w)
 
 void vk_init_queue(unsigned int qflags, int count)
 {
+       int i;
+
+       for(i=0; i<num_initq; i++) {
+               if(initq[i].flags == qflags) {
+                       initq[i].count += count;
+                       return;
+               }
+       }
+
        if(num_initq >= MAX_INIT_QUEUE) {
                fprintf(stderr, "vk_init_queue: too many queues\n");
                return;
@@ -238,9 +271,69 @@ VkQueue vk_getq_fam(int fam, int n)
        return q;
 }
 
-VkQueue vk_getq(unsigned int flags)
+VkQueue vk_getq(unsigned int flags, int n)
 {
-       return vk_getq_fam(vk_find_qfamily(flags), 0);
+       return vk_getq_fam(vk_find_qfamily(flags), n);
+}
+
+static VkCommandPool find_cmdpool(int qfam)
+{
+       int i;
+       VkCommandPoolCreateInfo pinf;
+
+       for(i=0; i<num_initq; i++) {
+               if(initq[i].qfam == qfam) {
+                       if(!initq[i].cmdpool) {
+                               /* allocate command pool for this queue family */
+                               memset(&pinf, 0, sizeof pinf);
+                               pinf.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
+                               pinf.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
+                               pinf.queueFamilyIndex = qfam;
+
+                               if(vkCreateCommandPool(vkdev, &pinf, 0, &initq[i].cmdpool) != 0) {
+                                       fprintf(stderr, "ck_create_cmdbuf: failed to create command buffer pool\n");
+                                       return 0;
+                               }
+                       }
+                       return initq[i].cmdpool;
+               }
+       }
+
+       fprintf(stderr, "vk_create_cmdbuf: failed to find command pool for queue family: %d\n", qfam);
+       return 0;
+}
+
+VkCommandBuffer vk_create_cmdbuf_fam(int qfam, int level)
+{
+       VkCommandBufferAllocateInfo inf = {0};
+       VkCommandBuffer cmdbuf;
+       VkCommandPool cmdpool;
+
+       if(!(cmdpool = find_cmdpool(qfam))) {
+               return 0;
+       }
+
+       inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
+       inf.commandPool = cmdpool;
+       inf.level = level;
+       inf.commandBufferCount = 1;
+
+       if(vkAllocateCommandBuffers(vkdev, &inf, &cmdbuf) != 0) {
+               fprintf(stderr, "vk_create_cmdbuf: failed to allocate command buffer\n");
+               return 0;
+       }
+       return cmdbuf;
+}
+
+VkCommandBuffer vk_create_cmdbuf(unsigned int qflags, int level)
+{
+       int qfam;
+
+       if((qfam = vk_find_qfamily(qflags)) == -1) {
+               fprintf(stderr, "vk_create_cmdbuf: failed to find matching queue family\n");
+               return 0;
+       }
+       return vk_create_cmdbuf_fam(qfam, level);
 }
 
 int vk_create_rpass(void)
@@ -494,6 +587,205 @@ VkFramebuffer vk_fb(int fb)
 }
 
 
+int vk_create_pipeln(void)
+{
+       int i;
+       struct pipeline pipeln = {0}, *pp = &pipeln;
+
+       if(!pipelines) {
+               pipelines = darr_alloc(0, sizeof *pipelines);
+               darr_push(pipelines, &pipeln);  /* add dummy pipeline */
+       }
+
+       for(i=1; i<darr_size(pipelines); i++) {
+               if(!pipelines[i].used) {
+                       pp = pipelines + i;
+               }
+       }
+
+       /* init pipeline defaults */
+       memset(pp, 0, sizeof *pp);
+       pp->used = 1;
+       pp->vport[2] = pp->scissor[2] = 640;
+       pp->vport[3] = pp->scissor[3] = 480;
+       pp->prim = VKPRIM_TRIANGLES;
+       pp->polymode = VK_POLYGON_MODE_FILL;
+       pp->cull = VK_CULL_MODE_BACK_BIT;
+       pp->frontface = VK_FRONT_FACE_COUNTER_CLOCKWISE;
+       pp->colorwr = 0xf;      /* RGBA */
+       pp->zbuf = 1;
+       pp->depthwr = 1;
+       pp->stencil = 0;
+       pp->stencilwr = 1;
+       pp->sop = VK_COMPARE_OP_ALWAYS;
+       pp->smask = 0xffffffff;
+       pp->blend = 0;
+       pp->srcblend = pp->srcblend_a = VK_BLEND_FACTOR_ONE;
+       pp->dstblend = pp->dstblend_a = VK_BLEND_FACTOR_ZERO;
+
+       if(pp == &pipeln) {
+               darr_push(pipelines, pp);
+               return darr_size(pipelines) - 1;
+       }
+       return pp - pipelines;
+}
+
+void vk_free_pipeln(int pp)
+{
+       if(!pipelines || pp < 1 || pp >= darr_size(pipelines)) {
+               return;
+       }
+
+       if(pipelines[pp].used && pipelines[pp].vkobj) {
+               vkDestroyPipeline(vkdev, pipelines[pp].vkobj, 0);
+       }
+       pipelines[pp].used = 0;
+}
+
+void vk_viewport(int pp, int x, int y, int width, int height)
+{
+       struct pipeline *p = pipelines + pp;
+       p->vport[0] = x;
+       p->vport[1] = y;
+       p->vport[2] = width;
+       p->vport[3] = height;
+       p->vkobj_valid = 0;
+}
+
+void vk_scissor(int pp, int x, int y, int width, int height)
+{
+       struct pipeline *p = pipelines + pp;
+       p->scissor[0] = x;
+       p->scissor[1] = y;
+       p->scissor[2] = width;
+       p->scissor[3] = height;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_shader(int pp, int type, VkShaderModule sdr)
+{
+       struct pipeline *p = pipelines + pp;
+       p->sdr[type] = sdr;
+       p->vkobj_valid = 0;
+}
+
+/* TODO: vertex input */
+void vk_pipeln_prim(int pp, int prim)
+{
+       struct pipeline *p = pipelines + pp;
+       p->prim = prim;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_polymode(int pp, int mode)
+{
+       struct pipeline *p = pipelines + pp;
+       p->polymode = mode;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_cull(int pp, int cull)
+{
+       struct pipeline *p = pipelines + pp;
+       p->cull = cull;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_frontface(int pp, int ff)
+{
+       struct pipeline *p = pipelines + pp;
+       p->frontface = ff;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_multisample(int pp, int nsamples)
+{
+       /* TODO */
+}
+
+void vk_pipeln_colormask(int pp, int r, int g, int b, int a)
+{
+       struct pipeline *p = pipelines + pp;
+       p->colorwr = 0;
+       if(r) p->colorwr |= VK_COLOR_COMPONENT_R_BIT;
+       if(g) p->colorwr |= VK_COLOR_COMPONENT_G_BIT;
+       if(b) p->colorwr |= VK_COLOR_COMPONENT_B_BIT;
+       if(a) p->colorwr |= VK_COLOR_COMPONENT_A_BIT;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_depthmask(int pp, int z)
+{
+       struct pipeline *p = pipelines + pp;
+       p->depthwr = z;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_stencilmask(int pp, int s)
+{
+       struct pipeline *p = pipelines + pp;
+       p->stencilwr = s;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_zbuffer(int pp, int enable)
+{
+       struct pipeline *p = pipelines + pp;
+       p->zbuf = enable;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_stencil(int pp, int enable)
+{
+       struct pipeline *p = pipelines + pp;
+       p->stencil = enable;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_stencil_op(int pp, int sfail, int zfail, int zpass)
+{
+       struct pipeline *p = pipelines + pp;
+       p->sfail = sfail;
+       p->szfail = zfail;
+       p->szpass = zpass;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_stencil_func(int pp, int op, unsigned int ref, unsigned int mask)
+{
+       struct pipeline *p = pipelines + pp;
+       p->sop = op;
+       p->sref = ref;
+       p->smask = mask;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_blend(int pp, int enable)
+{
+       struct pipeline *p = pipelines + pp;
+       p->blend = enable;
+       p->vkobj_valid = 0;
+}
+
+void vk_pipeln_blendfunc(int pp, int src, int dst)
+{
+       struct pipeline *p = pipelines + pp;
+       p->srcblend = src;
+       p->dstblend = dst;
+       p->vkobj_valid = 0;
+}
+
+VkPipeline vk_pipeln(int pp)
+{
+       struct pipeline *p = pipelines + pp;
+
+       if(p->vkobj_valid) {
+               return p->vkobj;
+       }
+       /* TODO */
+}
+
+
 #define ARRSZ(arr)     (sizeof arr / sizeof *arr)
 static const char *known_layer_list[] = {
        "VK_LAYER_GOOGLE_threading",
@@ -717,6 +1009,9 @@ static int create_device(void)
                                        initq[i].flags);
                        return -1;
                }
+               initq[i].qfam = qfam;
+               initq[i].cmdpool = 0;
+
                qinf[i].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
                qinf[i].queueFamilyIndex = qfam;
                qinf[i].queueCount = initq[i].count;