vulkan_test2

diff src/vku.c @ 17:f8bd29f124a8

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 27 Jun 2018 01:57:55 +0300
parents 236f923a00a3
children
line diff
     1.1 --- a/src/vku.c	Tue Jun 26 08:42:12 2018 +0300
     1.2 +++ b/src/vku.c	Wed Jun 27 01:57:55 2018 +0300
     1.3 @@ -27,9 +27,8 @@
     1.4  static uint32_t vkext_count, vkdevext_count;
     1.5  
     1.6  static VkDebugReportCallbackEXT debug_callback_obj;
     1.7 -static VkResult (*vk_create_debug_report_callback)(VkInstance,
     1.8 -		const VkDebugReportCallbackCreateInfoEXT*, const VkAllocationCallbacks*,
     1.9 -		VkDebugReportCallbackEXT*);
    1.10 +static PFN_vkCreateDebugReportCallbackEXT vk_create_debug_report_callback;
    1.11 +static PFN_vkDestroyDebugReportCallbackEXT vk_destroy_debug_report_callback;
    1.12  static void (*user_dbg_callback)(const char*, void*);
    1.13  static void *user_dbg_callback_data;
    1.14  
    1.15 @@ -137,7 +136,8 @@
    1.16  	static const char *layer_names[] = {
    1.17  		"VK_LAYER_LUNARG_standard_validation",
    1.18  		"VK_LAYER_LUNARG_parameter_validation",
    1.19 -		"VK_LAYER_LUNARG_core_validation"
    1.20 +		"VK_LAYER_LUNARG_core_validation",
    1.21 +		"VK_LAYER_LUNARG_image"
    1.22  	};
    1.23  
    1.24  	sel_dev = -1;
    1.25 @@ -177,6 +177,7 @@
    1.26  		fprintf(stderr, "FUCK EVERYTHING\n");
    1.27  		return -1;
    1.28  	}
    1.29 +	vk_destroy_debug_report_callback = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(vk, "vkDestroyDebugReportCallbackEXT");
    1.30  	vku_set_debug_callback(user_dbg_callback, user_dbg_callback_data);	/* set debug callback */
    1.31  
    1.32  
    1.33 @@ -289,11 +290,20 @@
    1.34  		return -1;
    1.35  	}
    1.36  
    1.37 -	if(!(vkcmdbuf = vku_alloc_cmdbuf(vkcmdpool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) {
    1.38 -		fprintf(stderr, "failed to create primary command buffer\n");
    1.39 +	/* XXX hardcoded 2 command buffers for a swapchain with 2 images */
    1.40 +	if(!(swapchain_cmdbuf = malloc(2 * sizeof *swapchain_cmdbuf))) {
    1.41 +		fprintf(stderr, "failed to allocate command buffer array\n");
    1.42  		return -1;
    1.43  	}
    1.44  
    1.45 +	for(i=0; i<2; i++) {
    1.46 +		if(!(swapchain_cmdbuf[i] = vku_alloc_cmdbuf(vkcmdpool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) {
    1.47 +			fprintf(stderr, "failed to create primary command buffer\n");
    1.48 +			return -1;
    1.49 +		}
    1.50 +	}
    1.51 +	vkcmdbuf = swapchain_cmdbuf[0];
    1.52 +
    1.53  	return 0;
    1.54  }
    1.55  
    1.56 @@ -303,6 +313,7 @@
    1.57  		vkDeviceWaitIdle(vkdev);
    1.58  		vkDestroyCommandPool(vkdev, vkcmdpool, 0);
    1.59  		vkDestroyDevice(vkdev, 0);
    1.60 +		vk_destroy_debug_report_callback(vk, debug_callback_obj, 0);
    1.61  		vkDestroyInstance(vk, 0);
    1.62  		vk = 0;
    1.63  	}
    1.64 @@ -355,7 +366,8 @@
    1.65  	vkResetCommandBuffer(buf, 0);
    1.66  }
    1.67  
    1.68 -void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence)
    1.69 +void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkSemaphore sem_wait,
    1.70 +		VkSemaphore sem_done, VkFence done_fence)
    1.71  {
    1.72  	VkSubmitInfo info;
    1.73  
    1.74 @@ -363,6 +375,14 @@
    1.75  	info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
    1.76  	info.commandBufferCount = 1;
    1.77  	info.pCommandBuffers = &buf;
    1.78 +	if(sem_wait) {
    1.79 +		info.pWaitSemaphores = &sem_wait;
    1.80 +		info.waitSemaphoreCount = 1;
    1.81 +	}
    1.82 +	if(sem_done) {
    1.83 +		info.pSignalSemaphores = &sem_done;
    1.84 +		info.signalSemaphoreCount = 1;
    1.85 +	}
    1.86  
    1.87  	vkQueueSubmit(q, 1, &info, done_fence);
    1.88  }
    1.89 @@ -412,13 +432,14 @@
    1.90  	return images;
    1.91  }
    1.92  
    1.93 -int vku_get_next_image(VkSwapchainKHR sc)
    1.94 +int vku_get_next_image(VkSwapchainKHR sc, VkSemaphore semdone)
    1.95  {
    1.96  	uint32_t next;
    1.97  
    1.98 -	if(vkAcquireNextImageKHR(vkdev, sc, UINT64_MAX, 0, 0, &next) != 0) {
    1.99 +	if(vkAcquireNextImageKHR(vkdev, sc, UINT64_MAX, semdone, 0, &next) != 0) {
   1.100  		return -1;
   1.101  	}
   1.102 +	vkcmdbuf = swapchain_cmdbuf[next];
   1.103  	return (int)next;
   1.104  }
   1.105  
   1.106 @@ -478,7 +499,7 @@
   1.107  	vkDestroyFramebuffer(vkdev, fb, 0);
   1.108  }
   1.109  
   1.110 -void vku_present(VkSwapchainKHR sc, int img_idx)
   1.111 +void vku_present(VkSwapchainKHR sc, int img_idx, VkSemaphore sem_wait)
   1.112  {
   1.113  	VkPresentInfoKHR inf;
   1.114  	VkResult res;
   1.115 @@ -490,6 +511,10 @@
   1.116  	inf.pSwapchains = &sc;
   1.117  	inf.pImageIndices = &index;
   1.118  	inf.pResults = &res;
   1.119 +	if(sem_wait) {
   1.120 +		inf.pWaitSemaphores = &sem_wait;
   1.121 +		inf.waitSemaphoreCount = 1;
   1.122 +	}
   1.123  
   1.124  	vkQueuePresentKHR(vkq, &inf);
   1.125  }
   1.126 @@ -622,6 +647,53 @@
   1.127  	vkCmdEndRenderPass(cmdbuf);
   1.128  }
   1.129  
   1.130 +VkSemaphore vku_create_semaphore(void)
   1.131 +{
   1.132 +	VkSemaphore s;
   1.133 +	VkSemaphoreCreateInfo sinf;
   1.134 +
   1.135 +	memset(&sinf, 0, sizeof sinf);
   1.136 +	sinf.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
   1.137 +
   1.138 +	if(vkCreateSemaphore(vkdev, &sinf, 0, &s) != 0) {
   1.139 +		fprintf(stderr, "vku_create_semaphore failed\n");
   1.140 +		return 0;
   1.141 +	}
   1.142 +	return s;
   1.143 +}
   1.144 +
   1.145 +void vku_destroy_semaphore(VkSemaphore s)
   1.146 +{
   1.147 +	vkDestroySemaphore(vkdev, s, 0);
   1.148 +}
   1.149 +
   1.150 +VkFence vku_create_fence(void)
   1.151 +{
   1.152 +	VkFence f;
   1.153 +	VkFenceCreateInfo finf;
   1.154 +
   1.155 +	memset(&finf, 0, sizeof finf);
   1.156 +	finf.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
   1.157 +
   1.158 +	if(vkCreateFence(vkdev, &finf, 0, &f) != 0) {
   1.159 +		fprintf(stderr, "vku_create_fence failed\n");
   1.160 +		return 0;
   1.161 +	}
   1.162 +	return f;
   1.163 +}
   1.164 +
   1.165 +void vku_destroy_fence(VkFence f)
   1.166 +{
   1.167 +	vkDestroyFence(vkdev, f, 0);
   1.168 +}
   1.169 +
   1.170 +void vku_wait_fence(VkFence f)
   1.171 +{
   1.172 +	vkWaitForFences(vkdev, 1, &f, VK_TRUE, UINT64_MAX);
   1.173 +	vkResetFences(vkdev, 1, &f);
   1.174 +}
   1.175 +
   1.176 +
   1.177  #ifdef VK_USE_PLATFORM_XLIB_KHR
   1.178  int vku_xlib_usable_visual(Display *dpy, VisualID vid)
   1.179  {