<commit_before>/*
 * Copyright 2013, <NAME>. All rights reserved.
 * If you intend to use, modify or redistribute this file contact <EMAIL>
 */

package com.elfeck.ephemeral.glContext;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL30.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class EPHVertexArrayObject {

	private static EPHShaderProgramPool shaderProgramPool;

	private boolean dead;
	private int mode, size, usage, handle, uniformKey;
	private int[] viewPortRect, scissorRect;
	private List<EPHVaoEntry> entries;
	private EPHVertexBufferObject vbo;
	private EPHIndexBufferObject ibo;

	public EPHVertexArrayObject(int mode, int usage, int[] viewPortRect, int[] scissorRect, List<EPHVertexAttribute> vertexAttributes) {
		dead = false;
		this.mode = mode;
		this.size = 0;
		this.usage = usage;
		this.viewPortRect = viewPortRect;
		this.scissorRect = scissorRect;
		handle = -1;
		uniformKey = 0;
		entries = new ArrayList<EPHVaoEntry>();
		vbo = new EPHVertexBufferObject(vertexAttributes);
		ibo = new EPHIndexBufferObject();
	}

	public EPHVertexArrayObject(int[] viewPortRect, int[] scissorRect, List<EPHVertexAttribute> vertexAttributes) {
		this(EPHRenderUtils.TYPE_TRIANGLES, EPHRenderUtils.MODE_STATIC_DRAW, viewPortRect, scissorRect, vertexAttributes);
	}

	public EPHVertexArrayObject(List<EPHVertexAttribute> vertexAttributes) {
		this(EPHRenderUtils.TYPE_TRIANGLES, EPHRenderUtils.MODE_STATIC_DRAW,
				EPHRenderContext.getWindowDimensions(), EPHRenderContext.getWindowDimensions(), vertexAttributes);
	}

	private void glInit() {
		if (handle < 0) handle = glGenVertexArrays();
		glUpdateVbo();
		glUpdateIbo();
		glBindVertexArray(handle);
		vbo.glBind();
		ibo.glBind();
		glBindVertexArray(0);
	}

	private void glUpdateVbo() {
		vbo.glInit(usage);
	}

	private void glUpdateIbo() {
		ibo.glInit(usage);
	}

	private Map<Integer, String> vertexAttributesToMap(List<EPHVertexAttribute> vertexAttributes) {
		Map<Integer, String> names = new HashMap<Integer, String>();
		for (EPHVertexAttribute va : vertexAttributes) {
			names.put(va.index, va.name);
		}
		return names;
	}

	private boolean hasVisibleEntries() {
		boolean hasVisibleEntries = false;
		for (EPHVaoEntry entry : entries) {
			if (entry.isVisible()) {
				hasVisibleEntries = true;
				break;
			}
		}
		return hasVisibleEntries;
	}

	public synchronized void glRender() {
		if (handle < 0) glInit();
		if (!vbo.isUpdated()) glUpdateVbo();
		if (!ibo.isUpdated()) glUpdateIbo();
		if (size > 0 && hasVisibleEntries()) {
			glBindVertexArray(handle);
			glViewport(viewPortRect[0], viewPortRect[1], viewPortRect[2], viewPortRect[3]);
			glScissor(scissorRect[0], scissorRect[1], scissorRect[2], scissorRect[3]);
			EPHShaderProgram currentProgram = null;
			EPHVaoEntry current, next;
			boolean bindRequ = true;
			for (int i = 0; i < entries.size(); i++) {
				current = entries.get(i);
				if (!current.visible) continue;
				next = i < entries.size() - 1 ? entries.get(i + 1) : null;
				currentProgram = shaderProgramPool.getShaderProgram(current.programKey);
				if (!currentProgram.isLinked()) currentProgram.glAttachAndLinkProgram(vertexAttributesToMap(vbo.getVertexAttributes()));
				if (bindRequ) currentProgram.glBind();
				currentProgram.glUseUniforms(current.uniformKey);
				glDrawElements(mode, current.iboUpperBound - current.iboLowerBound + 1, GL_UNSIGNED_INT, current.iboLowerBound * 4);
				bindRequ = next == null ? true : current.programKey.equals(next.programKey) ? false : true;
				if (bindRequ) currentProgram.glUnbind();
			}
			glBindVertexArray(0);
		}
	}

	public void glDispose() {
		vbo.glDispose();
		ibo.glDispose();
		glDeleteBuffers(handle);
	}

	public synchronized EPHVaoEntry addData(List<Float> vertexValues, List<Integer> indices, String programKey) {
		EPHVaoEntry entry = new EPHVaoEntry();
		entry.programKey = programKey;
		return addData(vertexValues, indices, entry);
	}

	public synchronized EPHVaoEntry addData(List<Float> vertexValues, List<Integer> indices, EPHVaoEntry entry) {
		if (!EPHRenderContext.isInitialized()) {
			synchronized (EPHRenderContext.glInitMonitor) {
				try {
					EPHRenderContext.glInitMonitor.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		entry.vboLowerBound = vbo.getCurrentIndex();
		entry.vboUpperBound = vbo.getCurrentIndex() + vertexValues.size() - 1;
		entry.iboLowerBound = ibo.getCurrentIndex();
		entry.iboUpperBound = ibo.getCurrentIndex() + indices.size() - 1;
		entry.uniformKey = uniformKey++;
		entry.utb = shaderProgramPool.getShaderProgram(entry.programKey).getUniformTemplateBuffer();
		ibo.addData(indices, vbo.addData(vertexValues));
		size += indices.size();
		entries.add(entry);
		return entry;
	}

	public synchronized void removeData(EPHVaoEntry entry) {
		ibo.removeData(entry.iboLowerBound, entry.iboUpperBound, vbo.removeData(entry.vboLowerBound, entry.vboUpperBound));
		size -= entry.iboUpperBound - entry.iboLowerBound + 1;
		int deletedVertexValues = entry.vboUpperBound - entry.vboLowerBound + 1;
		int deletedIndices = entry.iboUpperBound - entry.iboLowerBound + 1;
		for (int i = entries.indexOf(entry) + 1; i < entries.size(); i++) {
			EPHVaoEntry currentEntry = entries.get(i);
			currentEntry.vboLowerBound -= deletedVertexValues;
			currentEntry.vboUpperBound -= deletedVertexValues;
			currentEntry.iboLowerBound -= deletedIndices;
			currentEntry.iboUpperBound -= deletedIndices;
		}
		entries.remove(entry);
	}

	public synchronized void setViewportRect(int[] bounds) {
		viewPortRect = bounds;
	}

	public synchronized void setScissorRect(int[] bounds) {
		scissorRect = bounds;
	}

	public synchronized void resetViewPortAndScissor() {
		viewPortRect = EPHRenderContext.getWindowDimensions();
		scissorRect = EPHRenderContext.getWindowDimensions();
	}
<commit_msg>Major git messup happend cause by eclipse crash. Should be all fixed now and added subData updating. Also: basic mouse input added<commit_after>/*
 * Copyright 2013, <NAME>. All rights reserved.
 * If you intend to use, modify or redistribute this file contact <EMAIL>
 */

package com.elfeck.ephemeral.glContext;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL30.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class EPHVertexArrayObject {

	private static EPHShaderProgramPool shaderProgramPool;

	private boolean dead;
	private int mode, size, usage, handle, uniformKey;
	private int[] viewPortRect, scissorRect;
	private List<EPHVaoEntry> entries;
	private EPHVertexBufferObject vbo;
	private EPHIndexBufferObject ibo;

	public EPHVertexArrayObject(int mode, int usage, int[] viewPortRect, int[] scissorRect, List<EPHVertexAttribute> vertexAttributes) {
		dead = false;
		this.mode = mode;
		this.size = 0;
		this.usage = usage;
		this.viewPortRect = viewPortRect;
		this.scissorRect = scissorRect;
		handle = -1;
		uniformKey = 0;
		entries = new ArrayList<EPHVaoEntry>();
		vbo = new EPHVertexBufferObject(vertexAttributes);
		ibo = new EPHIndexBufferObject();
	}

	public EPHVertexArrayObject(int[] viewPortRect, int[] scissorRect, List<EPHVertexAttribute> vertexAttributes) {
		this(EPHRenderUtils.TYPE_TRIANGLES, EPHRenderUtils.MODE_STATIC_DRAW, viewPortRect, scissorRect, vertexAttributes);
	}

	public EPHVertexArrayObject(List<EPHVertexAttribute> vertexAttributes) {
		this(EPHRenderUtils.TYPE_TRIANGLES, EPHRenderUtils.MODE_STATIC_DRAW,
				EPHRenderContext.getWindowDimensions(), EPHRenderContext.getWindowDimensions(), vertexAttributes);
	}

	private void glInit() {
		if (handle < 0) handle = glGenVertexArrays();
		glUpdateVbo();
		glUpdateIbo();
		glBindVertexArray(handle);
		vbo.glBind();
		ibo.glBind();
		glBindVertexArray(0);
	}

	private void glUpdateVbo() {
		vbo.glInit(usage);
	}

	private void glUpdateIbo() {
		ibo.glInit(usage);
	}

	private Map<Integer, String> vertexAttributesToMap(List<EPHVertexAttribute> vertexAttributes) {
		Map<Integer, String> names = new HashMap<Integer, String>();
		for (EPHVertexAttribute va : vertexAttributes) {
			names.put(va.index, va.name);
		}
		return names;
	}

	private boolean hasVisibleEntries() {
		boolean hasVisibleEntries = false;
		for (EPHVaoEntry entry : entries) {
			if (entry.isVisible()) {
				hasVisibleEntries = true;
				break;
			}
		}
		return hasVisibleEntries;
	}

	public synchronized void glRender() {
		if (handle < 0) glInit();
		if (!vbo.isUpdated()) glUpdateVbo();
		if (!ibo.isUpdated()) glUpdateIbo();
		if (size > 0 && hasVisibleEntries()) {
			glBindVertexArray(handle);
			glViewport(viewPortRect[0], viewPortRect[1], viewPortRect[2], viewPortRect[3]);
			glScissor(scissorRect[0], scissorRect[1], scissorRect[2], scissorRect[3]);
			EPHShaderProgram currentProgram = null;
			EPHVaoEntry current, next;
			boolean bindRequ = true;
			for (int i = 0; i < entries.size(); i++) {
				current = entries.get(i);
				if (!current.visible) continue;
				next = i < entries.size() - 1 ? entries.get(i + 1) : null;
				currentProgram = shaderProgramPool.getShaderProgram(current.programKey);
				if (!currentProgram.isLinked()) currentProgram.glAttachAndLinkProgram(vertexAttributesToMap(vbo.getVertexAttributes()));
				if (bindRequ) currentProgram.glBind();
				currentProgram.glUseUniforms(current.uniformKey);
				glDrawElements(mode, current.iboUpperBound - current.iboLowerBound + 1, GL_UNSIGNED_INT, current.iboLowerBound * 4);
				bindRequ = next == null ? true : current.programKey.equals(next.programKey) ? false : true;
				if (bindRequ) currentProgram.glUnbind();
			}
			glBindVertexArray(0);
		}
	}

	public void glDispose() {
		vbo.glDispose();
		ibo.glDispose();
		glDeleteBuffers(handle);
	}

	public synchronized EPHVaoEntry addData(List<Float> vertexValues, List<Integer> indices, String programKey) {
		EPHVaoEntry entry = new EPHVaoEntry();
		entry.programKey = programKey;
		return addData(vertexValues, indices, entry);
	}

	public synchronized EPHVaoEntry addData(List<Float> vertexValues, List<Integer> indices, EPHVaoEntry entry) {
		if (!EPHRenderContext.isInitialized()) {
			synchronized (EPHRenderContext.glInitMonitor) {
				try {
					EPHRenderContext.glInitMonitor.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		entry.vboLowerBound = vbo.getCurrentIndex();
		entry.vboUpperBound = vbo.getCurrentIndex() + vertexValues.size() - 1;
		entry.iboLowerBound = ibo.getCurrentIndex();
		entry.iboUpperBound = ibo.getCurrentIndex() + indices.size() - 1;
		entry.uniformKey = uniformKey++;
		entry.utb = shaderProgramPool.getShaderProgram(entry.programKey).getUniformTemplateBuffer();
		ibo.addData(indices, vbo.addData(vertexValues));
		size += indices.size();
		entries.add(entry);
		return entry;
	}

	public synchronized void removeData(EPHVaoEntry entry) {
		ibo.removeData(entry.iboLowerBound, entry.iboUpperBound, vbo.removeData(entry.vboLowerBound, entry.vboUpperBound));
		size -= entry.iboUpperBound - entry.iboLowerBound + 1;
		int deletedVertexValues = entry.vboUpperBound - entry.vboLowerBound + 1;
		int deletedIndices = entry.iboUpperBound - entry.iboLowerBound + 1;
		for (int i = entries.indexOf(entry) + 1; i < entries.size(); i++) {
			EPHVaoEntry currentEntry = entries.get(i);
			currentEntry.vboLowerBound -= deletedVertexValues;
			currentEntry.vboUpperBound -= deletedVertexValues;
			currentEntry.iboLowerBound -= deletedIndices;
			currentEntry.iboUpperBound -= deletedIndices;
		}
		entries.remove(entry);
	}

	public synchronized void updateVbo(EPHVaoEntry entry, int subLower, int subUpper, List<Float> vertexValues) {
		if (subLower < 0) subLower = 0;
		if (subUpper < 0) subUpper = entry.vboUpperBound - entry.vboLowerBound + 1;
		vbo.updateData(entry.vboLowerBound + subLower, entry.vboLowerBound + subUpper, vertexValues);
	}

	public synchronized void updateIbo(EPHVaoEntry entry, int subLower, int subUpper, List<Integer> indices) {
		if (subLower < 0) subLower = 0;
		if (subUpper < 0) subUpper = entry.iboUpperBound - entry.iboLowerBound + 1;
		ibo.updateData(entry.vboLowerBound + subLower, entry.vboLowerBound + subUpper, indices);
	}
	public synchronized void setViewportRect(int[] bounds) {
		viewPortRect = bounds;
	}

	public synchronized void setScissorRect(int[] bounds) {
		scissorRect = bounds;
	}

	public synchronized void resetViewPortAndScissor() {
		viewPortRect = EPHRenderContext.getWindowDimensions();
		scissorRect = EPHRenderContext.getWindowDimensions();
	}

{'fraction_non_alphanumeric': 0.11435119329856172, 'fraction_numerical': 0.006480164374901217, 'mean_word_length': 5.0463449593884375, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 37, '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': '5525537', 'n_tokens_mistral': 4523, 'n_tokens_neox': 4080, 'n_words': 869}