]> git.tdb.fi Git - libs/gl.git/blob - source/render/renderer.h
Deprecate external access to Renderer::flush_shader_data
[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 "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 Sampler;
23 class Texture;
24 class Texturing;
25 class VertexSetup;
26 class WindingTest;
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                 unsigned texture_count;
91                 unsigned lowest_effect_texunit;
92                 const Clipping *clipping;
93                 const Program *shprog;
94                 unsigned shdata_count;
95                 const VertexSetup *vertex_setup;
96                 const WindingTest *winding_test;
97                 bool reverse_winding;
98                 unsigned object_lod_bias;
99
100                 State();
101         };
102
103         enum ChangeMask
104         {
105                 MATRIX = 2,
106                 SHADER_DATA = 16,
107                 STANDARD_SHDATA = 64,
108                 CAMERA_SHDATA = 128,
109                 CLIPPING_SHDATA = 512
110         };
111
112         const Camera *default_camera;
113         unsigned char changed;
114         std::vector<State> state_stack;
115         State *state;
116         std::vector<BoundTexture> texture_stack;
117         ProgramData standard_shdata;
118         std::vector<BoundProgramData> shdata_stack;
119         std::set<const Renderable *> excluded;
120
121 public:
122         Renderer();
123         DEPRECATED Renderer(const Camera *);
124 private:
125         void init();
126 public:
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_texture(Tag, const Texture *, const Sampler * = 0);
144 private:
145         void set_texture(Tag, int, const Texture *, const Sampler *);
146         void flush_textures();
147 public:
148 #pragma GCC diagnostic push
149 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
150         DEPRECATED void set_texture(const Texture *, const Sampler * = 0);
151         DEPRECATED void set_texturing(const Texturing *);
152         DEPRECATED unsigned allocate_effect_texunit();
153 #pragma GCC diagnostic pop
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_winding_test(const WindingTest *);
175         void set_reverse_winding(bool);
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 render(const Renderable &, Tag = Tag());
196         void draw(const Batch &);
197         void draw_instanced(const Batch &, unsigned);
198
199 private:
200         void apply_state();
201 };
202
203 } // namespace GL
204 } // namespace Msp
205
206 #endif