[vulkan] AHB support: Make relevant functions custom
bug: 122080810
+ Import utility libraries for dealing with Vulkan structs
from Intel mesa implementation.
Change-Id: Iee750d5d52567160deeca56d2baa16522b364fa1
diff --git a/system/vulkan_enc/AndroidHardwareBuffer.cpp b/system/vulkan_enc/AndroidHardwareBuffer.cpp
index 70ff6cc..5f4c675 100644
--- a/system/vulkan_enc/AndroidHardwareBuffer.cpp
+++ b/system/vulkan_enc/AndroidHardwareBuffer.cpp
@@ -14,10 +14,9 @@
// limitations under the License.
#include "AndroidHardwareBuffer.h"
-#include "Resources.h"
-
#include "gralloc_cb.h"
#include "vk_format_info.h"
+#include "vk_util.h"
namespace goldfish_vk {
@@ -61,7 +60,7 @@
VkAndroidHardwareBufferFormatPropertiesANDROID* ahbFormatProps =
(VkAndroidHardwareBufferFormatPropertiesANDROID*)vk_find_struct(
- (vk_struct*)pProperties->pNext,
+ (vk_struct_common*)pProperties->pNext,
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID);
if (ahbFormatProps) {
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index df75721..1434965 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -27,6 +27,7 @@
#include "gralloc_cb.h"
#include "goldfish_address_space.h"
#include "goldfish_vk_private_defs.h"
+#include "vk_util.h"
#include <string>
#include <unordered_map>
@@ -143,6 +144,23 @@
AHardwareBuffer** ahbHandle = nullptr;
};
+ // custom guest-side structs for images/buffers because of AHardwareBuffer :((
+ struct VkImage_Info {
+ VkDevice device;
+ VkImageCreateInfo createInfo;
+ VkDeviceMemory currentBacking = VK_NULL_HANDLE;
+ VkDeviceSize currentBackingOffset = 0;
+ VkDeviceSize currentBackingSize = 0;
+ };
+
+ struct VkBuffer_Info {
+ VkDevice device;
+ VkBufferCreateInfo createInfo;
+ VkDeviceMemory currentBacking = VK_NULL_HANDLE;
+ VkDeviceSize currentBackingOffset = 0;
+ VkDeviceSize currentBackingSize = 0;
+ };
+
#define HANDLE_REGISTER_IMPL_IMPL(type) \
std::unordered_map<type, type##_Info> info_##type; \
void register_##type(type obj) { \
@@ -202,6 +220,24 @@
info_VkDeviceMemory.erase(mem);
}
+ void unregister_VkImage(VkImage img) {
+ AutoLock lock(mLock);
+
+ auto it = info_VkImage.find(img);
+ if (it == info_VkImage.end()) return;
+
+ info_VkImage.erase(img);
+ }
+
+ void unregister_VkBuffer(VkBuffer buf) {
+ AutoLock lock(mLock);
+
+ auto it = info_VkBuffer.find(buf);
+ if (it == info_VkBuffer.end()) return;
+
+ info_VkBuffer.erase(buf);
+ }
+
// TODO: Upgrade to 1.1
static constexpr uint32_t kMaxApiVersion = VK_MAKE_VERSION(1, 0, 65);
static constexpr uint32_t kMinApiVersion = VK_MAKE_VERSION(1, 0, 0);
@@ -958,6 +994,129 @@
// no-op
}
+ VkResult on_vkCreateImage(
+ void* context, VkResult,
+ VkDevice device, const VkImageCreateInfo *pCreateInfo,
+ const VkAllocationCallbacks *pAllocator,
+ VkImage *pImage) {
+ VkEncoder* enc = (VkEncoder*)context;
+ return enc->vkCreateImage(device, pCreateInfo, pAllocator, pImage);
+ }
+
+ void on_vkDestroyImage(
+ void* context,
+ VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
+ VkEncoder* enc = (VkEncoder*)context;
+ enc->vkDestroyImage(device, image, pAllocator);
+ }
+
+ void on_vkGetImageMemoryRequirements(
+ void *context, VkDevice device, VkImage image,
+ VkMemoryRequirements *pMemoryRequirements) {
+ VkEncoder* enc = (VkEncoder*)context;
+ enc->vkGetImageMemoryRequirements(
+ device, image, pMemoryRequirements);
+ }
+
+ void on_vkGetImageMemoryRequirements2(
+ void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
+ VkMemoryRequirements2 *pMemoryRequirements) {
+ VkEncoder* enc = (VkEncoder*)context;
+ enc->vkGetImageMemoryRequirements2(
+ device, pInfo, pMemoryRequirements);
+ }
+
+ void on_vkGetImageMemoryRequirements2KHR(
+ void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
+ VkMemoryRequirements2 *pMemoryRequirements) {
+ VkEncoder* enc = (VkEncoder*)context;
+ enc->vkGetImageMemoryRequirements2KHR(
+ device, pInfo, pMemoryRequirements);
+ }
+
+ VkResult on_vkBindImageMemory(
+ void* context, VkResult,
+ VkDevice device, VkImage image, VkDeviceMemory memory,
+ VkDeviceSize memoryOffset) {
+ VkEncoder* enc = (VkEncoder*)context;
+ return enc->vkBindImageMemory(device, image, memory, memoryOffset);
+ }
+
+ VkResult on_vkBindImageMemory2(
+ void* context, VkResult,
+ VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos) {
+ VkEncoder* enc = (VkEncoder*)context;
+ return enc->vkBindImageMemory2(device, bindingCount, pBindInfos);
+ }
+
+ VkResult on_vkBindImageMemory2KHR(
+ void* context, VkResult,
+ VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos) {
+ VkEncoder* enc = (VkEncoder*)context;
+ return enc->vkBindImageMemory2KHR(device, bindingCount, pBindInfos);
+ }
+
+ VkResult on_vkCreateBuffer(
+ void* context, VkResult,
+ VkDevice device, const VkBufferCreateInfo *pCreateInfo,
+ const VkAllocationCallbacks *pAllocator,
+ VkBuffer *pBuffer) {
+ VkEncoder* enc = (VkEncoder*)context;
+ return enc->vkCreateBuffer(device, pCreateInfo, pAllocator, pBuffer);
+ }
+
+ void on_vkDestroyBuffer(
+ void* context,
+ VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) {
+ VkEncoder* enc = (VkEncoder*)context;
+ enc->vkDestroyBuffer(device, buffer, pAllocator);
+ }
+
+ void on_vkGetBufferMemoryRequirements(
+ void* context, VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements) {
+ VkEncoder* enc = (VkEncoder*)context;
+ enc->vkGetBufferMemoryRequirements(
+ device, buffer, pMemoryRequirements);
+ }
+
+ void on_vkGetBufferMemoryRequirements2(
+ void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo,
+ VkMemoryRequirements2* pMemoryRequirements) {
+ VkEncoder* enc = (VkEncoder*)context;
+ enc->vkGetBufferMemoryRequirements2(device, pInfo, pMemoryRequirements);
+ }
+
+ void on_vkGetBufferMemoryRequirements2KHR(
+ void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo,
+ VkMemoryRequirements2* pMemoryRequirements) {
+ VkEncoder* enc = (VkEncoder*)context;
+ enc->vkGetBufferMemoryRequirements2KHR(device, pInfo, pMemoryRequirements);
+ }
+
+ VkResult on_vkBindBufferMemory(
+ void *context, VkResult,
+ VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset) {
+ VkEncoder *enc = (VkEncoder *)context;
+ return enc->vkBindBufferMemory(
+ device, buffer, memory, memoryOffset);
+ }
+
+ VkResult on_vkBindBufferMemory2(
+ void *context, VkResult,
+ VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos) {
+ VkEncoder *enc = (VkEncoder *)context;
+ return enc->vkBindBufferMemory2(
+ device, bindInfoCount, pBindInfos);
+ }
+
+ VkResult on_vkBindBufferMemory2KHR(
+ void *context, VkResult,
+ VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos) {
+ VkEncoder *enc = (VkEncoder *)context;
+ return enc->vkBindBufferMemory2KHR(
+ device, bindInfoCount, pBindInfos);
+ }
+
void unwrap_VkNativeBufferANDROID(
const VkImageCreateInfo* pCreateInfo,
VkImageCreateInfo* local_pCreateInfo) {
@@ -1330,6 +1489,125 @@
mImpl->on_vkUnmapMemory(context, device, memory);
}
+VkResult ResourceTracker::on_vkCreateImage(
+ void* context, VkResult input_result,
+ VkDevice device, const VkImageCreateInfo *pCreateInfo,
+ const VkAllocationCallbacks *pAllocator,
+ VkImage *pImage) {
+ return mImpl->on_vkCreateImage(
+ context, input_result,
+ device, pCreateInfo, pAllocator, pImage);
+}
+
+void ResourceTracker::on_vkDestroyImage(
+ void* context,
+ VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
+ mImpl->on_vkDestroyImage(context,
+ device, image, pAllocator);
+}
+
+void ResourceTracker::on_vkGetImageMemoryRequirements(
+ void *context, VkDevice device, VkImage image,
+ VkMemoryRequirements *pMemoryRequirements) {
+ mImpl->on_vkGetImageMemoryRequirements(
+ context, device, image, pMemoryRequirements);
+}
+
+void ResourceTracker::on_vkGetImageMemoryRequirements2(
+ void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
+ VkMemoryRequirements2 *pMemoryRequirements) {
+ mImpl->on_vkGetImageMemoryRequirements2(
+ context, device, pInfo, pMemoryRequirements);
+}
+
+void ResourceTracker::on_vkGetImageMemoryRequirements2KHR(
+ void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
+ VkMemoryRequirements2 *pMemoryRequirements) {
+ mImpl->on_vkGetImageMemoryRequirements2KHR(
+ context, device, pInfo, pMemoryRequirements);
+}
+
+VkResult ResourceTracker::on_vkBindImageMemory(
+ void* context, VkResult input_result,
+ VkDevice device, VkImage image, VkDeviceMemory memory,
+ VkDeviceSize memoryOffset) {
+ return mImpl->on_vkBindImageMemory(
+ context, input_result, device, image, memory, memoryOffset);
+}
+
+VkResult ResourceTracker::on_vkBindImageMemory2(
+ void* context, VkResult input_result,
+ VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos) {
+ return mImpl->on_vkBindImageMemory2(
+ context, input_result, device, bindingCount, pBindInfos);
+}
+
+VkResult ResourceTracker::on_vkBindImageMemory2KHR(
+ void* context, VkResult input_result,
+ VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos) {
+ return mImpl->on_vkBindImageMemory2KHR(
+ context, input_result, device, bindingCount, pBindInfos);
+}
+
+VkResult ResourceTracker::on_vkCreateBuffer(
+ void* context, VkResult input_result,
+ VkDevice device, const VkBufferCreateInfo *pCreateInfo,
+ const VkAllocationCallbacks *pAllocator,
+ VkBuffer *pBuffer) {
+ return mImpl->on_vkCreateBuffer(
+ context, input_result,
+ device, pCreateInfo, pAllocator, pBuffer);
+}
+
+void ResourceTracker::on_vkDestroyBuffer(
+ void* context,
+ VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) {
+ mImpl->on_vkDestroyBuffer(context, device, buffer, pAllocator);
+}
+
+void ResourceTracker::on_vkGetBufferMemoryRequirements(
+ void* context, VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements) {
+ mImpl->on_vkGetBufferMemoryRequirements(context, device, buffer, pMemoryRequirements);
+}
+
+void ResourceTracker::on_vkGetBufferMemoryRequirements2(
+ void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo,
+ VkMemoryRequirements2* pMemoryRequirements) {
+ mImpl->on_vkGetBufferMemoryRequirements2(
+ context, device, pInfo, pMemoryRequirements);
+}
+
+void ResourceTracker::on_vkGetBufferMemoryRequirements2KHR(
+ void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo,
+ VkMemoryRequirements2* pMemoryRequirements) {
+ mImpl->on_vkGetBufferMemoryRequirements2KHR(
+ context, device, pInfo, pMemoryRequirements);
+}
+
+VkResult ResourceTracker::on_vkBindBufferMemory(
+ void* context, VkResult input_result,
+ VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset) {
+ return mImpl->on_vkBindBufferMemory(
+ context, input_result,
+ device, buffer, memory, memoryOffset);
+}
+
+VkResult ResourceTracker::on_vkBindBufferMemory2(
+ void* context, VkResult input_result,
+ VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos) {
+ return mImpl->on_vkBindBufferMemory2(
+ context, input_result,
+ device, bindInfoCount, pBindInfos);
+}
+
+VkResult ResourceTracker::on_vkBindBufferMemory2KHR(
+ void* context, VkResult input_result,
+ VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos) {
+ return mImpl->on_vkBindBufferMemory2KHR(
+ context, input_result,
+ device, bindInfoCount, pBindInfos);
+}
+
void ResourceTracker::unwrap_VkNativeBufferANDROID(
const VkImageCreateInfo* pCreateInfo,
VkImageCreateInfo* local_pCreateInfo) {
diff --git a/system/vulkan_enc/ResourceTracker.h b/system/vulkan_enc/ResourceTracker.h
index 0afed7f..c851574 100644
--- a/system/vulkan_enc/ResourceTracker.h
+++ b/system/vulkan_enc/ResourceTracker.h
@@ -121,6 +121,64 @@
VkDevice device,
VkDeviceMemory memory);
+ VkResult on_vkCreateImage(
+ void* context, VkResult input_result,
+ VkDevice device, const VkImageCreateInfo *pCreateInfo,
+ const VkAllocationCallbacks *pAllocator,
+ VkImage *pImage);
+ void on_vkDestroyImage(
+ void* context,
+ VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator);
+
+ void on_vkGetImageMemoryRequirements(
+ void *context, VkDevice device, VkImage image,
+ VkMemoryRequirements *pMemoryRequirements);
+ void on_vkGetImageMemoryRequirements2(
+ void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
+ VkMemoryRequirements2 *pMemoryRequirements);
+ void on_vkGetImageMemoryRequirements2KHR(
+ void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
+ VkMemoryRequirements2 *pMemoryRequirements);
+
+ VkResult on_vkBindImageMemory(
+ void* context, VkResult input_result,
+ VkDevice device, VkImage image, VkDeviceMemory memory,
+ VkDeviceSize memoryOffset);
+ VkResult on_vkBindImageMemory2(
+ void* context, VkResult input_result,
+ VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos);
+ VkResult on_vkBindImageMemory2KHR(
+ void* context, VkResult input_result,
+ VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos);
+
+ VkResult on_vkCreateBuffer(
+ void* context, VkResult input_result,
+ VkDevice device, const VkBufferCreateInfo *pCreateInfo,
+ const VkAllocationCallbacks *pAllocator,
+ VkBuffer *pBuffer);
+ void on_vkDestroyBuffer(
+ void* context,
+ VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator);
+
+ void on_vkGetBufferMemoryRequirements(
+ void* context, VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements);
+ void on_vkGetBufferMemoryRequirements2(
+ void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo,
+ VkMemoryRequirements2* pMemoryRequirements);
+ void on_vkGetBufferMemoryRequirements2KHR(
+ void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo,
+ VkMemoryRequirements2* pMemoryRequirements);
+
+ VkResult on_vkBindBufferMemory(
+ void* context, VkResult input_result,
+ VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset);
+ VkResult on_vkBindBufferMemory2(
+ void* context, VkResult input_result,
+ VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos);
+ VkResult on_vkBindBufferMemory2KHR(
+ void* context, VkResult input_result,
+ VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos);
+
void unwrap_VkNativeBufferANDROID(
const VkImageCreateInfo* pCreateInfo,
VkImageCreateInfo* local_pCreateInfo);
@@ -169,7 +227,7 @@
#define DEFINE_TRANSFORMED_TYPE_PROTOTYPE(type) \
void transformImpl_##type##_tohost(const type*, uint32_t); \
void transformImpl_##type##_fromhost(const type*, uint32_t); \
-
+
LIST_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_PROTOTYPE)
private:
diff --git a/system/vulkan_enc/Resources.h b/system/vulkan_enc/Resources.h
index 0726aac..0948df6 100644
--- a/system/vulkan_enc/Resources.h
+++ b/system/vulkan_enc/Resources.h
@@ -72,18 +72,4 @@
GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_U64_DECL)
GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DEFINE_TRIVIAL_NON_DISPATCHABLE_HANDLE_STRUCT)
-// From vkd3d: utility function to search for extension structs
-struct vk_struct {
- VkStructureType sType;
- struct vk_struct *pNext;
-};
-
-static const void *vk_find_struct(struct vk_struct *chain, VkStructureType type) {
- while (chain) {
- if (chain->sType == type) return chain;
- chain = chain->pNext;
- }
- return nullptr;
-}
-
} // extern "C"
diff --git a/system/vulkan_enc/VulkanHandles.h b/system/vulkan_enc/VulkanHandles.h
index bf91bce..2bf601b 100644
--- a/system/vulkan_enc/VulkanHandles.h
+++ b/system/vulkan_enc/VulkanHandles.h
@@ -27,9 +27,7 @@
GOLDFISH_VK_LIST_TRIVIAL_DISPATCHABLE_HANDLE_TYPES(f)
#define GOLDFISH_VK_LIST_TRIVIAL_NON_DISPATCHABLE_HANDLE_TYPES(f) \
- f(VkBuffer) \
f(VkBufferView) \
- f(VkImage) \
f(VkImageView) \
f(VkShaderModule) \
f(VkDescriptorPool) \
@@ -60,6 +58,8 @@
#define GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(f) \
f(VkDeviceMemory) \
+ f(VkBuffer) \
+ f(VkImage) \
GOLDFISH_VK_LIST_TRIVIAL_NON_DISPATCHABLE_HANDLE_TYPES(f) \
#define GOLDFISH_VK_LIST_HANDLE_TYPES(f) \
diff --git a/system/vulkan_enc/vk_util.h b/system/vulkan_enc/vk_util.h
new file mode 100644
index 0000000..b2a89bc
--- /dev/null
+++ b/system/vulkan_enc/vk_util.h
@@ -0,0 +1,210 @@
+/*
+ * 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_UTIL_H
+#define VK_UTIL_H
+
+/* common inlines and macros for vulkan drivers */
+
+#include <vulkan/vulkan.h>
+
+struct vk_struct_common {
+ VkStructureType sType;
+ struct vk_struct_common *pNext;
+};
+
+#define vk_foreach_struct(__iter, __start) \
+ for (struct vk_struct_common *__iter = (struct vk_struct_common *)(__start); \
+ __iter; __iter = __iter->pNext)
+
+#define vk_foreach_struct_const(__iter, __start) \
+ for (const struct vk_struct_common *__iter = (const struct vk_struct_common *)(__start); \
+ __iter; __iter = __iter->pNext)
+
+/**
+ * A wrapper for a Vulkan output array. A Vulkan output array is one that
+ * follows the convention of the parameters to
+ * vkGetPhysicalDeviceQueueFamilyProperties().
+ *
+ * Example Usage:
+ *
+ * VkResult
+ * vkGetPhysicalDeviceQueueFamilyProperties(
+ * VkPhysicalDevice physicalDevice,
+ * uint32_t* pQueueFamilyPropertyCount,
+ * VkQueueFamilyProperties* pQueueFamilyProperties)
+ * {
+ * VK_OUTARRAY_MAKE(props, pQueueFamilyProperties,
+ * pQueueFamilyPropertyCount);
+ *
+ * vk_outarray_append(&props, p) {
+ * p->queueFlags = ...;
+ * p->queueCount = ...;
+ * }
+ *
+ * vk_outarray_append(&props, p) {
+ * p->queueFlags = ...;
+ * p->queueCount = ...;
+ * }
+ *
+ * return vk_outarray_status(&props);
+ * }
+ */
+struct __vk_outarray {
+ /** May be null. */
+ void *data;
+
+ /**
+ * Capacity, in number of elements. Capacity is unlimited (UINT32_MAX) if
+ * data is null.
+ */
+ uint32_t cap;
+
+ /**
+ * Count of elements successfully written to the array. Every write is
+ * considered successful if data is null.
+ */
+ uint32_t *filled_len;
+
+ /**
+ * Count of elements that would have been written to the array if its
+ * capacity were sufficient. Vulkan functions often return VK_INCOMPLETE
+ * when `*filled_len < wanted_len`.
+ */
+ uint32_t wanted_len;
+};
+
+static inline void
+__vk_outarray_init(struct __vk_outarray *a,
+ void *data, uint32_t * len)
+{
+ a->data = data;
+ a->cap = *len;
+ a->filled_len = len;
+ *a->filled_len = 0;
+ a->wanted_len = 0;
+
+ if (a->data == NULL)
+ a->cap = UINT32_MAX;
+}
+
+static inline VkResult
+__vk_outarray_status(const struct __vk_outarray *a)
+{
+ if (*a->filled_len < a->wanted_len)
+ return VK_INCOMPLETE;
+ else
+ return VK_SUCCESS;
+}
+
+static inline void *
+__vk_outarray_next(struct __vk_outarray *a, size_t elem_size)
+{
+ void *p = NULL;
+
+ a->wanted_len += 1;
+
+ if (*a->filled_len >= a->cap)
+ return NULL;
+
+ if (a->data != NULL)
+ p = ((uint8_t*)a->data) + (*a->filled_len) * elem_size;
+
+ *a->filled_len += 1;
+
+ return p;
+}
+
+#define vk_outarray(elem_t) \
+ struct { \
+ struct __vk_outarray base; \
+ elem_t meta[]; \
+ }
+
+#define vk_outarray_typeof_elem(a) __typeof__((a)->meta[0])
+#define vk_outarray_sizeof_elem(a) sizeof((a)->meta[0])
+
+#define vk_outarray_init(a, data, len) \
+ __vk_outarray_init(&(a)->base, (data), (len))
+
+#define VK_OUTARRAY_MAKE(name, data, len) \
+ vk_outarray(__typeof__((data)[0])) name; \
+ vk_outarray_init(&name, (data), (len))
+
+#define vk_outarray_status(a) \
+ __vk_outarray_status(&(a)->base)
+
+#define vk_outarray_next(a) \
+ ((vk_outarray_typeof_elem(a) *) \
+ __vk_outarray_next(&(a)->base, vk_outarray_sizeof_elem(a)))
+
+/**
+ * Append to a Vulkan output array.
+ *
+ * This is a block-based macro. For example:
+ *
+ * vk_outarray_append(&a, elem) {
+ * elem->foo = ...;
+ * elem->bar = ...;
+ * }
+ *
+ * The array `a` has type `vk_outarray(elem_t) *`. It is usually declared with
+ * VK_OUTARRAY_MAKE(). The variable `elem` is block-scoped and has type
+ * `elem_t *`.
+ *
+ * The macro unconditionally increments the array's `wanted_len`. If the array
+ * is not full, then the macro also increment its `filled_len` and then
+ * executes the block. When the block is executed, `elem` is non-null and
+ * points to the newly appended element.
+ */
+#define vk_outarray_append(a, elem) \
+ for (vk_outarray_typeof_elem(a) *elem = vk_outarray_next(a); \
+ elem != NULL; elem = NULL)
+
+static inline void *
+__vk_find_struct(void *start, VkStructureType sType)
+{
+ vk_foreach_struct(s, start) {
+ if (s->sType == sType)
+ return s;
+ }
+
+ return NULL;
+}
+
+#define vk_find_struct(__start, __sType) \
+ __vk_find_struct((__start), __sType)
+
+#define vk_find_struct_const(__start, __sType) \
+ (const void *)__vk_find_struct((void *)(__start), __sType)
+
+uint32_t vk_get_driver_version(void);
+
+uint32_t vk_get_version_override(void);
+
+#define VK_EXT_OFFSET (1000000000UL)
+#define VK_ENUM_EXTENSION(__enum) \
+ ((__enum) >= VK_EXT_OFFSET ? ((((__enum) - VK_EXT_OFFSET) / 1000UL) + 1) : 0)
+#define VK_ENUM_OFFSET(__enum) \
+ ((__enum) >= VK_EXT_OFFSET ? ((__enum) % 1000) : (__enum))
+
+#endif /* VK_UTIL_H */