[SPIR-V] lit computes specular as n_dot_h * m and fails for half

Open
#9,123 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
compilers

Research direction

Start by reproducing with dxc -T cs_6_5 -E main -spirv repro.hlsl using the shader in the issue, and compare the OpFMul output with the DXIL path. The lit lowering for SPIR-V needs to emit a Pow (GLSL.std.450) for the specular term, and the half overload must produce operands whose types match the result type. Done means the SPIR-V validates and lit(0.5, 0.25, 2) yields (1, 0.5, 0.0625, 1).

Written by the indexing model from the issue text.

Description

spirv

lit is wrong when targeting SPIR-V:

  1. The specular term is computed as n_dot_h * m instead of pow(n_dot_h, m). DXIL is correct.
  2. The half overload fails to compile.
Repro

dxc -T cs_6_5 -E main -spirv repro.hlsl

RWStructuredBuffer<float4> Out : register(u0);
StructuredBuffer<float> In : register(t1);

[numthreads(1,1,1)]
void main() {
  Out[0] = lit(In[0], In[1], In[2]);
}

Output contains OpFMul %float %n_dot_h %m instead of a Pow. For example, lit(0.5, 0.25, 2) returns (1, 0.5, 0.5, 1) instead of (1, 0.5, 0.0625, 1).

Replacing float with half and adding -enable-16bit-types fails with:

fatal error: generated SPIR-V is invalid: GLSL.std.450 FMax: expected types of all operands to be equal to Result Type
  %34 = OpExtInst %float %1 FMax %float_0 %29

With constant half arguments (lit((half)0.25, (half)0.5, (half)3.0)), it fails with Internal compiler error: LLVM Assert.

Found by llvm/offload-test-suite#1724.

Dominant language
C++
Stars
3.7k
Forks
926
Avg merge
3d 22h
Merged PRs (30d)
45

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.