-
在着色器代码中定义分离的纹理和采样器变量:
layout(set = 0, binding = 0) uniform sampler mySampler; // 定义纹理采样器变量
layout(set = 0, binding = 1) uniform texture2D myTexture; // 定义纹理变量
在这里,我们通过 layout(set = x, binding = y) 指令来指定纹理或采样器变量的绑定点。 set 表示纹理集的索引,并且必须在着色器管道创建时进行分配。 binding 表示特定于着色器代码的纹理索引,具有唯一性且必须相对于纹理集而言是唯一的。
-
在代码中,我们需要通过 vkBindImageMemory() 函数为纹理和采样器以及它们对应的纹理图像和采样器描述符分别分配内存:
auto textureImage = createImage(texWidth, texHeight, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
auto textureImageView = createImageView(textureImage.image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT);
VkSamplerCreateInfo samplerInfo = {};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerInfo.magFilter = VK_FILTER_LINEAR;
samplerInfo.minFilter = VK_FILTER_LINEAR;
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.anisotropyEnable = VK_TRUE;
samplerInfo.maxAnisotropy = 16;
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
samplerInfo.unnormalizedCoordinates = VK_FALSE;
samplerInfo.compareEnable = VK_FALSE;
samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
samplerInfo.mipLodBias = 0.0f;
samplerInfo.minLod = 0.0f;
samplerInfo.maxLod = static_cast<float>(mipLevels);
VkSampler sampler;
if (vkCreateSampler(device, &samplerInfo, nullptr, &sampler) != VK_SUCCESS) {
throw std::runtime_error("failed to create texture sampler!");
}
vkBindImageMemory(device, textureImage.image, textureImage.memory, 0);
vkBindImageMemory(device, textureImage.image, textureImage.memory, 0);
以上是 Vulkan 中用于创建纹理和采样器的代码。对于纹理,我们需要首先通过 createImage() 函数创建一个二维纹理图像,并使用 vkBindImageMemory() 函数将图像内存绑定到适当位置。接着我们创建 VkImageView 对象,该对象用于访问纹理图片的特定方面,并在 vkUpdateDescriptorSetWithImageView() 函数中使用该视图对象更新描述符集。对于采样器,我们通过 vkCreateSampler() 函数创建一个采样器对象,并将其用于纹理采样。
-
创建对应的描述符集和描述符布局:
VkDescriptorSetLayoutBinding samplerLayoutBinding = {};
samplerLayoutBinding.binding = 0;
samplerLayoutBinding.descriptorCount = 1;
samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER;
samplerLayoutBinding.pImmutableSamplers = nullptr;
samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
// 创建纹理采样器布局绑定描述符
VkDescriptorSetLayoutBinding textureLayoutBinding = {};
textureLayoutBinding.binding = 1;
textureLayoutBinding.descriptorCount = 1;
textureLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
textureLayoutBinding.pImmutableSamplers = nullptr;
textureLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
// 这里我们将纹理采样器描述符和纹理描述符都放在了同一个描述符集中
std::array<VkDescriptorSetLayoutBinding, 2> bindings = { samplerLayoutBinding, textureLayoutBinding };
VkDescriptorSetLayoutCreateInfo layoutInfo = {};
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
layoutInfo.pBindings = bindings.data();
if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) {
throw std::runtime_error("failed to create descriptor set layout!");
}
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 1;
pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout;
if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
throw std::runtime_error("failed to create pipeline layout!");
}
我们使用VkDescriptorSetLayoutBinding定义了两个布局绑定。一个绑定采样器样本,另一个绑定纹理图像。这两个绑定都被指定为只能通过着色器访问,指明了它们的数据类型和数量,并且通过pImmutableSamplers指针指定了用作相应绑定的唯一采样器对象和纹理对象。我们将这两个绑定打包到单个数组中,并传递到VkDescriptorSetLayoutCreateInfo结构体中,以便为描述符集创建布局。我们还在VkPipelineLayoutCreateInfo中指定了上述描述符集。
-
创建VkWriteDescriptorSet结构体并对其进行更新:
void Texture::createDescriptorSets() {
std::vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), descriptorSetLayout);
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size());
allocInfo.pSetLayouts = layouts.data();
descriptorSets.resize(swapChainImages.size());
if (vkAllocateDescriptorSets(device, &allocInfo, descriptorSets.data()) != VK_SUCCESS) {
throw std::runtime_error("failed to allocate descriptor sets!");
}
for (size_t i = 0; i < swapChainImages.size(); i++) {
VkDescriptorBufferInfo bufferInfo = {};
bufferInfo.buffer = uniformBuffers[i];
bufferInfo.offset = 0;
bufferInfo.range = sizeof(UniformBufferObject);
VkDescriptorImageInfo imageInfo = {};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = textureImageView;
imageInfo.sampler = textureSampler;
std::array<VkWriteDescriptorSet, 2> descriptorWrites = {};
descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[0].dstSet = descriptorSets[i];
descriptorWrites[0].dstBinding = 0;
descriptorWrites[0].dstArrayElement = 0;
descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER;
descriptorWrites[0].descriptorCount = 1;
descriptorWrites[0].pSamplerInfo = &samplerInfo;
descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[1].dstSet = descriptorSets[i];
descriptorWrites[1].dstBinding = 1;
descriptorWrites[1].dstArrayElement = 0;
descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
descriptorWrites[1].descriptorCount = 1;
descriptorWrites[1].pImageInfo = &imageInfo;
vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
}
}
在此代码中,我们首先为描述符集分配空间,并使用 VkDescriptorSetLayout 布局对象在 for 循环中迭代每个 swapChainImage。在每次迭代中,我们将每个 UniformBufferObject 绑定到相应的着色器变量块中,并将纹理采样状态和图像绑定到旋转链的描述符集中。最后,我们调用vkUpdateDescriptorSets()将新修改的描述符集写入到显卡内存。
在Vulkan中,我们需要借助于图形管线、缓存区和原语等等细节以及动态切换而进行大量的底层控制,但它是一个灵活高效的图形API,并且支持像分离纹理和采样器这样的功能。