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