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); }
73 const Texture *texture;
74 const Sampler *sampler;
80 struct BoundProgramData
82 const ProgramData *shdata;
83 mutable unsigned generation;
85 BoundProgramData(const ProgramData *);
92 const Framebuffer *framebuffer;
95 unsigned texture_count;
96 const Clipping *clipping;
97 const Program *shprog;
98 unsigned shdata_count;
99 const VertexSetup *vertex_setup;
100 FaceWinding front_face;
102 const DepthTest *depth_test;
103 const StencilTest *stencil_test;
105 unsigned object_lod_bias;
116 unsigned char changed;
117 std::vector<State> state_stack;
119 std::vector<BoundTexture> texture_stack;
120 ProgramData standard_shdata;
121 std::vector<BoundProgramData> shdata_stack;
122 std::set<const Renderable *> excluded;
123 PipelineState pipeline_state;
130 /** Sets the camera to render from. The model matrix is reset to identity. */
131 void set_camera(const Camera &);
133 const Camera *get_camera() const { return state->camera; }
135 /** Replaces the Renderer's model matrix. */
136 void set_matrix(const Matrix &);
138 /** Applies a transform to the Renderer's model matrix. */
139 void transform(const Matrix &);
141 /** Returns the current model matrix. */
142 const Matrix &get_matrix() const { return state->model_matrix; }
144 void set_framebuffer(const Framebuffer *);
145 void set_viewport(const Rect *);
146 void set_scissor(const Rect *);
148 const Framebuffer *get_framebuffer() const { return state->framebuffer; }
150 void set_texture(Tag, const Texture *, const Sampler * = 0);
152 void flush_textures();
154 DEPRECATED void set_material(const Material *);
156 DEPRECATED void set_lighting(const Lighting *);
157 void set_clipping(const Clipping *);
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);
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);
168 DEPRECATED void flush_shader_data() { flush_shader_data_(); }
170 void flush_shader_data_();
173 void set_vertex_setup(const VertexSetup *);
174 void set_front_face(FaceWinding);
175 void set_face_cull(CullMode);
177 void set_depth_test(const DepthTest *);
178 void set_stencil_test(const StencilTest *);
179 void set_blend(const Blend *);
181 void set_object_lod_bias(unsigned);
182 unsigned get_object_lod_bias() const { return state->object_lod_bias; }
184 /** Saves the current state so it can be restored later. */
187 /** Restores a previously saved state. Must be matched with an earlier
191 /** Unbinds all objects and resets related state. There must be no unpopped
192 state in the stack. The Renderer remains valid and may be reused for
193 further rendering. */
196 void exclude(const Renderable &);
197 void include(const Renderable &);
199 void clear(const ClearValue *);
201 void render(const Renderable &, Tag = Tag());
202 void draw(const Batch &);
203 void draw_instanced(const Batch &, unsigned);
205 void resolve_multisample(Framebuffer &);
207 void begin_query(const QueryPool &, unsigned);
208 void end_query(const QueryPool &, unsigned);