]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/pipelinestate_backend.cpp
Unify handling of shader resources in PipelineState
[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::RESOURCES)
109         {
110                 for(const PipelineState::BoundResource &r: self.resources)
111                 {
112                         if(!r.changed && changes!=~0U)
113                                 continue;
114
115                         if(r.used)
116                         {
117                                 if(r.type==PipelineState::UNIFORM_BLOCK)
118                                 {
119                                         if(r.binding>=0)
120                                         {
121                                                 glBindBufferRange(GL_UNIFORM_BUFFER, r.binding, r.buffer->id, r.block->get_offset(), r.block->get_data_size());
122                                                 dev_state.bound_uniform_blocks[r.binding] = 1;
123                                         }
124                                         else if(r.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
125                                         {
126                                                 const char *data = static_cast<const char *>(r.block->get_data_pointer());
127                                                 for(const Program::UniformCall &call: self.shprog->uniform_calls)
128                                                         call.func(call.location, call.size, data+call.location*16);
129                                         }
130                                 }
131                                 else if(r.type==PipelineState::TEXTURE)
132                                 {
133                                         if(ARB_direct_state_access)
134                                                 glBindTextureUnit(r.binding, r.texture->id);
135                                         else
136                                         {
137                                                 glActiveTexture(GL_TEXTURE0+r.binding);
138                                                 if(dev_state.bound_tex_targets[r.binding] && static_cast<int>(r.texture->target)!=dev_state.bound_tex_targets[r.binding])
139                                                         glBindTexture(dev_state.bound_tex_targets[r.binding], 0);
140                                                 glBindTexture(r.texture->target, r.texture->id);
141                                         }
142
143                                         dev_state.bound_tex_targets[r.binding] = r.texture->target;
144
145                                         glBindSampler(r.binding, r.sampler->id);
146                                         r.sampler->refresh();
147                                 }
148                         }
149
150                         r.changed = false;
151                 }
152         }
153
154         if(changes&PipelineState::VERTEX_SETUP)
155         {
156                 const VertexSetup *vertex_setup = self.vertex_setup;
157                 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
158                 if(vertex_setup)
159                 {
160                         static Require _req(MSP_primitive_restart);
161
162                         vertex_setup->refresh();
163                         unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
164                         if(ri!=dev_state.restart_index)
165                         {
166                                 if(!dev_state.restart_index)
167                                         glEnable(GL_PRIMITIVE_RESTART);
168                                 glPrimitiveRestartIndex(ri);
169                                 dev_state.restart_index = ri;
170                         }
171                 }
172         }
173
174         if(changes&PipelineState::FACE_CULL)
175         {
176                 glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
177
178                 if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD)
179                 {
180                         glEnable(GL_CULL_FACE);
181                         glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
182                 }
183                 else
184                         glDisable(GL_CULL_FACE);
185         }
186
187         if(changes&PipelineState::DEPTH_TEST)
188         {
189                 const DepthTest &depth_test = self.depth_test;
190                 if(depth_test.enabled)
191                 {
192                         glEnable(GL_DEPTH_TEST);
193                         glDepthFunc(get_gl_predicate(depth_test.compare));
194                 }
195                 else
196                         glDisable(GL_DEPTH_TEST);
197
198                 glDepthMask(depth_test.write);
199         }
200
201         if(changes&PipelineState::STENCIL_TEST)
202         {
203                 const StencilTest &stencil_test = self.stencil_test;
204                 if(stencil_test.enabled)
205                 {
206                         glEnable(GL_STENCIL_TEST);
207                         glStencilFunc(get_gl_predicate(stencil_test.compare), stencil_test.reference, 0xFFFFFFFF);
208                         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));
209                 }
210                 else
211                         glDisable(GL_STENCIL_TEST);
212         }
213
214         if(changes&PipelineState::BLEND)
215         {
216                 const Blend &blend = self.blend;
217                 if(blend.enabled)
218                 {
219                         glEnable(GL_BLEND);
220                         glBlendEquation(get_gl_blend_equation(blend.equation));
221                         glBlendFunc(get_gl_blend_factor(blend.src_factor), get_gl_blend_factor(blend.dst_factor));
222                         glBlendColor(blend.constant.r, blend.constant.g, blend.constant.b, blend.constant.a);
223                         ColorWriteMask cw = blend.write_mask;
224                         glColorMask((cw&WRITE_RED)!=0, (cw&WRITE_GREEN)!=0, (cw&WRITE_BLUE)!=0, (cw&WRITE_ALPHA)!=0);
225                 }
226                 else
227                 {
228                         glDisable(GL_BLEND);
229                         glColorMask(true, true, true, true);
230                 }
231         }
232
233         applied_to = &device;
234         dev_state.last_pipeline = this;
235         changes = 0;
236 }
237
238 void OpenGLPipelineState::clear()
239 {
240         OpenGLDeviceState &dev_state = Device::get_current().get_state();
241         if(dev_state.last_pipeline)
242         {
243                 glUseProgram(0);
244                 glBindVertexArray(0);
245
246                 for(unsigned i=0; i<dev_state.n_clip_distances; ++i)
247                         glDisable(GL_CLIP_PLANE0+i);
248                 dev_state.n_clip_distances = 0;
249
250                 for(unsigned i=0; i<dev_state.bound_tex_targets.size(); ++i)
251                         if(dev_state.bound_tex_targets[i])
252                         {
253                                 if(ARB_direct_state_access)
254                                         glBindTextureUnit(i, 0);
255                                 else
256                                 {
257                                         glActiveTexture(GL_TEXTURE0+i);
258                                         glBindTexture(dev_state.bound_tex_targets[i], 0);
259                                 }
260                                 dev_state.bound_tex_targets[i] = 0;
261                         }
262
263                 for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
264                         if(dev_state.bound_uniform_blocks[i])
265                         {
266                                 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
267                                 dev_state.bound_uniform_blocks[i] = 0;
268                         }
269
270                 glDisable(GL_DEPTH_TEST);
271                 glDepthMask(true);
272                 glDisable(GL_STENCIL_TEST);
273                 glDisable(GL_BLEND);
274
275                 dev_state.last_pipeline->applied_to = 0;
276                 dev_state.last_pipeline = 0;
277         }
278 }
279
280 } // namespace GL
281 } // namespace Msp