<commit_before>/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.badlogic.gdx.backends.gwt;

import java.nio.Buffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.google.gwt.typedarrays.client.Float32Array;
import com.google.gwt.typedarrays.client.Uint16Array;
import com.google.gwt.webgl.client.WebGLActiveInfo;
import com.google.gwt.webgl.client.WebGLBuffer;
import com.google.gwt.webgl.client.WebGLFramebuffer;
import com.google.gwt.webgl.client.WebGLProgram;
import com.google.gwt.webgl.client.WebGLRenderbuffer;
import com.google.gwt.webgl.client.WebGLRenderingContext;
import com.google.gwt.webgl.client.WebGLShader;
import com.google.gwt.webgl.client.WebGLTexture;
import com.google.gwt.webgl.client.WebGLUniformLocation;

public class GwtGL20 implements GL20 {
	final	Map<Integer, WebGLProgram> programs = new HashMap<Integer, WebGLProgram>();
	int nextProgramId = 1;
	final	Map<Integer, WebGLShader> shaders = new HashMap<Integer, WebGLShader>();
	int nextShaderId = 1;
	final Map<Integer, WebGLBuffer> buffers = new HashMap<Integer, WebGLBuffer>();
	int nextBufferId = 1;
	final Map<Integer, WebGLFramebuffer> frameBuffers = new HashMap<Integer, WebGLFramebuffer>();
	int nextFrameBufferId = 1;
	final Map<Integer, WebGLRenderbuffer> renderBuffers = new HashMap<Integer, WebGLRenderbuffer>();
	int nextRenderBufferId = 1;
	final Map<Integer, WebGLTexture> textures = new HashMap<Integer, WebGLTexture>();
	int nextTextureId = 1;
	final Map<Integer, Map<Integer, WebGLUniformLocation>> uniforms = new HashMap<Integer, Map<Integer, WebGLUniformLocation>>();
	int nextUniformId = 1;
	int currProgram = 0;
	
	Float32Array floatBuffer = Float32Array.create(2000 * 20);
	Uint16Array shortBuffer = Uint16Array.create(2000 * 6);
	
	final WebGLRenderingContext gl;

	protected GwtGL20 (WebGLRenderingContext gl) {
		this.gl = gl;
		this.gl.pixelStorei(WebGLRenderingContext.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
	}
	
	private void ensureCapacity(FloatBuffer buffer) {
		if(buffer.remaining() > floatBuffer.getLength()) {
			floatBuffer = Float32Array.create(buffer.remaining());
		}
	}
	
	private void ensureCapacity(ShortBuffer buffer) {
		if(buffer.remaining() > shortBuffer.getLength()) {
			shortBuffer = Uint16Array.create(buffer.remaining());
		}
	}
	
	public Float32Array copy(FloatBuffer buffer) {
		ensureCapacity(buffer);
		float[] array = buffer.array();
		for(int i = buffer.position(), j = 0; i < buffer.limit(); i++, j++) {
			floatBuffer.set(j, array[i]);
		}
		return floatBuffer;
	}
	
	public Uint16Array copy(ShortBuffer buffer) {
		ensureCapacity(buffer);
		short[] array = buffer.array();
		for(int i = buffer.position(), j = 0; i < buffer.limit(); i++, j++) {
			shortBuffer.set(j, array[i]);
		}
		return shortBuffer;
	}
	
	private int allocateUniformLocationId(int program, WebGLUniformLocation location) {
		Map<Integer, WebGLUniformLocation> progUniforms = uniforms.get(program);
		if(progUniforms == null) {
			progUniforms = new HashMap<Integer, WebGLUniformLocation>();
			uniforms.put(program, progUniforms);
		}
		// FIXME check if uniform already stored.
		int id = nextUniformId++;
		progUniforms.put(id, location);
		return id;
	}
	
	private WebGLUniformLocation getUniformLocation (int location) {
		return uniforms.get(currProgram).get(location);
	}
	
	private int allocateShaderId(WebGLShader shader) {
		int id = nextShaderId++;
		shaders.put(id, shader);
		return id;
	}<commit_msg>[fixed] buffer copy returns subbuffer in GWTGL20. typed arrays are indeed weird.<commit_after>/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.badlogic.gdx.backends.gwt;

import java.nio.Buffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.google.gwt.typedarrays.client.Float32Array;
import com.google.gwt.typedarrays.client.Uint16Array;
import com.google.gwt.webgl.client.WebGLActiveInfo;
import com.google.gwt.webgl.client.WebGLBuffer;
import com.google.gwt.webgl.client.WebGLFramebuffer;
import com.google.gwt.webgl.client.WebGLProgram;
import com.google.gwt.webgl.client.WebGLRenderbuffer;
import com.google.gwt.webgl.client.WebGLRenderingContext;
import com.google.gwt.webgl.client.WebGLShader;
import com.google.gwt.webgl.client.WebGLTexture;
import com.google.gwt.webgl.client.WebGLUniformLocation;

public class GwtGL20 implements GL20 {
	final	Map<Integer, WebGLProgram> programs = new HashMap<Integer, WebGLProgram>();
	int nextProgramId = 1;
	final	Map<Integer, WebGLShader> shaders = new HashMap<Integer, WebGLShader>();
	int nextShaderId = 1;
	final Map<Integer, WebGLBuffer> buffers = new HashMap<Integer, WebGLBuffer>();
	int nextBufferId = 1;
	final Map<Integer, WebGLFramebuffer> frameBuffers = new HashMap<Integer, WebGLFramebuffer>();
	int nextFrameBufferId = 1;
	final Map<Integer, WebGLRenderbuffer> renderBuffers = new HashMap<Integer, WebGLRenderbuffer>();
	int nextRenderBufferId = 1;
	final Map<Integer, WebGLTexture> textures = new HashMap<Integer, WebGLTexture>();
	int nextTextureId = 1;
	final Map<Integer, Map<Integer, WebGLUniformLocation>> uniforms = new HashMap<Integer, Map<Integer, WebGLUniformLocation>>();
	int nextUniformId = 1;
	int currProgram = 0;
	
	Float32Array floatBuffer = Float32Array.create(2000 * 20);
	Uint16Array shortBuffer = Uint16Array.create(2000 * 6);
	
	final WebGLRenderingContext gl;

	protected GwtGL20 (WebGLRenderingContext gl) {
		this.gl = gl;
		this.gl.pixelStorei(WebGLRenderingContext.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
	}
	
	private void ensureCapacity(FloatBuffer buffer) {
		if(buffer.remaining() > floatBuffer.getLength()) {
			floatBuffer = Float32Array.create(buffer.remaining());
		}
	}
	
	private void ensureCapacity(ShortBuffer buffer) {
		if(buffer.remaining() > shortBuffer.getLength()) {
			shortBuffer = Uint16Array.create(buffer.remaining());
		}
	}
	
	public Float32Array copy(FloatBuffer buffer) {
		ensureCapacity(buffer);
		float[] array = buffer.array();
		for(int i = buffer.position(), j = 0; i < buffer.limit(); i++, j++) {
			floatBuffer.set(j, array[i]);
		}
		return floatBuffer.subarray(0, buffer.remaining());
	}
	
	public Uint16Array copy(ShortBuffer buffer) {
		ensureCapacity(buffer);
		short[] array = buffer.array();
		for(int i = buffer.position(), j = 0; i < buffer.limit(); i++, j++) {
			shortBuffer.set(j, array[i]);
		}
		return shortBuffer.subarray(0,  buffer.remaining());
	}
	
	private int allocateUniformLocationId(int program, WebGLUniformLocation location) {
		Map<Integer, WebGLUniformLocation> progUniforms = uniforms.get(program);
		if(progUniforms == null) {
			progUniforms = new HashMap<Integer, WebGLUniformLocation>();
			uniforms.put(program, progUniforms);
		}
		// FIXME check if uniform already stored.
		int id = nextUniformId++;
		progUniforms.put(id, location);
		return id;
	}
	
	private WebGLUniformLocation getUniformLocation (int location) {
		return uniforms.get(currProgram).get(location);
	}
	
	private int allocateShaderId(WebGLShader shader) {
		int id = nextShaderId++;
		shaders.put(id, shader);
		return id;
	}
{'fraction_non_alphanumeric': 0.14298601714027967, 'fraction_numerical': 0.013531799729364006, 'mean_word_length': 5.366834170854271, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6080337', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2659, 'n_words': 677}