8

It isn't clear to me when it's a good idea to use VK_IMAGE_LAYOUT_GENERAL as opposed to transitioning to the optimal layout for whatever action I'm about to perform. Currently, my policy is to always transition to the optimal layout.

But VK_IMAGE_LAYOUT_GENERAL exists. Maybe I should be using it when I'm only going to use a given layout for a short period of time.

For example, right now, I'm writing code to generate mipmaps using vkCmdBlitImage. As I loop through the sub-resources performing the vkCmdBlitImage commands, should I transition to VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL as I scale down into a mip, then transition to VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL when I'll be the source for the next mip before finally transitioning to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL when I'm all done? It seems like a lot of transitioning, and maybe generating the mips in VK_IMAGE_LAYOUT_GENERAL is better.

I appreciate the answer might be to measure, but it's hard to measure on all my target GPUs (especially because I haven't got anything running on Android yet) so if anyone has any decent rule of thumb to apply it would be much appreciated.

FWIW, I'm writing Vulkan code that will run on desktop GPUs and Android, but I'm mainly concerned about performance on the latter.

Columbo
  • 6,648
  • 4
  • 19
  • 30

2 Answers2

11

You would use it when:

  1. You are lazy
  2. You need to map the memory to host (unless you can use PREINITIALIZED)
  3. When you use the image as multiple incompatible attachments and you have no choice
  4. For Store Images

( 5. Other cases when you would switch layouts too much (and you don't even need barriers) relatively to the work done on the images. Measurement needed to confirm GENERAL is better in that case. Most likely a premature optimalization even then. )

PS: You could transition all the mip-maps together to TRANSFER_DST by a single command beforehand and then only the one you need to SRC. With a decent HDD, it should be even best to already have them stored with mip-maps, if that's a option (and perhaps even have a better quality using some sophisticated algorithm).

PS2: Too bad, there's not a mip-map creation command. The cmdBlit most likely does it anyway under the hood for Images smaller than half resolution....

krOoze
  • 12,301
  • 1
  • 20
  • 34
  • It's important to note that such measurements need to be taken on the appropriate implementations. IE: ones that are known to be sensitive to layouts. – Nicol Bolas May 04 '16 at 16:56
  • ^ Well, that's implied. Measurements should always be done on the machine the code is supposed to run on. – krOoze May 04 '16 at 17:08
  • Accepted this answer. I think your point about premature optimization is really the crux of the matter. I suppose I was worrying that my current approach which possibly over-uses transitions was premature optimization, but you argue convincingly that trying to *avoid* transitions would be the premature optimization in this case. I will carry on using transitions to optimal layouts until I have progressed far enough to measure on target hardware. – Columbo May 05 '16 at 07:51
  • 1
    Yes, using optimal layouts is straightforward enough, to be considered basic (and not an optimalization). ;; What I mean is: often the case 5 begs to be written completely differently and trying to optimalize it directly is a waste of time. ;; Besides log(N) times for your mip-maps is not that much and you need a barrier anyway so the layout transition is probably free as part of that. – krOoze May 05 '16 at 12:22
2

If you read from mipmap[n] image for creating the mipmap[n+1] image then you should use the transfer image flags if you want your code to run on all Vulkan implementations and get the most performance across all implementations as the flags may be used by the GPU to optimize the image for reads or writes.

So if you want to go cross-vendor only use VK_IMAGE_LAYOUT_GENERAL for setting up the descriptor that uses the final image and not image reads or writes.

If you don't want to use that many transitions you may copy from a buffer instead of an image, though you obviously wouldn't get the format conversion, scaling and filtering that vkCmdBlitImage does for you for free.

Also don't forget to check if the target format actually supports the BLIT_SRC or BLIT_DST bits. This is independent of whether you use the transfer or general layout for copies.

Sascha Willems
  • 5,280
  • 1
  • 14
  • 21