microsoft / microsoft/DirectXShaderCompiler

Ternary on a non-scalar loaded from a constant buffer

Open
#6,801 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug dxil shader-linking
Dominant language
C++
Stars
3.7k
Forks
900
Avg merge
2d 11h
Merged PRs (30d)
44

Description

Description

Vague (and different than normal) error about non-scalar values in ternaries when those non-scalar values (say for example, a struct) comes from a constant buffer.

Steps to Reproduce

https://godbolt.org/z/qoed1s8f4

Expected Behavior
(produced with a RWByteAddressBuffer, not a ConstantBuffer)

<source>:##:##: error: conditional operator only supports results with numeric scalar, vector, or matrix types.
    value = decider ? inBuffer.Load<MyStruct>(globalId) : inBuffer.Load<MyStruct>(globalId*2);

Actual Behavior

The ternary version compiles to dxil if validation is disabled (unconfirmed if it functions properly as the system it's in is complicated to test), but fails validation, and then also fails to compile to spirv.

The DXIL Validation error:

error: validation errors
Function: dx.hl.cast.handleToRes.%"struct.ConstantBuffer<MyStruct>" (i32, %dx.types.Handle): error: External function 'dx.hl.cast.handleToRes.%"struct.ConstantBuffer<MyStruct>" (i32, %dx.types.Handle)' is not a DXIL function.
Function: dx.hl.cast.handleToRes.%"struct.ConstantBuffer<MyStruct>" (i32, %dx.types.Handle): error: Function 'dx.hl.cast.handleToRes.%"struct.ConstantBuffer<MyStruct>" (i32, %dx.types.Handle)' uses resource in function signature.
<source>:39:13: error: Resource handle should returned by createHandle.
note: at '%6 = call %dx.types.Handle @"dx.op.createHandleForLib.struct.ConstantBuffer<MyStruct>"(i32 160, %"struct.ConstantBuffer<MyStruct>" %5)' in block '#0' of function 'main'.
<source>:39:13: error: Expect Cbuffer for CBufferLoad handle.
note: at '%6 = call %dx.types.Handle @"dx.op.createHandleForLib.struct.ConstantBuffer<MyStruct>"(i32 160, %"struct.ConstantBuffer<MyStruct>" %5)' in block '#0' of function 'main'.
<source>:39:13: error: opcode 'CreateHandleForLib' should only be used in 'Library'.
note: at '%6 = call %dx.types.Handle @"dx.op.createHandleForLib.struct.ConstantBuffer<MyStruct>"(i32 160, %"struct.ConstantBuffer<MyStruct>" %5)' in block '#0' of function 'main'.
Validation failed.

The SPIR-V error:

error: casting to type 'ConstantBuffer<MyStruct>' unimplemented

Environment

  • DXC version 1.8.2403.37 (march 2024 patch 2)
  • Host Operating System Windows 22H2 (Build 19045.4412)

Contributor guide

Open the contributing guide

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.

Research direction

Start with the linked Godbolt reproduction and compare the ConstantBuffer case with the RWByteAddressBuffer case across the DXIL and SPIR-V compiler paths. Trace how the ternary expression and non-scalar loaded values are lowered, then verify that the result produces a valid diagnostic or valid output without the reported validation and casting errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.