khronosTextureContainer.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // Test that it is a ktx formatted file, based on the first 12 bytes, character representation is:
  82. // '�', 'K', 'T', 'X', ' ', '1', '1', '�', '\r', '\n', '\x1A', '\n'
  83. // 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
  84. var identifier = new Uint8Array(this.data.buffer, this.data.byteOffset, 12);
  85. if (identifier[0] !== 0xAB || identifier[1] !== 0x4B || identifier[2] !== 0x54 || identifier[3] !== 0x58 || identifier[4] !== 0x20 || identifier[5] !== 0x31 ||
  86. identifier[6] !== 0x31 || identifier[7] !== 0xBB || identifier[8] !== 0x0D || identifier[9] !== 0x0A || identifier[10] !== 0x1A || identifier[11] !== 0x0A) {
  87. this.isInvalid = true;
  88. Logger.Error("texture missing KTX identifier");
  89. return;
  90. }
  91. // load the reset of the header in native 32 bit uint
  92. var dataSize = Uint32Array.BYTES_PER_ELEMENT;
  93. var headerDataView = new DataView(this.data.buffer, this.data.byteOffset + 12, 13 * dataSize);
  94. var endianness = headerDataView.getUint32(0, true);
  95. var littleEndian = endianness === 0x04030201;
  96. this.glType = headerDataView.getUint32(1 * dataSize, littleEndian); // must be 0 for compressed textures
  97. this.glTypeSize = headerDataView.getUint32(2 * dataSize, littleEndian); // must be 1 for compressed textures
  98. this.glFormat = headerDataView.getUint32(3 * dataSize, littleEndian); // must be 0 for compressed textures
  99. this.glInternalFormat = headerDataView.getUint32(4 * dataSize, littleEndian); // the value of arg passed to gl.compressedTexImage2D(,,x,,,,)
  100. this.glBaseInternalFormat = headerDataView.getUint32(5 * dataSize, littleEndian); // specify GL_RGB, GL_RGBA, GL_ALPHA, etc (un-compressed only)
  101. this.pixelWidth = headerDataView.getUint32(6 * dataSize, littleEndian); // level 0 value of arg passed to gl.compressedTexImage2D(,,,x,,,)
  102. this.pixelHeight = headerDataView.getUint32(7 * dataSize, littleEndian); // level 0 value of arg passed to gl.compressedTexImage2D(,,,,x,,)
  103. this.pixelDepth = headerDataView.getUint32(8 * dataSize, littleEndian); // level 0 value of arg passed to gl.compressedTexImage3D(,,,,,x,,)
  104. this.numberOfArrayElements = headerDataView.getUint32(9 * dataSize, littleEndian); // used for texture arrays
  105. this.numberOfFaces = headerDataView.getUint32(10 * dataSize, littleEndian); // used for cubemap textures, should either be 1 or 6
  106. this.numberOfMipmapLevels = headerDataView.getUint32(11 * dataSize, littleEndian); // number of levels; disregard possibility of 0 for compressed textures
  107. this.bytesOfKeyValueData = headerDataView.getUint32(12 * dataSize, littleEndian); // the amount of space after the header for meta-data
  108. // Make sure we have a compressed type. Not only reduces work, but probably better to let dev know they are not compressing.
  109. if (this.glType !== 0) {
  110. Logger.Error("only compressed formats currently supported");
  111. return;
  112. } else {
  113. // value of zero is an indication to generate mipmaps @ runtime. Not usually allowed for compressed, so disregard.
  114. this.numberOfMipmapLevels = Math.max(1, this.numberOfMipmapLevels);
  115. }
  116. if (this.pixelHeight === 0 || this.pixelDepth !== 0) {
  117. Logger.Error("only 2D textures currently supported");
  118. return;
  119. }
  120. if (this.numberOfArrayElements !== 0) {
  121. Logger.Error("texture arrays not currently supported");
  122. return;
  123. }
  124. if (this.numberOfFaces !== facesExpected) {
  125. Logger.Error("number of faces expected" + facesExpected + ", but found " + this.numberOfFaces);
  126. return;
  127. }
  128. // we now have a completely validated file, so could use existence of loadType as success
  129. // would need to make this more elaborate & adjust checks above to support more than one load type
  130. this.loadType = KhronosTextureContainer.COMPRESSED_2D;
  131. }
  132. /**
  133. * Uploads KTX content to a Babylon Texture.
  134. * It is assumed that the texture has already been created & is currently bound
  135. * @hidden
  136. */
  137. public uploadLevels(texture: InternalTexture, loadMipmaps: boolean): void {
  138. switch (this.loadType) {
  139. case KhronosTextureContainer.COMPRESSED_2D:
  140. this._upload2DCompressedLevels(texture, loadMipmaps);
  141. break;
  142. case KhronosTextureContainer.TEX_2D:
  143. case KhronosTextureContainer.COMPRESSED_3D:
  144. case KhronosTextureContainer.TEX_3D:
  145. }
  146. }
  147. private _upload2DCompressedLevels(texture: InternalTexture, loadMipmaps: boolean): void {
  148. // initialize width & height for level 1
  149. var dataOffset = KhronosTextureContainer.HEADER_LEN + this.bytesOfKeyValueData;
  150. var width = this.pixelWidth;
  151. var height = this.pixelHeight;
  152. var mipmapCount = loadMipmaps ? this.numberOfMipmapLevels : 1;
  153. for (var level = 0; level < mipmapCount; level++) {
  154. var imageSize = new Int32Array(this.data.buffer, this.data.byteOffset + dataOffset, 1)[0]; // size per face, since not supporting array cubemaps
  155. dataOffset += 4; //image data starts from next multiple of 4 offset. Each face refers to same imagesize field above.
  156. for (var face = 0; face < this.numberOfFaces; face++) {
  157. var byteArray = new Uint8Array(this.data.buffer, this.data.byteOffset + dataOffset, imageSize);
  158. const engine = texture.getEngine();
  159. engine._uploadCompressedDataToTextureDirectly(texture, this.glInternalFormat, width, height, byteArray, face, level);
  160. dataOffset += imageSize; // add size of the image for the next face/mipmap
  161. dataOffset += 3 - ((imageSize + 3) % 4); // add padding for odd sized image
  162. }
  163. width = Math.max(1.0, width * 0.5);
  164. height = Math.max(1.0, height * 0.5);
  165. }
  166. }
  167. }