vulkan_test2

changeset 14:9fb6c24691ea

this shit never ends...
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 26 Jun 2018 07:20:03 +0300
parents d34f84bede17
children 196122a2b8c2
files src/vku.c src/vku.h src/wsys_x11.c
diffstat 3 files changed, 109 insertions(+), 7 deletions(-) [+]
line diff
     1.1 --- a/src/vku.c	Mon Jun 25 08:00:57 2018 +0300
     1.2 +++ b/src/vku.c	Tue Jun 26 07:20:03 2018 +0300
     1.3 @@ -312,7 +312,7 @@
     1.4  }
     1.5  
     1.6  VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n,
     1.7 -		VkPresentModeKHR pmode, VkSwapchainKHR prev)
     1.8 +		VkFormat fmt, VkPresentModeKHR pmode, VkSwapchainKHR prev)
     1.9  {
    1.10  	VkSwapchainKHR sc;
    1.11  	VkSwapchainCreateInfoKHR inf;
    1.12 @@ -321,7 +321,7 @@
    1.13  	inf.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    1.14  	inf.surface = surf;
    1.15  	inf.minImageCount = n;
    1.16 -	inf.imageFormat = VK_FORMAT_B8G8R8A8_UNORM;	/* TODO enumerate and choose */
    1.17 +	inf.imageFormat = fmt;
    1.18  	inf.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
    1.19  	inf.imageExtent.width = xsz;
    1.20  	inf.imageExtent.height = ysz;
    1.21 @@ -366,6 +366,62 @@
    1.22  	return (int)next;
    1.23  }
    1.24  
    1.25 +VkImageView vku_create_view(VkImage img, VkFormat fmt)
    1.26 +{
    1.27 +	VkImageView view;
    1.28 +	VkImageViewCreateInfo iv;
    1.29 +
    1.30 +	memset(&iv, 0, sizeof iv);
    1.31 +	iv.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
    1.32 +	iv.image = img;
    1.33 +	iv.viewType = VK_IMAGE_VIEW_TYPE_2D;
    1.34 +	iv.format = fmt;
    1.35 +	iv.components.r = iv.components.g = iv.components.b = iv.components.a =
    1.36 +		VK_COMPONENT_SWIZZLE_IDENTITY;
    1.37 +	iv.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
    1.38 +	iv.subresourceRange.baseMipLevel = 0;
    1.39 +	iv.subresourceRange.levelCount = 1;
    1.40 +	iv.subresourceRange.baseArrayLayer = 0;
    1.41 +	iv.subresourceRange.layerCount = 1;
    1.42 +
    1.43 +	if(vkCreateImageView(vkdev, &iv, 0, &view) != 0) {
    1.44 +		fprintf(stderr, "vku_create_view failed\n");
    1.45 +		return 0;
    1.46 +	}
    1.47 +	return view;
    1.48 +}
    1.49 +
    1.50 +void vku_destroy_view(VkImageView view)
    1.51 +{
    1.52 +	vkDestroyImageView(vkdev, view, 0);
    1.53 +}
    1.54 +
    1.55 +VkFramebuffer vku_create_framebuffer(VkImageView view, int width, int height, VkRenderPass rpass)
    1.56 +{
    1.57 +	VkFramebuffer fb;
    1.58 +	VkFramebufferCreateInfo fbinf;
    1.59 +
    1.60 +	memset(&fbinf, 0, sizeof fbinf);
    1.61 +	fbinf.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    1.62 +	fbinf.renderPass = rpass;
    1.63 +	fbinf.attachmentCount = 1;
    1.64 +	fbinf.pAttachments = &view;
    1.65 +	fbinf.width = width;
    1.66 +	fbinf.height = height;
    1.67 +	fbinf.layers = 1;
    1.68 +
    1.69 +	if(vkCreateFramebuffer(vkdev, &fbinf, 0, &fb) != 0) {
    1.70 +		fprintf(stderr, "vku_create_framebuffer failed\n");
    1.71 +		return 0;
    1.72 +	}
    1.73 +	return fb;
    1.74 +}
    1.75 +
    1.76 +void vku_destroy_framebuffer(VkFramebuffer fb)
    1.77 +{
    1.78 +	vkDestroyFramebuffer(vkdev, fb, 0);
    1.79 +}
    1.80 +
    1.81  void vku_present(VkSwapchainKHR sc, int img_idx)
    1.82  {
    1.83  	VkPresentInfoKHR inf;
     2.1 --- a/src/vku.h	Mon Jun 25 08:00:57 2018 +0300
     2.2 +++ b/src/vku.h	Tue Jun 26 07:20:03 2018 +0300
     2.3 @@ -13,8 +13,12 @@
     2.4  VkCommandPool vkcmdpool;
     2.5  VkCommandBuffer vkcmdbuf;	/* primary command buffer */
     2.6  
     2.7 +int swapchain_size;
     2.8  VkImage *swapchain_images;
     2.9 +VkFormat swapchain_image_fmt;
    2.10 +VkImageView *swapchain_views;
    2.11  int next_swapchain_image;
    2.12 +VkFramebuffer *swapchain_framebuf;
    2.13  
    2.14  VkViewport vkvport;
    2.15  VkRenderPass vkrpass;
    2.16 @@ -42,11 +46,17 @@
    2.17  void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence);
    2.18  
    2.19  VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n,
    2.20 -		VkPresentModeKHR pmode, VkSwapchainKHR prev);
    2.21 +		VkFormat fmt, VkPresentModeKHR pmode, VkSwapchainKHR prev);
    2.22  VkImage *vku_get_swapchain_images(VkSwapchainKHR sc, int *count);
    2.23  int vku_get_next_image(VkSwapchainKHR sc);
    2.24  void vku_present(VkSwapchainKHR sc, int img_idx);
    2.25  
    2.26 +VkImageView vku_create_view(VkImage img, VkFormat fmt);
    2.27 +void vku_destroy_view(VkImageView view);
    2.28 +
    2.29 +VkFramebuffer vku_create_framebuffer(VkImageView view, int width, int height, VkRenderPass rpass);
    2.30 +void vku_destroy_framebuffer(VkFramebuffer fb);
    2.31 +
    2.32  struct vku_buffer *vku_create_buffer(int sz, unsigned int usage);
    2.33  void vku_destroy_buffer(struct vku_buffer *buf);
    2.34  
     3.1 --- a/src/wsys_x11.c	Mon Jun 25 08:00:57 2018 +0300
     3.2 +++ b/src/wsys_x11.c	Tue Jun 26 07:20:03 2018 +0300
     3.3 @@ -214,26 +214,62 @@
     3.4  
     3.5  int wsys_process_events(int mode)
     3.6  {
     3.7 +	int i;
     3.8  	XEvent xev;
     3.9  
    3.10  	if(pending & RESHAPE) {
    3.11  		VkSwapchainKHR sc;
    3.12 -		if(!(sc = vku_create_swapchain(surf, win_width, win_height, 2, VK_PRESENT_MODE_FIFO_KHR, swapchain))) {
    3.13 +		VkFormat fmt = VK_FORMAT_B8G8R8A8_UNORM;	/* TODO enumerate and choose */
    3.14 +
    3.15 +		printf("DBG reshape\n");
    3.16 +
    3.17 +		if(!vkrpass) {
    3.18 +			if(!(vkrpass = vku_create_renderpass(fmt, VK_FORMAT_UNDEFINED))) {
    3.19 +				abort();
    3.20 +			}
    3.21 +		}
    3.22 +
    3.23 +		if(!(sc = vku_create_swapchain(surf, win_width, win_height, 2, fmt,
    3.24 +						VK_PRESENT_MODE_FIFO_KHR, swapchain))) {
    3.25  			fprintf(stderr, "Failed to create %dx%d double-buffered swapchain\n", win_width, win_height);
    3.26  			return -1;
    3.27  		}
    3.28  		swapchain = sc;
    3.29  
    3.30  		free(swapchain_images);
    3.31 +		swapchain_size = 2;
    3.32  		swapchain_images = vku_get_swapchain_images(sc, 0);
    3.33  		next_swapchain_image = vku_get_next_image(swapchain);
    3.34  
    3.35 -		if(!vkrpass) {
    3.36 -			if(!(vkrpass = vku_create_renderpass(VK_FORMAT_R8G8B8_UNORM, VK_FORMAT_UNDEFINED))) {
    3.37 -				abort();
    3.38 +		if(!swapchain_views) {
    3.39 +			if(!(swapchain_views = calloc(swapchain_size, sizeof *swapchain_views))) {
    3.40 +				fprintf(stderr, "Failed to allocate image views\n");
    3.41 +				return -1;
    3.42  			}
    3.43  		}
    3.44  
    3.45 +		for(i=0; i<swapchain_size; i++) {
    3.46 +			if(swapchain_views[i]) {
    3.47 +				vku_destroy_view(swapchain_views[i]);
    3.48 +			}
    3.49 +			swapchain_views[i] = vku_create_view(swapchain_images[i], fmt);
    3.50 +		}
    3.51 +
    3.52 +		if(!swapchain_framebuf) {
    3.53 +			if(!(swapchain_framebuf = calloc(swapchain_size, sizeof *swapchain_framebuf))) {
    3.54 +				fprintf(stderr, "Failed to allocate framebuffers\n");
    3.55 +				return -1;
    3.56 +			}
    3.57 +		}
    3.58 +
    3.59 +		for(i=0; i<swapchain_size; i++) {
    3.60 +			if(swapchain_framebuf[i]) {
    3.61 +				vku_destroy_framebuffer(swapchain_framebuf[i]);
    3.62 +			}
    3.63 +			swapchain_framebuf[i] = vku_create_framebuffer(swapchain_views[i],
    3.64 +					win_width, win_height, vkrpass);
    3.65 +		}
    3.66 +
    3.67  		if(cb.reshape) {
    3.68  			cb.reshape(win_width, win_height);
    3.69  		}