我正在尝试使用但不了解该过程来生成 mipmaps 运行时(实现glGenerateMipmap()
功能) 。vkCmdBlitImage
到目前为止,这是我的代码:
VkImageCreateInfo imageCreateInfo = {};
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageCreateInfo.pNext = nullptr;
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
imageCreateInfo.format = format;
imageCreateInfo.mipLevels = mipLevels;
imageCreateInfo.arrayLayers = 1;
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageCreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
imageCreateInfo.extent = { static_cast<std::uint32_t>( width ), static_cast<std::uint32_t>( height ), 1 };
imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
err = vkCreateImage( GfxDeviceGlobal::device, &imageCreateInfo, nullptr, &image );
...
for (int i = 1; i < mipLevels; ++i)
{
const std::int32_t mipWidth = width >> i;
const std::int32_t mipHeight = height >> i;
VkImageBlit imageBlit = {};
imageBlit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageBlit.srcSubresource.baseArrayLayer = 0;
imageBlit.srcSubresource.layerCount = 1;
imageBlit.srcSubresource.mipLevel = 0;
imageBlit.srcOffsets[ 0 ] = { 0, 0, 0 };
imageBlit.srcOffsets[ 1 ] = { width, height, 1 };
imageBlit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageBlit.dstSubresource.baseArrayLayer = 0;
imageBlit.dstSubresource.layerCount = 1;
imageBlit.dstSubresource.mipLevel = i;
imageBlit.dstOffsets[ 0 ] = { 0, 0, 0 };
imageBlit.dstOffsets[ 1 ] = { mipWidth, mipHeight, 1 };
vkCmdBlitImage( texCmdBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1, &imageBlit, VK_FILTER_LINEAR );
}
每个 mip 级别都需要自己的图像吗?它们应该如何初始化?为纹理数据使用暂存缓冲区时,我应该何时生成 mipmap?
编辑:我将代码修改为只使用建议的一个图像,现在它似乎可以工作(我可以在 RenderDoc 纹理查看器中看到正确的 mip 级别图像,并且我没有收到任何验证错误)。