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