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 constexpr unsigned pipeline_mask = PipelineState::SHPROG|PipelineState::VERTEX_SETUP|PipelineState::FACE_CULL|
39 PipelineState::DEPTH_TEST|PipelineState::STENCIL_TEST|PipelineState::BLEND|PipelineState::PRIMITIVE_TYPE;
40 if(self.changes&pipeline_mask)
41 handle = device.get_pipeline_cache().get_pipeline(self);
43 if(self.changes&(PipelineState::SHPROG|PipelineState::UNIFORMS|PipelineState::TEXTURES))
45 unsigned changed_sets = (self.changes&PipelineState::SHPROG ? ~0U : 0U);
46 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
47 if(u.changed || changed_sets==~0U)
49 u.used = self.shprog->uses_binding(u.binding);
51 changed_sets |= 1<<(u.binding>>20);
54 for(const PipelineState::BoundTexture &t: self.textures)
55 if(t.changed || changed_sets==~0U)
57 t.used = self.shprog->uses_binding(t.binding);
58 changed_sets |= 1<<(t.binding>>20);
64 descriptor_set_handles.resize(self.shprog->get_n_descriptor_sets());
65 for(unsigned i=0; i<descriptor_set_handles.size(); ++i)
66 if(changed_sets&(1<<i))
67 descriptor_set_handles[i] = device.get_pipeline_cache().get_descriptor_set(self, i);
73 uint64_t VulkanPipelineState::compute_hash() const
75 const PipelineState &self = *static_cast<const PipelineState *>(this);
76 const FrameFormat &format = self.framebuffer->get_format();
78 uint64_t result = hash<64>(self.shprog);
79 result = hash_update<64>(result, self.vertex_setup->compute_hash());
80 result = hash_round<64>(result, self.primitive_type);
82 if(self.front_face!=NON_MANIFOLD && self.face_cull!=NO_CULL)
84 result = hash_round<64>(result, self.front_face);
85 result = hash_round<64>(result, self.face_cull);
88 result = hash_round<64>(result, format.get_samples());
90 if(const DepthTest *depth_test = self.depth_test)
91 if(depth_test->enabled)
93 result = hash_round<64>(result, depth_test->compare);
94 result = hash_update<64>(result, depth_test->write);
97 if(const StencilTest *stencil_test = self.stencil_test)
98 if(stencil_test->enabled)
100 result = hash_round<64>(result, stencil_test->compare);
101 result = hash_round<64>(result, stencil_test->stencil_fail_op);
102 result = hash_round<64>(result, stencil_test->depth_fail_op);
103 result = hash_round<64>(result, stencil_test->depth_pass_op);
104 result = hash_update<64>(result, stencil_test->reference);
107 if(const Blend *blend = self.blend)
110 result = hash_round<64>(result, blend->equation);
111 result = hash_round<64>(result, blend->src_factor);
112 result = hash_round<64>(result, blend->dst_factor);
113 result = hash_round<64>(result, blend->write_mask);
116 for(FrameAttachment a: format)
117 result = hash_update<64>(result, a);
122 void VulkanPipelineState::fill_creation_info(vector<char> &buffer) const
124 const PipelineState &self = *static_cast<const PipelineState *>(this);
126 const FrameFormat &format = self.framebuffer->get_format();
127 VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(format, false, false, false);
129 unsigned n_color_attachments = 0;
130 for(FrameAttachment a: format)
132 unsigned attach_pt = get_attach_point(a);
133 if(attach_pt!=get_attach_point(DEPTH_ATTACHMENT) && attach_pt!=get_attach_point(STENCIL_ATTACHMENT))
134 ++n_color_attachments;
137 StructureBuilder sb(buffer, 10);
138 VkGraphicsPipelineCreateInfo *&pipeline_info = sb.add<VkGraphicsPipelineCreateInfo>();
139 VkPipelineInputAssemblyStateCreateInfo *&input_assembly_info = sb.add<VkPipelineInputAssemblyStateCreateInfo>();
140 VkPipelineViewportStateCreateInfo *&viewport_info = sb.add<VkPipelineViewportStateCreateInfo>();
141 VkPipelineRasterizationStateCreateInfo *&raster_info = sb.add<VkPipelineRasterizationStateCreateInfo>();
142 VkPipelineMultisampleStateCreateInfo *&multisample_info = sb.add<VkPipelineMultisampleStateCreateInfo>();
143 VkPipelineDepthStencilStateCreateInfo *&depth_stencil_info = sb.add<VkPipelineDepthStencilStateCreateInfo>();
144 VkPipelineColorBlendStateCreateInfo *&blend_info = sb.add<VkPipelineColorBlendStateCreateInfo>();
145 VkPipelineColorBlendAttachmentState *&blend_attachments = sb.add<VkPipelineColorBlendAttachmentState>(n_color_attachments);
146 VkPipelineDynamicStateCreateInfo *&dynamic_info = sb.add<VkPipelineDynamicStateCreateInfo>();
147 VkDynamicState *&dynamic_states = sb.add<VkDynamicState>(2);
149 input_assembly_info->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
150 input_assembly_info->topology = static_cast<VkPrimitiveTopology>(get_vulkan_primitive_type(self.primitive_type));
151 input_assembly_info->primitiveRestartEnable = true;
153 viewport_info->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
154 viewport_info->viewportCount = 1;
155 viewport_info->pViewports = 0;
156 viewport_info->scissorCount = 1;
157 viewport_info->pScissors = 0;
159 raster_info->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
160 raster_info->depthClampEnable = VK_FALSE;
161 raster_info->rasterizerDiscardEnable = VK_FALSE;
162 raster_info->polygonMode = VK_POLYGON_MODE_FILL;
163 if(self.face_cull==NO_CULL || self.front_face==NON_MANIFOLD)
165 raster_info->cullMode = VK_CULL_MODE_NONE;
166 raster_info->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
170 raster_info->cullMode = (self.face_cull==CULL_FRONT ? VK_CULL_MODE_FRONT_BIT : VK_CULL_MODE_BACK_BIT);
171 raster_info->frontFace = (self.front_face==CLOCKWISE ? VK_FRONT_FACE_CLOCKWISE : VK_FRONT_FACE_COUNTER_CLOCKWISE);
173 raster_info->depthBiasEnable = VK_FALSE;
174 raster_info->depthBiasConstantFactor = 0.0f;
175 raster_info->depthBiasClamp = 0.0f;
176 raster_info->depthBiasSlopeFactor = 0.0f;
177 raster_info->lineWidth = 1.0f;
179 multisample_info->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
180 multisample_info->rasterizationSamples = static_cast<VkSampleCountFlagBits>(get_vulkan_samples(format.get_samples()));
181 multisample_info->sampleShadingEnable = VK_FALSE;
182 multisample_info->minSampleShading = 1.0f;
183 multisample_info->pSampleMask = 0;
184 multisample_info->alphaToCoverageEnable = VK_FALSE;
185 multisample_info->alphaToOneEnable = VK_FALSE;
187 depth_stencil_info->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
188 if(const DepthTest *depth_test = self.depth_test)
190 depth_stencil_info->depthTestEnable = depth_test->enabled;
191 depth_stencil_info->depthWriteEnable = depth_test->write;
192 depth_stencil_info->depthCompareOp = static_cast<VkCompareOp>(get_vulkan_predicate(depth_test->compare));
193 depth_stencil_info->depthBoundsTestEnable = VK_FALSE;
195 if(const StencilTest *stencil_test = self.stencil_test)
197 depth_stencil_info->stencilTestEnable = stencil_test->enabled;
198 depth_stencil_info->front.failOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(stencil_test->stencil_fail_op));
199 depth_stencil_info->front.passOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(stencil_test->depth_pass_op));
200 depth_stencil_info->front.depthFailOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(stencil_test->depth_fail_op));
201 depth_stencil_info->front.compareOp = static_cast<VkCompareOp>(get_vulkan_predicate(stencil_test->compare));
202 depth_stencil_info->front.compareMask = 0xFFFFFFFFU;
203 depth_stencil_info->front.writeMask = 0xFFFFFFFFU;
204 depth_stencil_info->front.reference = stencil_test->reference;
205 depth_stencil_info->back = depth_stencil_info->front;
208 if(const Blend *blend = self.blend)
210 for(unsigned i=0; i<n_color_attachments; ++i)
212 blend_attachments[i].blendEnable = blend->enabled;
213 blend_attachments[i].srcColorBlendFactor = static_cast<VkBlendFactor>(get_vulkan_blend_factor(blend->src_factor));
214 blend_attachments[i].dstColorBlendFactor = static_cast<VkBlendFactor>(get_vulkan_blend_factor(blend->dst_factor));
215 blend_attachments[i].colorBlendOp = static_cast<VkBlendOp>(get_vulkan_blend_equation(blend->equation));
216 blend_attachments[i].srcAlphaBlendFactor = blend_attachments[i].srcColorBlendFactor;
217 blend_attachments[i].dstAlphaBlendFactor = blend_attachments[i].dstColorBlendFactor;
218 blend_attachments[i].alphaBlendOp = blend_attachments[i].colorBlendOp;
219 blend_attachments[i].colorWriteMask = get_vulkan_color_mask(blend->write_mask);
224 for(unsigned i=0; i<n_color_attachments; ++i)
225 blend_attachments[i].colorWriteMask = VK_COLOR_COMPONENT_R_BIT|VK_COLOR_COMPONENT_G_BIT|VK_COLOR_COMPONENT_B_BIT|VK_COLOR_COMPONENT_A_BIT;
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;
232 dynamic_states[0] = VK_DYNAMIC_STATE_VIEWPORT;
233 dynamic_states[1] = VK_DYNAMIC_STATE_SCISSOR;
235 dynamic_info->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
236 dynamic_info->dynamicStateCount = 2;
237 dynamic_info->pDynamicStates = dynamic_states;
239 pipeline_info->sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
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;
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);
259 if(self.vertex_setup)
261 self.vertex_setup->refresh();
262 pipeline_info->pVertexInputState = reinterpret_cast<const VkPipelineVertexInputStateCreateInfo *>(self.vertex_setup->creation_info.data());
266 uint64_t VulkanPipelineState::compute_descriptor_set_hash(unsigned index) const
268 const PipelineState &self = *static_cast<const PipelineState *>(this);
270 uint64_t result = hash<64>(0, 0);
271 for(const PipelineState::BoundUniformBlock &b: self.uniform_blocks)
272 if(b.used && b.binding>=0 && static_cast<unsigned>(b.binding>>20)==index)
274 result = hash_update<64>(result, b.binding);
275 result = hash_update<64>(result, reinterpret_cast<uintptr_t>(b.block));
277 for(const PipelineState::BoundTexture &t: self.textures)
278 if(t.used && (t.binding>>20)==index)
280 result = hash_update<64>(result, t.binding);
281 result = hash_update<64>(result, reinterpret_cast<uintptr_t>(t.texture));
282 result = hash_update<64>(result, reinterpret_cast<uintptr_t>(t.sampler));
288 VkDescriptorSetLayout VulkanPipelineState::get_descriptor_set_layout(unsigned index) const
290 return static_cast<const PipelineState *>(this)->shprog->desc_set_layout_handles[index];
293 unsigned VulkanPipelineState::fill_descriptor_writes(unsigned index, vector<char> &buffer) const
295 const PipelineState &self = *static_cast<const PipelineState *>(this);
297 unsigned n_buffers = 0;
298 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
299 if(u.used && u.binding>=0 && static_cast<unsigned>(u.binding>>20)==index)
301 unsigned n_images = 0;
302 for(const PipelineState::BoundTexture &t: self.textures)
303 if(t.used && (t.binding>>20)==index)
305 unsigned n_writes = n_buffers+n_images;
307 StructureBuilder sb(buffer, 3);
308 VkWriteDescriptorSet *&writes = sb.add<VkWriteDescriptorSet>(n_writes);
309 VkDescriptorBufferInfo *&buffers = sb.add<VkDescriptorBufferInfo>(n_buffers);
310 VkDescriptorImageInfo *&images = sb.add<VkDescriptorImageInfo>(n_images);
312 VkWriteDescriptorSet *write_ptr = writes;
313 VkDescriptorBufferInfo *buffer_ptr = buffers;
314 VkDescriptorImageInfo *image_ptr = images;
316 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
317 if(u.used && u.binding>=0 && static_cast<unsigned>(u.binding>>20)==index)
319 buffer_ptr->buffer = handle_cast<::VkBuffer>(u.block->get_buffer()->handle);
320 buffer_ptr->offset = u.block->get_offset();
321 buffer_ptr->range = u.block->get_data_size();
323 write_ptr->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
324 write_ptr->dstBinding = u.binding&0xFFFFF;
325 write_ptr->descriptorCount = 1;
326 write_ptr->descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
327 write_ptr->pBufferInfo = buffer_ptr;
333 for(const PipelineState::BoundTexture &t: self.textures)
334 if(t.used && (t.binding>>20)==index)
336 image_ptr->sampler = handle_cast<::VkSampler>(t.sampler->handle);
337 image_ptr->imageView = handle_cast<::VkImageView>(t.texture->view_handle);
338 image_ptr->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
340 write_ptr->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
341 write_ptr->dstBinding = t.binding&0xFFFFF;
342 write_ptr->descriptorCount = 1;
343 write_ptr->descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
344 write_ptr->pImageInfo = image_ptr;
353 void VulkanPipelineState::apply(VkCommandBuffer command_buffer) const
355 const PipelineState &self = *static_cast<const PipelineState *>(this);
356 const VulkanFunctions &vk = device.get_functions();
358 vk.CmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, handle);
359 if(const VertexSetup *vs = self.vertex_setup)
361 vk.CmdBindVertexBuffers(command_buffer, 0, vs->n_bindings, vs->buffers, vs->offsets);
362 VkIndexType index_type = static_cast<VkIndexType>(get_vulkan_index_type(vs->get_index_type()));
363 vk.CmdBindIndexBuffer(command_buffer, vs->get_index_buffer()->handle, 0, index_type);
366 if(!self.uniform_blocks.empty())
368 const PipelineState::BoundUniformBlock &first_block = self.uniform_blocks.front();
369 if(first_block.used && first_block.binding==ReflectData::PUSH_CONSTANT)
371 const UniformBlock &pc_block = *first_block.block;
372 vk.CmdPushConstants(command_buffer, self.shprog->layout_handle, VK_SHADER_STAGE_ALL,
373 pc_block.get_offset(), pc_block.get_data_size(), pc_block.get_data_pointer());
377 vk.CmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, self.shprog->layout_handle, 0, descriptor_set_handles.size(), descriptor_set_handles.data(), 0, 0);
379 VkViewport viewport = { };
382 viewport.x = self.viewport->left;
383 viewport.y = self.framebuffer->get_height()-(self.viewport->bottom+self.viewport->height);
384 viewport.width = self.viewport->width;
385 viewport.height = self.viewport->height;
391 viewport.width = self.framebuffer->get_width();
392 viewport.height = self.framebuffer->get_height();
394 viewport.minDepth = 0.0f;
395 viewport.maxDepth = 1.0f;
396 vk.CmdSetViewport(command_buffer, 0, 1, &viewport);
398 VkRect2D scissor = { };
401 scissor.offset.x = self.scissor->left;
402 scissor.offset.y = self.framebuffer->get_height()-(self.scissor->bottom+self.scissor->height);
403 scissor.extent.width = self.scissor->width;
404 scissor.extent.height = self.scissor->height;
408 scissor.offset.x = 0;
409 scissor.offset.y = 0;
410 scissor.extent.width = self.framebuffer->get_width();
411 scissor.extent.height = self.framebuffer->get_height();
413 vk.CmdSetScissor(command_buffer, 0, 1, &scissor);