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