]> git.tdb.fi Git - libs/gl.git/blob - source/core/pipelinestate.cpp
Store default-block uniform data in a memory block
[libs/gl.git] / source / core / pipelinestate.cpp
1 #include <stdexcept>
2 #include <msp/core/algorithm.h>
3 #include <msp/gl/extensions/arb_direct_state_access.h>
4 #include <msp/gl/extensions/arb_sampler_objects.h>
5 #include <msp/gl/extensions/arb_shader_objects.h>
6 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
7 #include <msp/gl/extensions/arb_vertex_array_object.h>
8 #include <msp/gl/extensions/msp_primitive_restart.h>
9 #include "blend.h"
10 #include "buffer.h"
11 #include "deviceinfo.h"
12 #include "depthtest.h"
13 #include "framebuffer.h"
14 #include "pipelinestate.h"
15 #include "program.h"
16 #include "rect.h"
17 #include "sampler.h"
18 #include "stenciltest.h"
19 #include "texture.h"
20 #include "uniformblock.h"
21 #include "vertexsetup.h"
22
23 using namespace std;
24
25 namespace Msp {
26 namespace GL {
27
28 const PipelineState *PipelineState::last_applied = 0;
29 vector<int> PipelineState::bound_tex_targets;
30 vector<char> PipelineState::bound_uniform_blocks;
31 unsigned PipelineState::restart_index = 0;
32
33 PipelineState::PipelineState():
34         framebuffer(0),
35         viewport(0),
36         scissor(0),
37         shprog(0),
38         vertex_setup(0),
39         front_face(COUNTERCLOCKWISE),
40         face_cull(NO_CULL),
41         enabled_clip_planes(0),
42         depth_test(0),
43         stencil_test(0),
44         blend(0),
45         changes(0)
46 {
47         if(bound_tex_targets.empty())
48                 bound_tex_targets.resize(Limits::get_global().max_texture_bindings);
49         if(bound_uniform_blocks.empty())
50                 bound_uniform_blocks.resize(Limits::get_global().max_uniform_bindings);
51 }
52
53 PipelineState::~PipelineState()
54 {
55         if(this==last_applied)
56                 last_applied = 0;
57 }
58
59 template<typename T>
60 void PipelineState::set(T &target, T value, unsigned flag)
61 {
62         if(value!=target)
63         {
64                 target = value;
65                 changes |= flag;
66         }
67 }
68
69 void PipelineState::set_framebuffer(const Framebuffer *f)
70 {
71         set(framebuffer, f, FRAMEBUFFER|VIEWPORT);
72 }
73
74 void PipelineState::set_viewport(const Rect *v)
75 {
76         set(viewport, v, VIEWPORT);
77 }
78
79 void PipelineState::set_scissor(const Rect *s)
80 {
81         set(scissor, s, SCISSOR);
82 }
83
84 void PipelineState::set_shader_program(const Program *p)
85 {
86         set(shprog, p, SHPROG);
87 }
88
89 void PipelineState::set_vertex_setup(const VertexSetup *s)
90 {
91         set(vertex_setup, s, VERTEX_SETUP);
92 }
93
94 void PipelineState::set_front_face(FaceWinding w)
95 {
96         set(front_face, w, FACE_CULL);
97 }
98
99 void PipelineState::set_face_cull(CullMode c)
100 {
101         set(face_cull, c, FACE_CULL);
102 }
103
104 void PipelineState::set_enabled_clip_planes(unsigned p)
105 {
106         set(enabled_clip_planes, p, CLIP_PLANES);
107 }
108
109 void PipelineState::set_texture(unsigned binding, const Texture *tex, const Sampler *samp)
110 {
111         if((tex!=0)!=(samp!=0))
112                 throw invalid_argument("PipelineState::set_texture");
113
114         auto i = lower_bound_member(textures, binding, &BoundTexture::binding);
115         if(i==textures.end() || i->binding!=binding)
116                 i = textures.insert(i, BoundTexture(binding));
117         if(tex!=i->texture || samp!=i->sampler)
118         {
119                 i->texture = tex;
120                 i->sampler = samp;
121                 i->changed = true;
122                 changes |= TEXTURES;
123         }
124 }
125
126 void PipelineState::set_uniform_block(int binding, const UniformBlock *block)
127 {
128         auto i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
129         if(i==uniform_blocks.end() || i->binding!=binding)
130                 i = uniform_blocks.insert(i, BoundUniformBlock(binding));
131         if(block!=i->block || binding<0)
132         {
133                 i->block = block;
134                 i->changed = true;
135                 changes |= UNIFORMS;
136         }
137 }
138
139 void PipelineState::set_depth_test(const DepthTest *dt)
140 {
141         set(depth_test, dt, DEPTH_TEST);
142 }
143
144 void PipelineState::set_stencil_test(const StencilTest *st)
145 {
146         set(stencil_test, st, STENCIL_TEST);
147 }
148
149 void PipelineState::set_blend(const Blend *b)
150 {
151         set(blend, b, BLEND);
152 }
153
154 void PipelineState::apply() const
155 {
156         if(!last_applied)
157                 Texture::unbind_scratch();
158
159         apply(this==last_applied ? changes : ~0U);
160 }
161
162 void PipelineState::apply(unsigned mask) const
163 {
164         if(mask&FRAMEBUFFER)
165         {
166                 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
167                 if(framebuffer)
168                 {
169                         framebuffer->refresh();
170                         framebuffer->require_complete();
171                 }
172         }
173
174         if(mask&VIEWPORT)
175         {
176                 if(viewport)
177                         glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
178                 else if(framebuffer)
179                         glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
180         }
181
182         if(mask&SCISSOR)
183         {
184                 if(scissor)
185                 {
186                         glEnable(GL_SCISSOR_TEST);
187                         glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
188                 }
189                 else
190                         glDisable(GL_SCISSOR_TEST);
191         }
192
193         if(mask&SHPROG)
194                 glUseProgram(shprog ? shprog->id : 0);
195
196         if(mask&VERTEX_SETUP)
197         {
198                 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
199                 if(vertex_setup)
200                 {
201                         static Require _req(MSP_primitive_restart);
202
203                         vertex_setup->refresh();
204                         unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
205                         if(ri!=restart_index)
206                         {
207                                 if(!restart_index)
208                                         glEnable(GL_PRIMITIVE_RESTART);
209                                 glPrimitiveRestartIndex(ri);
210                                 restart_index = ri;
211                         }
212                 }
213         }
214
215         if(mask&FACE_CULL)
216         {
217                 glFrontFace(front_face==CLOCKWISE ? GL_CW : GL_CCW);
218
219                 if(face_cull!=NO_CULL && front_face!=NON_MANIFOLD)
220                 {
221                         glEnable(GL_CULL_FACE);
222                         glCullFace(face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
223                 }
224                 else
225                         glDisable(GL_CULL_FACE);
226         }
227
228         if(mask&CLIP_PLANES)
229         {
230                 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
231                 for(unsigned i=0; i<max_clip_planes; ++i)
232                 {
233                         if((enabled_clip_planes>>i)&1)
234                                 glEnable(GL_CLIP_PLANE0+i);
235                         else
236                                 glDisable(GL_CLIP_PLANE0+i);
237                 }
238         }
239
240         if(mask&TEXTURES)
241         {
242                 for(const BoundTexture &t: textures)
243                         if(t.changed || mask==~0U)
244                         {
245                                 if(t.texture && t.sampler)
246                                 {
247                                         if(ARB_direct_state_access)
248                                                 glBindTextureUnit(t.binding, t.texture->id);
249                                         else
250                                         {
251                                                 glActiveTexture(GL_TEXTURE0+t.binding);
252                                                 if(bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=bound_tex_targets[t.binding])
253                                                         glBindTexture(bound_tex_targets[t.binding], 0);
254                                                 glBindTexture(t.texture->target, t.texture->id);
255                                         }
256
257                                         bound_tex_targets[t.binding] = t.texture->target;
258
259                                         glBindSampler(t.binding, t.sampler->id);
260                                         t.sampler->refresh();
261                                 }
262
263                                 t.changed = false;
264                         }
265         }
266
267         if(mask&UNIFORMS)
268         {
269                 for(const BoundUniformBlock &u: uniform_blocks)
270                         if(u.changed || mask==~0U)
271                         {
272                                 if(u.block)
273                                 {
274                                         if(u.binding>=0)
275                                         {
276                                                 glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size());
277                                                 bound_uniform_blocks[u.binding] = 1;
278                                         }
279                                         else if(shprog)
280                                         {
281                                                 const char *data = static_cast<const char *>(u.block->get_data_pointer());
282                                                 for(const Program::UniformCall &call: shprog->uniform_calls)
283                                                         call.func(call.location, call.size, data+call.location*16);
284                                         }
285                                 }
286
287                                 u.changed = false;
288                         }
289         }
290
291         if(mask&DEPTH_TEST)
292         {
293                 if(depth_test && depth_test->enabled)
294                 {
295                         glEnable(GL_DEPTH_TEST);
296                         glDepthFunc(get_gl_predicate(depth_test->compare));
297                 }
298                 else
299                         glDisable(GL_DEPTH_TEST);
300
301                 glDepthMask(!depth_test || depth_test->write);
302         }
303
304         if(mask&STENCIL_TEST)
305         {
306                 if(stencil_test && stencil_test->enabled)
307                 {
308                         glEnable(GL_STENCIL_TEST);
309                         glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF);
310                         glStencilOp(get_gl_stencil_op(stencil_test->stencil_fail_op), get_gl_stencil_op(stencil_test->depth_fail_op), get_gl_stencil_op(stencil_test->depth_pass_op));
311                 }
312                 else
313                         glDisable(GL_STENCIL_TEST);
314         }
315
316         if(mask&BLEND)
317         {
318                 if(blend && blend->enabled)
319                 {
320                         glEnable(GL_BLEND);
321                         glBlendEquation(get_gl_blend_equation(blend->equation));
322                         glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor));
323                         glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a);
324                 }
325                 else
326                         glDisable(GL_BLEND);
327         }
328
329         last_applied = this;
330         changes &= ~mask;
331 }
332
333 void PipelineState::clear()
334 {
335         if(last_applied)
336         {
337                 glUseProgram(0);
338                 glBindVertexArray(0);
339
340                 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
341                 for(unsigned i=0; i<max_clip_planes; ++i)
342                         if((last_applied->enabled_clip_planes>>i)&1)
343                                 glDisable(GL_CLIP_PLANE0+i);
344
345                 for(unsigned i=0; i<bound_tex_targets.size(); ++i)
346                         if(bound_tex_targets[i])
347                         {
348                                 if(ARB_direct_state_access)
349                                         glBindTextureUnit(i, 0);
350                                 else
351                                 {
352                                         glActiveTexture(GL_TEXTURE0+i);
353                                         glBindTexture(bound_tex_targets[i], 0);
354                                 }
355                                 bound_tex_targets[i] = 0;
356                         }
357
358                 for(unsigned i=0; i<bound_uniform_blocks.size(); ++i)
359                         if(bound_uniform_blocks[i])
360                         {
361                                 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
362                                 bound_uniform_blocks[i] = 0;
363                         }
364
365                 glDisable(GL_DEPTH_TEST);
366                 glDepthMask(true);
367                 glDisable(GL_STENCIL_TEST);
368                 glDisable(GL_BLEND);
369
370                 last_applied = 0;
371         }
372 }
373
374
375 PipelineState::BoundTexture::BoundTexture(unsigned b):
376         binding(b),
377         changed(false),
378         texture(0),
379         sampler(0)
380 { }
381
382
383 PipelineState::BoundUniformBlock::BoundUniformBlock(int b):
384         binding(b),
385         changed(false),
386         block(0)
387 { }
388
389 } // namespace GL
390 } // namespace Msp