Hi,
I believe I have found a bug in shader compilation.
I am building for WebGL 2.0, ES 3.0. The generated shader code appears to be using a bitfieldExtract function, which is not defined until the 3.1 specification (bitfieldExtract - OpenGL ES 3.1 Reference Pages).
My shader code is using division by powers of two to avoid bitwise operations for this platform, but the compiler is too clever and subs them back in. The code I am using is:
uint vi = (uint)(v * (16.0f * 16.0f));
int ex = (int)(vi / 16 % 16);
int ey = (int)(vi % 16);
float2 e = float2(ex / 15.0f, ey / 15.0f);
return e;
For context, I am trying to pack/unpack more channels into a render target. After using the above function to unpack 2 channels, it appears to produce the following (disassembled) code:
u_xlat0.xy = u_xlat0.xz * vec2(256.0, 256.0);
u_xlatu0.xy = uvec2(u_xlat0.xy);
u_xlatu2.x = bitfieldExtract(u_xlatu0.x, int(4), int(4));
u_xlatu2.y = bitfieldExtract(u_xlatu0.y, int(4), int(4));
u_xlati0.xy = ivec2(uvec2(u_xlatu0.x & uint(15u), u_xlatu0.y & uint(15u)));
u_xlat2.xy = vec2(ivec2(u_xlatu2.xy));
u_xlat2.zw = vec2(u_xlati0.xy);
u_xlat0.x = u_xlat2.w * 0.13333334;
… and this fails to run in the webGL target.
I can work around this by instead using a non-integer number:
uint vi = (uint)(v * (16.0f * 16.0f));
int ex = (int)(vi / 16.0001 % 16.0001);
int ey = (int)(vi % 16);
float2 e = float2(ex / 15.0f, ey / 15.0f);
return e;
Then the compiled code looks like:
u_xlat0.xy = u_xlat0.xz * vec2(256.0, 256.0);
u_xlatu0.xy = uvec2(u_xlat0.xy);
u_xlat2.xy = vec2(u_xlatu0.xy);
u_xlat2.xy = u_xlat2.xy * vec2(0.00390620157, 0.00390620157);
u_xlat2.xy = fract(u_xlat2.xy);
u_xlat2.xy = u_xlat2.xy * vec2(16.0000992, 16.0000992);
u_xlati0.xy = ivec2(uvec2(u_xlatu0.x & uint(15u), u_xlatu0.y & uint(15u)));
u_xlat2.xy = trunc(u_xlat2.xy);
u_xlat2.zw = vec2(u_xlati0.xy);
u_xlat0.x = u_xlat2.w * 0.13333334;
…and it all works fine, but that seems like a bit of a cludge.
Probably worth saying I have the Auto Graphics API option set, and using Core RP 8.2.0, URP 8.2.0.
Many thanks!