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
|
#include "image.h"
#include "vkassert.h"
#include <rune/core/alloc.h>
int _create_image_view(struct vkimage *image, struct vkdev *dev, VkFormat format, VkImageAspectFlags aflags) {
VkImageViewCreateInfo vcinfo;
vcinfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
vcinfo.pNext = NULL;
vcinfo.flags = 0;
vcinfo.image = image->handle;
vcinfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
vcinfo.format = format;
vcinfo.components.r = VK_COMPONENT_SWIZZLE_R;
vcinfo.components.g = VK_COMPONENT_SWIZZLE_G;
vcinfo.components.b = VK_COMPONENT_SWIZZLE_B;
vcinfo.components.a = VK_COMPONENT_SWIZZLE_A;
vcinfo.subresourceRange.aspectMask = aflags;
vcinfo.subresourceRange.baseMipLevel = 0;
vcinfo.subresourceRange.levelCount = 1;
vcinfo.subresourceRange.baseArrayLayer = 0;
vcinfo.subresourceRange.layerCount = 1;
vkassert(vkCreateImageView(dev->ldev, &vcinfo, NULL, &image->view));
}
struct vkimage* create_vkimage(struct vkdev *dev, VkFormat format, uint32_t width, uint32_t height, uint32_t usage, uint32_t mem_flags, uint32_t aflags, int create_view) {
struct vkimage *ret = rune_alloc(sizeof(struct vkimage));
ret->width = width;
ret->height = height;
VkImageCreateInfo icinfo;
icinfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
icinfo.pNext = NULL;
icinfo.flags = 0;
icinfo.imageType = VK_IMAGE_TYPE_2D;
icinfo.extent.width = width;
icinfo.extent.height = height;
icinfo.extent.depth = 1;
icinfo.mipLevels = 1;
icinfo.arrayLayers = 1;
icinfo.format = format;
icinfo.tiling = VK_IMAGE_TILING_OPTIMAL;
icinfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
icinfo.usage = usage;
icinfo.samples = VK_SAMPLE_COUNT_1_BIT;
icinfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
vkassert(vkCreateImage(dev->ldev, &icinfo, NULL, &ret->handle));
VkMemoryRequirements mem_req;
vkGetImageMemoryRequirements(dev->ldev, ret->handle, &mem_req);
int32_t mem_type = get_memory_index(dev, mem_req.memoryTypeBits, mem_flags);
if (mem_type == -1)
log_output(LOG_ERROR, "Image does not have a valid memory type");
VkMemoryAllocateInfo mainfo;
mainfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
mainfo.pNext = NULL;
mainfo.allocationSize = mem_req.size;
mainfo.memoryTypeIndex = mem_type;
vkassert(vkAllocateMemory(dev->ldev, &mainfo, NULL, &ret->memory));
vkassert(vkBindImageMemory(dev->ldev, ret->handle, ret->memory, 0));
if (create_view == 1)
_create_image_view(ret, dev, format, aflags);
return ret;
}
void destroy_vkimage(struct vkimage *image, struct vkdev *dev) {
if (image->view)
vkDestroyImageView(dev->ldev, image->view, NULL);
if (image->memory)
vkFreeMemory(dev->ldev, image->memory, NULL);
if (image->handle)
vkDestroyImage(dev->ldev, image->handle, NULL);
rune_free(image);
}
|