forked from Tencent/ncnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.h
295 lines (230 loc) · 8.92 KB
/
command.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#ifndef NCNN_COMMAND_H
#define NCNN_COMMAND_H
#include "platform.h"
#if NCNN_VULKAN
#include "mat.h"
#include <vulkan/vulkan.h>
namespace ncnn {
class Pipeline;
class VkCompute
{
public:
VkCompute(const VulkanDevice* vkdev);
virtual ~VkCompute();
public:
void record_upload(const Mat& src, VkMat& dst, const Option& opt);
void record_upload(const Mat& src, VkImageMat& dst, const Option& opt);
void record_download(const VkMat& src, Mat& dst, const Option& opt);
void record_download(const VkImageMat& src, Mat& dst, const Option& opt);
void record_buffer_to_image(const VkMat& src, VkImageMat& dst, const Option& opt);
void record_image_to_buffer(const VkImageMat& src, VkMat& dst, const Option& opt);
void record_clone(const Mat& src, VkMat& dst, const Option& opt);
void record_clone(const Mat& src, VkImageMat& dst, const Option& opt);
void record_clone(const VkMat& src, Mat& dst, const Option& opt);
void record_clone(const VkImageMat& src, Mat& dst, const Option& opt);
void record_clone(const VkMat& src, VkMat& dst, const Option& opt);
void record_clone(const VkImageMat& src, VkImageMat& dst, const Option& opt);
void record_clone(const VkMat& src, VkImageMat& dst, const Option& opt);
void record_clone(const VkImageMat& src, VkMat& dst, const Option& opt);
void record_pipeline(const Pipeline* pipeline, const std::vector<VkMat>& bindings, const std::vector<vk_constant_type>& constants, const VkMat& dispatcher);
void record_pipeline(const Pipeline* pipeline, const std::vector<VkImageMat>& bindings, const std::vector<vk_constant_type>& constants, const VkImageMat& dispatcher);
void record_pipeline(const Pipeline* pipeline, const std::vector<VkMat>& buffer_bindings, const std::vector<VkImageMat>& image_bindings, const std::vector<vk_constant_type>& constants, const VkMat& dispatcher);
void record_pipeline(const Pipeline* pipeline, const std::vector<VkMat>& buffer_bindings, const std::vector<VkImageMat>& image_bindings, const std::vector<vk_constant_type>& constants, const VkImageMat& dispatcher);
void record_pipeline(const Pipeline* pipeline, const std::vector<VkMat>& buffer_bindings, const std::vector<VkImageMat>& image_bindings, const std::vector<vk_constant_type>& constants, const Mat& dispatcher);
#if NCNN_BENCHMARK
void record_write_timestamp(uint32_t query);
#endif // NCNN_BENCHMARK
#if __ANDROID_API__ >= 26
void record_import_android_hardware_buffer(const ImportAndroidHardwareBufferPipeline* pipeline, const VkImageMat& src, const VkMat& dst);
void record_import_android_hardware_buffer(const ImportAndroidHardwareBufferPipeline* pipeline, const VkImageMat& src, const VkImageMat& dst);
#endif // __ANDROID_API__ >= 26
int submit_and_wait();
int reset();
#if NCNN_BENCHMARK
int create_query_pool(uint32_t query_count);
int get_query_pool_results(uint32_t first_query, uint32_t query_count, std::vector<uint64_t>& results);
#endif // NCNN_BENCHMARK
protected:
int init();
int begin_command_buffer();
int end_command_buffer();
protected:
const VulkanDevice* vkdev;
VkCommandPool compute_command_pool;
VkCommandBuffer compute_command_buffer;
VkFence compute_command_fence;
std::vector<VkMat> upload_staging_buffers;
std::vector<VkMat> download_post_buffers;
std::vector<Mat> download_post_mats_fp16;
std::vector<Mat> download_post_mats;
std::vector<VkImageMemory*> image_blocks_to_destroy;
// the good-old path for device without VK_KHR_push_descriptor
std::vector<VkDescriptorPool> descriptor_pools;
std::vector<VkDescriptorSet> descriptorsets;
struct record
{
enum
{
TYPE_copy_buffer,
TYPE_copy_image,
TYPE_copy_buffer_to_image,
TYPE_copy_image_to_buffer,
TYPE_bind_pipeline,
TYPE_bind_descriptorsets,
TYPE_push_constants,
TYPE_dispatch,
TYPE_memory_barrers,
TYPE_buffer_barrers,
TYPE_image_barrers,
#if NCNN_BENCHMARK
TYPE_write_timestamp,
#endif // NCNN_BENCHMARK
TYPE_post_download,
TYPE_post_cast_float16_to_float32,
};
int type;
VkCommandBuffer command_buffer;
union
{
struct
{
VkBuffer src;
VkBuffer dst;
uint32_t region_count;
const VkBufferCopy* regions;
} copy_buffer;
struct
{
VkImage src;
VkImageLayout src_layout;
VkImage dst;
VkImageLayout dst_layout;
uint32_t region_count;
const VkImageCopy* regions;
} copy_image;
struct
{
VkBuffer src;
VkImage dst;
VkImageLayout layout;
uint32_t region_count;
const VkBufferImageCopy* regions;
} copy_buffer_to_image;
struct
{
VkImage src;
VkImageLayout layout;
VkBuffer dst;
uint32_t region_count;
const VkBufferImageCopy* regions;
} copy_image_to_buffer;
struct
{
VkPipelineBindPoint bind_point;
VkPipeline pipeline;
} bind_pipeline;
struct
{
VkPipelineBindPoint bind_point;
VkPipelineLayout pipeline_layout;
uint32_t descriptorset_count;
uint32_t descriptorset_offset;
} bind_descriptorsets;
struct
{
VkPipelineLayout pipeline_layout;
VkShaderStageFlags stage_flags;
uint32_t size;
const void* values;
} push_constants;
struct
{
uint32_t group_count_x;
uint32_t group_count_y;
uint32_t group_count_z;
} dispatch;
struct
{
VkPipelineStageFlags src_stage;
VkPipelineStageFlags dst_stage;
uint32_t barrier_count;
const VkMemoryBarrier* barriers;
} memory_barrers;
struct
{
VkPipelineStageFlags src_stage;
VkPipelineStageFlags dst_stage;
uint32_t barrier_count;
const VkBufferMemoryBarrier* barriers;
} buffer_barrers;
struct
{
VkPipelineStageFlags src_stage;
VkPipelineStageFlags dst_stage;
uint32_t barrier_count;
const VkImageMemoryBarrier* barriers;
} image_barrers;
#if NCNN_BENCHMARK
struct
{
uint32_t query;
} write_timestamp;
#endif // NCNN_BENCHMARK
struct
{
uint32_t download_post_buffer_mat_offset;
uint32_t download_post_mat_fp16_offset;
} post_download;
struct
{
uint32_t download_post_mat_fp16_offset;
uint32_t download_post_mat_offset;
} post_cast_float16_to_float32;
};
};
std::vector<record> delayed_records;
#if NCNN_BENCHMARK
uint32_t query_count;
VkQueryPool query_pool;
#endif // NCNN_BENCHMARK
};
class VkTransfer
{
public:
VkTransfer(const VulkanDevice* vkdev);
~VkTransfer();
public:
void record_upload(const Mat& src, VkMat& dst, const Option& opt, bool flatten = true);
void record_upload(const Mat& src, VkImageMat& dst, const Option& opt);
int submit_and_wait();
protected:
int init();
int begin_command_buffer();
int end_command_buffer();
protected:
const VulkanDevice* vkdev;
VkCommandPool compute_command_pool;
VkCommandPool transfer_command_pool;
VkCommandBuffer upload_command_buffer;
VkCommandBuffer compute_command_buffer;
VkSemaphore upload_compute_semaphore;
VkFence upload_command_fence;
VkFence compute_command_fence;
std::vector<VkMat> upload_staging_buffers;
};
} // namespace ncnn
#endif // NCNN_VULKAN
#endif // NCNN_COMMAND_H