2016-12-06 13:42:46 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gskvulkanimageprivate.h"
|
2016-12-15 23:22:23 +00:00
|
|
|
|
|
|
|
#include "gskvulkanbufferprivate.h"
|
2016-12-06 13:42:46 +00:00
|
|
|
#include "gskvulkanmemoryprivate.h"
|
|
|
|
#include "gskvulkanpipelineprivate.h"
|
|
|
|
|
2023-06-15 12:29:15 +00:00
|
|
|
#include "gdk/gdkmemoryformatprivate.h"
|
|
|
|
|
2023-06-15 21:17:40 +00:00
|
|
|
#include "gskrendernodeprivate.h"
|
|
|
|
|
2016-12-06 13:42:46 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-12-17 03:22:44 +00:00
|
|
|
struct _GskVulkanUploader
|
|
|
|
{
|
|
|
|
GdkVulkanContext *vulkan;
|
|
|
|
|
|
|
|
GskVulkanCommandPool *command_pool;
|
|
|
|
|
2017-09-20 23:11:42 +00:00
|
|
|
GArray *before_buffer_barriers;
|
|
|
|
GArray *before_image_barriers;
|
2016-12-17 03:54:19 +00:00
|
|
|
VkCommandBuffer copy_buffer;
|
2017-09-20 23:11:42 +00:00
|
|
|
GArray *after_buffer_barriers;
|
|
|
|
GArray *after_image_barriers;
|
2016-12-17 03:54:19 +00:00
|
|
|
|
2016-12-17 03:22:44 +00:00
|
|
|
GSList *staging_image_free_list;
|
|
|
|
GSList *staging_buffer_free_list;
|
|
|
|
};
|
|
|
|
|
2016-12-06 13:42:46 +00:00
|
|
|
struct _GskVulkanImage
|
|
|
|
{
|
2016-12-08 19:19:56 +00:00
|
|
|
GObject parent_instance;
|
|
|
|
|
2016-12-06 13:42:46 +00:00
|
|
|
GdkVulkanContext *vulkan;
|
|
|
|
|
2023-06-14 18:37:58 +00:00
|
|
|
GdkMemoryFormat format;
|
2023-06-17 14:05:09 +00:00
|
|
|
VkFormat vk_format;
|
2016-12-08 19:46:02 +00:00
|
|
|
gsize width;
|
|
|
|
gsize height;
|
2017-09-20 23:11:42 +00:00
|
|
|
VkImageUsageFlags vk_usage;
|
2016-12-06 13:42:46 +00:00
|
|
|
VkImage vk_image;
|
|
|
|
VkImageView vk_image_view;
|
2017-09-20 23:11:42 +00:00
|
|
|
VkImageLayout vk_image_layout;
|
|
|
|
VkAccessFlags vk_access;
|
2016-12-06 13:42:46 +00:00
|
|
|
|
|
|
|
GskVulkanMemory *memory;
|
|
|
|
};
|
|
|
|
|
2016-12-08 19:19:56 +00:00
|
|
|
G_DEFINE_TYPE (GskVulkanImage, gsk_vulkan_image, G_TYPE_OBJECT)
|
|
|
|
|
2016-12-17 03:22:44 +00:00
|
|
|
GskVulkanUploader *
|
|
|
|
gsk_vulkan_uploader_new (GdkVulkanContext *context,
|
|
|
|
GskVulkanCommandPool *command_pool)
|
|
|
|
{
|
|
|
|
GskVulkanUploader *self;
|
|
|
|
|
2023-03-03 11:48:49 +00:00
|
|
|
self = g_new0 (GskVulkanUploader, 1);
|
2016-12-17 03:22:44 +00:00
|
|
|
|
|
|
|
self->vulkan = g_object_ref (context);
|
|
|
|
self->command_pool = command_pool;
|
|
|
|
|
2017-09-20 23:11:42 +00:00
|
|
|
self->before_buffer_barriers = g_array_new (FALSE, FALSE, sizeof (VkBufferMemoryBarrier));
|
|
|
|
self->after_buffer_barriers = g_array_new (FALSE, FALSE, sizeof (VkBufferMemoryBarrier));
|
|
|
|
|
|
|
|
self->before_image_barriers = g_array_new (FALSE, FALSE, sizeof (VkImageMemoryBarrier));
|
|
|
|
self->after_image_barriers = g_array_new (FALSE, FALSE, sizeof (VkImageMemoryBarrier));
|
2016-12-17 03:54:19 +00:00
|
|
|
|
2016-12-17 03:22:44 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_vulkan_uploader_free (GskVulkanUploader *self)
|
|
|
|
{
|
|
|
|
gsk_vulkan_uploader_reset (self);
|
|
|
|
|
2017-09-20 23:11:42 +00:00
|
|
|
g_array_unref (self->after_buffer_barriers);
|
|
|
|
g_array_unref (self->before_buffer_barriers);
|
|
|
|
g_array_unref (self->after_image_barriers);
|
|
|
|
g_array_unref (self->before_image_barriers);
|
2016-12-17 03:54:19 +00:00
|
|
|
|
2016-12-17 03:22:44 +00:00
|
|
|
g_object_unref (self->vulkan);
|
|
|
|
|
2023-03-03 11:48:49 +00:00
|
|
|
g_free (self);
|
2016-12-17 03:22:44 +00:00
|
|
|
}
|
|
|
|
|
2016-12-17 03:54:19 +00:00
|
|
|
static void
|
|
|
|
gsk_vulkan_uploader_add_image_barrier (GskVulkanUploader *self,
|
|
|
|
gboolean after,
|
2017-09-20 23:11:42 +00:00
|
|
|
GskVulkanImage *image,
|
|
|
|
VkImageLayout new_layout,
|
|
|
|
VkAccessFlags new_access)
|
2016-12-17 03:54:19 +00:00
|
|
|
{
|
|
|
|
GArray *array;
|
2017-09-20 23:11:42 +00:00
|
|
|
VkImageMemoryBarrier barrier = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
|
|
|
|
.srcAccessMask = image->vk_access,
|
|
|
|
.dstAccessMask = new_access,
|
|
|
|
.oldLayout = image->vk_image_layout,
|
|
|
|
.newLayout = new_layout,
|
|
|
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
.image = image->vk_image,
|
|
|
|
.subresourceRange = {
|
|
|
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
.baseMipLevel = 0,
|
|
|
|
.levelCount = 1,
|
|
|
|
.baseArrayLayer = 0,
|
|
|
|
.layerCount = 1
|
|
|
|
}
|
|
|
|
};
|
2016-12-17 03:54:19 +00:00
|
|
|
|
|
|
|
if (after)
|
2017-09-20 23:11:42 +00:00
|
|
|
array = self->after_image_barriers;
|
2016-12-17 03:54:19 +00:00
|
|
|
else
|
2017-09-20 23:11:42 +00:00
|
|
|
array = self->before_image_barriers;
|
|
|
|
|
|
|
|
g_array_append_val (array, barrier);
|
|
|
|
|
|
|
|
image->vk_image_layout = new_layout;
|
|
|
|
image->vk_access = new_access;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_uploader_add_buffer_barrier (GskVulkanUploader *self,
|
|
|
|
gboolean after,
|
|
|
|
const VkBufferMemoryBarrier *barrier)
|
|
|
|
{
|
|
|
|
GArray *array;
|
|
|
|
|
|
|
|
if (after)
|
|
|
|
array = self->after_buffer_barriers;
|
|
|
|
else
|
|
|
|
array = self->before_buffer_barriers;
|
2016-12-17 03:54:19 +00:00
|
|
|
|
|
|
|
g_array_append_val (array, *barrier);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VkCommandBuffer
|
|
|
|
gsk_vulkan_uploader_get_copy_buffer (GskVulkanUploader *self)
|
|
|
|
{
|
|
|
|
if (self->copy_buffer == VK_NULL_HANDLE)
|
|
|
|
self->copy_buffer = gsk_vulkan_command_pool_get_buffer (self->command_pool);
|
|
|
|
|
|
|
|
return self->copy_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_vulkan_uploader_upload (GskVulkanUploader *self)
|
|
|
|
{
|
2019-10-04 14:52:43 +00:00
|
|
|
VkPipelineStageFlagBits host_and_transfer_bits = VK_PIPELINE_STAGE_HOST_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT;
|
|
|
|
|
2017-09-20 23:11:42 +00:00
|
|
|
if (self->before_buffer_barriers->len > 0 || self->before_image_barriers->len > 0)
|
2016-12-17 03:54:19 +00:00
|
|
|
{
|
|
|
|
VkCommandBuffer command_buffer;
|
|
|
|
|
|
|
|
command_buffer = gsk_vulkan_command_pool_get_buffer (self->command_pool);
|
|
|
|
vkCmdPipelineBarrier (command_buffer,
|
2019-10-04 14:52:43 +00:00
|
|
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | host_and_transfer_bits,
|
2017-09-26 22:18:31 +00:00
|
|
|
VK_PIPELINE_STAGE_HOST_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT,
|
2016-12-17 03:54:19 +00:00
|
|
|
0,
|
|
|
|
0, NULL,
|
2017-09-20 23:11:42 +00:00
|
|
|
self->before_buffer_barriers->len, (VkBufferMemoryBarrier *) self->before_buffer_barriers->data,
|
|
|
|
self->before_image_barriers->len, (VkImageMemoryBarrier *) self->before_image_barriers->data);
|
2017-09-28 00:09:35 +00:00
|
|
|
gsk_vulkan_command_pool_submit_buffer (self->command_pool, command_buffer, 0, NULL, 0, NULL, VK_NULL_HANDLE);
|
2017-09-20 23:11:42 +00:00
|
|
|
g_array_set_size (self->before_buffer_barriers, 0);
|
|
|
|
g_array_set_size (self->before_image_barriers, 0);
|
2016-12-17 03:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* append these to existing buffer */
|
2017-09-20 23:11:42 +00:00
|
|
|
if (self->after_buffer_barriers->len > 0 || self->after_image_barriers->len > 0)
|
2016-12-17 03:54:19 +00:00
|
|
|
{
|
|
|
|
VkCommandBuffer command_buffer = gsk_vulkan_uploader_get_copy_buffer (self);
|
|
|
|
vkCmdPipelineBarrier (command_buffer,
|
2019-10-04 14:52:43 +00:00
|
|
|
host_and_transfer_bits,
|
2017-09-26 22:18:31 +00:00
|
|
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
2016-12-17 03:54:19 +00:00
|
|
|
0,
|
|
|
|
0, NULL,
|
2017-09-20 23:11:42 +00:00
|
|
|
self->after_buffer_barriers->len, (VkBufferMemoryBarrier *) self->after_buffer_barriers->data,
|
|
|
|
self->after_image_barriers->len, (VkImageMemoryBarrier *) self->after_image_barriers->data);
|
|
|
|
g_array_set_size (self->after_buffer_barriers, 0);
|
|
|
|
g_array_set_size (self->after_image_barriers, 0);
|
2016-12-17 03:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (self->copy_buffer != VK_NULL_HANDLE)
|
|
|
|
{
|
2017-09-28 00:09:35 +00:00
|
|
|
gsk_vulkan_command_pool_submit_buffer (self->command_pool, self->copy_buffer, 0, NULL, 0, NULL, VK_NULL_HANDLE);
|
2016-12-17 03:54:19 +00:00
|
|
|
self->copy_buffer = VK_NULL_HANDLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 03:22:44 +00:00
|
|
|
void
|
|
|
|
gsk_vulkan_uploader_reset (GskVulkanUploader *self)
|
|
|
|
{
|
2017-09-20 23:11:42 +00:00
|
|
|
g_array_set_size (self->before_image_barriers, 0);
|
2016-12-17 03:54:19 +00:00
|
|
|
self->copy_buffer = VK_NULL_HANDLE;
|
2017-09-20 23:11:42 +00:00
|
|
|
g_array_set_size (self->after_image_barriers, 0);
|
2016-12-17 03:54:19 +00:00
|
|
|
|
2016-12-17 03:22:44 +00:00
|
|
|
g_slist_free_full (self->staging_image_free_list, g_object_unref);
|
|
|
|
self->staging_image_free_list = NULL;
|
|
|
|
g_slist_free_full (self->staging_buffer_free_list, (GDestroyNotify) gsk_vulkan_buffer_free);
|
|
|
|
self->staging_buffer_free_list = NULL;
|
|
|
|
}
|
|
|
|
|
2023-06-14 18:37:58 +00:00
|
|
|
typedef struct _GskMemoryFormatInfo GskMemoryFormatInfo;
|
|
|
|
|
|
|
|
struct _GskMemoryFormatInfo
|
|
|
|
{
|
|
|
|
VkFormat format;
|
|
|
|
VkComponentMapping components;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const GskMemoryFormatInfo *
|
|
|
|
gsk_memory_format_get_vk_format_infos (GdkMemoryFormat format)
|
|
|
|
{
|
|
|
|
#define SWIZZLE(a, b, c, d) { VK_COMPONENT_SWIZZLE_ ## a, VK_COMPONENT_SWIZZLE_ ## b, VK_COMPONENT_SWIZZLE_ ## c, VK_COMPONENT_SWIZZLE_ ## d }
|
|
|
|
#define DEFAULT_SWIZZLE SWIZZLE (R, G, B, A)
|
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case GDK_MEMORY_B8G8R8A8_PREMULTIPLIED:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_B8G8R8A8_UNORM, DEFAULT_SWIZZLE },
|
|
|
|
{ VK_FORMAT_R8G8B8A8_UNORM, SWIZZLE(B, G, R, A) },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_A8R8G8B8_PREMULTIPLIED:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R8G8B8A8_UNORM, SWIZZLE(G, B, A, R) },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_R8G8B8A8_PREMULTIPLIED:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R8G8B8A8_UNORM, DEFAULT_SWIZZLE },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
GDK_MEMORY_B8G8R8A8,
|
|
|
|
GDK_MEMORY_A8R8G8B8,
|
|
|
|
GDK_MEMORY_R8G8B8A8,
|
|
|
|
GDK_MEMORY_A8B8G8R8,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case GDK_MEMORY_R8G8B8:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R8G8B8_UNORM, DEFAULT_SWIZZLE },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_B8G8R8:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_B8G8R8_UNORM, DEFAULT_SWIZZLE },
|
|
|
|
{ VK_FORMAT_R8G8B8_UNORM, SWIZZLE(B, G, R, A) },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_R16G16B16:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R16G16B16_UNORM, DEFAULT_SWIZZLE },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_R16G16B16A16_PREMULTIPLIED:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R16G16B16A16_UNORM, DEFAULT_SWIZZLE },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
GDK_MEMORY_R16G16B16A16,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case GDK_MEMORY_R16G16B16_FLOAT:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R16G16B16_SFLOAT, DEFAULT_SWIZZLE },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R16G16B16A16_SFLOAT, DEFAULT_SWIZZLE },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
GDK_MEMORY_R16G16B16A16_FLOAT,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case GDK_MEMORY_R32G32B32_FLOAT:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R32G32B32_SFLOAT, DEFAULT_SWIZZLE },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R32G32B32A32_SFLOAT, DEFAULT_SWIZZLE },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
GDK_MEMORY_R32G32B32A32_FLOAT,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case GDK_MEMORY_G8A8_PREMULTIPLIED:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R8G8_UNORM, SWIZZLE (R, R, R, G) },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
GDK_MEMORY_G8A8,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case GDK_MEMORY_G8:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R8_UNORM, SWIZZLE (R, R, R, ONE) },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_G16A16_PREMULTIPLIED:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R16G16_UNORM, SWIZZLE (R, R, R, G) },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
GDK_MEMORY_G16A16
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case GDK_MEMORY_G16:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R16_UNORM, SWIZZLE (R, R, R, ONE) },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_A8:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R8_UNORM, SWIZZLE (R, R, R, R) },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_A16:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_R16_UNORM, SWIZZLE (R, R, R, R) },
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_B8G8R8A8:
|
|
|
|
case GDK_MEMORY_A8R8G8B8:
|
|
|
|
case GDK_MEMORY_R8G8B8A8:
|
|
|
|
case GDK_MEMORY_A8B8G8R8:
|
|
|
|
case GDK_MEMORY_R16G16B16A16:
|
|
|
|
case GDK_MEMORY_R16G16B16A16_FLOAT:
|
|
|
|
case GDK_MEMORY_R32G32B32A32_FLOAT:
|
|
|
|
case GDK_MEMORY_G8A8:
|
|
|
|
case GDK_MEMORY_G16A16:
|
|
|
|
{
|
|
|
|
static const GskMemoryFormatInfo info[] = {
|
|
|
|
{ VK_FORMAT_UNDEFINED }
|
|
|
|
};
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GDK_MEMORY_N_FORMATS:
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#undef DEFAULT_SWIZZLE
|
|
|
|
#undef SWIZZLE
|
|
|
|
}
|
|
|
|
|
2023-06-15 21:13:31 +00:00
|
|
|
static GdkMemoryFormat
|
|
|
|
gsk_memory_format_get_fallback (GdkMemoryFormat format)
|
|
|
|
{
|
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case GDK_MEMORY_B8G8R8A8_PREMULTIPLIED:
|
|
|
|
return GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_A8R8G8B8_PREMULTIPLIED:
|
|
|
|
return GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_R8G8B8A8_PREMULTIPLIED:
|
|
|
|
return GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_B8G8R8A8:
|
|
|
|
return GDK_MEMORY_B8G8R8A8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_A8R8G8B8:
|
|
|
|
return GDK_MEMORY_A8R8G8B8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_R8G8B8A8:
|
|
|
|
return GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_A8B8G8R8:
|
|
|
|
return GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_R8G8B8:
|
|
|
|
return GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_B8G8R8:
|
|
|
|
return GDK_MEMORY_R8G8B8;
|
|
|
|
case GDK_MEMORY_R16G16B16:
|
|
|
|
return GDK_MEMORY_R16G16B16A16_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_R16G16B16A16_PREMULTIPLIED:
|
|
|
|
return GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_R16G16B16A16:
|
|
|
|
return GDK_MEMORY_R16G16B16A16_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_R16G16B16_FLOAT:
|
|
|
|
return GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED:
|
|
|
|
return GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_R16G16B16A16_FLOAT:
|
|
|
|
return GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_R32G32B32_FLOAT:
|
|
|
|
return GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED:
|
|
|
|
return GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_R32G32B32A32_FLOAT:
|
|
|
|
return GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_G8A8_PREMULTIPLIED:
|
|
|
|
return GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_G8A8:
|
|
|
|
return GDK_MEMORY_G8A8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_G8:
|
|
|
|
return GDK_MEMORY_R8G8B8;
|
|
|
|
case GDK_MEMORY_G16A16_PREMULTIPLIED:
|
|
|
|
return GDK_MEMORY_R16G16B16A16_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_G16A16:
|
|
|
|
return GDK_MEMORY_G16A16_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_G16:
|
|
|
|
return GDK_MEMORY_R16G16B16;
|
|
|
|
case GDK_MEMORY_A8:
|
|
|
|
return GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
case GDK_MEMORY_A16:
|
|
|
|
return GDK_MEMORY_R16G16B16A16_PREMULTIPLIED;
|
|
|
|
|
|
|
|
case GDK_MEMORY_N_FORMATS:
|
|
|
|
default:
|
|
|
|
return GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 18:37:58 +00:00
|
|
|
static gboolean
|
|
|
|
gsk_vulkan_context_supports_format (GdkVulkanContext *context,
|
|
|
|
VkFormat format)
|
|
|
|
{
|
|
|
|
VkFormatProperties properties;
|
|
|
|
|
|
|
|
vkGetPhysicalDeviceFormatProperties (gdk_vulkan_context_get_physical_device (context),
|
|
|
|
format,
|
|
|
|
&properties);
|
|
|
|
|
|
|
|
if ((properties.linearTilingFeatures & (VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT)) &&
|
|
|
|
(properties.optimalTilingFeatures & (VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT)))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-06-14 18:36:14 +00:00
|
|
|
static void
|
2023-06-14 18:37:58 +00:00
|
|
|
gsk_vulkan_image_create_view (GskVulkanImage *self,
|
|
|
|
const GskMemoryFormatInfo *format)
|
2023-06-14 18:36:14 +00:00
|
|
|
{
|
|
|
|
GSK_VK_CHECK (vkCreateImageView, gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
&(VkImageViewCreateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
|
|
|
|
.image = self->vk_image,
|
|
|
|
.viewType = VK_IMAGE_VIEW_TYPE_2D,
|
2023-06-14 18:37:58 +00:00
|
|
|
.format = format->format,
|
|
|
|
.components = format->components,
|
2023-06-14 18:36:14 +00:00
|
|
|
.subresourceRange = {
|
|
|
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
.baseMipLevel = 0,
|
|
|
|
.levelCount = 1,
|
|
|
|
.baseArrayLayer = 0,
|
|
|
|
.layerCount = 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NULL,
|
|
|
|
&self->vk_image_view);
|
|
|
|
}
|
|
|
|
|
2016-12-06 13:42:46 +00:00
|
|
|
static GskVulkanImage *
|
|
|
|
gsk_vulkan_image_new (GdkVulkanContext *context,
|
2023-06-14 18:37:58 +00:00
|
|
|
GdkMemoryFormat format,
|
2016-12-06 13:42:46 +00:00
|
|
|
gsize width,
|
|
|
|
gsize height,
|
|
|
|
VkImageTiling tiling,
|
|
|
|
VkImageUsageFlags usage,
|
2017-09-20 23:11:42 +00:00
|
|
|
VkImageLayout layout,
|
|
|
|
VkAccessFlags access,
|
2016-12-06 13:42:46 +00:00
|
|
|
VkMemoryPropertyFlags memory)
|
|
|
|
{
|
|
|
|
VkMemoryRequirements requirements;
|
|
|
|
GskVulkanImage *self;
|
2023-06-14 18:37:58 +00:00
|
|
|
const GskMemoryFormatInfo *vk_format;
|
2016-12-06 13:42:46 +00:00
|
|
|
|
2023-05-09 22:08:24 +00:00
|
|
|
g_assert (width > 0 && height > 0);
|
|
|
|
|
2023-06-15 21:13:31 +00:00
|
|
|
while (TRUE)
|
2023-06-14 18:37:58 +00:00
|
|
|
{
|
2023-06-15 21:13:31 +00:00
|
|
|
for (vk_format = gsk_memory_format_get_vk_format_infos (format);
|
|
|
|
vk_format->format != VK_FORMAT_UNDEFINED;
|
|
|
|
vk_format++)
|
|
|
|
{
|
|
|
|
if (gsk_vulkan_context_supports_format (context, vk_format->format))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (vk_format->format != VK_FORMAT_UNDEFINED)
|
2023-06-14 18:37:58 +00:00
|
|
|
break;
|
|
|
|
|
2023-06-15 21:13:31 +00:00
|
|
|
format = gsk_memory_format_get_fallback (format);
|
2023-06-14 18:37:58 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 19:19:56 +00:00
|
|
|
self = g_object_new (GSK_TYPE_VULKAN_IMAGE, NULL);
|
2016-12-06 13:42:46 +00:00
|
|
|
|
|
|
|
self->vulkan = g_object_ref (context);
|
2023-06-14 18:37:58 +00:00
|
|
|
self->format = format;
|
2023-06-17 14:05:09 +00:00
|
|
|
self->vk_format = vk_format->format;
|
2016-12-08 19:46:02 +00:00
|
|
|
self->width = width;
|
|
|
|
self->height = height;
|
2017-09-20 23:11:42 +00:00
|
|
|
self->vk_usage = usage;
|
|
|
|
self->vk_image_layout = layout;
|
|
|
|
self->vk_access = access;
|
2016-12-06 13:42:46 +00:00
|
|
|
|
|
|
|
GSK_VK_CHECK (vkCreateImage, gdk_vulkan_context_get_device (context),
|
|
|
|
&(VkImageCreateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
|
|
|
|
.flags = 0,
|
|
|
|
.imageType = VK_IMAGE_TYPE_2D,
|
2023-06-14 18:37:58 +00:00
|
|
|
.format = vk_format->format,
|
2016-12-06 13:42:46 +00:00
|
|
|
.extent = { width, height, 1 },
|
|
|
|
.mipLevels = 1,
|
|
|
|
.arrayLayers = 1,
|
|
|
|
.samples = VK_SAMPLE_COUNT_1_BIT,
|
|
|
|
.tiling = tiling,
|
|
|
|
.usage = usage,
|
|
|
|
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
2017-09-20 23:11:42 +00:00
|
|
|
.initialLayout = self->vk_image_layout,
|
2016-12-06 13:42:46 +00:00
|
|
|
},
|
|
|
|
NULL,
|
|
|
|
&self->vk_image);
|
|
|
|
|
|
|
|
vkGetImageMemoryRequirements (gdk_vulkan_context_get_device (context),
|
|
|
|
self->vk_image,
|
|
|
|
&requirements);
|
|
|
|
|
|
|
|
self->memory = gsk_vulkan_memory_new (context,
|
|
|
|
requirements.memoryTypeBits,
|
|
|
|
memory,
|
|
|
|
requirements.size);
|
|
|
|
|
|
|
|
GSK_VK_CHECK (vkBindImageMemory, gdk_vulkan_context_get_device (context),
|
|
|
|
self->vk_image,
|
|
|
|
gsk_vulkan_memory_get_device_memory (self->memory),
|
|
|
|
0);
|
|
|
|
|
2023-06-14 18:37:58 +00:00
|
|
|
gsk_vulkan_image_create_view (self, vk_format);
|
2023-06-14 18:36:14 +00:00
|
|
|
|
|
|
|
return self;
|
2016-12-06 13:42:46 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 06:02:06 +00:00
|
|
|
GskVulkanImage *
|
|
|
|
gsk_vulkan_image_new_from_texture (GskVulkanUploader *uploader,
|
|
|
|
GdkTexture *texture)
|
|
|
|
{
|
|
|
|
GdkTextureDownloader *downloader;
|
|
|
|
GskVulkanImage *result;
|
2023-06-14 00:14:40 +00:00
|
|
|
GskVulkanImageMap map;
|
2023-06-13 06:02:06 +00:00
|
|
|
|
|
|
|
downloader = gdk_texture_downloader_new (texture);
|
2023-06-14 00:14:40 +00:00
|
|
|
result = gsk_vulkan_image_new_for_upload (uploader,
|
2023-06-14 18:37:58 +00:00
|
|
|
gdk_texture_get_format (texture),
|
2023-06-14 00:14:40 +00:00
|
|
|
gdk_texture_get_width (texture),
|
|
|
|
gdk_texture_get_height (texture));
|
2023-06-14 18:37:58 +00:00
|
|
|
gdk_texture_downloader_set_format (downloader, result->format);
|
2023-06-15 12:29:15 +00:00
|
|
|
gsk_vulkan_image_map_memory (result, uploader, GSK_VULKAN_WRITE, &map);
|
2023-06-14 00:14:40 +00:00
|
|
|
gdk_texture_downloader_download_into (downloader, map.data, map.stride);
|
|
|
|
gsk_vulkan_image_unmap_memory (result, uploader, &map);
|
2023-06-13 06:02:06 +00:00
|
|
|
gdk_texture_downloader_free (downloader);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-06-14 00:08:17 +00:00
|
|
|
GskVulkanImage *
|
|
|
|
gsk_vulkan_image_new_for_upload (GskVulkanUploader *uploader,
|
2023-06-14 18:37:58 +00:00
|
|
|
GdkMemoryFormat format,
|
2023-06-14 00:08:17 +00:00
|
|
|
gsize width,
|
|
|
|
gsize height)
|
|
|
|
{
|
|
|
|
GskVulkanImage *self;
|
|
|
|
|
|
|
|
self = gsk_vulkan_image_new (uploader->vulkan,
|
2023-06-14 18:37:58 +00:00
|
|
|
format,
|
2023-06-14 00:08:17 +00:00
|
|
|
width,
|
|
|
|
height,
|
2023-06-14 01:21:24 +00:00
|
|
|
VK_IMAGE_TILING_LINEAR,
|
2023-06-14 00:08:17 +00:00
|
|
|
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
|
|
|
|
VK_IMAGE_USAGE_SAMPLED_BIT,
|
|
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
|
|
VK_ACCESS_TRANSFER_WRITE_BIT,
|
|
|
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2023-06-14 01:21:24 +00:00
|
|
|
static void
|
|
|
|
gsk_vulkan_image_map_memory_direct (GskVulkanImage *self,
|
|
|
|
GskVulkanUploader *uploader,
|
2023-06-15 12:29:15 +00:00
|
|
|
GskVulkanMapMode mode,
|
2023-06-14 01:21:24 +00:00
|
|
|
GskVulkanImageMap *map)
|
2023-06-14 00:08:17 +00:00
|
|
|
{
|
2023-06-14 01:21:24 +00:00
|
|
|
VkImageSubresource image_res;
|
|
|
|
VkSubresourceLayout image_layout;
|
2023-06-14 00:08:17 +00:00
|
|
|
|
2023-06-15 12:29:15 +00:00
|
|
|
if (self->vk_image_layout != VK_IMAGE_LAYOUT_PREINITIALIZED)
|
|
|
|
{
|
|
|
|
gsk_vulkan_uploader_add_image_barrier (uploader,
|
|
|
|
FALSE,
|
|
|
|
self,
|
|
|
|
VK_IMAGE_LAYOUT_GENERAL,
|
|
|
|
(mode & GSK_VULKAN_READ ? VK_ACCESS_MEMORY_READ_BIT : 0) |
|
|
|
|
(mode & GSK_VULKAN_WRITE ? VK_ACCESS_MEMORY_WRITE_BIT : 0));
|
|
|
|
|
|
|
|
if (mode & GSK_VULKAN_READ)
|
|
|
|
{
|
|
|
|
gsk_vulkan_uploader_upload (uploader);
|
|
|
|
GSK_VK_CHECK (vkQueueWaitIdle, gdk_vulkan_context_get_queue (self->vulkan));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 01:21:24 +00:00
|
|
|
image_res.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
image_res.mipLevel = 0;
|
|
|
|
image_res.arrayLayer = 0;
|
|
|
|
|
|
|
|
vkGetImageSubresourceLayout (gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
self->vk_image, &image_res, &image_layout);
|
|
|
|
|
2023-06-15 12:29:15 +00:00
|
|
|
map->mode = mode;
|
2023-06-14 01:21:24 +00:00
|
|
|
map->staging_buffer = NULL;
|
|
|
|
map->data = gsk_vulkan_memory_map (self->memory) + image_layout.offset;
|
|
|
|
map->stride = image_layout.rowPitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_image_unmap_memory_direct (GskVulkanImage *self,
|
|
|
|
GskVulkanUploader *uploader,
|
|
|
|
GskVulkanImageMap *map)
|
|
|
|
{
|
|
|
|
gsk_vulkan_memory_unmap (self->memory);
|
|
|
|
|
|
|
|
gsk_vulkan_uploader_add_image_barrier (uploader,
|
|
|
|
TRUE,
|
|
|
|
self,
|
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
|
|
VK_ACCESS_SHADER_READ_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_image_map_memory_indirect (GskVulkanImage *self,
|
|
|
|
GskVulkanUploader *uploader,
|
2023-06-15 12:29:15 +00:00
|
|
|
GskVulkanMapMode mode,
|
2023-06-14 01:21:24 +00:00
|
|
|
GskVulkanImageMap *map)
|
|
|
|
{
|
2023-06-15 12:29:15 +00:00
|
|
|
map->mode = mode;
|
|
|
|
map->stride = self->width * gdk_memory_format_bytes_per_pixel (self->format);
|
|
|
|
map->staging_buffer = gsk_vulkan_buffer_new_map (uploader->vulkan, self->height * map->stride, mode);
|
|
|
|
|
|
|
|
if (self->vk_image_layout != VK_IMAGE_LAYOUT_PREINITIALIZED)
|
|
|
|
{
|
|
|
|
if (mode & GSK_VULKAN_READ)
|
|
|
|
{
|
|
|
|
gsk_vulkan_uploader_add_image_barrier (uploader,
|
|
|
|
FALSE,
|
|
|
|
self,
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
|
|
|
VK_ACCESS_TRANSFER_READ_BIT);
|
|
|
|
|
|
|
|
vkCmdCopyImageToBuffer (gsk_vulkan_uploader_get_copy_buffer (uploader),
|
|
|
|
self->vk_image,
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
|
|
|
gsk_vulkan_buffer_get_buffer (map->staging_buffer),
|
|
|
|
1,
|
|
|
|
(VkBufferImageCopy[1]) {
|
|
|
|
{
|
|
|
|
.bufferOffset = 0,
|
|
|
|
.imageSubresource = {
|
|
|
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
.mipLevel = 0,
|
|
|
|
.baseArrayLayer = 0,
|
|
|
|
.layerCount = 1
|
|
|
|
},
|
|
|
|
.imageOffset = { 0, 0, 0 },
|
|
|
|
.imageExtent = {
|
|
|
|
.width = self->width,
|
|
|
|
.height = self->height,
|
|
|
|
.depth = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
gsk_vulkan_uploader_upload (uploader);
|
|
|
|
GSK_VK_CHECK (vkQueueWaitIdle, gdk_vulkan_context_get_queue (self->vulkan));
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 00:08:17 +00:00
|
|
|
|
|
|
|
map->data = gsk_vulkan_buffer_map (map->staging_buffer);
|
|
|
|
}
|
|
|
|
|
2023-06-14 01:21:24 +00:00
|
|
|
static void
|
|
|
|
gsk_vulkan_image_unmap_memory_indirect (GskVulkanImage *self,
|
|
|
|
GskVulkanUploader *uploader,
|
|
|
|
GskVulkanImageMap *map)
|
2023-06-14 00:08:17 +00:00
|
|
|
{
|
|
|
|
gsk_vulkan_buffer_unmap (map->staging_buffer);
|
|
|
|
|
2023-06-15 12:29:15 +00:00
|
|
|
if (map->mode & GSK_VULKAN_WRITE)
|
|
|
|
{
|
|
|
|
gsk_vulkan_uploader_add_buffer_barrier (uploader,
|
|
|
|
FALSE,
|
|
|
|
&(VkBufferMemoryBarrier) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
|
|
|
.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT,
|
|
|
|
.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
|
|
|
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
.buffer = gsk_vulkan_buffer_get_buffer (map->staging_buffer),
|
|
|
|
.offset = 0,
|
|
|
|
.size = VK_WHOLE_SIZE,
|
|
|
|
});
|
|
|
|
|
|
|
|
gsk_vulkan_uploader_add_image_barrier (uploader,
|
|
|
|
FALSE,
|
|
|
|
self,
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
|
|
VK_ACCESS_TRANSFER_WRITE_BIT);
|
|
|
|
|
|
|
|
vkCmdCopyBufferToImage (gsk_vulkan_uploader_get_copy_buffer (uploader),
|
|
|
|
gsk_vulkan_buffer_get_buffer (map->staging_buffer),
|
|
|
|
self->vk_image,
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
|
|
1,
|
|
|
|
(VkBufferImageCopy[1]) {
|
|
|
|
{
|
|
|
|
.bufferOffset = 0,
|
|
|
|
.imageSubresource = {
|
|
|
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
.mipLevel = 0,
|
|
|
|
.baseArrayLayer = 0,
|
|
|
|
.layerCount = 1
|
|
|
|
},
|
|
|
|
.imageOffset = { 0, 0, 0 },
|
|
|
|
.imageExtent = {
|
|
|
|
.width = self->width,
|
|
|
|
.height = self->height,
|
|
|
|
.depth = 1
|
|
|
|
}
|
2023-06-14 00:08:17 +00:00
|
|
|
}
|
2023-06-15 12:29:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
uploader->staging_buffer_free_list = g_slist_prepend (uploader->staging_buffer_free_list,
|
|
|
|
map->staging_buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gsk_vulkan_buffer_free (map->staging_buffer);
|
|
|
|
}
|
2023-06-14 00:08:17 +00:00
|
|
|
|
|
|
|
gsk_vulkan_uploader_add_image_barrier (uploader,
|
|
|
|
TRUE,
|
|
|
|
self,
|
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
|
|
VK_ACCESS_SHADER_READ_BIT);
|
|
|
|
}
|
|
|
|
|
2023-06-14 01:21:24 +00:00
|
|
|
void
|
|
|
|
gsk_vulkan_image_map_memory (GskVulkanImage *self,
|
|
|
|
GskVulkanUploader *uploader,
|
2023-06-15 12:29:15 +00:00
|
|
|
GskVulkanMapMode mode,
|
2023-06-14 01:21:24 +00:00
|
|
|
GskVulkanImageMap *map)
|
|
|
|
{
|
2023-06-14 01:31:11 +00:00
|
|
|
if (!GSK_DEBUG_CHECK (STAGING) && gsk_vulkan_memory_can_map (self->memory, TRUE))
|
2023-06-15 12:29:15 +00:00
|
|
|
gsk_vulkan_image_map_memory_direct (self, uploader, mode, map);
|
2023-06-14 01:21:24 +00:00
|
|
|
else
|
2023-06-15 12:29:15 +00:00
|
|
|
gsk_vulkan_image_map_memory_indirect (self, uploader, mode, map);
|
2023-06-14 01:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_vulkan_image_unmap_memory (GskVulkanImage *self,
|
|
|
|
GskVulkanUploader *uploader,
|
|
|
|
GskVulkanImageMap *map)
|
|
|
|
{
|
|
|
|
if (map->staging_buffer)
|
|
|
|
gsk_vulkan_image_unmap_memory_indirect (self, uploader, map);
|
|
|
|
else
|
|
|
|
gsk_vulkan_image_unmap_memory_direct (self, uploader, map);
|
|
|
|
}
|
|
|
|
|
2016-12-08 20:14:41 +00:00
|
|
|
GskVulkanImage *
|
|
|
|
gsk_vulkan_image_new_for_swapchain (GdkVulkanContext *context,
|
|
|
|
VkImage image,
|
|
|
|
VkFormat format,
|
|
|
|
gsize width,
|
|
|
|
gsize height)
|
|
|
|
{
|
|
|
|
GskVulkanImage *self;
|
|
|
|
|
|
|
|
self = g_object_new (GSK_TYPE_VULKAN_IMAGE, NULL);
|
|
|
|
|
|
|
|
self->vulkan = g_object_ref (context);
|
|
|
|
self->width = width;
|
|
|
|
self->height = height;
|
|
|
|
self->vk_image = image;
|
2023-06-17 14:05:09 +00:00
|
|
|
self->vk_format = format;
|
2016-12-08 20:14:41 +00:00
|
|
|
|
2023-06-14 18:37:58 +00:00
|
|
|
gsk_vulkan_image_create_view (self,
|
|
|
|
&(GskMemoryFormatInfo) {
|
|
|
|
format,
|
|
|
|
{ VK_COMPONENT_SWIZZLE_R,
|
|
|
|
VK_COMPONENT_SWIZZLE_G,
|
|
|
|
VK_COMPONENT_SWIZZLE_B,
|
|
|
|
VK_COMPONENT_SWIZZLE_A
|
|
|
|
}
|
|
|
|
});
|
2016-12-08 20:14:41 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:01:07 +00:00
|
|
|
GskVulkanImage *
|
2017-09-20 23:11:42 +00:00
|
|
|
gsk_vulkan_image_new_for_atlas (GdkVulkanContext *context,
|
|
|
|
gsize width,
|
|
|
|
gsize height)
|
|
|
|
{
|
|
|
|
GskVulkanImage *self;
|
|
|
|
|
|
|
|
self = gsk_vulkan_image_new (context,
|
2023-06-14 18:37:58 +00:00
|
|
|
GDK_MEMORY_DEFAULT,
|
2017-09-20 23:11:42 +00:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
VK_IMAGE_TILING_OPTIMAL,
|
|
|
|
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
|
|
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
|
|
0,
|
2016-12-22 18:01:07 +00:00
|
|
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-09-26 23:12:20 +00:00
|
|
|
GskVulkanImage *
|
2023-05-09 20:25:22 +00:00
|
|
|
gsk_vulkan_image_new_for_offscreen (GdkVulkanContext *context,
|
2023-06-14 19:32:26 +00:00
|
|
|
GdkMemoryFormat preferred_format,
|
2023-05-09 20:25:22 +00:00
|
|
|
gsize width,
|
|
|
|
gsize height)
|
2017-09-26 23:12:20 +00:00
|
|
|
{
|
|
|
|
GskVulkanImage *self;
|
|
|
|
|
|
|
|
self = gsk_vulkan_image_new (context,
|
2023-06-14 19:32:26 +00:00
|
|
|
preferred_format,
|
2017-09-26 23:12:20 +00:00
|
|
|
width,
|
|
|
|
height,
|
2023-06-15 12:29:15 +00:00
|
|
|
VK_IMAGE_TILING_LINEAR,
|
2017-09-26 23:12:20 +00:00
|
|
|
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
|
|
|
|
VK_IMAGE_USAGE_SAMPLED_BIT |
|
|
|
|
VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
|
|
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
2023-05-09 20:25:22 +00:00
|
|
|
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
|
2017-09-26 23:12:20 +00:00
|
|
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *
|
2016-12-22 18:01:07 +00:00
|
|
|
gsk_vulkan_image_download (GskVulkanImage *self,
|
|
|
|
GskVulkanUploader *uploader)
|
|
|
|
{
|
2023-06-15 12:29:15 +00:00
|
|
|
GskVulkanImageMap map;
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *texture;
|
2018-03-18 03:38:49 +00:00
|
|
|
GBytes *bytes;
|
2016-12-22 18:01:07 +00:00
|
|
|
|
2023-06-15 12:29:15 +00:00
|
|
|
gsk_vulkan_image_map_memory (self, uploader, GSK_VULKAN_READ, &map);
|
|
|
|
bytes = g_bytes_new (map.data, map.stride * self->height);
|
2018-03-18 03:38:49 +00:00
|
|
|
texture = gdk_memory_texture_new (self->width, self->height,
|
2023-06-14 18:37:58 +00:00
|
|
|
self->format,
|
2018-03-18 03:38:49 +00:00
|
|
|
bytes,
|
2023-06-15 12:29:15 +00:00
|
|
|
map.stride);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
gsk_vulkan_image_unmap_memory (self, uploader, &map);
|
2016-12-22 18:01:07 +00:00
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2017-09-21 17:42:57 +00:00
|
|
|
void
|
|
|
|
gsk_vulkan_image_upload_regions (GskVulkanImage *self,
|
|
|
|
GskVulkanUploader *uploader,
|
|
|
|
guint num_regions,
|
|
|
|
GskImageRegion *regions)
|
2017-09-20 14:00:48 +00:00
|
|
|
{
|
|
|
|
GskVulkanBuffer *staging;
|
|
|
|
guchar *mem;
|
2017-09-21 17:42:57 +00:00
|
|
|
guchar *m;
|
|
|
|
gsize size;
|
|
|
|
gsize offset;
|
|
|
|
VkBufferImageCopy *bufferImageCopy;
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
for (int i = 0; i < num_regions; i++)
|
|
|
|
size += regions[i].width * regions[i].height * 4;
|
2017-09-20 14:00:48 +00:00
|
|
|
|
2023-06-15 12:29:15 +00:00
|
|
|
staging = gsk_vulkan_buffer_new_map (uploader->vulkan, size, GSK_VULKAN_WRITE);
|
2017-09-20 14:00:48 +00:00
|
|
|
mem = gsk_vulkan_buffer_map (staging);
|
|
|
|
|
2017-09-21 17:42:57 +00:00
|
|
|
bufferImageCopy = alloca (sizeof (VkBufferImageCopy) * num_regions);
|
|
|
|
memset (bufferImageCopy, 0, sizeof (VkBufferImageCopy) * num_regions);
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
for (int i = 0; i < num_regions; i++)
|
2017-09-20 14:00:48 +00:00
|
|
|
{
|
2017-09-21 17:42:57 +00:00
|
|
|
m = mem + offset;
|
|
|
|
if (regions[i].stride == regions[i].width * 4)
|
2017-09-20 14:00:48 +00:00
|
|
|
{
|
2017-09-21 17:42:57 +00:00
|
|
|
memcpy (m, regions[i].data, regions[i].stride * regions[i].height);
|
2017-09-20 14:00:48 +00:00
|
|
|
}
|
2017-09-21 17:42:57 +00:00
|
|
|
else
|
|
|
|
{
|
2021-02-04 05:09:09 +00:00
|
|
|
for (gsize r = 0; r < regions[i].height; r++)
|
2017-09-21 17:42:57 +00:00
|
|
|
memcpy (m + r * regions[i].width * 4, regions[i].data + r * regions[i].stride, regions[i].width * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferImageCopy[i].bufferOffset = offset;
|
gsk/vulkanimage: Set buffer row length and height
This doesn't make any difference now, but will allow us to copy
subregions more easily. This is not obvious, but here's a quick
explanation:
Leaving 'bufferRowLength' and 'bufferImageHeight' implies that
Vulkan will assume the size passed in the 'imageExtent' field.
Right now, this assumption is correct - the only user of this
function is the glyph cache, and it only copies and uploads
exact rects. Next commits will change that assumption, so we
must pass 'buffer*' fields, and tell Vulkan, "this part of the
buffer represents an image of width x height, and I want the
subregion (x, y, smallerWidth, smallerHeight) of this image".
2023-03-23 02:01:10 +00:00
|
|
|
bufferImageCopy[i].bufferRowLength = regions[i].width;
|
|
|
|
bufferImageCopy[i].bufferImageHeight = regions[i].height;
|
2017-09-21 17:42:57 +00:00
|
|
|
bufferImageCopy[i].imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
bufferImageCopy[i].imageSubresource.mipLevel = 0;
|
|
|
|
bufferImageCopy[i].imageSubresource.baseArrayLayer = 0;
|
|
|
|
bufferImageCopy[i].imageSubresource.layerCount = 1;
|
|
|
|
bufferImageCopy[i].imageOffset.x = regions[i].x;
|
|
|
|
bufferImageCopy[i].imageOffset.y = regions[i].y;
|
|
|
|
bufferImageCopy[i].imageOffset.z = 0;
|
|
|
|
bufferImageCopy[i].imageExtent.width = regions[i].width;
|
|
|
|
bufferImageCopy[i].imageExtent.height = regions[i].height;
|
|
|
|
bufferImageCopy[i].imageExtent.depth = 1;
|
|
|
|
|
|
|
|
offset += regions[i].width * regions[i].height * 4;
|
2017-09-20 14:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gsk_vulkan_buffer_unmap (staging);
|
|
|
|
|
|
|
|
gsk_vulkan_uploader_add_image_barrier (uploader,
|
|
|
|
FALSE,
|
2017-09-20 23:11:42 +00:00
|
|
|
self,
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
|
|
VK_ACCESS_TRANSFER_WRITE_BIT);
|
2017-09-20 14:00:48 +00:00
|
|
|
|
|
|
|
vkCmdCopyBufferToImage (gsk_vulkan_uploader_get_copy_buffer (uploader),
|
|
|
|
gsk_vulkan_buffer_get_buffer (staging),
|
|
|
|
self->vk_image,
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
2017-09-21 17:42:57 +00:00
|
|
|
num_regions,
|
|
|
|
bufferImageCopy);
|
2017-09-20 14:00:48 +00:00
|
|
|
|
|
|
|
gsk_vulkan_uploader_add_image_barrier (uploader,
|
|
|
|
TRUE,
|
2017-09-20 23:11:42 +00:00
|
|
|
self,
|
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
|
|
VK_ACCESS_SHADER_READ_BIT);
|
2017-09-20 14:00:48 +00:00
|
|
|
|
|
|
|
uploader->staging_buffer_free_list = g_slist_prepend (uploader->staging_buffer_free_list, staging);
|
|
|
|
}
|
|
|
|
|
2017-04-26 16:28:14 +00:00
|
|
|
static void
|
2016-12-08 19:19:56 +00:00
|
|
|
gsk_vulkan_image_finalize (GObject *object)
|
2016-12-06 13:42:46 +00:00
|
|
|
{
|
2016-12-08 19:19:56 +00:00
|
|
|
GskVulkanImage *self = GSK_VULKAN_IMAGE (object);
|
|
|
|
|
2016-12-06 13:42:46 +00:00
|
|
|
if (self->vk_image_view != VK_NULL_HANDLE)
|
|
|
|
{
|
|
|
|
vkDestroyImageView (gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
self->vk_image_view,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2016-12-08 20:14:41 +00:00
|
|
|
/* memory is NULL for for_swapchain() images, where we don't own
|
|
|
|
* the VkImage */
|
|
|
|
if (self->memory)
|
|
|
|
{
|
|
|
|
vkDestroyImage (gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
self->vk_image,
|
|
|
|
NULL);
|
2017-12-14 00:43:50 +00:00
|
|
|
|
|
|
|
gsk_vulkan_memory_free (self->memory);
|
2016-12-08 20:14:41 +00:00
|
|
|
}
|
2016-12-06 13:42:46 +00:00
|
|
|
|
|
|
|
g_object_unref (self->vulkan);
|
|
|
|
|
2016-12-08 19:19:56 +00:00
|
|
|
G_OBJECT_CLASS (gsk_vulkan_image_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_image_class_init (GskVulkanImageClass *klass)
|
|
|
|
{
|
|
|
|
G_OBJECT_CLASS (klass)->finalize = gsk_vulkan_image_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_image_init (GskVulkanImage *self)
|
|
|
|
{
|
2016-12-06 13:42:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 19:46:02 +00:00
|
|
|
gsize
|
|
|
|
gsk_vulkan_image_get_width (GskVulkanImage *self)
|
|
|
|
{
|
|
|
|
return self->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
gsize
|
|
|
|
gsk_vulkan_image_get_height (GskVulkanImage *self)
|
|
|
|
{
|
|
|
|
return self->height;
|
|
|
|
}
|
|
|
|
|
2016-12-06 13:42:46 +00:00
|
|
|
VkImage
|
|
|
|
gsk_vulkan_image_get_image (GskVulkanImage *self)
|
|
|
|
{
|
|
|
|
return self->vk_image;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkImageView
|
|
|
|
gsk_vulkan_image_get_image_view (GskVulkanImage *self)
|
|
|
|
{
|
|
|
|
return self->vk_image_view;
|
|
|
|
}
|
2023-06-17 14:05:09 +00:00
|
|
|
|
|
|
|
VkFormat
|
|
|
|
gsk_vulkan_image_get_vk_format (GskVulkanImage *self)
|
|
|
|
{
|
|
|
|
return self->vk_format;
|
|
|
|
}
|