2023-08-12 20:10:16 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gskvulkanbufferprivate.h"
|
|
|
|
|
|
|
|
#include "gskvulkandeviceprivate.h"
|
|
|
|
#include "gskvulkanmemoryprivate.h"
|
|
|
|
|
|
|
|
struct _GskVulkanBuffer
|
|
|
|
{
|
2023-08-21 00:18:37 +00:00
|
|
|
GskGpuBuffer parent_instance;
|
|
|
|
|
2023-08-12 20:10:16 +00:00
|
|
|
GskVulkanDevice *device;
|
|
|
|
|
|
|
|
VkBuffer vk_buffer;
|
|
|
|
|
|
|
|
GskVulkanAllocator *allocator;
|
|
|
|
GskVulkanAllocation allocation;
|
|
|
|
};
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
G_DEFINE_TYPE (GskVulkanBuffer, gsk_vulkan_buffer, GSK_TYPE_GPU_BUFFER)
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_buffer_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GskVulkanBuffer *self = GSK_VULKAN_BUFFER (object);
|
|
|
|
|
|
|
|
vkDestroyBuffer (gsk_vulkan_device_get_vk_device (self->device),
|
|
|
|
self->vk_buffer,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gsk_vulkan_free (self->allocator, &self->allocation);
|
|
|
|
gsk_vulkan_allocator_unref (self->allocator);
|
|
|
|
|
|
|
|
g_object_unref (self->device);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gsk_vulkan_buffer_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static guchar *
|
|
|
|
gsk_vulkan_buffer_map (GskGpuBuffer *buffer)
|
|
|
|
{
|
|
|
|
GskVulkanBuffer *self = GSK_VULKAN_BUFFER (buffer);
|
|
|
|
|
|
|
|
return self->allocation.map;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-03-08 21:54:53 +00:00
|
|
|
gsk_vulkan_buffer_unmap (GskGpuBuffer *buffer,
|
|
|
|
gsize size)
|
2023-08-21 00:18:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_buffer_class_init (GskVulkanBufferClass *klass)
|
|
|
|
{
|
|
|
|
GskGpuBufferClass *buffer_class = GSK_GPU_BUFFER_CLASS (klass);
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
buffer_class->map = gsk_vulkan_buffer_map;
|
|
|
|
buffer_class->unmap = gsk_vulkan_buffer_unmap;
|
|
|
|
|
|
|
|
gobject_class->finalize = gsk_vulkan_buffer_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_buffer_init (GskVulkanBuffer *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static GskGpuBuffer *
|
2023-08-12 20:10:16 +00:00
|
|
|
gsk_vulkan_buffer_new_internal (GskVulkanDevice *device,
|
|
|
|
gsize size,
|
|
|
|
VkBufferUsageFlags usage)
|
|
|
|
{
|
|
|
|
VkMemoryRequirements requirements;
|
|
|
|
GskVulkanBuffer *self;
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
self = g_object_new (GSK_TYPE_VULKAN_BUFFER, NULL);
|
2023-08-12 20:10:16 +00:00
|
|
|
|
|
|
|
self->device = g_object_ref (device);
|
|
|
|
|
|
|
|
GSK_VK_CHECK (vkCreateBuffer, gsk_vulkan_device_get_vk_device (device),
|
|
|
|
&(VkBufferCreateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
|
|
|
.size = size,
|
|
|
|
.flags = 0,
|
|
|
|
.usage = usage,
|
|
|
|
.sharingMode = VK_SHARING_MODE_EXCLUSIVE
|
|
|
|
},
|
|
|
|
NULL,
|
|
|
|
&self->vk_buffer);
|
|
|
|
|
|
|
|
vkGetBufferMemoryRequirements (gsk_vulkan_device_get_vk_device (device),
|
|
|
|
self->vk_buffer,
|
|
|
|
&requirements);
|
|
|
|
|
|
|
|
self->allocator = gsk_vulkan_device_find_allocator (device,
|
|
|
|
requirements.memoryTypeBits,
|
|
|
|
GSK_VULKAN_MEMORY_MAPPABLE,
|
2024-03-16 21:32:03 +00:00
|
|
|
GSK_VULKAN_MEMORY_MAPPABLE |
|
|
|
|
VK_MEMORY_PROPERTY_HOST_CACHED_BIT);
|
2023-08-12 20:10:16 +00:00
|
|
|
gsk_vulkan_alloc (self->allocator,
|
|
|
|
requirements.size,
|
|
|
|
requirements.alignment,
|
|
|
|
&self->allocation);
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
gsk_gpu_buffer_setup (GSK_GPU_BUFFER (self), self->allocation.size);
|
|
|
|
|
2023-08-12 20:10:16 +00:00
|
|
|
GSK_VK_CHECK (vkBindBufferMemory, gsk_vulkan_device_get_vk_device (device),
|
|
|
|
self->vk_buffer,
|
|
|
|
self->allocation.vk_memory,
|
|
|
|
self->allocation.offset);
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
return GSK_GPU_BUFFER (self);
|
2023-08-12 20:10:16 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
GskGpuBuffer *
|
|
|
|
gsk_vulkan_buffer_new_vertex (GskVulkanDevice *device,
|
|
|
|
gsize size)
|
2023-08-12 20:10:16 +00:00
|
|
|
{
|
|
|
|
return gsk_vulkan_buffer_new_internal (device, size,
|
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
|
|
|
|
| VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
GskGpuBuffer *
|
2023-08-12 20:10:16 +00:00
|
|
|
gsk_vulkan_buffer_new_storage (GskVulkanDevice *device,
|
|
|
|
gsize size)
|
|
|
|
{
|
|
|
|
return gsk_vulkan_buffer_new_internal (device, size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
GskGpuBuffer *
|
|
|
|
gsk_vulkan_buffer_new_write (GskVulkanDevice *device,
|
|
|
|
gsize size)
|
2023-08-12 20:10:16 +00:00
|
|
|
{
|
2023-08-21 00:18:37 +00:00
|
|
|
return gsk_vulkan_buffer_new_internal (device, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
|
2023-08-12 20:10:16 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
GskGpuBuffer *
|
|
|
|
gsk_vulkan_buffer_new_read (GskVulkanDevice *device,
|
|
|
|
gsize size)
|
2023-08-12 20:10:16 +00:00
|
|
|
{
|
2023-08-21 00:18:37 +00:00
|
|
|
return gsk_vulkan_buffer_new_internal (device, size, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
|
2023-08-12 20:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VkBuffer
|
2023-08-21 00:18:37 +00:00
|
|
|
gsk_vulkan_buffer_get_vk_buffer (GskVulkanBuffer *self)
|
2023-08-12 20:10:16 +00:00
|
|
|
{
|
|
|
|
return self->vk_buffer;
|
|
|
|
}
|
|
|
|
|