stdlib_inc.glsl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Compatibility renames. These are exposed with the "godot_" prefix
  2. // to work around two distinct Adreno bugs:
  3. // 1. Some Adreno devices expose ES310 functions in ES300 shaders.
  4. // Internally, we must use the "godot_" prefix, but user shaders
  5. // will be mapped automatically.
  6. // 2. Adreno 3XX devices have poor implementations of the other packing
  7. // functions, so we just use our own there to keep it simple.
  8. #ifdef USE_HALF2FLOAT
  9. // Floating point pack/unpack functions are part of the GLSL ES 300 specification used by web and mobile.
  10. // It appears to be safe to expose these on mobile, but when running through ANGLE this appears to break.
  11. uint float2half(uint f) {
  12. uint e = f & uint(0x7f800000);
  13. if (e <= uint(0x38000000)) {
  14. return uint(0);
  15. } else {
  16. return ((f >> uint(16)) & uint(0x8000)) |
  17. (((e - uint(0x38000000)) >> uint(13)) & uint(0x7c00)) |
  18. ((f >> uint(13)) & uint(0x03ff));
  19. }
  20. }
  21. uint half2float(uint h) {
  22. uint h_e = h & uint(0x7c00);
  23. return ((h & uint(0x8000)) << uint(16)) | uint((h_e >> uint(10)) != uint(0)) * (((h_e + uint(0x1c000)) << uint(13)) | ((h & uint(0x03ff)) << uint(13)));
  24. }
  25. uint godot_packHalf2x16(vec2 v) {
  26. return float2half(floatBitsToUint(v.x)) | float2half(floatBitsToUint(v.y)) << uint(16);
  27. }
  28. vec2 godot_unpackHalf2x16(uint v) {
  29. return vec2(uintBitsToFloat(half2float(v & uint(0xffff))),
  30. uintBitsToFloat(half2float(v >> uint(16))));
  31. }
  32. uint godot_packUnorm2x16(vec2 v) {
  33. uvec2 uv = uvec2(round(clamp(v, vec2(0.0), vec2(1.0)) * 65535.0));
  34. return uv.x | uv.y << uint(16);
  35. }
  36. vec2 godot_unpackUnorm2x16(uint p) {
  37. return vec2(float(p & uint(0xffff)), float(p >> uint(16))) * 0.000015259021; // 1.0 / 65535.0 optimization
  38. }
  39. uint godot_packSnorm2x16(vec2 v) {
  40. uvec2 uv = uvec2(round(clamp(v, vec2(-1.0), vec2(1.0)) * 32767.0) + 32767.0);
  41. return uv.x | uv.y << uint(16);
  42. }
  43. vec2 godot_unpackSnorm2x16(uint p) {
  44. vec2 v = vec2(float(p & uint(0xffff)), float(p >> uint(16)));
  45. return clamp((v - 32767.0) * vec2(0.00003051851), vec2(-1.0), vec2(1.0));
  46. }
  47. #define packHalf2x16 godot_packHalf2x16
  48. #define unpackHalf2x16 godot_unpackHalf2x16
  49. #define packUnorm2x16 godot_packUnorm2x16
  50. #define unpackUnorm2x16 godot_unpackUnorm2x16
  51. #define packSnorm2x16 godot_packSnorm2x16
  52. #define unpackSnorm2x16 godot_unpackSnorm2x16
  53. #endif // USE_HALF2FLOAT
  54. // Always expose these as they are ES310 functions and not available in ES300 or GLSL 330.
  55. uint godot_packUnorm4x8(vec4 v) {
  56. uvec4 uv = uvec4(round(clamp(v, vec4(0.0), vec4(1.0)) * 255.0));
  57. return uv.x | (uv.y << uint(8)) | (uv.z << uint(16)) | (uv.w << uint(24));
  58. }
  59. vec4 godot_unpackUnorm4x8(uint p) {
  60. 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
  61. }
  62. uint godot_packSnorm4x8(vec4 v) {
  63. uvec4 uv = uvec4(round(clamp(v, vec4(-1.0), vec4(1.0)) * 127.0) + 127.0);
  64. return uv.x | uv.y << uint(8) | uv.z << uint(16) | uv.w << uint(24);
  65. }
  66. vec4 godot_unpackSnorm4x8(uint p) {
  67. vec4 v = vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24)));
  68. return clamp((v - vec4(127.0)) * vec4(0.00787401574), vec4(-1.0), vec4(1.0));
  69. }
  70. #define packUnorm4x8 godot_packUnorm4x8
  71. #define unpackUnorm4x8 godot_unpackUnorm4x8
  72. #define packSnorm4x8 godot_packSnorm4x8
  73. #define unpackSnorm4x8 godot_unpackSnorm4x8