// Class: openfl._internal.renderer.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_formats_atf_ATFReader() {return require("./../../../../openfl/_internal/formats/atf/ATFReader");}
function openfl__$internal_renderer_opengl_GLUtils() {return require("./../../../../openfl/_internal/renderer/opengl/GLUtils");}
function lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$() {return require("./../../../../lime/graphics/_WebGLRenderContext/WebGLRenderContext_Impl_");}
function openfl_utils__$ByteArray_ByteArray_$Impl_$() {return require("./../../../../openfl/utils/_ByteArray/ByteArray_Impl_");}
function openfl_display3D_Context3D() {return require("./../../../../openfl/display3D/Context3D");}
function openfl__$internal_renderer_opengl_GLTextureBase() {return require("./../../../../openfl/_internal/renderer/opengl/GLTextureBase");}
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","renderer","opengl","GLCubeTexture"];
GLCubeTexture.prototype = {
	
};
GLCubeTexture.prototype.__class__ = GLCubeTexture.prototype.constructor = $hxClasses["openfl._internal.renderer.opengl.GLCubeTexture"] = GLCubeTexture;

// Init



// Statics

GLCubeTexture.create = function(cubeTexture,renderer) {
	var gl = renderer.__context.webgl;
	cubeTexture.__textureTarget = gl.TEXTURE_CUBE_MAP;
	cubeTexture.__uploadedSides = 0;
}
GLCubeTexture.uploadCompressedTextureFromByteArray = function(cubeTexture,renderer,data,byteArrayOffset) {
	var reader = new (openfl__$internal_formats_atf_ATFReader().default)(data,byteArrayOffset);
	var alpha = reader.readHeader(cubeTexture.__size,cubeTexture.__size,true);
	var gl = renderer.__context.webgl;
	gl.bindTexture(cubeTexture.__textureTarget,cubeTexture.__textureID);
	(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
	var hasTexture = false;
	reader.readTextures(function(side,level,gpuFormat,width,height,blockLength,bytes) {
	});
	if(!hasTexture) {
		var _g = 0;
		while(_g < 6) {
			var side1 = _g++;
			var elements = cubeTexture.__size * cubeTexture.__size * 4;
			var this1;
			if(elements != null) {
				this1 = new Uint8Array(elements);
			} else {
				this1 = null;
			}
			var data1 = this1;
			(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,GLCubeTexture.__sideToTarget(renderer.__context,side1),0,cubeTexture.__internalFormat,cubeTexture.__size,cubeTexture.__size,0,cubeTexture.__format,gl.UNSIGNED_BYTE,data1);
			(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
		}
	}
	gl.bindTexture(cubeTexture.__textureTarget,null);
	(openfl__$internal_renderer_opengl_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);
	if(image == null) {
		return;
	}
	if(miplevel == 0 && image.buffer != null && image.buffer.data == null && image.buffer.get_src() != null) {
		var gl = renderer.__context.webgl;
		var size1 = cubeTexture.__size >> miplevel;
		if(size1 == 0) {
			return;
		}
		var target = GLCubeTexture.__sideToTarget(renderer.__context,side);
		renderer.bindTexture(gl.TEXTURE_CUBE_MAP,cubeTexture.__textureID);
		(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
		(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,target,miplevel,cubeTexture.__internalFormat,cubeTexture.__format,gl.UNSIGNED_BYTE,image.buffer.get_src());
		(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
		renderer.bindTexture(cubeTexture.__textureTarget,null);
		(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
		cubeTexture.__uploadedSides |= 1 << side;
		return;
	}
	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.__context.webgl;
	var size = cubeTexture.__size >> miplevel;
	if(size == 0) {
		return;
	}
	var target = GLCubeTexture.__sideToTarget(renderer.__context,side);
	renderer.bindTexture(gl.TEXTURE_CUBE_MAP,cubeTexture.__textureID);
	(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
	(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,target,miplevel,cubeTexture.__internalFormat,size,size,0,cubeTexture.__format,gl.UNSIGNED_BYTE,data);
	(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
	renderer.bindTexture(cubeTexture.__textureTarget,null);
	(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
	cubeTexture.__uploadedSides |= 1 << side;
}
GLCubeTexture.setSamplerState = function(cubeTexture,renderer,state) {
	if(!state.equals(cubeTexture.__samplerState)) {
		var gl = renderer.__context.webgl;
		if(state.minFilter != gl.NEAREST && state.minFilter != gl.LINEAR && !state.mipmapGenerated) {
			gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
			(openfl__$internal_renderer_opengl_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_renderer_opengl_GLUtils().default).CheckGLError();
		}
	}
	(openfl__$internal_renderer_opengl_GLTextureBase().default).setSamplerState(cubeTexture,renderer,state);
}
GLCubeTexture.__sideToTarget = function(context,side) {
	var gl = context.webgl;
	switch(side) {
	case 0:
		return gl.TEXTURE_CUBE_MAP_POSITIVE_X;
	case 1:
		return gl.TEXTURE_CUBE_MAP_NEGATIVE_X;
	case 2:
		return gl.TEXTURE_CUBE_MAP_POSITIVE_Y;
	case 3:
		return gl.TEXTURE_CUBE_MAP_NEGATIVE_Y;
	case 4:
		return gl.TEXTURE_CUBE_MAP_POSITIVE_Z;
	case 5:
		return gl.TEXTURE_CUBE_MAP_NEGATIVE_Z;
	default:
		throw new (js__$Boot_HaxeError().default)(new (openfl_errors_IllegalOperationError().default)());
	}
}


// Export

exports.default = GLCubeTexture;