-
Notifications
You must be signed in to change notification settings - Fork 781
Add compute shader derivatives sample for Vulkan extension #1421
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
base: main
Are you sure you want to change the base?
Add compute shader derivatives sample for Vulkan extension #1421
Conversation
Introduce a sample demonstrating the `VK_KHR_compute_shader_derivatives` extension. Includes new shader, CMake, and Vulkan application files showcasing the use of derivative instructions in compute shaders with quad-based derivative groups.
| float v = data[idx * 4 + 0]; | ||
| float ddx = data[idx * 4 + 1]; | ||
| float ddy = data[idx * 4 + 2]; | ||
| LOGI("compute-derivatives CPU: tid=({}, {}) v={} ddx={} ddy={}", x, y, v, ddx, ddy); |
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.
Would be better to display them in the app's GUI instead of simply outputting them to the terminal. Depending on how/where you run the sample you don't have direct access to the terminal.
- Store and format compute results for GUI display - Update README with GitHub repository link for sample
|
Again, not sure what I'm supposed to see here, but I'm getting the same weird random frames on the display as I saw with relaxed_extended_instruction and some console output: |
| add_instance_extension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); | ||
| // Device extension providing the feature | ||
| add_device_extension(VK_KHR_COMPUTE_SHADER_DERIVATIVES_EXTENSION_NAME); | ||
| // Toolchains may still emit SPV_NV_compute_shader_derivatives; enable NV extension if available to satisfy validation |
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.
Can we double-check this? Both Slang and DXC should support khr_compute_shader_derivatives.
It’s probably better not to request the NV extension, since if the shader is using SPV_NV_compute_shader_derivatives that would cause issues if a device supports the KHR extension but not the NV version.
We could add a comment suggesting users update their toolchain if they encounter this problem.
| void ComputeShaderDerivatives::request_gpu_features(vkb::core::PhysicalDeviceC &gpu) | ||
| { | ||
| // Require quads derivative group (the sample shader uses layout(derivative_group_quadsNV/derivative_group_quads_khr)) | ||
| REQUEST_REQUIRED_FEATURE(gpu, VkPhysicalDeviceComputeShaderDerivativesFeaturesKHR, computeDerivativeGroupQuads); |
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.
Some implementations only support computeDerivativeGroupLinear. As a suggestion, could we add an example using computeDerivativeGroupLinear?
The mapping from a thread to 2D coordinates isn’t immediately obvious when using 1D quads or computeDerivativeGroupLinear, so it would be useful to have an example
https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_Derivatives.html
Ideally, this sample could have two shader versions—derivatives_linear.comp and derivatives_quad.comp, both producing identical output with a toggle to alternate between both versions.
Also, as mentioned earlier, I think the sample would be clearer and more useful if we added a SampleGrad to use the derivatives.
asuessenbach
left a comment
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.
Just one minor issue.
Besides that, to see something in the UI, it's not enough to override on_update_ui_overlay.
You have to call draw_ui, which requires some more framework boilerplate code...
Do we have something like a template with a framework-based hello_triangle, or so, that could be used as the base to generate a new sample?
| float ddy = data[idx * 4 + 2]; | ||
| // Store as human-readable line for GUI and also keep LOGI for debug environments | ||
| char buf[160]; | ||
| snprintf(buf, sizeof(buf), "tid=(%u,%u) v=%.6f ddx=%.6f ddy=%.6f", x, y, v, ddx, ddy); |
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.
Maybe use std::format, instead of snprintf into a fixed size buffer.
Yes, we do. The default templates that are used when you create a new sample via our scripts include rendering the UI. That's what new samples should use, as the also include other boilerplate like documentation. |
Extends the compute shader derivatives sample to demonstrate a practical use case: gradient-based edge detection on a procedural radial pattern. Adds a two-stage rendering pipeline where a compute shader calculates spatial derivatives and gradient magnitude, writing to a storage image, then a graphics pipeline displays the result using a fullscreen triangle technique. Includes comprehensive documentation explaining derivative computation, rendering architecture, and the fullscreen triangle optimization. Requires shader storage image read/write features and shader draw parameters extension.
# Conflicts: # samples/extensions/README.adoc # samples/tooling/profiles/vulkan_profiles.hpp
Aligns variable declarations and inline comments to improve code readability. Removes trailing whitespace from empty lines. Human: [Commits history] Update: Support more than one joint in .arm files Also adds names to the joints based on the Blender names. Add support for loading .arm files from Blender/Armory for GPU skinning # Conflicts: # framework/rendering/subpasses/forward_subpass.cpp # framework/scene_graph/components/sub_mesh.cpp Fix compute shader derivatives sample descriptor set update timing Moves descriptor set writes to prepare() to ensure resources are updated before first use, addressing potential rendering issues from accessing uninitialized descriptors. Enhance compute shader derivatives sample with practical visualization Extends the compute shader derivatives sample to demonstrate a practical use case: gradient-based edge detection on a procedural radial pattern. Adds a two-stage rendering pipeline where a compute shader calculates spatial derivatives and gradient magnitude, writing to a storage image, then a graphics pipeline displays the result using a fullscreen triangle technique. Includes comprehensive documentation explaining derivative computation, rendering architecture, and the fullscreen triangle optimization. Requires shader storage image read/write features and shader draw parameters extension. Revert "add shader quad control sample demonstrating VK_KHR_shader_quad_control features." This reverts commit d9524ac. add shader quad control sample demonstrating VK_KHR_shader_quad_control features. Add compute shader derivatives sample for Vulkan extension Introduce a sample demonstrating the `VK_KHR_compute_shader_derivatives` extension. Includes new shader, CMake, and Vulkan application files showcasing the use of derivative instructions in compute shaders with quad-based derivative groups. bash might not work with nmake let's try nmake try to diagnose what's happening. try to diagnose what's happening. astc can't find sys/time.h when using sccache. [Message] [Diff] --- a/samples/extensions/compute_shader_derivatives/compute_shader_derivatives.cpp +++ b/samples/extensions/compute_shader_derivatives/compute_shader_derivatives.cpp @@ -175,9 +175,9 @@ // Require quads derivative group (the sample shader uses layout(derivative_group_quadsNV/derivative_group_quads_khr)) REQUEST_REQUIRED_FEATURE(gpu, VkPhysicalDeviceComputeShaderDerivativesFeaturesKHR, computeDerivativeGroupQuads); // Users may switch to the linear mode by changing the shader qualifier - + // Storage image read/write without format (required for storage images without explicit format qualifiers) - gpu.get_mutable_requested_features().shaderStorageImageReadWithoutFormat = VK_TRUE; + gpu.get_mutable_requested_features().shaderStorageImageReadWithoutFormat = VK_TRUE; gpu.get_mutable_requested_features().shaderStorageImageWriteWithoutFormat = VK_TRUE; } @@ -335,7 +335,7 @@ color_blend_ci.pAttachments = &blend_attachment; // Dynamic state - VkDynamicState dynamic_states[] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR}; + VkDynamicState dynamic_states[] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR}; VkPipelineDynamicStateCreateInfo dynamic_ci{VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO}; dynamic_ci.dynamicStateCount = 2; dynamic_ci.pDynamicStates = dynamic_states; @@ -435,7 +435,7 @@ // Begin render pass to display the computed image and GUI VkClearValue clear_values[2]; - clear_values[0].color = {{0.0f, 0.0f, 0.0f, 1.0f}}; // Clear to black (will be covered by image) + clear_values[0].color = {{0.0f, 0.0f, 0.0f, 1.0f}}; // Clear to black (will be covered by image) clear_values[1].depthStencil = {1.0f, 0}; VkRenderPassBeginInfo render_pass_begin_info = vkb::initializers::render_pass_begin_info(); @@ -464,7 +464,7 @@ // Render the computed image as a fullscreen quad vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, graphics_pipeline); vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, graphics_pipeline_layout, 0, 1, &graphics_descriptor_set, 0, nullptr); - vkCmdDraw(cmd, 3, 1, 0, 0); // Draw fullscreen triangle (3 vertices) + vkCmdDraw(cmd, 3, 1, 0, 0); // Draw fullscreen triangle (3 vertices) // Draw the GUI overlay on top draw_ui(cmd); @@ -501,7 +501,7 @@ drawer.text("- Red/Yellow: Edges (high gradient magnitude)"); drawer.text("- Gradient magnitude = sqrt(dx^2 + dy^2)"); drawer.text(""); - + drawer.text("This demonstrates edge detection using compute shader"); drawer.text("derivatives, useful for LOD selection, filtering, and"); drawer.text("spatial analysis in compute pipelines.");
Updates copyright year ranges across multiple files to include 2026. Also fixes a typo in one header (202 -> 2026) and aligns code formatting for improved readability. Happy New Year!
|
This does now draw something for me, though I'm not currently convinced it's right (which could be an issue in our driver). I'm certainly not seeing any bright red or yellow. Can you share the result you're expecting? |
Description
Introduce a sample demonstrating the
VK_KHR_compute_shader_derivativesextension. Includes new shader, CMake, and Vulkan application files showcasing the use of derivative instructions in compute shaders with quad-based derivative groups.General Checklist:
Please ensure the following points are checked:
Note: The Samples CI runs a number of checks including:
If this PR contains framework changes:
batchcommand line argument to make sure all samples still work properlySample Checklist
If your PR contains a new or modified sample, these further checks must be carried out in addition to the General Checklist: