]> git.tdb.fi Git - libs/gl.git/blob - source/builders/sequencebuilder.cpp
Rearrange postprocessor creation code in SequenceBuilder
[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::Renderable &r: tmpl.get_renderables())
20                 renderables[r.slot_name] = r.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         if(tmpl.is_clear_enabled())
54         {
55                 sequence.set_clear_colors(tmpl.get_clear_colors());
56                 sequence.set_clear_depth(tmpl.get_clear_depth());
57                 sequence.set_clear_stencil(tmpl.get_clear_stencil());
58         }
59
60         for(const SequenceTemplate::Step &s: tmpl.get_steps())
61         {
62                 Renderable *renderable = get_item(renderables, s.renderable_name);
63                 if(!renderable)
64                         throw missing_renderable(s.renderable_name);
65
66                 Sequence::Step &step = sequence.add_step(s.tag, *renderable);
67                 step.set_depth_test(s.depth_test);
68                 step.set_stencil_test(s.stencil_test);
69                 step.set_lighting(s.lighting);
70         }
71
72 #ifdef DEBUG
73         unsigned index = 0;
74 #endif
75         for(const SequenceTemplate::PostProcessor &p: tmpl.get_postprocessors())
76         {
77                 PostProcessor *proc = 0;
78                 if(!p.slot_name.empty())
79                         proc = get_item(postprocessors, p.slot_name);
80                 if(!proc)
81                 {
82                         if(p.postprocessor_template)
83                         {
84                                 proc = p.postprocessor_template->create(sequence.get_width(), sequence.get_height());
85 #ifdef DEBUG
86                                 if(!debug_name.empty())
87                                         proc->set_debug_name(format("%s/%d.pproc", debug_name, index++));
88 #endif
89                         }
90                         else
91                                 throw invalid_operation("SequenceBuilder::build");
92
93                         sequence.add_owned(proc);
94                 }
95
96                 sequence.add_postprocessor(*proc);
97         }
98 }
99
100 Sequence *SequenceBuilder::build() const
101 {
102         RefPtr<Sequence> sequence = new Sequence();
103         build(*sequence);
104         return sequence.release();
105 }
106
107 Sequence *SequenceBuilder::build(unsigned w, unsigned h) const
108 {
109         RefPtr<Sequence> sequence = new Sequence(w, h, create_frame_format());
110         build(*sequence);
111         return sequence.release();
112 }
113
114 Sequence *SequenceBuilder::build(const View &view) const
115 {
116         RefPtr<Sequence> sequence = new Sequence(view.get_width(), view.get_height(), create_frame_format());
117         build(*sequence);
118         return sequence.release();
119 }
120
121 Sequence *SequenceBuilder::build(const Framebuffer &fbo) const
122 {
123         RefPtr<Sequence> sequence = new Sequence(fbo.get_width(), fbo.get_height(), create_frame_format());
124         build(*sequence);
125         return sequence.release();
126 }
127
128 FrameFormat SequenceBuilder::create_frame_format() const
129 {
130         unsigned samples = min(tmpl.get_maximum_multisample(), DeviceInfo::get_global().limits.max_samples);
131         if(samples<tmpl.get_required_multisample())
132                 throw invalid_operation("SequenceBuilder::create_frame_format");
133
134         PixelComponents color_comp = (tmpl.get_alpha() ? RGBA : RGB);
135         DataType color_type = (tmpl.get_hdr() ? HALF_FLOAT : UNSIGNED_BYTE);
136         PixelFormat color_pf = make_pixelformat(color_comp, color_type);
137
138         return (COLOR_ATTACHMENT,color_pf, DEPTH_ATTACHMENT).set_samples(samples);
139 }
140
141 } // namespace GL
142 } // namespace Msp