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