2021-11-16 16:25:42 +01:00
|
|
|
|
2024-02-24 18:42:17 +01:00
|
|
|
// Compatibility renames. These are exposed with the "godot_" prefix
|
|
|
|
// to work around two distinct Adreno bugs:
|
|
|
|
// 1. Some Adreno devices expose ES310 functions in ES300 shaders.
|
|
|
|
// Internally, we must use the "godot_" prefix, but user shaders
|
|
|
|
// will be mapped automatically.
|
|
|
|
// 2. Adreno 3XX devices have poor implementations of the other packing
|
|
|
|
// functions, so we just use our own everywhere to keep it simple.
|
|
|
|
|
2022-05-10 19:02:44 +02:00
|
|
|
// Floating point pack/unpack functions are part of the GLSL ES 300 specification used by web and mobile.
|
2021-11-16 16:25:42 +01:00
|
|
|
uint float2half(uint f) {
|
2024-09-01 00:10:00 +02:00
|
|
|
uint b = f + uint(0x00001000);
|
|
|
|
uint e = (b & uint(0x7F800000)) >> 23;
|
|
|
|
uint m = b & uint(0x007FFFFF);
|
|
|
|
return (b & uint(0x80000000)) >> uint(16) | uint(e > uint(112)) * ((((e - uint(112)) << uint(10)) & uint(0x7C00)) | m >> uint(13)) | (uint(e < uint(113)) & uint(e > uint(101))) * ((((uint(0x007FF000) + m) >> (uint(125) - e)) + uint(1)) >> uint(1)) | uint(e > uint(143)) * uint(0x7FFF);
|
2021-11-16 16:25:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint half2float(uint h) {
|
2024-09-01 00:10:00 +02:00
|
|
|
uint e = (h & uint(0x7C00)) >> uint(10);
|
|
|
|
uint m = (h & uint(0x03FF)) << uint(13);
|
|
|
|
uint v = m >> uint(23);
|
|
|
|
return (h & uint(0x8000)) << uint(16) | uint(e != uint(0)) * ((e + uint(112)) << uint(23) | m) | (uint(e == uint(0)) & uint(m != uint(0))) * ((v - uint(37)) << uint(23) | ((m << (uint(150) - v)) & uint(0x007FE000)));
|
2021-11-16 16:25:42 +01:00
|
|
|
}
|
|
|
|
|
2024-02-24 18:42:17 +01:00
|
|
|
uint godot_packHalf2x16(vec2 v) {
|
2021-11-16 16:25:42 +01:00
|
|
|
return float2half(floatBitsToUint(v.x)) | float2half(floatBitsToUint(v.y)) << uint(16);
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:42:17 +01:00
|
|
|
vec2 godot_unpackHalf2x16(uint v) {
|
2021-11-16 16:25:42 +01:00
|
|
|
return vec2(uintBitsToFloat(half2float(v & uint(0xffff))),
|
|
|
|
uintBitsToFloat(half2float(v >> uint(16))));
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:42:17 +01:00
|
|
|
uint godot_packUnorm2x16(vec2 v) {
|
2021-11-16 16:25:42 +01:00
|
|
|
uvec2 uv = uvec2(round(clamp(v, vec2(0.0), vec2(1.0)) * 65535.0));
|
|
|
|
return uv.x | uv.y << uint(16);
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:42:17 +01:00
|
|
|
vec2 godot_unpackUnorm2x16(uint p) {
|
2021-11-16 16:25:42 +01:00
|
|
|
return vec2(float(p & uint(0xffff)), float(p >> uint(16))) * 0.000015259021; // 1.0 / 65535.0 optimization
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:42:17 +01:00
|
|
|
uint godot_packSnorm2x16(vec2 v) {
|
2021-11-16 16:25:42 +01:00
|
|
|
uvec2 uv = uvec2(round(clamp(v, vec2(-1.0), vec2(1.0)) * 32767.0) + 32767.0);
|
|
|
|
return uv.x | uv.y << uint(16);
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:42:17 +01:00
|
|
|
vec2 godot_unpackSnorm2x16(uint p) {
|
2021-11-16 16:25:42 +01:00
|
|
|
vec2 v = vec2(float(p & uint(0xffff)), float(p >> uint(16)));
|
|
|
|
return clamp((v - 32767.0) * vec2(0.00003051851), vec2(-1.0), vec2(1.0));
|
|
|
|
}
|
|
|
|
|
2022-12-03 04:36:18 +01:00
|
|
|
uint godot_packUnorm4x8(vec4 v) {
|
2021-11-16 16:25:42 +01:00
|
|
|
uvec4 uv = uvec4(round(clamp(v, vec4(0.0), vec4(1.0)) * 255.0));
|
2022-05-16 20:56:03 +02:00
|
|
|
return uv.x | (uv.y << uint(8)) | (uv.z << uint(16)) | (uv.w << uint(24));
|
2021-11-16 16:25:42 +01:00
|
|
|
}
|
|
|
|
|
2022-12-03 04:36:18 +01:00
|
|
|
vec4 godot_unpackUnorm4x8(uint p) {
|
2022-05-16 20:56:03 +02:00
|
|
|
return vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))) * 0.00392156862; // 1.0 / 255.0
|
2021-11-16 16:25:42 +01:00
|
|
|
}
|
|
|
|
|
2022-12-03 04:36:18 +01:00
|
|
|
uint godot_packSnorm4x8(vec4 v) {
|
2021-11-16 16:25:42 +01:00
|
|
|
uvec4 uv = uvec4(round(clamp(v, vec4(-1.0), vec4(1.0)) * 127.0) + 127.0);
|
|
|
|
return uv.x | uv.y << uint(8) | uv.z << uint(16) | uv.w << uint(24);
|
|
|
|
}
|
|
|
|
|
2022-12-03 04:36:18 +01:00
|
|
|
vec4 godot_unpackSnorm4x8(uint p) {
|
2022-05-16 20:56:03 +02:00
|
|
|
vec4 v = vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24)));
|
2021-11-16 16:25:42 +01:00
|
|
|
return clamp((v - vec4(127.0)) * vec4(0.00787401574), vec4(-1.0), vec4(1.0));
|
|
|
|
}
|
2022-11-12 00:30:06 +01:00
|
|
|
|
2022-12-03 04:36:18 +01:00
|
|
|
#define packUnorm4x8 godot_packUnorm4x8
|
|
|
|
#define unpackUnorm4x8 godot_unpackUnorm4x8
|
|
|
|
#define packSnorm4x8 godot_packSnorm4x8
|
|
|
|
#define unpackSnorm4x8 godot_unpackSnorm4x8
|
2024-02-24 18:42:17 +01:00
|
|
|
#define packHalf2x16 godot_packHalf2x16
|
|
|
|
#define unpackHalf2x16 godot_unpackHalf2x16
|
|
|
|
#define packUnorm2x16 godot_packUnorm2x16
|
|
|
|
#define unpackUnorm2x16 godot_unpackUnorm2x16
|
|
|
|
#define packSnorm2x16 godot_packSnorm2x16
|
|
|
|
#define unpackSnorm2x16 godot_unpackSnorm2x16
|