1 #include <msp/core/hash.h>
7 #include "framebuffer.h"
8 #include "pipelinestate.h"
9 #include "pipelinestate_backend.h"
13 #include "stenciltest.h"
14 #include "structurebuilder.h"
16 #include "uniformblock.h"
17 #include "vertexsetup.h"
25 VulkanPipelineState::VulkanPipelineState():
26 device(Device::get_current())
29 VulkanPipelineState::VulkanPipelineState(VulkanPipelineState &&other):
34 void VulkanPipelineState::update() const
36 const PipelineState &self = *static_cast<const PipelineState *>(this);
38 if(changes&PipelineState::VERTEX_SETUP)
39 self.vertex_setup->refresh();
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(changes&pipeline_mask)
44 handle = device.get_pipeline_cache().get_pipeline(self);
46 if(changes&(PipelineState::SHPROG|PipelineState::UNIFORMS|PipelineState::TEXTURES))
48 unsigned changed_sets = (changes&PipelineState::SHPROG ? ~0U : 0U);
49 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
50 if(u.changed || changed_sets==~0U)
53 u.used = self.shprog->uses_uniform_block_binding(u.binding);
55 changed_sets |= 1<<(u.binding>>20);
58 for(const PipelineState::BoundTexture &t: self.textures)
59 if(t.changed || changed_sets==~0U)
61 if(t.texture && t.sampler)
62 t.used = self.shprog->uses_texture_binding(t.binding);
63 changed_sets |= 1<<(t.binding>>20);
64 if(t.texture && t.level>=0)
65 t.texture->refresh_mip_views();
71 descriptor_set_handles.resize(self.shprog->get_n_descriptor_sets());
72 for(unsigned i=0; i<descriptor_set_handles.size(); ++i)
73 if(changed_sets&(1<<i))
74 descriptor_set_handles[i] = device.get_pipeline_cache().get_descriptor_set(self, i);
80 uint64_t VulkanPipelineState::compute_hash() const
82 const PipelineState &self = *static_cast<const PipelineState *>(this);
83 const FrameFormat &format = self.framebuffer->get_format();
85 uint64_t result = hash<64>(self.shprog);
86 result = hash_update<64>(result, self.vertex_setup->compute_hash());
87 result = hash_round<64>(result, self.primitive_type);
89 if(self.front_face!=NON_MANIFOLD && self.face_cull!=NO_CULL)
91 result = hash_round<64>(result, self.front_face);
92 result = hash_round<64>(result, self.face_cull);
95 result = hash_round<64>(result, format.get_samples());
97 if(self.depth_test.enabled)
99 result = hash_round<64>(result, self.depth_test.compare);
100 result = hash_update<64>(result, self.depth_test.write);
103 if(self.stencil_test.enabled)
105 result = hash_round<64>(result, self.stencil_test.compare);
106 result = hash_round<64>(result, self.stencil_test.stencil_fail_op);
107 result = hash_round<64>(result, self.stencil_test.depth_fail_op);
108 result = hash_round<64>(result, self.stencil_test.depth_pass_op);
109 result = hash_update<64>(result, self.stencil_test.reference);
112 if(self.blend.enabled)
114 result = hash_round<64>(result, self.blend.equation);
115 result = hash_round<64>(result, self.blend.src_factor);
116 result = hash_round<64>(result, self.blend.dst_factor);
117 result = hash_round<64>(result, self.blend.write_mask);
120 for(FrameAttachment a: format)
121 result = hash_update<64>(result, a);
126 void VulkanPipelineState::fill_creation_info(vector<char> &buffer) const
128 const PipelineState &self = *static_cast<const PipelineState *>(this);
130 const FrameFormat &format = self.framebuffer->get_format();
131 VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(format, false, false, false);
133 unsigned n_color_attachments = 0;
134 for(FrameAttachment a: format)
136 unsigned attach_pt = get_attach_point(a);
137 if(attach_pt!=get_attach_point(DEPTH_ATTACHMENT) && attach_pt!=get_attach_point(STENCIL_ATTACHMENT))
138 ++n_color_attachments;
141 StructureBuilder sb(buffer, 10);
142 VkGraphicsPipelineCreateInfo *&pipeline_info = sb.add<VkGraphicsPipelineCreateInfo>();
143 VkPipelineInputAssemblyStateCreateInfo *&input_assembly_info = sb.add<VkPipelineInputAssemblyStateCreateInfo>();
144 VkPipelineViewportStateCreateInfo *&viewport_info = sb.add<VkPipelineViewportStateCreateInfo>();
145 VkPipelineRasterizationStateCreateInfo *&raster_info = sb.add<VkPipelineRasterizationStateCreateInfo>();
146 VkPipelineMultisampleStateCreateInfo *&multisample_info = sb.add<VkPipelineMultisampleStateCreateInfo>();
147 VkPipelineDepthStencilStateCreateInfo *&depth_stencil_info = sb.add<VkPipelineDepthStencilStateCreateInfo>();
148 VkPipelineColorBlendStateCreateInfo *&blend_info = sb.add<VkPipelineColorBlendStateCreateInfo>();
149 VkPipelineColorBlendAttachmentState *&blend_attachments = sb.add<VkPipelineColorBlendAttachmentState>(n_color_attachments);
150 VkPipelineDynamicStateCreateInfo *&dynamic_info = sb.add<VkPipelineDynamicStateCreateInfo>();
151 VkDynamicState *&dynamic_states = sb.add<VkDynamicState>(2);
153 input_assembly_info->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
154 input_assembly_info->topology = static_cast<VkPrimitiveTopology>(get_vulkan_primitive_type(self.primitive_type));
155 input_assembly_info->primitiveRestartEnable = true;
157 viewport_info->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
158 viewport_info->viewportCount = 1;
159 viewport_info->pViewports = 0;
160 viewport_info->scissorCount = 1;
161 viewport_info->pScissors = 0;
163 raster_info->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
164 raster_info->depthClampEnable = VK_FALSE;
165 raster_info->rasterizerDiscardEnable = VK_FALSE;
166 raster_info->polygonMode = VK_POLYGON_MODE_FILL;
167 if(self.face_cull==NO_CULL || self.front_face==NON_MANIFOLD)
169 raster_info->cullMode = VK_CULL_MODE_NONE;
170 raster_info->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
174 raster_info->cullMode = (self.face_cull==CULL_FRONT ? VK_CULL_MODE_FRONT_BIT : VK_CULL_MODE_BACK_BIT);
175 raster_info->frontFace = (self.front_face==CLOCKWISE ? VK_FRONT_FACE_CLOCKWISE : VK_FRONT_FACE_COUNTER_CLOCKWISE);
177 raster_info->depthBiasEnable = VK_FALSE;
178 raster_info->depthBiasConstantFactor = 0.0f;
179 raster_info->depthBiasClamp = 0.0f;
180 raster_info->depthBiasSlopeFactor = 0.0f;
181 raster_info->lineWidth = 1.0f;
183 multisample_info->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
184 multisample_info->rasterizationSamples = static_cast<VkSampleCountFlagBits>(get_vulkan_samples(format.get_samples()));
185 multisample_info->sampleShadingEnable = VK_FALSE;
186 multisample_info->minSampleShading = 1.0f;
187 multisample_info->pSampleMask = 0;
188 multisample_info->alphaToCoverageEnable = VK_FALSE;
189 multisample_info->alphaToOneEnable = VK_FALSE;
191 depth_stencil_info->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
192 depth_stencil_info->depthTestEnable = self.depth_test.enabled;
193 depth_stencil_info->depthWriteEnable = self.depth_test.write;
194 depth_stencil_info->depthCompareOp = static_cast<VkCompareOp>(get_vulkan_predicate(self.depth_test.compare));
195 depth_stencil_info->depthBoundsTestEnable = VK_FALSE;
197 depth_stencil_info->stencilTestEnable = self.stencil_test.enabled;
198 depth_stencil_info->front.failOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(self.stencil_test.stencil_fail_op));
199 depth_stencil_info->front.passOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(self.stencil_test.depth_pass_op));
200 depth_stencil_info->front.depthFailOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(self.stencil_test.depth_fail_op));
201 depth_stencil_info->front.compareOp = static_cast<VkCompareOp>(get_vulkan_predicate(self.stencil_test.compare));
202 depth_stencil_info->front.compareMask = 0xFFFFFFFFU;
203 depth_stencil_info->front.writeMask = 0xFFFFFFFFU;
204 depth_stencil_info->front.reference = self.stencil_test.reference;
205 depth_stencil_info->back = depth_stencil_info->front;
207 for(unsigned i=0; i<n_color_attachments; ++i)
209 blend_attachments[i].blendEnable = self.blend.enabled;
210 blend_attachments[i].srcColorBlendFactor = static_cast<VkBlendFactor>(get_vulkan_blend_factor(self.blend.src_factor));
211 blend_attachments[i].dstColorBlendFactor = static_cast<VkBlendFactor>(get_vulkan_blend_factor(self.blend.dst_factor));
212 blend_attachments[i].colorBlendOp = static_cast<VkBlendOp>(get_vulkan_blend_equation(self.blend.equation));
213 blend_attachments[i].srcAlphaBlendFactor = blend_attachments[i].srcColorBlendFactor;
214 blend_attachments[i].dstAlphaBlendFactor = blend_attachments[i].dstColorBlendFactor;
215 blend_attachments[i].alphaBlendOp = blend_attachments[i].colorBlendOp;
216 blend_attachments[i].colorWriteMask = get_vulkan_color_mask(self.blend.write_mask);
219 blend_info->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
220 blend_info->attachmentCount = n_color_attachments;
221 blend_info->pAttachments = blend_attachments;
223 dynamic_states[0] = VK_DYNAMIC_STATE_VIEWPORT;
224 dynamic_states[1] = VK_DYNAMIC_STATE_SCISSOR;
226 dynamic_info->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
227 dynamic_info->dynamicStateCount = 2;
228 dynamic_info->pDynamicStates = dynamic_states;
230 pipeline_info->sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
232 pipeline_info->pInputAssemblyState = input_assembly_info;
233 pipeline_info->pTessellationState = 0;
234 pipeline_info->pViewportState = viewport_info;
235 pipeline_info->pRasterizationState = raster_info;
236 pipeline_info->pMultisampleState = multisample_info;
237 pipeline_info->pDepthStencilState = depth_stencil_info;
238 pipeline_info->pColorBlendState = blend_info;
239 pipeline_info->pDynamicState = dynamic_info;
240 pipeline_info->renderPass = handle_cast<::VkRenderPass>(render_pass);
241 pipeline_info->subpass = 0;
245 pipeline_info->stageCount = self.shprog->n_stages;
246 pipeline_info->pStages = reinterpret_cast<const VkPipelineShaderStageCreateInfo *>(self.shprog->creation_info.data());
247 pipeline_info->layout = handle_cast<::VkPipelineLayout>(self.shprog->layout_handle);
250 if(self.vertex_setup)
251 pipeline_info->pVertexInputState = reinterpret_cast<const VkPipelineVertexInputStateCreateInfo *>(self.vertex_setup->creation_info.data());
254 uint64_t VulkanPipelineState::compute_descriptor_set_hash(unsigned index) const
256 const PipelineState &self = *static_cast<const PipelineState *>(this);
258 uint64_t result = hash<64>(0, 0);
259 for(const PipelineState::BoundUniformBlock &b: self.uniform_blocks)
260 if(b.used && b.binding>=0 && static_cast<unsigned>(b.binding>>20)==index)
262 result = hash_update<64>(result, b.binding);
263 result = hash_update<64>(result, reinterpret_cast<uintptr_t>(b.block));
264 result = hash_update<64>(result, reinterpret_cast<uintptr_t>(b.buffer->handle));
266 for(const PipelineState::BoundTexture &t: self.textures)
267 if(t.used && (t.binding>>20)==index)
269 result = hash_update<64>(result, t.binding);
270 result = hash_update<64>(result, reinterpret_cast<uintptr_t>(t.texture->handle));
271 result = hash_update<64>(result, reinterpret_cast<uintptr_t>(t.sampler->handle));
272 result = hash_update<64>(result, t.level);
278 VkDescriptorSetLayout VulkanPipelineState::get_descriptor_set_layout(unsigned index) const
280 return static_cast<const PipelineState *>(this)->shprog->desc_set_layout_handles[index];
283 unsigned VulkanPipelineState::fill_descriptor_writes(unsigned index, vector<char> &buffer) const
285 const PipelineState &self = *static_cast<const PipelineState *>(this);
287 unsigned n_buffers = 0;
288 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
289 if(u.used && u.binding>=0 && static_cast<unsigned>(u.binding>>20)==index)
291 unsigned n_images = 0;
292 for(const PipelineState::BoundTexture &t: self.textures)
293 if(t.used && (t.binding>>20)==index)
295 unsigned n_writes = n_buffers+n_images;
297 StructureBuilder sb(buffer, 3);
298 VkWriteDescriptorSet *&writes = sb.add<VkWriteDescriptorSet>(n_writes);
299 VkDescriptorBufferInfo *&buffers = sb.add<VkDescriptorBufferInfo>(n_buffers);
300 VkDescriptorImageInfo *&images = sb.add<VkDescriptorImageInfo>(n_images);
302 VkWriteDescriptorSet *write_ptr = writes;
303 VkDescriptorBufferInfo *buffer_ptr = buffers;
304 VkDescriptorImageInfo *image_ptr = images;
306 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
307 if(u.used && u.binding>=0 && static_cast<unsigned>(u.binding>>20)==index)
309 buffer_ptr->buffer = handle_cast<::VkBuffer>(u.buffer->handle);
310 buffer_ptr->offset = u.block->get_offset();
311 buffer_ptr->range = u.block->get_data_size();
313 write_ptr->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
314 write_ptr->dstBinding = u.binding&0xFFFFF;
315 write_ptr->descriptorCount = 1;
316 write_ptr->descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
317 write_ptr->pBufferInfo = buffer_ptr;
323 for(const PipelineState::BoundTexture &t: self.textures)
324 if(t.used && (t.binding>>20)==index)
326 image_ptr->sampler = handle_cast<::VkSampler>(t.sampler->handle);
328 image_ptr->imageView = handle_cast<::VkImageView>(t.texture->view_handle);
330 image_ptr->imageView = handle_cast<::VkImageView>(t.texture->mip_view_handles[t.level]);
331 image_ptr->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
333 write_ptr->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
334 write_ptr->dstBinding = t.binding&0xFFFFF;
335 write_ptr->descriptorCount = 1;
336 write_ptr->descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
337 write_ptr->pImageInfo = image_ptr;
346 void VulkanPipelineState::apply(VkCommandBuffer command_buffer, unsigned frame, bool negative_viewport) const
348 const PipelineState &self = *static_cast<const PipelineState *>(this);
349 const VulkanFunctions &vk = device.get_functions();
351 vk.CmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, handle);
352 if(const VertexSetup *vs = self.vertex_setup)
354 vk.CmdBindVertexBuffers(command_buffer, 0, vs->n_bindings, vs->buffers, vs->offsets);
355 VkIndexType index_type = static_cast<VkIndexType>(get_vulkan_index_type(vs->get_index_type()));
356 vk.CmdBindIndexBuffer(command_buffer, vs->get_index_buffer()->handle, 0, index_type);
359 if(!self.uniform_blocks.empty())
361 const PipelineState::BoundUniformBlock &first_block = self.uniform_blocks.front();
362 if(first_block.used && first_block.binding==ReflectData::PUSH_CONSTANT)
364 const UniformBlock &pc_block = *first_block.block;
365 vk.CmdPushConstants(command_buffer, self.shprog->layout_handle, self.shprog->stage_flags,
366 pc_block.get_offset(), pc_block.get_data_size(), pc_block.get_data_pointer());
370 if(!descriptor_set_handles.empty())
372 vector<uint32_t> dynamic_offsets;
373 dynamic_offsets.reserve(self.uniform_blocks.size());
374 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
375 if(u.used && u.binding>=0)
377 if(u.buffer->get_usage()==STREAMING)
378 dynamic_offsets.push_back(frame*u.buffer->get_size());
380 dynamic_offsets.push_back(0);
383 vk.CmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, self.shprog->layout_handle,
384 0, descriptor_set_handles.size(), descriptor_set_handles.data(), dynamic_offsets.size(), dynamic_offsets.data());
387 Rect fb_rect = self.framebuffer->get_rect();
389 Rect viewport_rect = fb_rect.intersect(self.viewport);
390 VkViewport viewport = { };
391 viewport.x = viewport_rect.left;
392 viewport.y = viewport_rect.bottom;
393 viewport.width = viewport_rect.width;
394 viewport.height = viewport_rect.height;
395 if(negative_viewport)
397 viewport.y += viewport.height;
398 viewport.height = -viewport.height;
400 viewport.minDepth = 0.0f;
401 viewport.maxDepth = 1.0f;
402 vk.CmdSetViewport(command_buffer, 0, 1, &viewport);
404 Rect scissor_rect = fb_rect.intersect(self.scissor);
405 VkRect2D scissor = { };
406 scissor.offset.x = scissor_rect.left;
407 scissor.offset.y = scissor_rect.bottom;
408 scissor.extent.width = scissor_rect.width;
409 scissor.extent.height = scissor_rect.height;
410 vk.CmdSetScissor(command_buffer, 0, 1, &scissor);