SampleCmp with gradient or bias flag is lost when a later sample in the function has none

Open Beginner friendly
#9,135 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
compilers

Research direction

Start with lib/DXIL/DxilShaderFlags.cpp, where the issue identifies the flag assignment, then find the validator code that computes the same flag. Check how the flag is updated across the listed sample opcodes and compare with the expected behavior in DXIL.rst. Done means both flag computation paths retain the flag when SampleCmpBias or SampleCmpGrad appears before another sample instruction, and validation accepts the matching declaration.

Written by the indexing model from the issue text.

Description

bug needs-triage

Description
The SampleCmpGradientOrBias shader flag is not set when SampleCmpBias or SampleCmpGrad is followed in the same function by Sample, SampleBias, SampleGrad, or SampleCmp. It seems the flag depends on the order of the instructions and not on whether SampleCmpBias or SampleCmpGrad itself is used.

Steps to Reproduce

Texture2D<float> depth : register(t0);
SamplerComparisonState less : register(s0);

float4 main(float2 uv : TEXCOORD0) : SV_Target {
	return depth.SampleCmpBias(less, uv, 0.5, 1.0) + depth.SampleCmp(less, uv, 0.25);
}

Compiled with dxc -T ps_6_8 -E main you will find no additional functionality is listed, but with the two calls swapped the listing will contain

; Note: shader requires additional functionality:
;       SampleCmp with gradient or bias

Expected

The flag should be set whenever SampleCmpBias or SampleCmpGrad is used. The documentation in DXIL.rst says the feature should be listed if "Use of the instructions SampleCmpGrad or SampleCmpBias". When the flag is not present the shader is accepted on devices that lack SampleCmpGradientAndBiasSupported.

Cause
It looks like the flag is assigned rather than accumulated in lib/DXIL/DxilShaderFlags.cpp:

case DXIL::OpCode::Sample:
case DXIL::OpCode::SampleBias:
case DXIL::OpCode::SampleCmp:
case DXIL::OpCode::SampleCmpBias:
  ...
  hasSampleCmpGradientOrBias = dxilOp == DXIL::OpCode::SampleCmpBias;

Since SampleGrad and SampleCmpGrad have a case of their own with the same assignment against SampleCmpGrad, whichever of these instructions comes last in the function decides the flag. I suspect this should've been |= rather than =.

The validator also computes the flags the same way so it has the same issue. If you have a a module that declares the flag for the shader above, it too fails validation with "Flags must match usage" error.

Environment

  • DXC 1.9.2609.5
Dominant language
C++
Stars
3.7k
Forks
918
Avg merge
3d 12h
Merged PRs (30d)
41

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/DirectXShaderCompiler

All issues in microsoft/DirectXShaderCompiler

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.