Elffuss BraidHub

kikocis/ncnn public

master · 66.0 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 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#ifndef NCNN_MAT_H
16#define NCNN_MAT_H
17
18#include <stdlib.h>
19#include <string.h>
20#if __ARM_NEON
21#include <arm_neon.h>
22#endif
23#if __SSE2__
24#include <emmintrin.h>
25#if __AVX__
26#include <immintrin.h>
27#endif
28#endif
29#if __mips_msa
30#include <msa.h>
31#endif
32#if __loongarch_sx
33#include <lsxintrin.h>
34#endif
35#if __riscv_vector
36#include <riscv_vector.h>
37#include "cpu.h" // cpu_riscv_vlenb()
38#endif
39
40#include "allocator.h"
41#include "option.h"
42#include "platform.h"
43
44#if NCNN_PIXEL
45#if NCNN_PLATFORM_API
46#if __ANDROID_API__ >= 9
47#include <android/bitmap.h>
48#include <jni.h>
49#endif // __ANDROID_API__ >= 9
50#endif // NCNN_PLATFORM_API
51#endif // NCNN_PIXEL
52
53namespace ncnn {
54
55#if NCNN_VULKAN
56class VkMat;
57class VkImageMat;
58#endif // NCNN_VULKAN
59
60// the three dimension matrix
61class NCNN_EXPORT Mat
62{
63public:
64 // empty
65 Mat();
66 // vec
67 Mat(int w, size_t elemsize = 4u, Allocator* allocator = 0);
68 // image
69 Mat(int w, int h, size_t elemsize = 4u, Allocator* allocator = 0);
70 // dim
71 Mat(int w, int h, int c, size_t elemsize = 4u, Allocator* allocator = 0);
72 // cube
73 Mat(int w, int h, int d, int c, size_t elemsize = 4u, Allocator* allocator = 0);
74 // packed vec
75 Mat(int w, size_t elemsize, int elempack, Allocator* allocator = 0);
76 // packed image
77 Mat(int w, int h, size_t elemsize, int elempack, Allocator* allocator = 0);
78 // packed dim
79 Mat(int w, int h, int c, size_t elemsize, int elempack, Allocator* allocator = 0);
80 // packed cube
81 Mat(int w, int h, int d, int c, size_t elemsize, int elempack, Allocator* allocator = 0);
82 // copy
83 Mat(const Mat& m);
84 // external vec
85 Mat(int w, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
86 // external image
87 Mat(int w, int h, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
88 // external dim
89 Mat(int w, int h, int c, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
90 // external cube
91 Mat(int w, int h, int d, int c, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
92 // external packed vec
93 Mat(int w, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
94 // external packed image
95 Mat(int w, int h, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
96 // external packed dim
97 Mat(int w, int h, int c, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
98 // external packed cube
99 Mat(int w, int h, int d, int c, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
100 // release
101 ~Mat();
102 // assign
103 Mat& operator=(const Mat& m);
104 // set all
105 void fill(float v);
106 void fill(int v);
107#if __ARM_NEON
108 void fill(float32x4_t _v);
109 void fill(uint16x4_t _v);
110#if !defined(_MSC_VER)
111 void fill(int32x4_t _v);
112#endif
113 void fill(int32x4_t _v0, int32x4_t _v1);
114#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
115#if !defined(_MSC_VER)
116 void fill(float16x4_t _v);
117 void fill(float16x8_t _v);
118#endif
119#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
120#endif // __ARM_NEON
121#if __SSE2__
122#if __AVX__
123#if __AVX512F__
124 void fill(__m512 _v);
125#endif // __AVX512F__
126 void fill(__m256 _v, int i = 0);
127#endif // __AVX__
128 void fill(__m128 _v);
129 void fill(__m128i _v);
130#endif // __SSE2__
131#if __mips_msa
132 void fill(v4f32 _v);
133#endif // __mips_msa
134#if __loongarch_sx
135 void fill(__m128 _v);
136#endif //__loongarch_sx
137#if __riscv_vector
138 void fill(vfloat32m1_t _v);
139 void fill(vuint16m1_t _v);
140 void fill(vint8m1_t _v);
141#if __riscv_zfh
142 void fill(vfloat16m1_t _v);
143#endif // __riscv_zfh
144#endif // __riscv_vector
145 template<typename T>
146 void fill(T v);
147 // deep copy
148 Mat clone(Allocator* allocator = 0) const;
149 // deep copy from other mat, inplace
150 void clone_from(const ncnn::Mat& mat, Allocator* allocator = 0);
151 // reshape vec
152 Mat reshape(int w, Allocator* allocator = 0) const;
153 // reshape image
154 Mat reshape(int w, int h, Allocator* allocator = 0) const;
155 // reshape dim
156 Mat reshape(int w, int h, int c, Allocator* allocator = 0) const;
157 // reshape cube
158 Mat reshape(int w, int h, int d, int c, Allocator* allocator = 0) const;
159 // allocate vec
160 void create(int w, size_t elemsize = 4u, Allocator* allocator = 0);
161 // allocate image
162 void create(int w, int h, size_t elemsize = 4u, Allocator* allocator = 0);
163 // allocate dim
164 void create(int w, int h, int c, size_t elemsize = 4u, Allocator* allocator = 0);
165 // allocate cube
166 void create(int w, int h, int d, int c, size_t elemsize = 4u, Allocator* allocator = 0);
167 // allocate packed vec
168 void create(int w, size_t elemsize, int elempack, Allocator* allocator = 0);
169 // allocate packed image
170 void create(int w, int h, size_t elemsize, int elempack, Allocator* allocator = 0);
171 // allocate packed dim
172 void create(int w, int h, int c, size_t elemsize, int elempack, Allocator* allocator = 0);
173 // allocate packed cube
174 void create(int w, int h, int d, int c, size_t elemsize, int elempack, Allocator* allocator = 0);
175 // allocate like
176 void create_like(const Mat& m, Allocator* allocator = 0);
177#if NCNN_VULKAN
178 // allocate like
179 void create_like(const VkMat& m, Allocator* allocator = 0);
180 // allocate like
181 void create_like(const VkImageMat& im, Allocator* allocator = 0);
182#endif // NCNN_VULKAN
183 // refcount++
184 void addref();
185 // refcount--
186 void release();
187
188 bool empty() const;
189 size_t total() const;
190
191 // bits per element
192 int elembits() const;
193
194 // shape only
195 Mat shape() const;
196
197 // data reference
198 Mat channel(int c);
199 const Mat channel(int c) const;
200 Mat depth(int z);
201 const Mat depth(int z) const;
202 float* row(int y);
203 const float* row(int y) const;
204 template<typename T>
205 T* row(int y);
206 template<typename T>
207 const T* row(int y) const;
208
209 // range reference
210 Mat channel_range(int c, int channels);
211 const Mat channel_range(int c, int channels) const;
212 Mat depth_range(int z, int depths);
213 const Mat depth_range(int z, int depths) const;
214 Mat row_range(int y, int rows);
215 const Mat row_range(int y, int rows) const;
216 Mat range(int x, int n);
217 const Mat range(int x, int n) const;
218
219 // access raw data
220 template<typename T>
221 operator T*();
222 template<typename T>
223 operator const T*() const;
224
225 // convenient access float vec element
226 float& operator[](size_t i);
227 const float& operator[](size_t i) const;
228
229#if NCNN_PIXEL
230 enum PixelType
231 {
232 PIXEL_CONVERT_SHIFT = 16,
233 PIXEL_FORMAT_MASK = 0x0000ffff,
234 PIXEL_CONVERT_MASK = 0xffff0000,
235
236 PIXEL_RGB = 1,
237 PIXEL_BGR = 2,
238 PIXEL_GRAY = 3,
239 PIXEL_RGBA = 4,
240 PIXEL_BGRA = 5,
241
242 PIXEL_RGB2BGR = PIXEL_RGB | (PIXEL_BGR << PIXEL_CONVERT_SHIFT),
243 PIXEL_RGB2GRAY = PIXEL_RGB | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT),
244 PIXEL_RGB2RGBA = PIXEL_RGB | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT),
245 PIXEL_RGB2BGRA = PIXEL_RGB | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT),
246
247 PIXEL_BGR2RGB = PIXEL_BGR | (PIXEL_RGB << PIXEL_CONVERT_SHIFT),
248 PIXEL_BGR2GRAY = PIXEL_BGR | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT),
249 PIXEL_BGR2RGBA = PIXEL_BGR | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT),
250 PIXEL_BGR2BGRA = PIXEL_BGR | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT),
251
252 PIXEL_GRAY2RGB = PIXEL_GRAY | (PIXEL_RGB << PIXEL_CONVERT_SHIFT),
253 PIXEL_GRAY2BGR = PIXEL_GRAY | (PIXEL_BGR << PIXEL_CONVERT_SHIFT),
254 PIXEL_GRAY2RGBA = PIXEL_GRAY | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT),
255 PIXEL_GRAY2BGRA = PIXEL_GRAY | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT),
256
257 PIXEL_RGBA2RGB = PIXEL_RGBA | (PIXEL_RGB << PIXEL_CONVERT_SHIFT),
258 PIXEL_RGBA2BGR = PIXEL_RGBA | (PIXEL_BGR << PIXEL_CONVERT_SHIFT),
259 PIXEL_RGBA2GRAY = PIXEL_RGBA | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT),
260 PIXEL_RGBA2BGRA = PIXEL_RGBA | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT),
261
262 PIXEL_BGRA2RGB = PIXEL_BGRA | (PIXEL_RGB << PIXEL_CONVERT_SHIFT),
263 PIXEL_BGRA2BGR = PIXEL_BGRA | (PIXEL_BGR << PIXEL_CONVERT_SHIFT),
264 PIXEL_BGRA2GRAY = PIXEL_BGRA | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT),
265 PIXEL_BGRA2RGBA = PIXEL_BGRA | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT),
266 };
267 // convenient construct from pixel data
268 static Mat from_pixels(const unsigned char* pixels, int type, int w, int h, Allocator* allocator = 0);
269 // convenient construct from pixel data with stride(bytes-per-row) parameter
270 static Mat from_pixels(const unsigned char* pixels, int type, int w, int h, int stride, Allocator* allocator = 0);
271 // convenient construct from pixel data and resize to specific size
272 static Mat from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int target_width, int target_height, Allocator* allocator = 0);
273 // convenient construct from pixel data and resize to specific size with stride(bytes-per-row) parameter
274 static Mat from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int stride, int target_width, int target_height, Allocator* allocator = 0);
275 // convenient construct from pixel data roi
276 static Mat from_pixels_roi(const unsigned char* pixels, int type, int w, int h, int roix, int roiy, int roiw, int roih, Allocator* allocator = 0);
277 // convenient construct from pixel data roi with stride(bytes-per-row) parameter
278 static Mat from_pixels_roi(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, Allocator* allocator = 0);
279 // convenient construct from pixel data roi and resize to specific size
280 static Mat from_pixels_roi_resize(const unsigned char* pixels, int type, int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height, Allocator* allocator = 0);
281 // convenient construct from pixel data roi and resize to specific size with stride(bytes-per-row) parameter
282 static Mat from_pixels_roi_resize(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, int target_width, int target_height, Allocator* allocator = 0);
283
284 // convenient export to pixel data
285 void to_pixels(unsigned char* pixels, int type) const;
286 // convenient export to pixel data with stride(bytes-per-row) parameter
287 void to_pixels(unsigned char* pixels, int type, int stride) const;
288 // convenient export to pixel data and resize to specific size
289 void to_pixels_resize(unsigned char* pixels, int type, int target_width, int target_height) const;
290 // convenient export to pixel data and resize to specific size with stride(bytes-per-row) parameter
291 void to_pixels_resize(unsigned char* pixels, int type, int target_width, int target_height, int target_stride) const;
292
293#if NCNN_PLATFORM_API
294#if __ANDROID_API__ >= 9
295 // convenient construct from android Bitmap
296 static Mat from_android_bitmap(JNIEnv* env, jobject bitmap, int type_to, Allocator* allocator = 0);
297 // convenient construct from android Bitmap and resize to specific size
298 static Mat from_android_bitmap_resize(JNIEnv* env, jobject bitmap, int type_to, int target_width, int target_height, Allocator* allocator = 0);
299 // convenient construct from android Bitmap roi
300 static Mat from_android_bitmap_roi(JNIEnv* env, jobject bitmap, int type_to, int roix, int roiy, int roiw, int roih, Allocator* allocator = 0);
301 // convenient construct from android Bitmap roi and resize to specific size
302 static Mat from_android_bitmap_roi_resize(JNIEnv* env, jobject bitmap, int type_to, int roix, int roiy, int roiw, int roih, int target_width, int target_height, Allocator* allocator = 0);
303 // convenient export to android Bitmap and resize to the android Bitmap size
304 void to_android_bitmap(JNIEnv* env, jobject bitmap, int type_from) const;
305#endif // __ANDROID_API__ >= 9
306#endif // NCNN_PLATFORM_API
307#endif // NCNN_PIXEL
308
309 // substract channel-wise mean values, then multiply by normalize values, pass 0 to skip
310 void substract_mean_normalize(const float* mean_vals, const float* norm_vals);
311
312 // convenient construct from half precision floating point data
313 static Mat from_float16(const unsigned short* data, int size);
314
315 // pointer to the data
316 void* data;
317
318 // pointer to the reference counter
319 // when points to user-allocated data, the pointer is NULL
320 int* refcount;
321
322 // element size in bytes
323 // 4 = float32/int32
324 // 2 = float16
325 // 1 = int8/uint8
326 // 0 = empty
327 size_t elemsize;
328
329 // packed count inside element
330 // c/1-d-h-w-1 c/1-h-w-1 h/1-w-1 w/1-1 scalar
331 // c/4-d-h-w-4 c/4-h-w-4 h/4-w-4 w/4-4 sse/neon
332 // c/8-d-h-w-8 c/8-h-w-8 h/8-w-8 w/8-8 avx/fp16
333 int elempack;
334
335 // the allocator
336 Allocator* allocator;
337
338 // the dimension rank
339 int dims;
340
341 int w;
342 int h;
343 int d;
344 int c;
345
346 size_t cstep;
347};
348
349#if NCNN_VULKAN
350
351// the three dimension matrix, vulkan version
352class NCNN_EXPORT VkMat
353{
354public:
355 // empty
356 VkMat();
357 // vec
358 VkMat(int w, size_t elemsize, VkAllocator* allocator);
359 // image
360 VkMat(int w, int h, size_t elemsize, VkAllocator* allocator);
361 // dim
362 VkMat(int w, int h, int c, size_t elemsize, VkAllocator* allocator);
363 // cube
364 VkMat(int w, int h, int d, int c, size_t elemsize, VkAllocator* allocator);
365 // packed vec
366 VkMat(int w, size_t elemsize, int elempack, VkAllocator* allocator);
367 // packed image
368 VkMat(int w, int h, size_t elemsize, int elempack, VkAllocator* allocator);
369 // packed dim
370 VkMat(int w, int h, int c, size_t elemsize, int elempack, VkAllocator* allocator);
371 // packed cube
372 VkMat(int w, int h, int d, int c, size_t elemsize, int elempack, VkAllocator* allocator);
373 // copy
374 VkMat(const VkMat& m);
375 // external vec
376 VkMat(int w, VkBufferMemory* data, size_t elemsize, VkAllocator* allocator);
377 // external image
378 VkMat(int w, int h, VkBufferMemory* data, size_t elemsize, VkAllocator* allocator);
379 // external dim
380 VkMat(int w, int h, int c, VkBufferMemory* data, size_t elemsize, VkAllocator* allocator);
381 // external cube
382 VkMat(int w, int h, int d, int c, VkBufferMemory* data, size_t elemsize, VkAllocator* allocator);
383 // external packed vec
384 VkMat(int w, VkBufferMemory* data, size_t elemsize, int elempack, VkAllocator* allocator);
385 // external packed image
386 VkMat(int w, int h, VkBufferMemory* data, size_t elemsize, int elempack, VkAllocator* allocator);
387 // external packed dim
388 VkMat(int w, int h, int c, VkBufferMemory* data, size_t elemsize, int elempack, VkAllocator* allocator);
389 // external packed cube
390 VkMat(int w, int h, int d, int c, VkBufferMemory* data, size_t elemsize, int elempack, VkAllocator* allocator);
391 // release
392 ~VkMat();
393 // assign
394 VkMat& operator=(const VkMat& m);
395 // allocate vec
396 void create(int w, size_t elemsize, VkAllocator* allocator);
397 // allocate image
398 void create(int w, int h, size_t elemsize, VkAllocator* allocator);
399 // allocate dim
400 void create(int w, int h, int c, size_t elemsize, VkAllocator* allocator);
401 // allocate cube
402 void create(int w, int h, int d, int c, size_t elemsize, VkAllocator* allocator);
403 // allocate packed vec
404 void create(int w, size_t elemsize, int elempack, VkAllocator* allocator);
405 // allocate packed image
406 void create(int w, int h, size_t elemsize, int elempack, VkAllocator* allocator);
407 // allocate packed dim
408 void create(int w, int h, int c, size_t elemsize, int elempack, VkAllocator* allocator);
409 // allocate packed cube
410 void create(int w, int h, int d, int c, size_t elemsize, int elempack, VkAllocator* allocator);
411 // allocate like
412 void create_like(const Mat& m, VkAllocator* allocator);
413 // allocate like
414 void create_like(const VkMat& m, VkAllocator* allocator);
415 // allocate like
416 void create_like(const VkImageMat& im, VkAllocator* allocator);
417
418 // mapped
419 Mat mapped() const;
420 void* mapped_ptr() const;
421
422 // refcount++
423 void addref();
424 // refcount--
425 void release();
426
427 bool empty() const;
428 size_t total() const;
429
430 // bits per element
431 int elembits() const;
432
433 // shape only
434 Mat shape() const;
435
436 // low-level reference
437 VkBuffer buffer() const;
438 size_t buffer_offset() const;
439 size_t buffer_capacity() const;
440
441 // device buffer
442 VkBufferMemory* data;
443
444 // pointer to the reference counter
445 // when points to user-allocated data, the pointer is NULL
446 int* refcount;
447
448 // element size in bytes
449 // 4 = float32/int32
450 // 2 = float16
451 // 1 = int8/uint8
452 // 0 = empty
453 size_t elemsize;
454
455 // packed count inside element
456 // c/1-d-h-w-1 c/1-h-w-1 h/1-w-1 w/1-1 scalar
457 // c/4-d-h-w-4 c/4-h-w-4 h/4-w-4 w/4-4 sse/neon
458 // c/8-d-h-w-8 c/8-h-w-8 h/8-w-8 w/8-8 avx/fp16
459 int elempack;
460
461 // the allocator
462 VkAllocator* allocator;
463
464 // the dimension rank
465 int dims;
466
467 int w;
468 int h;
469 int d;
470 int c;
471
472 size_t cstep;
473};
474
475class NCNN_EXPORT VkImageMat
476{
477public:
478 // empty
479 VkImageMat();
480 // vec
481 VkImageMat(int w, size_t elemsize, VkAllocator* allocator);
482 // image
483 VkImageMat(int w, int h, size_t elemsize, VkAllocator* allocator);
484 // dim
485 VkImageMat(int w, int h, int c, size_t elemsize, VkAllocator* allocator);
486 // cube
487 VkImageMat(int w, int h, int d, int c, size_t elemsize, VkAllocator* allocator);
488 // packed vec
489 VkImageMat(int w, size_t elemsize, int elempack, VkAllocator* allocator);
490 // packed image
491 VkImageMat(int w, int h, size_t elemsize, int elempack, VkAllocator* allocator);
492 // packed dim
493 VkImageMat(int w, int h, int c, size_t elemsize, int elempack, VkAllocator* allocator);
494 // packed cube
495 VkImageMat(int w, int h, int d, int c, size_t elemsize, int elempack, VkAllocator* allocator);
496 // copy
497 VkImageMat(const VkImageMat& m);
498 // external vec
499 VkImageMat(int w, VkImageMemory* data, size_t elemsize, VkAllocator* allocator);
500 // external image
501 VkImageMat(int w, int h, VkImageMemory* data, size_t elemsize, VkAllocator* allocator);
502 // external dim
503 VkImageMat(int w, int h, int c, VkImageMemory* data, size_t elemsize, VkAllocator* allocator);
504 // external cube
505 VkImageMat(int w, int h, int d, int c, VkImageMemory* data, size_t elemsize, VkAllocator* allocator);
506 // external packed vec
507 VkImageMat(int w, VkImageMemory* data, size_t elemsize, int elempack, VkAllocator* allocator);
508 // external packed image
509 VkImageMat(int w, int h, VkImageMemory* data, size_t elemsize, int elempack, VkAllocator* allocator);
510 // external packed dim
511 VkImageMat(int w, int h, int c, VkImageMemory* data, size_t elemsize, int elempack, VkAllocator* allocator);
512 // external packed cube
513 VkImageMat(int w, int h, int d, int c, VkImageMemory* data, size_t elemsize, int elempack, VkAllocator* allocator);
514 // release
515 ~VkImageMat();
516 // assign
517 VkImageMat& operator=(const VkImageMat& m);
518 // allocate vec
519 void create(int w, size_t elemsize, VkAllocator* allocator);
520 // allocate image
521 void create(int w, int h, size_t elemsize, VkAllocator* allocator);
522 // allocate dim
523 void create(int w, int h, int c, size_t elemsize, VkAllocator* allocator);
524 // allocate cube
525 void create(int w, int h, int d, int c, size_t elemsize, VkAllocator* allocator);
526 // allocate packed vec
527 void create(int w, size_t elemsize, int elempack, VkAllocator* allocator);
528 // allocate packed image
529 void create(int w, int h, size_t elemsize, int elempack, VkAllocator* allocator);
530 // allocate packed dim
531 void create(int w, int h, int c, size_t elemsize, int elempack, VkAllocator* allocator);
532 // allocate packed cube
533 void create(int w, int h, int d, int c, size_t elemsize, int elempack, VkAllocator* allocator);
534 // allocate like
535 void create_like(const Mat& m, VkAllocator* allocator);
536 // allocate like
537 void create_like(const VkMat& m, VkAllocator* allocator);
538 // allocate like
539 void create_like(const VkImageMat& im, VkAllocator* allocator);
540
541 // mapped
542 Mat mapped() const;
543 void* mapped_ptr() const;
544
545 // refcount++
546 void addref();
547 // refcount--
548 void release();
549
550 bool empty() const;
551 size_t total() const;
552
553 // bits per element
554 int elembits() const;
555
556 // shape only
557 Mat shape() const;
558
559 // low-level reference
560 VkImage image() const;
561 VkImageView imageview() const;
562
563#if NCNN_PLATFORM_API
564#if __ANDROID_API__ >= 26
565 // convenient construct from android hardware buffer
566 static VkImageMat from_android_hardware_buffer(VkAndroidHardwareBufferImageAllocator* allocator);
567#endif // __ANDROID_API__ >= 26
568#endif // NCNN_PLATFORM_API
569
570 // device image
571 VkImageMemory* data;
572
573 // pointer to the reference counter
574 // when points to user-allocated data, the pointer is NULL
575 int* refcount;
576
577 // element size in bytes
578 // 4 = float32/int32
579 // 2 = float16
580 // 1 = int8/uint8
581 // 0 = empty
582 size_t elemsize;
583
584 // packed count inside element
585 // c/1-d-h-w-1 c/1-h-w-1 h/1-w-1 w/1-1 scalar
586 // c/4-d-h-w-4 c/4-h-w-4 h/4-w-4 w/4-4 sse/neon
587 // c/8-d-h-w-8 c/8-h-w-8 h/8-w-8 w/8-8 avx/fp16
588 int elempack;
589
590 // the allocator
591 VkAllocator* allocator;
592
593 // the dimension rank
594 int dims;
595
596 int w;
597 int h;
598 int d;
599 int c;
600};
601
602// type for vulkan specialization constant and push constant
603union vk_specialization_type
604{
605 int i;
606 float f;
607 uint32_t u32;
608};
609union vk_constant_type
610{
611 int i;
612 float f;
613};
614#endif // NCNN_VULKAN
615
616// misc function
617#if NCNN_PIXEL
618// convert yuv420sp(nv21) to rgb, the fast approximate version
619NCNN_EXPORT void yuv420sp2rgb(const unsigned char* yuv420sp, int w, int h, unsigned char* rgb);
620// convert yuv420sp(nv12) to rgb, the fast approximate version
621NCNN_EXPORT void yuv420sp2rgb_nv12(const unsigned char* yuv420sp, int w, int h, unsigned char* rgb);
622// convert yuv420sp(nv21) to rgb with half resize, the faster approximate version
623NCNN_EXPORT void yuv420sp2rgb_half(const unsigned char* yuv420sp, int w, int h, unsigned char* rgb);
624// image pixel bilinear resize
625NCNN_EXPORT void resize_bilinear_c1(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h);
626NCNN_EXPORT void resize_bilinear_c2(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h);
627NCNN_EXPORT void resize_bilinear_c3(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h);
628NCNN_EXPORT void resize_bilinear_c4(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h);
629// image pixel bilinear resize with stride(bytes-per-row) parameter
630NCNN_EXPORT void resize_bilinear_c1(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride);
631NCNN_EXPORT void resize_bilinear_c2(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride);
632NCNN_EXPORT void resize_bilinear_c3(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride);
633NCNN_EXPORT void resize_bilinear_c4(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride);
634// image pixel bilinear resize, convenient wrapper for yuv420sp(nv21/nv12)
635NCNN_EXPORT void resize_bilinear_yuv420sp(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h);
636#endif // NCNN_PIXEL
637#if NCNN_PIXEL_ROTATE
638// type is the from type, 6 means rotating from 6 to 1
639//
640// 1 2 3 4 5 6 7 8
641//
642// 888888 888888 88 88 8888888888 88 88 8888888888
643// 88 88 88 88 88 88 88 88 88 88 88 88
644// 8888 8888 8888 8888 88 8888888888 8888888888 88
645// 88 88 88 88
646// 88 88 888888 888888
647//
648// ref http://sylvana.net/jpegcrop/exif_orientation.html
649// image pixel kanna rotate
650NCNN_EXPORT void kanna_rotate_c1(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type);
651NCNN_EXPORT void kanna_rotate_c2(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type);
652NCNN_EXPORT void kanna_rotate_c3(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type);
653NCNN_EXPORT void kanna_rotate_c4(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type);
654// image pixel kanna rotate with stride(bytes-per-row) parameter
655NCNN_EXPORT void kanna_rotate_c1(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type);
656NCNN_EXPORT void kanna_rotate_c2(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type);
657NCNN_EXPORT void kanna_rotate_c3(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type);
658NCNN_EXPORT void kanna_rotate_c4(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type);
659// image pixel kanna rotate, convenient wrapper for yuv420sp(nv21/nv12)
660NCNN_EXPORT void kanna_rotate_yuv420sp(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type);
661#endif // NCNN_PIXEL_ROTATE
662#if NCNN_PIXEL_AFFINE
663// resolve affine transform matrix from rotation angle, scale factor and x y offset
664NCNN_EXPORT void get_rotation_matrix(float angle, float scale, float dx, float dy, float* tm);
665// resolve affine transform matrix from two set of points, num_point must be >= 2
666NCNN_EXPORT void get_affine_transform(const float* points_from, const float* points_to, int num_point, float* tm);
667// resolve the inversion affine transform matrix
668NCNN_EXPORT void invert_affine_transform(const float* tm, float* tm_inv);
669// image pixel bilinear warpaffine inverse transform, set -233 for transparent border color, the color RGBA is little-endian encoded
670NCNN_EXPORT void warpaffine_bilinear_c1(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, const float* tm, int type = 0, unsigned int v = 0);
671NCNN_EXPORT void warpaffine_bilinear_c2(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, const float* tm, int type = 0, unsigned int v = 0);
672NCNN_EXPORT void warpaffine_bilinear_c3(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, const float* tm, int type = 0, unsigned int v = 0);
673NCNN_EXPORT void warpaffine_bilinear_c4(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, const float* tm, int type = 0, unsigned int v = 0);
674// image pixel bilinear warpaffine inverse transform with stride(bytes-per-row) parameter, set -233 for transparent border color, the color RGBA is little-endian encoded
675NCNN_EXPORT void warpaffine_bilinear_c1(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, const float* tm, int type = 0, unsigned int v = 0);
676NCNN_EXPORT void warpaffine_bilinear_c2(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, const float* tm, int type = 0, unsigned int v = 0);
677NCNN_EXPORT void warpaffine_bilinear_c3(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, const float* tm, int type = 0, unsigned int v = 0);
678NCNN_EXPORT void warpaffine_bilinear_c4(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, const float* tm, int type = 0, unsigned int v = 0);
679// image pixel bilinear warpaffine, convenient wrapper for yuv420sp(nv21/nv12), set -233 for transparent border color, the color YUV_ is little-endian encoded
680NCNN_EXPORT void warpaffine_bilinear_yuv420sp(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, const float* tm, int type = 0, unsigned int v = 0);
681#endif // NCNN_PIXEL_AFFINE
682#if NCNN_PIXEL_DRAWING
683// draw rectangle, set thickness -1 for filled rectangle, the color RGBA is little-endian encoded
684NCNN_EXPORT void draw_rectangle_c1(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness);
685NCNN_EXPORT void draw_rectangle_c2(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness);
686NCNN_EXPORT void draw_rectangle_c3(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness);
687NCNN_EXPORT void draw_rectangle_c4(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness);
688// draw rectangle with stride(bytes-per-row) parameter, set thickness -1 for filled rectangle, the color RGBA is little-endian encoded
689NCNN_EXPORT void draw_rectangle_c1(unsigned char* pixels, int w, int h, int stride, int rx, int ry, int rw, int rh, unsigned int color, int thickness);
690NCNN_EXPORT void draw_rectangle_c2(unsigned char* pixels, int w, int h, int stride, int rx, int ry, int rw, int rh, unsigned int color, int thickness);
691NCNN_EXPORT void draw_rectangle_c3(unsigned char* pixels, int w, int h, int stride, int rx, int ry, int rw, int rh, unsigned int color, int thickness);
692NCNN_EXPORT void draw_rectangle_c4(unsigned char* pixels, int w, int h, int stride, int rx, int ry, int rw, int rh, unsigned int color, int thickness);
693// draw rectangle, convenient wrapper for yuv420sp(nv21/nv12), set thickness -1 for filled rectangle, the color YUV_ is little-endian encoded
694NCNN_EXPORT void draw_rectangle_yuv420sp(unsigned char* yuv420sp, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness);
695// draw circle, set thickness -1 for filled circle, the color RGBA is little-endian encoded
696NCNN_EXPORT void draw_circle_c1(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness);
697NCNN_EXPORT void draw_circle_c2(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness);
698NCNN_EXPORT void draw_circle_c3(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness);
699NCNN_EXPORT void draw_circle_c4(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness);
700// draw circle with stride(bytes-per-row) parameter, set thickness -1 for filled circle, the color RGBA is little-endian encoded
701NCNN_EXPORT void draw_circle_c1(unsigned char* pixels, int w, int h, int stride, int cx, int cy, int radius, unsigned int color, int thickness);
702NCNN_EXPORT void draw_circle_c2(unsigned char* pixels, int w, int h, int stride, int cx, int cy, int radius, unsigned int color, int thickness);
703NCNN_EXPORT void draw_circle_c3(unsigned char* pixels, int w, int h, int stride, int cx, int cy, int radius, unsigned int color, int thickness);
704NCNN_EXPORT void draw_circle_c4(unsigned char* pixels, int w, int h, int stride, int cx, int cy, int radius, unsigned int color, int thickness);
705// draw circle, convenient wrapper for yuv420sp(nv21/nv12), set thickness -1 for filled circle, the color YUV_ is little-endian encoded
706NCNN_EXPORT void draw_circle_yuv420sp(unsigned char* yuv420sp, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness);
707// draw line, the color RGBA is little-endian encoded
708NCNN_EXPORT void draw_line_c1(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness);
709NCNN_EXPORT void draw_line_c2(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness);
710NCNN_EXPORT void draw_line_c3(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness);
711NCNN_EXPORT void draw_line_c4(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness);
712// draw line with stride(bytes-per-row) parameter, the color RGBA is little-endian encoded
713NCNN_EXPORT void draw_line_c1(unsigned char* pixels, int w, int h, int stride, int x0, int y0, int x1, int y1, unsigned int color, int thickness);
714NCNN_EXPORT void draw_line_c2(unsigned char* pixels, int w, int h, int stride, int x0, int y0, int x1, int y1, unsigned int color, int thickness);
715NCNN_EXPORT void draw_line_c3(unsigned char* pixels, int w, int h, int stride, int x0, int y0, int x1, int y1, unsigned int color, int thickness);
716NCNN_EXPORT void draw_line_c4(unsigned char* pixels, int w, int h, int stride, int x0, int y0, int x1, int y1, unsigned int color, int thickness);
717// draw line, convenient wrapper for yuv420sp(nv21/nv12), the color YUV_ is little-endian encoded
718NCNN_EXPORT void draw_line_yuv420sp(unsigned char* yuv420sp, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness);
719// resolve text bounding box size
720NCNN_EXPORT void get_text_drawing_size(const char* text, int fontpixelsize, int* w, int* h);
721// draw ascii printables and newline, the color RGBA is little-endian encoded
722NCNN_EXPORT void draw_text_c1(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color);
723NCNN_EXPORT void draw_text_c2(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color);
724NCNN_EXPORT void draw_text_c3(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color);
725NCNN_EXPORT void draw_text_c4(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color);
726// draw ascii printables and newline with stride(bytes-per-row) parameter, the color RGBA is little-endian encoded
727NCNN_EXPORT void draw_text_c1(unsigned char* pixels, int w, int h, int stride, const char* text, int x, int y, int fontpixelsize, unsigned int color);
728NCNN_EXPORT void draw_text_c2(unsigned char* pixels, int w, int h, int stride, const char* text, int x, int y, int fontpixelsize, unsigned int color);
729NCNN_EXPORT void draw_text_c3(unsigned char* pixels, int w, int h, int stride, const char* text, int x, int y, int fontpixelsize, unsigned int color);
730NCNN_EXPORT void draw_text_c4(unsigned char* pixels, int w, int h, int stride, const char* text, int x, int y, int fontpixelsize, unsigned int color);
731// draw ascii printables and newline, convenient wrapper for yuv420sp(nv21/nv12), the color YUV_ is little-endian encoded
732NCNN_EXPORT void draw_text_yuv420sp(unsigned char* yuv420sp, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color);
733#endif // NCNN_PIXEL_DRAWING
734
735// type conversion
736// convert float to half precision floating point
737NCNN_EXPORT unsigned short float32_to_float16(float value);
738// convert half precision floating point to float
739NCNN_EXPORT float float16_to_float32(unsigned short value);
740// convert float to brain half
741NCNN_EXPORT NCNN_FORCEINLINE unsigned short float32_to_bfloat16(float value)
742{
743 // 16 : 16
744 union
745 {
746 unsigned int u;
747 float f;
748 } tmp;
749 tmp.f = value;
750 return tmp.u >> 16;
751}
752// convert brain half to float
753NCNN_EXPORT NCNN_FORCEINLINE float bfloat16_to_float32(unsigned short value)
754{
755 // 16 : 16
756 union
757 {
758 unsigned int u;
759 float f;
760 } tmp;
761 tmp.u = value << 16;
762 return tmp.f;
763}
764
765// mat process
766enum BorderType
767{
768 BORDER_CONSTANT = 0,
769 BORDER_REPLICATE = 1,
770 BORDER_REFLECT = 2,
771 BORDER_TRANSPARENT = -233,
772};
773NCNN_EXPORT void copy_make_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, int type, float v, const Option& opt = Option());
774NCNN_EXPORT void copy_make_border_3d(const Mat& src, Mat& dst, int top, int bottom, int left, int right, int front, int behind, int type, float v, const Option& opt = Option());
775NCNN_EXPORT void copy_cut_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, const Option& opt = Option());
776NCNN_EXPORT void copy_cut_border_3d(const Mat& src, Mat& dst, int top, int bottom, int left, int right, int front, int behind, const Option& opt = Option());
777NCNN_EXPORT void resize_nearest(const Mat& src, Mat& dst, int w, int h, const Option& opt = Option());
778NCNN_EXPORT void resize_bilinear(const Mat& src, Mat& dst, int w, int h, const Option& opt = Option());
779NCNN_EXPORT void resize_bicubic(const Mat& src, Mat& dst, int w, int h, const Option& opt = Option());
780NCNN_EXPORT void convert_packing(const Mat& src, Mat& dst, int elempack, const Option& opt = Option());
781NCNN_EXPORT void flatten(const Mat& src, Mat& dst, const Option& opt = Option());
782NCNN_EXPORT void cast_float32_to_float16(const Mat& src, Mat& dst, const Option& opt = Option());
783NCNN_EXPORT void cast_float16_to_float32(const Mat& src, Mat& dst, const Option& opt = Option());
784NCNN_EXPORT void cast_int8_to_float32(const Mat& src, Mat& dst, const Option& opt = Option());
785NCNN_EXPORT void cast_float32_to_bfloat16(const Mat& src, Mat& dst, const Option& opt = Option());
786NCNN_EXPORT void cast_bfloat16_to_float32(const Mat& src, Mat& dst, const Option& opt = Option());
787NCNN_EXPORT void quantize_to_int8(const Mat& src, Mat& dst, const Mat& scale_data, const Option& opt = Option());
788NCNN_EXPORT void dequantize_from_int32(const Mat& src, Mat& dst, const Mat& scale_data, const Mat& bias_data, const Option& opt = Option());
789NCNN_EXPORT void requantize_from_int32_to_int8(const Mat& src, Mat& dst, const Mat& scale_in_data, const Mat& scale_out_data, const Mat& bias_data, int activation_type, const Mat& activation_params, const Option& opt = Option());
790
791NCNN_FORCEINLINE Mat::Mat()
792 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
793{
794}
795
796NCNN_FORCEINLINE Mat::Mat(int _w, size_t _elemsize, Allocator* _allocator)
797 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
798{
799 create(_w, _elemsize, _allocator);
800}
801
802NCNN_FORCEINLINE Mat::Mat(int _w, int _h, size_t _elemsize, Allocator* _allocator)
803 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
804{
805 create(_w, _h, _elemsize, _allocator);
806}
807
808NCNN_FORCEINLINE Mat::Mat(int _w, int _h, int _c, size_t _elemsize, Allocator* _allocator)
809 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
810{
811 create(_w, _h, _c, _elemsize, _allocator);
812}
813
814NCNN_FORCEINLINE Mat::Mat(int _w, int _h, int _d, int _c, size_t _elemsize, Allocator* _allocator)
815 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
816{
817 create(_w, _h, _d, _c, _elemsize, _allocator);
818}
819
820NCNN_FORCEINLINE Mat::Mat(int _w, size_t _elemsize, int _elempack, Allocator* _allocator)
821 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
822{
823 create(_w, _elemsize, _elempack, _allocator);
824}
825
826NCNN_FORCEINLINE Mat::Mat(int _w, int _h, size_t _elemsize, int _elempack, Allocator* _allocator)
827 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
828{
829 create(_w, _h, _elemsize, _elempack, _allocator);
830}
831
832NCNN_FORCEINLINE Mat::Mat(int _w, int _h, int _c, size_t _elemsize, int _elempack, Allocator* _allocator)
833 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
834{
835 create(_w, _h, _c, _elemsize, _elempack, _allocator);
836}
837
838NCNN_FORCEINLINE Mat::Mat(int _w, int _h, int _d, int _c, size_t _elemsize, int _elempack, Allocator* _allocator)
839 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
840{
841 create(_w, _h, _d, _c, _elemsize, _elempack, _allocator);
842}
843
844NCNN_FORCEINLINE Mat::Mat(const Mat& m)
845 : data(m.data), refcount(m.refcount), elemsize(m.elemsize), elempack(m.elempack), allocator(m.allocator), dims(m.dims), w(m.w), h(m.h), d(m.d), c(m.c), cstep(m.cstep)
846{
847 addref();
848}
849
850NCNN_FORCEINLINE Mat::Mat(int _w, void* _data, size_t _elemsize, Allocator* _allocator)
851 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(1), w(_w), h(1), d(1), c(1)
852{
853 cstep = w;
854}
855
856NCNN_FORCEINLINE Mat::Mat(int _w, int _h, void* _data, size_t _elemsize, Allocator* _allocator)
857 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(2), w(_w), h(_h), d(1), c(1)
858{
859 cstep = (size_t)w * h;
860}
861
862NCNN_FORCEINLINE Mat::Mat(int _w, int _h, int _c, void* _data, size_t _elemsize, Allocator* _allocator)
863 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(3), w(_w), h(_h), d(1), c(_c)
864{
865 cstep = alignSize((size_t)w * h * elemsize, 16) / elemsize;
866}
867
868NCNN_FORCEINLINE Mat::Mat(int _w, int _h, int _d, int _c, void* _data, size_t _elemsize, Allocator* _allocator)
869 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(4), w(_w), h(_h), d(_d), c(_c)
870{
871 cstep = alignSize((size_t)w * h * d * elemsize, 16) / elemsize;
872}
873
874NCNN_FORCEINLINE Mat::Mat(int _w, void* _data, size_t _elemsize, int _elempack, Allocator* _allocator)
875 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(1), w(_w), h(1), d(1), c(1)
876{
877 cstep = w;
878}
879
880NCNN_FORCEINLINE Mat::Mat(int _w, int _h, void* _data, size_t _elemsize, int _elempack, Allocator* _allocator)
881 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(2), w(_w), h(_h), d(1), c(1)
882{
883 cstep = (size_t)w * h;
884}
885
886NCNN_FORCEINLINE Mat::Mat(int _w, int _h, int _c, void* _data, size_t _elemsize, int _elempack, Allocator* _allocator)
887 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(3), w(_w), h(_h), d(1), c(_c)
888{
889 cstep = alignSize((size_t)w * h * elemsize, 16) / elemsize;
890}
891
892NCNN_FORCEINLINE Mat::Mat(int _w, int _h, int _d, int _c, void* _data, size_t _elemsize, int _elempack, Allocator* _allocator)
893 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(4), w(_w), h(_h), d(_d), c(_c)
894{
895 cstep = alignSize((size_t)w * h * d * elemsize, 16) / elemsize;
896}
897
898NCNN_FORCEINLINE Mat::~Mat()
899{
900 release();
901}
902
903NCNN_FORCEINLINE void Mat::fill(float _v)
904{
905 int size = (int)total();
906 float* ptr = (float*)data;
907
908 int i = 0;
909#if __ARM_NEON
910 float32x4_t _c = vdupq_n_f32(_v);
911 for (; i + 3 < size; i += 4)
912 {
913 vst1q_f32(ptr, _c);
914 ptr += 4;
915 }
916#endif // __ARM_NEON
917 for (; i < size; i++)
918 {
919 *ptr++ = _v;
920 }
921}
922
923NCNN_FORCEINLINE void Mat::fill(int _v)
924{
925 int size = (int)total();
926 int* ptr = (int*)data;
927
928 int i = 0;
929#if __ARM_NEON
930 int32x4_t _c = vdupq_n_s32(_v);
931 for (; i + 3 < size; i += 4)
932 {
933 vst1q_s32(ptr, _c);
934 ptr += 4;
935 }
936#endif // __ARM_NEON
937 for (; i < size; i++)
938 {
939 *ptr++ = _v;
940 }
941}
942
943#if __ARM_NEON
944NCNN_FORCEINLINE void Mat::fill(float32x4_t _v)
945{
946 int size = (int)total();
947 float* ptr = (float*)data;
948 for (int i = 0; i < size; i++)
949 {
950 vst1q_f32(ptr, _v);
951 ptr += 4;
952 }
953}
954
955NCNN_FORCEINLINE void Mat::fill(uint16x4_t _v)
956{
957 int size = (int)total();
958 unsigned short* ptr = (unsigned short*)data;
959 for (int i = 0; i < size; i++)
960 {
961 vst1_u16(ptr, _v);
962 ptr += 4;
963 }
964}
965
966#if !defined(_MSC_VER)
967NCNN_FORCEINLINE void Mat::fill(int32x4_t _v)
968{
969 int size = (int)total();
970 int* ptr = (int*)data;
971 for (int i = 0; i < size; i++)
972 {
973 vst1q_s32(ptr, _v);
974 ptr += 4;
975 }
976}
977#endif
978
979NCNN_FORCEINLINE void Mat::fill(int32x4_t _v0, int32x4_t _v1)
980{
981 int size = (int)total();
982 int* ptr = (int*)data;
983 for (int i = 0; i < size; i++)
984 {
985 vst1q_s32(ptr, _v0);
986 vst1q_s32(ptr + 4, _v1);
987 ptr += 8;
988 }
989}
990#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
991#if !defined(_MSC_VER)
992NCNN_FORCEINLINE void Mat::fill(float16x4_t _v)
993{
994 int size = (int)total();
995 __fp16* ptr = (__fp16*)data;
996 for (int i = 0; i < size; i++)
997 {
998 vst1_f16(ptr, _v);
999 ptr += 4;
1000 }
1001}
1002
1003NCNN_FORCEINLINE void Mat::fill(float16x8_t _v)
1004{
1005 int size = (int)total();
1006 __fp16* ptr = (__fp16*)data;
1007 for (int i = 0; i < size; i++)
1008 {
1009 vst1q_f16(ptr, _v);
1010 ptr += 8;
1011 }
1012}
1013#endif
1014#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1015#endif // __ARM_NEON
1016
1017#if __SSE2__
1018#if __AVX__
1019#if __AVX512F__
1020NCNN_FORCEINLINE void Mat::fill(__m512 _v)
1021{
1022 int size = (int)total();
1023 float* ptr = (float*)data;
1024 for (int i = 0; i < size; i++)
1025 {
1026 _mm512_storeu_ps(ptr, _v);
1027 ptr += 16;
1028 }
1029}
1030#endif // __AVX512F__
1031NCNN_FORCEINLINE void Mat::fill(__m256 _v, int _i)
1032{
1033 // old gcc cannot overload __m128 and __m256 type
1034 // add a dummy int parameter for different mangled function symbol
1035 (void)_i;
1036 int size = (int)total();
1037 float* ptr = (float*)data;
1038 for (int i = 0; i < size; i++)
1039 {
1040 _mm256_storeu_ps(ptr, _v);
1041 ptr += 8;
1042 }
1043}
1044#endif // __AVX__
1045NCNN_FORCEINLINE void Mat::fill(__m128 _v)
1046{
1047 int size = (int)total();
1048 float* ptr = (float*)data;
1049 for (int i = 0; i < size; i++)
1050 {
1051 _mm_storeu_ps(ptr, _v);
1052 ptr += 4;
1053 }
1054}
1055NCNN_FORCEINLINE void Mat::fill(__m128i _v)
1056{
1057 int size = (int)total();
1058 unsigned short* ptr = (unsigned short*)data;
1059 for (int i = 0; i < size; i++)
1060 {
1061 _mm_store_si128((__m128i*)ptr, _v);
1062 ptr += 8;
1063 }
1064}
1065#endif // __SSE2__
1066
1067#if __mips_msa
1068NCNN_FORCEINLINE void Mat::fill(v4f32 _v)
1069{
1070 int size = (int)total();
1071 float* ptr = (float*)data;
1072 for (int i = 0; i < size; i++)
1073 {
1074 __msa_st_w((v4i32)_v, ptr, 0);
1075 ptr += 4;
1076 }
1077}
1078#endif // __mips_msa
1079
1080#if __loongarch_sx
1081NCNN_FORCEINLINE void Mat::fill(__m128 _v)
1082{
1083 int size = (int)total();
1084 float* ptr = (float*)data;
1085 for (int i = 0; i < size; i++)
1086 {
1087 __lsx_vst(_v, ptr, 0);
1088 ptr += 4;
1089 }
1090}
1091#endif // __loongarch_sx
1092#if __riscv_vector
1093NCNN_FORCEINLINE void Mat::fill(vfloat32m1_t _v)
1094{
1095 const int packn = cpu_riscv_vlenb() / 4;
1096 const size_t vl = vsetvl_e32m1(packn);
1097
1098 int size = (int)total();
1099 float* ptr = (float*)data;
1100 for (int i = 0; i < size; i++)
1101 {
1102 vse32_v_f32m1(ptr, _v, vl);
1103 ptr += packn;
1104 }
1105}
1106
1107NCNN_FORCEINLINE void Mat::fill(vuint16m1_t _v)
1108{
1109 const int packn = cpu_riscv_vlenb() / 2;
1110 const size_t vl = vsetvl_e16m1(packn);
1111
1112 int size = (int)total();
1113 unsigned short* ptr = (unsigned short*)data;
1114 for (int i = 0; i < size; i++)
1115 {
1116 vse16_v_u16m1(ptr, _v, vl);
1117 ptr += packn;
1118 }
1119}
1120
1121NCNN_FORCEINLINE void Mat::fill(vint8m1_t _v)
1122{
1123 const int packn = cpu_riscv_vlenb() / 1;
1124 const size_t vl = vsetvl_e8m1(packn);
1125
1126 int size = (int)total();
1127 signed char* ptr = (signed char*)data;
1128 for (int i = 0; i < size; i++)
1129 {
1130 vse8_v_i8m1(ptr, _v, vl);
1131 ptr += packn;
1132 }
1133}
1134#if __riscv_zfh
1135NCNN_FORCEINLINE void Mat::fill(vfloat16m1_t _v)
1136{
1137 const int packn = cpu_riscv_vlenb() / 2;
1138 const size_t vl = vsetvl_e16m1(packn);
1139
1140 int size = (int)total();
1141 __fp16* ptr = (__fp16*)data;
1142 for (int i = 0; i < size; i++)
1143 {
1144 vse16_v_f16m1(ptr, _v, vl);
1145 ptr += packn;
1146 }
1147}
1148#endif // __riscv_zfh
1149#endif // __riscv_vector
1150
1151template<typename T>
1152NCNN_FORCEINLINE void Mat::fill(T _v)
1153{
1154 int size = (int)total();
1155 T* ptr = (T*)data;
1156 for (int i = 0; i < size; i++)
1157 {
1158 ptr[i] = _v;
1159 }
1160}
1161
1162NCNN_FORCEINLINE Mat& Mat::operator=(const Mat& m)
1163{
1164 if (this == &m)
1165 return *this;
1166
1167 if (m.refcount)
1168 NCNN_XADD(m.refcount, 1);
1169
1170 release();
1171
1172 data = m.data;
1173 refcount = m.refcount;
1174 elemsize = m.elemsize;
1175 elempack = m.elempack;
1176 allocator = m.allocator;
1177
1178 dims = m.dims;
1179 w = m.w;
1180 h = m.h;
1181 d = m.d;
1182 c = m.c;
1183
1184 cstep = m.cstep;
1185
1186 return *this;
1187}
1188
1189NCNN_FORCEINLINE void Mat::addref()
1190{
1191 if (refcount)
1192 NCNN_XADD(refcount, 1);
1193}
1194
1195NCNN_FORCEINLINE void Mat::release()
1196{
1197 if (refcount && NCNN_XADD(refcount, -1) == 1)
1198 {
1199 if (allocator)
1200 allocator->fastFree(data);
1201 else
1202 fastFree(data);
1203 }
1204
1205 data = 0;
1206
1207 elemsize = 0;
1208 elempack = 0;
1209
1210 dims = 0;
1211 w = 0;
1212 h = 0;
1213 d = 0;
1214 c = 0;
1215
1216 cstep = 0;
1217
1218 refcount = 0;
1219}
1220
1221NCNN_FORCEINLINE bool Mat::empty() const
1222{
1223 return data == 0 || total() == 0;
1224}
1225
1226NCNN_FORCEINLINE size_t Mat::total() const
1227{
1228 return cstep * c;
1229}
1230
1231NCNN_FORCEINLINE int Mat::elembits() const
1232{
1233 return elempack ? static_cast<int>(elemsize * 8) / elempack : 0;
1234}
1235
1236NCNN_FORCEINLINE Mat Mat::shape() const
1237{
1238 if (dims == 1)
1239 return Mat(w * elempack, (void*)0);
1240 if (dims == 2)
1241 return Mat(w, h * elempack, (void*)0);
1242 if (dims == 3)
1243 return Mat(w, h, c * elempack, (void*)0);
1244 if (dims == 4)
1245 return Mat(w, h, d, c * elempack, (void*)0);
1246
1247 return Mat();
1248}
1249
1250NCNN_FORCEINLINE Mat Mat::channel(int _c)
1251{
1252 Mat m(w, h, d, (unsigned char*)data + cstep * _c * elemsize, elemsize, elempack, allocator);
1253 m.dims = dims - 1;
1254 if (dims == 4)
1255 m.cstep = (size_t)w * h;
1256 return m;
1257}
1258
1259NCNN_FORCEINLINE const Mat Mat::channel(int _c) const
1260{
1261 Mat m(w, h, d, (unsigned char*)data + cstep * _c * elemsize, elemsize, elempack, allocator);
1262 m.dims = dims - 1;
1263 if (dims == 4)
1264 m.cstep = (size_t)w * h;
1265 return m;
1266}
1267
1268NCNN_FORCEINLINE Mat Mat::depth(int z)
1269{
1270 return Mat(w, h, (unsigned char*)data + (size_t)w * h * z * elemsize, elemsize, elempack, allocator);
1271}
1272
1273NCNN_FORCEINLINE const Mat Mat::depth(int z) const
1274{
1275 return Mat(w, h, (unsigned char*)data + (size_t)w * h * z * elemsize, elemsize, elempack, allocator);
1276}
1277
1278NCNN_FORCEINLINE float* Mat::row(int y)
1279{
1280 return (float*)((unsigned char*)data + (size_t)w * y * elemsize);
1281}
1282
1283NCNN_FORCEINLINE const float* Mat::row(int y) const
1284{
1285 return (const float*)((unsigned char*)data + (size_t)w * y * elemsize);
1286}
1287
1288template<typename T>
1289NCNN_FORCEINLINE T* Mat::row(int y)
1290{
1291 return (T*)((unsigned char*)data + (size_t)w * y * elemsize);
1292}
1293
1294template<typename T>
1295NCNN_FORCEINLINE const T* Mat::row(int y) const
1296{
1297 return (const T*)((unsigned char*)data + (size_t)w * y * elemsize);
1298}
1299
1300NCNN_FORCEINLINE Mat Mat::channel_range(int _c, int channels)
1301{
1302 Mat m(w, h, d, channels, (unsigned char*)data + cstep * _c * elemsize, elemsize, elempack, allocator);
1303 m.dims = dims;
1304 return m;
1305}
1306
1307NCNN_FORCEINLINE const Mat Mat::channel_range(int _c, int channels) const
1308{
1309 Mat m(w, h, d, channels, (unsigned char*)data + cstep * _c * elemsize, elemsize, elempack, allocator);
1310 m.dims = dims;
1311 return m;
1312}
1313
1314NCNN_FORCEINLINE Mat Mat::depth_range(int z, int depths)
1315{
1316 Mat m(w, h, depths, (unsigned char*)data + (size_t)w * h * z * elemsize, elemsize, elempack, allocator);
1317 m.cstep = (size_t)w * h;
1318 return m;
1319}
1320
1321NCNN_FORCEINLINE const Mat Mat::depth_range(int z, int depths) const
1322{
1323 Mat m(w, h, depths, (unsigned char*)data + (size_t)w * h * z * elemsize, elemsize, elempack, allocator);
1324 m.cstep = (size_t)w * h;
1325 return m;
1326}
1327
1328NCNN_FORCEINLINE Mat Mat::row_range(int y, int rows)
1329{
1330 return Mat(w, rows, (unsigned char*)data + (size_t)w * y * elemsize, elemsize, elempack, allocator);
1331}
1332
1333NCNN_FORCEINLINE const Mat Mat::row_range(int y, int rows) const
1334{
1335 return Mat(w, rows, (unsigned char*)data + (size_t)w * y * elemsize, elemsize, elempack, allocator);
1336}
1337
1338NCNN_FORCEINLINE Mat Mat::range(int x, int n)
1339{
1340 return Mat(n, (unsigned char*)data + x * elemsize, elemsize, elempack, allocator);
1341}
1342
1343NCNN_FORCEINLINE const Mat Mat::range(int x, int n) const
1344{
1345 return Mat(n, (unsigned char*)data + x * elemsize, elemsize, elempack, allocator);
1346}
1347
1348template<typename T>
1349NCNN_FORCEINLINE Mat::operator T*()
1350{
1351 return (T*)data;
1352}
1353
1354template<typename T>
1355NCNN_FORCEINLINE Mat::operator const T*() const
1356{
1357 return (const T*)data;
1358}
1359
1360NCNN_FORCEINLINE float& Mat::operator[](size_t i)
1361{
1362 return ((float*)data)[i];
1363}
1364
1365NCNN_FORCEINLINE const float& Mat::operator[](size_t i) const
1366{
1367 return ((const float*)data)[i];
1368}
1369
1370#if NCNN_VULKAN
1371
1372NCNN_FORCEINLINE VkMat::VkMat()
1373 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
1374{
1375}
1376
1377NCNN_FORCEINLINE VkMat::VkMat(int _w, size_t _elemsize, VkAllocator* _allocator)
1378 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
1379{
1380 create(_w, _elemsize, _allocator);
1381}
1382
1383NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, size_t _elemsize, VkAllocator* _allocator)
1384 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
1385{
1386 create(_w, _h, _elemsize, _allocator);
1387}
1388
1389NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, int _c, size_t _elemsize, VkAllocator* _allocator)
1390 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
1391{
1392 create(_w, _h, _c, _elemsize, _allocator);
1393}
1394
1395NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, int _d, int _c, size_t _elemsize, VkAllocator* _allocator)
1396 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
1397{
1398 create(_w, _h, _d, _c, _elemsize, _allocator);
1399}
1400
1401NCNN_FORCEINLINE VkMat::VkMat(int _w, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1402 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
1403{
1404 create(_w, _elemsize, _elempack, _allocator);
1405}
1406
1407NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1408 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
1409{
1410 create(_w, _h, _elemsize, _elempack, _allocator);
1411}
1412
1413NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, int _c, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1414 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
1415{
1416 create(_w, _h, _c, _elemsize, _elempack, _allocator);
1417}
1418
1419NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, int _d, int _c, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1420 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
1421{
1422 create(_w, _h, _d, _c, _elemsize, _elempack, _allocator);
1423}
1424
1425NCNN_FORCEINLINE VkMat::VkMat(const VkMat& m)
1426 : data(m.data), refcount(m.refcount), elemsize(m.elemsize), elempack(m.elempack), allocator(m.allocator), dims(m.dims), w(m.w), h(m.h), d(m.d), c(m.c)
1427{
1428 addref();
1429
1430 cstep = m.cstep;
1431}
1432
1433NCNN_FORCEINLINE VkMat::VkMat(int _w, VkBufferMemory* _data, size_t _elemsize, VkAllocator* _allocator)
1434 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(1), w(_w), h(1), d(1), c(1)
1435{
1436 cstep = w;
1437}
1438
1439NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, VkBufferMemory* _data, size_t _elemsize, VkAllocator* _allocator)
1440 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(2), w(_w), h(_h), d(1), c(1)
1441{
1442 cstep = w * h;
1443}
1444
1445NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, int _c, VkBufferMemory* _data, size_t _elemsize, VkAllocator* _allocator)
1446 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(3), w(_w), h(_h), d(1), c(_c)
1447{
1448 cstep = alignSize(w * h * elemsize, 16) / elemsize;
1449}
1450
1451NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, int _d, int _c, VkBufferMemory* _data, size_t _elemsize, VkAllocator* _allocator)
1452 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(4), w(_w), h(_h), d(_d), c(_c)
1453{
1454 cstep = alignSize(w * h * d * elemsize, 16) / elemsize;
1455}
1456
1457NCNN_FORCEINLINE VkMat::VkMat(int _w, VkBufferMemory* _data, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1458 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(1), w(_w), h(1), d(1), c(1)
1459{
1460 cstep = w;
1461}
1462
1463NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, VkBufferMemory* _data, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1464 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(2), w(_w), h(_h), d(1), c(1)
1465{
1466 cstep = w * h;
1467}
1468
1469NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, int _c, VkBufferMemory* _data, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1470 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(3), w(_w), h(_h), d(1), c(_c)
1471{
1472 cstep = alignSize(w * h * elemsize, 16) / elemsize;
1473}
1474
1475NCNN_FORCEINLINE VkMat::VkMat(int _w, int _h, int _d, int _c, VkBufferMemory* _data, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1476 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(4), w(_w), h(_h), d(_d), c(_c)
1477{
1478 cstep = alignSize(w * h * d * elemsize, 16) / elemsize;
1479}
1480
1481NCNN_FORCEINLINE VkMat::~VkMat()
1482{
1483 release();
1484}
1485
1486NCNN_FORCEINLINE VkMat& VkMat::operator=(const VkMat& m)
1487{
1488 if (this == &m)
1489 return *this;
1490
1491 if (m.refcount)
1492 NCNN_XADD(m.refcount, 1);
1493
1494 release();
1495
1496 data = m.data;
1497 refcount = m.refcount;
1498 elemsize = m.elemsize;
1499 elempack = m.elempack;
1500 allocator = m.allocator;
1501
1502 dims = m.dims;
1503 w = m.w;
1504 h = m.h;
1505 d = m.d;
1506 c = m.c;
1507
1508 cstep = m.cstep;
1509
1510 return *this;
1511}
1512
1513NCNN_FORCEINLINE Mat VkMat::mapped() const
1514{
1515 if (!allocator->mappable)
1516 return Mat();
1517
1518 if (dims == 1)
1519 return Mat(w, mapped_ptr(), elemsize, elempack, 0);
1520
1521 if (dims == 2)
1522 return Mat(w, h, mapped_ptr(), elemsize, elempack, 0);
1523
1524 if (dims == 3)
1525 return Mat(w, h, c, mapped_ptr(), elemsize, elempack, 0);
1526
1527 if (dims == 4)
1528 return Mat(w, h, d, c, mapped_ptr(), elemsize, elempack, 0);
1529
1530 return Mat();
1531}
1532
1533NCNN_FORCEINLINE void* VkMat::mapped_ptr() const
1534{
1535 if (!allocator->mappable)
1536 return 0;
1537
1538 return (unsigned char*)data->mapped_ptr + data->offset;
1539}
1540
1541NCNN_FORCEINLINE void VkMat::addref()
1542{
1543 if (refcount)
1544 NCNN_XADD(refcount, 1);
1545}
1546
1547NCNN_FORCEINLINE void VkMat::release()
1548{
1549 if (refcount && NCNN_XADD(refcount, -1) == 1)
1550 {
1551 if (allocator && data)
1552 {
1553 allocator->fastFree(data);
1554 }
1555 }
1556
1557 data = 0;
1558
1559 elemsize = 0;
1560 elempack = 0;
1561
1562 dims = 0;
1563 w = 0;
1564 h = 0;
1565 d = 0;
1566 c = 0;
1567
1568 cstep = 0;
1569
1570 refcount = 0;
1571}
1572
1573NCNN_FORCEINLINE bool VkMat::empty() const
1574{
1575 return data == 0 || total() == 0;
1576}
1577
1578NCNN_FORCEINLINE size_t VkMat::total() const
1579{
1580 return cstep * c;
1581}
1582
1583NCNN_FORCEINLINE int VkMat::elembits() const
1584{
1585 return elempack ? static_cast<int>(elemsize) * 8 / elempack : 0;
1586}
1587
1588NCNN_FORCEINLINE Mat VkMat::shape() const
1589{
1590 if (dims == 1)
1591 return Mat(w * elempack, (void*)0);
1592 if (dims == 2)
1593 return Mat(w, h * elempack, (void*)0);
1594 if (dims == 3)
1595 return Mat(w, h, c * elempack, (void*)0);
1596 if (dims == 4)
1597 return Mat(w, h, d, c * elempack, (void*)0);
1598
1599 return Mat();
1600}
1601
1602NCNN_FORCEINLINE VkBuffer VkMat::buffer() const
1603{
1604 return data->buffer;
1605}
1606
1607NCNN_FORCEINLINE size_t VkMat::buffer_offset() const
1608{
1609 return data->offset;
1610}
1611
1612NCNN_FORCEINLINE size_t VkMat::buffer_capacity() const
1613{
1614 return data->capacity;
1615}
1616
1617NCNN_FORCEINLINE VkImageMat::VkImageMat()
1618 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0)
1619{
1620}
1621
1622NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, size_t _elemsize, VkAllocator* _allocator)
1623 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0)
1624{
1625 create(_w, _elemsize, _allocator);
1626}
1627
1628NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, size_t _elemsize, VkAllocator* _allocator)
1629 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0)
1630{
1631 create(_w, _h, _elemsize, _allocator);
1632}
1633
1634NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, int _c, size_t _elemsize, VkAllocator* _allocator)
1635 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0)
1636{
1637 create(_w, _h, _c, _elemsize, _allocator);
1638}
1639
1640NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, int _d, int _c, size_t _elemsize, VkAllocator* _allocator)
1641 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0)
1642{
1643 create(_w, _h, _d, _c, _elemsize, _allocator);
1644}
1645
1646NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1647 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0)
1648{
1649 create(_w, _elemsize, _elempack, _allocator);
1650}
1651
1652NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1653 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0)
1654{
1655 create(_w, _h, _elemsize, _elempack, _allocator);
1656}
1657
1658NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, int _c, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1659 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0)
1660{
1661 create(_w, _h, _c, _elemsize, _elempack, _allocator);
1662}
1663
1664NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, int _d, int _c, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1665 : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0)
1666{
1667 create(_w, _h, _d, _c, _elemsize, _elempack, _allocator);
1668}
1669
1670NCNN_FORCEINLINE VkImageMat::VkImageMat(const VkImageMat& m)
1671 : data(m.data), refcount(m.refcount), elemsize(m.elemsize), elempack(m.elempack), allocator(m.allocator), dims(m.dims), w(m.w), h(m.h), d(m.d), c(m.c)
1672{
1673 addref();
1674}
1675
1676NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, VkImageMemory* _data, size_t _elemsize, VkAllocator* _allocator)
1677 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(1), w(_w), h(1), d(1), c(1)
1678{
1679}
1680
1681NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, VkImageMemory* _data, size_t _elemsize, VkAllocator* _allocator)
1682 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(2), w(_w), h(_h), d(1), c(1)
1683{
1684}
1685
1686NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, int _c, VkImageMemory* _data, size_t _elemsize, VkAllocator* _allocator)
1687 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(3), w(_w), h(_h), d(1), c(_c)
1688{
1689}
1690
1691NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, int _d, int _c, VkImageMemory* _data, size_t _elemsize, VkAllocator* _allocator)
1692 : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(4), w(_w), h(_h), d(_d), c(_c)
1693{
1694}
1695
1696NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, VkImageMemory* _data, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1697 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(1), w(_w), h(1), d(1), c(1)
1698{
1699}
1700
1701NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, VkImageMemory* _data, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1702 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(2), w(_w), h(_h), d(1), c(1)
1703{
1704}
1705
1706NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, int _c, VkImageMemory* _data, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1707 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(3), w(_w), h(_h), d(1), c(_c)
1708{
1709}
1710
1711NCNN_FORCEINLINE VkImageMat::VkImageMat(int _w, int _h, int _d, int _c, VkImageMemory* _data, size_t _elemsize, int _elempack, VkAllocator* _allocator)
1712 : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(4), w(_w), h(_h), d(_d), c(_c)
1713{
1714}
1715
1716NCNN_FORCEINLINE VkImageMat::~VkImageMat()
1717{
1718 release();
1719}
1720
1721NCNN_FORCEINLINE VkImageMat& VkImageMat::operator=(const VkImageMat& m)
1722{
1723 if (this == &m)
1724 return *this;
1725
1726 if (m.refcount)
1727 NCNN_XADD(m.refcount, 1);
1728
1729 release();
1730
1731 data = m.data;
1732 refcount = m.refcount;
1733 elemsize = m.elemsize;
1734 elempack = m.elempack;
1735 allocator = m.allocator;
1736
1737 dims = m.dims;
1738 w = m.w;
1739 h = m.h;
1740 d = m.d;
1741 c = m.c;
1742
1743 return *this;
1744}
1745
1746NCNN_FORCEINLINE Mat VkImageMat::mapped() const
1747{
1748 if (!allocator->mappable || !data->mapped_ptr)
1749 return Mat();
1750
1751 if (dims == 1)
1752 return Mat(w, mapped_ptr(), elemsize, elempack, 0);
1753
1754 if (dims == 2)
1755 return Mat(w, h, mapped_ptr(), elemsize, elempack, 0);
1756
1757 if (dims == 3)
1758 return Mat(w, h, c, mapped_ptr(), elemsize, elempack, 0);
1759
1760 if (dims == 4)
1761 return Mat(w, h, d, c, mapped_ptr(), elemsize, elempack, 0);
1762
1763 return Mat();
1764}
1765
1766NCNN_FORCEINLINE void* VkImageMat::mapped_ptr() const
1767{
1768 if (!allocator->mappable || !data->mapped_ptr)
1769 return 0;
1770
1771 return (unsigned char*)data->mapped_ptr + data->bind_offset;
1772}
1773
1774NCNN_FORCEINLINE void VkImageMat::addref()
1775{
1776 if (refcount)
1777 NCNN_XADD(refcount, 1);
1778}
1779
1780NCNN_FORCEINLINE void VkImageMat::release()
1781{
1782 if (refcount && NCNN_XADD(refcount, -1) == 1)
1783 {
1784 if (allocator && data)
1785 {
1786 allocator->fastFree(data);
1787 }
1788 }
1789
1790 data = 0;
1791
1792 elemsize = 0;
1793 elempack = 0;
1794
1795 dims = 0;
1796 w = 0;
1797 h = 0;
1798 d = 0;
1799 c = 0;
1800
1801 refcount = 0;
1802}
1803
1804NCNN_FORCEINLINE bool VkImageMat::empty() const
1805{
1806 return data == 0 || total() == 0;
1807}
1808
1809NCNN_FORCEINLINE size_t VkImageMat::total() const
1810{
1811 return w * h * d * c;
1812}
1813
1814NCNN_FORCEINLINE int VkImageMat::elembits() const
1815{
1816 return elempack ? static_cast<int>(elemsize) * 8 / elempack : 0;
1817}
1818
1819NCNN_FORCEINLINE Mat VkImageMat::shape() const
1820{
1821 if (dims == 1)
1822 return Mat(w * elempack, (void*)0);
1823 if (dims == 2)
1824 return Mat(w, h * elempack, (void*)0);
1825 if (dims == 3)
1826 return Mat(w, h, c * elempack, (void*)0);
1827 if (dims == 4)
1828 return Mat(w, h, d, c * elempack, (void*)0);
1829
1830 return Mat();
1831}
1832
1833NCNN_FORCEINLINE VkImage VkImageMat::image() const
1834{
1835 return data->image;
1836}
1837
1838NCNN_FORCEINLINE VkImageView VkImageMat::imageview() const
1839{
1840 return data->imageview;
1841}
1842
1843#endif // NCNN_VULKAN
1844
1845} // namespace ncnn
1846
1847#endif // NCNN_MAT_H