fix pipeline cleanup
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 17 Sep 2024 03:13:54 +0000 (06:13 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 17 Sep 2024 03:13:54 +0000 (06:13 +0300)
.gitignore
Makefile
sdr/sdr.p.glsl [new file with mode: 0644]
sdr/sdr.v.glsl [new file with mode: 0644]
src/app.c
src/vk.c

index 5f30f93..9bab0d3 100644 (file)
@@ -6,3 +6,4 @@ vktest
 *.vcxproj.user
 vktest3/
 x64/
+*.spv
index d7c439b..f7c00ca 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -14,10 +14,10 @@ LDFLAGS = -lvulkan -lX11 -lX11-xcb
 $(bin): $(obj) $(spirv)
        $(CC) -o $@ $(obj) $(LDFLAGS)
 
-%.spv: %.v.glsl
+%.v.spv: %.v.glsl
        glslangValidator -o $@ -S vert -V $<
 
-%.spv: %.p.glsl
+%.p.spv: %.p.glsl
        glslangValidator -o $@ -S frag -V $<
 
 -include $(dep)
diff --git a/sdr/sdr.p.glsl b/sdr/sdr.p.glsl
new file mode 100644 (file)
index 0000000..a04f99a
--- /dev/null
@@ -0,0 +1,10 @@
+#version 450
+#extension GL_ARB_separate_shader_objects : enable
+
+layout(location = 0) in vec3 incol;
+layout(location = 0) out vec4 outcol;
+
+void main()
+{
+       outcol = vec4(incol, 1.0);
+}
diff --git a/sdr/sdr.v.glsl b/sdr/sdr.v.glsl
new file mode 100644 (file)
index 0000000..3a60cbe
--- /dev/null
@@ -0,0 +1,26 @@
+#version 450
+#extension GL_ARB_separate_shader_objects : enable
+
+out gl_PerVertex {
+       vec4 gl_Position;
+};
+
+layout(location = 0) out vec3 color;
+
+vec2 vpos[3] = vec2[](
+       vec2(0.0, -0.5),
+       vec2(0.5, 0.5),
+       vec2(-0.5, 0.5)
+);
+
+vec3 vcol[3] = vec3[](
+       vec3(1.0, 0.0, 0.0),
+       vec3(0.0, 1.0, 0.0),
+       vec3(0.0, 0.0, 1.0)
+);
+
+void main()
+{
+       gl_Position = vec4(vpos[gl_VertexIndex], 0.0, 1.0);
+       color = vcol[gl_VertexIndex];
+}
index c242b32..4bece43 100644 (file)
--- a/src/app.c
+++ b/src/app.c
@@ -20,6 +20,7 @@ static int *fb;
 static struct frame frame[NUM_FRM];
 static VkSemaphore sem_draw;
 static VkQueue queue;
+static VkShaderModule vsdr, psdr;
 
 int app_init(void)
 {
@@ -74,6 +75,13 @@ int app_init(void)
        vk_pipeln_rpass(pipeln, vk_rpass(rpass));
        vk_pipeln_viewport(pipeln, 0, 0, win_width, win_height);
 
+       if(!(vsdr = vk_load_shader("sdr/sdr.v.spv")) || !(psdr = vk_load_shader("sdr/sdr.p.spv"))) {
+               fprintf(stderr, "failed to load shaders\n");
+               return -1;
+       }
+       vk_pipeln_shader(pipeln, VKSDR_VERTEX, vsdr);
+       vk_pipeln_shader(pipeln, VKSDR_PIXEL, psdr);
+
        sem_draw = vk_create_sem();
        return 0;
 }
@@ -89,6 +97,9 @@ void app_cleanup(void)
        }
        vk_free_rpass(rpass);
 
+       vk_free_shader(vsdr);
+       vk_free_shader(psdr);
+
        for(i=0; i<NUM_FRM; i++) {
                vk_free_fence(frame[i].imgfence);
                vk_free_sem(frame[i].sem_getimg);
@@ -138,9 +149,8 @@ void app_display(void)
                rpbegin.clearValueCount = 1;
 
                vkCmdBeginRenderPass(cmdbuf, &rpbegin, VK_SUBPASS_CONTENTS_INLINE);
-
-               /* ... */
-
+               vkCmdBindPipeline(cmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS, vk_pipeln(pipeln));
+               vkCmdDraw(cmdbuf, 3, 1, 0, 0);
                vkCmdEndRenderPass(cmdbuf);
                vkEndCommandBuffer(cmdbuf);
        }
index 743464f..e10cce1 100644 (file)
--- a/src/vk.c
+++ b/src/vk.c
@@ -217,6 +217,14 @@ void vk_cleanup(void)
                }
        }
 
+       if(pipelines) {
+               for(i=0; i<darr_size(pipelines); i++) {
+                       if(pipelines[i].used) {
+                               vk_free_pipeln(i);
+                       }
+               }
+       }
+
        if(vkdev) {
                vkDestroyDevice(vkdev, 0);
                vkdev = 0;
@@ -744,9 +752,18 @@ void vk_free_pipeln(int pp)
        if(!pipelines || pp < 1 || pp >= darr_size(pipelines)) {
                return;
        }
+       if(!pipelines[pp].used) {
+               return;
+       }
+
+       if(pipelines[pp].vkobj_layout) {
+               vkDestroyPipelineLayout(vkdev, pipelines[pp].vkobj_layout, 0);
+               pipelines[pp].vkobj_layout = 0;
+       }
 
-       if(pipelines[pp].used && pipelines[pp].vkobj) {
+       if(pipelines[pp].vkobj) {
                vkDestroyPipeline(vkdev, pipelines[pp].vkobj, 0);
+               pipelines[pp].vkobj = 0;
        }
        pipelines[pp].used = 0;
 }