-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathwarp.cpp
211 lines (168 loc) · 5.53 KB
/
warp.cpp
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
// rife implemented with ncnn library
#include "rife_ops.h"
#include "warp.comp.hex.h"
#include "warp_pack4.comp.hex.h"
#include "warp_pack8.comp.hex.h"
using namespace ncnn;
Warp::Warp()
{
support_vulkan = true;
pipeline_warp = 0;
pipeline_warp_pack4 = 0;
pipeline_warp_pack8 = 0;
}
int Warp::create_pipeline(const Option& opt)
{
if (!vkdev)
return 0;
std::vector<vk_specialization_type> specializations(0 + 0);
// pack1
{
static std::vector<uint32_t> spirv;
static ncnn::Mutex lock;
{
ncnn::MutexLockGuard guard(lock);
if (spirv.empty())
{
compile_spirv_module(warp_comp_data, sizeof(warp_comp_data), opt, spirv);
}
}
pipeline_warp = new Pipeline(vkdev);
pipeline_warp->set_optimal_local_size_xyz();
pipeline_warp->create(spirv.data(), spirv.size() * 4, specializations);
}
// pack4
{
static std::vector<uint32_t> spirv;
static ncnn::Mutex lock;
{
ncnn::MutexLockGuard guard(lock);
if (spirv.empty())
{
compile_spirv_module(warp_pack4_comp_data, sizeof(warp_pack4_comp_data), opt, spirv);
}
}
pipeline_warp_pack4 = new Pipeline(vkdev);
pipeline_warp_pack4->set_optimal_local_size_xyz();
pipeline_warp_pack4->create(spirv.data(), spirv.size() * 4, specializations);
}
// pack8
if (opt.use_shader_pack8)
{
static std::vector<uint32_t> spirv;
static ncnn::Mutex lock;
{
ncnn::MutexLockGuard guard(lock);
if (spirv.empty())
{
compile_spirv_module(warp_pack8_comp_data, sizeof(warp_pack8_comp_data), opt, spirv);
}
}
pipeline_warp_pack8 = new Pipeline(vkdev);
pipeline_warp_pack8->set_optimal_local_size_xyz();
pipeline_warp_pack8->create(spirv.data(), spirv.size() * 4, specializations);
}
return 0;
}
int Warp::destroy_pipeline(const Option& opt)
{
delete pipeline_warp;
pipeline_warp = 0;
delete pipeline_warp_pack4;
pipeline_warp_pack4 = 0;
delete pipeline_warp_pack8;
pipeline_warp_pack8 = 0;
return 0;
}
int Warp::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
{
const Mat& image_blob = bottom_blobs[0];
const Mat& flow_blob = bottom_blobs[1];
int w = image_blob.w;
int h = image_blob.h;
int channels = image_blob.c;
Mat& top_blob = top_blobs[0];
top_blob.create(w, h, channels);
if (top_blob.empty())
return -100;
#pragma omp parallel for num_threads(opt.num_threads)
for (int q = 0; q < channels; q++)
{
float* outptr = top_blob.channel(q);
const Mat image = image_blob.channel(q);
const float* fxptr = flow_blob.channel(0);
const float* fyptr = flow_blob.channel(1);
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
float flow_x = fxptr[0];
float flow_y = fyptr[0];
float sample_x = x + flow_x;
float sample_y = y + flow_y;
// bilinear interpolate
float v;
{
int x0 = floor(sample_x);
int y0 = floor(sample_y);
int x1 = x0 + 1;
int y1 = y0 + 1;
x0 = std::min(std::max(x0, 0), w - 1);
y0 = std::min(std::max(y0, 0), h - 1);
x1 = std::min(std::max(x1, 0), w - 1);
y1 = std::min(std::max(y1, 0), h - 1);
float alpha = sample_x - x0;
float beta = sample_y - y0;
float v0 = image.row(y0)[x0];
float v1 = image.row(y0)[x1];
float v2 = image.row(y1)[x0];
float v3 = image.row(y1)[x1];
float v4 = v0 * (1 - alpha) + v1 * alpha;
float v5 = v2 * (1 - alpha) + v3 * alpha;
v = v4 * (1 - beta) + v5 * beta;
}
outptr[0] = v;
outptr += 1;
fxptr += 1;
fyptr += 1;
}
}
}
return 0;
}
int Warp::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const
{
const VkMat& image_blob = bottom_blobs[0];
const VkMat& flow_blob = bottom_blobs[1];
int w = image_blob.w;
int h = image_blob.h;
int channels = image_blob.c;
size_t elemsize = image_blob.elemsize;
int elempack = image_blob.elempack;
VkMat& top_blob = top_blobs[0];
top_blob.create(w, h, channels, elemsize, elempack, opt.blob_vkallocator);
if (top_blob.empty())
return -100;
std::vector<VkMat> bindings(3);
bindings[0] = image_blob;
bindings[1] = flow_blob;
bindings[2] = top_blob;
std::vector<vk_constant_type> constants(4);
constants[0].i = top_blob.w;
constants[1].i = top_blob.h;
constants[2].i = top_blob.c;
constants[3].i = top_blob.cstep;
if (elempack == 8)
{
cmd.record_pipeline(pipeline_warp_pack8, bindings, constants, top_blob);
}
else if (elempack == 4)
{
cmd.record_pipeline(pipeline_warp_pack4, bindings, constants, top_blob);
}
else // if (elempack == 1)
{
cmd.record_pipeline(pipeline_warp, bindings, constants, top_blob);
}
return 0;
}