1 #ifndef MSP_GL_RENDERER_H_
2 #define MSP_GL_RENDERER_H_
8 #include "programdata.h"
9 #include "renderer_backend.h"
30 Rendering supervisor. This is the primary interface for setting state and
31 issuing draw commands.
33 The Renderer class allows setting textures and uniform values by names (using
34 ProgramData for the latter). The names are resolved into binding points when
35 the resources are needed for a draw command.
37 A state stack is provided to help with state management in render graphs.
38 Renderables can save the state by pushing it on the stack before beginning
39 their work, and pop it afterwards to restore it without disturbing state set
42 class Renderer: public RendererBackend
44 friend RendererBackend;
48 RAII helper class for pushing state on the stack.
56 Push(Renderer &r): renderer(r) { renderer.push_state(); }
57 ~Push() { renderer.pop_state(); }
64 mutable int binding = -1;
65 const Texture *texture = 0;
66 const Sampler *sampler = 0;
70 struct BoundProgramData
72 const ProgramData *shdata;
73 mutable unsigned generation = 0;
75 BoundProgramData(const ProgramData *);
80 const Camera *camera = 0;
82 const Framebuffer *framebuffer = 0;
83 const Rect *viewport = 0;
84 const Rect *scissor = 0;
85 unsigned texture_count = 0;
86 const Program *shprog = 0;
87 unsigned shdata_count = 0;
88 const VertexSetup *vertex_setup = 0;
89 FaceWinding front_face = NON_MANIFOLD;
90 CullMode face_cull = NO_CULL;
91 const DepthTest *depth_test = 0;
92 const StencilTest *stencil_test = 0;
93 const Blend *blend = 0;
94 unsigned object_lod_bias = 0;
103 unsigned char changed = 0;
104 std::vector<State> state_stack;
105 State *current_state = 0;
106 ProgramData standard_shdata;
107 std::vector<BoundProgramData> shdata_stack;
108 std::vector<BoundTexture> texture_stack;
115 /** Begins rendering, allowing commands to be issued. */
118 /** Ends rendering. Any global state is reset to defaults. No further
119 commands are allowed before the next call to begin(). */
122 /** Saves the current state so it can be restored later. */
125 /** Restores a previously saved state. Must be matched with an earlier
130 State &get_state() const;
133 /** Sets the camera to render from. The model matrix is reset to identity. */
134 void set_camera(const Camera &);
136 const Camera *get_camera() const { return get_state().camera; }
138 /** Replaces the Renderer's model matrix. */
139 void set_matrix(const Matrix &);
141 /** Returns the current model matrix. */
142 const Matrix &get_matrix() const { return get_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 get_state().framebuffer; }
150 /** Sets the shader program to use. As a convenience, uniform values may be
151 specified at the same time. */
152 void set_shader_program(const Program *prog, const ProgramData *data = 0);
154 /** Adds uniform values, which will be available for shader programs. If
155 multiple ProgramData objects with the same uniforms are added, the one added
156 last will be used. */
157 void add_shader_data(const ProgramData &data);
159 void set_texture(Tag, const Texture *, const Sampler * = 0);
162 void flush_shader_data();
163 void flush_textures();
166 void set_vertex_setup(const VertexSetup *);
167 void set_front_face(FaceWinding);
168 void set_face_cull(CullMode);
170 void set_depth_test(const DepthTest *);
171 void set_stencil_test(const StencilTest *);
172 void set_blend(const Blend *);
174 void set_object_lod_bias(unsigned);
175 unsigned get_object_lod_bias() const { return get_state().object_lod_bias; }
177 void clear(const ClearValue *);
179 /** Draws a batch of primitives. A shader must be active. */
180 void draw(const Batch &);
182 /** Draws multiple instances of a batch of primitives. A shader must be active. */
183 void draw_instanced(const Batch &, unsigned);
185 /** Resolves multisample attachments from the active framebuffer into
187 void resolve_multisample(Framebuffer &target);
189 void begin_query(const QueryPool &, unsigned);
190 void end_query(const QueryPool &, unsigned);