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_GENERAL;
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;
}