stdlib_inc.glsl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #ifdef USE_GLES_OVER_GL
  2. // Floating point pack/unpack functions are part of the GLSL ES 300 specification used by web and mobile.
  3. uint float2half(uint f) {
  4. uint e = f & uint(0x7f800000);
  5. if (e <= uint(0x38000000)) {
  6. return uint(0);
  7. } else {
  8. return ((f >> uint(16)) & uint(0x8000)) |
  9. (((e - uint(0x38000000)) >> uint(13)) & uint(0x7c00)) |
  10. ((f >> uint(13)) & uint(0x03ff));
  11. }
  12. }
  13. uint half2float(uint h) {
  14. uint h_e = h & uint(0x7c00);
  15. return ((h & uint(0x8000)) << uint(16)) | uint((h_e >> uint(10)) != uint(0)) * (((h_e + uint(0x1c000)) << uint(13)) | ((h & uint(0x03ff)) << uint(13)));
  16. }
  17. uint packHalf2x16(vec2 v) {
  18. return float2half(floatBitsToUint(v.x)) | float2half(floatBitsToUint(v.y)) << uint(16);
  19. }
  20. vec2 unpackHalf2x16(uint v) {
  21. return vec2(uintBitsToFloat(half2float(v & uint(0xffff))),
  22. uintBitsToFloat(half2float(v >> uint(16))));
  23. }
  24. uint packUnorm2x16(vec2 v) {
  25. uvec2 uv = uvec2(round(clamp(v, vec2(0.0), vec2(1.0)) * 65535.0));
  26. return uv.x | uv.y << uint(16);
  27. }
  28. vec2 unpackUnorm2x16(uint p) {
  29. return vec2(float(p & uint(0xffff)), float(p >> uint(16))) * 0.000015259021; // 1.0 / 65535.0 optimization
  30. }
  31. uint packSnorm2x16(vec2 v) {
  32. uvec2 uv = uvec2(round(clamp(v, vec2(-1.0), vec2(1.0)) * 32767.0) + 32767.0);
  33. return uv.x | uv.y << uint(16);
  34. }
  35. vec2 unpackSnorm2x16(uint p) {
  36. vec2 v = vec2(float(p & uint(0xffff)), float(p >> uint(16)));
  37. return clamp((v - 32767.0) * vec2(0.00003051851), vec2(-1.0), vec2(1.0));
  38. }
  39. #endif
  40. // Compatibility renames. These are exposed with the "godot_" prefix
  41. // to work around an Adreno bug which was exposing these ES310 functions
  42. // in ES300 shaders. Internally, we must use the "godot_" prefix, but user shaders
  43. // will be mapped automatically.
  44. uint godot_packUnorm4x8(vec4 v) {
  45. uvec4 uv = uvec4(round(clamp(v, vec4(0.0), vec4(1.0)) * 255.0));
  46. return uv.x | (uv.y << uint(8)) | (uv.z << uint(16)) | (uv.w << uint(24));
  47. }
  48. vec4 godot_unpackUnorm4x8(uint p) {
  49. 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
  50. }
  51. uint godot_packSnorm4x8(vec4 v) {
  52. uvec4 uv = uvec4(round(clamp(v, vec4(-1.0), vec4(1.0)) * 127.0) + 127.0);
  53. return uv.x | uv.y << uint(8) | uv.z << uint(16) | uv.w << uint(24);
  54. }
  55. vec4 godot_unpackSnorm4x8(uint p) {
  56. vec4 v = vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24)));
  57. return clamp((v - vec4(127.0)) * vec4(0.00787401574), vec4(-1.0), vec4(1.0));
  58. }
  59. #define packUnorm4x8 godot_packUnorm4x8
  60. #define unpackUnorm4x8 godot_unpackUnorm4x8
  61. #define packSnorm4x8 godot_packSnorm4x8
  62. #define unpackSnorm4x8 godot_unpackSnorm4x8