eff9348b8395a78226436110633a400c9e373d26
[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                 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                 if(!proc)
97                         throw invalid_operation("SequenceBuilder::build");
98         }
99 }
100
101 Sequence *SequenceBuilder::build() const
102 {
103         RefPtr<Sequence> sequence = new Sequence();
104         build(*sequence);
105         return sequence.release();
106 }
107
108 Sequence *SequenceBuilder::build(unsigned w, unsigned h) const
109 {
110         RefPtr<Sequence> sequence = new Sequence(w, h, create_frame_format());
111         build(*sequence);
112         return sequence.release();
113 }
114
115 Sequence *SequenceBuilder::build(const View &view) const
116 {
117         RefPtr<Sequence> sequence = new Sequence(view.get_width(), view.get_height(), create_frame_format());
118         build(*sequence);
119         return sequence.release();
120 }
121
122 Sequence *SequenceBuilder::build(const Framebuffer &fbo) const
123 {
124         RefPtr<Sequence> sequence = new Sequence(fbo.get_width(), fbo.get_height(), create_frame_format());
125         build(*sequence);
126         return sequence.release();
127 }
128
129 FrameFormat SequenceBuilder::create_frame_format() const
130 {
131         unsigned samples = min(tmpl.get_maximum_multisample(), DeviceInfo::get_global().limits.max_samples);
132         if(samples<tmpl.get_required_multisample())
133                 throw invalid_operation("SequenceBuilder::create_frame_format");
134
135         PixelComponents color_comp = (tmpl.get_alpha() ? RGBA : RGB);
136         DataType color_type = (tmpl.get_hdr() ? HALF_FLOAT : UNSIGNED_BYTE);
137         PixelFormat color_pf = make_pixelformat(color_comp, color_type);
138
139         return (COLOR_ATTACHMENT,color_pf, DEPTH_ATTACHMENT).set_samples(samples);
140 }
141
142 } // namespace GL
143 } // namespace Msp