[Piglit] [v2 6/7] ext_memory_object: Support for importing vulkan memory

Topi Pohjolainen topi.pohjolainen at gmail.com
Mon Dec 18 13:17:16 UTC 2017


Signed-off-by: Topi Pohjolainen <topi.pohjolainen at intel.com>
---
 tests/spec/ext_memory_object/common.c    | 71 ++++++++++++++++++++++++++++++++
 tests/spec/ext_memory_object/common.h    |  8 ++++
 tests/spec/ext_memory_object/vk_common.c | 12 ++++++
 tests/spec/ext_memory_object/vk_common.h |  3 ++
 4 files changed, 94 insertions(+)

diff --git a/tests/spec/ext_memory_object/common.c b/tests/spec/ext_memory_object/common.c
index 4e629a787..b389b73ce 100644
--- a/tests/spec/ext_memory_object/common.c
+++ b/tests/spec/ext_memory_object/common.c
@@ -23,6 +23,7 @@
  */
 
 #include "common.h"
+#include "vk_common.h"
 
 static void
 print_usage_and_exit(const char *usage_prefix)
@@ -69,3 +70,73 @@ parse_tex_layout(const char **args, unsigned num_args,
 	layout->first_layer = read_unsigned(args[10], usage_prefix);
 	layout->num_layers = read_unsigned(args[11], usage_prefix);
 }
+
+bool
+create_mem_obj_for_vk_dev_mem(VkDevice dev, VkDeviceMemory mem,
+			      uint64_t size, GLuint *mem_obj)
+{
+	PFN_vkGetMemoryFdKHR get_fd = vk_get_proc_addr_for_mem_fd(dev);
+	const VkMemoryGetFdInfoKHR info = {
+		.sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
+		.memory = mem,
+		.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR
+	};
+	int fd;
+
+	assert(get_fd);
+	if (get_fd(dev, &info, &fd) != VK_SUCCESS)
+		return false;
+
+	glCreateMemoryObjectsEXT(1, mem_obj);
+
+	glImportMemoryFdEXT(*mem_obj, size, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fd);
+
+	return true;
+}
+
+void
+create_tex_from_vk_dev_mem(const struct tex_layout *layout,
+			   GLuint mem_obj, unsigned offset, GLuint *tex)
+{
+	assert(layout->num_samples == 1);
+
+	glGenTextures(1, tex);
+	glBindTexture(layout->target, *tex);
+
+	switch (layout->target) {
+	case GL_TEXTURE_1D:
+		assert(layout->z == 1);
+		assert(layout->h == 1);
+		glTexStorageMem1DEXT(layout->target, layout->num_levels,
+				     layout->format, layout->w,
+				     mem_obj, offset);
+		break;
+	case GL_TEXTURE_1D_ARRAY:
+		assert(layout->z == 1);
+		assert(layout->h == 1);
+		glTexStorageMem2DEXT(layout->target, layout->num_levels,
+				     layout->format, layout->w,
+				     layout->num_layers,
+				     mem_obj, offset);
+	case GL_TEXTURE_2D:
+		assert(layout->z == 1);
+		glTexStorageMem2DEXT(layout->target, layout->num_levels,
+				     layout->format, layout->w, layout->h,
+				     mem_obj, offset);
+		break;
+	case GL_TEXTURE_2D_ARRAY:
+		assert(layout->z == 1);
+		glTexStorageMem3DEXT(layout->target, layout->num_levels,
+				     layout->format,
+				     layout->w, layout->h, layout->num_layers,
+				     mem_obj, offset);
+	case GL_TEXTURE_3D:
+		glTexStorageMem3DEXT(layout->target, layout->num_levels,
+				     layout->format,
+				     layout->w, layout->h, layout->z,
+				     mem_obj, offset);
+		break;
+	default:
+		assert(!"Invalid target");
+	}
+}
diff --git a/tests/spec/ext_memory_object/common.h b/tests/spec/ext_memory_object/common.h
index 35e54c529..efd4528b5 100644
--- a/tests/spec/ext_memory_object/common.h
+++ b/tests/spec/ext_memory_object/common.h
@@ -42,4 +42,12 @@ void
 parse_tex_layout(const char **args, unsigned num_args,
 		 const char *usage_prefix, struct tex_layout *layout);
 
+bool
+create_mem_obj_for_vk_dev_mem(VkDevice dev, VkDeviceMemory mem,
+			      uint64_t size, GLuint *mem_obj);
+
+void
+create_tex_from_vk_dev_mem(const struct tex_layout *layout,
+			   GLuint mem_obj, unsigned offset, GLuint *tex);
+
 #endif
diff --git a/tests/spec/ext_memory_object/vk_common.c b/tests/spec/ext_memory_object/vk_common.c
index 2a02d058c..8b38c4b10 100644
--- a/tests/spec/ext_memory_object/vk_common.c
+++ b/tests/spec/ext_memory_object/vk_common.c
@@ -565,3 +565,15 @@ vk_create_fence(VkDevice dev)
 
         return fence;
 }
+
+PFN_vkGetMemoryFdKHR
+vk_get_proc_addr_for_mem_fd(VkDevice dev)
+{
+	static PFN_vkGetMemoryFdKHR get_fd = NULL;
+
+	if (get_fd == NULL)
+		get_fd = (PFN_vkGetMemoryFdKHR)vkGetDeviceProcAddr(
+						dev, "vkGetMemoryFdKHR");
+
+	return get_fd;
+}
diff --git a/tests/spec/ext_memory_object/vk_common.h b/tests/spec/ext_memory_object/vk_common.h
index 3ad8fd6c3..cda3318ec 100644
--- a/tests/spec/ext_memory_object/vk_common.h
+++ b/tests/spec/ext_memory_object/vk_common.h
@@ -107,4 +107,7 @@ vk_draw(VkPipeline pipeline, VkBuffer vb, VkFence fence);
 VkFence
 vk_create_fence(VkDevice dev);
 
+PFN_vkGetMemoryFdKHR
+vk_get_proc_addr_for_mem_fd(VkDevice dev);
+
 #endif
-- 
2.14.1



More information about the Piglit mailing list