<commit_before>package haxepunk.graphics.hardware;

#if js
import js.html.Int32Array;
#end

#if nme
import flash.gl.GL;
import flash.gl.GLBuffer;
#else
import lime.graphics.opengl.GL;
import lime.graphics.opengl.GLBuffer;
#end

class RenderBuffer
{
	static inline var INITIAL_SIZE:Int = 100;

	static inline function resize(length:Int, minChunks:Int, chunkSize:Int)
	{
		return Std.int(Math.max(
			Std.int(length * 2 / chunkSize),
			minChunks
		) * chunkSize);
	}

	public var buffer:Float32Array;
	public var glBuffer:GLBuffer;

	public var length(get, never):Int;
	inline function get_length() return buffer.length;

	#if js
	var intArray:Int32Array;
	#end

	public function new()
	{
		init();
	}

	public function init()
	{
		glBuffer = GL.createBuffer();
	}

	public function ensureSize(triangles:Int, floatsPerTriangle:Int)
	{
		var bufferLength = buffer == null ? 0 : buffer.length;
		if (bufferLength < triangles * floatsPerTriangle)
		{
			buffer = new Float32Array(resize(bufferLength, triangles, floatsPerTriangle));
			#if js
			intArray = new Int32Array(buffer.buffer);
			#end

			GL.bindBuffer(GL.ARRAY_BUFFER, glBuffer);
			#if (lime >= "4.0.0")
			GL.bufferData(GL.ARRAY_BUFFER, buffer.length * Float32Array.BYTES_PER_ELEMENT, buffer, GL.DYNAMIC_DRAW);
			#else
			GL.bufferData(GL.ARRAY_BUFFER, buffer, GL.DYNAMIC_DRAW);
			#end
		}<commit_msg>Recreate GLBuffer on context loss<commit_after>package haxepunk.graphics.hardware;

#if js
import js.html.Int32Array;
#end

#if nme
import flash.gl.GL;
import flash.gl.GLBuffer;
#else
import lime.graphics.opengl.GL;
import lime.graphics.opengl.GLBuffer;
#end

class RenderBuffer
{
	static inline var INITIAL_SIZE:Int = 100;

	static inline function resize(length:Int, minChunks:Int, chunkSize:Int)
	{
		return Std.int(Math.max(
			Std.int(length * 2 / chunkSize),
			minChunks
		) * chunkSize);
	}

	public var buffer:Float32Array;
	public var glBuffer:GLBuffer;

	public var length(get, never):Int;
	inline function get_length() return buffer.length;

	#if js
	var intArray:Int32Array;
	#end

	public function new()
	{
		init();
	}

	public function init()
	{
		glBuffer = GL.createBuffer();
	}

	public function ensureSize(triangles:Int, floatsPerTriangle:Int)
	{
		if (GLUtils.invalid(glBuffer))
		{
			buffer = null;
			init();
		}
		var bufferLength = buffer == null ? 0 : buffer.length;
		if (bufferLength < triangles * floatsPerTriangle)
		{
			buffer = new Float32Array(resize(bufferLength, triangles, floatsPerTriangle));
			#if js
			intArray = new Int32Array(buffer.buffer);
			#end

			GL.bindBuffer(GL.ARRAY_BUFFER, glBuffer);
			#if (lime >= "4.0.0")
			GL.bufferData(GL.ARRAY_BUFFER, buffer.length * Float32Array.BYTES_PER_ELEMENT, buffer, GL.DYNAMIC_DRAW);
			#else
			GL.bufferData(GL.ARRAY_BUFFER, buffer, GL.DYNAMIC_DRAW);
			#end
		}
{'fraction_non_alphanumeric': 0.1171875, 'fraction_numerical': 0.014204545454545454, 'mean_word_length': 4.375954198473282, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2754767', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1005, 'n_words': 225}