// Class: openfl._internal.stage3D.opengl.GLCubeTexture

var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this

$global.Object.defineProperty(exports, "__esModule", {value: true});

var __map_reserved = {};

// Imports

var $hxClasses = require("./../../../../hxClasses_stub").default;
var $import = require("./../../../../import_stub").default;
function openfl__$internal_stage3D_atf_ATFReader() {return require("./../../../../openfl/_internal/stage3D/atf/ATFReader");}
function openfl__$internal_stage3D_GLUtils() {return require("./../../../../openfl/_internal/stage3D/GLUtils");}
function openfl__$internal_stage3D_opengl_GLTextureBase() {return require("./../../../../openfl/_internal/stage3D/opengl/GLTextureBase");}
function Std() {return require("./../../../../Std");}
function lime_utils__$DataPointer_DataPointer_$Impl_$() {return require("./../../../../lime/utils/_DataPointer/DataPointer_Impl_");}
function openfl_display3D_textures_CubeTexture() {return require("./../../../../openfl/display3D/textures/CubeTexture");}
function lime_utils_BytePointerData() {return require("./../../../../lime/utils/BytePointerData");}
function openfl_utils__$ByteArray_ByteArray_$Impl_$() {return require("./../../../../openfl/utils/_ByteArray/ByteArray_Impl_");}
function openfl_display3D_Context3D() {return require("./../../../../openfl/display3D/Context3D");}
function js__$Boot_HaxeError() {return require("./../../../../js/_Boot/HaxeError");}
function openfl_errors_IllegalOperationError() {return require("./../../../../openfl/errors/IllegalOperationError");}

// Constructor

var GLCubeTexture = function(){}

// Meta

GLCubeTexture.__name__ = ["openfl","_internal","stage3D","opengl","GLCubeTexture"];
GLCubeTexture.prototype = {
	
};
GLCubeTexture.prototype.__class__ = $hxClasses["openfl._internal.stage3D.opengl.GLCubeTexture"] = GLCubeTexture;

// Init



// Statics

GLCubeTexture.create = function(cubeTexture,renderer) {
	var gl = renderer.__gl;
	cubeTexture.__textureTarget = gl.TEXTURE_CUBE_MAP;
	cubeTexture.__uploadedSides = 0;
}
GLCubeTexture.uploadCompressedTextureFromByteArray = function(cubeTexture,renderer,data,byteArrayOffset) {
	var reader = new (openfl__$internal_stage3D_atf_ATFReader().default)(data,byteArrayOffset);
	var alpha = reader.readHeader(cubeTexture.__size,cubeTexture.__size,true);
	var gl = renderer.__gl;
	gl.bindTexture(cubeTexture.__textureTarget,cubeTexture.__textureID);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	var hasTexture = false;
	reader.readTextures(function(side,level,gpuFormat,width,height,blockLength,bytes) {
		var format = (openfl__$internal_stage3D_opengl_GLTextureBase().default).__compressedTextureFormats.toTextureFormat(alpha,gpuFormat);
		if(format == 0) {
			return;
		}
		hasTexture = true;
		var target = GLCubeTexture.__sideToTarget(gl,side);
		cubeTexture.__format = format;
		cubeTexture.__internalFormat = format;
		if(alpha && gpuFormat == 2) {
			var size = (Std().default)["int"](blockLength / 2);
			gl.compressedTexImage2D(target,level,cubeTexture.__internalFormat,width,height,0,size,(lime_utils__$DataPointer_DataPointer_$Impl_$().default).fromBytes(bytes));
			(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
			var alphaTexture = new (openfl_display3D_textures_CubeTexture().default)(cubeTexture.__context,cubeTexture.__size,"compressed",cubeTexture.__optimizeForRenderToTexture,cubeTexture.__streamingLevels);
			alphaTexture.__format = format;
			alphaTexture.__internalFormat = format;
			gl.bindTexture(alphaTexture.__textureTarget,alphaTexture.__textureID);
			(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
			var alphaTexture1 = alphaTexture.__internalFormat;
			var this1 = new (lime_utils_BytePointerData().default)(bytes,size);
			gl.compressedTexImage2D(target,level,alphaTexture1,width,height,0,size,(lime_utils__$DataPointer_DataPointer_$Impl_$().default).fromBytesPointer(this1));
			(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
			cubeTexture.__alphaTexture = alphaTexture;
		} else {
			gl.compressedTexImage2D(target,level,cubeTexture.__internalFormat,width,height,0,blockLength,(lime_utils__$DataPointer_DataPointer_$Impl_$().default).fromBytes(bytes));
			(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
		}
	});
	if(!hasTexture) {
		var _g = 0;
		while(_g < 6) {
			var side1 = _g++;
			var elements = cubeTexture.__size * cubeTexture.__size * 4;
			var this2;
			if(elements != null) {
				this2 = new Uint8Array(elements);
			} else {
				this2 = null;
			}
			var data1 = this2;
			var tmp = GLCubeTexture.__sideToTarget(gl,side1);
			gl.texImage2D(tmp,0,cubeTexture.__internalFormat,cubeTexture.__size,cubeTexture.__size,0,cubeTexture.__format,gl.UNSIGNED_BYTE,(lime_utils__$DataPointer_DataPointer_$Impl_$().default).fromArrayBufferView(data1));
			(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
		}
	}
	gl.bindTexture(cubeTexture.__textureTarget,null);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
}
GLCubeTexture.uploadFromBitmapData = function(cubeTexture,renderer,source,side,miplevel,generateMipmap) {
	if(generateMipmap == null) {
		generateMipmap = false;
	}
	if(miplevel == null) {
		miplevel = 0;
	}
	var size = cubeTexture.__size >> miplevel;
	if(size == 0) {
		return;
	}
	var image = cubeTexture.__getImage(source);
	GLCubeTexture.uploadFromTypedArray(cubeTexture,renderer,image.get_data(),side,miplevel);
}
GLCubeTexture.uploadFromByteArray = function(cubeTexture,renderer,data,byteArrayOffset,side,miplevel) {
	if(byteArrayOffset == 0) {
		GLCubeTexture.uploadFromTypedArray(cubeTexture,renderer,data.b,side,miplevel);
		return;
	}
	var buffer = (openfl_utils__$ByteArray_ByteArray_$Impl_$().default).toArrayBuffer(data);
	var this1;
	if(buffer != null) {
		this1 = new Uint8Array(buffer,byteArrayOffset);
	} else {
		this1 = null;
	}
	GLCubeTexture.uploadFromTypedArray(cubeTexture,renderer,this1,side,miplevel);
}
GLCubeTexture.uploadFromTypedArray = function(cubeTexture,renderer,data,side,miplevel) {
	if(data == null) {
		return;
	}
	var gl = renderer.__gl;
	var size = cubeTexture.__size >> miplevel;
	if(size == 0) {
		return;
	}
	var target = GLCubeTexture.__sideToTarget(gl,side);
	gl.bindTexture(gl.TEXTURE_CUBE_MAP,cubeTexture.__textureID);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	gl.texImage2D(target,miplevel,cubeTexture.__internalFormat,size,size,0,cubeTexture.__format,gl.UNSIGNED_BYTE,(lime_utils__$DataPointer_DataPointer_$Impl_$().default).fromArrayBufferView(data));
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	gl.bindTexture(cubeTexture.__textureTarget,null);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	cubeTexture.__uploadedSides |= 1 << side;
}
GLCubeTexture.setSamplerState = function(cubeTexture,renderer,state) {
	if(!state.equals(cubeTexture.__samplerState)) {
		var gl = renderer.__gl;
		if(state.minFilter != gl.NEAREST && state.minFilter != gl.LINEAR && !state.mipmapGenerated) {
			gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
			(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
			state.mipmapGenerated = true;
		}
		if(state.maxAniso != 0.0) {
			gl.texParameterf(gl.TEXTURE_CUBE_MAP,(openfl_display3D_Context3D().default).TEXTURE_MAX_ANISOTROPY_EXT,state.maxAniso);
			(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
		}
	}
	(openfl__$internal_stage3D_opengl_GLTextureBase().default).setSamplerState(cubeTexture,renderer,state);
}
GLCubeTexture.__sideToTarget = function(gl,side) {
	switch(side) {
	case 0:
		return gl.TEXTURE_CUBE_MAP_NEGATIVE_X;
	case 1:
		return gl.TEXTURE_CUBE_MAP_POSITIVE_X;
	case 2:
		return gl.TEXTURE_CUBE_MAP_NEGATIVE_Y;
	case 3:
		return gl.TEXTURE_CUBE_MAP_POSITIVE_Y;
	case 4:
		return gl.TEXTURE_CUBE_MAP_NEGATIVE_Z;
	case 5:
		return gl.TEXTURE_CUBE_MAP_POSITIVE_Z;
	default:
		throw new (js__$Boot_HaxeError().default)(new (openfl_errors_IllegalOperationError().default)());
	}
}


// Export

exports.default = GLCubeTexture;