2016-12-05 01:50:06 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gskvulkanbufferprivate.h"
|
|
|
|
#include "gskvulkanmemoryprivate.h"
|
|
|
|
#include "gskvulkanpipelineprivate.h"
|
|
|
|
|
|
|
|
struct _GskVulkanBuffer
|
|
|
|
{
|
|
|
|
GdkVulkanContext *vulkan;
|
|
|
|
|
|
|
|
gsize size;
|
|
|
|
|
|
|
|
VkBuffer vk_buffer;
|
|
|
|
|
|
|
|
GskVulkanMemory *memory;
|
|
|
|
};
|
|
|
|
|
2016-12-15 23:22:23 +00:00
|
|
|
static GskVulkanBuffer *
|
|
|
|
gsk_vulkan_buffer_new_internal (GdkVulkanContext *context,
|
|
|
|
gsize size,
|
|
|
|
VkBufferUsageFlags usage)
|
2016-12-05 01:50:06 +00:00
|
|
|
{
|
|
|
|
VkMemoryRequirements requirements;
|
|
|
|
GskVulkanBuffer *self;
|
|
|
|
|
2023-03-03 11:48:49 +00:00
|
|
|
self = g_new0 (GskVulkanBuffer, 1);
|
2016-12-05 01:50:06 +00:00
|
|
|
|
|
|
|
self->vulkan = g_object_ref (context);
|
|
|
|
self->size = size;
|
|
|
|
|
|
|
|
GSK_VK_CHECK (vkCreateBuffer, gdk_vulkan_context_get_device (context),
|
|
|
|
&(VkBufferCreateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
|
|
|
.size = size,
|
|
|
|
.flags = 0,
|
2016-12-15 23:22:23 +00:00
|
|
|
.usage = usage,
|
2016-12-05 01:50:06 +00:00
|
|
|
.sharingMode = VK_SHARING_MODE_EXCLUSIVE
|
|
|
|
},
|
|
|
|
NULL,
|
|
|
|
&self->vk_buffer);
|
|
|
|
|
|
|
|
vkGetBufferMemoryRequirements (gdk_vulkan_context_get_device (context),
|
|
|
|
self->vk_buffer,
|
|
|
|
&requirements);
|
|
|
|
|
|
|
|
self->memory = gsk_vulkan_memory_new (context,
|
|
|
|
requirements.memoryTypeBits,
|
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
gsk/vulkan/buffer: Pass aligned memory value
This was a tricky one to figure out, but it's pretty simple to
understand (I hope!).
So, this AMD card I'm using requires buffer memory sizes to be
aligned to 16 bytes. Intel is aligned to 4 bytes I think, but
AMD - or at least this AMD model in particular - uses 16 bytes
for alignment.
When creating a a particular texture (I did not determin which one
specifically!) a buffer of size 1276 bytes is requested.
1276 / 16 = 79.75, which is clearly not aligned to the required
16 bytes.
We request Vulkan to create a buffer of 1276 bytes for us, it
figures out that it's not aligned, and creates a buffer of 1280
bytes, which is aligned. The extra 4 bytes are wasted, but that's
okay. We immediately query this buffer for this exact information,
using vkGetBufferMemoryRequirements(), and proceed to create actual
memory to back this buffer up.
The buffer tells us we must use 1280 bytes, so we pass 1280 bytes
and everyone is happy, right? Of course not. We pass 1276 bytes,
and Vulkan is subtly unhappy at us.
Fix that by passing the value that Vulkan asks us to use, i.e.,
the size returned by vkGetBufferMemoryRequirements().
2023-04-02 21:23:21 +00:00
|
|
|
requirements.size);
|
2016-12-05 01:50:06 +00:00
|
|
|
|
|
|
|
GSK_VK_CHECK (vkBindBufferMemory, gdk_vulkan_context_get_device (context),
|
|
|
|
self->vk_buffer,
|
|
|
|
gsk_vulkan_memory_get_device_memory (self->memory),
|
|
|
|
0);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:22:23 +00:00
|
|
|
GskVulkanBuffer *
|
|
|
|
gsk_vulkan_buffer_new (GdkVulkanContext *context,
|
|
|
|
gsize size)
|
|
|
|
{
|
|
|
|
return gsk_vulkan_buffer_new_internal (context, size,
|
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
|
|
|
|
| VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
GskVulkanBuffer *
|
2023-05-25 20:21:23 +00:00
|
|
|
gsk_vulkan_buffer_new_storage (GdkVulkanContext *context,
|
|
|
|
gsize size)
|
|
|
|
{
|
|
|
|
return gsk_vulkan_buffer_new_internal (context, size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
GskVulkanBuffer *
|
2023-06-15 12:29:15 +00:00
|
|
|
gsk_vulkan_buffer_new_map (GdkVulkanContext *context,
|
|
|
|
gsize size,
|
|
|
|
GskVulkanMapMode mode)
|
2016-12-15 23:22:23 +00:00
|
|
|
{
|
2023-06-15 12:29:15 +00:00
|
|
|
return gsk_vulkan_buffer_new_internal (context,
|
|
|
|
size,
|
2023-06-20 13:32:23 +00:00
|
|
|
(mode & GSK_VULKAN_READ ? VK_BUFFER_USAGE_TRANSFER_DST_BIT : 0) |
|
|
|
|
(mode & GSK_VULKAN_WRITE ? VK_BUFFER_USAGE_TRANSFER_SRC_BIT : 0));
|
2016-12-15 23:22:23 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 01:50:06 +00:00
|
|
|
void
|
|
|
|
gsk_vulkan_buffer_free (GskVulkanBuffer *self)
|
|
|
|
{
|
|
|
|
vkDestroyBuffer (gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
self->vk_buffer,
|
|
|
|
NULL);
|
|
|
|
|
2017-12-14 01:14:58 +00:00
|
|
|
gsk_vulkan_memory_free (self->memory);
|
|
|
|
|
2016-12-05 01:50:06 +00:00
|
|
|
g_object_unref (self->vulkan);
|
|
|
|
|
2023-03-03 11:48:49 +00:00
|
|
|
g_free (self);
|
2016-12-05 01:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VkBuffer
|
|
|
|
gsk_vulkan_buffer_get_buffer (GskVulkanBuffer *self)
|
|
|
|
{
|
|
|
|
return self->vk_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
guchar *
|
|
|
|
gsk_vulkan_buffer_map (GskVulkanBuffer *self)
|
|
|
|
{
|
|
|
|
return gsk_vulkan_memory_map (self->memory);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_vulkan_buffer_unmap (GskVulkanBuffer *self)
|
|
|
|
{
|
|
|
|
gsk_vulkan_memory_unmap (self->memory);
|
|
|
|
}
|