vulkan_test2

changeset 15:196122a2b8c2

...
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 26 Jun 2018 08:39:30 +0300
parents 9fb6c24691ea
children 236f923a00a3
files src/main.c src/vku.c src/vku.h
diffstat 3 files changed, 104 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/src/main.c	Tue Jun 26 07:20:03 2018 +0300
     1.2 +++ b/src/main.c	Tue Jun 26 08:39:30 2018 +0300
     1.3 @@ -55,6 +55,16 @@
     1.4  	vkgl_clear_color(1, 0, 0, 1);
     1.5  	vkgl_clear(VKGL_COLOR_BUFFER_BIT);
     1.6  
     1.7 +	vku_begin_cmdbuf(vkcmdbuf, VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT);
     1.8 +	vku_begin_renderpass(vkcmdbuf, vkrpass, swapchain_framebuf[next_swapchain_image],
     1.9 +			VK_SUBPASS_CONTENTS_INLINE);
    1.10 +	vkCmdBindPipeline(vkcmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline);
    1.11 +	vkCmdDraw(vkcmdbuf, 3, 1, 0, 0);
    1.12 +	vku_end_renderpass(vkcmdbuf);
    1.13 +	vku_end_cmdbuf(vkcmdbuf);
    1.14 +
    1.15 +	vku_submit_cmdbuf(vkq, vkcmdbuf, 0);
    1.16 +
    1.17  	wsys_swap_buffers();
    1.18  }
    1.19  
     2.1 --- a/src/vku.c	Tue Jun 26 07:20:03 2018 +0300
     2.2 +++ b/src/vku.c	Tue Jun 26 08:39:30 2018 +0300
     2.3 @@ -12,6 +12,10 @@
     2.4  static int ver_patch(uint32_t ver);
     2.5  static const char *mem_size_str(long sz);
     2.6  
     2.7 +static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback_thunk(VkDebugReportFlagsEXT flags,
     2.8 +		VkDebugReportObjectTypeEXT otype, uint64_t obj, size_t loc, int32_t code,
     2.9 +		const char *layer_prefix, const char *msg, void *udata);
    2.10 +
    2.11  VkInstance vk;
    2.12  VkDevice vkdev;
    2.13  VkQueue vkq;
    2.14 @@ -22,6 +26,13 @@
    2.15  static VkExtensionProperties *vkext, *vkdevext;
    2.16  static uint32_t vkext_count, vkdevext_count;
    2.17  
    2.18 +static VkDebugReportCallbackEXT debug_callback_obj;
    2.19 +static VkResult (*vk_create_debug_report_callback)(VkInstance,
    2.20 +		const VkDebugReportCallbackCreateInfoEXT*, const VkAllocationCallbacks*,
    2.21 +		VkDebugReportCallbackEXT*);
    2.22 +static void (*user_dbg_callback)(const char*, void*);
    2.23 +static void *user_dbg_callback_data;
    2.24 +
    2.25  
    2.26  int vku_have_extension(const char *name)
    2.27  {
    2.28 @@ -83,10 +94,30 @@
    2.29  	return 0;
    2.30  }
    2.31  
    2.32 +void vku_set_debug_callback(void (*func)(const char*, void*), void *cls)
    2.33 +{
    2.34 +	if(!debug_callback_obj && vk_create_debug_report_callback) {
    2.35 +		VkDebugReportCallbackCreateInfoEXT foo;
    2.36 +
    2.37 +		memset(&foo, 0, sizeof foo);
    2.38 +		foo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
    2.39 +		foo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
    2.40 +		foo.pfnCallback = debug_callback_thunk;
    2.41 +
    2.42 +		vk_create_debug_report_callback(vk, &foo, 0, &debug_callback_obj);
    2.43 +	}
    2.44 +
    2.45 +	user_dbg_callback = func;
    2.46 +	user_dbg_callback_data = cls;
    2.47 +}
    2.48 +
    2.49 +
    2.50  int vku_create_dev(void)
    2.51  {
    2.52  	int i, j;
    2.53 +	uint32_t nlayers;
    2.54  	VkInstanceCreateInfo inst_info;
    2.55 +	VkLayerProperties *layers;
    2.56  	VkDeviceCreateInfo dev_info;
    2.57  	VkDeviceQueueCreateInfo queue_info;
    2.58  	VkCommandPoolCreateInfo cmdpool_info;
    2.59 @@ -97,11 +128,15 @@
    2.60  #ifdef VK_USE_PLATFORM_XLIB_KHR
    2.61  		"VK_KHR_xlib_surface",
    2.62  #endif
    2.63 -		"VK_KHR_surface"
    2.64 +		"VK_KHR_surface",
    2.65 +		"VK_EXT_debug_report"
    2.66  	};
    2.67  	static const char *devext_names[] = {
    2.68  		"VK_KHR_swapchain"
    2.69  	};
    2.70 +	static const char *layer_names[] = {
    2.71 +		"VK_LAYER_LUNARG_standard_validation"
    2.72 +	};
    2.73  
    2.74  	sel_dev = -1;
    2.75  	sel_qfamily = -1;
    2.76 @@ -113,10 +148,22 @@
    2.77  		}
    2.78  	}
    2.79  
    2.80 +	/* enumerate available validation layers */
    2.81 +	vkEnumerateInstanceLayerProperties(&nlayers, 0);
    2.82 +	layers = alloca(nlayers * sizeof *layers);
    2.83 +	vkEnumerateInstanceLayerProperties(&nlayers, layers);
    2.84 +
    2.85 +	printf("Available validation layers:\n");
    2.86 +	for(i=0; i<(int)nlayers; i++) {
    2.87 +		printf(" %s\n", layers[i].layerName);
    2.88 +	}
    2.89 +
    2.90  	memset(&inst_info, 0, sizeof inst_info);
    2.91  	inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    2.92  	inst_info.ppEnabledExtensionNames = ext_names;
    2.93  	inst_info.enabledExtensionCount = sizeof ext_names / sizeof *ext_names;
    2.94 +	inst_info.ppEnabledLayerNames = layer_names;
    2.95 +	inst_info.enabledLayerCount = sizeof layer_names / sizeof *layer_names;
    2.96  
    2.97  	if(vkCreateInstance(&inst_info, 0, &vk) != 0) {
    2.98  		fprintf(stderr, "failed to create vulkan instance\n");
    2.99 @@ -124,6 +171,13 @@
   2.100  	}
   2.101  	printf("created vulkan instance\n");
   2.102  
   2.103 +	if(!(vk_create_debug_report_callback = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(vk, "vkCreateDebugReportCallbackEXT"))) {
   2.104 +		fprintf(stderr, "FUCK EVERYTHING\n");
   2.105 +		return -1;
   2.106 +	}
   2.107 +	vku_set_debug_callback(user_dbg_callback, user_dbg_callback_data);	/* set debug callback */
   2.108 +
   2.109 +
   2.110  	if(vkEnumeratePhysicalDevices(vk, &num_devices, 0) != 0) {
   2.111  		fprintf(stderr, "failed to enumerate vulkan physical devices\n");
   2.112  		return -1;
   2.113 @@ -544,6 +598,27 @@
   2.114  	vkDestroyRenderPass(vkdev, rpass, 0);
   2.115  }
   2.116  
   2.117 +void vku_begin_renderpass(VkCommandBuffer cmdbuf, VkRenderPass rpass, VkFramebuffer fb,
   2.118 +		VkSubpassContents cont)
   2.119 +{
   2.120 +	VkRenderPassBeginInfo rpinf;
   2.121 +
   2.122 +	memset(&rpinf, 0, sizeof rpinf);
   2.123 +	rpinf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
   2.124 +	rpinf.renderPass = rpass;
   2.125 +	rpinf.framebuffer = fb;
   2.126 +	rpinf.renderArea.offset.x = vkvport.x;
   2.127 +	rpinf.renderArea.offset.y = vkvport.y;
   2.128 +	rpinf.renderArea.extent.width = vkvport.width;
   2.129 +	rpinf.renderArea.extent.height = vkvport.height;
   2.130 +
   2.131 +	vkCmdBeginRenderPass(cmdbuf, &rpinf, cont);
   2.132 +}
   2.133 +
   2.134 +void vku_end_renderpass(VkCommandBuffer cmdbuf)
   2.135 +{
   2.136 +	vkCmdEndRenderPass(cmdbuf);
   2.137 +}
   2.138  
   2.139  #ifdef VK_USE_PLATFORM_XLIB_KHR
   2.140  int vku_xlib_usable_visual(Display *dpy, VisualID vid)
   2.141 @@ -666,3 +741,16 @@
   2.142  	sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]);
   2.143  	return str;
   2.144  }
   2.145 +
   2.146 +static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback_thunk(VkDebugReportFlagsEXT flags,
   2.147 +		VkDebugReportObjectTypeEXT otype, uint64_t obj, size_t loc, int32_t code,
   2.148 +		const char *layer_prefix, const char *msg, void *udata)
   2.149 +{
   2.150 +	if(user_dbg_callback) {
   2.151 +		user_dbg_callback(msg, user_dbg_callback_data);
   2.152 +	} else {
   2.153 +		fprintf(stderr, "VK DEBUG (%s): %s\n", layer_prefix, msg);
   2.154 +	}
   2.155 +
   2.156 +	return VK_TRUE;
   2.157 +}
     3.1 --- a/src/vku.h	Tue Jun 26 07:20:03 2018 +0300
     3.2 +++ b/src/vku.h	Tue Jun 26 08:39:30 2018 +0300
     3.3 @@ -33,6 +33,8 @@
     3.4  int vku_have_extension(const char *name);
     3.5  int vku_have_device_extension(const char *name);
     3.6  
     3.7 +void vku_set_debug_callback(void (*func)(const char*, void*), void *cls);
     3.8 +
     3.9  int vku_create_dev(void);
    3.10  void vku_cleanup(void);
    3.11  
    3.12 @@ -65,6 +67,9 @@
    3.13  
    3.14  VkRenderPass vku_create_renderpass(VkFormat cfmt, VkFormat dsfmt);
    3.15  void vku_destroy_renderpass(VkRenderPass rpass);
    3.16 +void vku_begin_renderpass(VkCommandBuffer cmdbuf, VkRenderPass rpass, VkFramebuffer fb,
    3.17 +		VkSubpassContents cont);
    3.18 +void vku_end_renderpass(VkCommandBuffer cmdbuf);
    3.19  
    3.20  /* platform-specific */
    3.21  #ifdef VK_USE_PLATFORM_XLIB_KHR