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