vulkan_test2

changeset 5:cec4b0e7fce8

created swapchain
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Sep 2017 17:48:18 +0300
parents c31c4115d44a
children 1dd2c7398afc
files src/vku.c src/vku.h src/wsys_x11.c
diffstat 3 files changed, 80 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/src/vku.c	Fri Sep 22 15:26:29 2017 +0300
     1.2 +++ b/src/vku.c	Fri Sep 22 17:48:18 2017 +0300
     1.3 @@ -99,6 +99,9 @@
     1.4  #endif
     1.5  		"VK_KHR_surface"
     1.6  	};
     1.7 +	static const char *devext_names[] = {
     1.8 +		"VK_KHR_swapchain"
     1.9 +	};
    1.10  
    1.11  	sel_dev = -1;
    1.12  	sel_qfamily = -1;
    1.13 @@ -189,6 +192,14 @@
    1.14  		return -1;
    1.15  	}
    1.16  
    1.17 +	for(i=0; i<sizeof devext_names / sizeof *devext_names; i++) {
    1.18 +		if(!vku_have_device_extension(devext_names[i])) {
    1.19 +			fprintf(stderr, "required extension (%s) not found on the selected device (%d)\n",
    1.20 +					ext_names[i], sel_dev);
    1.21 +			return -1;
    1.22 +		}
    1.23 +	}
    1.24 +
    1.25  	/* create device & command queue */
    1.26  	memset(&queue_info, 0, sizeof queue_info);
    1.27  	queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
    1.28 @@ -200,6 +211,8 @@
    1.29  	dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
    1.30  	dev_info.queueCreateInfoCount = 1;
    1.31  	dev_info.pQueueCreateInfos = &queue_info;
    1.32 +	dev_info.enabledExtensionCount = sizeof devext_names / sizeof *devext_names;
    1.33 +	dev_info.ppEnabledExtensionNames = devext_names;
    1.34  
    1.35  	if(vkCreateDevice(phys_devices[sel_dev], &dev_info, 0, &vkdev) != 0) {
    1.36  		fprintf(stderr, "failed to create device %d\n", sel_dev);
    1.37 @@ -298,6 +311,34 @@
    1.38  	vkQueueSubmit(q, 1, &info, done_fence);
    1.39  }
    1.40  
    1.41 +VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n,
    1.42 +		VkPresentModeKHR pmode, VkSwapchainKHR prev)
    1.43 +{
    1.44 +	VkSwapchainKHR sc;
    1.45 +	VkSwapchainCreateInfoKHR inf;
    1.46 +
    1.47 +	memset(&inf, 0, sizeof inf);
    1.48 +	inf.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    1.49 +	inf.surface = surf;
    1.50 +	inf.minImageCount = n;
    1.51 +	inf.imageFormat = VK_FORMAT_B8G8R8A8_UNORM;	/* TODO enumerate and choose */
    1.52 +	inf.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
    1.53 +	inf.imageExtent.width = xsz;
    1.54 +	inf.imageExtent.height = ysz;
    1.55 +	inf.imageArrayLayers = 1;
    1.56 +	inf.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
    1.57 +	inf.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;	/* XXX make this an option? */
    1.58 +	inf.preTransform = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
    1.59 +	inf.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
    1.60 +	inf.presentMode = pmode;
    1.61 +	inf.oldSwapchain = prev;
    1.62 +
    1.63 +	if(vkCreateSwapchainKHR(vkdev, &inf, 0, &sc) != 0) {
    1.64 +		return 0;
    1.65 +	}
    1.66 +	return sc;
    1.67 +}
    1.68 +
    1.69  struct vku_buffer *vku_create_buffer(int sz, unsigned int usage)
    1.70  {
    1.71  	struct vku_buffer *buf;
    1.72 @@ -342,11 +383,29 @@
    1.73  }
    1.74  
    1.75  #ifdef VK_USE_PLATFORM_XLIB_KHR
    1.76 +
    1.77  int vku_xlib_usable_visual(Display *dpy, VisualID vid)
    1.78  {
    1.79  	return vkGetPhysicalDeviceXlibPresentationSupportKHR(phys_devices[sel_dev],
    1.80  			sel_qfamily, dpy, vid);
    1.81  }
    1.82 +
    1.83 +VkSurfaceKHR vku_xlib_create_surface(Display *dpy, Window win)
    1.84 +{
    1.85 +	VkSurfaceKHR surf;
    1.86 +	VkXlibSurfaceCreateInfoKHR inf;
    1.87 +
    1.88 +	memset(&inf, 0, sizeof inf);
    1.89 +	inf.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
    1.90 +	inf.dpy = dpy;
    1.91 +	inf.window = win;
    1.92 +
    1.93 +	if(vkCreateXlibSurfaceKHR(vk, &inf, 0, &surf) != 0) {
    1.94 +		return 0;
    1.95 +	}
    1.96 +	return surf;
    1.97 +}
    1.98 +
    1.99  #endif	/* VK_USE_PLATFORM_XLIB_KHR */
   1.100  
   1.101  static const char *get_device_name(VkPhysicalDeviceType type)
     2.1 --- a/src/vku.h	Fri Sep 22 15:26:29 2017 +0300
     2.2 +++ b/src/vku.h	Fri Sep 22 17:48:18 2017 +0300
     2.3 @@ -34,6 +34,9 @@
     2.4  
     2.5  void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence);
     2.6  
     2.7 +VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n,
     2.8 +		VkPresentModeKHR pmode, VkSwapchainKHR prev);
     2.9 +
    2.10  struct vku_buffer *vku_create_buffer(int sz, unsigned int usage);
    2.11  void vku_destroy_buffer(struct vku_buffer *buf);
    2.12  
    2.13 @@ -44,6 +47,7 @@
    2.14  #ifdef VK_USE_PLATFORM_XLIB_KHR
    2.15  #include <X11/Xlib.h>
    2.16  int vku_xlib_usable_visual(Display *dpy, VisualID vid);
    2.17 +VkSurfaceKHR vku_xlib_create_surface(Display *dpy, Window win);
    2.18  #endif
    2.19  
    2.20  #endif	/* VKU_H_ */
     3.1 --- a/src/wsys_x11.c	Fri Sep 22 15:26:29 2017 +0300
     3.2 +++ b/src/wsys_x11.c	Fri Sep 22 17:48:18 2017 +0300
     3.3 @@ -24,6 +24,8 @@
     3.4  
     3.5  static Display *dpy;
     3.6  static Window win;
     3.7 +static VkSurfaceKHR surf;
     3.8 +static VkSwapchainKHR swapchain;
     3.9  static Atom xa_wm_delete;
    3.10  static int win_width, win_height;
    3.11  static int win_mapped;
    3.12 @@ -87,6 +89,14 @@
    3.13  	XSelectInput(dpy, win, evmask);
    3.14  	XSetWMProtocols(dpy, win, &xa_wm_delete, 1);
    3.15  
    3.16 +	if(!(surf = vku_xlib_create_surface(dpy, win))) {
    3.17 +		fprintf(stderr, "failed to create vulkan surface for the window\n");
    3.18 +		XDestroyWindow(dpy, win);
    3.19 +		XCloseDisplay(dpy);
    3.20 +		return -1;
    3.21 +	}
    3.22 +	/* swapchain gets created before the first reshape invocation */
    3.23 +
    3.24  	wsys_set_window_title("X11 window");
    3.25  	XMapWindow(dpy, win);
    3.26  
    3.27 @@ -206,6 +216,13 @@
    3.28  	XEvent xev;
    3.29  
    3.30  	if(pending & RESHAPE) {
    3.31 +		VkSwapchainKHR sc;
    3.32 +		if(!(sc = vku_create_swapchain(surf, win_width, win_height, 2, VK_PRESENT_MODE_FIFO_KHR, swapchain))) {
    3.33 +			fprintf(stderr, "Failed to create %dx%d double-buffered swapchain\n", win_width, win_height);
    3.34 +			return -1;
    3.35 +		}
    3.36 +		swapchain = sc;
    3.37 +
    3.38  		if(cb.reshape) {
    3.39  			cb.reshape(win_width, win_height);
    3.40  		}