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