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