babylon.khronosTextureContainer.ts 9.8 KB

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