-
Notifications
You must be signed in to change notification settings - Fork 686
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
Add sample for dynamic rendering local read #887
Merged
marty-johnson59
merged 29 commits into
KhronosGroup:main
from
SaschaWillems:dynamic_rendering_local_read
Sep 6, 2024
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
e7e9b2d
Updated Vulkan headers tp 1.3.276
SaschaWillems 4bad03b
Only destroy render pass if != null
SaschaWillems 036fd41
Added support for loading colors from glTF files
SaschaWillems 7277926
Added new sample for dynamic rendering local read
SaschaWillems 691f845
Added dynamic rendering local read sample to readme and docs navigation
SaschaWillems 286c4fb
Update copyright
SaschaWillems 3573958
Adjusted memory barrier
SaschaWillems 7504395
Clean up api sample base class interfaces
SaschaWillems 9214e1e
Revert "Clean up api sample base class interfaces"
SaschaWillems ed8a8d1
Merge remote-tracking branch 'upstream/main' into dynamic_rendering_l…
SaschaWillems 860036c
Adjust to recent framework changes
SaschaWillems efdf627
Clang format
SaschaWillems 227fab4
Disable GUI for dynamic rendering
SaschaWillems 8dad7eb
Tring to fix things
SaschaWillems cec4458
Correct merge error
SaschaWillems 3ffad83
Added missing break
SaschaWillems a9b1523
Merge remote-tracking branch 'upstream/main' into dynamic_rendering_l…
SaschaWillems 5967c05
Fix documentation
SaschaWillems be1c22c
Minor cleanup
SaschaWillems 29edcdf
Desperately trying to fix clang format
SaschaWillems 05509bc
Trying to fix CI
SaschaWillems 3a86d5d
Merge remote-tracking branch 'upstream/main' into dynamic_rendering_l…
SaschaWillems d5e2a4b
Trying to fix CI
SaschaWillems 6ee9bed
Merge branch 'main' of https://github.com/SaschaWillems/Vulkan-Samples
SaschaWillems fdc6969
Merge branch 'main' of https://github.com/SaschaWillems/Vulkan-Samples
SaschaWillems 0c64397
Merge branch 'main' into dynamic_rendering_local_read
SaschaWillems 4230301
Merge branch 'main' into dynamic_rendering_local_read
SaschaWillems df6f17b
Clang format
SaschaWillems 74a5cea
Added read-only flag to lights buffer
SaschaWillems File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,7 @@ struct Vertex | |
glm::vec2 uv; | ||
glm::vec4 joint0; | ||
glm::vec4 weight0; | ||
glm::vec3 color; | ||
}; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,3 @@ include::./extensions/README.adoc[] | |
|
||
[[tooling-samples]] | ||
include::./tooling/README.adoc[] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You removed more or less the complete readme?!? |
||
[[general-samples]] | ||
include::./general/README.adoc[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
samples/extensions/dynamic_rendering_local_read/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright (c) 2024, Sascha Willems | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 the "License"; | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
get_filename_component(FOLDER_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) | ||
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_LIST_DIR} PATH) | ||
get_filename_component(CATEGORY_NAME ${PARENT_DIR} NAME) | ||
|
||
add_sample( | ||
ID ${FOLDER_NAME} | ||
CATEGORY ${CATEGORY_NAME} | ||
AUTHOR "Sascha Willems" | ||
NAME "Dynamic Rendering local reads" | ||
DESCRIPTION "Demonstrates the dynamic rendering local read extension to use input attachments with dynamic rendering" | ||
SHADER_FILES_GLSL | ||
"dynamic_rendering_local_read/composition.frag" | ||
"dynamic_rendering_local_read/composition.vert" | ||
"dynamic_rendering_local_read/scene_opaque.vert" | ||
"dynamic_rendering_local_read/scene_opaque.frag" | ||
"dynamic_rendering_local_read/scene_transparent.vert" | ||
"dynamic_rendering_local_read/scene_transparent.frag") |
121 changes: 121 additions & 0 deletions
121
samples/extensions/dynamic_rendering_local_read/README.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//// | ||
- Copyright (c) 2024, Sascha Willems | ||
- | ||
- SPDX-License-Identifier: Apache-2.0 | ||
- | ||
- Licensed under the Apache License, Version 2.0 the "License"; | ||
- you may not use this file except in compliance with the License. | ||
- You may obtain a copy of the License at | ||
- | ||
- http://www.apache.org/licenses/LICENSE-2.0 | ||
- | ||
- Unless required by applicable law or agreed to in writing, software | ||
- distributed under the License is distributed on an "AS IS" BASIS, | ||
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
- See the License for the specific language governing permissions and | ||
- limitations under the License. | ||
- | ||
//// | ||
= Dynamic Rendering local read | ||
|
||
ifdef::site-gen-antora[] | ||
TIP: The source for this sample can be found in the https://github.com/KhronosGroup/Vulkan-Samples/tree/main/samples/extensions/dynamic_rendering_local_read[Khronos Vulkan samples github repository]. | ||
endif::[] | ||
|
||
image::./images/sample.png[Sample] | ||
|
||
== Overview | ||
|
||
This sample demonstrates how to use the `VK_KHR_dynamic_rendering_local_read` extension in conjunction with the `VK_KHR_dynamic_rendering` extension. This combination can replace core render and subpasses, making it possible to do local reads via input attachments with dynamic rendering. | ||
|
||
== Toggling between dynamic rendering and renderpasses | ||
|
||
To make it easy to compare the two different approaches of using either dynamic rendering + local reads or renderpasses + subpasses, this sample has code for both rendering paths. | ||
|
||
A define in `dynamic_rendering_local_read.h` can be used to toggle between the two techniques: | ||
|
||
[,cpp] | ||
---- | ||
#define USE_DYNAMIC_RENDERING | ||
---- | ||
|
||
This is enabled by default, making the sample use dynamic rendering with local reads. If you want to use renderpass + subpasses instead, comment this define out and compile the sample. | ||
|
||
== Comparison | ||
|
||
For a primer on the differences between renderpasses and dynamic rendering, see the readme of the xref:../dynamic_rendering/README.adoc[dynamic rendering sample]. | ||
|
||
Here is the comparison table from that example extended with the newly added features from `VK_KHR_dynamic_rendering_local_read` in *bold*: | ||
|
||
|=== | ||
| Vulkan 1.0 | Dynamic Rendering | ||
|
||
| Rendering begins with `vkCmdBeginRenderPass` | ||
| Rendering begins with `vkCmdBeginRenderingKHR` | ||
|
||
| Rendering struct is `VkRenderPassBeginInfo` | ||
| Rendering struct is `VkRenderingInfoKHR` | ||
|
||
| Attachments are referenced by `VkFramebuffer` | ||
| Attachments are referenced by `VkRenderingAttachmentInfoKHR` | ||
|
||
| `VkFramebuffer` objects are heap-allocated and opaque | ||
| `VkRenderingAttachmentInfoKHR` objects are stack-allocated | ||
|
||
| Graphics pipeline creation references a `VkRenderPass` | ||
| Graphics pipeline creation references a `VkPipelineRenderingCreateInfoKHR` | ||
|
||
| *Subpasses are advanced with `vkCmdNextSubpass`* | ||
| *`VkImageMemoryBarrier` to `VK_IMAGE_LAYOUT_RENDERING_LOCAL_READ_KHR` image layout* | ||
|
||
| *Local reads in shaders use `subpassLoad`* | ||
| *Local reads in shaders use `subpassLoad`* | ||
|=== | ||
|
||
== The sample | ||
|
||
With subpasses it's possible to do pixel local reads within a single renderpass. Local read means that you can't freely sample (like with a texture + sampler) but are instead limited to reading the pixel value from the previous subpass at the exact same position. This is based on how esp. tile based GPU architectures work. On such architectures workloads that don't need to sample arbitrarily can improve performance using subpasses and pixel local reads using input attachments. One such example is a deferred renderer with a composition pass. First multiple attachments are filled with different information (albedo, normals, world space position) and then at a later point those attachments are combined into a single image. This composition step reads those attachments at the exact same position that the current pass is operating on, so instead of sampling from these we can use them as input attachments instead and do only pixel local reads. | ||
|
||
The rendering setup for this sample looks like this: | ||
|
||
image::./images/deferred_setup.png[Deferred setup describing subpasses] | ||
|
||
A big criticism with renderpasses was how involved esp. the setup is. Getting renderpasses and subpasses incl. dependencies correct can be tricky and renderpasses are kinda hard to integrate into a dynamically changing setup, making them a hard fit for complex Vulkan projects like game engines. With dynamic rendering, setup is far less involved and moves mostly to command buffer creation. If you look at the sample you can easily spot how much code required by looking at the parts that are deactivated via the `dynamic_rendering_local_read` C++ define. More on this can be found in the xref:../dynamic_rendering/README.adoc[dynamic rendering sample] readme. For this sample we'll only look at draw time. | ||
|
||
== Replacing subpasses for local reads | ||
|
||
=== Input attachments | ||
|
||
Just like local reads in subpasses, dynamic rendering local read also makes use of input attachments. That should make it easy to convert existing code to this new extension. So unless you do advanced things like input attachment reordering, the changes required to add pixel local reads to dynamic rendering are minimal and only affect the application side. There are no changes to the shader interface, so shaders that have been used with renderpasses + subpasses can be used without any changes. Even with dynamic rendering and local reads you use `subpassInput` and `subpassLoad`. | ||
|
||
=== Self-Dependencies | ||
|
||
With the `dynamicRenderingLocalReads` feature enabled, it's now possible to use pipeline barriers within dynamic rendering if they include the `VK_DEPENDENCY_BY_REGION_BIT`. Such a barrier makes attachments before the barrier readable as input attachments afterwards. The extension also introduces the new image layout `VK_IMAGE_LAYOUT_RENDERING_LOCAL_READ_KHR` that can be used for storage images and attachments to make writes to those visible via input attachments. | ||
|
||
=== Renderpasses with subpasses | ||
|
||
. Start a new renderpass with `vkCmdBeginRenderPass` (this also starts the first subpass) | ||
. Fill G-Buffer attachments | ||
. Start the second subpass with `vkCmdNextSubpass` | ||
. Combine G-Buffer attachments using input attachments (and draw to screen using a full-screen quad) | ||
. Start the third subpass with `vkCmdNextSubpass` | ||
. Draw transparent geometry with a forward pass reading depth from an attachment | ||
. End renderpass with `vkCmdEndRenderPass` | ||
|
||
=== Dynamic render with local read | ||
|
||
. Start dynamic rendering with `vkCmdBeginRenderingKHR` | ||
. Fill G-Buffer attachments | ||
. Insert a memory barrier with the "by region" bit set to make attachment writes visible for input attachment reads for the next draw call | ||
. Combine G-Buffer attachments using input attachments (and draw to screen using a full-screen quad) | ||
. Draw transparent geometry with a forward pass reading depth from an attachment | ||
. End dynamic rendering with `vkCmdEndRenderingKHR` | ||
|
||
== Conclusion | ||
|
||
With the addition of `VK_KHR_dynamic_rendering_local_read` it's now finally possible to fully replace renderpasses, including those that have multiple subpasses. This makes dynamic rendering a fully fledged replacement for renderpasses on all implementations, including tile based architectures. | ||
|
||
== Additional information | ||
|
||
* https://docs.vulkan.org/spec/latest/proposals/proposals/VK_KHR_dynamic_rendering_local_read.html[Extension proposal] | ||
* https://www.khronos.org/blog/streamlining-subpasses[Extension blog post] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to check for
(render_pass != VK_NULL_HANDLE)
here, as vkDestroyRenderPass can handle that.