+ State &state = get_state();
+
+ if(tex)
+ {
+ if(ResourceManager *res_mgr = tex->get_manager())
+ res_mgr->resource_used(*tex);
+ if(!tex->is_loaded())
+ tex = &placeholder_texture;
+ }
+
+ if(texture_stack.size()>state.texture_count)
+ {
+ BoundTexture &bt = texture_stack[state.texture_count];
+ if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
+ {
+ ++state.texture_count;
+ return;
+ }
+ else
+ flush_textures();
+ }
+
+ for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
+ if((--i)->tag==tag)
+ {
+ i->replaced = texture_stack.size();
+ break;
+ }
+
+ texture_stack.emplace_back();
+ BoundTexture &bound_tex = texture_stack.back();
+ bound_tex.tag = tag;
+ bound_tex.texture = tex;
+ bound_tex.sampler = samp;
+ bound_tex.level = level;
+ state.texture_count = texture_stack.size();