]> git.tdb.fi Git - libs/gl.git/blob - source/materials/rendermethod.cpp
Remove collection-less constructor overloads from most loaders
[libs/gl.git] / source / materials / rendermethod.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/strings/format.h>
4 #include "error.h"
5 #include "rendermethod.h"
6 #include "program.h"
7 #include "programdata.h"
8 #include "renderer.h"
9 #include "texture.h"
10 #include "texture2d.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 RenderMethod::RenderMethod():
18         shprog(0),
19         shprog_from_material(false),
20         shdata(0),
21         material(0),
22         face_cull(CULL_BACK),
23         receive_shadows(false),
24         image_based_lighting(false)
25 { }
26
27 void RenderMethod::set_material_textures()
28 {
29         const Tag *material_texture_tags = material->get_texture_tags();
30         for(const Tag *tag=material_texture_tags; tag->id; ++tag)
31                 set_texture(*tag, material->get_texture(*tag), material->get_sampler(*tag));
32 }
33
34 void RenderMethod::maybe_create_material_shader()
35 {
36         if(shprog && !shprog_from_material)
37                 return;
38
39         map<string, int> extra_spec;
40         if(receive_shadows)
41                 extra_spec["use_shadow_map"] = true;
42         if(image_based_lighting)
43                 extra_spec["use_image_based_lighting"] = true;
44
45         shprog = material->create_compatible_shader(extra_spec);
46
47         if(shdata)
48                 shdata = new ProgramData(*shdata, shprog);
49
50         shprog_from_material = true;
51 }
52
53 void RenderMethod::set_shader_program(const Program *prog, const ProgramData *data)
54 {
55         shprog = prog;
56         shprog_from_material = false;
57         shdata = (data ? new ProgramData(*data) : 0);
58 }
59
60 Tag RenderMethod::get_slotted_uniform_tag(Tag slot) const
61 {
62         auto i = uniform_slots.find(slot);
63         if(i==uniform_slots.end())
64                 return Tag();
65         return i->second;
66 }
67
68 void RenderMethod::set_material(const Material *mat)
69 {
70         material = mat;
71         maybe_create_material_shader();
72         set_material_textures();
73 }
74
75 void RenderMethod::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
76 {
77         auto i = find_member(textures, tag, &TextureSlot::tag);
78         if(i==textures.end())
79         {
80                 textures.push_back(TextureSlot(tag));
81                 i = textures.end()-1;
82         }
83         i->texture = tex;
84         if(samp)
85                 i->sampler = samp;
86 }
87
88 Tag RenderMethod::get_texture_tag(const string &slot) const
89 {
90         auto i = find_member(textures, slot, &TextureSlot::slot_name);
91         return (i!=textures.end() ? i->tag : Tag());
92 }
93
94 void RenderMethod::set_face_cull(CullMode fc)
95 {
96         face_cull = fc;
97 }
98
99 void RenderMethod::set_receive_shadows(bool rs)
100 {
101         receive_shadows = rs;
102 }
103
104 void RenderMethod::apply(Renderer &renderer) const
105 {
106         for(const TextureSlot &t: textures)
107                 renderer.set_texture(t.tag, t.texture, t.sampler);
108         renderer.set_shader_program(shprog, shdata.get());
109         if(material)
110                 renderer.add_shader_data(material->get_shader_data());
111         renderer.set_face_cull(face_cull);
112 }
113
114 void RenderMethod::set_debug_name(const string &name)
115 {
116 #ifdef DEBUG
117         if(shdata.refcount()==1)
118                 shdata->set_debug_name(name+" [UBO]");
119 #else
120         (void)name;
121 #endif
122 }
123
124
125 DataFile::Loader::ActionMap RenderMethod::Loader::shared_actions;
126
127 RenderMethod::Loader::Loader(RenderMethod &p, Collection &c):
128         DataFile::CollectionObjectLoader<RenderMethod>(p, &c)
129 {
130         set_actions(shared_actions);
131 }
132
133 void RenderMethod::Loader::init_actions()
134 {
135         add("face_cull", &RenderMethod::face_cull);
136         add("shader",   &Loader::shader);
137         add("image_based_lighting", &RenderMethod::image_based_lighting);
138         add("material", &Loader::material_inline);
139         add("material", &Loader::material);
140         add("material_slot", &RenderMethod::material_slot);
141         add("receive_shadows", &RenderMethod::receive_shadows);
142         add("texture", &Loader::texture);
143         add("uniforms", &Loader::uniforms);
144         add("uniform_slot", &Loader::uniform_slot);
145         add("uniform_slot", &Loader::uniform_slot2);
146 }
147
148 void RenderMethod::Loader::set_inline_base_name(const string &n)
149 {
150         inline_base_name = n;
151 }
152
153 void RenderMethod::Loader::finish()
154 {
155         if(obj.material)
156                 obj.maybe_create_material_shader();
157 }
158
159 void RenderMethod::Loader::material_inline()
160 {
161         Material::GenericLoader ldr(get_collection());
162         load_sub_with(ldr);
163         RefPtr<Material> mat = ldr.get_object();
164         get_collection().add(inline_base_name+".mat", mat.get());
165         obj.material = mat.release();
166         obj.set_material_textures();
167 }
168
169 void RenderMethod::Loader::material(const string &name)
170 {
171         obj.material = &get_collection().get<Material>(name);
172         obj.set_material_textures();
173 }
174
175 void RenderMethod::Loader::shader(const string &n)
176 {
177         obj.shprog = &get_collection().get<Program>(n);
178         obj.shprog_from_material = false;
179         if(obj.shdata)
180                 obj.shdata = new ProgramData(*obj.shdata, obj.shprog);
181 }
182
183 void RenderMethod::Loader::texture(const string &n)
184 {
185         auto i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
186         if(i==obj.textures.end())
187         {
188                 obj.textures.push_back(TextureSlot(n));
189                 i = obj.textures.end()-1;
190         }
191         TextureSlot::Loader ldr(*i, n, coll);
192         load_sub_with(ldr);
193 }
194
195 void RenderMethod::Loader::uniforms()
196 {
197         if(!obj.shprog || obj.shprog_from_material)
198                 throw runtime_error("Shader is required for uniforms");
199         if(!obj.shdata)
200                 obj.shdata = new ProgramData(obj.shprog);
201         else if(obj.shdata.refcount()>1)
202                 obj.shdata = new ProgramData(*obj.shdata);
203         load_sub(*obj.shdata);
204 }
205
206 void RenderMethod::Loader::uniform_slot(const string &name)
207 {
208         uniform_slot2(name, name);
209 }
210
211 void RenderMethod::Loader::uniform_slot2(const string &name, const string &slot)
212 {
213         obj.uniform_slots[slot] = name;
214 }
215
216
217 RenderMethod::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
218         CollectionObjectLoader<TextureSlot>(ts, c),
219         auto_slot_name(an)
220 {
221         add("sampler", &TextureSlot::sampler);
222         add("slot", &Loader::slot_auto);
223         add("slot", &TextureSlot::slot_name);
224         add("texture", &TextureSlot::texture);
225 }
226
227 void RenderMethod::TextureSlot::Loader::slot_auto()
228 {
229         obj.slot_name = auto_slot_name;
230 }
231
232 } // namespace GL
233 } // namespace Msp