]> git.tdb.fi Git - libs/gl.git/blob - source/materials/renderpass.cpp
Support compile modes and specialization constants in the test harness
[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 const string &RenderPass::get_slotted_uniform_name(const string &slot) const
110 {
111         map<string, string>::const_iterator i = uniform_slots.find(slot);
112         if(i==uniform_slots.end())
113         {
114                 static string empty;
115                 return empty;
116         }
117         return i->second;
118 }
119
120 void RenderPass::set_material(const Material *mat)
121 {
122         material = mat;
123         material.keep();
124         finalize_material(0);
125 }
126
127 void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler *samp)
128 {
129         if(!texturing)
130                 texturing = new Texturing;
131
132         texturing->attach(index, *tex, (samp ? samp : texturing->get_attached_sampler(index)));
133 }
134
135 int RenderPass::get_texture_index(const string &n) const
136 {
137         map<string, unsigned>::const_iterator i = tex_names.find(n);
138         if(i==tex_names.end())
139                 return -1;
140         return i->second;
141 }
142
143 void RenderPass::apply(Renderer &renderer) const
144 {
145         renderer.set_texturing(texturing);
146         renderer.set_material(material.get());
147         renderer.set_shader_program(shprog.get(), shdata.get());
148         renderer.set_reverse_winding(back_faces);
149 }
150
151
152 RenderPass::Loader::Loader(RenderPass &p):
153         DataFile::CollectionObjectLoader<RenderPass>(p, 0)
154 {
155         init();
156 }
157
158 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
159         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
160 {
161         init();
162 }
163
164 void RenderPass::Loader::init()
165 {
166         add("shader",   &Loader::shader);
167         add("material", &Loader::material_inline);
168         add("material", &Loader::material);
169         add("material_slot", &RenderPass::material_slot);
170         add("back_faces",&RenderPass::back_faces);
171         add("texunit",  &Loader::texunit);
172         add("texunit",  &Loader::texunit_auto);
173         add("texunit",  &Loader::texunit_named);
174         add("uniforms", &Loader::uniforms);
175         add("uniform_slot", &Loader::uniform_slot);
176         add("uniform_slot", &Loader::uniform_slot2);
177 }
178
179 // Temporary compatibility feature
180 string RenderPass::Loader::get_shader_name(const string &n)
181 {
182         if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
183         {
184                 IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
185                 return n+".shader";
186         }
187         return n;
188 }
189
190 void RenderPass::Loader::material_inline()
191 {
192         Material::GenericLoader ldr(coll);
193         load_sub_with(ldr);
194         obj.material = ldr.get_material();
195         obj.finalize_material(coll);
196 }
197
198 void RenderPass::Loader::material(const string &name)
199 {
200         obj.material = &get_collection().get<Material>(name);
201         obj.material.keep();
202         obj.finalize_material(coll);
203 }
204
205 void RenderPass::Loader::shader(const string &n)
206 {
207         obj.shprog = &get_collection().get<Program>(get_shader_name(n));
208         obj.shprog.keep();
209         obj.shprog_from_material = false;
210         if(obj.shdata)
211                 obj.shdata = new ProgramData(*obj.shdata, obj.shprog.get());
212         if(obj.material)
213                 obj.finalize_material(coll);
214 }
215
216 void RenderPass::Loader::texunit(unsigned i)
217 {
218         if(!obj.texturing)
219                 obj.texturing = new Texturing;
220         TextureLoader ldr(*obj.texturing, i, coll);
221         load_sub_with(ldr);
222 }
223
224 void RenderPass::Loader::texunit_auto(const string &n)
225 {
226         if(!obj.texturing)
227                 obj.texturing = new Texturing;
228         int i = obj.texturing->find_free_unit(n);
229         if(i<0)
230                 throw runtime_error("no free texunit");
231         texunit_named(i, n);
232 }
233
234 void RenderPass::Loader::texunit_named(unsigned i, const string &n)
235 {
236         texunit(i);
237         obj.tex_names[n] = i;
238         obj.ensure_private_shader_data();
239         obj.shdata->uniform(n, static_cast<int>(i));
240 }
241
242 void RenderPass::Loader::uniforms()
243 {
244         obj.ensure_private_shader_data();
245         load_sub(*obj.shdata);
246 }
247
248 void RenderPass::Loader::uniform_slot(const string &name)
249 {
250         uniform_slot2(name, name);
251 }
252
253 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
254 {
255         obj.uniform_slots[slot] = name;
256 }
257
258
259 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
260         DataFile::CollectionObjectLoader<Texturing>(t, c),
261         index(i),
262         tex(0),
263         samp(0)
264 {
265         add("sampler",   &TextureLoader::sampler);
266         add("texture",   &TextureLoader::texture);
267 }
268
269 void RenderPass::TextureLoader::finish()
270 {
271         if(tex)
272                 obj.attach(index, *tex, samp);
273         else if(samp)
274                 obj.attach(index, *samp);
275 }
276
277 void RenderPass::TextureLoader::sampler(const string &name)
278 {
279         samp = &get_collection().get<Sampler>(name);
280 }
281
282 void RenderPass::TextureLoader::texture(const string &name)
283 {
284         tex = &get_collection().get<Texture>(name);
285 }
286
287 } // namespace GL
288 } // namespace Msp