-
Notifications
You must be signed in to change notification settings - Fork 1
/
device.c
112 lines (94 loc) · 3.03 KB
/
device.c
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
#include <vulkan/vulkan.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "device.h"
#include "compute.h"
#include "instance.h"
#include "pipeline.h"
#include "memory.h"
uint32_t ComputeQueueFamilyIndex;
VkDevice LogicalDevice = VK_NULL_HANDLE;
VkQueue ComputingQueue = VK_NULL_HANDLE;
VkCommandPool ComputeCmdPool = VK_NULL_HANDLE;
VkDescriptorPool DescriptorPool = VK_NULL_HANDLE;
void CreateDeviceAndComuteQueue(void)
{
uint32_t count = 0;
vkGetPhysicalDeviceQueueFamilyProperties(PhysicalDevice, &count, NULL);
VkQueueFamilyProperties families[count];
vkGetPhysicalDeviceQueueFamilyProperties(PhysicalDevice, &count, families);
printf("Found queue families count: %d\n", count);
ComputeQueueFamilyIndex = 0;
while ((ComputeQueueFamilyIndex < count) && ((families[ComputeQueueFamilyIndex].queueFlags & VK_QUEUE_COMPUTE_BIT) == 0))
{
ComputeQueueFamilyIndex++;
}
free(families);
if(ComputeQueueFamilyIndex == count)
{
printf("Compute family not found\n");
return;
}
float prio = 1.0f;
VkDeviceCreateInfo deviceCreateInfo = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.pQueueCreateInfos = &(VkDeviceQueueCreateInfo) {
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.queueFamilyIndex = ComputeQueueFamilyIndex,
.queueCount = 1,
.pQueuePriorities = &prio,
},
.queueCreateInfoCount = 1,
};
if (vkCreateDevice(PhysicalDevice, &deviceCreateInfo, NULL, &LogicalDevice) != VK_SUCCESS)
{
printf("Could not create logical device\n");
return;
}
vkGetDeviceQueue(LogicalDevice, ComputeQueueFamilyIndex, 0, &ComputingQueue);
}
void CreateCommandPool(void)
{
VkCommandPoolCreateInfo poolCreateInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.queueFamilyIndex = ComputeQueueFamilyIndex,
};
if (vkCreateCommandPool(LogicalDevice, &poolCreateInfo, NULL, &ComputeCmdPool) != VK_SUCCESS)
{
printf("Failed to create command pool\n");
}
}
void CreateDescriptorPool(void)
{
VkDescriptorPoolCreateInfo descriptorPoolCreateInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.maxSets = 1,
.pPoolSizes = &(VkDescriptorPoolSize){
.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptorCount = 2,
},
.poolSizeCount = 1,
};
if (vkCreateDescriptorPool(LogicalDevice, &descriptorPoolCreateInfo, NULL, &DescriptorPool) != VK_SUCCESS)
{
printf("Failed to create descriptor pool\n");
}
}
void DestroyCommandPoolAndLogicalDevice(void)
{
if (ComputeCmdPool != VK_NULL_HANDLE)
{
vkDestroyCommandPool(LogicalDevice, ComputeCmdPool, NULL);
}
if (DescriptorPool != VK_NULL_HANDLE)
{
vkDestroyDescriptorPool(LogicalDevice, DescriptorPool, NULL);
}
DestroyBuffers();
if(LogicalDevice != VK_NULL_HANDLE)
{
vkDestroyDevice(LogicalDevice,NULL);
}
}