Skip to content

Commit d8aad78

Browse files
authored
Add support for KHR_compute_shader_derivatives (#7249)
Add support for KHR_compute_shader_derivatives - DirectxShaderCompiler already supports `NV_compute_shader_derivatives` which is functionality identical to `KHR_compute_shader_derivatives` - The KHR extension will be used by default instead of the NV one following the same approach as the RT extension. - We currently explain this in a comment in `tools/clang/lib/SPIRV/FeatureManager.cpp` `FeatureManager::enabledByDefault`. - Check commit introducing RT for more info 04a84f0 Fixes #7179
1 parent 1eb83c7 commit d8aad78

File tree

7 files changed

+72
-0
lines changed

7 files changed

+72
-0
lines changed

docs/SPIR-V.rst

+1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ Supported extensions
315315
* SPV_KHR_fragment_shader_barycentric
316316
* SPV_KHR_physical_storage_buffer
317317
* SPV_KHR_vulkan_memory_model
318+
* SPV_KHR_compute_shader_derivatives
318319
* SPV_NV_compute_shader_derivatives
319320
* SPV_KHR_maximal_reconvergence
320321
* SPV_KHR_float_controls

tools/clang/include/clang/SPIRV/FeatureManager.h

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ enum class Extension {
5959
KHR_physical_storage_buffer,
6060
KHR_vulkan_memory_model,
6161
NV_compute_shader_derivatives,
62+
KHR_compute_shader_derivatives,
6263
KHR_fragment_shader_barycentric,
6364
KHR_maximal_reconvergence,
6465
KHR_float_controls,

tools/clang/lib/SPIRV/CapabilityVisitor.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,12 @@ bool CapabilityVisitor::visit(SpirvModule *, Visitor::Phase phase) {
852852
spv::Capability::FragmentShaderShadingRateInterlockEXT,
853853
});
854854

855+
addExtensionAndCapabilitiesIfEnabled(
856+
Extension::KHR_compute_shader_derivatives,
857+
{
858+
spv::Capability::ComputeDerivativeGroupQuadsKHR,
859+
spv::Capability::ComputeDerivativeGroupLinearKHR,
860+
});
855861
addExtensionAndCapabilitiesIfEnabled(
856862
Extension::NV_compute_shader_derivatives,
857863
{

tools/clang/lib/SPIRV/FeatureManager.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ Extension FeatureManager::getExtensionSymbol(llvm::StringRef name) {
215215
.Case("SPV_KHR_physical_storage_buffer",
216216
Extension::KHR_physical_storage_buffer)
217217
.Case("SPV_KHR_vulkan_memory_model", Extension::KHR_vulkan_memory_model)
218+
.Case("SPV_KHR_compute_shader_derivatives",
219+
Extension::KHR_compute_shader_derivatives)
218220
.Case("SPV_NV_compute_shader_derivatives",
219221
Extension::NV_compute_shader_derivatives)
220222
.Case("SPV_KHR_fragment_shader_barycentric",
@@ -283,6 +285,8 @@ const char *FeatureManager::getExtensionName(Extension symbol) {
283285
return "SPV_KHR_physical_storage_buffer";
284286
case Extension::KHR_vulkan_memory_model:
285287
return "SPV_KHR_vulkan_memory_model";
288+
case Extension::KHR_compute_shader_derivatives:
289+
return "SPV_KHR_compute_shader_derivatives";
286290
case Extension::NV_compute_shader_derivatives:
287291
return "SPV_NV_compute_shader_derivatives";
288292
case Extension::KHR_fragment_shader_barycentric:
@@ -370,6 +374,10 @@ bool FeatureManager::enabledByDefault(Extension ext) {
370374
// KHR_ray_tracing and NV_ray_tracing are mutually exclusive so enable only
371375
// KHR extension by default
372376
case Extension::NV_ray_tracing:
377+
return false;
378+
// KHR_compute_shader_derivatives and NV_compute_shader_derivatives are
379+
// mutually exclusive so enable only KHR extension by default
380+
case Extension::NV_compute_shader_derivatives:
373381
return false;
374382
// Enabling EXT_demote_to_helper_invocation changes the code generation
375383
// behavior for the 'discard' statement. Therefore we will only enable it if

tools/clang/lib/SPIRV/SpirvEmitter.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -15049,6 +15049,10 @@ void SpirvEmitter::addDerivativeGroupExecutionMode() {
1504915049
// to 2D quad rules. Using derivative operations in any numthreads
1505015050
// configuration not matching either of these is invalid and will produce an
1505115051
// error.
15052+
static_assert(spv::ExecutionMode::DerivativeGroupQuadsNV ==
15053+
spv::ExecutionMode::DerivativeGroupQuadsKHR);
15054+
static_assert(spv::ExecutionMode::DerivativeGroupLinearNV ==
15055+
spv::ExecutionMode::DerivativeGroupLinearKHR);
1505215056
spv::ExecutionMode em = spv::ExecutionMode::DerivativeGroupQuadsNV;
1505315057
if (numThreads[0] % 4 == 0 && numThreads[1] == 1 && numThreads[2] == 1) {
1505415058
em = spv::ExecutionMode::DerivativeGroupLinearNV;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %dxc -T cs_6_6 -E main -fspv-extension=SPV_KHR_compute_shader_derivatives -fcgl %s -spirv 2>&1 | FileCheck %s
2+
3+
// CHECK: OpCapability ComputeDerivativeGroupQuadsKHR
4+
// CHECK: OpExtension "SPV_KHR_compute_shader_derivatives"
5+
// CHECK: OpExecutionMode %main DerivativeGroupQuadsKHR
6+
7+
8+
SamplerState ss : register(s2);
9+
SamplerComparisonState scs;
10+
11+
RWStructuredBuffer<uint> o;
12+
Texture1D <float> t1;
13+
14+
[numthreads(2,2,1)]
15+
void main(uint3 id : SV_GroupThreadID)
16+
{
17+
// CHECK: OpDPdx %float %float_0_5
18+
o[0] = ddx(0.5);
19+
// CHECK: OpDPdxCoarse %float %float_0_5
20+
o[1] = ddx_coarse(0.5);
21+
// CHECK: OpDPdy %float %float_0_5
22+
o[2] = ddy(0.5);
23+
// CHECK: OpDPdyCoarse %float %float_0_5
24+
o[3] = ddy_coarse(0.5);
25+
// CHECK: OpDPdxFine %float %float_0_5
26+
o[4] = ddx_fine(0.5);
27+
// CHECK: OpDPdyFine %float %float_0_5
28+
o[5] = ddy_fine(0.5);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %dxc -T cs_6_6 -E main -fspv-extension=SPV_KHR_compute_shader_derivatives -fcgl %s -spirv 2>&1 | FileCheck %s --check-prefix=CHECK
2+
// RUN: %dxc -T cs_6_6 -E main -fspv-extension=SPV_KHR_compute_shader_derivatives %s -spirv 2>&1 | FileCheck %s --check-prefix=CHECK
3+
4+
// CHECK: OpCapability ComputeDerivativeGroupLinearKHR
5+
// CHECK: OpExtension "SPV_KHR_compute_shader_derivatives"
6+
// CHECK: OpExecutionMode %main DerivativeGroupLinearKHR
7+
8+
SamplerState ss : register(s2);
9+
SamplerComparisonState scs;
10+
11+
RWStructuredBuffer<uint> o;
12+
Texture1D <float> t1;
13+
14+
[numthreads(16,1,1)]
15+
void main(uint3 id : SV_GroupThreadID)
16+
{
17+
//CHECK: [[t1:%[0-9]+]] = OpLoad %type_1d_image %t1
18+
//CHECK-NEXT: [[ss1:%[0-9]+]] = OpLoad %type_sampler %ss
19+
//CHECK-NEXT: [[si1:%[0-9]+]] = OpSampledImage %type_sampled_image [[t1]] [[ss1]]
20+
//CHECK-NEXT: [[query1:%[0-9]+]] = OpImageQueryLod %v2float [[si1]] %float_0_5
21+
//CHECK-NEXT: {{%[0-9]+}} = OpCompositeExtract %float [[query1]] 0
22+
o[0] = t1.CalculateLevelOfDetail(ss, 0.5);
23+
}

0 commit comments

Comments
 (0)