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