]> git.tdb.fi Git - libs/gl.git/blob - source/render/renderer.h
Check the flat qualifier from the correct member
[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 replaced = -1;
68         };
69
70         struct BoundProgramData
71         {
72                 const ProgramData *shdata;
73                 mutable unsigned generation = 0;
74
75                 BoundProgramData(const ProgramData *);
76         };
77
78         struct State
79         {
80                 const Camera *camera = 0;
81                 Matrix model_matrix;
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;
95         };
96
97         enum ChangeMask
98         {
99                 MATRIX = 2,
100                 SHADER_DATA = 16
101         };
102
103         unsigned frame_index = 0;
104         unsigned char changed = 0;
105         std::vector<State> state_stack;
106         State *current_state = 0;
107         ProgramData standard_shdata;
108         std::vector<BoundProgramData> shdata_stack;
109         std::vector<BoundTexture> texture_stack;
110         Commands commands;
111
112 public:
113         Renderer();
114
115         /** Begins rendering, allowing commands to be issued. */
116         void begin();
117
118         /** Ends rendering.  Any global state is reset to defaults.  No further
119         commands are allowed before the next call to begin(). */
120         void end();
121
122         using RendererBackend::begin;
123         using RendererBackend::end;
124
125         /** Saves the current state so it can be restored later. */
126         void push_state();
127
128         /** Restores a previously saved state.  Must be matched with an earlier
129         push_state call. */
130         void pop_state();
131
132 private:
133         State &get_state() const;
134
135 public:
136         /** Sets the camera to render from.  The model matrix is reset to identity. */
137         void set_camera(const Camera &);
138
139         const Camera *get_camera() const { return get_state().camera; }
140
141         /** Replaces the Renderer's model matrix. */
142         void set_matrix(const Matrix &);
143
144         /** Returns the current model matrix. */
145         const Matrix &get_matrix() const { return get_state().model_matrix; }
146
147         void set_framebuffer(const Framebuffer *);
148         void set_viewport(const Rect *);
149         void set_scissor(const Rect *);
150
151         const Framebuffer *get_framebuffer() const { return get_state().framebuffer; }
152
153         /** Sets the shader program to use.  As a convenience, uniform values may be
154         specified at the same time. */
155         void set_shader_program(const Program *prog, const ProgramData *data = 0);
156
157         /** Adds uniform values, which will be available for shader programs.  If
158         multiple ProgramData objects with the same uniforms are added, the one added
159         last will be used. */
160         void add_shader_data(const ProgramData &data);
161
162         void set_texture(Tag, const Texture *, const Sampler * = 0);
163
164 private:
165         void flush_shader_data();
166         void flush_textures();
167
168 public:
169         void set_vertex_setup(const VertexSetup *);
170         void set_front_face(FaceWinding);
171         void set_face_cull(CullMode);
172
173         void set_depth_test(const DepthTest *);
174         void set_stencil_test(const StencilTest *);
175         void set_blend(const Blend *);
176
177         void set_object_lod_bias(unsigned);
178         unsigned get_object_lod_bias() const { return get_state().object_lod_bias; }
179
180         /** Clears framebuffer contents.  If values is not null, it must contain one
181         element for each attachment.  Otherwise the framebuffer contents are
182         discarded and become undefined. */
183         void clear(const ClearValue *values);
184
185         /** Draws a batch of primitives.  A shader must be active. */
186         void draw(const Batch &);
187
188         /** Draws multiple instances of a batch of primitives.  A shader must be active. */
189         void draw_instanced(const Batch &, unsigned);
190
191         /** Resolves multisample attachments from the active framebuffer into
192         target. */
193         void resolve_multisample(Framebuffer &target);
194
195         void begin_query(const QueryPool &, unsigned);
196         void end_query(const QueryPool &, unsigned);
197
198 private:
199         void apply_state();
200 };
201
202 } // namespace GL
203 } // namespace Msp
204
205 #endif