2015-10-24 06:29:05 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-04-08 11:35:22 +00:00
|
|
|
#include "gstvkmemory.h"
|
2015-10-24 06:29:05 +00:00
|
|
|
|
2019-09-11 09:24:50 +00:00
|
|
|
#include "gstvkdebug-private.h"
|
|
|
|
|
2015-10-24 06:29:05 +00:00
|
|
|
/**
|
|
|
|
* SECTION:vkmemory
|
2019-04-11 06:52:54 +00:00
|
|
|
* @title: GstVulkanMemory
|
2015-10-24 06:29:05 +00:00
|
|
|
* @short_description: memory subclass for Vulkan device memory
|
2020-08-12 05:59:01 +00:00
|
|
|
* @see_also: #GstVulkanDevice, #GstMemory, #GstAllocator
|
2015-10-24 06:29:05 +00:00
|
|
|
*
|
|
|
|
* GstVulkanMemory is a #GstMemory subclass providing support for the mapping of
|
2017-03-08 18:01:13 +00:00
|
|
|
* Vulkan device memory.
|
2015-10-24 06:29:05 +00:00
|
|
|
*/
|
|
|
|
|
2016-02-08 01:22:11 +00:00
|
|
|
/* WARNING: while suballocation is allowed, nothing prevents aliasing which
|
|
|
|
* requires external synchronisation */
|
|
|
|
|
2015-10-24 06:29:05 +00:00
|
|
|
#define GST_CAT_DEFUALT GST_CAT_VULKAN_MEMORY
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFUALT);
|
|
|
|
|
|
|
|
static GstAllocator *_vulkan_memory_allocator;
|
|
|
|
|
|
|
|
static void
|
|
|
|
_vk_mem_init (GstVulkanMemory * mem, GstAllocator * allocator,
|
|
|
|
GstMemory * parent, GstVulkanDevice * device, guint32 memory_type_index,
|
|
|
|
GstAllocationParams * params, gsize size,
|
|
|
|
VkMemoryPropertyFlags mem_prop_flags, gpointer user_data,
|
|
|
|
GDestroyNotify notify)
|
|
|
|
{
|
|
|
|
gsize align = gst_memory_alignment, offset = 0, maxsize = size;
|
|
|
|
GstMemoryFlags flags = 0;
|
2015-12-02 06:54:30 +00:00
|
|
|
gchar *props_str;
|
2015-10-24 06:29:05 +00:00
|
|
|
|
|
|
|
if (params) {
|
|
|
|
flags = params->flags;
|
|
|
|
align |= params->align;
|
|
|
|
offset = params->prefix;
|
|
|
|
maxsize += params->prefix + params->padding;
|
2019-05-16 09:39:20 +00:00
|
|
|
maxsize += align;
|
2015-10-24 06:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_memory_init (GST_MEMORY_CAST (mem), flags, allocator, parent, maxsize,
|
|
|
|
align, offset, size);
|
|
|
|
|
|
|
|
mem->device = gst_object_ref (device);
|
2015-12-29 05:05:17 +00:00
|
|
|
mem->alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
2015-10-24 06:29:05 +00:00
|
|
|
mem->alloc_info.pNext = NULL;
|
|
|
|
mem->alloc_info.allocationSize = (VkDeviceSize) mem->mem.maxsize;
|
|
|
|
mem->alloc_info.memoryTypeIndex = memory_type_index;
|
|
|
|
mem->properties = mem_prop_flags;
|
|
|
|
mem->notify = notify;
|
|
|
|
mem->user_data = user_data;
|
2016-02-08 01:22:11 +00:00
|
|
|
mem->vk_offset = 0;
|
2015-10-24 06:29:05 +00:00
|
|
|
|
|
|
|
g_mutex_init (&mem->lock);
|
|
|
|
|
2019-09-11 09:24:50 +00:00
|
|
|
props_str = gst_vulkan_memory_property_flags_to_string (mem_prop_flags);
|
2015-12-02 06:54:30 +00:00
|
|
|
|
|
|
|
GST_CAT_DEBUG (GST_CAT_VULKAN_MEMORY, "new Vulkan memory:%p size:%"
|
|
|
|
G_GSIZE_FORMAT " properties:%s", mem, maxsize, props_str);
|
|
|
|
|
|
|
|
g_free (props_str);
|
2015-10-24 06:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstVulkanMemory *
|
|
|
|
_vk_mem_new (GstAllocator * allocator, GstMemory * parent,
|
|
|
|
GstVulkanDevice * device, guint32 memory_type_index,
|
|
|
|
GstAllocationParams * params, gsize size,
|
|
|
|
VkMemoryPropertyFlags mem_props_flags, gpointer user_data,
|
|
|
|
GDestroyNotify notify)
|
|
|
|
{
|
2016-02-08 01:22:11 +00:00
|
|
|
GstVulkanMemory *mem = g_new0 (GstVulkanMemory, 1);
|
2015-10-24 06:29:05 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
VkResult err;
|
|
|
|
|
|
|
|
_vk_mem_init (mem, allocator, parent, device, memory_type_index, params,
|
|
|
|
size, mem_props_flags, user_data, notify);
|
|
|
|
|
2015-12-29 05:05:17 +00:00
|
|
|
err =
|
|
|
|
vkAllocateMemory (device->device, &mem->alloc_info, NULL, &mem->mem_ptr);
|
2015-10-24 06:29:05 +00:00
|
|
|
if (gst_vulkan_error_to_g_error (err, &error, "vkAllocMemory") < 0) {
|
|
|
|
GST_CAT_ERROR (GST_CAT_VULKAN_MEMORY, "Failed to allocate device memory %s",
|
|
|
|
error->message);
|
|
|
|
gst_memory_unref ((GstMemory *) mem);
|
|
|
|
g_clear_error (&error);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
_vk_mem_map_full (GstVulkanMemory * mem, GstMapInfo * info, gsize size)
|
|
|
|
{
|
|
|
|
gpointer data;
|
|
|
|
VkResult err;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if ((mem->properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0) {
|
|
|
|
GST_CAT_ERROR (GST_CAT_VULKAN_MEMORY, "Cannot map host-invisible memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-08 01:22:11 +00:00
|
|
|
err = vkMapMemory (mem->device->device, mem->mem_ptr, mem->vk_offset,
|
|
|
|
size, 0, &data);
|
2015-10-24 06:29:05 +00:00
|
|
|
if (gst_vulkan_error_to_g_error (err, &error, "vkMapMemory") < 0) {
|
|
|
|
GST_CAT_ERROR (GST_CAT_VULKAN_MEMORY, "Failed to map device memory %s",
|
|
|
|
error->message);
|
|
|
|
g_clear_error (&error);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_vk_mem_unmap_full (GstVulkanMemory * mem, GstMapInfo * info)
|
|
|
|
{
|
2023-01-04 16:30:47 +00:00
|
|
|
if ((info->flags & GST_MAP_WRITE)
|
|
|
|
&& !(mem->properties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
|
|
|
|
GError *error = NULL;
|
|
|
|
VkResult err;
|
|
|
|
VkMappedMemoryRange range = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
|
|
|
|
/* .pNext = */
|
|
|
|
.memory = mem->mem_ptr,
|
|
|
|
.offset = mem->vk_offset,
|
2023-03-29 10:58:14 +00:00
|
|
|
.size = VK_WHOLE_SIZE,
|
2023-01-04 16:30:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
err = vkFlushMappedMemoryRanges (mem->device->device, 1u, &range);
|
|
|
|
if (gst_vulkan_error_to_g_error (err, &error,
|
|
|
|
"vkFlushMappedMemoryRanges") < 0) {
|
|
|
|
GST_CAT_WARNING (GST_CAT_VULKAN_MEMORY, "Failed to flush memory: %s",
|
|
|
|
error->message);
|
|
|
|
g_clear_error (&error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 06:29:05 +00:00
|
|
|
vkUnmapMemory (mem->device->device, mem->mem_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstMemory *
|
|
|
|
_vk_mem_copy (GstVulkanMemory * src, gssize offset, gssize size)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstMemory *
|
2016-02-08 01:22:11 +00:00
|
|
|
_vk_mem_share (GstVulkanMemory * mem, gssize offset, gsize size)
|
2015-10-24 06:29:05 +00:00
|
|
|
{
|
2016-02-08 01:22:11 +00:00
|
|
|
GstVulkanMemory *shared = g_new0 (GstVulkanMemory, 1);
|
|
|
|
GstVulkanMemory *parent = mem;
|
|
|
|
GstAllocationParams params = { 0, };
|
|
|
|
|
|
|
|
if (size == -1)
|
|
|
|
size = mem->mem.size - offset;
|
|
|
|
|
|
|
|
g_return_val_if_fail (size > 0, NULL);
|
|
|
|
|
|
|
|
while ((parent = (GstVulkanMemory *) (GST_MEMORY_CAST (parent)->parent)));
|
|
|
|
|
|
|
|
params.flags = GST_MEMORY_FLAGS (mem);
|
|
|
|
params.align = GST_MEMORY_CAST (parent)->align;
|
|
|
|
|
|
|
|
_vk_mem_init (shared, _vulkan_memory_allocator, GST_MEMORY_CAST (mem),
|
|
|
|
parent->device, parent->alloc_info.memoryTypeIndex, ¶ms, size,
|
|
|
|
parent->properties, NULL, NULL);
|
|
|
|
shared->mem_ptr = parent->mem_ptr;
|
|
|
|
shared->wrapped = TRUE;
|
|
|
|
shared->vk_offset = offset + mem->vk_offset;
|
|
|
|
|
|
|
|
return GST_MEMORY_CAST (shared);
|
2015-10-24 06:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_vk_mem_is_span (GstVulkanMemory * mem1, GstVulkanMemory * mem2, gsize * offset)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstMemory *
|
|
|
|
_vk_mem_alloc (GstAllocator * allocator, gsize size,
|
|
|
|
GstAllocationParams * params)
|
|
|
|
{
|
|
|
|
g_critical ("Subclass should override GstAllocatorClass::alloc() function");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_vk_mem_free (GstAllocator * allocator, GstMemory * memory)
|
|
|
|
{
|
|
|
|
GstVulkanMemory *mem = (GstVulkanMemory *) memory;
|
|
|
|
|
|
|
|
GST_CAT_TRACE (GST_CAT_VULKAN_MEMORY, "freeing buffer memory:%p "
|
2015-12-29 05:05:17 +00:00
|
|
|
"id:%" G_GUINT64_FORMAT, mem, (guint64) mem->mem_ptr);
|
2015-10-24 06:29:05 +00:00
|
|
|
|
|
|
|
g_mutex_clear (&mem->lock);
|
|
|
|
|
|
|
|
if (mem->notify)
|
|
|
|
mem->notify (mem->user_data);
|
|
|
|
|
2016-02-08 01:22:11 +00:00
|
|
|
if (mem->mem_ptr && !mem->wrapped)
|
|
|
|
vkFreeMemory (mem->device->device, mem->mem_ptr, NULL);
|
2015-10-24 06:29:05 +00:00
|
|
|
|
|
|
|
gst_object_unref (mem->device);
|
2019-08-02 07:31:59 +00:00
|
|
|
|
|
|
|
g_free (mem);
|
2015-10-24 06:29:05 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 06:52:54 +00:00
|
|
|
/**
|
|
|
|
* gst_vulkan_memory_find_memory_type_index_with_type_properties:
|
|
|
|
* @device: a #GstVulkanDevice
|
|
|
|
* @type_bits: memory type bits to search for
|
|
|
|
* @properties: memory properties to search for
|
|
|
|
* @type_index: resulting index of the memory type
|
|
|
|
*
|
|
|
|
* Returns: whether a valid memory type could be found
|
|
|
|
*
|
|
|
|
* Since: 1.18
|
|
|
|
*/
|
2016-02-08 01:22:11 +00:00
|
|
|
gboolean
|
|
|
|
gst_vulkan_memory_find_memory_type_index_with_type_properties (GstVulkanDevice *
|
2019-04-11 06:52:54 +00:00
|
|
|
device, guint32 type_bits, VkMemoryPropertyFlags properties,
|
|
|
|
guint32 * type_index)
|
2016-02-08 01:22:11 +00:00
|
|
|
{
|
|
|
|
guint32 i;
|
|
|
|
|
|
|
|
/* Search memtypes to find first index with those properties */
|
|
|
|
for (i = 0; i < 32; i++) {
|
2019-04-11 06:52:54 +00:00
|
|
|
if ((type_bits & 1) == 1) {
|
2016-02-08 01:22:11 +00:00
|
|
|
/* Type is available, does it match user properties? */
|
2019-09-16 02:59:08 +00:00
|
|
|
if ((device->physical_device->memory_properties.memoryTypes[i].
|
2016-02-08 01:22:11 +00:00
|
|
|
propertyFlags & properties) == properties) {
|
2019-04-11 06:52:54 +00:00
|
|
|
*type_index = i;
|
2016-02-08 01:22:11 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2019-04-11 06:52:54 +00:00
|
|
|
type_bits >>= 1;
|
2016-02-08 01:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-10-24 06:29:05 +00:00
|
|
|
/**
|
|
|
|
* gst_vulkan_memory_alloc:
|
|
|
|
* @device:a #GstVulkanDevice
|
|
|
|
* @memory_type_index: the Vulkan memory type index
|
|
|
|
* @params: a #GstAllocationParams
|
|
|
|
* @size: the size to allocate
|
|
|
|
*
|
|
|
|
* Allocated a new #GstVulkanMemory.
|
|
|
|
*
|
|
|
|
* Returns: a #GstMemory object backed by a vulkan device memory
|
2019-04-11 06:52:54 +00:00
|
|
|
*
|
|
|
|
* Since: 1.18
|
2015-10-24 06:29:05 +00:00
|
|
|
*/
|
|
|
|
GstMemory *
|
|
|
|
gst_vulkan_memory_alloc (GstVulkanDevice * device, guint32 memory_type_index,
|
|
|
|
GstAllocationParams * params, gsize size, VkMemoryPropertyFlags mem_flags)
|
|
|
|
{
|
|
|
|
GstVulkanMemory *mem;
|
|
|
|
|
|
|
|
mem = _vk_mem_new (_vulkan_memory_allocator, NULL, device, memory_type_index,
|
|
|
|
params, size, mem_flags, NULL, NULL);
|
|
|
|
|
|
|
|
return (GstMemory *) mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GstVulkanMemoryAllocator, gst_vulkan_memory_allocator,
|
|
|
|
GST_TYPE_ALLOCATOR);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vulkan_memory_allocator_class_init (GstVulkanMemoryAllocatorClass * klass)
|
|
|
|
{
|
|
|
|
GstAllocatorClass *allocator_class = (GstAllocatorClass *) klass;
|
|
|
|
|
|
|
|
allocator_class->alloc = _vk_mem_alloc;
|
|
|
|
allocator_class->free = _vk_mem_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vulkan_memory_allocator_init (GstVulkanMemoryAllocator * allocator)
|
|
|
|
{
|
|
|
|
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
|
|
|
|
|
|
|
|
alloc->mem_type = GST_VULKAN_MEMORY_ALLOCATOR_NAME;
|
|
|
|
alloc->mem_map_full = (GstMemoryMapFullFunction) _vk_mem_map_full;
|
|
|
|
alloc->mem_unmap_full = (GstMemoryUnmapFullFunction) _vk_mem_unmap_full;
|
|
|
|
alloc->mem_copy = (GstMemoryCopyFunction) _vk_mem_copy;
|
|
|
|
alloc->mem_share = (GstMemoryShareFunction) _vk_mem_share;
|
|
|
|
alloc->mem_is_span = (GstMemoryIsSpanFunction) _vk_mem_is_span;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_vulkan_memory_init_once:
|
|
|
|
*
|
|
|
|
* Initializes the Vulkan memory allocator. It is safe to call this function
|
|
|
|
* multiple times. This must be called before any other #GstVulkanMemory operation.
|
2019-04-11 06:52:54 +00:00
|
|
|
*
|
|
|
|
* Since: 1.18
|
2015-10-24 06:29:05 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_vulkan_memory_init_once (void)
|
|
|
|
{
|
2021-03-22 03:34:36 +00:00
|
|
|
static gsize _init = 0;
|
2015-10-24 06:29:05 +00:00
|
|
|
|
|
|
|
if (g_once_init_enter (&_init)) {
|
|
|
|
GST_DEBUG_CATEGORY_INIT (GST_CAT_VULKAN_MEMORY, "vulkanmemory", 0,
|
|
|
|
"Vulkan Memory");
|
|
|
|
|
|
|
|
_vulkan_memory_allocator =
|
|
|
|
g_object_new (gst_vulkan_memory_allocator_get_type (), NULL);
|
2017-05-15 17:31:31 +00:00
|
|
|
gst_object_ref_sink (_vulkan_memory_allocator);
|
2015-10-24 06:29:05 +00:00
|
|
|
|
|
|
|
gst_allocator_register (GST_VULKAN_MEMORY_ALLOCATOR_NAME,
|
|
|
|
gst_object_ref (_vulkan_memory_allocator));
|
|
|
|
g_once_init_leave (&_init, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_is_vulkan_memory:
|
|
|
|
* @mem:a #GstMemory
|
2017-03-08 18:01:13 +00:00
|
|
|
*
|
2015-10-24 06:29:05 +00:00
|
|
|
* Returns: whether the memory at @mem is a #GstVulkanMemory
|
2019-04-11 06:52:54 +00:00
|
|
|
*
|
|
|
|
* Since: 1.18
|
2015-10-24 06:29:05 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_is_vulkan_memory (GstMemory * mem)
|
|
|
|
{
|
|
|
|
return mem != NULL && mem->allocator != NULL &&
|
|
|
|
g_type_is_a (G_OBJECT_TYPE (mem->allocator),
|
|
|
|
GST_TYPE_VULKAN_MEMORY_ALLOCATOR);
|
|
|
|
}
|