[Piglit] [v3 04/11] ext_memory_object: Support for setting up vulkan device

Pohjolainen, Topi topi.pohjolainen at gmail.com
Thu Dec 21 16:17:41 UTC 2017


On Thu, Dec 21, 2017 at 11:03:15AM -0500, Andres Rodriguez wrote:
> 
> 
> On 2017-12-21 07:02 AM, Topi Pohjolainen wrote:
> > v2:
> >     - drop magic number in allocator (Andres)
> >     - fix indentation (Andres)
> >     - allow more than one physical device (Andres)
> >     - fix indentation (Andres)
> >     - use better names for static variables (Andres). Here I
> >       simply shose to use structs and allocate them dynamically
> >       instead of using static variables at all. This was
> >       actually on my todo list, just forgot it.
> > 
> > CC: Andres Rodriguez <andresx7 at gmail.com>
> > Signed-off-by: Topi Pohjolainen <topi.pohjolainen at intel.com>
> > ---
> >   tests/spec/ext_memory_object/vk_common.c | 236 +++++++++++++++++++++++++++++++
> >   tests/spec/ext_memory_object/vk_common.h |  48 +++++++
> >   2 files changed, 284 insertions(+)
> >   create mode 100644 tests/spec/ext_memory_object/vk_common.c
> >   create mode 100644 tests/spec/ext_memory_object/vk_common.h
> > 
> > diff --git a/tests/spec/ext_memory_object/vk_common.c b/tests/spec/ext_memory_object/vk_common.c
> > new file mode 100644
> > index 000000000..2c3e8272e
> > --- /dev/null
> > +++ b/tests/spec/ext_memory_object/vk_common.c
> > @@ -0,0 +1,236 @@
> > +/*
> > + * Copyright 2017 Intel Corporation
> > + *
> > + * Permission is hereby granted, free of charge, to any person obtaining a
> > + * copy of this software and associated documentation files (the "Software"),
> > + * to deal in the Software without restriction, including without limitation
> > + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> > + * and/or sell copies of the Software, and to permit persons to whom the
> > + * Software is furnished to do so, subject to the following conditions:
> > + *
> > + * The above copyright notice and this permission notice (including the next
> > + * paragraph) shall be included in all copies or substantial portions of the
> > + * Software.
> > + *
> > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> > + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> > + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
> > + * DEALINGS IN THE SOFTWARE.
> > + */
> > +
> > +#include "vk_common.h"
> > +#include "piglit-util-gl.h"
> > +
> > +static void *
> > +test_vk_alloc(void *user_data, size_t size, size_t alignment,
> > +              VkSystemAllocationScope scope)
> > +{
> > +	assert(user_data == (void *)0xdeadbeef);
> > +	void *mem = malloc(size);
> > +	return mem;
> > +}
> > +
> > +static void *
> > +test_vk_realloc(void *user_data, void *orig, size_t size,
> > +                size_t alignment, VkSystemAllocationScope scope)
> > +{
> > +	assert(user_data == (void *)0xdeadbeef);
> > +	return realloc(orig, size);
> > +}
> > +
> > +static void
> > +test_vk_free(void *user_data, void *mem)
> > +{
> > +	assert(user_data == (void *)0xdeadbeef);
> > +	free(mem);
> > +}
> > +
> > +static void
> > +test_vk_dummy_notify(void *user_ata, size_t size,
> > +                     VkInternalAllocationType allocation_type,
> > +                     VkSystemAllocationScope allocation_scope)
> > +{
> > +}
> > +
> > +static const VkAllocationCallbacks test_alloc_cb = {
> > +	.pUserData = (void *)0xdeadbeef,
> > +	.pfnAllocation = test_vk_alloc,
> > +	.pfnReallocation = test_vk_realloc,
> > +	.pfnFree = test_vk_free,
> > +	.pfnInternalAllocation = test_vk_dummy_notify,
> > +	.pfnInternalFree = test_vk_dummy_notify
> > +};
> > +
> > +static VkInstance
> > +create_vk_instance(void)
> > +{
> > +	const VkInstanceCreateInfo info = {
> > +		.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
> > +		.pApplicationInfo = &(VkApplicationInfo) {
> > +			.pApplicationName = "vk_core_renderer",
> > +			.apiVersion = VK_MAKE_VERSION(1, 0, 0),
> > +	    	},
> > +	};
> > +
> > +	VkInstance inst = VK_NULL_HANDLE;
> > +	if (vkCreateInstance(&info, &test_alloc_cb, &inst) != VK_SUCCESS)
> > +		inst = VK_NULL_HANDLE;
> > +
> > +	return inst;
> > +}
> > +
> > +static VkPhysicalDevice
> > +create_vk_phys_dev(VkInstance inst)
> > +{
> > +	unsigned count = 0;
> > +	VkPhysicalDevice first = VK_NULL_HANDLE;
> > +	VkPhysicalDevice *all;
> > +
> > +	if (vkEnumeratePhysicalDevices(inst, &count, NULL) != VK_SUCCESS ||
> > +	    count == 0)
> > +		return VK_NULL_HANDLE;
> > +
> > +	all = malloc(count * sizeof(VkPhysicalDevice));
> > +
> > +	if (vkEnumeratePhysicalDevices(inst, &count, all) == VK_SUCCESS)
> > +		first = all[0];
> > +
> > +	free(all);
> > +
> > +	return first;
> > +}
> > +
> > +static VkDevice
> > +create_vk_device(VkPhysicalDevice phys_dev)
> > +{
> > +	const VkDeviceCreateInfo info = {
> > +		.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
> > +		.queueCreateInfoCount = 1,
> > +		.pQueueCreateInfos = &(VkDeviceQueueCreateInfo) {
> > +			.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
> > +			.queueFamilyIndex = 0,
> > +			.queueCount = 1,
> > +			.pQueuePriorities = (float[]) {1.0f},
> > +		},
> > +	};
> > +        VkDevice dev = VK_NULL_HANDLE;
> > +
> > +	if (vkCreateDevice(phys_dev, &info, NULL, &dev) != VK_SUCCESS)
> > +		dev = VK_NULL_HANDLE;
> > +
> > +	return dev;
> > +}
> > +
> > +static VkPipelineCache
> > +vk_create_pipeline_cache(VkDevice dev)
> > +{
> > +	const VkPipelineCacheCreateInfo info = {
> > +		.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO
> > +	};
> > +	VkPipelineCache cache = VK_NULL_HANDLE;
> > +
> > +	if (vkCreatePipelineCache(dev, &info, NULL, &cache) != VK_SUCCESS)
> > +		cache = VK_NULL_HANDLE;
> > +
> > +	return cache;
> > +}
> > +
> > +static VkCommandPool
> > +vk_create_cmd_pool(VkDevice dev)
> > +{
> > +	const VkCommandPoolCreateInfo info = {
> > +		.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
> > +		.queueFamilyIndex = 0,
> > +		.flags = 0,
> > +	};
> > +	VkCommandPool pool = VK_NULL_HANDLE;
> > +
> > +	if (vkCreateCommandPool(dev, &info, NULL, &pool) != VK_SUCCESS)
> > +		pool = VK_NULL_HANDLE;
> > +
> > +	return pool;
> > +}
> > +
> > +static VkCommandBuffer
> > +vk_create_cmd_buffer(VkDevice dev, VkCommandPool pool)
> > +{
> > +	const VkCommandBufferAllocateInfo alloc_info = {
> > +		.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
> > +		.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
> > +		.commandBufferCount = 1,
> > +		.commandPool = pool,
> > +	};
> > +	const VkCommandBufferBeginInfo begin_info = {
> > +		.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
> > +	};
> > +	VkCommandBuffer buf = VK_NULL_HANDLE;
> > +
> > +	if (vkAllocateCommandBuffers(dev, &alloc_info, &buf) != VK_SUCCESS)
> > +		return VK_NULL_HANDLE;
> > +
> > +	if (vkBeginCommandBuffer(buf, &begin_info) != VK_SUCCESS) {
> > +		vkFreeCommandBuffers(dev, pool, 1, &buf);
> > +		buf = VK_NULL_HANDLE;
> > +	}
> > +
> > +	return buf;
> > +}
> > +
> > +void
> > +vk_core_init(struct vk_core *core)
> > +{
> 
> Make sure all members of vk_core are initialized to VK_NULL_HANDLE before
> starting. Otherwise the cleanup sequence might be borked in failure cases.
> 
> I think the following should be ok:
>     assert(VK_NULL_HANDLE == 0);
>     memset(core, 0, sizeof(*core));

Ah, good catch, thanks!

> 
> With that added this patch is:
> Reviewed-by: Andres Rodriguez <andresx7 at gmail.com>
> 
> 
> > +	core->inst = create_vk_instance();
> > +	if (core->inst == VK_NULL_HANDLE)
> > +		piglit_report_result(PIGLIT_FAIL);
> > +
> > +	core->phys_dev = create_vk_phys_dev(core->inst);
> > +	if (core->phys_dev == VK_NULL_HANDLE)
> > +		goto fail;
> > +
> > +	core->dev = create_vk_device(core->phys_dev);
> > +	if (core->dev == VK_NULL_HANDLE)
> > +		goto fail;
> > +
> > +	core->pipeline_cache = vk_create_pipeline_cache(core->dev);
> > +	if (core->pipeline_cache == VK_NULL_HANDLE)
> > +		goto fail;
> > +
> > +	core->cmd_pool = vk_create_cmd_pool(core->dev);
> > +	if (core->cmd_pool == VK_NULL_HANDLE)
> > +		goto fail;
> > +
> > +	core->cmd_buf = vk_create_cmd_buffer(core->dev, core->cmd_pool);
> > +	if (core->cmd_buf == VK_NULL_HANDLE)
> > +		goto fail;
> > +
> > +	vkGetDeviceQueue(core->dev, 0, 0, &core->queue);
> > +
> > +	return;
> > +
> > +fail:
> > +	vk_core_cleanup(core);
> > +	piglit_report_result(PIGLIT_FAIL);
> > +}
> > +
> > +void
> > +vk_core_cleanup(struct vk_core *core)
> > +{
> > +	if (core->cmd_buf != VK_NULL_HANDLE)
> > +		vkFreeCommandBuffers(
> > +			core->dev, core->cmd_pool, 1, &core->cmd_buf);
> > +
> > +	if (core->cmd_pool != VK_NULL_HANDLE)
> > +		vkDestroyCommandPool(core->dev, core->cmd_pool, NULL);
> > +
> > +	if (core->pipeline_cache != VK_NULL_HANDLE)
> > +		vkDestroyPipelineCache(core->dev, core->pipeline_cache, NULL);
> > +
> > +	if (core->dev != VK_NULL_HANDLE)
> > +		vkDestroyDevice(core->dev, &test_alloc_cb);
> > +
> > +	if (core->inst != VK_NULL_HANDLE)
> > +		vkDestroyInstance(core->inst, &test_alloc_cb);
> > +}
> > diff --git a/tests/spec/ext_memory_object/vk_common.h b/tests/spec/ext_memory_object/vk_common.h
> > new file mode 100644
> > index 000000000..7674d95f3
> > --- /dev/null
> > +++ b/tests/spec/ext_memory_object/vk_common.h
> > @@ -0,0 +1,48 @@
> > +/*
> > + * Copyright 2017 Intel Corporation
> > + *
> > + * Permission is hereby granted, free of charge, to any person obtaining a
> > + * copy of this software and associated documentation files (the "Software"),
> > + * to deal in the Software without restriction, including without limitation
> > + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> > + * and/or sell copies of the Software, and to permit persons to whom the
> > + * Software is furnished to do so, subject to the following conditions:
> > + *
> > + * The above copyright notice and this permission notice (including the next
> > + * paragraph) shall be included in all copies or substantial portions of the
> > + * Software.
> > + *
> > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> > + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> > + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
> > + * DEALINGS IN THE SOFTWARE.
> > + */
> > +
> > +#ifndef VK_COMMON_H
> > +#define VK_COMMON_H
> > +
> > +#include <stdbool.h>
> > +
> > +#define VK_PROTOTYPES
> > +#include <vulkan/vulkan.h>
> > +
> > +struct vk_core {
> > +	VkInstance inst;
> > +	VkPhysicalDevice phys_dev;
> > +	VkDevice dev;
> > +	VkPipelineCache pipeline_cache;
> > +	VkCommandPool cmd_pool;
> > +	VkCommandBuffer cmd_buf;
> > +	VkQueue queue;
> > +};
> > +
> > +void
> > +vk_core_init(struct vk_core *core);
> > +
> > +void
> > +vk_core_cleanup(struct vk_core *core);
> > +
> > +#endif
> > 


More information about the Piglit mailing list