]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/pipelinestate_backend.cpp
fb802995f6355e46a6794f07e6f4ec5d1b4e7593
[libs/gl.git] / source / backends / opengl / pipelinestate_backend.cpp
1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_sampler_objects.h>
3 #include <msp/gl/extensions/arb_shader_objects.h>
4 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
5 #include <msp/gl/extensions/arb_vertex_array_object.h>
6 #include <msp/gl/extensions/ext_framebuffer_object.h>
7 #include <msp/gl/extensions/msp_primitive_restart.h>
8 #include "blend.h"
9 #include "buffer.h"
10 #include "depthtest.h"
11 #include "device.h"
12 #include "framebuffer.h"
13 #include "gl.h"
14 #include "pipelinestate.h"
15 #include "pipelinestate_backend.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 OpenGLPipelineState::~OpenGLPipelineState()
30 {
31         if(applied_to)
32                 applied_to->get_state().last_pipeline = 0;
33 }
34
35 void OpenGLPipelineState::apply() const
36 {
37         const PipelineState &self = *static_cast<const PipelineState *>(this);
38         Device &device = Device::get_current();
39         unsigned mask = changes;
40
41         if(applied_to && applied_to!=&device)
42         {
43                 applied_to->get_state().last_pipeline = 0;
44                 mask = ~0U;
45         }
46
47         OpenGLDeviceState &dev_state = device.get_state();
48         if(!dev_state.last_pipeline)
49                 OpenGLTexture::unbind_scratch();
50         
51         if(this!=dev_state.last_pipeline)
52         {
53                 if(dev_state.last_pipeline)
54                         dev_state.last_pipeline->applied_to = 0;
55                 mask = ~0U;
56         }
57
58         if(mask&PipelineState::FRAMEBUFFER)
59         {
60                 const Framebuffer *framebuffer = self.framebuffer;
61                 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
62                 if(framebuffer)
63                 {
64                         framebuffer->refresh();
65                         framebuffer->require_complete();
66                 }
67         }
68
69         if(mask&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
70         {
71                 if(const Rect *viewport = self.viewport)
72                         glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
73                 else if(const Framebuffer *framebuffer = self.framebuffer)
74                         glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
75         }
76
77         if(mask&PipelineState::SCISSOR)
78         {
79                 if(const Rect *scissor = self.scissor)
80                 {
81                         glEnable(GL_SCISSOR_TEST);
82                         glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
83                 }
84                 else
85                         glDisable(GL_SCISSOR_TEST);
86         }
87
88         if(mask&PipelineState::SHPROG)
89         {
90                 glUseProgram(self.shprog ? self.shprog->id : 0);
91
92                 unsigned ncd = (self.shprog ? self.shprog->get_n_clip_distances() : 0);
93                 if(ncd!=dev_state.n_clip_distances)
94                 {
95                         for(unsigned i=0; (i<ncd || i<dev_state.n_clip_distances); ++i)
96                         {
97                                 if(i<ncd)
98                                         glEnable(GL_CLIP_PLANE0+i);
99                                 else
100                                         glDisable(GL_CLIP_PLANE0+i);
101                         }
102                         dev_state.n_clip_distances = ncd;
103                 }
104         }
105
106         if(mask&PipelineState::UNIFORMS)
107         {
108                 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
109                         if(u.changed || mask==~0U)
110                         {
111                                 if(u.used)
112                                 {
113                                         if(u.binding>=0)
114                                         {
115                                                 glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.buffer->id, u.block->get_offset(), u.block->get_data_size());
116                                                 dev_state.bound_uniform_blocks[u.binding] = 1;
117                                         }
118                                         else if(u.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
119                                         {
120                                                 const char *data = static_cast<const char *>(u.block->get_data_pointer());
121                                                 for(const Program::UniformCall &call: self.shprog->uniform_calls)
122                                                         call.func(call.location, call.size, data+call.location*16);
123                                         }
124                                 }
125
126                                 u.changed = false;
127                         }
128         }
129
130         if(mask&PipelineState::TEXTURES)
131         {
132                 for(const PipelineState::BoundTexture &t: self.textures)
133                         if(t.changed || mask==~0U)
134                         {
135                                 if(t.used)
136                                 {
137                                         if(ARB_direct_state_access)
138                                                 glBindTextureUnit(t.binding, t.texture->id);
139                                         else
140                                         {
141                                                 glActiveTexture(GL_TEXTURE0+t.binding);
142                                                 if(dev_state.bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=dev_state.bound_tex_targets[t.binding])
143                                                         glBindTexture(dev_state.bound_tex_targets[t.binding], 0);
144                                                 glBindTexture(t.texture->target, t.texture->id);
145                                         }
146
147                                         dev_state.bound_tex_targets[t.binding] = t.texture->target;
148
149                                         glBindSampler(t.binding, t.sampler->id);
150                                         t.sampler->refresh();
151                                 }
152
153                                 t.changed = false;
154                         }
155         }
156
157         if(mask&PipelineState::VERTEX_SETUP)
158         {
159                 const VertexSetup *vertex_setup = self.vertex_setup;
160                 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
161                 if(vertex_setup)
162                 {
163                         static Require _req(MSP_primitive_restart);
164
165                         vertex_setup->refresh();
166                         unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
167                         if(ri!=dev_state.restart_index)
168                         {
169                                 if(!dev_state.restart_index)
170                                         glEnable(GL_PRIMITIVE_RESTART);
171                                 glPrimitiveRestartIndex(ri);
172                                 dev_state.restart_index = ri;
173                         }
174                 }
175         }
176
177         if(mask&PipelineState::FACE_CULL)
178         {
179                 glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
180
181                 if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD)
182                 {
183                         glEnable(GL_CULL_FACE);
184                         glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
185                 }
186                 else
187                         glDisable(GL_CULL_FACE);
188         }
189
190         if(mask&PipelineState::DEPTH_TEST)
191         {
192                 const DepthTest *depth_test = self.depth_test;
193                 if(depth_test && depth_test->enabled)
194                 {
195                         glEnable(GL_DEPTH_TEST);
196                         glDepthFunc(get_gl_predicate(depth_test->compare));
197                 }
198                 else
199                         glDisable(GL_DEPTH_TEST);
200
201                 glDepthMask(!depth_test || depth_test->write);
202         }
203
204         if(mask&PipelineState::STENCIL_TEST)
205         {
206                 const StencilTest *stencil_test = self.stencil_test;
207                 if(stencil_test && stencil_test->enabled)
208                 {
209                         glEnable(GL_STENCIL_TEST);
210                         glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF);
211                         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));
212                 }
213                 else
214                         glDisable(GL_STENCIL_TEST);
215         }
216
217         if(mask&PipelineState::BLEND)
218         {
219                 const Blend *blend = self.blend;
220                 if(blend && blend->enabled)
221                 {
222                         glEnable(GL_BLEND);
223                         glBlendEquation(get_gl_blend_equation(blend->equation));
224                         glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor));
225                         glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a);
226                         ColorWriteMask cw = blend->write_mask;
227                         glColorMask((cw&WRITE_RED)!=0, (cw&WRITE_GREEN)!=0, (cw&WRITE_BLUE)!=0, (cw&WRITE_ALPHA)!=0);
228                 }
229                 else
230                 {
231                         glDisable(GL_BLEND);
232                         glColorMask(true, true, true, true);
233                 }
234         }
235
236         applied_to = &device;
237         dev_state.last_pipeline = this;
238         changes = 0;
239 }
240
241 void OpenGLPipelineState::clear()
242 {
243         OpenGLDeviceState &dev_state = Device::get_current().get_state();
244         if(dev_state.last_pipeline)
245         {
246                 glUseProgram(0);
247                 glBindVertexArray(0);
248
249                 for(unsigned i=0; i<dev_state.n_clip_distances; ++i)
250                         glDisable(GL_CLIP_PLANE0+i);
251                 dev_state.n_clip_distances = 0;
252
253                 for(unsigned i=0; i<dev_state.bound_tex_targets.size(); ++i)
254                         if(dev_state.bound_tex_targets[i])
255                         {
256                                 if(ARB_direct_state_access)
257                                         glBindTextureUnit(i, 0);
258                                 else
259                                 {
260                                         glActiveTexture(GL_TEXTURE0+i);
261                                         glBindTexture(dev_state.bound_tex_targets[i], 0);
262                                 }
263                                 dev_state.bound_tex_targets[i] = 0;
264                         }
265
266                 for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
267                         if(dev_state.bound_uniform_blocks[i])
268                         {
269                                 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
270                                 dev_state.bound_uniform_blocks[i] = 0;
271                         }
272
273                 glDisable(GL_DEPTH_TEST);
274                 glDepthMask(true);
275                 glDisable(GL_STENCIL_TEST);
276                 glDisable(GL_BLEND);
277
278                 dev_state.last_pipeline->applied_to = 0;
279                 dev_state.last_pipeline = 0;
280         }
281 }
282
283 } // namespace GL
284 } // namespace Msp