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