Once the texture is prepared by linear or optimal tiling, it is just a matter of updating the descriptor set with the created image resource object. This is done in the implemented VulkanDrawable::createDescriptorSet()
function. The input parameter useTexture
must be true
to support the texture. The following highlights a single-line change that is required in this function to support the texture.
When the useTexture
parameter is true
, the second VkWriteDescriptorSet
element (with index 1
) is populated with the texture information. Here are two things that are of utmost importance:
pImageInfo
field of VkWriteDescriptorSet
must be set with the texture object's (TextureData
) descsImgInfo
(of the type VkDescriptorImageInfo
).1
, therefore, writes[1].dstBinding= 1;
:// Creates the descriptor sets using descriptor pool. // This function depend on the createDescriptorPool() // and createUniformBuffer(). void VulkanDrawable::createDescriptorSet(bool useTexture) { VulkanPipeline* pipelineObj = rendererObj->getPipelineObject(); VkResult result; // Create the descriptor allocation structure and specify // the descriptor pool and descriptor layout VkDescriptorSetAllocateInfo dsAllocInfo[1]; dsAllocInfo[0].sType = VK_STRUCTURE_TYPE_DESCRIPTOR _SET_ALLOCATE_INFO; dsAllocInfo[0].pNext = NULL; dsAllocInfo[0].descriptorPool = descriptorPool; dsAllocInfo[0].descriptorSetCount = 1; dsAllocInfo[0].pSetLayouts = descLayout.data(); // Allocate the number of descriptor set needs to be produced descriptorSet.resize(1); // Allocate descriptor sets result = vkAllocateDescriptorSets(deviceObj->device, dsAllocInfo, descriptorSet.data()); assert(result == VK_SUCCESS); // Allocate two write descriptors for - 1. MVP and 2. Texture VkWriteDescriptorSet writes[2]; memset(&writes, 0, sizeof(writes)); // Specify the uniform buffer related // information into first write descriptor writes[0] = {}; writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writes[0].pNext = NULL; writes[0].dstSet = descriptorSet[0]; writes[0].descriptorCount = 1; writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; writes[0].pBufferInfo = &UniformData.bufferInfo; writes[0].dstArrayElement = 0; writes[0].dstBinding = 0; // DESCRIPTOR_SET_BINDING_INDEX // If texture is used then update the second // write descriptor structure if (useTexture) { writes[1] = {}; writes[1].sType = VK_STRUCTURE_TYPE_WRITE- _DESCRIPTOR_SET; writes[1].dstSet = descriptorSet[0]; writes[1].dstBinding = 1; writes[1].descriptorCount = 1; writes[1].descriptorType = VK_DESCRIPTOR_TYPE_ COMBINED_IMAGE_SAMPLER; writes[1].pImageInfo = &textures->descsImgInfo; / } // Update the uniform buffer into the allocated descriptor set vkUpdateDescriptorSets(deviceObj->device, useTexture ? 2 : 1, writes, 0, NULL); }
For more information on this function and the creation of the descriptor set, refer to the Creating the descriptor sets section in Chapter 10, Descriptors and Push Constant.
The following is the output of the rendered texture:
3.15.168.214