]> git.tdb.fi Git - libs/gl.git/blob - source/builders/sequencebuilder.cpp
Mark constant data as const
[libs/gl.git] / source / builders / sequencebuilder.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/maputils.h>
3 #include <msp/strings/format.h>
4 #include "deviceinfo.h"
5 #include "error.h"
6 #include "sequence.h"
7 #include "sequencebuilder.h"
8 #include "sequencetemplate.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 SequenceBuilder::SequenceBuilder(const SequenceTemplate &t):
16         tmpl(t)
17 {
18         for(const SequenceTemplate::Step &s: tmpl.get_steps())
19                 renderables[s.slot_name] = s.default_renderable;
20         for(const SequenceTemplate::PostProcessor &p: tmpl.get_postprocessors())
21                 if(!p.slot_name.empty())
22                         postprocessors[p.slot_name] = 0;
23 }
24
25 void SequenceBuilder::set_renderable(const string &name, Renderable &rend)
26 {
27         get_item(renderables, name) = &rend;
28 }
29
30 void SequenceBuilder::set_postprocessor(const string &name, PostProcessor &pproc)
31 {
32         get_item(postprocessors, name) = &pproc;
33 }
34
35 void SequenceBuilder::set_debug_name(const string &name)
36 {
37 #ifdef DEBUG
38         debug_name = name;
39 #else
40         (void)name;
41 #endif
42 }
43
44 void SequenceBuilder::build(Sequence &sequence) const
45 {
46 #ifdef DEBUG
47         if(!debug_name.empty())
48                 sequence.set_debug_name(debug_name);
49 #endif
50
51         for(const SequenceTemplate::Step &s: tmpl.get_steps())
52         {
53                 Renderable *renderable = get_item(renderables, s.slot_name);
54                 if(!renderable)
55                         continue;
56
57                 Sequence::Step &step = sequence.add_step(s.tag, *renderable);
58                 step.set_blend(s.blend);
59                 step.set_depth_test(s.depth_test);
60                 step.set_stencil_test(s.stencil_test);
61                 step.set_lighting(s.lighting);
62         }
63
64 #ifdef DEBUG
65         unsigned index = 0;
66 #endif
67         for(const SequenceTemplate::PostProcessor &p: tmpl.get_postprocessors())
68         {
69                 PostProcessor *proc = 0;
70                 if(!p.slot_name.empty())
71                         proc = get_item(postprocessors, p.slot_name);
72                 if(proc)
73                         sequence.add_postprocessor(*proc);
74                 else if(p.postprocessor_template)
75                 {
76                         proc = p.postprocessor_template->create(sequence.get_width(), sequence.get_height());
77                         if(proc)
78                         {
79 #ifdef DEBUG
80                                 if(!debug_name.empty())
81                                         proc->set_debug_name(format("%s/%d.pproc", debug_name, index++));
82 #endif
83                                 sequence.add_postprocessor_owned(proc);
84                         }
85                 }
86         }
87 }
88
89 Sequence *SequenceBuilder::build(unsigned w, unsigned h) const
90 {
91         RefPtr<Sequence> sequence = new Sequence(w, h, create_frame_format());
92         build(*sequence);
93         return sequence.release();
94 }
95
96 Sequence *SequenceBuilder::build(const View &view) const
97 {
98         RefPtr<Sequence> sequence = new Sequence(view, create_frame_format());
99         build(*sequence);
100         return sequence.release();
101 }
102
103 Sequence *SequenceBuilder::build(const Framebuffer &fbo) const
104 {
105         RefPtr<Sequence> sequence = new Sequence(fbo, create_frame_format());
106         build(*sequence);
107         return sequence.release();
108 }
109
110 FrameFormat SequenceBuilder::create_frame_format() const
111 {
112         unsigned samples = min(tmpl.get_maximum_multisample(), Limits::get_global().max_samples);
113         if(samples<tmpl.get_required_multisample())
114                 throw invalid_operation("SequenceBuilder::create_frame_format");
115
116         PixelComponents color_comp = (tmpl.get_alpha() ? RGBA : RGB);
117         DataType color_type = (tmpl.get_hdr() ? HALF_FLOAT : UNSIGNED_BYTE);
118         PixelFormat color_pf = make_pixelformat(color_comp, color_type);
119
120         return (COLOR_ATTACHMENT,color_pf, DEPTH_ATTACHMENT).set_samples(samples);
121 }
122
123 } // namespace GL
124 } // namespace Msp