microsoft / DirectXShaderCompiler

This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Other
3.08k stars 686 forks source link

Validation error caused by GatherCmp/ddx/ddy with wave intrinsics #1614

Closed TheRealMJP closed 5 years ago

TheRealMJP commented 6 years ago

Hey guys,

I was playing around with wave intrinsics in a shader from this project, and I hit a weird case that seems like it might be a bug. Everything was compiling okay initially, but then I made a change where I added a call to WaveActiveBitOr. After adding that, some unrelated code (for sampling shadow maps) started producing the following validation errors:

D:\Dev\DeferredTexturing\SampleFramework12\v1.01\Shaders\Shadows.hlsl:524:26 @[ ./Shading.hlsl:262:41 @[ Mesh.hlsl:187:28 ] ] Gradient operations are not affected by wave-sensitive data or control flow.
D:\Dev\DeferredTexturing\SampleFramework12\v1.01\Shaders\Shadows.hlsl:525:26 @[ ./Shading.hlsl:262:41 @[ Mesh.hlsl:187:28 ] ] Gradient operations are not affected by wave-sensitive data or control flow.
D:\Dev\DeferredTexturing\SampleFramework12\v1.01\Shaders\Shadows.hlsl:246:40 @[ D:\Dev\DeferredTexturing\SampleFramework12\v1.01\Shaders\Shadows.hlsl:501:20 @[ D:\Dev\DeferredTexturing\SampleFramework12\v1.01\Shaders\Shadows.hlsl:527:12 @[ ./Shading.hlsl:262:41 @[ Mesh.hlsl:187:28 ] ] ] ] Gradient operations are not affected by wave-sensitive data or control flow.
D:\Dev\DeferredTexturing\SampleFramework12\v1.01\Shaders\Shadows.hlsl:246:40 @[ D:\Dev\DeferredTexturing\SampleFramework12\v1.01\Shaders\Shadows.hlsl:567:20 @[ 

The first two lines it was complaining about are these ones

float3 shadowPosDX = ddx(shadowPos);
float3 shadowPosDY = ddy(shadowPos);

The third line is this one:

v1[(col + FS_2) / 2] = shadowMap.GatherCmp(shadowSampler, float3(tc.xy, shadowMapIdx), sampleDepth, int2(col, row));

It's a bit strange, since the derivative instructions are in totally code that's totally unrelated to where I put the wave intrinsic, and also isn't used inside of divergent flow control. The GatherCmp one is especially confusing, since as far as I know the Gather functions don't use gradients (they always return the top mip level).

If you guys could help figure out what's going on here, I would greatly appreciate it! I've attached a fully pre-processed version of the shader code that you can compile yourselves: just use a target of ps_6_0 and an entry point of "PSForward".

Apologies in advance if this turns out to be a mistake on my end. :)

-Matt

Mesh_PP.hlsl.txt

python3kgae commented 5 years ago

Fixed with https://github.com/Microsoft/DirectXShaderCompiler/pull/1648

TheRealMJP commented 5 years ago

Thank you for fixing this so quickly! Since this issue was in the validator, does this mean that I'll have to wait until the next Windows release to get an updated version of dxil.dll with this fix?

tex3d commented 5 years ago

Until this change makes it into the DXIL.dll in an SDK build you can flight, you will have to use developer mode in you machine's settings and enable experimental shader models to allow unsigned shaders to be used with your device. To see how to enable experimental shader models, see this:

https://github.com/Microsoft/DirectXShaderCompiler/blob/master/tools/dxexp/dxexp.cpp#L270