khronosTextureContainer.ts 8.9 KB

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