2016-12-05 01:50:06 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gskvulkanmemoryprivate.h"
|
|
|
|
|
2023-07-03 13:57:30 +00:00
|
|
|
#include "gskvulkanprivate.h"
|
|
|
|
|
2016-12-05 01:50:06 +00:00
|
|
|
struct _GskVulkanMemory
|
|
|
|
{
|
|
|
|
GdkVulkanContext *vulkan;
|
|
|
|
|
|
|
|
gsize size;
|
|
|
|
|
2023-06-14 00:55:32 +00:00
|
|
|
VkMemoryType vk_memory_type;
|
2016-12-05 01:50:06 +00:00
|
|
|
VkDeviceMemory vk_memory;
|
|
|
|
};
|
|
|
|
|
|
|
|
GskVulkanMemory *
|
|
|
|
gsk_vulkan_memory_new (GdkVulkanContext *context,
|
|
|
|
uint32_t allowed_types,
|
|
|
|
VkMemoryPropertyFlags flags,
|
|
|
|
gsize size)
|
|
|
|
{
|
|
|
|
VkPhysicalDeviceMemoryProperties properties;
|
|
|
|
GskVulkanMemory *self;
|
|
|
|
uint32_t i;
|
|
|
|
|
2023-03-03 11:48:49 +00:00
|
|
|
self = g_new0 (GskVulkanMemory, 1);
|
2016-12-05 01:50:06 +00:00
|
|
|
|
|
|
|
self->vulkan = g_object_ref (context);
|
|
|
|
self->size = size;
|
|
|
|
|
|
|
|
vkGetPhysicalDeviceMemoryProperties (gdk_vulkan_context_get_physical_device (context),
|
|
|
|
&properties);
|
|
|
|
|
|
|
|
for (i = 0; i < properties.memoryTypeCount; i++)
|
|
|
|
{
|
|
|
|
if (!(allowed_types & (1 << i)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((properties.memoryTypes[i].propertyFlags & flags) == flags)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (i < properties.memoryTypeCount);
|
|
|
|
|
2023-06-14 00:55:32 +00:00
|
|
|
self->vk_memory_type = properties.memoryTypes[i];
|
2016-12-05 01:50:06 +00:00
|
|
|
GSK_VK_CHECK (vkAllocateMemory, gdk_vulkan_context_get_device (context),
|
|
|
|
&(VkMemoryAllocateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
|
|
|
.allocationSize = size,
|
|
|
|
.memoryTypeIndex = i
|
|
|
|
},
|
|
|
|
NULL,
|
|
|
|
&self->vk_memory);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_vulkan_memory_free (GskVulkanMemory *self)
|
|
|
|
{
|
|
|
|
vkFreeMemory (gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
self->vk_memory,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_object_unref (self->vulkan);
|
|
|
|
|
2023-03-03 11:48:49 +00:00
|
|
|
g_free (self);
|
2016-12-05 01:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VkDeviceMemory
|
|
|
|
gsk_vulkan_memory_get_device_memory (GskVulkanMemory *self)
|
|
|
|
{
|
|
|
|
return self->vk_memory;
|
|
|
|
}
|
|
|
|
|
2023-06-14 00:55:32 +00:00
|
|
|
gboolean
|
|
|
|
gsk_vulkan_memory_can_map (GskVulkanMemory *self,
|
|
|
|
gboolean fast)
|
|
|
|
{
|
|
|
|
if (!(self->vk_memory_type.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* FIXME: no support implemented for this */
|
|
|
|
if (!(self->vk_memory_type.propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!fast)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (!(self->vk_memory_type.propertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-12-05 01:50:06 +00:00
|
|
|
guchar *
|
|
|
|
gsk_vulkan_memory_map (GskVulkanMemory *self)
|
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
|
2023-06-14 00:55:32 +00:00
|
|
|
g_assert (gsk_vulkan_memory_can_map (self, FALSE));
|
|
|
|
|
2016-12-05 01:50:06 +00:00
|
|
|
GSK_VK_CHECK (vkMapMemory, gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
self->vk_memory,
|
|
|
|
0,
|
|
|
|
self->size,
|
|
|
|
0,
|
|
|
|
&data);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_vulkan_memory_unmap (GskVulkanMemory *self)
|
|
|
|
{
|
|
|
|
vkUnmapMemory (gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
self->vk_memory);
|
|
|
|
}
|
|
|
|
|