]> git.tdb.fi Git - libs/gl.git/blob - source/render/renderer.h
Add functions to retrieve steps and postprocessors from Sequence
[libs/gl.git] / source / render / renderer.h
1 #ifndef MSP_GL_RENDERER_H_
2 #define MSP_GL_RENDERER_H_
3
4 #include <set>
5 #include <vector>
6 #include "commands.h"
7 #include "matrix.h"
8 #include "pipelinestate.h"
9 #include "programdata.h"
10 #include "tag.h"
11
12 namespace Msp {
13 namespace GL {
14
15 class Batch;
16 class Buffer;
17 class Camera;
18 union ClearValue;
19 class Material;
20 class Mesh;
21 class Lighting;
22 class Program;
23 class QueryPool;
24 class Renderable;
25 class Sampler;
26 class Texture;
27 class VertexSetup;
28
29 /**
30 A class for supervising the rendering process.  While many Renderables (in
31 particular, Objects and Scenes) can by rendered without a Renderer, using one
32 will often be more efficient.  This is especially true for ObjectInstances.
33
34 The Renderer works by deferring GL state changes until something is actually
35 being drawn.  This avoids many unnecessary GL calls if consecutive renderables
36 use the same resources.
37
38 A state stack is provided to help with state scoping.  Typically a Renderable
39 will push the current state on entry, set whatever state it requires, render
40 itself, and pop the state when it's done.  An RAII helper class is provided for
41 the push/pop operation.
42 */
43 class Renderer
44 {
45 public:
46         class Push
47         {
48         private:
49                 Renderer &renderer;
50
51         public:
52                 Push(Renderer &r): renderer(r) { renderer.push_state(); }
53                 ~Push() { renderer.pop_state(); }
54         };
55
56         class Exclude
57         {
58         private:
59                 Renderer &renderer;
60                 const Renderable &renderable;
61
62         public:
63                 Exclude(Renderer &r, const Renderable &e): renderer(r), renderable(e) { renderer.exclude(renderable); }
64                 ~Exclude() { renderer.include(renderable); }
65         };
66
67 private:
68         struct BoundTexture
69         {
70                 Tag tag;
71                 mutable int unit = -1;
72                 const Texture *texture = 0;
73                 const Sampler *sampler = 0;
74                 int replaced = -1;
75         };
76
77         struct BoundProgramData
78         {
79                 const ProgramData *shdata;
80                 mutable unsigned generation = 0;
81
82                 BoundProgramData(const ProgramData *);
83         };
84
85         struct State
86         {
87                 const Camera *camera = 0;
88                 Matrix model_matrix;
89                 const Framebuffer *framebuffer = 0;
90                 const Rect *viewport = 0;
91                 const Rect *scissor = 0;
92                 unsigned texture_count = 0;
93                 const Program *shprog = 0;
94                 unsigned shdata_count = 0;
95                 const VertexSetup *vertex_setup = 0;
96                 FaceWinding front_face = NON_MANIFOLD;
97                 CullMode face_cull = NO_CULL;
98                 const DepthTest *depth_test = 0;
99                 const StencilTest *stencil_test = 0;
100                 const Blend *blend = 0;
101                 unsigned object_lod_bias = 0;
102         };
103
104         enum ChangeMask
105         {
106                 MATRIX = 2,
107                 SHADER_DATA = 16
108         };
109
110         unsigned char changed;
111         std::vector<State> state_stack;
112         State *state;
113         std::vector<BoundTexture> texture_stack;
114         ProgramData standard_shdata;
115         std::vector<BoundProgramData> shdata_stack;
116         std::set<const Renderable *> excluded;
117         PipelineState pipeline_state;
118         Commands commands;
119
120 public:
121         Renderer();
122         ~Renderer();
123
124         /** Sets the camera to render from.  The model matrix is reset to identity. */
125         void set_camera(const Camera &);
126
127         const Camera *get_camera() const { return state->camera; }
128
129         /** Replaces the Renderer's model matrix. */
130         void set_matrix(const Matrix &);
131
132         /** Applies a transform to the Renderer's model matrix. */
133         void transform(const Matrix &);
134
135         /** Returns the current model matrix. */
136         const Matrix &get_matrix() const { return state->model_matrix; }
137
138         void set_framebuffer(const Framebuffer *);
139         void set_viewport(const Rect *);
140         void set_scissor(const Rect *);
141
142         const Framebuffer *get_framebuffer() const { return state->framebuffer; }
143
144         void set_texture(Tag, const Texture *, const Sampler * = 0);
145 private:
146         void flush_textures();
147 public:
148         DEPRECATED void set_material(const Material *);
149
150         DEPRECATED void set_lighting(const Lighting *);
151
152         /** Sets the shader program to use.  An initial set of data can be set as
153         well, with the same semantics as add_shader_data. */
154         void set_shader_program(const Program *prog, const ProgramData *data = 0);
155
156         /** Adds another set of data to be use with shader programs.  The data is
157         independent of any shader program changes and remains in effect until the
158         Renderer state is popped. */
159         void add_shader_data(const ProgramData &data);
160
161         DEPRECATED void flush_shader_data() { flush_shader_data_(); }
162 private:
163         void flush_shader_data_();
164
165 public:
166         void set_vertex_setup(const VertexSetup *);
167         void set_front_face(FaceWinding);
168         void set_face_cull(CullMode);
169
170         void set_depth_test(const DepthTest *);
171         void set_stencil_test(const StencilTest *);
172         void set_blend(const Blend *);
173
174         void set_object_lod_bias(unsigned);
175         unsigned get_object_lod_bias() const { return state->object_lod_bias; }
176
177         /** Saves the current state so it can be restored later. */
178         void push_state();
179
180         /** Restores a previously saved state.  Must be matched with an earlier
181         push_state call. */
182         void pop_state();
183
184         /** Unbinds all objects and resets related state.  There must be no unpopped
185         state in the stack.  The Renderer remains valid and may be reused for
186         further rendering. */
187         void end();
188
189         void exclude(const Renderable &);
190         void include(const Renderable &);
191
192         void clear(const ClearValue *);
193
194         void render(const Renderable &, Tag = Tag());
195         void draw(const Batch &);
196         void draw_instanced(const Batch &, unsigned);
197
198         void resolve_multisample(Framebuffer &);
199
200         void begin_query(const QueryPool &, unsigned);
201         void end_query(const QueryPool &, unsigned);
202
203 private:
204         void apply_state();
205 };
206
207 } // namespace GL
208 } // namespace Msp
209
210 #endif