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