vulkan_test2

changeset 6:1dd2c7398afc

swapchain images and other tales of woe
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Sep 2017 18:13:03 +0300
parents cec4b0e7fce8
children 20eb42197ab8
files src/vku.c src/vku.h src/wsys_x11.c
diffstat 3 files changed, 54 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/src/vku.c	Fri Sep 22 17:48:18 2017 +0300
     1.2 +++ b/src/vku.c	Fri Sep 22 18:13:03 2017 +0300
     1.3 @@ -339,6 +339,49 @@
     1.4  	return sc;
     1.5  }
     1.6  
     1.7 +VkImage *vku_get_swapchain_images(VkSwapchainKHR sc, int *count)
     1.8 +{
     1.9 +	uint32_t nimg;
    1.10 +	VkImage *images;
    1.11 +
    1.12 +	if(vkGetSwapchainImagesKHR(vkdev, sc, &nimg, 0) != 0) {
    1.13 +		return 0;
    1.14 +	}
    1.15 +	if(!(images = malloc(nimg * sizeof *images))) {
    1.16 +		return 0;
    1.17 +	}
    1.18 +	vkGetSwapchainImagesKHR(vkdev, sc, &nimg, images);
    1.19 +
    1.20 +	if(count) *count = (int)nimg;
    1.21 +	return images;
    1.22 +}
    1.23 +
    1.24 +int vku_get_next_image(VkSwapchainKHR sc)
    1.25 +{
    1.26 +	uint32_t next;
    1.27 +
    1.28 +	if(vkAcquireNextImageKHR(vkdev, sc, UINT64_MAX, 0, 0, &next) != 0) {
    1.29 +		return -1;
    1.30 +	}
    1.31 +	return (int)next;
    1.32 +}
    1.33 +
    1.34 +void vku_present(VkSwapchainKHR sc, int img_idx)
    1.35 +{
    1.36 +	VkPresentInfoKHR inf;
    1.37 +	VkResult res;
    1.38 +	uint32_t index = img_idx;
    1.39 +
    1.40 +	memset(&inf, 0, sizeof inf);
    1.41 +	inf.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
    1.42 +	inf.swapchainCount = 1;
    1.43 +	inf.pSwapchains = &sc;
    1.44 +	inf.pImageIndices = &index;
    1.45 +	inf.pResults = &res;
    1.46 +
    1.47 +	vkQueuePresentKHR(vkq, &inf);
    1.48 +}
    1.49 +
    1.50  struct vku_buffer *vku_create_buffer(int sz, unsigned int usage)
    1.51  {
    1.52  	struct vku_buffer *buf;
     2.1 --- a/src/vku.h	Fri Sep 22 17:48:18 2017 +0300
     2.2 +++ b/src/vku.h	Fri Sep 22 18:13:03 2017 +0300
     2.3 @@ -36,6 +36,9 @@
     2.4  
     2.5  VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n,
     2.6  		VkPresentModeKHR pmode, VkSwapchainKHR prev);
     2.7 +VkImage *vku_get_swapchain_images(VkSwapchainKHR sc, int *count);
     2.8 +int vku_get_next_image(VkSwapchainKHR sc);
     2.9 +void vku_present(VkSwapchainKHR sc, int img_idx);
    2.10  
    2.11  struct vku_buffer *vku_create_buffer(int sz, unsigned int usage);
    2.12  void vku_destroy_buffer(struct vku_buffer *buf);
     3.1 --- a/src/wsys_x11.c	Fri Sep 22 17:48:18 2017 +0300
     3.2 +++ b/src/wsys_x11.c	Fri Sep 22 18:13:03 2017 +0300
     3.3 @@ -26,6 +26,8 @@
     3.4  static Window win;
     3.5  static VkSurfaceKHR surf;
     3.6  static VkSwapchainKHR swapchain;
     3.7 +static VkImage *swapchain_images;
     3.8 +static int next_swapchain_image;
     3.9  static Atom xa_wm_delete;
    3.10  static int win_width, win_height;
    3.11  static int win_mapped;
    3.12 @@ -198,7 +200,8 @@
    3.13  
    3.14  void wsys_swap_buffers(void)
    3.15  {
    3.16 -	/* TODO */
    3.17 +	vku_present(swapchain, next_swapchain_image);
    3.18 +	next_swapchain_image = vku_get_next_image(swapchain);
    3.19  }
    3.20  
    3.21  void wsys_redisplay(void)
    3.22 @@ -223,6 +226,10 @@
    3.23  		}
    3.24  		swapchain = sc;
    3.25  
    3.26 +		free(swapchain_images);
    3.27 +		swapchain_images = vku_get_swapchain_images(sc, 0);
    3.28 +		next_swapchain_image = vku_get_next_image(swapchain);
    3.29 +
    3.30  		if(cb.reshape) {
    3.31  			cb.reshape(win_width, win_height);
    3.32  		}