Rhu Mage
Rhu Mage

Reputation: 717

Why do I need resources per swapchain image

I have been following different tutorials and I don't understand why I need resources per swapchain image instead of per frame in flight.

This tutorial: https://vulkan-tutorial.com/Uniform_buffers

has a uniform buffer per swapchain image. Why would I need that if different images are not in flight at the same time? Can I not start rewriting if the previous frame has completed?

Also lunarg tutorial on depth buffers says:

And you need only one for rendering each frame, even if the swapchain has more than one image. This is because you can reuse the same depth buffer while using each image in the swapchain.

This doesn't explain anything, it basically says you can because you can. So why can I reuse the depth buffer but not other resources?

Upvotes: 4

Views: 2361

Answers (2)

krOoze
krOoze

Reputation: 13246

It is to minimize synchronization in the case of the simple Hello Cube app.

Let's say your uniforms change each frame. That means main loop is something like:

  1. Poll (or simulate)
  2. Update (e.g. your uniforms)
  3. Draw
  4. Repeat

If step #2 did not have its own uniform, then it needs to write a uniform previous frame is reading. That means it has to sync with a Fence. That would mean the previous frame is no longer considered "in-flight".

Upvotes: 6

Ekzuzy
Ekzuzy

Reputation: 3437

It all depends on the way You are using Your resources and the performance You want to achieve.

If, after each frame, You are willing to wait for the rendering to finish and You are still happy with the final performance, You can use only one copy of each resource. Waiting is the easiest synchronization, You are sure that resources are not used anymore, so You can reuse them for the next frame. But if You want to efficiently utilize both CPU's and GPU's power, and You don't want to wait after each frame, then You need to see how each resource is being used.

Depth buffer is usually used only temporarily. If You don't perform any postprocessing, if Your render pass setup uses depth data only internally (You don't specify STORE for storeOp), then You can use only one depth buffer (depth image) all the time. This is because when rendering is done, depth data isn't used anymore, it can be safely discarded. This applies to all other resources that don't need to persist between frames.

But if different data needs to be used for each frame, or if generated data is used in the next frame, then You usually need another copy of a given resource. Updating data requires synchronization - to avoid waiting in such situations You need to have a copy a resource. So in case of uniform buffers, You update data in a given buffer and use it in a given frame. You cannot modify its contents until the frame is finished - so to prepare another frame of animation while the previous one is still being processed on a GPU, You need to use another copy.

Similarly if the generated data is required for the next frame (for example framebuffer used for screen space reflections). Reusing the same resource would cause its contents to be overwritten. That's why You need another copy.

You can find more information here: https://software.intel.com/en-us/articles/api-without-secrets-the-practical-approach-to-vulkan-part-1

Upvotes: 2

Related Questions