Skip to content

Edit tutorial to reset & record command buffers each frame #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 21, 2022

Conversation

charles-lunarg
Copy link
Contributor

This is a large change to the tutorial which makes the tutorial re-record command buffers. Originally described in #202 (and this PR fixes #202 )

Changes:

  • Modify the function that records into the command buffer so that it accepts a command buffer and image index.
  • Split "Rendering and Presentation" into 2 chapters, one that introduces fences, semaphores, the main rendering functions (acquire/submit/present). Second chapter is dedicated to introducing multiple frames in flight.
  • Rewrite the introduction of fences and semaphores.
  • Made buffers, descriptors, and command buffers have MAX_IMAGE_IN_FLIGHT duplication, not swapchain image count.
  • Remove command buffer, descriptor set, and VkBuffer recreation from swapchain resizing
  • Remove imagesInFlight, since it is no longer necessary.

@Overv
Copy link
Owner

Overv commented Dec 20, 2021

Very nice! I'll be able to start reviewing this PR sometime this week.

@charles-lunarg
Copy link
Contributor Author

charles-lunarg commented Dec 20, 2021

It has come to my attention that there may be a sync bug W.R.T the depth buffer. Gist is that the same depth buffer could be read/written to by multiple frames in flight. A much more detailed description & solution is presented in the vk-guide.dev repo.
vblanco20-1/vulkan-guide#53

@ralphtheninja
Copy link

@charles-lunarg Thanks for taking your time with this. I'm a beginner myself and I was pondering these exact concepts the other day.

Copy link
Owner

@Overv Overv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work! I have only some minor comments 👍

@ralphtheninja
Copy link

Nice review!

@Overv
Copy link
Owner

Overv commented Dec 29, 2021

@Krenodeno Would you be interested in incorporating these changes in the French translation as well? Otherwise we'll have to add a disclaimer that some parts of the translation are out of date.

@Krenodeno
Copy link
Contributor

Sure, I'll do it. Not sure how much time it will take though, I'll do my best :)

@charles-lunarg
Copy link
Contributor Author

I addressed all the changes requested by Overv, cleaning up some paragraphs, and fixing resizing to not deadlock.

These include:

  • Adding more information about fence and semaphore resetting
  • Adding a paragraph explaining the choice of 2 for MAX_FRAMES_IN_FLIGHT
  • Using uint32_t for currentFrame instead of size_t
  • Moving resetting the fence to after acquiring a swapchain image
  • Uncommenting some accidentally commented code.

@Krenodeno
Copy link
Contributor

@Overv How can I edit the translation ? Forking the fork will not display change here (or does it ?), or Charles will have to merge to be displayed here.

@Overv
Copy link
Owner

Overv commented Jan 25, 2022

@Overv How can I edit the translation ? Forking the fork will not display change here (or does it ?), or Charles will have to merge to be displayed here.

There are still going to be a few changes to this PR (possibly somewhat significant), so I'd wait a bit with the translation work. I think that once this PR is finished it would be good to get the changes in the English version out as quickly as possible and then you can work on the translation in your own pull request just like with earlier changes.

@Krenodeno
Copy link
Contributor

There are still going to be a few changes to this PR (possibly somewhat significant), so I'd wait a bit with the translation work. I think that once this PR is finished it would be good to get the changes in the English version out as quickly as possible and then you can work on the translation in your own pull request just like with earlier changes.

Okay, that's good for me !

Means the code now calls recordCommandBuffer every frame instead of ahead of time

Many changes to the structure of chapters 14 & 15, with small changes to subsequent
chapters.

Primary focus was shifting everything away from 'swapchain image count' to
MAX_FRAMES_IN_FLIGHT. This has caused a lot of chapter 15 to need to be rewritten.
Such as: Introducing fences alongside semaphores and not later; Waiting on a fence
at the start of the frame before introducing acquireNextImage; consolidating the
Frames In Flight concepts to apply to command buffers, fences, and semaphores at
the same time.

Chapter 14 saw command buffer allocation reduced to 1 at a time. This allows the
concept of Frames in Flight to not need introduction before having a triangle
drawing on the screen.
Greatly improve the descriptions of semaphores and fences before their
introduction into the code. Provide examples with psuedo code.

By using max_frames_in_flight command buffers & semaphores, we dont
need to keep track of previously sumitted frames' fences and wait
on them "just in case". This removes a lot of the confusion I had
when I first was trying to understand the vulkan update loop.
Un-comment vkDeviceWaitIdle and remove resizing of imagesInFlight (which was removed)
* Add description of fences needing explicit resetting while semaphores are automatic
* Elaborate on why 2 frames in flight are chosen
Because acquiring the swapchain image index may cause drawFrame to return early,
it was possible to cause the next vkWaitForFences to deadlock. By delaying fence
resetting till after acquiring, it prevents the deadlock.
@charles-lunarg charles-lunarg force-pushed the rerecord_command_buffers branch from 2c85b77 to 4bd8019 Compare February 7, 2022 07:21
@charles-lunarg
Copy link
Contributor Author

FYI There is nothing more to do in this PR as far as I can tell.
I don't have any outstanding questions or concerns. Totally capable of doing further revisions if someone else wants to review.

@SaschaWillems
Copy link
Contributor

Totally happy with these changes. They make the tutorial as a whole much better. Would love to se this merged and the website updated :)

@Overv
Copy link
Owner

Overv commented Feb 21, 2022

Excellent work!

@Overv Overv merged commit eaa9f7d into Overv:master Feb 21, 2022
@ralphtheninja
Copy link

@Overv Is this automatically deployed to the live tutorial?

@Overv
Copy link
Owner

Overv commented Feb 21, 2022

@ralphtheninja Yeah, the website is automatically redeployed once a day.

@charles-lunarg charles-lunarg deleted the rerecord_command_buffers branch February 27, 2022 21:30
@Overv
Copy link
Owner

Overv commented Mar 1, 2022

@Krenodeno Would you have time to work on the translation soon? :)

@Krenodeno
Copy link
Contributor

I'll try to look at it this week.
I think there are other changes made by other PRs that I'll have to update as well.

Overv added a commit that referenced this pull request Mar 8, 2022
@Overv Overv mentioned this pull request Apr 10, 2022
Overv pushed a commit that referenced this pull request Mar 8, 2023
* Update tutorial for command buffer re-recording

Means the code now calls recordCommandBuffer every frame instead of ahead of time

Many changes to the structure of chapters 14 & 15, with small changes to subsequent
chapters.

Primary focus was shifting everything away from 'swapchain image count' to
MAX_FRAMES_IN_FLIGHT. This has caused a lot of chapter 15 to need to be rewritten.
Such as: Introducing fences alongside semaphores and not later; Waiting on a fence
at the start of the frame before introducing acquireNextImage; consolidating the
Frames In Flight concepts to apply to command buffers, fences, and semaphores at
the same time.

Chapter 14 saw command buffer allocation reduced to 1 at a time. This allows the
concept of Frames in Flight to not need introduction before having a triangle
drawing on the screen.

* Update introduction to semaphores and fences

Greatly improve the descriptions of semaphores and fences before their
introduction into the code. Provide examples with psuedo code.

By using max_frames_in_flight command buffers & semaphores, we dont
need to keep track of previously sumitted frames' fences and wait
on them "just in case". This removes a lot of the confusion I had
when I first was trying to understand the vulkan update loop.

* Remove accidental code changes

Un-comment vkDeviceWaitIdle and remove resizing of imagesInFlight (which was removed)

* Use uint32_t instead of size_t for currentFrame

* Address typos and fixup changes for re-recording command buffers PR

* Add description of fences needing explicit resetting while semaphores are automatic
* Elaborate on why 2 frames in flight are chosen

* Fix deadlock in resizing from resetting the fence too early

Because acquiring the swapchain image index may cause drawFrame to return early,
it was possible to cause the next vkWaitForFences to deadlock. By delaying fence
resetting till after acquiring, it prevents the deadlock.
Overv added a commit that referenced this pull request Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Re-record command buffers every frame
5 participants