summaryrefslogtreecommitdiff
path: root/gsk/gskvulkanmemory.c
blob: 1ff46757d4b6129f4c228fd56176d91926e903b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "config.h"

#include "gskvulkanpipelineprivate.h"
#include "gskvulkanmemoryprivate.h"

struct _GskVulkanMemory
{
  GdkVulkanContext *vulkan;

  gsize size;

  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;

  self = g_slice_new0 (GskVulkanMemory);

  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);

  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);

  g_slice_free (GskVulkanMemory, self);
}

VkDeviceMemory
gsk_vulkan_memory_get_device_memory (GskVulkanMemory *self)
{
  return self->vk_memory;
}

guchar *
gsk_vulkan_memory_map (GskVulkanMemory *self)
{
  void *data;

  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);
}