transcodeCRNToDXT.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import CompressedTextureBuffer from '../Core/CompressedTextureBuffer.js';
  2. import defined from '../Core/defined.js';
  3. import PixelFormat from '../Core/PixelFormat.js';
  4. import RuntimeError from '../Core/RuntimeError.js';
  5. import crunch from '../ThirdParty/crunch.js';
  6. import createTaskProcessorWorker from './createTaskProcessorWorker.js';
  7. // Modified from texture-tester
  8. // See:
  9. // https://github.com/toji/texture-tester/blob/master/js/webgl-texture-util.js
  10. // http://toji.github.io/texture-tester/
  11. /**
  12. * @license
  13. *
  14. * Copyright (c) 2014, Brandon Jones. All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without modification,
  17. * are permitted provided that the following conditions are met:
  18. *
  19. * * Redistributions of source code must retain the above copyright notice, this
  20. * list of conditions and the following disclaimer.
  21. * * Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  27. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  29. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. // Taken from crnlib.h
  37. var CRN_FORMAT = {
  38. cCRNFmtInvalid: -1,
  39. cCRNFmtDXT1: 0,
  40. // cCRNFmtDXT3 is not currently supported when writing to CRN - only DDS.
  41. cCRNFmtDXT3: 1,
  42. cCRNFmtDXT5: 2
  43. // Crunch supports more formats than this, but we can't use them here.
  44. };
  45. // Mapping of Crunch formats to DXT formats.
  46. var DXT_FORMAT_MAP = {};
  47. DXT_FORMAT_MAP[CRN_FORMAT.cCRNFmtDXT1] = PixelFormat.RGB_DXT1;
  48. DXT_FORMAT_MAP[CRN_FORMAT.cCRNFmtDXT3] = PixelFormat.RGBA_DXT3;
  49. DXT_FORMAT_MAP[CRN_FORMAT.cCRNFmtDXT5] = PixelFormat.RGBA_DXT5;
  50. var dst;
  51. var dxtData;
  52. var cachedDstSize = 0;
  53. // Copy an array of bytes into or out of the emscripten heap.
  54. function arrayBufferCopy(src, dst, dstByteOffset, numBytes) {
  55. var i;
  56. var dst32Offset = dstByteOffset / 4;
  57. var tail = (numBytes % 4);
  58. var src32 = new Uint32Array(src.buffer, 0, (numBytes - tail) / 4);
  59. var dst32 = new Uint32Array(dst.buffer);
  60. for (i = 0; i < src32.length; i++) {
  61. dst32[dst32Offset + i] = src32[i];
  62. }
  63. for (i = numBytes - tail; i < numBytes; i++) {
  64. dst[dstByteOffset + i] = src[i];
  65. }
  66. }
  67. /**
  68. * @private
  69. */
  70. function transcodeCRNToDXT(arrayBuffer, transferableObjects) {
  71. // Copy the contents of the arrayBuffer into emscriptens heap.
  72. var srcSize = arrayBuffer.byteLength;
  73. var bytes = new Uint8Array(arrayBuffer);
  74. var src = crunch._malloc(srcSize);
  75. arrayBufferCopy(bytes, crunch.HEAPU8, src, srcSize);
  76. // Determine what type of compressed data the file contains.
  77. var crnFormat = crunch._crn_get_dxt_format(src, srcSize);
  78. var format = DXT_FORMAT_MAP[crnFormat];
  79. if (!defined(format)) {
  80. throw new RuntimeError('Unsupported compressed format.');
  81. }
  82. // Gather basic metrics about the DXT data.
  83. var levels = crunch._crn_get_levels(src, srcSize);
  84. var width = crunch._crn_get_width(src, srcSize);
  85. var height = crunch._crn_get_height(src, srcSize);
  86. // Determine the size of the decoded DXT data.
  87. var dstSize = 0;
  88. var i;
  89. for (i = 0; i < levels; ++i) {
  90. dstSize += PixelFormat.compressedTextureSizeInBytes(format, width >> i, height >> i);
  91. }
  92. // Allocate enough space on the emscripten heap to hold the decoded DXT data
  93. // or reuse the existing allocation if a previous call to this function has
  94. // already acquired a large enough buffer.
  95. if(cachedDstSize < dstSize) {
  96. if(defined(dst)) {
  97. crunch._free(dst);
  98. }
  99. dst = crunch._malloc(dstSize);
  100. dxtData = new Uint8Array(crunch.HEAPU8.buffer, dst, dstSize);
  101. cachedDstSize = dstSize;
  102. }
  103. // Decompress the DXT data from the Crunch file into the allocated space.
  104. crunch._crn_decompress(src, srcSize, dst, dstSize, 0, levels);
  105. // Release the crunch file data from the emscripten heap.
  106. crunch._free(src);
  107. // Mipmaps are unsupported, so copy the level 0 texture
  108. // When mipmaps are supported, a copy will still be necessary as dxtData is a view on the heap.
  109. var length = PixelFormat.compressedTextureSizeInBytes(format, width, height);
  110. // Get a copy of the 0th mip level. dxtData will exceed length when there are more mip levels.
  111. // Equivalent to dxtData.slice(0, length), which is not supported in IE11
  112. var level0DXTDataView = dxtData.subarray(0, length);
  113. var level0DXTData = new Uint8Array(length);
  114. level0DXTData.set(level0DXTDataView, 0);
  115. transferableObjects.push(level0DXTData.buffer);
  116. return new CompressedTextureBuffer(format, width, height, level0DXTData);
  117. }
  118. export default createTaskProcessorWorker(transcodeCRNToDXT);