-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09_Descriptors.h
112 lines (94 loc) · 6.14 KB
/
09_Descriptors.h
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
#pragma once // include guard
//# -----------------------------------------------------
// Step 9 - SetupDescriptors
//
//________//________// START Variables and Functions before main function of this step
//________//________// END Variables and Functions before main function of this step
void SetupDescriptors(VkDevice device,
VkBuffer buffer,
VkDescriptorSet* outDescriptorSet,
VkDescriptorSetLayout* outDescriptorSetLayout)
{
{
// Define and create your descriptors:
VkDescriptorSetLayoutBinding bindings[1];
// uniform buffer for your matrices:
bindings[0].binding = 0;
bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
bindings[0].descriptorCount = 1;
bindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
bindings[0].pImmutableSamplers = NULL;
VkDescriptorSetLayoutCreateInfo setLayoutCreateInfo = {};
setLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
setLayoutCreateInfo.bindingCount = 1;
setLayoutCreateInfo.pBindings = bindings;
VkResult result =
vkCreateDescriptorSetLayout( device,
// logical device that creates the descriptor set layout
&setLayoutCreateInfo,
// pointer to structure specifying the state of the descriptor set layout
NULL,
// optional controling host memory allocation
outDescriptorSetLayout );
// pointer to VkDescriptorSetLayout handle returned descriptor set layout object
ERR_VULKAN_EXIT( result, "Failed to create DescritorSetLayout." );
// descriptor pool creation:
VkDescriptorPoolSize uniformBufferPoolSize[1];
uniformBufferPoolSize[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
uniformBufferPoolSize[0].descriptorCount = 1;
VkDescriptorPoolCreateInfo poolCreateInfo = {};
poolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
poolCreateInfo.maxSets = 1;
poolCreateInfo.poolSizeCount = 1;
poolCreateInfo.pPoolSizes = uniformBufferPoolSize;
VkDescriptorPool descriptorPool;
result = vkCreateDescriptorPool( device,
// logical device that creates the descriptor pool
&poolCreateInfo,
// pointer to VkDescriptorPoolCreateInfo structure specifying the descriptor pool
NULL,
// optional controling host memory allocation
&descriptorPool );
// pointer to returning VkDescriptorPool handle for resulting descriptor pool object
ERR_VULKAN_EXIT( result, "Failed to create descriptor pool." );
// allocate your descriptor from the pool:
VkDescriptorSetAllocateInfo dsai = {};
dsai.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
dsai.descriptorPool = descriptorPool;
dsai.descriptorSetCount = 1;
dsai.pSetLayouts = outDescriptorSetLayout;
result = vkAllocateDescriptorSets( device,
// logical device that owns the descriptor pool
&dsai,
// pointer to VkDescriptorSetAllocateInfo structure describing allocation details
outDescriptorSet );
// pointer return array of VkDescriptorSet handles for the resulting descriptor sest objects
ERR_VULKAN_EXIT( result, "Failed to allocate descriptor sets." );
// When a set is allocated all values are undefined and all descriptors are not initialized.
// You must init all statically used bindings:
VkDescriptorBufferInfo dbi = {};
dbi.buffer = buffer;
dbi.offset = 0;
dbi.range = VK_WHOLE_SIZE;
VkWriteDescriptorSet wd = {};
wd.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
wd.dstSet = *outDescriptorSet;
wd.dstBinding = 0;
wd.dstArrayElement = 0;
wd.descriptorCount = 1;
wd.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
wd.pImageInfo = NULL;
wd.pBufferInfo = &dbi;
wd.pTexelBufferView = NULL;
vkUpdateDescriptorSets( device,
// logical device that updates the descriptor sets
1,
// number of elements in the pDescriptorWrites array
&wd,
// pointer to an array of VkWriteDescriptorSet structures describing the descriptor sets
0,
// number of elements in the pDescriptorCopies array
NULL );
// pointer to an array of VkCopyDescriptorSet structures describing the descriptor sets to copy
}
}// END SetupDescriptors(..)