1 #include <msp/core/algorithm.h>
2 #include <msp/core/hash.h>
8 #include "framebuffer.h"
9 #include "pipelinestate.h"
10 #include "pipelinestate_backend.h"
14 #include "stenciltest.h"
15 #include "structurebuilder.h"
17 #include "uniformblock.h"
18 #include "vertexsetup.h"
26 VulkanPipelineState::VulkanPipelineState():
27 device(Device::get_current())
30 VulkanPipelineState::VulkanPipelineState(VulkanPipelineState &&other):
35 void VulkanPipelineState::update() const
37 const PipelineState &self = *static_cast<const PipelineState *>(this);
39 unapplied |= changes&(PipelineState::VIEWPORT|PipelineState::SCISSOR|PipelineState::VERTEX_SETUP);
41 if(changes&PipelineState::VERTEX_SETUP)
42 self.vertex_setup->refresh();
44 constexpr unsigned pipeline_mask = PipelineState::SHPROG|PipelineState::VERTEX_SETUP|PipelineState::FACE_CULL|
45 PipelineState::DEPTH_TEST|PipelineState::STENCIL_TEST|PipelineState::BLEND|PipelineState::PRIMITIVE_TYPE;
46 if(changes&pipeline_mask)
48 handle = device.get_pipeline_cache().get_pipeline(self);
49 unapplied |= PipelineState::SHPROG;
52 if(changes&(PipelineState::SHPROG|PipelineState::UNIFORMS|PipelineState::TEXTURES))
54 unsigned changed_sets = (changes&PipelineState::SHPROG ? ~0U : 0U);
55 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
56 if(u.changed || changed_sets==~0U)
59 u.used = self.shprog->uses_uniform_block_binding(u.binding);
61 changed_sets |= 1<<(u.binding>>20);
64 for(const PipelineState::BoundTexture &t: self.textures)
65 if(t.changed || changed_sets==~0U)
67 if(t.texture && t.sampler)
68 t.used = self.shprog->uses_texture_binding(t.binding);
69 changed_sets |= 1<<(t.binding>>20);
70 if(t.texture && t.level>=0)
71 t.texture->refresh_mip_views();
79 descriptor_set_handles.resize(self.shprog->get_n_descriptor_sets());
80 for(unsigned i=0; i<descriptor_set_handles.size(); ++i)
81 if(changed_sets&(1<<i))
82 descriptor_set_handles[i] = device.get_pipeline_cache().get_descriptor_set(self, i);
83 unapplied |= PipelineState::UNIFORMS;
90 uint64_t VulkanPipelineState::compute_hash() const
92 const PipelineState &self = *static_cast<const PipelineState *>(this);
93 const FrameFormat &format = self.framebuffer->get_format();
95 uint64_t result = hash<64>(self.shprog);
96 result = hash_update<64>(result, self.vertex_setup->compute_hash());
97 result = hash_round<64>(result, self.primitive_type);
99 if(self.front_face!=NON_MANIFOLD && self.face_cull!=NO_CULL)
101 result = hash_round<64>(result, self.front_face);
102 result = hash_round<64>(result, self.face_cull);
105 result = hash_round<64>(result, format.get_samples());
107 if(self.depth_test.enabled)
109 result = hash_round<64>(result, self.depth_test.compare);
110 result = hash_update<64>(result, self.depth_test.write);
113 if(self.stencil_test.enabled)
115 result = hash_round<64>(result, self.stencil_test.compare);
116 result = hash_round<64>(result, self.stencil_test.stencil_fail_op);
117 result = hash_round<64>(result, self.stencil_test.depth_fail_op);
118 result = hash_round<64>(result, self.stencil_test.depth_pass_op);
119 result = hash_update<64>(result, self.stencil_test.reference);
122 if(self.blend.enabled)
124 result = hash_round<64>(result, self.blend.equation);
125 result = hash_round<64>(result, self.blend.src_factor);
126 result = hash_round<64>(result, self.blend.dst_factor);
127 result = hash_round<64>(result, self.blend.write_mask);
130 for(FrameAttachment a: format)
131 result = hash_update<64>(result, a);
136 void VulkanPipelineState::fill_creation_info(vector<char> &buffer) const
138 const PipelineState &self = *static_cast<const PipelineState *>(this);
140 const FrameFormat &format = self.framebuffer->get_format();
141 VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(format, false, false, false);
143 unsigned n_color_attachments = 0;
144 for(FrameAttachment a: format)
146 unsigned attach_pt = get_attach_point(a);
147 if(attach_pt!=get_attach_point(DEPTH_ATTACHMENT) && attach_pt!=get_attach_point(STENCIL_ATTACHMENT))
148 ++n_color_attachments;
151 StructureBuilder sb(buffer, 10);
152 VkGraphicsPipelineCreateInfo *&pipeline_info = sb.add<VkGraphicsPipelineCreateInfo>();
153 VkPipelineInputAssemblyStateCreateInfo *&input_assembly_info = sb.add<VkPipelineInputAssemblyStateCreateInfo>();
154 VkPipelineViewportStateCreateInfo *&viewport_info = sb.add<VkPipelineViewportStateCreateInfo>();
155 VkPipelineRasterizationStateCreateInfo *&raster_info = sb.add<VkPipelineRasterizationStateCreateInfo>();
156 VkPipelineMultisampleStateCreateInfo *&multisample_info = sb.add<VkPipelineMultisampleStateCreateInfo>();
157 VkPipelineDepthStencilStateCreateInfo *&depth_stencil_info = sb.add<VkPipelineDepthStencilStateCreateInfo>();
158 VkPipelineColorBlendStateCreateInfo *&blend_info = sb.add<VkPipelineColorBlendStateCreateInfo>();
159 VkPipelineColorBlendAttachmentState *&blend_attachments = sb.add<VkPipelineColorBlendAttachmentState>(n_color_attachments);
160 VkPipelineDynamicStateCreateInfo *&dynamic_info = sb.add<VkPipelineDynamicStateCreateInfo>();
161 VkDynamicState *&dynamic_states = sb.add<VkDynamicState>(2);
163 input_assembly_info->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
164 input_assembly_info->topology = static_cast<VkPrimitiveTopology>(get_vulkan_primitive_type(self.primitive_type));
165 input_assembly_info->primitiveRestartEnable = true;
167 viewport_info->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
168 viewport_info->viewportCount = 1;
169 viewport_info->pViewports = 0;
170 viewport_info->scissorCount = 1;
171 viewport_info->pScissors = 0;
173 raster_info->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
174 raster_info->depthClampEnable = VK_FALSE;
175 raster_info->rasterizerDiscardEnable = VK_FALSE;
176 raster_info->polygonMode = VK_POLYGON_MODE_FILL;
177 raster_info->frontFace = (self.front_face==CLOCKWISE ? VK_FRONT_FACE_COUNTER_CLOCKWISE : VK_FRONT_FACE_CLOCKWISE);
178 if(self.face_cull==NO_CULL || self.front_face==NON_MANIFOLD)
179 raster_info->cullMode = VK_CULL_MODE_NONE;
181 raster_info->cullMode = (self.face_cull==CULL_FRONT ? VK_CULL_MODE_FRONT_BIT : VK_CULL_MODE_BACK_BIT);
182 raster_info->depthBiasEnable = VK_FALSE;
183 raster_info->depthBiasConstantFactor = 0.0f;
184 raster_info->depthBiasClamp = 0.0f;
185 raster_info->depthBiasSlopeFactor = 0.0f;
186 raster_info->lineWidth = 1.0f;
188 multisample_info->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
189 multisample_info->rasterizationSamples = static_cast<VkSampleCountFlagBits>(get_vulkan_samples(format.get_samples()));
190 multisample_info->sampleShadingEnable = VK_FALSE;
191 multisample_info->minSampleShading = 1.0f;
192 multisample_info->pSampleMask = 0;
193 multisample_info->alphaToCoverageEnable = VK_FALSE;
194 multisample_info->alphaToOneEnable = VK_FALSE;
196 depth_stencil_info->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
197 depth_stencil_info->depthTestEnable = self.depth_test.enabled;
198 depth_stencil_info->depthWriteEnable = self.depth_test.write;
199 depth_stencil_info->depthCompareOp = static_cast<VkCompareOp>(get_vulkan_predicate(self.depth_test.compare));
200 depth_stencil_info->depthBoundsTestEnable = VK_FALSE;
202 depth_stencil_info->stencilTestEnable = self.stencil_test.enabled;
203 depth_stencil_info->front.failOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(self.stencil_test.stencil_fail_op));
204 depth_stencil_info->front.passOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(self.stencil_test.depth_pass_op));
205 depth_stencil_info->front.depthFailOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(self.stencil_test.depth_fail_op));
206 depth_stencil_info->front.compareOp = static_cast<VkCompareOp>(get_vulkan_predicate(self.stencil_test.compare));
207 depth_stencil_info->front.compareMask = 0xFFFFFFFFU;
208 depth_stencil_info->front.writeMask = 0xFFFFFFFFU;
209 depth_stencil_info->front.reference = self.stencil_test.reference;
210 depth_stencil_info->back = depth_stencil_info->front;
212 for(unsigned i=0; i<n_color_attachments; ++i)
214 blend_attachments[i].blendEnable = self.blend.enabled;
215 blend_attachments[i].srcColorBlendFactor = static_cast<VkBlendFactor>(get_vulkan_blend_factor(self.blend.src_factor));
216 blend_attachments[i].dstColorBlendFactor = static_cast<VkBlendFactor>(get_vulkan_blend_factor(self.blend.dst_factor));
217 blend_attachments[i].colorBlendOp = static_cast<VkBlendOp>(get_vulkan_blend_equation(self.blend.equation));
218 blend_attachments[i].srcAlphaBlendFactor = blend_attachments[i].srcColorBlendFactor;
219 blend_attachments[i].dstAlphaBlendFactor = blend_attachments[i].dstColorBlendFactor;
220 blend_attachments[i].alphaBlendOp = blend_attachments[i].colorBlendOp;
221 blend_attachments[i].colorWriteMask = get_vulkan_color_mask(self.blend.write_mask);
224 blend_info->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
225 blend_info->attachmentCount = n_color_attachments;
226 blend_info->pAttachments = blend_attachments;
228 dynamic_states[0] = VK_DYNAMIC_STATE_VIEWPORT;
229 dynamic_states[1] = VK_DYNAMIC_STATE_SCISSOR;
231 dynamic_info->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
232 dynamic_info->dynamicStateCount = 2;
233 dynamic_info->pDynamicStates = dynamic_states;
235 pipeline_info->sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
237 pipeline_info->pInputAssemblyState = input_assembly_info;
238 pipeline_info->pTessellationState = 0;
239 pipeline_info->pViewportState = viewport_info;
240 pipeline_info->pRasterizationState = raster_info;
241 pipeline_info->pMultisampleState = multisample_info;
242 pipeline_info->pDepthStencilState = depth_stencil_info;
243 pipeline_info->pColorBlendState = blend_info;
244 pipeline_info->pDynamicState = dynamic_info;
245 pipeline_info->renderPass = handle_cast<::VkRenderPass>(render_pass);
246 pipeline_info->subpass = 0;
250 pipeline_info->stageCount = self.shprog->n_stages;
251 pipeline_info->pStages = reinterpret_cast<const VkPipelineShaderStageCreateInfo *>(self.shprog->creation_info.data());
252 pipeline_info->layout = handle_cast<::VkPipelineLayout>(self.shprog->layout_handle);
255 if(self.vertex_setup)
256 pipeline_info->pVertexInputState = reinterpret_cast<const VkPipelineVertexInputStateCreateInfo *>(self.vertex_setup->creation_info.data());
259 uint64_t VulkanPipelineState::compute_descriptor_set_hash(unsigned index) const
261 const PipelineState &self = *static_cast<const PipelineState *>(this);
263 uint64_t result = hash<64>(0, 0);
265 auto i = lower_bound_member(self.uniform_blocks, static_cast<int>(index)<<20, &PipelineState::BoundUniformBlock::binding);
266 for(; (i!=self.uniform_blocks.end() && static_cast<unsigned>(i->binding)>>20==index); ++i)
269 result = hash_update<64>(result, i->binding);
270 result = hash_update<64>(result, reinterpret_cast<uintptr_t>(i->block));
271 result = hash_update<64>(result, reinterpret_cast<uintptr_t>(i->buffer->handle));
274 auto j = lower_bound_member(self.textures, index<<20, &PipelineState::BoundTexture::binding);
275 for(; (j!=self.textures.end() && j->binding>>20==index); ++j)
278 result = hash_update<64>(result, j->binding);
279 result = hash_update<64>(result, reinterpret_cast<uintptr_t>(j->texture->handle));
280 result = hash_update<64>(result, reinterpret_cast<uintptr_t>(j->sampler->handle));
281 result = hash_update<64>(result, j->level);
287 VkDescriptorSetLayout VulkanPipelineState::get_descriptor_set_layout(unsigned index) const
289 return static_cast<const PipelineState *>(this)->shprog->desc_set_layout_handles[index];
292 unsigned VulkanPipelineState::fill_descriptor_writes(unsigned index, vector<char> &buffer) const
294 const PipelineState &self = *static_cast<const PipelineState *>(this);
296 auto u_begin = lower_bound_member(self.uniform_blocks, static_cast<int>(index)<<20, &PipelineState::BoundUniformBlock::binding);
297 auto t_begin = lower_bound_member(self.textures, index<<20, &PipelineState::BoundTexture::binding);
299 unsigned n_buffers = 0;
300 for(auto i=u_begin; (i!=self.uniform_blocks.end() && static_cast<unsigned>(i->binding)>>20==index); ++i)
303 unsigned n_images = 0;
304 for(auto i=t_begin; (i!=self.textures.end() && i->binding>>20==index); ++i)
307 unsigned n_writes = n_buffers+n_images;
309 StructureBuilder sb(buffer, 3);
310 VkWriteDescriptorSet *&writes = sb.add<VkWriteDescriptorSet>(n_writes);
311 VkDescriptorBufferInfo *&buffers = sb.add<VkDescriptorBufferInfo>(n_buffers);
312 VkDescriptorImageInfo *&images = sb.add<VkDescriptorImageInfo>(n_images);
314 VkWriteDescriptorSet *write_ptr = writes;
315 VkDescriptorBufferInfo *buffer_ptr = buffers;
316 VkDescriptorImageInfo *image_ptr = images;
318 for(auto i=u_begin; (i!=self.uniform_blocks.end() && static_cast<unsigned>(i->binding)>>20==index); ++i)
321 buffer_ptr->buffer = handle_cast<::VkBuffer>(i->buffer->handle);
322 buffer_ptr->offset = i->block->get_offset();
323 buffer_ptr->range = i->block->get_data_size();
325 write_ptr->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
326 write_ptr->dstBinding = i->binding&0xFFFFF;
327 write_ptr->descriptorCount = 1;
328 write_ptr->descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
329 write_ptr->pBufferInfo = buffer_ptr;
335 for(auto i=t_begin; (i!=self.textures.end() && i->binding>>20==index); ++i)
338 image_ptr->sampler = handle_cast<::VkSampler>(i->sampler->handle);
340 image_ptr->imageView = handle_cast<::VkImageView>(i->texture->view_handle);
342 image_ptr->imageView = handle_cast<::VkImageView>(i->texture->mip_view_handles[i->level]);
343 image_ptr->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
345 write_ptr->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
346 write_ptr->dstBinding = i->binding&0xFFFFF;
347 write_ptr->descriptorCount = 1;
348 write_ptr->descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
349 write_ptr->pImageInfo = image_ptr;
358 void VulkanPipelineState::apply(VkCommandBuffer command_buffer, const VulkanPipelineState *last, unsigned frame, bool negative_viewport) const
360 const PipelineState &self = *static_cast<const PipelineState *>(this);
361 const VulkanFunctions &vk = device.get_functions();
367 const PipelineState &last_ps = *static_cast<const PipelineState *>(last);
368 if(handle!=last->handle)
369 unapplied |= PipelineState::SHPROG;
370 if(self.vertex_setup!=last_ps.vertex_setup)
371 unapplied |= PipelineState::VERTEX_SETUP;
372 for(unsigned i=0; (i<descriptor_set_handles.size() && i<last->descriptor_set_handles.size()); ++i)
373 if(descriptor_set_handles[i]!=last->descriptor_set_handles[i])
375 unapplied |= PipelineState::UNIFORMS;
378 if(self.viewport!=last_ps.viewport)
379 unapplied |= PipelineState::VIEWPORT;
380 if(self.scissor!=last_ps.scissor)
381 unapplied |= PipelineState::SCISSOR;
384 if(unapplied&PipelineState::SHPROG)
385 vk.CmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, handle);
387 if(unapplied&PipelineState::VERTEX_SETUP)
388 if(const VertexSetup *vs = self.vertex_setup)
390 vk.CmdBindVertexBuffers(command_buffer, 0, vs->n_bindings, vs->buffers, vs->offsets);
391 VkIndexType index_type = static_cast<VkIndexType>(get_vulkan_index_type(vs->get_index_type()));
392 vk.CmdBindIndexBuffer(command_buffer, vs->get_index_buffer()->handle, 0, index_type);
395 if(!self.uniform_blocks.empty())
397 const PipelineState::BoundUniformBlock &first_block = self.uniform_blocks.front();
398 if(first_block.used && first_block.binding==ReflectData::PUSH_CONSTANT)
400 const UniformBlock &pc_block = *first_block.block;
401 vk.CmdPushConstants(command_buffer, self.shprog->layout_handle, self.shprog->stage_flags,
402 pc_block.get_offset(), pc_block.get_data_size(), pc_block.get_data_pointer());
406 if((unapplied&PipelineState::UNIFORMS) && !descriptor_set_handles.empty())
408 vector<uint32_t> dynamic_offsets;
409 dynamic_offsets.reserve(self.uniform_blocks.size());
410 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
411 if(u.used && u.binding>=0)
413 if(u.buffer->get_usage()==STREAMING)
414 dynamic_offsets.push_back(frame*u.buffer->get_size());
416 dynamic_offsets.push_back(0);
419 vk.CmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, self.shprog->layout_handle,
420 0, descriptor_set_handles.size(), descriptor_set_handles.data(), dynamic_offsets.size(), dynamic_offsets.data());
423 if(unapplied&(PipelineState::VIEWPORT|PipelineState::SCISSOR))
425 Rect fb_rect = self.framebuffer->get_rect();
427 if(unapplied&PipelineState::VIEWPORT)
429 Rect viewport_rect = fb_rect.intersect(self.viewport);
430 VkViewport viewport = { };
431 viewport.x = viewport_rect.left;
432 viewport.y = viewport_rect.bottom;
433 viewport.width = viewport_rect.width;
434 viewport.height = viewport_rect.height;
435 if(negative_viewport)
437 viewport.y += viewport.height;
438 viewport.height = -viewport.height;
440 viewport.minDepth = 0.0f;
441 viewport.maxDepth = 1.0f;
442 vk.CmdSetViewport(command_buffer, 0, 1, &viewport);
445 if(unapplied&PipelineState::SCISSOR)
447 Rect scissor_rect = fb_rect.intersect(self.scissor);
448 VkRect2D scissor = { };
449 scissor.offset.x = scissor_rect.left;
450 scissor.offset.y = scissor_rect.bottom;
451 scissor.extent.width = scissor_rect.width;
452 scissor.extent.height = scissor_rect.height;
453 vk.CmdSetScissor(command_buffer, 0, 1, &scissor);