]> git.tdb.fi Git - libs/gl.git/blob - source/render/renderer.h
InstanceArray doesn't need to refresh the vertex arrays
[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 "programdata.h"
9 #include "renderer_backend.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 Rendering supervisor.  This is the primary interface for setting state and
31 issuing draw commands.
32
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.
36
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
40 by outer scopes.
41 */
42 class Renderer: public RendererBackend
43 {
44         friend RendererBackend;
45
46 public:
47         /**
48         RAII helper class for pushing state on the stack.
49         */
50         class Push
51         {
52         private:
53                 Renderer &renderer;
54
55         public:
56                 Push(Renderer &r): renderer(r) { renderer.push_state(); }
57                 ~Push() { renderer.pop_state(); }
58         };
59
60 private:
61         struct BoundTexture
62         {
63                 Tag tag;
64                 mutable int binding = -1;
65                 const Texture *texture = 0;
66                 const Sampler *sampler = 0;
67                 int level = -1;
68                 int replaced = -1;
69         };
70
71         struct BoundProgramData
72         {
73                 const ProgramData *shdata;
74                 mutable unsigned generation = 0;
75
76                 BoundProgramData(const ProgramData *);
77         };
78
79         struct State
80         {
81                 const Camera *camera = 0;
82                 Matrix model_matrix;
83                 const Framebuffer *framebuffer = 0;
84                 const Rect *viewport = 0;
85                 const Rect *scissor = 0;
86                 unsigned texture_count = 0;
87                 const Program *shprog = 0;
88                 unsigned shdata_count = 0;
89                 const VertexSetup *vertex_setup = 0;
90                 FaceWinding front_face = NON_MANIFOLD;
91                 CullMode face_cull = NO_CULL;
92                 const DepthTest *depth_test = 0;
93                 const StencilTest *stencil_test = 0;
94                 const Blend *blend = 0;
95                 unsigned object_lod_bias = 0;
96         };
97
98         enum ChangeMask
99         {
100                 MATRIX = 2,
101                 SHADER_DATA = 16
102         };
103
104         unsigned frame_index = 0;
105         unsigned char changed = 0;
106         std::vector<State> state_stack;
107         State *current_state = 0;
108         ProgramData standard_shdata;
109         std::vector<BoundProgramData> shdata_stack;
110         std::vector<BoundTexture> texture_stack;
111         Commands commands;
112
113 public:
114         Renderer();
115
116         /** Begins rendering, allowing commands to be issued. */
117         void begin();
118
119         /** Ends rendering.  Any global state is reset to defaults.  No further
120         commands are allowed before the next call to begin(). */
121         void end();
122
123         using RendererBackend::begin;
124         using RendererBackend::end;
125
126         /** Saves the current state so it can be restored later. */
127         void push_state();
128
129         /** Restores a previously saved state.  Must be matched with an earlier
130         push_state call. */
131         void pop_state();
132
133 private:
134         State &get_state() const;
135
136 public:
137         /** Sets the camera to render from.  The model matrix is reset to identity. */
138         void set_camera(const Camera &);
139
140         const Camera *get_camera() const { return get_state().camera; }
141
142         /** Replaces the Renderer's model matrix. */
143         void set_matrix(const Matrix &);
144
145         /** Returns the current model matrix. */
146         const Matrix &get_matrix() const { return get_state().model_matrix; }
147
148         void set_framebuffer(const Framebuffer *);
149         void set_viewport(const Rect *);
150         void set_scissor(const Rect *);
151
152         const Framebuffer *get_framebuffer() const { return get_state().framebuffer; }
153
154         /** Sets the shader program to use.  As a convenience, uniform values may be
155         specified at the same time. */
156         void set_shader_program(const Program *prog, const ProgramData *data = 0);
157
158         /** Adds uniform values, which will be available for shader programs.  If
159         multiple ProgramData objects with the same uniforms are added, the one added
160         last will be used. */
161         void add_shader_data(const ProgramData &data);
162
163         void set_texture(Tag, const Texture *, const Sampler * = 0);
164         void set_texture(Tag, const Texture *, int, const Sampler * = 0);
165
166 private:
167         void flush_shader_data();
168         void flush_textures();
169
170 public:
171         void set_vertex_setup(const VertexSetup *);
172         void set_front_face(FaceWinding);
173         void set_face_cull(CullMode);
174
175         void set_depth_test(const DepthTest *);
176         void set_stencil_test(const StencilTest *);
177         void set_blend(const Blend *);
178
179         void set_object_lod_bias(unsigned);
180         unsigned get_object_lod_bias() const { return get_state().object_lod_bias; }
181
182         /** Clears framebuffer contents.  If values is not null, it must contain one
183         element for each attachment.  Otherwise the framebuffer contents are
184         discarded and become undefined. */
185         void clear(const ClearValue *values);
186
187         /** Draws a batch of primitives.  A shader must be active. */
188         void draw(const Batch &);
189
190         /** Draws multiple instances of a batch of primitives.  A shader must be active. */
191         void draw_instanced(const Batch &, unsigned);
192
193         /** Resolves multisample attachments from the active framebuffer into
194         target. */
195         void resolve_multisample(Framebuffer &target);
196
197         void begin_query(const QueryPool &, unsigned);
198         void end_query(const QueryPool &, unsigned);
199
200 private:
201         void apply_state();
202 };
203
204 } // namespace GL
205 } // namespace Msp
206
207 #endif