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 | static int CreateLayoutBinding(VkDescriptorSetLayout* pDescLayout, VkDescriptorType type, uint32_t binding, VkShaderStageFlags stageFlag)
{
// Setup Layout Binding for the Uniform buffer that contains "camera"
VkDescriptorSetLayoutBinding layoutBinding = {};
layoutBinding.binding = binding;
layoutBinding.descriptorType = type;
layoutBinding.descriptorCount = 1;
layoutBinding.stageFlags = stageFlag;
// Setup the Description Layout Info
VkDescriptorSetLayoutCreateInfo descLayoutInfo = {};
descLayoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descLayoutInfo.pBindings = &layoutBinding;
descLayoutInfo.bindingCount = 1;
// Create Description Layout
VkDescriptorSetLayout descLayout = {};
VkResult res = vkCreateDescriptorSetLayout(_VkSystem.device, &descLayoutInfo, 0, &descLayout);
if (res != VK_SUCCESS)
{
VK_DEBUG("vkCreateDescriptorSetLayout failed");
return res;
}
*pDescLayout = descLayout;
return res;
}
int CreateSampleDescriptionSet(VkDescriptorPool descPool, VkDescriptorSetLayout* pDescLayout, VkDescriptorSet* pDescSet,
VkDeviceMemory* pDevMemory, VkBuffer* pBuffer, VkImage image)
{
VkDescriptorSetLayout descLayout = {};
if (CreateLayoutBinding(&descLayout, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT) != VK_SUCCESS)
return 1;
// Setup Descriptor Allocate Info
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descPool;
allocInfo.pSetLayouts = &descLayout;
allocInfo.descriptorSetCount = 1;
// Allocate Descriptor set/s
VkDescriptorSet descSet = {};
VkResult res = vkAllocateDescriptorSets(_VkSystem.device, &allocInfo, &descSet);
if (res != VK_SUCCESS)
{
VK_DEBUG("vkAllocateDescriptorSets failed");
}
// Create some random big buffer for the Sampler2D
int bufferSize = ALIGN_BUFFER_SIZE(sizeof(float) * 2); // vec2
VkDeviceMemory devMemory;
VkBuffer Buffer;
res = (VkResult)CreateBuffer(bufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &Buffer, &devMemory);
if (res != VK_SUCCESS)
{
VK_DEBUG("Creating Buffer failed");
}
// Setup Descriptor Buffer Info
VkDescriptorBufferInfo bufferInfo = {};
bufferInfo.vertBuffer = Buffer;
bufferInfo.range = bufferSize;
VkImageViewCreateInfo imageViewCreate = {};
imageViewCreate.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
imageViewCreate.format = _VkSystem.deviceFormat;
imageViewCreate.image = image;
imageViewCreate.subresourceRange.layerCount = 1;
imageViewCreate.subresourceRange.levelCount = 1;
imageViewCreate.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageViewCreate.viewType = VK_IMAGE_VIEW_TYPE_2D;
// Create our ImageView
VkImageView imageView = {};
res = vkCreateImageView(_VkSystem.device, &imageViewCreate, 0, &imageView);
if (res != VK_SUCCESS)
{
VK_DEBUG("Creating ImageView failed");
}
VkSamplerCreateInfo samplerInfo = {};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
VkSampler sampler;
res = vkCreateSampler(_VkSystem.device, &samplerInfo, 0, &sampler);
if (res != VK_SUCCESS)
{
VK_DEBUG("Creating Sampler failed");
}
// Setup Descriptor Image Info
VkDescriptorImageInfo imageInfo = {};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = imageView;
imageInfo.sampler = sampler;
// Setup Write Descriptor Set
VkWriteDescriptorSet writeDescSet = {};
writeDescSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescSet.descriptorCount = 1;
writeDescSet.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
writeDescSet.dstSet = descSet;
writeDescSet.pBufferInfo = &bufferInfo;
writeDescSet.pImageInfo = &imageInfo;
writeDescSet.dstBinding = 1;
// Update Descriptor set/s
vkUpdateDescriptorSets(_VkSystem.device, 1, &writeDescSet, 0, 0);
*pDescLayout = descLayout;
*pDescSet = descSet;
*pDevMemory = devMemory;
*pBuffer = Buffer;
return res;
}
|