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

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 openfl_utils__$ByteArray_ByteArray_$Impl_$() {return require("./../../../../openfl/utils/_ByteArray/ByteArray_Impl_");}
function lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$() {return require("./../../../../lime/graphics/_WebGLRenderContext/WebGLRenderContext_Impl_");}

// Constructor

var GLIndexBuffer3D = function(){}

// Meta

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

// Init



// Statics

GLIndexBuffer3D.create = function(indexBuffer,renderer,bufferUsage) {
	var gl = renderer.__context.webgl;
	indexBuffer.__elementType = gl.UNSIGNED_SHORT;
	indexBuffer.__id = gl.createBuffer();
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	indexBuffer.__usage = bufferUsage == "dynamicDraw" ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW;
}
GLIndexBuffer3D.dispose = function(indexBuffer,renderer) {
	var gl = renderer.__context.webgl;
	gl.deleteBuffer(indexBuffer.__id);
}
GLIndexBuffer3D.uploadFromByteArray = function(indexBuffer,renderer,data,byteArrayOffset,startOffset,count) {
	var offset = byteArrayOffset + startOffset * 2;
	var buffer = (openfl_utils__$ByteArray_ByteArray_$Impl_$().default).toArrayBuffer(data);
	var this1;
	if(buffer != null) {
		if(count == null) {
			this1 = new Int16Array(buffer,offset);
		} else {
			this1 = new Int16Array(buffer,offset,count);
		}
	} else {
		this1 = null;
	}
	GLIndexBuffer3D.uploadFromTypedArray(indexBuffer,renderer,this1);
}
GLIndexBuffer3D.uploadFromTypedArray = function(indexBuffer,renderer,data) {
	if(data == null) {
		return;
	}
	var gl = renderer.__context.webgl;
	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,indexBuffer.__id);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
	(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).bufferData(gl,gl.ELEMENT_ARRAY_BUFFER,data,indexBuffer.__usage);
	(openfl__$internal_stage3D_GLUtils().default).CheckGLError();
}
GLIndexBuffer3D.uploadFromVector = function(indexBuffer,renderer,data,startOffset,count) {
	if(data == null) {
		return;
	}
	var gl = renderer.__context.webgl;
	var length = startOffset + count;
	var existingInt16Array = indexBuffer.__tempInt16Array;
	if(indexBuffer.__tempInt16Array == null || indexBuffer.__tempInt16Array.length < count) {
		var this1;
		if(count != null) {
			this1 = new Int16Array(count);
		} else {
			this1 = null;
		}
		indexBuffer.__tempInt16Array = this1;
		if(existingInt16Array != null) {
			indexBuffer.__tempInt16Array.set(existingInt16Array);
		}
	}
	var _g1 = startOffset;
	var _g = length;
	while(_g1 < _g) {
		var i = _g1++;
		indexBuffer.__tempInt16Array[i - startOffset] = data[i];
	}
	GLIndexBuffer3D.uploadFromTypedArray(indexBuffer,renderer,indexBuffer.__tempInt16Array);
}


// Export

exports.default = GLIndexBuffer3D;