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