-
Notifications
You must be signed in to change notification settings - Fork 2
/
sgm_cl.h
207 lines (169 loc) · 6 KB
/
sgm_cl.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
#ifndef SGM_CL_H
#define SGM_CL_H
#include "sgm_source_path.h"
#include <CL/cl.h>
#include <string.h>
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <string>
#include <fstream>
#include <sstream>
namespace sgm_cl{
struct BlockDim {
BlockDim(int _x = 1, int _y = 1, int _z = 1):
x(_x), y(_y), z(_z) {}
int x, y, z;
};
struct GridDim {
GridDim(int _x = 1, int _y = 1, int _z = 1):
x(_x), y(_y), z(_z) {}
int x, y, z;
};
struct ArgumentPropereties
{
ArgumentPropereties(void* ptr = nullptr, size_t argsize = 0) :
arg_ptr(ptr), sizeof_arg(argsize) {}
void* arg_ptr;
size_t sizeof_arg;
};
enum MemFlag
{
MEM_FLAG_READ_WRITE = 1 << 0,
MEM_FLAG_WRITE_ONLY = 1 << 1,
MEM_FLAG_READ_ONLY = 1 << 2,
MEM_FLAG_USE_HOST_PTR = 1 << 3, // maybe needs to be removed, in cuda not trivial
MEM_FLAG_ALLOC_HOST_PTR = 1 << 4,
MEM_FLAG_COPY_HOST_PTR = 1 << 5
};
enum SyncMode
{
SYNC_MODE_ASYNC = 0,
SYNC_MODE_BLOCKING = 1
};
class CLContext {
public:
CLContext(int platform_id = 0, int device_id = 0, int num_streams = 1);
~CLContext();
CLContext(const CLContext& context) = delete;
CLContext& operator=(const CLContext& context) = delete;
CLContext(CLContext&& context) noexcept;
CLContext& operator=(CLContext&& context) noexcept;
cl_context GetCLContext() const {return cl_context_;}
cl_device_id GetDevId() const {return cl_device_id_;}
cl_command_queue GetCommandQueue(int id) const;
void Finish(int command_queue) const;
inline const std::string& CLInfo() const {return cl_info_;}
private:
std::string GetPlatformInfo(cl_platform_id platform_id, int info_name) const;
std::string GetDevInfo(cl_device_id dev_id, int info_name) const;
std::vector<cl_command_queue> cl_command_queues_;
std::string cl_info_;
cl_context cl_context_;
cl_device_id cl_device_id_;
};
class CLBuffer {
public:
CLBuffer(const CLContext* ctx, size_t size, MemFlag flag = MEM_FLAG_READ_WRITE,
void* host_ptr = nullptr);
~CLBuffer();
void Write(const void* data, SyncMode block_queue = SYNC_MODE_BLOCKING,
int command_queue = 0);
void Read(void* data, SyncMode block_queue = SYNC_MODE_BLOCKING,
int command_queue = 0) const;
ArgumentPropereties GetArgumentPropereties() const;
private:
void Write(const void* data, size_t offset, size_t size,
SyncMode block_queue,int command_queue);
void Read(void* data, size_t offset, size_t size, SyncMode block_queue,
int command_queue) const;
mutable cl_mem buffer_;
MemFlag flag_;
size_t size_;
const CLContext* context_;
};
class CLKernel {
public:
CLKernel(const CLContext* context, cl_program program, const std::string& kernel_name);
~CLKernel();
void Launch(int queue_id, GridDim gd, BlockDim bd);
template <typename... Types> void SetArgs(Types&&... args);
private:
void SetArgs(int num_args, void** argument, size_t* argument_sizes);
inline void FillArgVector(int arg_idx, void** arg_address, size_t* arg_sizeof);
template <typename T, typename... Types>
void FillArgVector(int arg_idx, void** arg_address, size_t* arg_sizeof,
T&& arg, Types&&... Fargs);
template <typename... Types>
void FillArgVector(int arg_idx, void** arg_address, size_t* arg_sizeof,
CLBuffer* arg, Types&&... Fargs);
cl_kernel kernel_;
const CLContext* context_;
};
class CLProgram{
public:
CLProgram(const std::string& source_path="", const CLContext* context=nullptr
, const std::string& compilation_options="-I \"./\"");
~CLProgram();
bool CreateProgram(const char* source, size_t size_src,
const std::string& compilation_options);
bool CreateKernels();
CLKernel* GetKernel(const std::string& kernel_name);
inline void SetCLContext(const CLContext& context) {context_ = &context;}
private:
const CLContext* context_;
cl_program cl_program_;
std::map<std::string, CLKernel*> kernels_;
};
class StereoSGMCL{
public:
StereoSGMCL(int width, int height, int disp_size, const CLContext* ctx = nullptr);
bool Init(const CLContext* ctx);
void Run(void* left_img, void* right_img, void* output);
~StereoSGMCL();
private:
void initCL();
void census();
void mem_init();
void matching_cost();
void scan_cost();
void winner_takes_all();
void median();
void check_consistency_left();
private:
int width_, height_, disp_size_;
const CLContext* context_;
CLProgram* sgm_prog_;
// CLKernel* census_kernel_;
// CLKernel* matching_cost_kernel_;
// CLKernel* aggre_cost_top2down_kernel_;
// CLKernel* aggre_cost_down2top_kernel_;
// CLKernel* aggre_cost_left2right_kernel_;
// CLKernel* aggre_cost_right2left_kernel_;
// CLKernel* aggre_cost_topleft2downright_kernel_;
// CLKernel* aggre_cost_topright2downleft_kernel_;
// CLKernel* aggre_cost_downleft2topright_kernel_;
// CLKernel* aggre_cost_downright2topleft_kernel_;
CLKernel * m_census_kernel;
CLKernel * m_matching_cost_kernel_128;
CLKernel * m_compute_stereo_horizontal_dir_kernel_0;
CLKernel * m_compute_stereo_horizontal_dir_kernel_4;
CLKernel * m_compute_stereo_vertical_dir_kernel_2;
CLKernel * m_compute_stereo_vertical_dir_kernel_6;
CLKernel * m_compute_stereo_oblique_dir_kernel_1;
CLKernel * m_compute_stereo_oblique_dir_kernel_3;
CLKernel * m_compute_stereo_oblique_dir_kernel_5;
CLKernel * m_compute_stereo_oblique_dir_kernel_7;
CLKernel * m_winner_takes_all_kernel128;
CLKernel * m_check_consistency_left;
CLKernel * m_median_3x3;
CLKernel * m_copy_u8_to_u16;
CLKernel * m_clear_buffer;
CLBuffer * d_src_left,* d_src_right,* d_left, *d_right, *d_matching_cost,
*d_scost,* d_left_disparity,* d_right_disparity,
*d_tmp_left_disp, *d_tmp_right_disp;
};
#include "sgm_cl.inl"
}
#endif