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