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