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

import org.lwjgl.opengl.GL41;

import speiger.src.coreengine.rendering.shader.uniform.IAutoUniform;
import speiger.src.coreengine.rendering.shader.uniform.Uniform;
import speiger.src.coreengine.rendering.utils.AllocationTracker;
import speiger.src.coreengine.rendering.utils.GLStateTracker;

public class TextureUniform extends Uniform implements IAutoUniform {
	final int unit;
	int value;
	
	public TextureUniform(String name, int unit) {
		super(name);
		this.unit = unit;
	}
	
	public TextureUniform set(int id) {
		if(this.value != id) {
			this.value = id;
			update();
		}
		return this;
	}
	
	@Override
	protected void processChanges(int programId, int location) {
		GL41.glProgramUniform1i(programId, location, value);
		AllocationTracker.INSTANCE.addGPUBytes(4L);
	}
	
	@Override
	public void bind(int programId) {
		GLStateTracker.TEXTURES.bind(unit, value);
	}
}
