-#include "vulkan/vk.h"
-extern GLFWwindow win;
+#define GLFW_INCLUDE_VULKAN
+#include <GLFW/glfw3.h>
+
+#include <alloca.h>
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <string>
+#include <vector>
+
+#include <gmath/gmath.h>
+
+#include "allocator.h"
+#include "gfxapi.h"
+#include "image.h"
+#include "vkutil.h"
+#include "vk.h"
+
+/* global variables */
+extern GLFWwindow *win;
+extern int win_w;
+extern int win_h;
+
+//command buffers
+static VkCommandBuffer init_buf;
+static VkCommandBuffer rbufs[2];
+//depth buffer
+static VkImage dimg;
+static VkImageView dview;
+static const VkFormat dformat = VK_FORMAT_D32_SFLOAT_S8_UINT;
+//swapchain
+static VkImage *images;
+static VkImageView *iviews;
+static uint32_t num_images;
+static VkFramebuffer fbs[2];
+//semaphores-drawing-presentation
+static uint32_t curr_img; // current sc image
+static VkSemaphore psema;
+
+/* static variables */
+static Vec4 clear_color(1, 0.1, 0.1, 1.0);
+
+/* static functions */
+static void error_callback(int error, const char *descr);
+static void clear(float r, float g, float b);
+static void viewport(int x, int y, int width, int height);
+static void zbuffer(bool enable);
+static void cull_face(Gfx_cull_face cf);
+static void reshape(int width, int height);
+static void swapbuffers();
+static void begin_drawing();
+static void end_drawing();
+
+static bool create_swapchain(VkSwapchainKHR *sc);
+static bool create_zbuffer();
+static bool create_renderpass();
+static bool create_framebuffers();
+static bool begin_init_command_buffer(VkCommandBuffer *cb);
+static bool end_init_command_buffer(VkCommandBuffer *cb);
+static bool allocate_rendering_command_buffers(VkCommandBuffer *bufs);
+static void free_rendering_command_buffers(VkCommandBuffer *bufs, int count);
+static bool begin_rendering_command_buffers(VkCommandBuffer *bufs, int count);
bool init_vulkan()
{
+ if(!glfwInit()) {
+ fprintf(stderr, "Failed to initialize GLFW.\n");
+ return false;
+ }
+
+ if(!glfwVulkanSupported()) {
+ fprintf(stderr, "No Vulkan support on the device.\n");
+ return false;
+ }
+
+ //TODO: remove later
+ glfwSetErrorCallback(error_callback);
+
+ /* create device and command pool! */
+ if(!vku_create_device()) {
+ fprintf(stderr, "Failed to initialize vulkan.\n");
+ return false;
+ }
+
+ if(!glfwGetPhysicalDevicePresentationSupport(vk_instance, vk_physical, vk_qfamily)) {
+ fprintf(stderr, "Presentation support not found.\n");
+ return false;
+ }
+
+ glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
+ if(!(win = glfwCreateWindow(win_w, win_h, "vkcow", 0, 0))) {
+ fprintf(stderr, "Failed to create window.\n");
+ return false;
+ }
+
+ VkResult res = glfwCreateWindowSurface(vk_instance, win, 0, &vk_surface);
+ if(res != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create KHR surface: %s\n", vku_get_vulkan_error_str(res));
+ return false;
+ }
+
+ if(!create_swapchain(&vk_swapchain)) {
+ fprintf(stderr, "Failed to create swapchain.\n");
+ return false;
+ }
+
+ init_buf = VK_NULL_HANDLE;
+ if(!begin_init_command_buffer(&init_buf)) {
+ fprintf(stderr, "Failed to start VK_NULL_HANDLE command buffer.\n");
+ return false;
+ }
+
+ if(!create_zbuffer()) {
+ fprintf(stderr, "Failed to create depth buffer.\n");
+ return false;
+ }
+
+ if(!create_renderpass()) {
+ fprintf(stderr, "Failed to create the renderpass.\n");
+ return false;
+ }
+
+ if(!create_framebuffers()) {
+ fprintf(stderr, "Failed to create the framebuffer.\n");
+ return false;
+ }
+
+ if(!end_init_command_buffer(&init_buf)) {
+ fprintf(stderr, "Failed to end the command buffer.\n");
+ return false;
+ }
+
+ /* rendering command buffers */
+ if(!allocate_rendering_command_buffers(rbufs)) {
+ fprintf(stderr, "Failed to allocate rendering command buffers.\n");
+ return false;
+ }
+
+ if(!begin_rendering_command_buffers(rbufs, 2)) {
+ fprintf(stderr, "Failed to begin rendering command buffers.\n");
+ return false;
+ }
+
+ gfx_clear = clear;
+ gfx_viewport = viewport;
+ gfx_zbuffer = zbuffer;
+ gfx_cull_face = cull_face;
+ gfx_reshape = reshape;
+ gfx_swapbuffers = swapbuffers;
+ gfx_begin_drawing = begin_drawing;
+ gfx_end_drawing = end_drawing;
+
+ return true;
+}
+
+static bool create_swapchain(VkSwapchainKHR *sc)
+{
+ /* surface capabilities */
+ VkSurfaceCapabilitiesKHR scap;
+ if(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vk_physical, vk_surface, &scap) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to get physical device surface capabilities\n");
+ return false;
+ }
+
+ /* presentation modes */
+ uint32_t prmode_cnt;
+ if(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical, vk_surface, &prmode_cnt, 0) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to get physical device surface presentation modes count.\n");
+ return false;
+ }
+
+ if(prmode_cnt == 0) {
+ fprintf(stderr, "Presentation modes not found.\n");
+ return false;
+ }
+
+ VkPresentModeKHR scmode = VK_PRESENT_MODE_FIFO_KHR;
+ VkPresentModeKHR *modes = new VkPresentModeKHR[prmode_cnt];
+
+ if(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical, vk_surface, &prmode_cnt, modes) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to get physical device presentation modes.\n");
+ return false;
+ }
+
+ for(uint32_t i=0; i<prmode_cnt; i++) {
+ if(modes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
+ scmode = VK_PRESENT_MODE_MAILBOX_KHR;
+ break;
+ }
+ if((scmode != VK_PRESENT_MODE_MAILBOX_KHR) && (modes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
+ //fallback
+ scmode = VK_PRESENT_MODE_IMMEDIATE_KHR;
+ }
+ }
+
+ /* swapchain extents */
+ VkExtent2D scextent;
+ if(scap.currentExtent.width == 0xffffffff || scap.currentExtent.height == 0xffffffff) {
+ scextent.width = win_w;
+ scextent.height = win_h;
+ }
+ else {
+ scextent = scap.currentExtent;
+ win_w = scextent.width;
+ win_h = scextent.height;
+ }
+
+ /* number of swapchain images (on intel that's 3: maybe should hardcode it to 2?) */
+ num_images = scap.minImageCount; //+ 1;
+ /* intel doesn't set the maxImageCount */
+ if(scap.maxImageCount > 0 && num_images > scap.maxImageCount)
+ num_images = scap.maxImageCount;
+
+ printf("num_images : %u\n", num_images);
+ assert(num_images > 0);
+
+ images = new VkImage[num_images];
+ iviews = new VkImageView[num_images];
+
+ /* transform flags */
+ VkSurfaceTransformFlagBitsKHR pre_transf = scap.currentTransform;
+ if(scap.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
+ pre_transf = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
+
+ /* find suitable colorspace, format */
+ VkFormat format;
+ VkColorSpaceKHR colorspace;
+
+ VkSurfaceFormatKHR sformat;
+ if(!vku_get_surface_format(vk_physical, vk_surface, &sformat)) {
+ return false;
+ }
+
+ format = sformat.format;
+ colorspace = sformat.colorSpace;
+
+ /* creating the swapchain */
+ VkSwapchainCreateInfoKHR sinfo;
+ memset(&sinfo, 0, sizeof sinfo);
+
+ sinfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
+ sinfo.surface = vk_surface;
+ sinfo.minImageCount = num_images;
+ sinfo.imageFormat = format;
+ sinfo.imageColorSpace = colorspace;
+ sinfo.imageExtent = scextent;
+ sinfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
+ sinfo.preTransform = pre_transf;
+ sinfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
+ sinfo.imageArrayLayers = 1;
+ sinfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
+ sinfo.presentMode = scmode;
+ sinfo.oldSwapchain = VK_NULL_HANDLE; //TODO
+ sinfo.clipped = VK_TRUE; //TODO
+
+ if(vkCreateSwapchainKHR(vk_device, &sinfo, 0, sc) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create swapchain.\n");
+ return false;
+ }
+
+ if(vkGetSwapchainImagesKHR(vk_device, *sc, &num_images, 0) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to get the number of the swapchain images.\n");
+ return false;
+ }
+
+ if(vkGetSwapchainImagesKHR(vk_device, *sc, &num_images, images) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to get the swapchain images.\n");
+ return false;
+ }
+
+ VkImageViewCreateInfo ivinf;
+ memset(&ivinf, 0, sizeof ivinf);
+ ivinf.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
+ ivinf.format = format;
+ ivinf.components = {
+ VK_COMPONENT_SWIZZLE_R,
+ VK_COMPONENT_SWIZZLE_G,
+ VK_COMPONENT_SWIZZLE_B,
+ VK_COMPONENT_SWIZZLE_A
+ };
+ ivinf.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
+ ivinf.subresourceRange.levelCount = 1;
+ ivinf.subresourceRange.layerCount = 1;
+ ivinf.viewType = VK_IMAGE_VIEW_TYPE_2D;
+
+ for(uint32_t i=0; i<num_images; i++) {
+ ivinf.image = images[i];
+
+ VkResult res;
+ if((res = vkCreateImageView(vk_device, &ivinf, 0, &iviews[i])) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create image view %d: %s.\n", i, vku_get_vulkan_error_str(res));
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static bool begin_init_command_buffer(VkCommandBuffer *cb)
+{
+ if(*cb == VK_NULL_HANDLE) {
+ *cb = vku_alloc_cmdbuf(vk_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
+
+ VkCommandBufferInheritanceInfo ciinf;
+ memset(&ciinf, 0, sizeof ciinf);
+ ciinf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
+ ciinf.renderPass = VK_NULL_HANDLE;
+ ciinf.framebuffer = VK_NULL_HANDLE;
+ ciinf.occlusionQueryEnable = VK_FALSE;
+
+ VkCommandBufferBeginInfo cbinf;
+ memset(&cbinf, 0, sizeof cbinf);
+ cbinf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
+ cbinf.pInheritanceInfo = &ciinf;
+
+ if(vkBeginCommandBuffer(*cb, &cbinf) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to begin command buffer.\n");
+ return false;
+ }
+ }
+ return true;
+}
+
+static bool create_zbuffer()
+{
+ VkImageCreateInfo dinfo;
+ memset(&dinfo, 0, sizeof dinfo);
+
+ dinfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
+ dinfo.imageType = VK_IMAGE_TYPE_2D;
+ dinfo.format = dformat;
+ dinfo.extent = {(uint32_t)win_w, (uint32_t)win_h, 1};
+ dinfo.mipLevels = 1;
+ dinfo.arrayLayers = 1;
+ dinfo.samples = VK_SAMPLE_COUNT_1_BIT;
+ dinfo.tiling = VK_IMAGE_TILING_OPTIMAL;
+ dinfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
+
+ if(vkCreateImage(vk_device, &dinfo, 0, &dimg) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create depth buffer image.\n");
+ return false;
+ }
+
+ VkMemoryRequirements dmem_reqs;
+ vkGetImageMemoryRequirements(vk_device, dimg, &dmem_reqs);
+
+ DevMemBlock block;
+ if(!vku_allocate(dmem_reqs.size, &block)) {
+ fprintf(stderr, "Failed to allocate zbuffer image.\n");
+ return false;
+ }
+
+ vkBindImageMemory(vk_device, dimg, block.dev_mem, 0);
+
+ if(!vk_image_set_layout(init_buf, dimg, VK_IMAGE_ASPECT_DEPTH_BIT,
+ VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
+ (VkAccessFlagBits)0)) {
+ fprintf(stderr, "Failed to set depth buffer layout.\n");
+ return false;
+ }
+
+ VkImageViewCreateInfo div_inf;
+ memset(&div_inf, 0, sizeof div_inf);
+
+ div_inf.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
+ div_inf.image = dimg;
+ div_inf.format = dformat;
+ div_inf.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
+ div_inf.subresourceRange.levelCount = 1;
+ div_inf.subresourceRange.layerCount = 1;
+ div_inf.viewType = VK_IMAGE_VIEW_TYPE_2D;
+
+ if(vkCreateImageView(vk_device, &div_inf, 0, &dview) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create image view for depth buffer.\n");
+ return false;
+ }
+
+ return true;
+}
+
+static bool create_renderpass()
+{
+ VkSurfaceFormatKHR sformat;
+ if(!vku_get_surface_format(vk_physical, vk_surface, &sformat)) {
+ fprintf(stderr, "Failed to get surface format.\n");
+ return false;
+ }
+
+ VkAttachmentDescription att[2];
+ memset(&att, 0, (sizeof att[0]) * 2);
+
+ att[0].format = sformat.format;
+ att[0].samples = VK_SAMPLE_COUNT_1_BIT;
+ att[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
+ att[0].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
+ att[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+ att[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
+ att[0].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
+ att[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
+
+ att[1].format = dformat;
+ att[1].samples = VK_SAMPLE_COUNT_1_BIT;
+ att[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
+ att[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
+ att[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+ att[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
+ att[1].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
+ att[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
+
+ VkAttachmentReference cref;
+ memset(&cref, 0, sizeof cref);
+ cref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
+
+ VkAttachmentReference dref;
+ memset(&dref, 0, sizeof dref);
+ dref.attachment = 1;
+ dref.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
+
+ VkSubpassDescription sd;
+ memset(&sd, 0, sizeof sd);
+ sd.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
+ sd.colorAttachmentCount = 1;
+ sd.pColorAttachments = &cref;
+ sd.pDepthStencilAttachment = &dref;
+
+ VkRenderPassCreateInfo rinf;
+ memset(&rinf, 0, sizeof rinf);
+ rinf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
+ rinf.attachmentCount = 2;
+ rinf.pAttachments = att;
+ rinf.subpassCount = 1;
+ rinf.pSubpasses = &sd;
+
+ if(vkCreateRenderPass(vk_device, &rinf, 0, &vk_renderpass) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create render pass.\n");
+ return false;
+ }
+
+ return true;
+}
+
+static bool create_framebuffers()
+{
+ /* framebuffer attachments */
+ VkImageView fb_att[2];
+ fb_att[1] = dview;
+
+ VkFramebufferCreateInfo fbinf;
+ memset(&fbinf, 0, sizeof fbinf);
+ fbinf.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
+ fbinf.renderPass = vk_renderpass;
+ fbinf.attachmentCount = 2;
+ fbinf.pAttachments = fb_att;
+ fbinf.width = win_w;
+ fbinf.height = win_h;
+ fbinf.layers = 1;
+
+ for(int i=0; i<2; i++) {
+ fb_att[0] = iviews[i];
+ if(vkCreateFramebuffer(vk_device, &fbinf, 0, &fbs[i]) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create framebuffer %i\n", i);
+ return false;
+ }
+ }
+ return true;
+}
+
+static bool end_init_command_buffer(VkCommandBuffer *cb)
+{
+ if(vkEndCommandBuffer(*cb) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to end command buffer.\n");
+ return false;
+ }
+
+ VkSubmitInfo si;
+ memset(&si, 0, sizeof si);
+ si.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
+ si.commandBufferCount = 1;
+ si.pCommandBuffers = cb;
+
+ if(vkQueueSubmit(vk_queue, 1, &si, VK_NULL_HANDLE) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to submit null queue.\n");
+ return false;
+ }
+
+ if(vkQueueWaitIdle(vk_queue) != VK_SUCCESS) {
+ fprintf(stderr, "QueueWaitIdle failure!\n");
+ return false;
+ }
+
+ vkFreeCommandBuffers(vk_device, vk_pool, 1, cb);
+ *cb = VK_NULL_HANDLE;
+
+ return true;
+}
+
+static bool allocate_rendering_command_buffers(VkCommandBuffer *bufs)
+{
+ return vku_alloc_cmdbufs(vk_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY,
+ 2, bufs);
+}
+
+static void free_rendering_command_buffers(VkCommandBuffer *bufs, int count)
+{
+ for(int i=0; i<count; i++) {
+ vku_free_cmdbuf(vk_pool, bufs[i]);
+ }
+}
+
+static bool begin_rendering_command_buffers(VkCommandBuffer *bufs, int count)
+{
+ VkCommandBufferInheritanceInfo iinf;
+ memset(&iinf, 0, sizeof iinf);
+ iinf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
+ iinf.renderPass = VK_NULL_HANDLE;
+ iinf.framebuffer = VK_NULL_HANDLE;
+
+ VkCommandBufferBeginInfo binf;
+ memset(&binf, 0, sizeof binf);
+ binf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
+ binf.pInheritanceInfo = &iinf;
+
+ // setting clear values
+ VkClearValue clvals[2];
+ for(int i=0; i<4; i++) {
+ clvals[0].color.float32[i] = clear_color[i];
+ }
+ clvals[1].depthStencil = {1.0f, 0};
+
+ //build drawing command buffers
+ for(int i=0; i<count; i++) {
+ if(vkBeginCommandBuffer(bufs[i], &binf) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to begin command buffer: %d\n", i);
+ return false;
+ }
+
+ VkImageMemoryBarrier imbarr;
+ memset(&imbarr, 0, sizeof imbarr);
+ imbarr.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
+ imbarr.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
+ imbarr.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
+ imbarr.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
+ imbarr.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ imbarr.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ imbarr.image = images[i];
+ imbarr.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
+ vkCmdPipelineBarrier(bufs[i], VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, 0, 0, 0, 1, &imbarr);
+
+ VkRenderPassBeginInfo rbinf;
+ memset(&rbinf, 0, sizeof rbinf);
+ rbinf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
+ rbinf.renderPass = vk_renderpass;
+ rbinf.framebuffer = fbs[i];
+ rbinf.renderArea.extent.width = win_w;
+ rbinf.renderArea.extent.height = win_h;
+ rbinf.clearValueCount = count;
+ rbinf.pClearValues = clvals;
+
+ vkCmdBeginRenderPass(bufs[i], &rbinf, VK_SUBPASS_CONTENTS_INLINE);
+ VkViewport viewport;
+ memset(&viewport, 0, sizeof viewport);
+ viewport.width = (float)win_w;
+ viewport.height = (float)win_h;
+ viewport.maxDepth = 1.0f;
+ vkCmdSetViewport(bufs[i], 0, 1, &viewport);
+
+ VkRect2D scissor;
+ memset(&scissor, 0, sizeof scissor);
+ scissor.extent.width = win_w;
+ scissor.extent.height = win_h;
+ vkCmdSetScissor(bufs[i], 0, 1, &scissor);
+ vkCmdEndRenderPass(bufs[i]);
+
+ /* pre-present barrier */
+ VkImageMemoryBarrier ppb;
+ memset(&ppb, 0, sizeof ppb);
+ ppb.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
+ ppb.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
+ ppb.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
+ ppb.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
+ ppb.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
+ ppb.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ ppb.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ ppb.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
+ ppb.image = images[i];
+
+ vkCmdPipelineBarrier(bufs[i], VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, 0, 0, 0, 1, &ppb);
+
+ if(vkEndCommandBuffer(bufs[i]) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to end command buffer: %d\n", i);
+ return false;
+ }
+ }
return true;
}
void cleanup_vulkan()
{
+ free_rendering_command_buffers(rbufs, 2);
+ if(win) {
+ glfwDestroyWindow(win);
+ }
+ glfwTerminate();
+
+ //TODOs according to the book:
+ // 1- make sure all threads have been terminated (when I add threads)
+ vku_cleanup();
}
-GLFWwindow *create_vulkan_window()
+static void error_callback(int error, const char *description)
{
- return 0;
+ fprintf(stderr, "GLFW error %d: %s.\n", error, description);
}
-void display_vulkan()
+static void reshape(int width, int height)
{
}
+
+static void clear(float r, float g, float b)
+{
+}
+
+static void viewport(int x, int y, int width, int height)
+{
+}
+
+static void zbuffer(bool enable)
+{
+}
+
+static void cull_face(Gfx_cull_face cf)
+{
+}
+
+static void swapbuffers()
+{
+}
+
+static void begin_drawing()
+{
+ VkSemaphoreCreateInfo psinf;
+ memset(&psinf, 0, sizeof psinf);
+ psinf.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
+
+ VkResult res;
+ if((res = vkCreateSemaphore(vk_device, &psinf, 0, &psema)) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create semaphore.\n");
+ }
+ assert(res == VK_SUCCESS);
+}
+
+static void end_drawing()
+{
+ VkResult res = vkAcquireNextImageKHR(vk_device, vk_swapchain,
+ UINT64_MAX, psema, VK_NULL_HANDLE, &curr_img);
+ if(res == VK_ERROR_OUT_OF_DATE_KHR) {
+ fprintf(stderr, "Swapchain out of date.\n");
+ return;
+ }
+ assert(res == VK_SUCCESS);
+
+ VkSubmitInfo sinf;
+ memset(&sinf, 0, sizeof sinf);
+ VkPipelineStageFlags psflags = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
+ sinf.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
+ sinf.waitSemaphoreCount = 1;
+ sinf.pWaitSemaphores = &psema;
+ sinf.pWaitDstStageMask = &psflags;
+ sinf.commandBufferCount = 1;
+ sinf.pCommandBuffers = &rbufs[curr_img];
+
+ res = vkQueueSubmit(vk_queue, 1, &sinf, VK_NULL_HANDLE);
+ assert(res == VK_SUCCESS);
+
+ VkPresentInfoKHR pinf;
+ memset(&pinf, 0, sizeof pinf);
+ pinf.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
+ pinf.swapchainCount = 1;
+ pinf.pSwapchains = &vk_swapchain;
+ pinf.pImageIndices = &curr_img;
+
+ res = vkQueuePresentKHR(vk_queue, &pinf);
+ if(res == VK_ERROR_OUT_OF_DATE_KHR) {
+ fprintf(stderr, "Swapchain out of date.\n");
+ }
+ assert(res == VK_SUCCESS);
+
+ res = vkQueueWaitIdle(vk_queue);
+ assert(res == VK_SUCCESS);
+
+ vkDestroySemaphore(vk_device, psema, 0);
+}