vk_rpass
authorJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 12 Aug 2022 06:14:36 +0000 (09:14 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 12 Aug 2022 06:14:36 +0000 (09:14 +0300)
src/vk.c
src/vk.h

index 44c838f..0749f07 100644 (file)
--- a/src/vk.c
+++ b/src/vk.c
@@ -4,6 +4,7 @@
 #include <vulkan/vulkan.h>
 #include "vk.h"
 #include "util.h"
+#include "darray.h"
 
 #ifdef __WIN32__
 #include <vulkan/vulkan_win32.h>
@@ -144,6 +145,7 @@ int vk_reshape(int xsz, int ysz)
 struct rpass {
        int used;
        int fmt;
+       int zfmt;
        int num_colbuf;
        int num_samples;
        /* TODO: stuff about depth-stencil */
@@ -174,6 +176,7 @@ int vk_create_rpass(void)
        /* init renderpass defaults */
        rp->used = 1;
        rp->fmt = vksurf_fmt[vksurf_selfmt].format;
+       rp->zfmt = VK_FORMAT_D24_UNORM_S8_UINT;
        rp->num_colbuf = 1;
        rp->num_samples = 1;
        rp->clear = 1;
@@ -211,48 +214,72 @@ void vk_rpass_msaa(int rp, int nsamp)
        rpasses[rp].num_samples = nsamp;
 }
 
-void vk_rpass_clear(int clear)
+void vk_rpass_clear(int rp, int clear)
 {
        rpasses[rp].clear = clear;
 }
 
 VkRenderPass vk_rpass(int rp)
 {
+       int i, zidx;
        struct rpass *r;
-       VkAttachmentDescription cat;
-       VkAttachmentReference catref[16];
+       VkAttachmentDescription att[17];
+       VkAttachmentReference catref[16], zatref;
        VkSubpassDescription subpass;
        VkRenderPassCreateInfo pinf;
-       VkSubpassDependency dep;
 
        r = rpasses + rp;
 
        if(!r->vkobj_valid) {
-               memset(&cat, 0, sizeof cat);
-               cat.format = r->fmt;
-               cat.samples = r->num_samples;
-               cat.loadOp = r->clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
-               cat.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
-               cat.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
-               cat.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
-               cat.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
-               cat.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
+               zidx = r->num_colbuf;
+               memset(att, 0, sizeof att);
+               for(i=0; i<r->num_colbuf; i++) {
+                       att[i].format = r->fmt;
+                       att[i].samples = r->num_samples;
+                       att[i].loadOp = r->clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+                       att[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
+                       att[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+                       att[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
+                       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].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
+               att[zidx].stencilLoadOp = 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++) {
-                       memset(&catref[i], 0, sizeof catref);
+                       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;
-
-               memset(&dep, 0, sizeof dep);
-               dep.srcSubpass = 
+               subpass.pDepthStencilAttachment = &zatref;
+
+               memset(&pinf, 0, sizeof pinf);
+               pinf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
+               pinf.attachmentCount = r->num_colbuf + 1;
+               pinf.pAttachments = att;
+               pinf.subpassCount = 1;
+               pinf.pSubpasses = &subpass;
+
+               if(vkCreateRenderPass(vkdev, &pinf, 0, &r->vkobj) != 0) {
+                       fprintf(stderr, "failed to create render pass!\n");
+                       return 0;
+               }
+               r->vkobj_valid = 1;
        }
 
-       return rpasses[rp].vkobj;
+       return r->vkobj;
 }
 
 #define ARRSZ(arr)     (sizeof arr / sizeof *arr)
index 4e94784..3322f14 100644 (file)
--- a/src/vk.h
+++ b/src/vk.h
@@ -2,6 +2,7 @@
 #define VK_H_
 
 #include <X11/Xlib.h>
+#include <vulkan/vulkan.h>
 
 enum {
        VKINIT_DEPTH    = 1,
@@ -20,6 +21,7 @@ int vk_create_rpass(void);
 void vk_free_rpass(int rp);
 void vk_rpass_colorbuf(int rp, int fmt, int n);
 void vk_rpass_msaa(int rp, int nsamp);
+void vk_rpass_clear(int rp, int clear);
 VkRenderPass vk_rpass(int rp);