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