microsoft / hlsl-specs

HLSL Specifications
MIT License
116 stars 29 forks source link

Various intrinsic functions do not produce integral constant results when fed integral constant expressions (HLSL 2021) #239

Open jeremyong opened 3 months ago

jeremyong commented 3 months ago

Description For NTTP manipulation, it's helpful to use built-in intrinsics for decoding/encoding information (e.g. countbits, firstbitlow, etc.). However, attempt to use these intrinsics in NTTP expressions results in a compiler error.

Steps to Reproduce Minimal example (CE):

struct PSInput
{
    float4 position : SV_Position;
    float4 color    : COLOR0;
};

template <int I>
struct Foo
{
    vector<float, countbits(I)> test;
};

float4 PSMain(PSInput input) : SV_Target0
{
    Foo<3> f;
    return input.color * input.color;

Actual Behavior

<source>:13:19: error: non-type template argument of type 'unsigned int' is not an integral constant expression
    vector<float, countbits(I)> test;
                  ^~~~~~~~~~~~
<source>:18:12: note: in instantiation of template class 'Foo<3>' requested here
    Foo<3> f;
           ^

Compiler returned: 5

Environment

devshgraphicsprogramming commented 3 months ago

Yeah this is basically lack of constexpr concept, I've come up against this before and had to make boost mpl style template abominations to do a compile time bit count etc.