]> git.tdb.fi Git - libs/gl.git/blob - source/builders/sequencebuilder.cpp
Refactor the structure of sequence template files
[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                         continue;
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                 RefPtr<PostProcessor> proc = 0;
78                 if(!p.slot_name.empty())
79                         proc = get_item(postprocessors, p.slot_name);
80                 if(proc)
81                         sequence.add_postprocessor(*proc);
82                 else if(p.postprocessor_template)
83                 {
84                         proc = p.postprocessor_template->create(sequence.get_width(), sequence.get_height());
85                         if(proc)
86                         {
87 #ifdef DEBUG
88                                 if(!debug_name.empty())
89                                         proc->set_debug_name(format("%s/%d.pproc", debug_name, index++));
90 #endif
91                                 sequence.add_postprocessor(*proc.get());
92                                 sequence.add_owned(proc.release());
93                         }
94                 }
95         }
96 }
97
98 Sequence *SequenceBuilder::build() const
99 {
100         RefPtr<Sequence> sequence = new Sequence();
101         build(*sequence);
102         return sequence.release();
103 }
104
105 Sequence *SequenceBuilder::build(unsigned w, unsigned h) const
106 {
107         RefPtr<Sequence> sequence = new Sequence(w, h, create_frame_format());
108         build(*sequence);
109         return sequence.release();
110 }
111
112 Sequence *SequenceBuilder::build(const View &view) const
113 {
114         RefPtr<Sequence> sequence = new Sequence(view.get_width(), view.get_height(), create_frame_format());
115         build(*sequence);
116         return sequence.release();
117 }
118
119 Sequence *SequenceBuilder::build(const Framebuffer &fbo) const
120 {
121         RefPtr<Sequence> sequence = new Sequence(fbo.get_width(), fbo.get_height(), create_frame_format());
122         build(*sequence);
123         return sequence.release();
124 }
125
126 FrameFormat SequenceBuilder::create_frame_format() const
127 {
128         unsigned samples = min(tmpl.get_maximum_multisample(), DeviceInfo::get_global().limits.max_samples);
129         if(samples<tmpl.get_required_multisample())
130                 throw invalid_operation("SequenceBuilder::create_frame_format");
131
132         PixelComponents color_comp = (tmpl.get_alpha() ? RGBA : RGB);
133         DataType color_type = (tmpl.get_hdr() ? HALF_FLOAT : UNSIGNED_BYTE);
134         PixelFormat color_pf = make_pixelformat(color_comp, color_type);
135
136         return (COLOR_ATTACHMENT,color_pf, DEPTH_ATTACHMENT).set_samples(samples);
137 }
138
139 } // namespace GL
140 } // namespace Msp