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