Skip to content

Conversation

@gpx1000
Copy link
Collaborator

@gpx1000 gpx1000 commented Oct 17, 2025

Description

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.

General Checklist:

Please ensure the following points are checked:

  • My code follows the coding style
  • I have reviewed file licenses
  • I have commented any added functions (in line with Doxygen)
  • I have commented any code that could be hard to understand
  • My changes do not add any new compiler warnings
  • My changes do not add any new validation layer errors or warnings
  • I have used existing framework/helper functions where possible
  • My changes do not add any regressions
  • I have tested every sample to ensure everything runs correctly
  • This PR describes the scope and expected impact of the changes I am making

Note: The Samples CI runs a number of checks including:

  • I have updated the header Copyright to reflect the current year (CI build will fail if Copyright is out of date)
  • My changes build on Windows, Linux, macOS and Android. Otherwise I have documented any exceptions

If this PR contains framework changes:

  • I did a full batch run using the batch command line argument to make sure all samples still work properly

Sample Checklist

If your PR contains a new or modified sample, these further checks must be carried out in addition to the General Checklist:

  • I have tested the sample on at least one compliant Vulkan implementation
  • If the sample is vendor-specific, I have tagged it appropriately
  • I have stated on what implementation the sample has been tested so that others can test on different implementations and platforms
  • Any dependent assets have been merged and published in downstream modules
  • For new samples, I have added a paragraph with a summary to the appropriate chapter in the readme of the folder that the sample belongs to e.g. api samples readme
  • For new samples, I have added a tutorial README.md file to guide users through what they need to know to implement code using this feature. For example, see conditional_rendering
  • For new samples, I have added a link to the Antora navigation so that the sample will be listed at the Vulkan documentation site

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);
Copy link
Collaborator

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
@gary-sweet
Copy link
Contributor

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:

[info] compute-derivatives CPU: tid=(0,0) v=0.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(1,0) v=1.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(2,0) v=2.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(3,0) v=3.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(0,1) v=4.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(1,1) v=5.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(2,1) v=6.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(3,1) v=7.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(0,2) v=8.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(1,2) v=9.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(2,2) v=10.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(3,2) v=11.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(0,3) v=12.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(1,3) v=13.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(2,3) v=14.000000 ddx=1.000000 ddy=4.000000
[info] compute-derivatives CPU: tid=(3,3) v=15.000000 ddx=1.000000 ddy=4.000000

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
Copy link
Contributor

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);
Copy link
Contributor

@iagoCL iagoCL Oct 22, 2025

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.

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.

4 participants