package speiger.src.coreengine.platform.graphics.opengl.utils.states;

import java.util.Arrays;

import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL33;
import org.lwjgl.opengl.GL44;

public class SamplerState implements IGLState {
	int[] samplers = new int[GL20.GL_TEXTURE31 - GL20.GL_TEXTURE0];
	
	public SamplerState bind(int sampler) {
		return bind(0, sampler);
	}
	
	public SamplerState bind(int unit, int sampler) {
		if(samplers[unit] != sampler) {
			this.samplers[unit] = sampler;
			GL33.glBindSampler(unit, sampler);
		}
		return this;
	}
	
	public SamplerState unbind(int unit) {
		if(samplers[unit] != 0) {
			samplers[unit] = 0;
			GL33.glBindSampler(unit, 0);
		}
		return this;
	}
	
	public SamplerState unbindAll() {
		setDefault();
		return this;
	}
		
	public int getSamplerId(int unit) {
		return samplers[unit];
	}
	
	@Override
	public void setDefault() {
		GL44.nglBindSamplers(0, samplers.length, 0);
		Arrays.fill(samplers, 0);
	}
	
	@Override
	public void reapply() {
		GL44.glBindSamplers(0, samplers);
	}
}