-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.c
More file actions
133 lines (118 loc) · 4.05 KB
/
Copy pathhelpers.c
File metadata and controls
133 lines (118 loc) · 4.05 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vulkan/vulkan.h>
#include "helpers.h"
static char *readFile(const char *path, size_t *lenOut) {
int fd = open(path, O_RDONLY, 0);
if (fd < 0) perror(path);
struct stat info;
fstat(fd, &info);
int len = info.st_size;
*lenOut = len;
char *r = malloc(len);
char *pos = r;
while (len > 0) {
int got = read(fd, pos, len);
if (got < 0) perror("read");
len -= got;
pos += got;
}
close(fd);
LOG("Loaded %s (%d bytes)\n", path, (int) *lenOut);
return r;
}
VkShaderModule loadShaderModule(VkDevice device, char *path) {
size_t len;
char *code = readFile(path, &len);
VkShaderModuleCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = len,
.pCode = (const uint32_t*) code,
};
VkShaderModule shaderModule;
if (vkCreateShaderModule(device, &info, NULL, &shaderModule) != VK_SUCCESS) {
DIE("failed to create shader module!\n");
}
return shaderModule;
}
uint32_t find_graphics_family(VkPhysicalDevice device) {
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, NULL);
VkQueueFamilyProperties *queueFamilies = malloc(sizeof(VkQueueFamilyProperties) * queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies);
LOG("Device has %d queue families\n", queueFamilyCount);
for (int i = 0; i < queueFamilyCount; i++) {
VkQueueFamilyProperties queueFamily = queueFamilies[i];
if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
LOG("Found graphics queue family (%d)\n", i);
return i;
}
}
DIE("No graphics queue!\n");
}
uint32_t findMemoryType(VkPhysicalDeviceMemoryProperties *memProperties,
uint32_t typeFilter,
VkMemoryPropertyFlags properties) {
for (uint32_t i = 0; i < memProperties->memoryTypeCount; i++) {
if ((typeFilter & (1 << i)) &&
(memProperties->memoryTypes[i].propertyFlags & properties) == properties) {
return i;
}
}
DIE("failed to find suitable memory type!\n");
}
void createBuffer(VkDevice device,
VkPhysicalDeviceMemoryProperties *memProperties,
VkDeviceSize size,
VkBufferUsageFlags usage,
VkMemoryPropertyFlags properties,
VkBuffer *buffer,
VkDeviceMemory *bufferMemory) {
VkBufferCreateInfo bufferInfo = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = size,
.usage = usage,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
if (vkCreateBuffer(device, &bufferInfo, NULL, buffer) != VK_SUCCESS) {
DIE("failed to create buffer!\n");
}
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, *buffer, &memRequirements);
VkMemoryAllocateInfo allocInfo = {
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.allocationSize = memRequirements.size,
.memoryTypeIndex = findMemoryType(memProperties, memRequirements.memoryTypeBits, properties),
};
if (vkAllocateMemory(device, &allocInfo, NULL, bufferMemory) != VK_SUCCESS) {
DIE("failed to allocate buffer memory!\n");
}
vkBindBufferMemory(device, *buffer, *bufferMemory, 0);
}
void show_extensions(void) {
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, NULL);
LOG("Extentions: %d\n", extensionCount);
VkExtensionProperties *extensions = malloc(sizeof(VkExtensionProperties) * extensionCount);
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, extensions);
for (int i = 0; i < extensionCount; i++) {
LOG("- %s\n", extensions[i].extensionName);
}
}
void createExportSemaphore(VkDevice device, VkSemaphore *semaphore) {
const VkExportSemaphoreCreateInfo exportInfo = {
.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO,
.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT,
};
const VkSemaphoreCreateInfo semaphoreInfo = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = &exportInfo,
};
if (vkCreateSemaphore(device, &semaphoreInfo, NULL, semaphore) != VK_SUCCESS) {
DIE("failed to create semaphore!\n");
}
}