]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/pipelinestate_backend.cpp
Don't try to apply push constants as uniforms in OpenGL
[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 const OpenGLPipelineState *OpenGLPipelineState::last_applied = 0;
30 vector<int> OpenGLPipelineState::bound_tex_targets;
31 vector<char> OpenGLPipelineState::bound_uniform_blocks;
32 unsigned OpenGLPipelineState::restart_index = 0;
33 unsigned OpenGLPipelineState::n_clip_distances = 0;
34
35 OpenGLPipelineState::OpenGLPipelineState()
36 {
37         if(bound_tex_targets.empty())
38                 bound_tex_targets.resize(Device::get_current().get_info().limits.max_texture_bindings);
39         if(bound_uniform_blocks.empty())
40                 bound_uniform_blocks.resize(Device::get_current().get_info().limits.max_uniform_bindings);
41 }
42
43 OpenGLPipelineState::~OpenGLPipelineState()
44 {
45         if(this==last_applied)
46                 last_applied = 0;
47 }
48
49 void OpenGLPipelineState::apply() const
50 {
51         if(!last_applied)
52                 OpenGLTexture::unbind_scratch();
53
54         apply(this==last_applied ? static_cast<const PipelineState *>(this)->changes : ~0U);
55 }
56
57 void OpenGLPipelineState::apply(unsigned mask) const
58 {
59         const PipelineState &self = *static_cast<const PipelineState *>(this);
60
61         if(mask&PipelineState::FRAMEBUFFER)
62         {
63                 const Framebuffer *framebuffer = self.framebuffer;
64                 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
65                 if(framebuffer)
66                 {
67                         framebuffer->refresh();
68                         framebuffer->require_complete();
69                 }
70         }
71
72         if(mask&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
73         {
74                 if(const Rect *viewport = self.viewport)
75                         glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
76                 else if(const Framebuffer *framebuffer = self.framebuffer)
77                         glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
78         }
79
80         if(mask&PipelineState::SCISSOR)
81         {
82                 if(const Rect *scissor = self.scissor)
83                 {
84                         glEnable(GL_SCISSOR_TEST);
85                         glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
86                 }
87                 else
88                         glDisable(GL_SCISSOR_TEST);
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!=n_clip_distances)
97                 {
98                         for(unsigned i=0; (i<ncd || i<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                         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.block)
115                                 {
116                                         if(u.binding>=0)
117                                         {
118                                                 glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size());
119                                                 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.texture && t.sampler)
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(bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=bound_tex_targets[t.binding])
146                                                         glBindTexture(bound_tex_targets[t.binding], 0);
147                                                 glBindTexture(t.texture->target, t.texture->id);
148                                         }
149
150                                         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!=restart_index)
171                         {
172                                 if(!restart_index)
173                                         glEnable(GL_PRIMITIVE_RESTART);
174                                 glPrimitiveRestartIndex(ri);
175                                 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 && 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 || depth_test->write);
205         }
206
207         if(mask&PipelineState::STENCIL_TEST)
208         {
209                 const StencilTest *stencil_test = self.stencil_test;
210                 if(stencil_test && 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 && 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         last_applied = this;
240         self.changes &= ~mask;
241 }
242
243 void OpenGLPipelineState::clear()
244 {
245         if(last_applied)
246         {
247                 glUseProgram(0);
248                 glBindVertexArray(0);
249
250                 for(unsigned i=0; i<n_clip_distances; ++i)
251                         glDisable(GL_CLIP_PLANE0+i);
252                 n_clip_distances = 0;
253
254                 for(unsigned i=0; i<bound_tex_targets.size(); ++i)
255                         if(bound_tex_targets[i])
256                         {
257                                 if(ARB_direct_state_access)
258                                         glBindTextureUnit(i, 0);
259                                 else
260                                 {
261                                         glActiveTexture(GL_TEXTURE0+i);
262                                         glBindTexture(bound_tex_targets[i], 0);
263                                 }
264                                 bound_tex_targets[i] = 0;
265                         }
266
267                 for(unsigned i=0; i<bound_uniform_blocks.size(); ++i)
268                         if(bound_uniform_blocks[i])
269                         {
270                                 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
271                                 bound_uniform_blocks[i] = 0;
272                         }
273
274                 glDisable(GL_DEPTH_TEST);
275                 glDepthMask(true);
276                 glDisable(GL_STENCIL_TEST);
277                 glDisable(GL_BLEND);
278
279                 last_applied = 0;
280         }
281 }
282
283 } // namespace GL
284 } // namespace Msp