]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
Track whether implicit shader data needs to be re-applied
[libs/gl.git] / source / renderer.h
1 #ifndef MSP_GL_RENDERER_H_
2 #define MSP_GL_RENDERER_H_
3
4 #include <set>
5 #include <vector>
6 #include "matrix.h"
7 #include "programdata.h"
8 #include "tag.h"
9
10 namespace Msp {
11 namespace GL {
12
13 class Batch;
14 class Buffer;
15 class Camera;
16 class Clipping;
17 class Material;
18 class Mesh;
19 class Lighting;
20 class Program;
21 class Renderable;
22 class Texture;
23 class Texturing;
24 class WindingTest;
25
26 /**
27 A class for supervising the rendering process.  While many Renderables (in
28 particular, Objects and Scenes) can by rendered without a Renderer, using one
29 will often be more efficient.  This is especially true for ObjectInstances.
30
31 The Renderer works by deferring GL state changes until something is actually
32 being drawn.  This avoids many unnecessary GL calls if consecutive renderables
33 use the same resources.
34
35 A state stack is provided to help with state scoping.  Typically a Renderable
36 will push the current state on entry, set whatever state it requires, render
37 itself, and pop the state when it's done.  An RAII helper class is provided for
38 the push/pop operation.
39 */
40 class Renderer
41 {
42 public:
43         class Push
44         {
45         private:
46                 Renderer &renderer;
47
48         public:
49                 Push(Renderer &r): renderer(r) { renderer.push_state(); }
50                 ~Push() { renderer.pop_state(); }
51         };
52
53         class Exclude
54         {
55         private:
56                 Renderer &renderer;
57                 const Renderable &renderable;
58
59         public:
60                 Exclude(Renderer &r, const Renderable &e): renderer(r), renderable(e) { renderer.exclude(renderable); }
61                 ~Exclude() { renderer.include(renderable); }
62         };
63
64 private:
65         struct State
66         {
67                 const Texture *texture;
68                 const Texturing *texturing;
69                 unsigned lowest_effect_texunit;
70                 const Material *material;
71                 const Lighting *lighting;
72                 Matrix lighting_matrix;
73                 const Clipping *clipping;
74                 Matrix clipping_matrix;
75                 const Program *shprog;
76                 unsigned shdata_count;
77                 const Mesh *mesh;
78                 const WindingTest *winding_test;
79                 bool reverse_winding;
80
81                 State();
82         };
83
84         class MtxStack: public MatrixStack
85         {
86         private:
87                 Renderer &renderer;
88
89         public:
90                 MtxStack(Renderer &);
91         private:
92                 virtual void update();
93         };
94
95         enum ChangeMask
96         {
97                 LEGACY_MATRIX = 1,
98                 MODERN_MATRIX = 2,
99                 MATRIX = LEGACY_MATRIX|MODERN_MATRIX,
100                 LIGHTING = 4,
101                 CLIPPING = 8,
102                 SHADER_DATA = 16,
103                 MATERIAL_SHDATA = 32,
104                 STANDARD_SHDATA = 64
105         };
106
107         MtxStack mtx_stack;
108         unsigned char changed;
109         bool matrices_loaded;
110         unsigned shdata_applied;
111         const Camera *camera;
112         std::vector<State> state_stack;
113         State *state;
114         ProgramData standard_shdata;
115         std::vector<const ProgramData *> shdata_stack;
116         std::set<const Renderable *> excluded;
117
118 public:
119         Renderer(const Camera *);
120         ~Renderer();
121
122         /** Resets all internal state and restarts rendering.  There must be no
123         unpopped state in the stack.  It is permissible to call begin() multiple
124         times without an intervening end(). */
125         void begin(const Camera *);
126
127         /** Deprecated as unsafe.  Use set_matrix() or transform() instead. */
128         MatrixStack &matrix_stack() { return mtx_stack; }
129
130         /** Replaces the Renderer's modelview matrix. */
131         void set_matrix(const Matrix &);
132
133         /** Applies a transform to the Renderer's modelview matrix. */
134         void transform(const Matrix &);
135
136         /** Returns the current modelview matrix. */
137         const Matrix &get_matrix() const { return mtx_stack.top(); }
138
139         const Camera *get_camera() const { return camera; }
140
141         void set_texture(const Texture *);
142         void set_texturing(const Texturing *);
143         unsigned allocate_effect_texunit();
144         void set_material(const Material *);
145
146         void set_lighting(const Lighting *);
147         void set_clipping(const Clipping *);
148
149         /** Sets the shader program to use.  An initial set of data can be set as
150         well, with the same semantics as add_shader_data. */
151         void set_shader_program(const Program *prog, const ProgramData *data = 0);
152
153         /** Adds another set of data to be use with shader programs.  The data is
154         independent of any shader program changes and remains in effect until the
155         Renderer state is popped. */
156         void add_shader_data(const ProgramData &data);
157
158         void set_mesh(const Mesh *);
159         void set_winding_test(const WindingTest *);
160         void set_reverse_winding(bool);
161
162         /** Saves the current state so it can be restored later. */
163         void push_state();
164
165         /** Restores a previously saved state.  Must be matched with an earlier
166         push_state call. */
167         void pop_state();
168
169         /** Prepares for temporarily bypassing the Renderer by synchronizing the
170         current state with GL.  No additional call is necessary to resume using the
171         Renderer.  DEPRECATED. */
172         void escape();
173
174         /** Unbinds all objects and resets related state.  There must be no unpopped
175         state in the stack.  Rendering with the same camera can be restarted without
176         an explicit begin() call. */
177         void end();
178
179         void exclude(const Renderable &);
180         void include(const Renderable &);
181
182         void render(const Renderable &, const Tag & = Tag());
183         void draw(const Batch &);
184
185 private:
186         void apply_state();
187         void reset_state();
188 };
189
190 } // namespace GL
191 } // namespace Msp
192
193 #endif