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