]> git.tdb.fi Git - libs/gl.git/blob - source/materials/rendermethod.cpp
053197589ab1fe6e9d3c83c78261ffc3e27ebc0e
[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         obj.material = ldr.store_object(get_collection(), inline_base_name+".mat");
164         obj.set_material_textures();
165 }
166
167 void RenderMethod::Loader::material(const string &name)
168 {
169         obj.material = &get_collection().get<Material>(name);
170         obj.set_material_textures();
171 }
172
173 void RenderMethod::Loader::shader(const string &n)
174 {
175         obj.shprog = &get_collection().get<Program>(n);
176         obj.shprog_from_material = false;
177         if(obj.shdata)
178                 obj.shdata = new ProgramData(*obj.shdata, obj.shprog);
179 }
180
181 void RenderMethod::Loader::texture(const string &n)
182 {
183         auto i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
184         if(i==obj.textures.end())
185         {
186                 obj.textures.push_back(TextureSlot(n));
187                 i = obj.textures.end()-1;
188         }
189         TextureSlot::Loader ldr(*i, n, coll);
190         load_sub_with(ldr);
191 }
192
193 void RenderMethod::Loader::uniforms()
194 {
195         if(!obj.shprog || obj.shprog_from_material)
196                 throw runtime_error("Shader is required for uniforms");
197         if(!obj.shdata)
198                 obj.shdata = new ProgramData(obj.shprog);
199         else if(obj.shdata.refcount()>1)
200                 obj.shdata = new ProgramData(*obj.shdata);
201         load_sub(*obj.shdata);
202 }
203
204 void RenderMethod::Loader::uniform_slot(const string &name)
205 {
206         uniform_slot2(name, name);
207 }
208
209 void RenderMethod::Loader::uniform_slot2(const string &name, const string &slot)
210 {
211         obj.uniform_slots[slot] = name;
212 }
213
214
215 RenderMethod::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
216         CollectionObjectLoader<TextureSlot>(ts, c),
217         auto_slot_name(an)
218 {
219         add("sampler", &TextureSlot::sampler);
220         add("slot", &Loader::slot_auto);
221         add("slot", &TextureSlot::slot_name);
222         add("texture", &TextureSlot::texture);
223 }
224
225 void RenderMethod::TextureSlot::Loader::slot_auto()
226 {
227         obj.slot_name = auto_slot_name;
228 }
229
230 } // namespace GL
231 } // namespace Msp