package speiger.src.coreengine.rendering.shader.uniform.vec;

import java.util.Arrays;

import org.lwjgl.opengl.GL41;

import speiger.src.coreengine.math.vector.matrix.Matrix4f;
import speiger.src.coreengine.rendering.shader.uniform.Uniform;
import speiger.src.coreengine.rendering.utils.AllocationTracker;

public class Matrix4fUniform extends Uniform {
	Matrix4f value;
	
	public Matrix4fUniform(String name, Matrix4f value) {
		super(name);
		this.value = value;
	}
	
	public Matrix4fUniform set(Matrix4f newMat) {
		if(hasChanged(newMat)) {
			value.load(newMat);
			update();
		}
		return this;
	}
	
	protected boolean hasChanged(Matrix4f input) {
		if(value.properties() != input.properties()) return true;
		return !Arrays.equals(value.getData(), input.getData());
	}
	
	@Override
	protected void processChanges(int programId, int location) {
		GL41.glProgramUniformMatrix4fv(programId, location, false, value.getData());
		AllocationTracker.INSTANCE.addGPUBytes(64L);
	}
}
