1 #ifndef MSP_GL_RENDERER_H_
2 #define MSP_GL_RENDERER_H_
9 #include "programdata.h"
10 #include "renderer_backend.h"
35 Rendering supervisor. This is the primary interface for setting state and
36 issuing draw commands.
38 The Renderer class allows setting textures and uniform values by names (using
39 ProgramData for the latter). The names are resolved into binding points when
40 the resources are needed for a draw command.
42 A state stack is provided to help with state management in render graphs.
43 Renderables can save the state by pushing it on the stack before beginning
44 their work, and pop it afterwards to restore it without disturbing state set
47 class Renderer: public RendererBackend
49 friend RendererBackend;
53 RAII helper class for pushing state on the stack.
61 Push(Renderer &r): renderer(r) { renderer.push_state(); }
62 ~Push() { renderer.pop_state(); }
69 mutable int binding = -1;
70 const Texture *texture = 0;
71 const Sampler *sampler = 0;
76 struct BoundProgramData
78 const ProgramData *shdata;
79 mutable unsigned generation = 0;
81 BoundProgramData(const ProgramData *);
86 const Camera *camera = 0;
88 const Framebuffer *framebuffer = 0;
89 const Rect *viewport = 0;
90 const Rect *scissor = 0;
91 unsigned texture_count = 0;
92 const Program *shprog = 0;
93 unsigned shdata_count = 0;
94 const VertexSetup *vertex_setup = 0;
95 FaceWinding front_face = NON_MANIFOLD;
96 CullMode face_cull = NO_CULL;
97 const DepthTest *depth_test = 0;
98 const StencilTest *stencil_test = 0;
99 const Blend *blend = 0;
100 unsigned object_lod_bias = 0;
109 unsigned frame_index = 0;
110 unsigned char changed = 0;
111 std::vector<State> state_stack;
112 State *current_state = 0;
113 ProgramData standard_shdata;
114 std::vector<BoundProgramData> shdata_stack;
115 std::vector<BoundTexture> texture_stack;
116 const Texture &placeholder_texture;
122 /** Begins rendering, allowing commands to be issued. */
125 /** Ends rendering. Any global state is reset to defaults. No further
126 commands are allowed before the next call to begin(). */
129 using RendererBackend::begin;
130 using RendererBackend::end;
132 /** Saves the current state so it can be restored later. */
135 /** Restores a previously saved state. Must be matched with an earlier
140 State &get_state() const;
143 /** Sets the camera to render from. The model matrix is reset to identity. */
144 void set_camera(const Camera &);
146 const Camera *get_camera() const { return get_state().camera; }
148 /** Replaces the Renderer's model matrix. */
149 void set_matrix(const Matrix &);
151 /** Returns the current model matrix. */
152 const Matrix &get_matrix() const { return get_state().model_matrix; }
154 void set_framebuffer(const Framebuffer *);
155 void set_viewport(const Rect *);
156 void set_scissor(const Rect *);
158 const Framebuffer *get_framebuffer() const { return get_state().framebuffer; }
160 /** Sets the shader program to use. As a convenience, uniform values may be
161 specified at the same time. */
162 void set_shader_program(const Program *prog, const ProgramData *data = 0);
164 /** Adds uniform values, which will be available for shader programs. If
165 multiple ProgramData objects with the same uniforms are added, the one added
166 last will be used. */
167 void add_shader_data(const ProgramData &data);
169 void set_texture(Tag, const Texture *, const Sampler * = 0);
170 void set_texture(Tag, const Texture *, int, const Sampler * = 0);
173 void flush_shader_data();
174 void flush_textures();
177 void set_vertex_setup(const VertexSetup *);
178 void set_front_face(FaceWinding);
179 void set_face_cull(CullMode);
181 void set_depth_test(const DepthTest *);
182 void set_stencil_test(const StencilTest *);
183 void set_blend(const Blend *);
185 void set_object_lod_bias(unsigned);
186 unsigned get_object_lod_bias() const { return get_state().object_lod_bias; }
188 /** Clears framebuffer contents. If values is not null, it must contain one
189 element for each attachment. Otherwise the framebuffer contents are
190 discarded and become undefined. */
191 void clear(const ClearValue *values);
193 /** Draws a batch of primitives. A shader must be active. */
194 void draw(const Batch &);
196 /** Draws multiple instances of a batch of primitives. A shader must be active. */
197 void draw_instanced(const Batch &, unsigned);
199 /** Resolves multisample attachments from the active framebuffer into
201 void resolve_multisample(Framebuffer &target);
203 void begin_query(const QueryPool &, unsigned);
204 void end_query(const QueryPool &, unsigned);
207 void apply_framebuffer();