]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/pipelinestate_backend.cpp
Always apply push constants
[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         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);
42
43         if(self.changes&(PipelineState::SHPROG|PipelineState::UNIFORMS|PipelineState::TEXTURES))
44         {
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)
48                         {
49                                 u.used = self.shprog->uses_binding(u.binding);
50                                 if(u.binding>=0)
51                                         changed_sets |= 1<<(u.binding>>20);
52                                 u.changed = false;
53                         }
54
55                 descriptor_set_handles.resize(self.shprog->get_n_descriptor_sets());
56                 for(unsigned i=0; i<descriptor_set_handles.size(); ++i)
57                         if(changed_sets&(1<<i))
58                                 descriptor_set_handles[i] = device.get_pipeline_cache().get_descriptor_set(self, i);
59         }
60
61         self.changes = 0;
62 }
63
64 uint64_t VulkanPipelineState::compute_hash() const
65 {
66         const PipelineState &self = *static_cast<const PipelineState *>(this);
67         const FrameFormat &format = self.framebuffer->get_format();
68
69         uint64_t result = hash<64>(self.shprog);
70         result = hash_update<64>(result, self.vertex_setup->compute_hash());
71         result = hash_round<64>(result, self.primitive_type);
72
73         if(self.front_face!=NON_MANIFOLD && self.face_cull!=NO_CULL)
74         {
75                 result = hash_round<64>(result, self.front_face);
76                 result = hash_round<64>(result, self.face_cull);
77         }
78
79         result = hash_round<64>(result, format.get_samples());
80
81         if(const DepthTest *depth_test = self.depth_test)
82                 if(depth_test->enabled)
83                 {
84                         result = hash_round<64>(result, depth_test->compare);
85                         result = hash_update<64>(result, depth_test->write);
86                 }
87
88         if(const StencilTest *stencil_test = self.stencil_test)
89                 if(stencil_test->enabled)
90                 {
91                         result = hash_round<64>(result, stencil_test->compare);
92                         result = hash_round<64>(result, stencil_test->stencil_fail_op);
93                         result = hash_round<64>(result, stencil_test->depth_fail_op);
94                         result = hash_round<64>(result, stencil_test->depth_pass_op);
95                         result = hash_update<64>(result, stencil_test->reference);
96                 }
97
98         if(const Blend *blend = self.blend)
99                 if(blend->enabled)
100                 {
101                         result = hash_round<64>(result, blend->equation);
102                         result = hash_round<64>(result, blend->src_factor);
103                         result = hash_round<64>(result, blend->dst_factor);
104                         result = hash_round<64>(result, blend->write_mask);
105                 }
106
107         for(FrameAttachment a: format)
108                 result = hash_update<64>(result, a);
109
110         return result;
111 }
112
113 void VulkanPipelineState::fill_creation_info(vector<char> &buffer) const
114 {
115         const PipelineState &self = *static_cast<const PipelineState *>(this);
116
117         const FrameFormat &format = self.framebuffer->get_format();
118         VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(format, false, false, false);
119
120         unsigned n_color_attachments = 0;
121         for(FrameAttachment a: format)
122         {
123                 unsigned attach_pt = get_attach_point(a);
124                 if(attach_pt!=get_attach_point(DEPTH_ATTACHMENT) && attach_pt!=get_attach_point(STENCIL_ATTACHMENT))
125                         ++n_color_attachments;
126         }
127
128         StructureBuilder sb(buffer, 10);
129         VkGraphicsPipelineCreateInfo *&pipeline_info = sb.add<VkGraphicsPipelineCreateInfo>();
130         VkPipelineInputAssemblyStateCreateInfo *&input_assembly_info = sb.add<VkPipelineInputAssemblyStateCreateInfo>();
131         VkPipelineViewportStateCreateInfo *&viewport_info = sb.add<VkPipelineViewportStateCreateInfo>();
132         VkPipelineRasterizationStateCreateInfo *&raster_info = sb.add<VkPipelineRasterizationStateCreateInfo>();
133         VkPipelineMultisampleStateCreateInfo *&multisample_info = sb.add<VkPipelineMultisampleStateCreateInfo>();
134         VkPipelineDepthStencilStateCreateInfo *&depth_stencil_info = sb.add<VkPipelineDepthStencilStateCreateInfo>();
135         VkPipelineColorBlendStateCreateInfo *&blend_info = sb.add<VkPipelineColorBlendStateCreateInfo>();
136         VkPipelineColorBlendAttachmentState *&blend_attachments = sb.add<VkPipelineColorBlendAttachmentState>(n_color_attachments);
137         VkPipelineDynamicStateCreateInfo *&dynamic_info = sb.add<VkPipelineDynamicStateCreateInfo>();
138         VkDynamicState *&dynamic_states = sb.add<VkDynamicState>(2);
139
140         input_assembly_info->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
141         input_assembly_info->topology = static_cast<VkPrimitiveTopology>(get_vulkan_primitive_type(self.primitive_type));
142         input_assembly_info->primitiveRestartEnable = true;
143
144         viewport_info->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
145         viewport_info->viewportCount = 1;
146         viewport_info->pViewports = 0;
147         viewport_info->scissorCount = 1;
148         viewport_info->pScissors = 0;
149
150         raster_info->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
151         raster_info->depthClampEnable = VK_FALSE;
152         raster_info->rasterizerDiscardEnable = VK_FALSE;
153         raster_info->polygonMode = VK_POLYGON_MODE_FILL;
154         if(self.face_cull==NO_CULL || self.front_face==NON_MANIFOLD)
155         {
156                 raster_info->cullMode = VK_CULL_MODE_NONE;
157                 raster_info->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
158         }
159         else
160         {
161                 raster_info->cullMode = (self.face_cull==CULL_FRONT ? VK_CULL_MODE_FRONT_BIT : VK_CULL_MODE_BACK_BIT);
162                 raster_info->frontFace = (self.front_face==CLOCKWISE ? VK_FRONT_FACE_CLOCKWISE : VK_FRONT_FACE_COUNTER_CLOCKWISE);
163         }
164         raster_info->depthBiasEnable = VK_FALSE;
165         raster_info->depthBiasConstantFactor = 0.0f;
166         raster_info->depthBiasClamp = 0.0f;
167         raster_info->depthBiasSlopeFactor = 0.0f;
168         raster_info->lineWidth = 1.0f;
169
170         multisample_info->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
171         multisample_info->rasterizationSamples = static_cast<VkSampleCountFlagBits>(get_vulkan_samples(format.get_samples()));
172         multisample_info->sampleShadingEnable = VK_FALSE;
173         multisample_info->minSampleShading = 1.0f;
174         multisample_info->pSampleMask = 0;
175         multisample_info->alphaToCoverageEnable = VK_FALSE;
176         multisample_info->alphaToOneEnable = VK_FALSE;
177
178         depth_stencil_info->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
179         if(const DepthTest *depth_test = self.depth_test)
180         {
181                 depth_stencil_info->depthTestEnable = depth_test->enabled;
182                 depth_stencil_info->depthWriteEnable = depth_test->write;
183                 depth_stencil_info->depthCompareOp = static_cast<VkCompareOp>(get_vulkan_predicate(depth_test->compare));
184                 depth_stencil_info->depthBoundsTestEnable = VK_FALSE;
185         }
186         if(const StencilTest *stencil_test = self.stencil_test)
187         {
188                 depth_stencil_info->stencilTestEnable = stencil_test->enabled;
189                 depth_stencil_info->front.failOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(stencil_test->stencil_fail_op));
190                 depth_stencil_info->front.passOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(stencil_test->depth_pass_op));
191                 depth_stencil_info->front.depthFailOp = static_cast<VkStencilOp>(get_vulkan_stencil_op(stencil_test->depth_fail_op));
192                 depth_stencil_info->front.compareOp = static_cast<VkCompareOp>(get_vulkan_predicate(stencil_test->compare));
193                 depth_stencil_info->front.compareMask = 0xFFFFFFFFU;
194                 depth_stencil_info->front.writeMask = 0xFFFFFFFFU;
195                 depth_stencil_info->front.reference = stencil_test->reference;
196                 depth_stencil_info->back = depth_stencil_info->front;
197         }
198
199         if(const Blend *blend = self.blend)
200         {
201                 for(unsigned i=0; i<n_color_attachments; ++i)
202                 {
203                         blend_attachments[i].blendEnable = blend->enabled;
204                         blend_attachments[i].srcColorBlendFactor = static_cast<VkBlendFactor>(get_vulkan_blend_factor(blend->src_factor));
205                         blend_attachments[i].dstColorBlendFactor = static_cast<VkBlendFactor>(get_vulkan_blend_factor(blend->dst_factor));
206                         blend_attachments[i].colorBlendOp = static_cast<VkBlendOp>(get_vulkan_blend_equation(blend->equation));
207                         blend_attachments[i].srcAlphaBlendFactor = blend_attachments[i].srcColorBlendFactor;
208                         blend_attachments[i].dstAlphaBlendFactor = blend_attachments[i].dstColorBlendFactor;
209                         blend_attachments[i].alphaBlendOp = blend_attachments[i].colorBlendOp;
210                         blend_attachments[i].colorWriteMask = get_vulkan_color_mask(blend->write_mask);
211                 }
212         }
213         else
214         {
215                 for(unsigned i=0; i<n_color_attachments; ++i)
216                         blend_attachments[i].colorWriteMask = VK_COLOR_COMPONENT_R_BIT|VK_COLOR_COMPONENT_G_BIT|VK_COLOR_COMPONENT_B_BIT|VK_COLOR_COMPONENT_A_BIT;
217         }
218
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;
222
223         dynamic_states[0] = VK_DYNAMIC_STATE_VIEWPORT;
224         dynamic_states[1] = VK_DYNAMIC_STATE_SCISSOR;
225
226         dynamic_info->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
227         dynamic_info->dynamicStateCount = 2;
228         dynamic_info->pDynamicStates = dynamic_states;
229
230         pipeline_info->sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
231
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;
242
243         if(self.shprog)
244         {
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);
248         }
249
250         if(self.vertex_setup)
251         {
252                 self.vertex_setup->refresh();
253                 pipeline_info->pVertexInputState = reinterpret_cast<const VkPipelineVertexInputStateCreateInfo *>(self.vertex_setup->creation_info.data());
254         }
255 }
256
257 uint64_t VulkanPipelineState::compute_descriptor_set_hash(unsigned index) const
258 {
259         const PipelineState &self = *static_cast<const PipelineState *>(this);
260
261         uint64_t result = hash<64>(0, 0);
262         for(const PipelineState::BoundUniformBlock &b: self.uniform_blocks)
263                 if(b.used && b.binding>=0 && static_cast<unsigned>(b.binding>>20)==index)
264                 {
265                         result = hash_update<64>(result, b.binding);
266                         result = hash_update<64>(result, reinterpret_cast<uintptr_t>(b.block));
267                 }
268
269         return result;
270 }
271
272 VkDescriptorSetLayout VulkanPipelineState::get_descriptor_set_layout(unsigned index) const
273 {
274         return static_cast<const PipelineState *>(this)->shprog->desc_set_layout_handles[index];
275 }
276
277 unsigned VulkanPipelineState::fill_descriptor_writes(unsigned index, vector<char> &buffer) const
278 {
279         const PipelineState &self = *static_cast<const PipelineState *>(this);
280
281         unsigned n_buffers = 0;
282         for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
283                 if(u.used && u.binding>=0 && static_cast<unsigned>(u.binding>>20)==index)
284                         ++n_buffers;
285
286         StructureBuilder sb(buffer, 2);
287         VkWriteDescriptorSet *&writes = sb.add<VkWriteDescriptorSet>(n_buffers);
288         VkDescriptorBufferInfo *&buffers = sb.add<VkDescriptorBufferInfo>(n_buffers);
289
290         VkWriteDescriptorSet *write_ptr = writes;
291         VkDescriptorBufferInfo *buffer_ptr = buffers;
292
293         for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
294                 if(u.used && u.binding>=0 && static_cast<unsigned>(u.binding>>20)==index)
295                 {
296                         buffer_ptr->buffer = handle_cast<::VkBuffer>(u.block->get_buffer()->handle);
297                         buffer_ptr->offset = u.block->get_offset();
298                         buffer_ptr->range = u.block->get_data_size();
299
300                         write_ptr->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
301                         write_ptr->dstBinding = u.binding&0xFFFFF;
302                         write_ptr->descriptorCount = 1;
303                         write_ptr->descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
304                         write_ptr->pBufferInfo = buffer_ptr;
305
306                         ++buffer_ptr;
307                         ++write_ptr;
308                 }
309
310         return n_buffers;
311 }
312
313 void VulkanPipelineState::apply(VkCommandBuffer command_buffer) const
314 {
315         const PipelineState &self = *static_cast<const PipelineState *>(this);
316         const VulkanFunctions &vk = device.get_functions();
317
318         vk.CmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, handle);
319         if(const VertexSetup *vs = self.vertex_setup)
320         {
321                 vk.CmdBindVertexBuffers(command_buffer, 0, vs->n_bindings, vs->buffers, vs->offsets);
322                 VkIndexType index_type = static_cast<VkIndexType>(get_vulkan_index_type(vs->get_index_type()));
323                 vk.CmdBindIndexBuffer(command_buffer, vs->get_index_buffer()->handle, 0, index_type);
324         }
325
326         if(!self.uniform_blocks.empty())
327         {
328                 const PipelineState::BoundUniformBlock &first_block = self.uniform_blocks.front();
329                 if(first_block.used && first_block.binding==ReflectData::PUSH_CONSTANT)
330                 {
331                         const UniformBlock &pc_block = *first_block.block;
332                         vk.CmdPushConstants(command_buffer, self.shprog->layout_handle, VK_SHADER_STAGE_ALL,
333                                 pc_block.get_offset(), pc_block.get_data_size(), pc_block.get_data_pointer());
334                 }
335         }
336
337         vk.CmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, self.shprog->layout_handle, 0, descriptor_set_handles.size(), descriptor_set_handles.data(), 0, 0);
338
339         VkViewport viewport = { };
340         if(self.viewport)
341         {
342                 viewport.x = self.viewport->left;
343                 viewport.y = self.framebuffer->get_height()-(self.viewport->bottom+self.viewport->height);
344                 viewport.width = self.viewport->width;
345                 viewport.height = self.viewport->height;
346         }
347         else
348         {
349                 viewport.x = 0;
350                 viewport.y = 0;
351                 viewport.width = self.framebuffer->get_width();
352                 viewport.height = self.framebuffer->get_height();
353         }
354         viewport.minDepth = 0.0f;
355         viewport.maxDepth = 1.0f;
356         vk.CmdSetViewport(command_buffer, 0, 1, &viewport);
357
358         VkRect2D scissor = { };
359         if(self.scissor)
360         {
361                 scissor.offset.x = self.scissor->left;
362                 scissor.offset.y = self.framebuffer->get_height()-(self.scissor->bottom+self.scissor->height);
363                 scissor.extent.width = self.scissor->width;
364                 scissor.extent.height = self.scissor->height;
365         }
366         else
367         {
368                 scissor.offset.x = 0;
369                 scissor.offset.y = 0;
370                 scissor.extent.width = self.framebuffer->get_width();
371                 scissor.extent.height = self.framebuffer->get_height();
372         }
373         vk.CmdSetScissor(command_buffer, 0, 1, &scissor);
374 }
375
376 } // namespace GL
377 } // namespace Msp