rgba-to-float.glsl.ts 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. export default `
  7. // rgbaToFloat adapted from https://github.com/ihmeuw/glsl-rgba-to-float
  8. // BSD 3-Clause License
  9. //
  10. // Copyright (c) 2019, Institute for Health Metrics and Evaluation All rights reserved.
  11. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  12. // - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  13. // - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  14. // - Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  17. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. // IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  19. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  20. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  22. // OF THE POSSIBILITY OF SUCH DAMAGE.
  23. ivec4 floatsToBytes(vec4 inputFloats, bool littleEndian) {
  24. ivec4 bytes = ivec4(inputFloats * 255.0);
  25. return (
  26. littleEndian
  27. ? bytes.abgr
  28. : bytes
  29. );
  30. }
  31. // Break the four bytes down into an array of 32 bits.
  32. void bytesToBits(const in ivec4 bytes, out bool bits[32]) {
  33. for (int channelIndex = 0; channelIndex < 4; ++channelIndex) {
  34. float acc = float(bytes[channelIndex]);
  35. for (int indexInByte = 7; indexInByte >= 0; --indexInByte) {
  36. float powerOfTwo = exp2(float(indexInByte));
  37. bool bit = acc >= powerOfTwo;
  38. bits[channelIndex * 8 + (7 - indexInByte)] = bit;
  39. acc = mod(acc, powerOfTwo);
  40. }
  41. }
  42. }
  43. // Compute the exponent of the 32-bit float.
  44. float getExponent(bool bits[32]) {
  45. const int startIndex = 1;
  46. const int bitStringLength = 8;
  47. const int endBeforeIndex = startIndex + bitStringLength;
  48. float acc = 0.0;
  49. int pow2 = bitStringLength - 1;
  50. for (int bitIndex = startIndex; bitIndex < endBeforeIndex; ++bitIndex) {
  51. acc += float(bits[bitIndex]) * exp2(float(pow2--));
  52. }
  53. return acc;
  54. }
  55. // Compute the mantissa of the 32-bit float.
  56. float getMantissa(bool bits[32], bool subnormal) {
  57. const int startIndex = 9;
  58. const int bitStringLength = 23;
  59. const int endBeforeIndex = startIndex + bitStringLength;
  60. // Leading/implicit/hidden bit convention:
  61. // If the number is not subnormal (with exponent 0), we add a leading 1 digit.
  62. float acc = float(!subnormal) * exp2(float(bitStringLength));
  63. int pow2 = bitStringLength - 1;
  64. for (int bitIndex = startIndex; bitIndex < endBeforeIndex; ++bitIndex) {
  65. acc += float(bits[bitIndex]) * exp2(float(pow2--));
  66. }
  67. return acc;
  68. }
  69. // Parse the float from its 32 bits.
  70. float bitsToFloat(bool bits[32]) {
  71. float signBit = float(bits[0]) * -2.0 + 1.0;
  72. float exponent = getExponent(bits);
  73. bool subnormal = abs(exponent - 0.0) < 0.01;
  74. float mantissa = getMantissa(bits, subnormal);
  75. float exponentBias = 127.0;
  76. return signBit * mantissa * exp2(exponent - exponentBias - 23.0);
  77. }
  78. float rgbaToFloat(vec4 texelRGBA, bool littleEndian) {
  79. ivec4 rgbaBytes = floatsToBytes(texelRGBA, littleEndian);
  80. bool bits[32];
  81. bytesToBits(rgbaBytes, bits);
  82. return bitsToFloat(bits);
  83. }
  84. `;