]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/pipelinestate_backend.cpp
Add support for alpha to coverage
[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_image_load_store.h>
4 #include <msp/gl/extensions/arb_shader_objects.h>
5 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
6 #include <msp/gl/extensions/arb_vertex_array_object.h>
7 #include <msp/gl/extensions/ext_framebuffer_object.h>
8 #include <msp/gl/extensions/msp_primitive_restart.h>
9 #include "blend.h"
10 #include "buffer.h"
11 #include "depthtest.h"
12 #include "device.h"
13 #include "framebuffer.h"
14 #include "gl.h"
15 #include "pipelinestate.h"
16 #include "pipelinestate_backend.h"
17 #include "program.h"
18 #include "rect.h"
19 #include "sampler.h"
20 #include "stenciltest.h"
21 #include "texture.h"
22 #include "uniformblock.h"
23 #include "vertexsetup.h"
24
25 using namespace std;
26
27 namespace Msp {
28 namespace GL {
29
30 OpenGLPipelineState::~OpenGLPipelineState()
31 {
32         if(applied_to)
33                 applied_to->get_state().last_pipeline = 0;
34 }
35
36 void OpenGLPipelineState::apply() const
37 {
38         const PipelineState &self = *static_cast<const PipelineState *>(this);
39         Device &device = Device::get_current();
40
41         if(applied_to && applied_to!=&device)
42         {
43                 applied_to->get_state().last_pipeline = 0;
44                 changes = ~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                 changes = ~0U;
56         }
57
58         if(changes&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(changes&(PipelineState::VIEWPORT|PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
70                 if(const Framebuffer *framebuffer = self.framebuffer)
71                 {
72                         Rect fb_rect = framebuffer->get_rect();
73                         if(changes&(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(changes&(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(changes&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(changes&PipelineState::RESOURCES)
110         {
111                 for(const PipelineState::BoundResource &r: self.resources)
112                 {
113                         if(!r.changed && changes!=~0U)
114                                 continue;
115
116                         if(r.used)
117                         {
118                                 if(r.type==PipelineState::UNIFORM_BLOCK)
119                                 {
120                                         if(r.binding>=0)
121                                         {
122                                                 glBindBufferRange(GL_UNIFORM_BUFFER, r.binding, r.buffer->id, r.block->get_offset(), r.block->get_data_size());
123                                                 dev_state.bound_uniform_blocks[r.binding] = 1;
124                                         }
125                                         else if(r.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
126                                         {
127                                                 const char *data = static_cast<const char *>(r.block->get_data_pointer());
128                                                 for(const Program::UniformCall &call: self.shprog->uniform_calls)
129                                                         call.func(call.location, call.size, data+call.location*16);
130                                         }
131                                 }
132                                 else if(r.type==PipelineState::SAMPLED_TEXTURE)
133                                 {
134                                         if(ARB_direct_state_access)
135                                                 glBindTextureUnit(r.binding, r.texture->id);
136                                         else
137                                         {
138                                                 glActiveTexture(GL_TEXTURE0+r.binding);
139                                                 if(dev_state.bound_tex_targets[r.binding] && static_cast<int>(r.texture->target)!=dev_state.bound_tex_targets[r.binding])
140                                                         glBindTexture(dev_state.bound_tex_targets[r.binding], 0);
141                                                 glBindTexture(r.texture->target, r.texture->id);
142                                         }
143
144                                         dev_state.bound_tex_targets[r.binding] = r.texture->target;
145
146                                         glBindSampler(r.binding, r.sampler->id);
147                                         r.sampler->refresh();
148                                 }
149                                 else if(r.type==PipelineState::STORAGE_TEXTURE)
150                                 {
151                                         static Require _req(ARB_shader_image_load_store);
152                                         GLenum gl_format = get_gl_pixelformat(r.texture->get_format());
153                                         glBindImageTexture(r.binding, r.texture->id, 0, true, 0, GL_READ_WRITE, gl_format);
154
155                                         dev_state.bound_storage_textures[r.binding] = 1;
156                                 }
157                         }
158
159                         r.changed = false;
160                 }
161         }
162
163         if(changes&PipelineState::VERTEX_SETUP)
164         {
165                 const VertexSetup *vertex_setup = self.vertex_setup;
166                 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
167                 if(vertex_setup)
168                 {
169                         static Require _req(MSP_primitive_restart);
170
171                         vertex_setup->refresh();
172                         unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
173                         if(ri!=dev_state.restart_index)
174                         {
175                                 if(!dev_state.restart_index)
176                                         glEnable(GL_PRIMITIVE_RESTART);
177                                 glPrimitiveRestartIndex(ri);
178                                 dev_state.restart_index = ri;
179                         }
180                 }
181         }
182
183         if(changes&PipelineState::FACE_CULL)
184         {
185                 glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
186
187                 if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD)
188                 {
189                         glEnable(GL_CULL_FACE);
190                         glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
191                 }
192                 else
193                         glDisable(GL_CULL_FACE);
194         }
195
196         if(changes&PipelineState::DEPTH_TEST)
197         {
198                 const DepthTest &depth_test = self.depth_test;
199                 if(depth_test.enabled)
200                 {
201                         glEnable(GL_DEPTH_TEST);
202                         glDepthFunc(get_gl_predicate(depth_test.compare));
203                 }
204                 else
205                         glDisable(GL_DEPTH_TEST);
206
207                 glDepthMask(depth_test.write);
208         }
209
210         if(changes&PipelineState::STENCIL_TEST)
211         {
212                 const StencilTest &stencil_test = self.stencil_test;
213                 if(stencil_test.enabled)
214                 {
215                         glEnable(GL_STENCIL_TEST);
216                         glStencilFunc(get_gl_predicate(stencil_test.compare), stencil_test.reference, 0xFFFFFFFF);
217                         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));
218                 }
219                 else
220                         glDisable(GL_STENCIL_TEST);
221         }
222
223         if(changes&PipelineState::BLEND)
224         {
225                 const Blend &blend = self.blend;
226                 if(blend.enabled)
227                 {
228                         glEnable(GL_BLEND);
229                         glBlendEquation(get_gl_blend_equation(blend.equation));
230                         glBlendFunc(get_gl_blend_factor(blend.src_factor), get_gl_blend_factor(blend.dst_factor));
231                         glBlendColor(blend.constant.r, blend.constant.g, blend.constant.b, blend.constant.a);
232                         ColorWriteMask cw = blend.write_mask;
233                         glColorMask((cw&WRITE_RED)!=0, (cw&WRITE_GREEN)!=0, (cw&WRITE_BLUE)!=0, (cw&WRITE_ALPHA)!=0);
234                 }
235                 else
236                 {
237                         glDisable(GL_BLEND);
238                         glColorMask(true, true, true, true);
239                 }
240
241                 if(blend.alpha_to_coverage && self.framebuffer && self.framebuffer->get_format().get_samples()>1)
242                         glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
243                 else
244                         glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
245         }
246
247         applied_to = &device;
248         dev_state.last_pipeline = this;
249         changes = 0;
250 }
251
252 void OpenGLPipelineState::clear()
253 {
254         OpenGLDeviceState &dev_state = Device::get_current().get_state();
255         if(dev_state.last_pipeline)
256         {
257                 glUseProgram(0);
258                 glBindVertexArray(0);
259
260                 for(unsigned i=0; i<dev_state.n_clip_distances; ++i)
261                         glDisable(GL_CLIP_PLANE0+i);
262                 dev_state.n_clip_distances = 0;
263
264                 for(unsigned i=0; i<dev_state.bound_tex_targets.size(); ++i)
265                         if(dev_state.bound_tex_targets[i])
266                         {
267                                 if(ARB_direct_state_access)
268                                         glBindTextureUnit(i, 0);
269                                 else
270                                 {
271                                         glActiveTexture(GL_TEXTURE0+i);
272                                         glBindTexture(dev_state.bound_tex_targets[i], 0);
273                                 }
274                                 dev_state.bound_tex_targets[i] = 0;
275                         }
276
277                 for(unsigned i=0; i<dev_state.bound_storage_textures.size(); ++i)
278                         if(dev_state.bound_storage_textures[i])
279                         {
280                                 glBindImageTexture(i, 0, 0, true, 0, GL_READ_ONLY, GL_RGBA8);
281                                 dev_state.bound_storage_textures[i] = 0;
282                         }
283
284                 for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
285                         if(dev_state.bound_uniform_blocks[i])
286                         {
287                                 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
288                                 dev_state.bound_uniform_blocks[i] = 0;
289                         }
290
291                 glDisable(GL_DEPTH_TEST);
292                 glDepthMask(true);
293                 glDisable(GL_STENCIL_TEST);
294                 glDisable(GL_BLEND);
295
296                 dev_state.last_pipeline->applied_to = 0;
297                 dev_state.last_pipeline = 0;
298         }
299 }
300
301 } // namespace GL
302 } // namespace Msp