]> git.tdb.fi Git - libs/gl.git/blob - source/builders/sequencebuilder.cpp
Set viewport and scissor before clear commands
[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         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.slot_name);
63                 if(!renderable)
64                         continue;
65
66                 Sequence::Step &step = sequence.add_step(s.tag, *renderable);
67                 step.set_blend(s.blend);
68                 step.set_depth_test(s.depth_test);
69                 step.set_stencil_test(s.stencil_test);
70                 step.set_lighting(s.lighting);
71         }
72
73 #ifdef DEBUG
74         unsigned index = 0;
75 #endif
76         for(const SequenceTemplate::PostProcessor &p: tmpl.get_postprocessors())
77         {
78                 PostProcessor *proc = 0;
79                 if(!p.slot_name.empty())
80                         proc = get_item(postprocessors, p.slot_name);
81                 if(proc)
82                         sequence.add_postprocessor(*proc);
83                 else if(p.postprocessor_template)
84                 {
85                         proc = p.postprocessor_template->create(sequence.get_width(), sequence.get_height());
86                         if(proc)
87                         {
88 #ifdef DEBUG
89                                 if(!debug_name.empty())
90                                         proc->set_debug_name(format("%s/%d.pproc", debug_name, index++));
91 #endif
92                                 sequence.add_postprocessor_owned(proc);
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