[Piglit] [v2 2/7] ext_memory_object: Support for setting up vulkan device
Andres Rodriguez
andresx7 at gmail.com
Tue Dec 19 01:04:51 UTC 2017
On 2017-12-18 08:17 AM, Topi Pohjolainen wrote:
> Signed-off-by: Topi Pohjolainen <topi.pohjolainen at intel.com>
> ---
> tests/spec/ext_memory_object/vk_common.c | 245 +++++++++++++++++++++++++++++++
> tests/spec/ext_memory_object/vk_common.h | 38 +++++
> 2 files changed, 283 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..ab871d501
> --- /dev/null
> +++ b/tests/spec/ext_memory_object/vk_common.c
> @@ -0,0 +1,245 @@
> +/*
> + * 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 VkInstance inst = VK_NULL_HANDLE;
> +static VkPhysicalDevice phys_dev = VK_NULL_HANDLE;
> +static VkDevice dev = VK_NULL_HANDLE;
> +static VkPipelineCache pipeline_cache = VK_NULL_HANDLE;
> +static VkCommandPool cmd_pool = VK_NULL_HANDLE;
> +static VkCommandBuffer cmd_buf = VK_NULL_HANDLE;
> +static VkQueue queue = VK_NULL_HANDLE;
> +
Since these variables are file scope consider giving them names that are
less likely to alias with local variables (which happens a lot in this
file, even with variables of different types!).
Not sure if there is any specific naming convention in piglit, but full
names + the 's' prefix would probably be okay. E.g. sInstance,
sPhysDevice, sDevice, etc.
> +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);
> + memset(mem, 139, size);
Might want to document why 139 is used here, is just to poison the
memory to test for bad usage?
> + 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 = "piglit_vk_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 dev = VK_NULL_HANDLE > +
> + if (vkEnumeratePhysicalDevices(inst, &count, NULL) != VK_SUCCESS ||
> + count == 0)
> + return VK_NULL_HANDLE;
> +
> + count = 1;
> + if (vkEnumeratePhysicalDevices(inst, &count, &dev) != VK_SUCCESS ||
> + count != 1) > + dev = VK_NULL_HANDLE;
> +
This should be simple to adapt to handle cases with more than one
physical device. An array can be allocated dynamically of size count and
you just return array[0].
> + return dev;
> +}
> +
> +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) {
Is the indentation here slightly off?
> + vkFreeCommandBuffers(dev, pool, 1, &buf);
> + buf = VK_NULL_HANDLE;
> + }
> +
> + return buf;
> +}
> +
> +VkDevice
> +vk_init(void)
> +{
> + /* Consider if Vulkan stack is already initialised. */
Slightly off indent here too.
> + if (dev != VK_NULL_HANDLE)
> + return dev;
> +
> + inst = create_vk_instance();
> + if (inst == VK_NULL_HANDLE)
> + piglit_report_result(PIGLIT_FAIL);
> +
> + phys_dev = create_vk_phys_dev(inst);
> + if (phys_dev == VK_NULL_HANDLE)
> + goto fail;
> +
> + dev = create_vk_device(phys_dev);
> + if (dev == VK_NULL_HANDLE)
> + goto fail;
> +
> + pipeline_cache = vk_create_pipeline_cache(dev);
> + if (pipeline_cache == VK_NULL_HANDLE)
> + goto fail;
> +
> + cmd_pool = vk_create_cmd_pool(dev);
> + if (cmd_pool == VK_NULL_HANDLE)
> + goto fail;
> +
> + cmd_buf = vk_create_cmd_buffer(dev, cmd_pool);
> + if (cmd_buf == VK_NULL_HANDLE)
> + goto fail;
> +
> + vkGetDeviceQueue(dev, 0, 0, &queue);
> +
> + return dev;
> +
> +fail:
> + vk_cleanup();
> + piglit_report_result(PIGLIT_FAIL);
> +}
> +
> +void
> +vk_cleanup(void)
> +{
> + if (cmd_buf != VK_NULL_HANDLE)
> + vkFreeCommandBuffers(dev, cmd_pool, 1, &cmd_buf);
> +
> + if (cmd_pool != VK_NULL_HANDLE)
> + vkDestroyCommandPool(dev, cmd_pool, NULL);
> +
> + if (pipeline_cache != VK_NULL_HANDLE)
> + vkDestroyPipelineCache(dev, pipeline_cache, NULL);
> +
> + if (dev != VK_NULL_HANDLE)
> + vkDestroyDevice(dev, &test_alloc_cb);
> +
> + if (inst != VK_NULL_HANDLE)
> + vkDestroyInstance(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..8ddf9c8be
> --- /dev/null
> +++ b/tests/spec/ext_memory_object/vk_common.h
> @@ -0,0 +1,38 @@
> +/*
> + * 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>
> +
> +VkDevice
> +vk_init(void);
> +
> +void
> +vk_cleanup(void);
> +
> +#endif
>
More information about the Piglit
mailing list