1 #include <msp/core/hash.h>
8 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
13 Texturing::~Texturing()
19 int Texturing::find_free_unit(const string &name_hint) const
21 unsigned max_unit = TexUnit::get_n_units();
22 // Leave some space for effect textures
23 max_unit -= min(max_unit/4, 8U);
24 unsigned initial_unit = (name_hint.empty() ? 0 : hash32(name_hint)%max_unit);
25 unsigned unit = initial_unit;
26 while(get_attached_texture(unit) || get_attached_sampler(unit))
28 unit = (unit+1)%max_unit;
29 if(unit==initial_unit)
36 void Texturing::attach(unsigned attch, const Texture &tex, const Sampler *samp)
38 set_attachment(attch, &tex, samp);
41 void Texturing::attach(unsigned attch, const Sampler &samp)
43 set_attachment(attch, 0, &samp);
46 void Texturing::detach(unsigned attch)
48 set_attachment(attch, 0, 0);
51 void Texturing::set_attachment(unsigned unit, const Texture *tex, const Sampler *samp)
53 if(unit>=TexUnit::get_n_units())
54 throw out_of_range("Texturing::set_attachment");
58 vector<Attachment>::iterator i;
59 for(i=attachments.begin(); (i!=attachments.end() && i->unit<=unit); ++i)
69 attachments.insert(i, Attachment(unit, tex, samp));
75 for(vector<Attachment>::iterator i=attachments.begin(); (i!=attachments.end() && i->unit<=unit); ++i)
80 unbind_attachment(unit);
86 const Texture *Texturing::get_attached_texture(unsigned unit) const
88 for(vector<Attachment>::const_iterator i=attachments.begin(); (i!=attachments.end() && i->unit<=unit); ++i)
94 const Sampler *Texturing::get_attached_sampler(unsigned unit) const
96 for(vector<Attachment>::const_iterator i=attachments.begin(); (i!=attachments.end() && i->unit<=unit); ++i)
102 void Texturing::bind() const
104 const Texturing *old = current();
105 if(set_current(this))
109 vector<Attachment>::const_iterator i = attachments.begin();
110 vector<Attachment>::const_iterator j = old->attachments.begin();
111 while(i!=attachments.end() || j!=old->attachments.end())
113 if(i!=attachments.end() && (j==old->attachments.end() || i->unit<=j->unit))
116 if(j!=old->attachments.end() && j->unit==i->unit)
122 unbind_attachment(j->unit);
129 for(vector<Attachment>::const_iterator i=attachments.begin(); i!=attachments.end(); ++i)
135 void Texturing::bind_attachment(const Attachment &attch) const
138 attch.sampler->bind_to(attch.unit);
140 Sampler::unbind_from(attch.unit);
142 attch.texture->bind_to(attch.unit);
144 Texture::unbind_from(attch.unit);
147 void Texturing::unbind()
149 const Texturing *old = current();
152 for(vector<Attachment>::const_iterator i=old->attachments.begin(); i!=old->attachments.end(); ++i)
153 unbind_attachment(i->unit);
157 void Texturing::unbind_attachment(unsigned unit)
159 Texture::unbind_from(unit);
160 Sampler::unbind_from(unit);
164 Texturing::Attachment::Attachment(unsigned u, const Texture *t, const Sampler *s):