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