]> git.tdb.fi Git - libs/gl.git/blob - source/materials/renderpass.cpp
Overhaul texture management in rendering classes
[libs/gl.git] / source / materials / renderpass.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/format.h>
5 #include "error.h"
6 #include "renderpass.h"
7 #include "program.h"
8 #include "programdata.h"
9 #include "renderer.h"
10 #include "texture.h"
11 #include "texture2d.h"
12 #include "texturing.h"
13 #include "uniform.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GL {
19
20 RenderPass::RenderPass():
21         shprog(0),
22         shprog_from_material(false),
23         shdata(0),
24         material(0),
25         back_faces(false)
26 { }
27
28 void RenderPass::finalize_material(DataFile::Collection *coll)
29 {
30         maybe_create_material_shader(coll);
31         ensure_private_shader_data();
32
33         const Tag *material_texture_tags = material->get_texture_tags();
34         for(const Tag *tag=material_texture_tags; tag->id; ++tag)
35                 set_texture(*tag, material->get_texture(*tag), material->get_sampler());
36 }
37
38 void RenderPass::maybe_create_material_shader(DataFile::Collection *coll)
39 {
40         if(shprog && !shprog_from_material)
41                 return;
42
43         if(coll)
44         {
45                 shprog = material->create_compatible_shader(*coll);
46                 shprog.keep();
47         }
48         else
49                 throw invalid_operation("no collection");
50
51         if(shdata)
52                 shdata = new ProgramData(*shdata, shprog.get());
53
54         shprog_from_material = true;
55 }
56
57 void RenderPass::ensure_private_shader_data()
58 {
59         if(!shprog)
60                 throw invalid_operation("RenderPass::ensure_private_shader_data");
61
62         if(!shdata)
63                 shdata = new ProgramData(shprog.get());
64         else if(shdata.refcount()>1)
65                 shdata = new ProgramData(*shdata);
66 }
67
68 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
69 {
70         shprog = prog;
71         shprog.keep();
72         shprog_from_material = false;
73         shdata = (data ? new ProgramData(*data) : 0);
74         if(material)
75                 finalize_material(0);
76 }
77
78 Tag RenderPass::get_slotted_uniform_tag(Tag slot) const
79 {
80         map<Tag, Tag>::const_iterator i = uniform_slots.find(slot);
81         if(i==uniform_slots.end())
82                 return Tag();
83         return i->second;
84 }
85
86 void RenderPass::set_material(const Material *mat, DataFile::Collection *coll)
87 {
88         material = mat;
89         material.keep();
90         finalize_material(coll);
91 }
92
93 void RenderPass::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
94 {
95         vector<TextureSlot>::iterator i = find_member(textures, tag, &TextureSlot::tag);
96         if(i==textures.end())
97         {
98                 textures.push_back(TextureSlot(tag));
99                 i = textures.end()-1;
100         }
101         i->texture = tex;
102         if(samp)
103                 i->sampler = samp;
104 }
105
106 Tag RenderPass::get_texture_tag(const string &slot) const
107 {
108         vector<TextureSlot>::const_iterator i = find_member(textures, slot, &TextureSlot::slot_name);
109         return (i!=textures.end() ? i->tag : Tag());
110 }
111
112 void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler *samp)
113 {
114         if(!shprog)
115                 throw invalid_operation("RenderPass::set_texture");
116
117         const vector<Program::UniformInfo> &uniforms = shprog->get_uniforms();
118         for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
119                 if(is_image(i->type) && i->binding==static_cast<int>(index))
120                         return set_texture(i->tag, tex, samp);
121
122         if(shdata)
123         {
124                 const vector<Tag> &tags = shdata->get_uniform_tags();
125                 for(vector<Tag>::const_iterator i=tags.begin(); i!=tags.end(); ++i)
126                 {
127                         vector<Program::UniformInfo>::const_iterator j = find_member(uniforms, *i, &Program::UniformInfo::tag);
128                         if(j==uniforms.end() || !is_image(j->type))
129                                 continue;
130                         if(const Uniform1i *uni1i = dynamic_cast<const Uniform1i *>(shdata->find_uniform(*i)))
131                                 if(uni1i->get()==static_cast<int>(index))
132                                         return set_texture(*i, tex, samp);
133                 }
134         }
135 }
136
137 int RenderPass::get_texture_index(const string &n) const
138 {
139         vector<TextureSlot>::const_iterator i = find_member(textures, n, &TextureSlot::slot_name);
140         return (shprog && i!=textures.end() ? shprog->get_uniform_binding(i->tag) : -1);
141 }
142
143 void RenderPass::apply(Renderer &renderer) const
144 {
145         for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
146                 renderer.set_texture(i->tag, i->texture, i->sampler);
147         renderer.set_material(material.get());
148         renderer.set_shader_program(shprog.get(), shdata.get());
149         renderer.set_reverse_winding(back_faces);
150 }
151
152
153 RenderPass::Loader::Loader(RenderPass &p):
154         DataFile::CollectionObjectLoader<RenderPass>(p, 0)
155 {
156         init();
157 }
158
159 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
160         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
161 {
162         init();
163 }
164
165 void RenderPass::Loader::init()
166 {
167         add("shader",   &Loader::shader);
168         add("material", &Loader::material_inline);
169         add("material", &Loader::material);
170         add("material_slot", &RenderPass::material_slot);
171         add("back_faces",&RenderPass::back_faces);
172         add("texture", &Loader::texture);
173         add("uniforms", &Loader::uniforms);
174         add("uniform_slot", &Loader::uniform_slot);
175         add("uniform_slot", &Loader::uniform_slot2);
176
177         // Deprecated
178         add("texunit",  &Loader::texunit);
179         add("texunit",  &Loader::texture);
180         add("texunit",  &Loader::texunit_named);
181 }
182
183 // Temporary compatibility feature
184 string RenderPass::Loader::get_shader_name(const string &n)
185 {
186         if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
187         {
188                 IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
189                 return n+".shader";
190         }
191         return n;
192 }
193
194 void RenderPass::Loader::material_inline()
195 {
196         Material::GenericLoader ldr(coll);
197         load_sub_with(ldr);
198         obj.material = ldr.get_material();
199         obj.finalize_material(coll);
200 }
201
202 void RenderPass::Loader::material(const string &name)
203 {
204         obj.material = &get_collection().get<Material>(name);
205         obj.material.keep();
206         obj.finalize_material(coll);
207 }
208
209 void RenderPass::Loader::shader(const string &n)
210 {
211         obj.shprog = &get_collection().get<Program>(get_shader_name(n));
212         obj.shprog.keep();
213         obj.shprog_from_material = false;
214         if(obj.shdata)
215                 obj.shdata = new ProgramData(*obj.shdata, obj.shprog.get());
216         if(obj.material)
217                 obj.finalize_material(coll);
218 }
219
220 void RenderPass::Loader::texture(const string &n)
221 {
222         vector<TextureSlot>::iterator i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
223         if(i==obj.textures.end())
224         {
225                 obj.textures.push_back(TextureSlot(n));
226                 i = obj.textures.end()-1;
227         }
228         TextureSlot::Loader ldr(*i, n, coll);
229         load_sub_with(ldr);
230 }
231
232 void RenderPass::Loader::texunit(unsigned)
233 {
234         IO::print(IO::cerr, "Warning: specifying textures by unit number is deprecated and may not produce expected results");
235         string name;
236         if(obj.shprog)
237         {
238                 const vector<Program::UniformInfo> &uniforms = obj.shprog->get_uniforms();
239                 for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
240                         if(is_image(i->type) && i->binding>=0)
241                         {
242                                 if(!name.empty())
243                                 {
244                                         name.clear();
245                                         break;
246                                 }
247                                 name = i->name;
248                         }
249         }
250
251         if(name.empty())
252                 throw runtime_error("Could not determine name for texture");
253
254         texture(name);
255 }
256
257 void RenderPass::Loader::texunit_named(unsigned, const string &n)
258 {
259         texture(n);
260 }
261
262 void RenderPass::Loader::uniforms()
263 {
264         obj.ensure_private_shader_data();
265         load_sub(*obj.shdata);
266 }
267
268 void RenderPass::Loader::uniform_slot(const string &name)
269 {
270         uniform_slot2(name, name);
271 }
272
273 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
274 {
275         obj.uniform_slots[slot] = name;
276 }
277
278
279 RenderPass::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
280         CollectionObjectLoader<TextureSlot>(ts, c),
281         auto_slot_name(an)
282 {
283         add("sampler", &TextureSlot::sampler);
284         add("slot", &Loader::slot_auto);
285         add("slot", &TextureSlot::slot_name);
286         add("texture", &TextureSlot::texture);
287 }
288
289 void RenderPass::TextureSlot::Loader::slot_auto()
290 {
291         obj.slot_name = auto_slot_name;
292 }
293
294 } // namespace GL
295 } // namespace Msp