KhronosTextureContainer constructor

KhronosTextureContainer(
  1. ByteBuffer arrayBuffer,
  2. int facesExpected
)
  • @param {ArrayBuffer} arrayBuffer- contents of the KTX container file
    • @param {number} facesExpected- should be either 1 or 6, based whether a cube texture or or
      • @param {boolean} threeDExpected- provision for indicating that data should be a 3D texture, not implemented
      • @param {boolean} textureArrayExpected- provision for indicating that data should be a texture array, not implemented

Implementation

KhronosTextureContainer(this.arrayBuffer, int facesExpected /*, threeDExpected, textureArrayExpected */ ) {

	// Test that it is a ktx formatted file, based on the first 12 bytes, character representation is:
	// '´', 'K', 'T', 'X', ' ', '1', '1', 'ª', '\r', '\n', '\x1A', '\n'
	// 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
	final identifier = arrayBuffer.asUint8List().sublist(0,12);

	if ( identifier[0] != 0xAB ||
		identifier[ 1 ] != 0x4B ||
		identifier[ 2 ] != 0x54 ||
		identifier[ 3 ] != 0x58 ||
		identifier[ 4 ] != 0x20 ||
		identifier[ 5 ] != 0x31 ||
		identifier[ 6 ] != 0x31 ||
		identifier[ 7 ] != 0xBB ||
		identifier[ 8 ] != 0x0D ||
		identifier[ 9 ] != 0x0A ||
		identifier[ 10 ] != 0x1A ||
		identifier[ 11 ] != 0x0A ) {

		console.error( 'texture missing KTX identifier' );
		return;

	}

	// load the reset of the header in native 32 bit uint
	const dataSize = 4;
	final headerDataView = arrayBuffer.asByteData();//DataView(arrayBuffer, 12, 13 * dataSize );
	final endianness = headerDataView.getUint32( 0, Endian.little );
	final littleEndian = endianness == 0x04030201? Endian.little :Endian.big;

	glType = headerDataView.getUint32( 1 * dataSize, littleEndian ); // must be 0 for compressed textures
	glTypeSize = headerDataView.getUint32( 2 * dataSize, littleEndian ); // must be 1 for compressed textures
	glFormat = headerDataView.getUint32( 3 * dataSize, littleEndian ); // must be 0 for compressed textures
	glInternalFormat = headerDataView.getUint32( 4 * dataSize, littleEndian ); // the value of arg passed to gl.compressedTexImage2D(,,x,,,,)
	glBaseInternalFormat = headerDataView.getUint32( 5 * dataSize, littleEndian ); // specify GL_RGB, GL_RGBA, GL_ALPHA, etc (un-compressed only)
	pixelWidth = headerDataView.getUint32( 6 * dataSize, littleEndian ); // level 0 value of arg passed to gl.compressedTexImage2D(,,,x,,,)
	pixelHeight = headerDataView.getUint32( 7 * dataSize, littleEndian ); // level 0 value of arg passed to gl.compressedTexImage2D(,,,,x,,)
	pixelDepth = headerDataView.getUint32( 8 * dataSize, littleEndian ); // level 0 value of arg passed to gl.compressedTexImage3D(,,,,,x,,)
	numberOfArrayElements = headerDataView.getUint32( 9 * dataSize, littleEndian ); // used for texture arrays
	numberOfFaces = headerDataView.getUint32( 10 * dataSize, littleEndian ); // used for cubemap textures, should either be 1 or 6
	numberOfMipmapLevels = headerDataView.getUint32( 11 * dataSize, littleEndian ); // number of levels; disregard possibility of 0 for compressed textures
	bytesOfKeyValueData = headerDataView.getUint32( 12 * dataSize, littleEndian ); // the amount of space after the header for meta-data

	// Make sure we have a compressed type.  Not only reduces work, but probably better to let dev know they are not compressing.
	if (glType != 0) {
		console.warning( 'only compressed formats currently supported' );
		return;
	} else {
		// value of zero is an indication to generate mipmaps @ runtime.  Not usually allowed for compressed, so disregard.
		numberOfMipmapLevels = math.max( 1, numberOfMipmapLevels );
	}

	if (pixelHeight == 0 || pixelDepth != 0 ) {
		console.warning( 'only 2D textures currently supported' );
		return;
	}

	if (numberOfArrayElements != 0 ) {
		console.warning( 'texture arrays not currently supported' );
		return;
	}

	if (numberOfFaces != facesExpected ) {
		console.warning( 'number of faces expected $facesExpected, but found $numberOfFaces' );
		return;
	}

	// we now have a completely validated file, so could use existence of loadType as success
	// would need to make this more elaborate & adjust checks above to support more than one load type
	loadType = 0;
}