+ 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);
+
+ /* 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 */
+ uint32_t fcount;
+ if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical, vk_surface, &fcount, 0) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to get format count for physical device.\n");
+ return false;
+ }
+ if(fcount == 0) {
+ fprintf(stderr, "No color formats were found.\n");
+ return false;
+ }
+
+ VkFormat format;
+ VkColorSpaceKHR colorspace;
+
+ VkSurfaceFormatKHR *formats = new VkSurfaceFormatKHR[fcount];
+ if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical, vk_surface, &fcount, formats) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to get surface formats.\n");
+ return false;
+ }
+ if((fcount == 1) && (formats[0].format == VK_FORMAT_UNDEFINED)) {
+ format = VK_FORMAT_B8G8R8_UNORM;
+ } else {
+ format = formats[0].format;
+ }
+ colorspace = formats[0].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, &vk_swapchain) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create swapchain.\n");
+ return false;
+ }
+
+ if(vkGetSwapchainImagesKHR(vk_device, vk_swapchain, &num_images, 0) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to get the number of the swapchain images.\n");
+ return false;
+ }
+
+ images = new VkImage[num_images];
+ if(vkGetSwapchainImagesKHR(vk_device, vk_swapchain, &num_images, images) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to get the swapchain images.\n");
+ return false;
+ }
+ assert(images);
+
+ if(!create_swapchain_image_views(format)) {
+ fprintf(stderr, "Failed to create image views for the swapchain images.\n");
+ return false;
+ }
+
+ // create renderpass
+
+
+ return true;
+}
+
+static bool create_swapchain_image_views(VkFormat format)
+{
+ 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;
+
+ iviews = new VkImageView[num_images];
+ for(uint32_t i=0; i<num_images; i++) {
+ ivinf.image = images[i];
+ if(vkCreateImageView(vk_device, &ivinf, 0, &iviews[i]) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create view for the swapchain image: %d.\n", i);
+ return false;
+ }
+ }
+ return true;
+}
+
+static bool begin_command_buffer()
+{
+ if(cmd_buf == VK_NULL_HANDLE) {
+ cmd_buf = 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(cmd_buf, &cbinf) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to begin command buffer.\n");
+ return false;
+ }
+ }
+ return true;
+}
+
+static bool create_zbuffer()
+{
+ VkFormat dformat = VK_FORMAT_D32_SFLOAT_S8_UINT;
+
+ 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 = {win_w, 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);
+
+ gpu_mem = vk_allocate(dmem_reqs.size);
+
+ if(!gpu_mem)
+ return false;
+
+ vkBindImageMemory(vk_device, dimg, gpu_mem, 0);
+
+ // XXX set image layout
+ if(!vk_image_set_layout(cmd_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");
+ }