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

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_GLUtils() {return require("./../../../../openfl/_internal/stage3D/GLUtils");}
function lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$() {return require("./../../../../lime/graphics/_WebGLRenderContext/WebGLRenderContext_Impl_");}
function openfl__$internal_stage3D_atf_ATFReader() {return require("./../../../../openfl/_internal/stage3D/atf/ATFReader");}
function openfl_display_BitmapData() {return require("./../../../../openfl/display/BitmapData");}
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_stage3D_opengl_GLTextureBase() {return require("./../../../../openfl/_internal/stage3D/opengl/GLTextureBase");}

// Constructor

var GLTexture = function(){}

// Meta

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

// Init



// Statics

GLTexture.create = function(texture,renderer) {
	var gl = renderer.__context.webgl;
	texture.__textureTarget = gl.TEXTURE_2D;
	gl.bindTexture(texture.__textureTarget,texture.__textureID);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,texture.__textureTarget,0,texture.__internalFormat,texture.__width,texture.__height,0,texture.__format,gl.UNSIGNED_BYTE,null);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	gl.bindTexture(texture.__textureTarget,null);
}
GLTexture.uploadCompressedTextureFromByteArray = function(texture,renderer,data,byteArrayOffset) {
	var reader = new (openfl__$internal_stage3D_atf_ATFReader().default)(data,byteArrayOffset);
	var alpha = reader.readHeader(texture.__width,texture.__height,false);
	var gl = renderer.__context.webgl;
	gl.bindTexture(texture.__textureTarget,texture.__textureID);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	var hasTexture = false;
	reader.readTextures(function(target,level,gpuFormat,width,height,blockLength,bytes) {
	});
	if(!hasTexture) {
		var elements = texture.__width * texture.__height * 4;
		var this1;
		if(elements != null) {
			this1 = new Uint8Array(elements);
		} else {
			this1 = null;
		}
		var data1 = this1;
		(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,texture.__textureTarget,0,texture.__internalFormat,texture.__width,texture.__height,0,texture.__format,gl.UNSIGNED_BYTE,data1);
		(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	}
	gl.bindTexture(texture.__textureTarget,null);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
}
GLTexture.uploadFromBitmapData = function(texture,renderer,source,miplevel,generateMipmap) {
	if(source == null) {
		return;
	}
	var width = texture.__width >> miplevel;
	var height = texture.__height >> miplevel;
	if(width == 0 && height == 0) {
		return;
	}
	if(width == 0) {
		width = 1;
	}
	if(height == 0) {
		height = 1;
	}
	if(source.width != width || source.height != height) {
		var copy = new (openfl_display_BitmapData().default)(width,height,true,0);
		copy.draw(source);
		source = copy;
	}
	var image = texture.__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 width1 = texture.__width >> miplevel;
		var height1 = texture.__height >> miplevel;
		if(width1 == 0 && height1 == 0) {
			return;
		}
		if(width1 == 0) {
			width1 = 1;
		}
		if(height1 == 0) {
			height1 = 1;
		}
		gl.bindTexture(texture.__textureTarget,texture.__textureID);
		(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
		(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,texture.__textureTarget,miplevel,texture.__internalFormat,texture.__format,gl.UNSIGNED_BYTE,image.buffer.get_src());
		(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
		gl.bindTexture(texture.__textureTarget,null);
		(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
		return;
	}
	GLTexture.uploadFromTypedArray(texture,renderer,image.get_data(),miplevel);
}
GLTexture.uploadFromByteArray = function(texture,renderer,data,byteArrayOffset,miplevel) {
	if(miplevel == null) {
		miplevel = 0;
	}
	if(byteArrayOffset == 0) {
		GLTexture.uploadFromTypedArray(texture,renderer,data.b,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;
	}
	GLTexture.uploadFromTypedArray(texture,renderer,this1,miplevel);
}
GLTexture.uploadFromTypedArray = function(texture,renderer,data,miplevel) {
	if(miplevel == null) {
		miplevel = 0;
	}
	if(data == null) {
		return;
	}
	var gl = renderer.__context.webgl;
	var width = texture.__width >> miplevel;
	var height = texture.__height >> miplevel;
	if(width == 0 && height == 0) {
		return;
	}
	if(width == 0) {
		width = 1;
	}
	if(height == 0) {
		height = 1;
	}
	gl.bindTexture(texture.__textureTarget,texture.__textureID);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,texture.__textureTarget,miplevel,texture.__internalFormat,width,height,0,texture.__format,gl.UNSIGNED_BYTE,data);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	gl.bindTexture(texture.__textureTarget,null);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
}
GLTexture.setSamplerState = function(texture,renderer,state) {
	if(!state.equals(texture.__samplerState)) {
		var gl = renderer.__context.webgl;
		if(state.minFilter != gl.NEAREST && state.minFilter != gl.LINEAR && !state.mipmapGenerated) {
			gl.generateMipmap(gl.TEXTURE_2D);
			(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
			state.mipmapGenerated = true;
		}
		if(state.maxAniso != 0.0) {
			gl.texParameterf(gl.TEXTURE_2D,(openfl_display3D_Context3D().default).TEXTURE_MAX_ANISOTROPY_EXT,state.maxAniso);
			(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
		}
	}
	(openfl__$internal_stage3D_opengl_GLTextureBase().default).setSamplerState(texture,renderer,state);
}


// Export

exports.default = GLTexture;