khronosTextureContainer.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* eslint-disable @typescript-eslint/naming-convention */
  2. import { Logger } from "../Misc/logger.js";
  3. /**
  4. * for description see https://www.khronos.org/opengles/sdk/tools/KTX/
  5. * for file layout see https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/
  6. */
  7. export class KhronosTextureContainer {
  8. /**
  9. * Creates a new KhronosTextureContainer
  10. * @param data contents of the KTX container file
  11. * @param facesExpected should be either 1 or 6, based whether a cube texture or or
  12. */
  13. constructor(
  14. /** contents of the KTX container file */
  15. data, facesExpected) {
  16. this.data = data;
  17. /**
  18. * If the container has been made invalid (eg. constructor failed to correctly load array buffer)
  19. */
  20. this.isInvalid = false;
  21. if (!KhronosTextureContainer.IsValid(data)) {
  22. this.isInvalid = true;
  23. Logger.Error("texture missing KTX identifier");
  24. return;
  25. }
  26. // load the reset of the header in native 32 bit uint
  27. const dataSize = Uint32Array.BYTES_PER_ELEMENT;
  28. const headerDataView = new DataView(this.data.buffer, this.data.byteOffset + 12, 13 * dataSize);
  29. const endianness = headerDataView.getUint32(0, true);
  30. const littleEndian = endianness === 0x04030201;
  31. this.glType = headerDataView.getUint32(1 * dataSize, littleEndian); // must be 0 for compressed textures
  32. this.glTypeSize = headerDataView.getUint32(2 * dataSize, littleEndian); // must be 1 for compressed textures
  33. this.glFormat = headerDataView.getUint32(3 * dataSize, littleEndian); // must be 0 for compressed textures
  34. this.glInternalFormat = headerDataView.getUint32(4 * dataSize, littleEndian); // the value of arg passed to gl.compressedTexImage2D(,,x,,,,)
  35. this.glBaseInternalFormat = headerDataView.getUint32(5 * dataSize, littleEndian); // specify GL_RGB, GL_RGBA, GL_ALPHA, etc (un-compressed only)
  36. this.pixelWidth = headerDataView.getUint32(6 * dataSize, littleEndian); // level 0 value of arg passed to gl.compressedTexImage2D(,,,x,,,)
  37. this.pixelHeight = headerDataView.getUint32(7 * dataSize, littleEndian); // level 0 value of arg passed to gl.compressedTexImage2D(,,,,x,,)
  38. this.pixelDepth = headerDataView.getUint32(8 * dataSize, littleEndian); // level 0 value of arg passed to gl.compressedTexImage3D(,,,,,x,,)
  39. this.numberOfArrayElements = headerDataView.getUint32(9 * dataSize, littleEndian); // used for texture arrays
  40. this.numberOfFaces = headerDataView.getUint32(10 * dataSize, littleEndian); // used for cubemap textures, should either be 1 or 6
  41. this.numberOfMipmapLevels = headerDataView.getUint32(11 * dataSize, littleEndian); // number of levels; disregard possibility of 0 for compressed textures
  42. this.bytesOfKeyValueData = headerDataView.getUint32(12 * dataSize, littleEndian); // the amount of space after the header for meta-data
  43. // Make sure we have a compressed type. Not only reduces work, but probably better to let dev know they are not compressing.
  44. if (this.glType !== 0) {
  45. Logger.Error("only compressed formats currently supported");
  46. this.isInvalid = true;
  47. return;
  48. }
  49. else {
  50. // value of zero is an indication to generate mipmaps @ runtime. Not usually allowed for compressed, so disregard.
  51. this.numberOfMipmapLevels = Math.max(1, this.numberOfMipmapLevels);
  52. }
  53. if (this.pixelHeight === 0 || this.pixelDepth !== 0) {
  54. Logger.Error("only 2D textures currently supported");
  55. this.isInvalid = true;
  56. return;
  57. }
  58. if (this.numberOfArrayElements !== 0) {
  59. Logger.Error("texture arrays not currently supported");
  60. this.isInvalid = true;
  61. return;
  62. }
  63. if (this.numberOfFaces !== facesExpected) {
  64. Logger.Error("number of faces expected" + facesExpected + ", but found " + this.numberOfFaces);
  65. this.isInvalid = true;
  66. return;
  67. }
  68. // we now have a completely validated file, so could use existence of loadType as success
  69. // would need to make this more elaborate & adjust checks above to support more than one load type
  70. this.loadType = KhronosTextureContainer.COMPRESSED_2D;
  71. }
  72. /**
  73. * Uploads KTX content to a Babylon Texture.
  74. * It is assumed that the texture has already been created & is currently bound
  75. * @internal
  76. */
  77. uploadLevels(texture, loadMipmaps) {
  78. switch (this.loadType) {
  79. case KhronosTextureContainer.COMPRESSED_2D:
  80. this._upload2DCompressedLevels(texture, loadMipmaps);
  81. break;
  82. case KhronosTextureContainer.TEX_2D:
  83. case KhronosTextureContainer.COMPRESSED_3D:
  84. case KhronosTextureContainer.TEX_3D:
  85. }
  86. }
  87. _upload2DCompressedLevels(texture, loadMipmaps) {
  88. // initialize width & height for level 1
  89. let dataOffset = KhronosTextureContainer.HEADER_LEN + this.bytesOfKeyValueData;
  90. let width = this.pixelWidth;
  91. let height = this.pixelHeight;
  92. const mipmapCount = loadMipmaps ? this.numberOfMipmapLevels : 1;
  93. for (let level = 0; level < mipmapCount; level++) {
  94. const imageSize = new Int32Array(this.data.buffer, this.data.byteOffset + dataOffset, 1)[0]; // size per face, since not supporting array cubemaps
  95. dataOffset += 4; //image data starts from next multiple of 4 offset. Each face refers to same imagesize field above.
  96. for (let face = 0; face < this.numberOfFaces; face++) {
  97. const byteArray = new Uint8Array(this.data.buffer, this.data.byteOffset + dataOffset, imageSize);
  98. const engine = texture.getEngine();
  99. engine._uploadCompressedDataToTextureDirectly(texture, texture.format, width, height, byteArray, face, level);
  100. dataOffset += imageSize; // add size of the image for the next face/mipmap
  101. dataOffset += 3 - ((imageSize + 3) % 4); // add padding for odd sized image
  102. }
  103. width = Math.max(1.0, width * 0.5);
  104. height = Math.max(1.0, height * 0.5);
  105. }
  106. }
  107. /**
  108. * Checks if the given data starts with a KTX file identifier.
  109. * @param data the data to check
  110. * @returns true if the data is a KTX file or false otherwise
  111. */
  112. static IsValid(data) {
  113. if (data.byteLength >= 12) {
  114. // '«', 'K', 'T', 'X', ' ', '1', '1', '»', '\r', '\n', '\x1A', '\n'
  115. const identifier = new Uint8Array(data.buffer, data.byteOffset, 12);
  116. if (identifier[0] === 0xab &&
  117. identifier[1] === 0x4b &&
  118. identifier[2] === 0x54 &&
  119. identifier[3] === 0x58 &&
  120. identifier[4] === 0x20 &&
  121. identifier[5] === 0x31 &&
  122. identifier[6] === 0x31 &&
  123. identifier[7] === 0xbb &&
  124. identifier[8] === 0x0d &&
  125. identifier[9] === 0x0a &&
  126. identifier[10] === 0x1a &&
  127. identifier[11] === 0x0a) {
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. }
  134. KhronosTextureContainer.HEADER_LEN = 12 + 13 * 4; // identifier + header elements (not including key value meta-data pairs)
  135. // load types
  136. KhronosTextureContainer.COMPRESSED_2D = 0; // uses a gl.compressedTexImage2D()
  137. KhronosTextureContainer.COMPRESSED_3D = 1; // uses a gl.compressedTexImage3D()
  138. KhronosTextureContainer.TEX_2D = 2; // uses a gl.texImage2D()
  139. KhronosTextureContainer.TEX_3D = 3; // uses a gl.texImage3D()
  140. //# sourceMappingURL=khronosTextureContainer.js.map