]> git.tdb.fi Git - libs/gl.git/blob - source/materials/rendermethod.cpp
Rename RenderPass to RenderMethod
[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):
128         DataFile::CollectionObjectLoader<RenderMethod>(p, 0)
129 {
130         set_actions(shared_actions);
131 }
132
133 RenderMethod::Loader::Loader(RenderMethod &p, Collection &c):
134         DataFile::CollectionObjectLoader<RenderMethod>(p, &c)
135 {
136         set_actions(shared_actions);
137 }
138
139 void RenderMethod::Loader::init_actions()
140 {
141         add("face_cull", &RenderMethod::face_cull);
142         add("shader",   &Loader::shader);
143         add("image_based_lighting", &RenderMethod::image_based_lighting);
144         add("material", &Loader::material_inline);
145         add("material", &Loader::material);
146         add("material_slot", &RenderMethod::material_slot);
147         add("receive_shadows", &RenderMethod::receive_shadows);
148         add("texture", &Loader::texture);
149         add("uniforms", &Loader::uniforms);
150         add("uniform_slot", &Loader::uniform_slot);
151         add("uniform_slot", &Loader::uniform_slot2);
152 }
153
154 void RenderMethod::Loader::set_inline_base_name(const string &n)
155 {
156         inline_base_name = n;
157 }
158
159 void RenderMethod::Loader::finish()
160 {
161         if(obj.material)
162                 obj.maybe_create_material_shader();
163 }
164
165 void RenderMethod::Loader::material_inline()
166 {
167         Material::GenericLoader ldr(coll);
168         load_sub_with(ldr);
169         RefPtr<Material> mat = ldr.get_material();
170         get_collection().add(inline_base_name+".mat", mat.get());
171         obj.material = mat.release();
172         obj.set_material_textures();
173 }
174
175 void RenderMethod::Loader::material(const string &name)
176 {
177         obj.material = &get_collection().get<Material>(name);
178         obj.set_material_textures();
179 }
180
181 void RenderMethod::Loader::shader(const string &n)
182 {
183         obj.shprog = &get_collection().get<Program>(n);
184         obj.shprog_from_material = false;
185         if(obj.shdata)
186                 obj.shdata = new ProgramData(*obj.shdata, obj.shprog);
187 }
188
189 void RenderMethod::Loader::texture(const string &n)
190 {
191         auto i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
192         if(i==obj.textures.end())
193         {
194                 obj.textures.push_back(TextureSlot(n));
195                 i = obj.textures.end()-1;
196         }
197         TextureSlot::Loader ldr(*i, n, coll);
198         load_sub_with(ldr);
199 }
200
201 void RenderMethod::Loader::uniforms()
202 {
203         if(!obj.shprog || obj.shprog_from_material)
204                 throw runtime_error("Shader is required for uniforms");
205         if(!obj.shdata)
206                 obj.shdata = new ProgramData(obj.shprog);
207         else if(obj.shdata.refcount()>1)
208                 obj.shdata = new ProgramData(*obj.shdata);
209         load_sub(*obj.shdata);
210 }
211
212 void RenderMethod::Loader::uniform_slot(const string &name)
213 {
214         uniform_slot2(name, name);
215 }
216
217 void RenderMethod::Loader::uniform_slot2(const string &name, const string &slot)
218 {
219         obj.uniform_slots[slot] = name;
220 }
221
222
223 RenderMethod::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
224         CollectionObjectLoader<TextureSlot>(ts, c),
225         auto_slot_name(an)
226 {
227         add("sampler", &TextureSlot::sampler);
228         add("slot", &Loader::slot_auto);
229         add("slot", &TextureSlot::slot_name);
230         add("texture", &TextureSlot::texture);
231 }
232
233 void RenderMethod::TextureSlot::Loader::slot_auto()
234 {
235         obj.slot_name = auto_slot_name;
236 }
237
238 } // namespace GL
239 } // namespace Msp