1 #ifndef MSP_GL_RENDERER_H_
2 #define MSP_GL_RENDERER_H_
8 #include "pipelinestate.h"
9 #include "programdata.h"
31 A class for supervising the rendering process. While many Renderables (in
32 particular, Objects and Scenes) can by rendered without a Renderer, using one
33 will often be more efficient. This is especially true for ObjectInstances.
35 The Renderer works by deferring GL state changes until something is actually
36 being drawn. This avoids many unnecessary GL calls if consecutive renderables
37 use the same resources.
39 A state stack is provided to help with state scoping. Typically a Renderable
40 will push the current state on entry, set whatever state it requires, render
41 itself, and pop the state when it's done. An RAII helper class is provided for
42 the push/pop operation.
53 Push(Renderer &r): renderer(r) { renderer.push_state(); }
54 ~Push() { renderer.pop_state(); }
61 const Renderable &renderable;
64 Exclude(Renderer &r, const Renderable &e): renderer(r), renderable(e) { renderer.exclude(renderable); }
65 ~Exclude() { renderer.include(renderable); }
72 mutable int unit = -1;
73 const Texture *texture = 0;
74 const Sampler *sampler = 0;
78 struct BoundProgramData
80 const ProgramData *shdata;
81 mutable unsigned generation = 0;
83 BoundProgramData(const ProgramData *);
88 const Camera *camera = 0;
90 const Framebuffer *framebuffer = 0;
91 const Rect *viewport = 0;
92 const Rect *scissor = 0;
93 unsigned texture_count = 0;
94 const Clipping *clipping = 0;
95 const Program *shprog = 0;
96 unsigned shdata_count = 0;
97 const VertexSetup *vertex_setup = 0;
98 FaceWinding front_face = NON_MANIFOLD;
99 CullMode face_cull = NO_CULL;
100 const DepthTest *depth_test = 0;
101 const StencilTest *stencil_test = 0;
102 const Blend *blend = 0;
103 unsigned object_lod_bias = 0;
112 unsigned char changed;
113 std::vector<State> state_stack;
115 std::vector<BoundTexture> texture_stack;
116 ProgramData standard_shdata;
117 std::vector<BoundProgramData> shdata_stack;
118 std::set<const Renderable *> excluded;
119 PipelineState pipeline_state;
126 /** Sets the camera to render from. The model matrix is reset to identity. */
127 void set_camera(const Camera &);
129 const Camera *get_camera() const { return state->camera; }
131 /** Replaces the Renderer's model matrix. */
132 void set_matrix(const Matrix &);
134 /** Applies a transform to the Renderer's model matrix. */
135 void transform(const Matrix &);
137 /** Returns the current model matrix. */
138 const Matrix &get_matrix() const { return state->model_matrix; }
140 void set_framebuffer(const Framebuffer *);
141 void set_viewport(const Rect *);
142 void set_scissor(const Rect *);
144 const Framebuffer *get_framebuffer() const { return state->framebuffer; }
146 void set_texture(Tag, const Texture *, const Sampler * = 0);
148 void flush_textures();
150 DEPRECATED void set_material(const Material *);
152 DEPRECATED void set_lighting(const Lighting *);
153 void set_clipping(const Clipping *);
155 /** Sets the shader program to use. An initial set of data can be set as
156 well, with the same semantics as add_shader_data. */
157 void set_shader_program(const Program *prog, const ProgramData *data = 0);
159 /** Adds another set of data to be use with shader programs. The data is
160 independent of any shader program changes and remains in effect until the
161 Renderer state is popped. */
162 void add_shader_data(const ProgramData &data);
164 DEPRECATED void flush_shader_data() { flush_shader_data_(); }
166 void flush_shader_data_();
169 void set_vertex_setup(const VertexSetup *);
170 void set_front_face(FaceWinding);
171 void set_face_cull(CullMode);
173 void set_depth_test(const DepthTest *);
174 void set_stencil_test(const StencilTest *);
175 void set_blend(const Blend *);
177 void set_object_lod_bias(unsigned);
178 unsigned get_object_lod_bias() const { return state->object_lod_bias; }
180 /** Saves the current state so it can be restored later. */
183 /** Restores a previously saved state. Must be matched with an earlier
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. */
192 void exclude(const Renderable &);
193 void include(const Renderable &);
195 void clear(const ClearValue *);
197 void render(const Renderable &, Tag = Tag());
198 void draw(const Batch &);
199 void draw_instanced(const Batch &, unsigned);
201 void resolve_multisample(Framebuffer &);
203 void begin_query(const QueryPool &, unsigned);
204 void end_query(const QueryPool &, unsigned);