Elffuss BraidHub

kikocis/ncnn public

master · 14.4 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "pipeline.h"
16
17#include "layer_shader_type.h"
18#include "mat.h"
19#include "pipelinecache.h"
20#include "option.h"
21
22#if __ANDROID_API__ >= 26
23#include <android/hardware_buffer.h>
24#endif // __ANDROID_API__ >= 26
25
26namespace ncnn {
27
28#if NCNN_VULKAN
29class PipelinePrivate
30{
31public:
32 VkShaderModule shader_module;
33 VkDescriptorSetLayout descriptorset_layout;
34 VkPipelineLayout pipeline_layout;
35 VkPipeline pipeline;
36 VkDescriptorUpdateTemplateKHR descriptor_update_template;
37
38 ShaderInfo shader_info;
39
40 uint32_t local_size_x;
41 uint32_t local_size_y;
42 uint32_t local_size_z;
43};
44
45Pipeline::Pipeline(const VulkanDevice* _vkdev)
46 : vkdev(_vkdev), d(new PipelinePrivate)
47{
48 d->shader_module = 0;
49 d->descriptorset_layout = 0;
50 d->pipeline_layout = 0;
51 d->pipeline = 0;
52 d->descriptor_update_template = 0;
53
54 d->local_size_x = 1;
55 d->local_size_y = 1;
56 d->local_size_z = 1;
57}
58
59Pipeline::~Pipeline()
60{
61 delete d;
62}
63
64Pipeline::Pipeline(const Pipeline&)
65 : d(0)
66{
67}
68
69Pipeline& Pipeline::operator=(const Pipeline&)
70{
71 return *this;
72}
73
74void Pipeline::set_optimal_local_size_xyz(int w, int h, int c)
75{
76 set_optimal_local_size_xyz(Mat(w, h, c, (void*)0));
77}
78
79void Pipeline::set_optimal_local_size_xyz(const Mat& local_size_xyz)
80{
81 int w = local_size_xyz.w;
82 int h = local_size_xyz.h;
83 int c = local_size_xyz.c;
84
85 if (w == 0 && h == 0 && c == 0)
86 {
87 // fallback to the common and safe 4x4x4
88 w = 4;
89 h = 4;
90 c = 4;
91 }
92
93 w = std::min(w, (int)vkdev->info.max_workgroup_size_x());
94 h = std::min(h, (int)vkdev->info.max_workgroup_size_y());
95 c = std::min(c, (int)vkdev->info.max_workgroup_size_z());
96
97 if (w * h * c <= (int)vkdev->info.max_workgroup_invocations())
98 {
99 return set_local_size_xyz(w, h, c);
100 }
101
102 int max_local_size_xy = (int)vkdev->info.max_workgroup_invocations() / c;
103
104 int wh_max = std::max(1, (int)sqrt(max_local_size_xy));
105 while (w * h >= wh_max)
106 {
107 w = std::max(1, w / 2);
108 h = std::max(1, h / 2);
109 }
110
111 set_local_size_xyz(w, h, c);
112}
113
114void Pipeline::set_local_size_xyz(int w, int h, int c)
115{
116 d->local_size_x = w;
117 d->local_size_y = h;
118 d->local_size_z = c;
119
120 // NCNN_LOGE("local size = %d %d %d", local_size_x, local_size_y, local_size_z);
121}
122
123int Pipeline::create(const uint32_t* spv_data, size_t spv_data_size, const std::vector<vk_specialization_type>& specializations)
124{
125 const PipelineCache* pipeline_cache = vkdev->get_pipeline_cache();
126
127 // get from pipeline cache
128 return pipeline_cache->get_pipeline(spv_data, spv_data_size, specializations, d->local_size_x, d->local_size_y, d->local_size_z,
129 &d->shader_module, &d->descriptorset_layout, &d->pipeline_layout, &d->pipeline, &d->descriptor_update_template,
130 d->shader_info);
131}
132
133int Pipeline::create(int shader_type_index, const Option& opt, const std::vector<vk_specialization_type>& specializations)
134{
135 const PipelineCache* pipeline_cache = opt.pipeline_cache ? opt.pipeline_cache : vkdev->get_pipeline_cache();
136
137 // get from pipeline cache
138 return pipeline_cache->get_pipeline(shader_type_index, opt, specializations, d->local_size_x, d->local_size_y, d->local_size_z,
139 &d->shader_module, &d->descriptorset_layout, &d->pipeline_layout, &d->pipeline, &d->descriptor_update_template,
140 d->shader_info);
141}
142
143VkShaderModule Pipeline::shader_module() const
144{
145 return d->shader_module;
146}
147
148VkDescriptorSetLayout Pipeline::descriptorset_layout() const
149{
150 return d->descriptorset_layout;
151}
152
153VkPipelineLayout Pipeline::pipeline_layout() const
154{
155 return d->pipeline_layout;
156}
157
158VkPipeline Pipeline::pipeline() const
159{
160 return d->pipeline;
161}
162
163VkDescriptorUpdateTemplateKHR Pipeline::descriptor_update_template() const
164{
165 return d->descriptor_update_template;
166}
167
168const ShaderInfo& Pipeline::shader_info() const
169{
170 return d->shader_info;
171}
172
173uint32_t Pipeline::local_size_x() const
174{
175 return d->local_size_x;
176}
177
178uint32_t Pipeline::local_size_y() const
179{
180 return d->local_size_y;
181}
182
183uint32_t Pipeline::local_size_z() const
184{
185 return d->local_size_z;
186}
187
188void Pipeline::set_shader_module(VkShaderModule shader_module)
189{
190 d->shader_module = shader_module;
191}
192
193void Pipeline::set_descriptorset_layout(VkDescriptorSetLayout descriptorset_layout)
194{
195 d->descriptorset_layout = descriptorset_layout;
196}
197
198void Pipeline::set_pipeline_layout(VkPipelineLayout pipeline_layout)
199{
200 d->pipeline_layout = pipeline_layout;
201}
202
203void Pipeline::set_pipeline(VkPipeline pipeline)
204{
205 d->pipeline = pipeline;
206}
207
208void Pipeline::set_descriptor_update_template(VkDescriptorUpdateTemplateKHR descriptor_update_template)
209{
210 d->descriptor_update_template = descriptor_update_template;
211}
212
213void Pipeline::set_shader_info(const ShaderInfo& shader_info)
214{
215 d->shader_info = shader_info;
216}
217
218#if NCNN_PLATFORM_API
219#if __ANDROID_API__ >= 26
220ImportAndroidHardwareBufferPipeline::ImportAndroidHardwareBufferPipeline(const VulkanDevice* _vkdev)
221 : Pipeline(_vkdev)
222{
223 sampler = 0;
224}
225
226ImportAndroidHardwareBufferPipeline::~ImportAndroidHardwareBufferPipeline()
227{
228 destroy();
229}
230
231int ImportAndroidHardwareBufferPipeline::create(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator, int _type_to, int _rotate_from, const Option& opt)
232{
233 int target_width;
234 int target_height;
235
236 if (rotate_from < 5) // 1 2 3 4
237 {
238 target_width = ahb_im_allocator->width();
239 target_height = ahb_im_allocator->height();
240 }
241 else // 5 6 7 8
242 {
243 target_width = ahb_im_allocator->height();
244 target_height = ahb_im_allocator->width();
245 }
246
247 return create(ahb_im_allocator, _type_to, _rotate_from, target_width, target_height, opt);
248}
249
250int ImportAndroidHardwareBufferPipeline::create(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator, int _type_to, int _rotate_from, int target_width, int target_height, const Option& opt)
251{
252 int w = ahb_im_allocator->width();
253 int h = ahb_im_allocator->height();
254
255 type_to = _type_to;
256 rotate_from = _rotate_from;
257
258 need_resize = false;
259 if (rotate_from < 5) // 1 2 3 4
260 {
261 if (target_width != w || target_height != h)
262 need_resize = true;
263 }
264 else // 5 6 7 8
265 {
266 if (target_width != h || target_height != w)
267 need_resize = true;
268 }
269
270 // if (type_to == 1 || type_to == 2)
271 // {
272 // outc = 3;
273 // out_elemsize = vkdev->info.support_fp16_storage() && opt.use_fp16_storage ? 2u : 4u;
274 // out_elempack = 1;
275 // }
276 // else if (type_to == 3)
277 // {
278 // outc = 1;
279 // out_elemsize = vkdev->info.support_fp16_storage() && opt.use_fp16_storage ? 2u : 4u;
280 // out_elempack = 1;
281 // }
282 // else // if (type_to == 4 || type_to == 5)
283 // {
284 // outc = 1;
285 // out_elemsize = ((vkdev->info.support_fp16_packed() && opt.use_fp16_packed) || (vkdev->info.support_fp16_storage() && opt.use_fp16_storage)) ? 8u : 16u;
286 // out_elempack = 4;
287 // }
288
289 set_local_size_xyz(8, 8, 1);
290
291 std::vector<vk_specialization_type> specializations(7);
292 specializations[0].i = ahb_im_allocator->width();
293 specializations[1].i = ahb_im_allocator->height();
294 specializations[2].i = target_width;
295 specializations[3].i = target_height;
296 specializations[4].i = type_to;
297 specializations[5].i = rotate_from;
298 specializations[6].i = need_resize;
299
300 create_shader_module(opt);
301
302 const ShaderInfo& _shader_info = shader_info();
303
304 if ((int)specializations.size() != _shader_info.specialization_count)
305 {
306 NCNN_LOGE("pipeline convert_ycbcr specialization count mismatch, expect %d but got %d", _shader_info.specialization_count, (int)specializations.size());
307 return -1;
308 }
309
310 create_sampler(ahb_im_allocator);
311
312 create_descriptorset_layout();
313
314 VkPipelineLayout pipeline_layout = 0;
315 VkPipeline pipeline = 0;
316 VkDescriptorUpdateTemplateKHR descriptor_update_template = 0;
317
318 vkdev->create_pipeline_layout(_shader_info.push_constant_count, descriptorset_layout(), &pipeline_layout);
319
320 vkdev->create_pipeline(shader_module(), pipeline_layout, specializations, &pipeline);
321
322 if (vkdev->info.support_VK_KHR_descriptor_update_template())
323 {
324 vkdev->create_descriptor_update_template(_shader_info.binding_count, _shader_info.binding_types, descriptorset_layout(), pipeline_layout, &descriptor_update_template);
325 }
326
327 set_pipeline_layout(pipeline_layout);
328 set_pipeline(pipeline);
329 set_descriptor_update_template(descriptor_update_template);
330
331 return 0;
332}
333
334void ImportAndroidHardwareBufferPipeline::destroy()
335{
336 if (sampler)
337 {
338 vkDestroySampler(vkdev->vkdevice(), sampler, 0);
339 sampler = 0;
340 }
341}
342
343int ImportAndroidHardwareBufferPipeline::create_shader_module(const Option& opt)
344{
345 int shader_type_index = LayerShaderType::convert_ycbcr;
346
347 std::vector<uint32_t> spirv;
348 int retc = compile_spirv_module(shader_type_index, opt, spirv);
349 if (retc != 0)
350 {
351 NCNN_LOGE("compile_spirv_module failed %d", retc);
352 return -1;
353 }
354
355 const uint32_t* spv_data = spirv.data();
356 size_t spv_data_size = spirv.size() * 4;
357
358 ShaderInfo shader_info;
359 int ret = resolve_shader_info(spv_data, spv_data_size, shader_info);
360 if (ret != 0)
361 {
362 NCNN_LOGE("resolve_shader_info failed %d", ret);
363 return -1;
364 }
365
366 set_shader_info(shader_info);
367
368 VkShaderModule shader_module = vkdev->compile_shader_module(spv_data, spv_data_size, local_size_x(), local_size_y(), local_size_z());
369 set_shader_module(shader_module);
370
371 return 0;
372}
373
374int ImportAndroidHardwareBufferPipeline::create_sampler(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator)
375{
376 VkResult ret;
377
378 VkExternalFormatANDROID externalFormatANDROID;
379 externalFormatANDROID.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID;
380 externalFormatANDROID.pNext = 0;
381 externalFormatANDROID.externalFormat = ahb_im_allocator->external_format();
382
383 VkSamplerYcbcrConversionInfoKHR samplerYcbcrConversionInfo;
384 samplerYcbcrConversionInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO_KHR;
385 samplerYcbcrConversionInfo.pNext = &externalFormatANDROID;
386 samplerYcbcrConversionInfo.conversion = ahb_im_allocator->samplerYcbcrConversion;
387
388 VkSamplerCreateInfo samplerCreateInfo;
389 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
390 samplerCreateInfo.pNext = &samplerYcbcrConversionInfo;
391 samplerCreateInfo.magFilter = need_resize ? VK_FILTER_LINEAR : VK_FILTER_NEAREST;
392 samplerCreateInfo.minFilter = need_resize ? VK_FILTER_LINEAR : VK_FILTER_NEAREST;
393 samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
394 samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
395 samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
396 samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
397 samplerCreateInfo.mipLodBias = 0.0f;
398 samplerCreateInfo.anisotropyEnable = VK_FALSE;
399 samplerCreateInfo.maxAnisotropy = 1;
400 samplerCreateInfo.compareEnable = VK_FALSE;
401 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
402 samplerCreateInfo.minLod = 0.0f;
403 samplerCreateInfo.maxLod = 0.0f;
404 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK; //VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; FIXME
405 samplerCreateInfo.unnormalizedCoordinates = VK_TRUE; //VK_FALSE; FIXME ?
406
407 ret = vkCreateSampler(vkdev->vkdevice(), &samplerCreateInfo, 0, &sampler);
408 if (ret != VK_SUCCESS)
409 {
410 NCNN_LOGE("vkCreateSampler failed %d", ret);
411 return -1;
412 }
413
414 return 0;
415}
416
417int ImportAndroidHardwareBufferPipeline::create_descriptorset_layout()
418{
419 VkDescriptorSetLayoutBinding descriptorSetLayoutBindings[3];
420 descriptorSetLayoutBindings[0].binding = 0;
421 descriptorSetLayoutBindings[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
422 descriptorSetLayoutBindings[0].descriptorCount = 1;
423 descriptorSetLayoutBindings[0].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
424 descriptorSetLayoutBindings[0].pImmutableSamplers = &sampler;
425 descriptorSetLayoutBindings[1].binding = 1;
426 descriptorSetLayoutBindings[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
427 descriptorSetLayoutBindings[1].descriptorCount = 1;
428 descriptorSetLayoutBindings[1].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
429 descriptorSetLayoutBindings[1].pImmutableSamplers = 0;
430 descriptorSetLayoutBindings[2].binding = 2;
431 descriptorSetLayoutBindings[2].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
432 descriptorSetLayoutBindings[2].descriptorCount = 1;
433 descriptorSetLayoutBindings[2].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
434 descriptorSetLayoutBindings[2].pImmutableSamplers = 0;
435
436 VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo;
437 descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
438 descriptorSetLayoutCreateInfo.pNext = 0;
439 descriptorSetLayoutCreateInfo.flags = 0;
440 descriptorSetLayoutCreateInfo.bindingCount = 3;
441 descriptorSetLayoutCreateInfo.pBindings = descriptorSetLayoutBindings;
442
443 if (vkdev->info.support_VK_KHR_push_descriptor())
444 {
445 descriptorSetLayoutCreateInfo.flags |= VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR;
446 }
447
448 VkDescriptorSetLayout descriptorset_layout = 0;
449 VkResult ret = vkCreateDescriptorSetLayout(vkdev->vkdevice(), &descriptorSetLayoutCreateInfo, 0, &descriptorset_layout);
450 if (ret != VK_SUCCESS)
451 {
452 NCNN_LOGE("vkCreateDescriptorSetLayout failed %d", ret);
453 return -1;
454 }
455
456 set_descriptorset_layout(descriptorset_layout);
457
458 return 0;
459}
460#endif // __ANDROID_API__ >= 26
461#endif // NCNN_PLATFORM_API
462
463#endif // NCNN_VULKAN
464
465} // namespace ncnn