Advertisement

What is the proper way to create a mipmapped texture in Vulkan?

Started by May 24, 2017 06:39 AM
1 comment, last by GuyWithBeard 7 years, 8 months ago

Hey,

I am wondering what is the most efficient way to create a mipmapped texture for use with a texture sampler in Vulkan. Basically I have a bunch of mip levels that I want to tuck into an image with the tiling set to VK_IMAGE_TILING_OPTIMAL, the layout VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL and the memory properties VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT.

To be able to map to host memory and memcpy the pixel data into the image I have to create a staging image with VK_IMAGE_TILING_LINEAR right? However, VK_IMAGE_TILING_LINEAR only supports one mip level. So what I do is create a staging image for every mip level, memcpy into those and then copy the mip level 0 of each staging image into the correct mip level of the final texture, with vkCmdCopyImage.

This seems like an awful lot of temporary images and copying just to get the texture put together. Is this the best way to do it? How do you do it? Do you have a pool of staging images or do you create temporaries every time?

Cheers!

There are a couple of different ways to load mipmapped textures in Vulkan. I use this way:

  • create a buffer that is big enough to contain all mipmap levels
  • bind it to pre-allocated scratch memory that is VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
  • copy the mipmaps into the buffer using vkMapMemory, memcpy, vkUnmapMemory
  • create an image with VK_IMAGE_TILING_OPTIMAL and bind it to VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT memory
  • use vkCmdCopyBufferToImage with VkBufferImageCopy entries for each mipmap that contain the offset into the buffer to point to the mip data.

Also make sure to use vkGetBufferMemoryRequirements and vkGetImageMemoryRequirements. The scratch host visible memory size is 6mb, which is enough for most textures and all the device local textures go into one huge memory.

This works well for mipmapped textures and also mipmapped cube maps.

Advertisement

Thanks, that sounds a lot more efficient!

This topic is closed to new replies.

Advertisement