]> git.tdb.fi Git - libs/gl.git/blob - source/core/pipelinestate.cpp
Remove remaining deprecated things from the core classes
[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_uniforms(const DefaultUniformBlock *block)
127 {
128         set_uniform_block_(-1, block);
129 }
130
131 void PipelineState::set_uniform_block(unsigned binding, const BufferBackedUniformBlock *block)
132 {
133         set_uniform_block_(binding, block);
134 }
135
136 void PipelineState::set_uniform_block_(int binding, const UniformBlock *block)
137 {
138         auto i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
139         if(i==uniform_blocks.end() || i->binding!=binding)
140                 i = uniform_blocks.insert(i, BoundUniformBlock(binding));
141         if(block!=i->block || binding<0)
142         {
143                 i->block = block;
144                 i->changed = true;
145                 changes |= UNIFORMS;
146         }
147 }
148
149 void PipelineState::set_depth_test(const DepthTest *dt)
150 {
151         set(depth_test, dt, DEPTH_TEST);
152 }
153
154 void PipelineState::set_stencil_test(const StencilTest *st)
155 {
156         set(stencil_test, st, STENCIL_TEST);
157 }
158
159 void PipelineState::set_blend(const Blend *b)
160 {
161         set(blend, b, BLEND);
162 }
163
164 void PipelineState::apply() const
165 {
166         if(!last_applied)
167                 Texture::unbind_scratch();
168
169         apply(this==last_applied ? changes : ~0U);
170 }
171
172 void PipelineState::apply(unsigned mask) const
173 {
174         if(mask&FRAMEBUFFER)
175         {
176                 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->get_id() : 0);
177                 if(framebuffer)
178                 {
179                         framebuffer->refresh();
180                         framebuffer->require_complete();
181                 }
182         }
183
184         if(mask&VIEWPORT)
185         {
186                 if(viewport)
187                         glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
188                 else if(framebuffer)
189                         glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
190         }
191
192         if(mask&SCISSOR)
193         {
194                 if(scissor)
195                 {
196                         glEnable(GL_SCISSOR_TEST);
197                         glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
198                 }
199                 else
200                         glDisable(GL_SCISSOR_TEST);
201         }
202
203         if(mask&SHPROG)
204                 glUseProgram(shprog ? shprog->get_id() : 0);
205
206         if(mask&VERTEX_SETUP)
207         {
208                 glBindVertexArray(vertex_setup ? vertex_setup->get_id() : 0);
209                 if(vertex_setup)
210                 {
211                         static Require _req(MSP_primitive_restart);
212
213                         vertex_setup->refresh();
214                         unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
215                         if(ri!=restart_index)
216                         {
217                                 if(!restart_index)
218                                         glEnable(GL_PRIMITIVE_RESTART);
219                                 glPrimitiveRestartIndex(ri);
220                                 restart_index = ri;
221                         }
222                 }
223         }
224
225         if(mask&FACE_CULL)
226         {
227                 glFrontFace(front_face==CLOCKWISE ? GL_CW : GL_CCW);
228
229                 if(face_cull!=NO_CULL && front_face!=NON_MANIFOLD)
230                 {
231                         glEnable(GL_CULL_FACE);
232                         glCullFace(face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
233                 }
234                 else
235                         glDisable(GL_CULL_FACE);
236         }
237
238         if(mask&CLIP_PLANES)
239         {
240                 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
241                 for(unsigned i=0; i<max_clip_planes; ++i)
242                 {
243                         if((enabled_clip_planes>>i)&1)
244                                 glEnable(GL_CLIP_PLANE0+i);
245                         else
246                                 glDisable(GL_CLIP_PLANE0+i);
247                 }
248         }
249
250         if(mask&TEXTURES)
251         {
252                 for(const BoundTexture &t: textures)
253                         if(t.changed || mask==~0U)
254                         {
255                                 if(t.texture && t.sampler)
256                                 {
257                                         if(ARB_direct_state_access)
258                                                 glBindTextureUnit(t.binding, t.texture->get_id());
259                                         else
260                                         {
261                                                 glActiveTexture(GL_TEXTURE0+t.binding);
262                                                 if(bound_tex_targets[t.binding] && static_cast<int>(t.texture->get_target())!=bound_tex_targets[t.binding])
263                                                         glBindTexture(bound_tex_targets[t.binding], 0);
264                                                 glBindTexture(t.texture->get_target(), t.texture->get_id());
265                                         }
266
267                                         bound_tex_targets[t.binding] = t.texture->get_target();
268
269                                         glBindSampler(t.binding, t.sampler->get_id());
270                                         t.sampler->refresh();
271                                 }
272
273                                 t.changed = false;
274                         }
275         }
276
277         if(mask&UNIFORMS)
278         {
279                 for(const BoundUniformBlock &u: uniform_blocks)
280                         if(u.changed || mask==~0U)
281                         {
282                                 if(u.block)
283                                 {
284                                         if(u.binding>=0)
285                                         {
286                                                 const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(u.block);
287                                                 glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, block->get_buffer()->get_id(), block->get_offset(), block->get_data_size());
288                                                 bound_uniform_blocks[u.binding] = 1;
289                                         }
290                                         else
291                                                 static_cast<const DefaultUniformBlock *>(u.block)->apply();
292                                 }
293
294                                 u.changed = false;
295                         }
296         }
297
298         if(mask&DEPTH_TEST)
299         {
300                 if(depth_test && depth_test->enabled)
301                 {
302                         glEnable(GL_DEPTH_TEST);
303                         glDepthFunc(get_gl_predicate(depth_test->compare));
304                 }
305                 else
306                         glDisable(GL_DEPTH_TEST);
307
308                 glDepthMask(!depth_test || depth_test->write);
309         }
310
311         if(mask&STENCIL_TEST)
312         {
313                 if(stencil_test && stencil_test->enabled)
314                 {
315                         glEnable(GL_STENCIL_TEST);
316                         glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF);
317                         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));
318                 }
319                 else
320                         glDisable(GL_STENCIL_TEST);
321         }
322
323         if(mask&BLEND)
324         {
325                 if(blend && blend->enabled)
326                 {
327                         glEnable(GL_BLEND);
328                         glBlendEquation(get_gl_blend_equation(blend->equation));
329                         glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor));
330                         glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a);
331                 }
332                 else
333                         glDisable(GL_BLEND);
334         }
335
336         last_applied = this;
337         changes &= ~mask;
338 }
339
340 void PipelineState::clear()
341 {
342         if(last_applied)
343         {
344                 glUseProgram(0);
345                 glBindVertexArray(0);
346
347                 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
348                 for(unsigned i=0; i<max_clip_planes; ++i)
349                         if((last_applied->enabled_clip_planes>>i)&1)
350                                 glDisable(GL_CLIP_PLANE0+i);
351
352                 for(unsigned i=0; i<bound_tex_targets.size(); ++i)
353                         if(bound_tex_targets[i])
354                         {
355                                 if(ARB_direct_state_access)
356                                         glBindTextureUnit(i, 0);
357                                 else
358                                 {
359                                         glActiveTexture(GL_TEXTURE0+i);
360                                         glBindTexture(bound_tex_targets[i], 0);
361                                 }
362                                 bound_tex_targets[i] = 0;
363                         }
364
365                 for(unsigned i=0; i<bound_uniform_blocks.size(); ++i)
366                         if(bound_uniform_blocks[i])
367                         {
368                                 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
369                                 bound_uniform_blocks[i] = 0;
370                         }
371
372                 glDisable(GL_DEPTH_TEST);
373                 glDepthMask(true);
374                 glDisable(GL_STENCIL_TEST);
375                 glDisable(GL_BLEND);
376
377                 last_applied = 0;
378         }
379 }
380
381
382 PipelineState::BoundTexture::BoundTexture(unsigned b):
383         binding(b),
384         changed(false),
385         texture(0),
386         sampler(0)
387 { }
388
389
390 PipelineState::BoundUniformBlock::BoundUniformBlock(int b):
391         binding(b),
392         changed(false),
393         block(0)
394 { }
395
396 } // namespace GL
397 } // namespace Msp