All files / engine/Source/Core loadKTX2.js

100% Statements 9/9
100% Branches 4/4
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                  1x               745x                                                                                                                   5x       4x       3x   1x 1x       4x 3x          
import Check from "./Check.js";
import Resource from "./Resource.js";
import KTX2Transcoder from "./KTX2Transcoder.js";
 
/**
 * Stores the supported formats that KTX2 can transcode to. Called during context creation.
 *
 * @param {boolean} s3tc Whether or not S3TC is supported
 * @param {boolean} pvrtc Whether or not PVRTC is supported
 * @param {boolean} astc Whether or not ASTC is supported
 * @param {boolean} etc Whether or not ETC is supported
 * @param {boolean} etc1 Whether or not ETC1 is supported
 * @param {boolean} bc7 Whether or not BC7 is supported
 * @private
 */
let supportedTranscoderFormats;
 
loadKTX2.setKTX2SupportedFormats = function (
  s3tc,
  pvrtc,
  astc,
  etc,
  etc1,
  bc7,
) {
  supportedTranscoderFormats = {
    s3tc: s3tc,
    pvrtc: pvrtc,
    astc: astc,
    etc: etc,
    etc1: etc1,
    bc7: bc7,
  };
};
 
/**
 * Asynchronously loads and parses the given URL to a KTX2 file or parses the raw binary data of a KTX2 file.
 * Returns a promise that will resolve to an object containing the image buffer, width, height, and format once loaded,
 * or reject if the URL failed to load or failed to parse the data. The data is loaded
 * using XMLHttpRequest, which means that in order to make requests to another origin,
 * the server must have Cross-Origin Resource sharing (CORS) headers enabled.
 * <p>
 * The following are part of the KTX2 format specification but are not supported:
 * <ul>
 *     <li>Metadata</li>
 *     <li>3D textures</li>
 *     <li>Texture Arrays</li>
 *     <li>Video</li>
 * </ul>
 * </p>
 *
 * @function loadKTX2
 *
 * @param {Resource|string|ArrayBuffer} resourceOrUrlOrBuffer The URL of the binary data or an ArrayBuffer.
 * @returns {Promise<CompressedTextureBuffer>|undefined} A promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
 *
 * @exception {RuntimeError} Invalid KTX2 file.
 * @exception {RuntimeError} KTX2 texture arrays are not supported.
 * @exception {RuntimeError} KTX2 3D textures are unsupported.
 * @exception {RuntimeError} No transcoding format target available for ETC1S compressed ktx2s.
 * @exception {RuntimeError} No transcoding format target available for UASTC compressed ktx2s.
 * @exception {RuntimeError} startTranscoding() failed.
 * @exception {RuntimeError} transcodeImage() failed.
 *
 * @example
 * // load a single URL asynchronously
 * Cesium.loadKTX2('some/url').then(function (ktx2Data) {
 *     const width = ktx2Data.width;
 *     const height = ktx2Data.height;
 *     const format = ktx2Data.internalFormat;
 *     const arrayBufferView = ktx2Data.bufferView;
 *     // use the data to create a texture
 * }).catch(function (error) {
 *     // an error occurred.
 * });
 *
 * @see {@link https://github.com/KhronosGroup/KTX-Specification|KTX file format}
 * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
 * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
 * @private
 */
function loadKTX2(resourceOrUrlOrBuffer) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("resourceOrUrlOrBuffer", resourceOrUrlOrBuffer);
  //>>includeEnd('debug');
 
  let loadPromise;
  if (
    resourceOrUrlOrBuffer instanceof ArrayBuffer ||
    ArrayBuffer.isView(resourceOrUrlOrBuffer)
  ) {
    loadPromise = Promise.resolve(resourceOrUrlOrBuffer);
  } else {
    const resource = Resource.createIfNeeded(resourceOrUrlOrBuffer);
    loadPromise = resource.fetchArrayBuffer();
  }
 
  // load module then return
  return loadPromise.then(function (data) {
    return KTX2Transcoder.transcode(data, supportedTranscoderFormats);
  });
}
 
export default loadKTX2;