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