]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/pipelinestate_backend.cpp
5f9e7eb9185a4e812c1826edf0e071677e2b20d7
[libs/gl.git] / source / backends / vulkan / pipelinestate_backend.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/hash.h>
3 #include "batch.h"
4 #include "blend.h"
5 #include "buffer.h"
6 #include "depthtest.h"
7 #include "device.h"
8 #include "framebuffer.h"
9 #include "pipelinestate.h"
10 #include "pipelinestate_backend.h"
11 #include "program.h"
12 #include "rect.h"
13 #include "renderpass.h"
14 #include "sampler.h"
15 #include "stenciltest.h"
16 #include "structurebuilder.h"
17 #include "texture.h"
18 #include "uniformblock.h"
19 #include "vertexsetup.h"
20 #include "vulkan.h"
21
22 using namespace std;
23
24 namespace Msp {
25 namespace GL {
26
27 VulkanPipelineState::VulkanPipelineState():
28         device(Device::get_current())
29 { }
30
31 VulkanPipelineState::VulkanPipelineState(VulkanPipelineState &&other):
32         device(other.device),
33         handle(other.handle)
34 { }
35
36 void VulkanPipelineState::update() const
37 {
38         const PipelineState &self = *static_cast<const PipelineState *>(this);
39
40         unapplied |= changes&(PipelineState::VIEWPORT|PipelineState::SCISSOR|PipelineState::VERTEX_SETUP);
41
42         if(changes&PipelineState::VERTEX_SETUP)
43                 self.vertex_setup->refresh();
44
45         if(changes&PipelineState::SHPROG)
46         {
47                 push_const_compat = hash<32>(self.shprog->stage_flags);
48                 push_const_compat = hash_update<32>(push_const_compat, self.shprog->get_push_constants_size());
49         }
50
51         constexpr unsigned pipeline_mask = PipelineState::SHPROG|PipelineState::VERTEX_SETUP|PipelineState::FACE_CULL|
52                 PipelineState::DEPTH_TEST|PipelineState::STENCIL_TEST|PipelineState::BLEND|PipelineState::PRIMITIVE_TYPE;
53         if(changes&pipeline_mask)
54         {
55                 handle = device.get_pipeline_cache().get_pipeline(self);
56                 unapplied |= PipelineState::SHPROG;
57         }
58
59         if(changes&(PipelineState::SHPROG|PipelineState::RESOURCES))
60         {
61                 unsigned changed_sets = (changes&PipelineState::SHPROG ? ~0U : 0U);
62                 for(const PipelineState::BoundResource &r: self.resources)
63                         if(r.changed || changed_sets==~0U)
64                         {
65                                 if(r.type==PipelineState::UNIFORM_BLOCK)
66                                         r.used = self.shprog->uses_uniform_block_binding(r.binding);
67                                 else if(r.type==PipelineState::SAMPLED_TEXTURE || r.type==PipelineState::STORAGE_TEXTURE)
68                                 {
69                                         r.used = self.shprog->uses_texture_binding(r.binding);
70                                         if(r.mip_level>=0)
71                                                 r.texture->refresh_mip_views();
72                                         if(r.type==PipelineState::SAMPLED_TEXTURE)
73                                                 r.sampler->refresh();
74                                 }
75                                 if(r.binding>=0)
76                                         changed_sets |= 1<<(r.binding>>20);
77                                 r.changed = false;
78                         }
79
80                 if(changed_sets)
81                 {
82                         descriptor_set_slots.resize(self.shprog->get_n_descriptor_sets());
83                         first_changed_desc_set = descriptor_set_slots.size();
84                         for(unsigned i=0; i<descriptor_set_slots.size(); ++i)
85                                 if(changed_sets&(1<<i))
86                                 {
87                                         descriptor_set_slots[i] = device.get_descriptor_pool().get_descriptor_set_slot(self, i);
88                                         first_changed_desc_set = min(first_changed_desc_set, i);
89                                 }
90
91                         unapplied |= PipelineState::RESOURCES;
92                 }
93         }
94
95         changes = 0;
96 }
97
98 uint64_t VulkanPipelineState::compute_hash() const
99 {
100         const PipelineState &self = *static_cast<const PipelineState *>(this);
101         const FrameFormat &format = self.framebuffer->get_format();
102
103         uint64_t result = hash<64>(self.shprog);
104         result = hash_update<64>(result, self.vertex_setup->compute_hash());
105         result = hash_round<64>(result, self.primitive_type);
106
107         if(self.front_face!=NON_MANIFOLD && self.face_cull!=NO_CULL)
108         {
109                 result = hash_round<64>(result, self.front_face);
110                 result = hash_round<64>(result, self.face_cull);
111         }
112
113         result = hash_round<64>(result, format.get_samples());
114
115         if(self.depth_test.enabled)
116         {
117                 result = hash_round<64>(result, self.depth_test.compare);
118                 result = hash_update<64>(result, self.depth_test.write);
119         }
120
121         if(self.stencil_test.enabled)
122         {
123                 result = hash_round<64>(result, self.stencil_test.compare);
124                 result = hash_round<64>(result, self.stencil_test.stencil_fail_op);
125                 result = hash_round<64>(result, self.stencil_test.depth_fail_op);
126                 result = hash_round<64>(result, self.stencil_test.depth_pass_op);
127                 result = hash_update<64>(result, self.stencil_test.reference);
128         }
129
130         if(self.blend.enabled)
131         {
132                 result = hash_round<64>(result, self.blend.equation);
133                 result = hash_round<64>(result, self.blend.src_factor);
134                 result = hash_round<64>(result, self.blend.dst_factor);
135                 result = hash_round<64>(result, self.blend.write_mask);
136         }
137
138         for(FrameAttachment a: format)
139                 result = hash_update<64>(result, a);
140
141         return result;
142 }
143
144 void VulkanPipelineState::fill_creation_info(vector<char> &buffer) const
145 {
146         const PipelineState &self = *static_cast<const PipelineState *>(this);
147
148         const FrameFormat &format = self.framebuffer->get_format();
149         RenderPass render_pass;
150         render_pass.framebuffer = self.framebuffer;
151         render_pass.update(device);
152
153         unsigned n_color_attachments = 0;
154         for(FrameAttachment a: format)
155         {
156                 unsigned attach_pt = get_attach_point(a);
157                 if(attach_pt!=get_attach_point(DEPTH_ATTACHMENT) && attach_pt!=get_attach_point(STENCIL_ATTACHMENT))
158                         ++n_color_attachments;
159         }
160
161         StructureBuilder sb(buffer, 10);
162         VkGraphicsPipelineCreateInfo *const &pipeline_info = sb.add<VkGraphicsPipelineCreateInfo>();
163         VkPipelineInputAssemblyStateCreateInfo *const &input_assembly_info = sb.add<VkPipelineInputAssemblyStateCreateInfo>();
164         VkPipelineViewportStateCreateInfo *const &viewport_info = sb.add<VkPipelineViewportStateCreateInfo>();
165         VkPipelineRasterizationStateCreateInfo *const &raster_info = sb.add<VkPipelineRasterizationStateCreateInfo>();
166         VkPipelineMultisampleStateCreateInfo *const &multisample_info = sb.add<VkPipelineMultisampleStateCreateInfo>();
167         VkPipelineDepthStencilStateCreateInfo *const &depth_stencil_info = sb.add<VkPipelineDepthStencilStateCreateInfo>();
168         VkPipelineColorBlendStateCreateInfo *const &blend_info = sb.add<VkPipelineColorBlendStateCreateInfo>();
169         VkPipelineColorBlendAttachmentState *const &blend_attachments = sb.add<VkPipelineColorBlendAttachmentState>(n_color_attachments);
170         VkPipelineDynamicStateCreateInfo *const &dynamic_info = sb.add<VkPipelineDynamicStateCreateInfo>();
171         VkDynamicState *const &dynamic_states = sb.add<VkDynamicState>(2);
172
173         input_assembly_info->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
174         input_assembly_info->topology = static_cast<VkPrimitiveTopology>(get_vulkan_primitive_type(self.primitive_type));
175         input_assembly_info->primitiveRestartEnable = true;
176
177         viewport_info->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
178         viewport_info->viewportCount = 1;
179         viewport_info->pViewports = 0;
180         viewport_info->scissorCount = 1;
181         viewport_info->pScissors = 0;
182
183         raster_info->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
184         raster_info->depthClampEnable = VK_FALSE;
185         raster_info->rasterizerDiscardEnable = VK_FALSE;
186         raster_info->polygonMode = VK_POLYGON_MODE_FILL;
187         raster_info->frontFace = (self.front_face==CLOCKWISE ? VK_FRONT_FACE_COUNTER_CLOCKWISE : VK_FRONT_FACE_CLOCKWISE);
188         if(self.face_cull==NO_CULL || self.front_face==NON_MANIFOLD)
189                 raster_info->cullMode = VK_CULL_MODE_NONE;
190         else
191                 raster_info->cullMode = (self.face_cull==CULL_FRONT ? VK_CULL_MODE_FRONT_BIT : VK_CULL_MODE_BACK_BIT);
192         raster_info->depthBiasEnable = VK_FALSE;
193         raster_info->depthBiasConstantFactor = 0.0f;
194         raster_info->depthBiasClamp = 0.0f;
195         raster_info->depthBiasSlopeFactor = 0.0f;
196         raster_info->lineWidth = 1.0f;
197
198         multisample_info->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
199         multisample_info->rasterizationSamples = static_cast<VkSampleCountFlagBits>(get_vulkan_samples(format.get_samples()));
200         multisample_info->sampleShadingEnable = VK_FALSE;
201         multisample_info->minSampleShading = 1.0f;
202         multisample_info->pSampleMask = 0;
203         multisample_info->alphaToCoverageEnable = VK_FALSE;
204         multisample_info->alphaToOneEnable = VK_FALSE;
205
206         depth_stencil_info->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
207         depth_stencil_info->depthTestEnable = self.depth_test.enabled;
208         depth_stencil_info->depthWriteEnable = self.depth_test.write;
209         depth_stencil_info->depthCompareOp = static_cast<VkCompareOp>(get_vulkan_predicate(self.depth_test.compare));
210         depth_stencil_info->depthBoundsTestEnable = VK_FALSE;
211
212         depth_stencil_info->stencilTestEnable = self.stencil_test.enabled;
213         depth_stencil_info->front.failOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(self.stencil_test.stencil_fail_op));
214         depth_stencil_info->front.passOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(self.stencil_test.depth_pass_op));
215         depth_stencil_info->front.depthFailOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(self.stencil_test.depth_fail_op));
216         depth_stencil_info->front.compareOp = static_cast<VkCompareOp>(get_vulkan_predicate(self.stencil_test.compare));
217         depth_stencil_info->front.compareMask = 0xFFFFFFFFU;
218         depth_stencil_info->front.writeMask = 0xFFFFFFFFU;
219         depth_stencil_info->front.reference = self.stencil_test.reference;
220         depth_stencil_info->back = depth_stencil_info->front;
221
222         for(unsigned i=0; i<n_color_attachments; ++i)
223         {
224                 blend_attachments[i].blendEnable = self.blend.enabled;
225                 blend_attachments[i].srcColorBlendFactor = static_cast<VkBlendFactor>(get_vulkan_blend_factor(self.blend.src_factor));
226                 blend_attachments[i].dstColorBlendFactor = static_cast<VkBlendFactor>(get_vulkan_blend_factor(self.blend.dst_factor));
227                 blend_attachments[i].colorBlendOp = static_cast<VkBlendOp>(get_vulkan_blend_equation(self.blend.equation));
228                 blend_attachments[i].srcAlphaBlendFactor = blend_attachments[i].srcColorBlendFactor;
229                 blend_attachments[i].dstAlphaBlendFactor = blend_attachments[i].dstColorBlendFactor;
230                 blend_attachments[i].alphaBlendOp = blend_attachments[i].colorBlendOp;
231                 blend_attachments[i].colorWriteMask = get_vulkan_color_mask(self.blend.write_mask);
232         }
233
234         blend_info->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
235         blend_info->attachmentCount = n_color_attachments;
236         blend_info->pAttachments = blend_attachments;
237
238         dynamic_states[0] = VK_DYNAMIC_STATE_VIEWPORT;
239         dynamic_states[1] = VK_DYNAMIC_STATE_SCISSOR;
240
241         dynamic_info->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
242         dynamic_info->dynamicStateCount = 2;
243         dynamic_info->pDynamicStates = dynamic_states;
244
245         pipeline_info->sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
246
247         pipeline_info->pInputAssemblyState = input_assembly_info;
248         pipeline_info->pTessellationState = 0;
249         pipeline_info->pViewportState = viewport_info;
250         pipeline_info->pRasterizationState = raster_info;
251         pipeline_info->pMultisampleState = multisample_info;
252         pipeline_info->pDepthStencilState = depth_stencil_info;
253         pipeline_info->pColorBlendState = blend_info;
254         pipeline_info->pDynamicState = dynamic_info;
255         pipeline_info->renderPass = handle_cast<::VkRenderPass>(render_pass.handle);
256         pipeline_info->subpass = 0;
257
258         if(self.shprog)
259         {
260                 pipeline_info->stageCount = self.shprog->n_stages;
261                 pipeline_info->pStages = reinterpret_cast<const VkPipelineShaderStageCreateInfo *>(self.shprog->creation_info.data());
262                 pipeline_info->layout = handle_cast<::VkPipelineLayout>(self.shprog->layout_handle);
263         }
264
265         if(self.vertex_setup)
266                 pipeline_info->pVertexInputState = reinterpret_cast<const VkPipelineVertexInputStateCreateInfo *>(self.vertex_setup->creation_info.data());
267 }
268
269 uint64_t VulkanPipelineState::compute_descriptor_set_hash(unsigned index) const
270 {
271         const PipelineState &self = *static_cast<const PipelineState *>(this);
272
273         uint64_t result = hash<64>(0, 0);
274         bool empty = true;
275
276         auto i = lower_bound_member(self.resources, static_cast<int>(index)<<20, &PipelineState::BoundResource::binding);
277         for(; (i!=self.resources.end() && static_cast<unsigned>(i->binding)>>20==index); ++i)
278         {
279                 if(!i->used)
280                         continue;
281
282                 result = hash_update<64>(result, i->binding);
283                 result = hash_update<64>(result, i->type);
284                 if(i->type==PipelineState::UNIFORM_BLOCK)
285                 {
286                         result = hash_update<64>(result, reinterpret_cast<uintptr_t>(i->block));
287                         result = hash_update<64>(result, reinterpret_cast<uintptr_t>(i->buffer->handle));
288                 }
289                 else if(i->type==PipelineState::SAMPLED_TEXTURE)
290                 {
291                         result = hash_update<64>(result, reinterpret_cast<uintptr_t>(i->texture->handle));
292                         result = hash_update<64>(result, reinterpret_cast<uintptr_t>(i->sampler->handle));
293                         result = hash_update<64>(result, i->mip_level);
294                 }
295                 else if(i->type==PipelineState::STORAGE_TEXTURE)
296                         result = hash_update<64>(result, reinterpret_cast<uintptr_t>(i->texture->handle));
297                 empty = false;
298         }
299
300         if(!empty)
301                 result = hash_update<64>(result, self.shprog->stage_flags);
302
303         return result;
304 }
305
306 bool VulkanPipelineState::is_descriptor_set_dynamic(unsigned index) const
307 {
308         const PipelineState &self = *static_cast<const PipelineState *>(this);
309
310         auto i = lower_bound_member(self.resources, static_cast<int>(index)<<20, &PipelineState::BoundResource::binding);
311         for(; (i!=self.resources.end() && static_cast<unsigned>(i->binding)>>20==index); ++i)
312                 if(i->used && i->type==PipelineState::UNIFORM_BLOCK && i->buffer->get_usage()==STREAMING)
313                         return true;
314
315         return false;
316 }
317
318 VkDescriptorSetLayout VulkanPipelineState::get_descriptor_set_layout(unsigned index) const
319 {
320         return static_cast<const PipelineState *>(this)->shprog->desc_set_layout_handles[index];
321 }
322
323 unsigned VulkanPipelineState::fill_descriptor_writes(unsigned index, unsigned frame, vector<char> &buffer) const
324 {
325         const PipelineState &self = *static_cast<const PipelineState *>(this);
326
327         auto begin = lower_bound_member(self.resources, static_cast<int>(index)<<20, &PipelineState::BoundResource::binding);
328
329         unsigned n_buffers = 0;
330         unsigned n_images = 0;
331         for(auto i=begin; (i!=self.resources.end() && static_cast<unsigned>(i->binding)>>20==index); ++i)
332                 if(i->used)
333                 {
334                         if(i->type==PipelineState::UNIFORM_BLOCK)
335                                 ++n_buffers;
336                         else if(i->type==PipelineState::SAMPLED_TEXTURE || i->type==PipelineState::STORAGE_TEXTURE)
337                                 ++n_images;
338                 }
339         unsigned n_writes = n_buffers+n_images;
340
341         StructureBuilder sb(buffer, 3);
342         VkWriteDescriptorSet *const &writes = sb.add<VkWriteDescriptorSet>(n_writes);
343         VkDescriptorBufferInfo *const &buffers = sb.add<VkDescriptorBufferInfo>(n_buffers);
344         VkDescriptorImageInfo *const &images = sb.add<VkDescriptorImageInfo>(n_images);
345
346         VkWriteDescriptorSet *write_ptr = writes;
347         VkDescriptorBufferInfo *buffer_ptr = buffers;
348         VkDescriptorImageInfo *image_ptr = images;
349
350         for(auto i=begin; (i!=self.resources.end() && static_cast<unsigned>(i->binding)>>20==index); ++i)
351         {
352                 if(!i->used)
353                         continue;
354
355                 if(i->type==PipelineState::UNIFORM_BLOCK)
356                 {
357                         buffer_ptr->buffer = handle_cast<::VkBuffer>(i->buffer->handle);
358                         buffer_ptr->offset = i->block->get_offset();
359                         if(i->buffer->get_usage()==STREAMING)
360                                 buffer_ptr->offset += frame*i->buffer->get_size();
361                         buffer_ptr->range = i->block->get_data_size();
362
363                         write_ptr->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
364                         write_ptr->dstBinding = i->binding&0xFFFFF;
365                         write_ptr->descriptorCount = 1;
366                         write_ptr->descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
367                         write_ptr->pBufferInfo = buffer_ptr;
368
369                         ++buffer_ptr;
370                 }
371                 else if(i->type==PipelineState::SAMPLED_TEXTURE || i->type==PipelineState::STORAGE_TEXTURE)
372                 {
373                         if(i->mip_level<0)
374                                 image_ptr->imageView = handle_cast<::VkImageView>(i->texture->view_handle);
375                         else
376                                 image_ptr->imageView = handle_cast<::VkImageView>(i->texture->mip_view_handles[i->mip_level]);
377
378                         if(i->type==PipelineState::SAMPLED_TEXTURE)
379                         {
380                                 image_ptr->sampler = handle_cast<::VkSampler>(i->sampler->handle);
381                                 image_ptr->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
382                                 write_ptr->descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
383                         }
384                         else if(i->type==PipelineState::STORAGE_TEXTURE)
385                         {
386                                 image_ptr->imageLayout = VK_IMAGE_LAYOUT_GENERAL;
387                                 write_ptr->descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
388                         }
389
390                         write_ptr->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
391                         write_ptr->dstBinding = i->binding&0xFFFFF;
392                         write_ptr->descriptorCount = 1;
393                         write_ptr->pImageInfo = image_ptr;
394
395                         ++image_ptr;
396                 }
397
398                 ++write_ptr;
399         }
400
401         return n_writes;
402 }
403
404 void VulkanPipelineState::synchronize_resources(bool discard_fb_contents) const
405 {
406         const PipelineState &self = *static_cast<const PipelineState *>(this);
407
408         for(const PipelineState::BoundResource &r: self.resources)
409                 if(r.type==PipelineState::STORAGE_TEXTURE)
410                         r.texture->change_layout(-1, VK_IMAGE_LAYOUT_GENERAL, false);
411 }
412
413 void VulkanPipelineState::apply(const VulkanCommandRecorder &vkCmd, const VulkanPipelineState *last, unsigned frame, bool negative_viewport) const
414 {
415         const PipelineState &self = *static_cast<const PipelineState *>(this);
416
417         if(!last)
418         {
419                 unapplied = ~0U;
420                 first_changed_desc_set = 0;
421         }
422         else if(last!=this)
423         {
424                 const PipelineState &last_ps = *static_cast<const PipelineState *>(last);
425                 if(handle!=last->handle)
426                 {
427                         unapplied |= PipelineState::SHPROG;
428                         if(self.push_const_compat!=last_ps.push_const_compat)
429                         {
430                                 unapplied |= PipelineState::RESOURCES;
431                                 first_changed_desc_set = 0;
432                         }
433                 }
434                 if(self.vertex_setup!=last_ps.vertex_setup)
435                         unapplied |= PipelineState::VERTEX_SETUP;
436                 for(unsigned i=0; i<descriptor_set_slots.size(); ++i)
437                         if(i>=last->descriptor_set_slots.size() || descriptor_set_slots[i]!=last->descriptor_set_slots[i])
438                         {
439                                 unapplied |= PipelineState::RESOURCES;
440                                 first_changed_desc_set = min(first_changed_desc_set, i);
441                                 break;
442                         }
443                 if(self.viewport!=last_ps.viewport)
444                         unapplied |= PipelineState::VIEWPORT;
445                 if(self.scissor!=last_ps.scissor)
446                         unapplied |= PipelineState::SCISSOR;
447         }
448
449         if(unapplied&PipelineState::SHPROG)
450                 vkCmd.BindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, handle);
451
452         if(unapplied&PipelineState::VERTEX_SETUP)
453                 if(const VertexSetup *vs = self.vertex_setup)
454                 {
455                         vkCmd.BindVertexBuffers(0, vs->n_bindings, vs->buffers, vs->offsets);
456                         VkIndexType index_type = static_cast<VkIndexType>(get_vulkan_index_type(vs->get_index_type()));
457                         vkCmd.BindIndexBuffer(vs->get_index_buffer()->handle, 0, index_type);
458                 }
459
460         if(!self.resources.empty())
461         {
462                 const PipelineState::BoundResource &first_res = self.resources.front();
463                 if(first_res.used && first_res.type==PipelineState::UNIFORM_BLOCK && first_res.binding==ReflectData::PUSH_CONSTANT)
464                 {
465                         const UniformBlock &pc_block = *first_res.block;
466                         vkCmd.PushConstants(self.shprog->layout_handle, self.shprog->stage_flags,
467                                 pc_block.get_offset(), pc_block.get_data_size(), pc_block.get_data_pointer());
468                 }
469         }
470
471         if((unapplied&PipelineState::RESOURCES) && !descriptor_set_slots.empty())
472         {
473                 vector<VkDescriptorSet> descriptor_set_handles;
474                 descriptor_set_handles.reserve(descriptor_set_slots.size()-first_changed_desc_set);
475                 for(unsigned i=first_changed_desc_set; i<descriptor_set_slots.size(); ++i)
476                         descriptor_set_handles.push_back(device.get_descriptor_pool().get_descriptor_set(
477                                 self.descriptor_set_slots[i], self, i, frame));
478
479                 vkCmd.BindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, self.shprog->layout_handle,
480                         first_changed_desc_set, descriptor_set_handles.size(), descriptor_set_handles.data(), 0, 0);
481         }
482
483         if(unapplied&(PipelineState::VIEWPORT|PipelineState::SCISSOR))
484         {
485                 Rect fb_rect = self.framebuffer->get_rect();
486
487                 if(unapplied&PipelineState::VIEWPORT)
488                 {
489                         Rect viewport_rect = fb_rect.intersect(self.viewport);
490                         VkViewport viewport = { };
491                         viewport.x = viewport_rect.left;
492                         viewport.y = viewport_rect.bottom;
493                         viewport.width = viewport_rect.width;
494                         viewport.height = viewport_rect.height;
495                         if(negative_viewport)
496                         {
497                                 viewport.y += viewport.height;
498                                 viewport.height = -viewport.height;
499                         }
500                         viewport.minDepth = 0.0f;
501                         viewport.maxDepth = 1.0f;
502                         vkCmd.SetViewport(0, 1, &viewport);
503                 }
504
505                 if(unapplied&PipelineState::SCISSOR)
506                 {
507                         Rect scissor_rect = fb_rect.intersect(self.scissor);
508                         VkRect2D scissor = { };
509                         scissor.offset.x = scissor_rect.left;
510                         scissor.offset.y = scissor_rect.bottom;
511                         scissor.extent.width = scissor_rect.width;
512                         scissor.extent.height = scissor_rect.height;
513                         vkCmd.SetScissor(0, 1, &scissor);
514                 }
515         }
516
517         unapplied = 0;
518         first_changed_desc_set = descriptor_set_slots.size();
519 }
520
521 } // namespace GL
522 } // namespace Msp