fixed some validation bugs
[vktest3] / src / app.c
index 044a885..bc931d1 100644 (file)
--- a/src/app.c
+++ b/src/app.c
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include "app.h"
 #include "vk.h"
 
+int win_width, win_height;
+
+static int rpass, pipeln, *fb, num_swap_img;
+static VkSemaphore sem_getimg, sem_draw;
+static VkQueue queue;
+
 int app_init(void)
 {
+       int i;
        unsigned int flags;
 
        if(vk_init(VKINIT_DEPTH, &flags) == -1) {
                return -1;
        }
+
+       if(!(queue = vk_getq(VKQ_GFX | VKQ_PRESENT, 0))) {
+               return -1;
+       }
+
+       /* force swapchain creation, to find out how many framebuffers to create */
+       vk_reshape(win_width, win_height);
+       if((num_swap_img = vk_num_swap_images()) <= 0) {
+               return -1;
+       }
+       if(!(fb = malloc(num_swap_img * sizeof *fb))) {
+               return -1;
+       }
+
+       rpass = vk_create_rpass();
+
+       for(i=0; i<num_swap_img; i++) {
+               fb[i] = vk_create_fb();
+               vk_fb_size(fb[i], win_width, win_height);
+               vk_fb_rpass(fb[i], rpass);
+               vk_fb_images(fb[i], 1, vk_swap_image(i));
+       }
+
+       pipeln = vk_create_pipeln();
+       vk_pipeln_rpass(pipeln, vk_rpass(rpass));
+       vk_pipeln_viewport(pipeln, 0, 0, win_width, win_height);
+
+       sem_getimg = vk_create_sem();
+       sem_draw = vk_create_sem();
        return 0;
 }
 
 void app_cleanup(void)
 {
+       vk_free_sem(sem_getimg);
+       vk_free_sem(sem_draw);
        vk_cleanup();
 }
 
 
 void app_display(void)
 {
+       int imgid;
+       VkCommandBuffer cmdbuf;
+
+       /* get the next image from the swap chain */
+       imgid = vk_next_swap_image(sem_getimg);
+       cmdbuf = vk_get_cmdbuf(imgid);
+
+       {
+               VkCommandBufferBeginInfo cmdbegin = {0};
+               VkRenderPassBeginInfo rpbegin = {0};
+               VkClearValue clear[2];
+
+               cmdbegin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
+               cmdbegin.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
+
+               if(vkBeginCommandBuffer(cmdbuf, &cmdbegin) != 0) {
+                       fprintf(stderr, "failed to begin command buffer recording\n");
+                       abort();
+               }
+
+               clear[0].color.float32[0] = 0.5f;
+               clear[0].color.float32[1] = 0.1f;
+               clear[0].color.float32[2] = 0.2f;
+               clear[0].color.float32[3] = 1.0f;
+               clear[1].depthStencil.depth = 1.0f;
+               clear[1].depthStencil.stencil = 0;
+
+               rpbegin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
+               rpbegin.renderPass = vk_rpass(rpass);
+               rpbegin.framebuffer = vk_fb(fb[imgid]);
+               vk_rect(&rpbegin.renderArea, 0, 0, win_width, win_height);
+               rpbegin.pClearValues = clear;
+               rpbegin.clearValueCount = 1;
+
+               vkCmdBeginRenderPass(cmdbuf, &rpbegin, VK_SUBPASS_CONTENTS_INLINE);
+
+               /* ... */
+
+               vkCmdEndRenderPass(cmdbuf);
+               vkEndCommandBuffer(cmdbuf);
+       }
+
+       /* submit the command buffer, wait for one semaphore, signal another */
+       vk_submit(queue, cmdbuf, sem_getimg, sem_draw);
+
+       /* swap buffers after drawing is finished */
+       vk_present(queue, imgid, sem_draw);
+
+       usleep(10000);
 }
 
 void app_reshape(int x, int y)
 {
+       int i;
+
+       return; /* XXX */
+
        if(vk_reshape(x, y) == -1) {
                abort();
        }
+
+       for(i=0; i<vk_num_swap_images(); i++) {
+               vk_fb_size(fb[i], x, y);
+               vk_fb_images(fb[i], 1, vk_swap_image(i));
+       }
 }
 
 void app_keyboard(int key, int press)