]> git.tdb.fi Git - libs/gl.git/blob - source/render/renderer.h
af9eac0426e4c265a76bd2dc54879a5f85b636c3
[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;
73                 const Texture *texture;
74                 const Sampler *sampler;
75                 int replaced;
76
77                 BoundTexture();
78         };
79
80         struct BoundProgramData
81         {
82                 const ProgramData *shdata;
83                 mutable unsigned generation;
84
85                 BoundProgramData(const ProgramData *);
86         };
87
88         struct State
89         {
90                 const Camera *camera;
91                 Matrix model_matrix;
92                 const Framebuffer *framebuffer;
93                 const Rect *viewport;
94                 const Rect *scissor;
95                 unsigned texture_count;
96                 const Clipping *clipping;
97                 const Program *shprog;
98                 unsigned shdata_count;
99                 const VertexSetup *vertex_setup;
100                 FaceWinding front_face;
101                 CullMode face_cull;
102                 const DepthTest *depth_test;
103                 const StencilTest *stencil_test;
104                 const Blend *blend;
105                 unsigned object_lod_bias;
106
107                 State();
108         };
109
110         enum ChangeMask
111         {
112                 MATRIX = 2,
113                 SHADER_DATA = 16
114         };
115
116         unsigned char changed;
117         std::vector<State> state_stack;
118         State *state;
119         std::vector<BoundTexture> texture_stack;
120         ProgramData standard_shdata;
121         std::vector<BoundProgramData> shdata_stack;
122         std::set<const Renderable *> excluded;
123         PipelineState pipeline_state;
124         Commands commands;
125
126 public:
127         Renderer();
128         ~Renderer();
129
130         /** Sets the camera to render from.  The model matrix is reset to identity. */
131         void set_camera(const Camera &);
132
133         const Camera *get_camera() const { return state->camera; }
134
135         /** Replaces the Renderer's model matrix. */
136         void set_matrix(const Matrix &);
137
138         /** Applies a transform to the Renderer's model matrix. */
139         void transform(const Matrix &);
140
141         /** Returns the current model matrix. */
142         const Matrix &get_matrix() const { return state->model_matrix; }
143
144         void set_framebuffer(const Framebuffer *);
145         void set_viewport(const Rect *);
146         void set_scissor(const Rect *);
147
148         const Framebuffer *get_framebuffer() const { return state->framebuffer; }
149
150         void set_texture(Tag, const Texture *, const Sampler * = 0);
151 private:
152         void flush_textures();
153 public:
154         DEPRECATED void set_material(const Material *);
155
156         DEPRECATED void set_lighting(const Lighting *);
157         void set_clipping(const Clipping *);
158
159         /** Sets the shader program to use.  An initial set of data can be set as
160         well, with the same semantics as add_shader_data. */
161         void set_shader_program(const Program *prog, const ProgramData *data = 0);
162
163         /** Adds another set of data to be use with shader programs.  The data is
164         independent of any shader program changes and remains in effect until the
165         Renderer state is popped. */
166         void add_shader_data(const ProgramData &data);
167
168         DEPRECATED void flush_shader_data() { flush_shader_data_(); }
169 private:
170         void flush_shader_data_();
171
172 public:
173         void set_vertex_setup(const VertexSetup *);
174         void set_front_face(FaceWinding);
175         void set_face_cull(CullMode);
176
177         void set_depth_test(const DepthTest *);
178         void set_stencil_test(const StencilTest *);
179         void set_blend(const Blend *);
180
181         void set_object_lod_bias(unsigned);
182         unsigned get_object_lod_bias() const { return state->object_lod_bias; }
183
184         /** Saves the current state so it can be restored later. */
185         void push_state();
186
187         /** Restores a previously saved state.  Must be matched with an earlier
188         push_state call. */
189         void pop_state();
190
191         /** Unbinds all objects and resets related state.  There must be no unpopped
192         state in the stack.  The Renderer remains valid and may be reused for
193         further rendering. */
194         void end();
195
196         void exclude(const Renderable &);
197         void include(const Renderable &);
198
199         void clear(const ClearValue *);
200
201         void render(const Renderable &, Tag = Tag());
202         void draw(const Batch &);
203         void draw_instanced(const Batch &, unsigned);
204
205         void resolve_multisample(Framebuffer &);
206
207         void begin_query(const QueryPool &, unsigned);
208         void end_query(const QueryPool &, unsigned);
209
210 private:
211         void apply_state();
212 };
213
214 } // namespace GL
215 } // namespace Msp
216
217 #endif