DirectXShaderCompiler icon indicating copy to clipboard operation
DirectXShaderCompiler copied to clipboard

Sizeof struct with enum value returns error

Open bfavela opened this issue 2 years ago • 5 comments

Description When using an enumerated value inside of a struct, the compiler is unable to perform a 'sizeof' on that struct member.

Steps to Reproduce Shader provided here (also will paste at the end as it's small): https://godbolt.org/z/PjsePWb6Y

Actual Behavior

:22:19: error: invalid application of 'sizeof' to non-numeric type 'SomeStruct' g_Buffer[0] = sizeof(SomeStruct);

Environment

  • DXC version any
  • Host Operating System any

RWStructuredBuffer g_Buffer : register(u1);

enum SomeEnum { SomeEnum_Val0, SomeEnum_Val1, SomeEnum_Val2, SomeEnum_Val3 };

struct SomeStruct { SomeEnum _enumMember; uint _uintMember; };

[RootSignature("UAV(u1)")] [numthreads(1, 1, 1)] void main(uint3 DTid : SV_DispatchThreadID) { g_Buffer[0] = sizeof(SomeStruct); }

bfavela avatar Aug 15 '23 21:08 bfavela

When adding tests for a fix, it would be great to include a struct containing bitfield enum members as well.

mstrgram avatar Aug 15 '23 22:08 mstrgram

I just hit this as well. It is worth noting that applying sizeof to an enum type directly also results in this error. The struct is not required. https://godbolt.org/z/6chdrWYWz

KStocky avatar Jan 21 '24 14:01 KStocky

If anyone wants a workaround. You can use the fact that this generates a compiler error to construct an is_enum template... https://godbolt.org/z/K5YxsM598... and then go from there...

KStocky avatar Jan 21 '24 15:01 KStocky

Turns out that if the enum appears in a base struct, then sizeof succeeds. I would recommend people start using the template in the following godbolt link (or something similar), instead of the sizeof operator. https://godbolt.org/z/b1e6rnEjh

KStocky avatar May 25 '24 18:05 KStocky