babylon.khronosTextureContainer.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. module BABYLON.Internals {
  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. static HEADER_LEN = 12 + (13 * 4); // identifier + header elements (not including key value meta-data pairs)
  8. // load types
  9. static COMPRESSED_2D = 0; // uses a gl.compressedTexImage2D()
  10. static COMPRESSED_3D = 1; // uses a gl.compressedTexImage3D()
  11. static TEX_2D = 2; // uses a gl.texImage2D()
  12. static TEX_3D = 3; // uses a gl.texImage3D()
  13. // elements of the header
  14. public glType : number;
  15. public glTypeSize : number;
  16. public glFormat : number;
  17. public glInternalFormat : number;
  18. public glBaseInternalFormat : number;
  19. public pixelWidth : number;
  20. public pixelHeight : number;
  21. public pixelDepth : number;
  22. public numberOfArrayElements : number;
  23. public numberOfFaces : number;
  24. public numberOfMipmapLevels : number;
  25. public bytesOfKeyValueData : number;
  26. public loadType : number;
  27. /**
  28. * @param {ArrayBuffer} arrayBuffer- contents of the KTX container file
  29. * @param {number} facesExpected- should be either 1 or 6, based whether a cube texture or or
  30. * @param {boolean} threeDExpected- provision for indicating that data should be a 3D texture, not implemented
  31. * @param {boolean} textureArrayExpected- provision for indicating that data should be a texture array, not implemented
  32. */
  33. public constructor (public arrayBuffer : any, facesExpected : number, threeDExpected? : boolean, textureArrayExpected? : boolean) {
  34. // Test that it is a ktx formatted file, based on the first 12 bytes, character representation is:
  35. // '«', 'K', 'T', 'X', ' ', '1', '1', '»', '\r', '\n', '\x1A', '\n'
  36. // 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
  37. var identifier = new Uint8Array(this.arrayBuffer, 0, 12);
  38. if (identifier[ 0] !== 0xAB || identifier[ 1] !== 0x4B || identifier[ 2] !== 0x54 || identifier[ 3] !== 0x58 || identifier[ 4] !== 0x20 || identifier[ 5] !== 0x31 ||
  39. identifier[ 6] !== 0x31 || identifier[ 7] !== 0xBB || identifier[ 8] !== 0x0D || identifier[ 9] !== 0x0A || identifier[10] !== 0x1A || identifier[11] !== 0x0A) {
  40. Tools.Error("texture missing KTX identifier");
  41. return;
  42. }
  43. // load the reset of the header in native 32 bit int
  44. var header = new Int32Array(this.arrayBuffer, 12, 13);
  45. // determine of the remaining header values are recorded in the opposite endianness & require conversion
  46. var oppositeEndianess = header[0] === 0x01020304;
  47. // read all the header elements in order they exist in the file, without modification (sans endainness)
  48. this.glType = oppositeEndianess ? this.switchEndainness(header[ 1]) : header[ 1]; // must be 0 for compressed textures
  49. this.glTypeSize = oppositeEndianess ? this.switchEndainness(header[ 2]) : header[ 2]; // must be 1 for compressed textures
  50. this.glFormat = oppositeEndianess ? this.switchEndainness(header[ 3]) : header[ 3]; // must be 0 for compressed textures
  51. this.glInternalFormat = oppositeEndianess ? this.switchEndainness(header[ 4]) : header[ 4]; // the value of arg passed to gl.compressedTexImage2D(,,x,,,,)
  52. this.glBaseInternalFormat = oppositeEndianess ? this.switchEndainness(header[ 5]) : header[ 5]; // specify GL_RGB, GL_RGBA, GL_ALPHA, etc (un-compressed only)
  53. this.pixelWidth = oppositeEndianess ? this.switchEndainness(header[ 6]) : header[ 6]; // level 0 value of arg passed to gl.compressedTexImage2D(,,,x,,,)
  54. this.pixelHeight = oppositeEndianess ? this.switchEndainness(header[ 7]) : header[ 7]; // level 0 value of arg passed to gl.compressedTexImage2D(,,,,x,,)
  55. this.pixelDepth = oppositeEndianess ? this.switchEndainness(header[ 8]) : header[ 8]; // level 0 value of arg passed to gl.compressedTexImage3D(,,,,,x,,)
  56. this.numberOfArrayElements = oppositeEndianess ? this.switchEndainness(header[ 9]) : header[ 9]; // used for texture arrays
  57. this.numberOfFaces = oppositeEndianess ? this.switchEndainness(header[10]) : header[10]; // used for cubemap textures, should either be 1 or 6
  58. this.numberOfMipmapLevels = oppositeEndianess ? this.switchEndainness(header[11]) : header[11]; // number of levels; disregard possibility of 0 for compressed textures
  59. this.bytesOfKeyValueData = oppositeEndianess ? this.switchEndainness(header[12]) : header[12]; // the amount of space after the header for meta-data
  60. // Make sure we have a compressed type. Not only reduces work, but probably better to let dev know they are not compressing.
  61. if (this.glType !== 0) {
  62. Tools.Error("only compressed formats currently supported");
  63. return;
  64. } else {
  65. // value of zero is an indication to generate mipmaps @ runtime. Not usually allowed for compressed, so disregard.
  66. this.numberOfMipmapLevels = Math.max(1, this.numberOfMipmapLevels);
  67. }
  68. if (this.pixelHeight === 0 || this.pixelDepth !== 0) {
  69. Tools.Error("only 2D textures currently supported");
  70. return;
  71. }
  72. if (this.numberOfArrayElements !== 0) {
  73. Tools.Error("texture arrays not currently supported");
  74. return;
  75. }
  76. if (this.numberOfFaces !== facesExpected) {
  77. Tools.Error("number of faces expected" + facesExpected + ", but found " + this.numberOfFaces);
  78. return;
  79. }
  80. // we now have a completely validated file, so could use existence of loadType as success
  81. // would need to make this more elaborate & adjust checks above to support more than one load type
  82. this.loadType = KhronosTextureContainer.COMPRESSED_2D;
  83. }
  84. // not as fast hardware based, but will probably never need to use
  85. public switchEndainness(val : number) : number {
  86. return ((val & 0xFF) << 24)
  87. | ((val & 0xFF00) << 8)
  88. | ((val >> 8) & 0xFF00)
  89. | ((val >> 24) & 0xFF);
  90. }
  91. /**
  92. * It is assumed that the texture has already been created & is currently bound
  93. */
  94. public uploadLevels(gl: WebGLRenderingContext, loadMipmaps: boolean) : void {
  95. switch (this.loadType){
  96. case KhronosTextureContainer.COMPRESSED_2D:
  97. this._upload2DCompressedLevels(gl, loadMipmaps);
  98. break;
  99. case KhronosTextureContainer.TEX_2D:
  100. case KhronosTextureContainer.COMPRESSED_3D:
  101. case KhronosTextureContainer.TEX_3D:
  102. }
  103. }
  104. private _upload2DCompressedLevels(gl: WebGLRenderingContext, loadMipmaps: boolean): void {
  105. // initialize width & height for level 1
  106. var dataOffset = KhronosTextureContainer.HEADER_LEN + this.bytesOfKeyValueData;
  107. var width = this.pixelWidth;
  108. var height = this.pixelHeight;
  109. var mipmapCount = loadMipmaps ? this.numberOfMipmapLevels : 1;
  110. for (var level = 0; level < mipmapCount; level++) {
  111. var imageSize = new Int32Array(this.arrayBuffer, dataOffset, 1)[0]; // size per face, since not supporting array cubemaps
  112. for (var face = 0; face < this.numberOfFaces; face++) {
  113. var sampler = this.numberOfFaces === 1 ? gl.TEXTURE_2D : (gl.TEXTURE_CUBE_MAP_POSITIVE_X + face);
  114. var byteArray = new Uint8Array(this.arrayBuffer, dataOffset + 4, imageSize);
  115. gl.compressedTexImage2D(sampler, level, this.glInternalFormat, width, height, 0, byteArray);
  116. dataOffset += imageSize + 4; // size of the image + 4 for the imageSize field
  117. dataOffset += 3 - ((imageSize + 3) % 4); // add padding for odd sized image
  118. }
  119. width = Math.max(1.0, width * 0.5);
  120. height = Math.max(1.0, height * 0.5);
  121. }
  122. }
  123. }
  124. }