+void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
+{
+ set_texture(tag, -1, tex, samp);
+}
+
+void Renderer::set_texture(Tag tag, int unit, const Texture *tex, const Sampler *samp)
+{
+ if(texture_stack.size()>state->texture_count)
+ {
+ BoundTexture &bt = texture_stack[state->texture_count];
+ if((!tag.id || bt.tag==tag) && (unit<0 || bt.unit==unit) && bt.texture==tex && bt.sampler==samp)
+ {
+ ++state->texture_count;
+ return;
+ }
+ else
+ flush_textures();
+ }
+
+ for(vector<BoundTexture>::iterator i=texture_stack.end(); i!=texture_stack.begin(); )
+ if((--i)->tag==tag && i->unit==unit)
+ {
+ i->replaced = texture_stack.size();
+ break;
+ }
+
+ texture_stack.push_back(BoundTexture());
+ BoundTexture &bound_tex = texture_stack.back();
+ bound_tex.tag = tag;
+ bound_tex.unit = unit;
+ bound_tex.texture = tex;
+ bound_tex.sampler = samp;
+ state->texture_count = texture_stack.size();
+}
+