[Solved] Multiple render passes, different programs, texturing problems

I have two different programs, and four different shaders:
3d program -> 3d vertex shader, 3d fragment shader
2d program -> 2d vertex shader, 2d fragment shader

I render using the 3d program first, 2d second. I’m using 4 textures for the 3D scene and 5 for the 2D overlay (3 of the textures are shared). This results in the last texture from the 2d overlay to use the texture at index zero instead. However, if I have the same amount of textures (4 & 4, 3 & 3, etc…) for both passes then the problem looks like it goes away (get the expected results).

Here’s the sampler usage in the 2d & 3d programs:


uniform sampler2D diffuse_maps[16];

color += texture(diffuse_maps[int(tex_index)], tex_coord);
}

Note: not sure if it’s relevant, but if I add another uniform to the 3d vertex shader openGL throws a 1282: invalid operation error when I try to bind my textures:

	private void bindTextures(GL3 gl)
	{
		for(int i = 0; i < m_textures.size(); i++)
		{
			m_shader.bindTexture(gl, i, m_textures.get(i).m_id);
			m_shader.setUniform1i(gl, "diffuse_maps["+i+"]", i);
		}
	}

bindTexture and setUniform


	
	public void bindTexture(GL3 gl, int unit, int id)
	{
		gl.glActiveTexture(GL3.GL_TEXTURE0 + unit);
                //ERROR!!!!!!!!!!!!!!!!!!!! occurs on the line below
		gl.glBindTexture(GL3.GL_TEXTURE_2D, id);//<---ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                //ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	}
	
	public void setUniform1i(GL3 gl, String name, int x)
	{
		gl.glUniform1i(wrapLocation(m_uniforms, name), x);
	}
	
	private Integer wrapLocation(HashMap<String, Integer> map, String name)
	{
		Integer location = map.get(name);
		if(location == null)
		{
			Logger.log(WARNING, "Could not bind shader var: " + name, getName());
			return -1;
		}
		return location;
	}