HoviTron Video Pipeline
KernelCuda.cuh
1/* ----------------------
2* Copyright 2023 Université Libre de Bruxelles(ULB), Universidad Politécnica de Madrid(UPM), CREAL, Deutsches Zentrum für Luft - und Raumfahrt(DLR)
3
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at < http://www.apache.org/licenses/LICENSE-2.0%3E
7
8* Unless required by applicable law or agreed to in writing, software
9* distributed under the License is distributed on an "AS IS" BASIS,
10* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11* See the License for the specific language governing permissionsand
12* limitations under the License.
13---------------------- */
14
15
16#ifndef KERNEL_CUDA_H
17#define KERNEL_CUDA_H
18
19#include <cuda.h>
20#include <cuda_runtime.h>
21#include "device_launch_parameters.h"
22#include "surface_functions.h"
23#include "types.h"
24#include "HvtStreamingAPI.h"
25#include "math.h"
26
27#define unreachable() (unreachable_kernel())
28
29/*------------ CUDA kernel params*/
30const dim3 threadsperblock(512);
31const dim3 blockspergrid(30 * 16);
32/*-------------------------------*/
33
34/*------------ CUDA KERNEL*/
35/// <summary>
36/// Copy RGB data from CUDA array into a cudaSurfaceObject_t. IT IS A CUDA KERNEL.
37/// </summary>
38/// <param name="RGB_vulkan"> The cudaSurfaceObject_t receiving the data.</param>
39/// <param name="RGB_cuda"> The RGB CUDA array that will give data.</param>
40/// <param name="baseWidth"> The width of the CUDA array.</param>
41/// <param name="baseHeight"> The height of the CUDA array.</param>
42__global__ void copy_color(cudaSurfaceObject_t RGB_vulkan, uchar * RGB_cuda, size_t baseWidth, size_t baseHeight);
43/// <summary>
44/// Copy the depth from a CUDA array into a cudaSurfaceObject_t. IT IS A CUDA KERNEL.
45/// </summary>
46/// <param name="D_vulkan"> The cudaSurfaceObject_t that will receive data.</param>
47/// <param name="D_cuda"> The depth CUDA array that will give data.</param>
48/// <param name="baseWidth"> The width of the CUDA array.</param>
49/// <param name="baseHeight"> The height of the CUDA array.</param>
50__global__ void copy_depth(cudaSurfaceObject_t D_vulkan, float* D_cuda, size_t baseWidth, size_t baseHeight);
51/// <summary>
52/// Remove the pitch added by another instance by copying the data in a new array. IT IS A CUDA KERNEL.
53/// </summary>
54/// <typeparam name="T"> The type of the array data.</typeparam>
55/// <param name="data_in"> The data to remove the pitch.</param>
56/// <param name="data_out"> The CUDA array that is receiving data.</param>
57/// <param name="baseWidth"> The width of the CUDA array.</param>
58/// <param name="baseHeight"> The height of the CUDA array.</param>
59/// <param name="nbInRow"> Number of elements in one row.</param>
60/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
61template<typename T> __global__ void remove_pitch(T* data_in, T* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels);
62/// <summary>
63/// Convert a CUDA array of type USHORT into a CUDA array of type UCHAR. IT IS A CUDA KERNEL.
64/// </summary>
65/// <param name="data_in"> The data to convert.</param>
66/// <param name="data_out"> The CUDA array that is receiving data.</param>
67/// <param name="baseWidth"> The width of the CUDA array.</param>
68/// <param name="baseHeight"> The height of the CUDA array.</param>
69/// <param name="nbInRow"> Number of elements in one row.</param>
70/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
71__global__ void uShort_2_uChar(unsigned short* data_in, unsigned char* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels);
72/// <summary>
73/// Convert a CUDA array of type USHORT into a CUDA array of type FLOAT. IT IS A CUDA KERNEL.
74/// </summary>
75/// <param name="data_in"> The data to convert.</param>
76/// <param name="data_out"> The CUDA array that is receiving data.</param>
77/// <param name="baseWidth"> The width of the CUDA array.</param>
78/// <param name="baseHeight"> The height of the CUDA array.</param>
79/// <param name="nbInRow"> Number of elements in one row.</param>
80/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
81__global__ void uShort_2_Float(unsigned short* data_in, float* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels);
82/// <summary>
83/// Copy and scale a float CUDA array into another one. IT IS A CUDA KERNEL.
84/// </summary>
85/// <typeparam name="T"> The type of the array data.</typeparam>
86/// <param name="data_in"> The data to scale.</param>
87/// <param name="data_out"> The CUDA array that is receiving data.</param>
88/// <param name="baseWidth"> The width of the CUDA array.</param>
89/// <param name="baseHeight"> The height of the CUDA array.</param>
90/// <param name="nbInRow"> Number of elements in one row.</param>
91/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
92/// <param name="scale"> The scaling factor. </param>
93template<typename T> __global__ void scale_data_array(T* data_in, T* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels, float scale);
94/// <summary>
95/// Copy a float CUDA array into another one and add an offset to the second one. IT IS A CUDA KERNEL.
96/// </summary>
97/// <typeparam name="T"> The type of the array data.</typeparam>
98/// <param name="data_in"> The data to add an offset.</param>
99/// <param name="data_out"> The CUDA array that is receiving data.</param>
100/// <param name="baseWidth"> The width of the CUDA array.</param>
101/// <param name="baseHeight"> The height of the CUDA array.</param>
102/// <param name="nbInRow"> Number of elements in one row.</param>
103/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
104/// <param name="offset"> The offset to add. </param>
105template<typename T> __global__ void add_offset_data_array(T* data_in, T* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels, float offset);
106/// <summary>
107/// Copy, add an offset, and scale one channel of a float CUDA array into another float cuda array. IT IS A CUDA KERNEL.
108/// </summary>
109/// <typeparam name="T"> The type of the array data.</typeparam>
110/// <param name="data_in"> The data to scale.</param>
111/// <param name="data_out"> The CUDA array that is receiving data.</param>
112/// <param name="baseWidth"> The width of the CUDA array.</param>
113/// <param name="baseHeight"> The height of the CUDA array.</param>
114/// <param name="nbInRow"> Number of elements in one row.</param>
115/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
116/// <param name="nbOfChannel"> The index of the channel that will be copied.</param>
117/// <param name="scale"> The scaling factor. </param>
118/// <param name="offset"> The offset to add.</param>
119template<typename T> __global__ void scale_add_data_array_uchannel(T* data_in, T* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels, size_t nbOfChannel, float scale, float offset);
120/// <summary>
121/// Copy and scale one channel of a float CUDA array into another float cuda array. IT IS A CUDA KERNEL.
122/// </summary>
123/// <typeparam name="T"> The type of the array data.</typeparam>
124/// <param name="data_in"> The data to scale.</param>
125/// <param name="data_out"> The CUDA array that is receiving data.</param>
126/// <param name="baseWidth"> The width of the CUDA array.</param>
127/// <param name="baseHeight"> The height of the CUDA array.</param>
128/// <param name="nbInRow"> Number of elements in one row.</param>
129/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
130/// <param name="nbOfChannel"> The index of the channel that will be copied.</param>
131/// <param name="scale"> The scaling factor. </param>
132template<typename T> __global__ void scale_data_array_uchannel(T* data_in, T* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels, size_t nbOfChannel, float scale);
133/// <summary>
134/// Compute the average L1 norm in a patch for all pixels between the current depth and the previous depth. Then correct the current depth if the average error is lower than the treshold taking into account the adjsutment factor.
135/// </summary>
136/// <typeparam name="T"> The type of the array data.</typeparam>
137/// <param name="prev_depth"> The previous depth map to compare with.</param>
138/// <param name="curr_depth"> The current depth map that will be adjusted.</param>
139/// <param name="baseWidth"> The width of the CUDA array.</param>
140/// <param name="baseHeight"> The height of the CUDA array.</param>
141/// <param name="sizePatch"> The size of the patch where the comparison around the pixels will be done.</param>
142/// <param name="treshold"> The treshold value in meters.</param>
143/// <param name="adjustementFactor"> The factor where 1.0 will correct the current depth with only the previous one, 0.5 will correct equally the current depth with both the current and the previous depth, and 0.0 will apply no ccorection. </param>
144template<typename T> __global__ void temporal_consistency_adjustement(T* prev_depth, T* curr_depth, size_t baseWidth, size_t baseHeight, unsigned int sizePatch,float treshold, float adjustementFactor);
145
146/*------------------------*/
147
148/*---------- C++ launching CUDA kernel functions*/
149/// <summary>
150/// Launch the CUDA kernel "copy_color" on a specific CUDA stream.
151/// </summary>
152/// <param name="RGB_vulkan"> The cudaSurfaceObject_t receiving the data.</param>
153/// <param name="RGB_cuda"> The RGB CUDA array that will give data.</param>
154/// <param name="baseWidth"> The width of the CUDA array.</param>
155/// <param name="baseHeight"> The height of the CUDA array.</param>
156/// <param name="stream"> The stream where the kernel will execute.</param>
157void copyColor(cudaSurfaceObject_t RGB_vulkan, uchar* RGB_cuda, size_t baseWidth, size_t baseHeight, cudaStream_t& stream);
158/// <summary>
159/// Launch the CUDA kernel "copy_depth" on a specific CUDA stream.
160/// </summary>
161/// <param name="D_vulkan"> The cudaSurfaceObject_t that will receive data.</param>
162/// <param name="D_cuda"> The depth CUDA array that will give data.</param>
163/// <param name="baseWidth"> The width of the CUDA array.</param>
164/// <param name="baseHeight"> The height of the CUDA array.</param>
165/// <param name="stream"> The stream where the kernel will execute.</param>
166void copyDepth(cudaSurfaceObject_t D_vulkan, float* D_cuda, size_t baseWidth, size_t baseHeight, cudaStream_t& stream);
167/// <summary>
168/// Launch the CUDA kernel "remove_pitch".
169/// </summary>
170/// <typeparam name="T"> The type of the array data.</typeparam>
171/// <param name="data_in"> The data to remove the pitch.</param>
172/// <param name="data_out"> The CUDA array that is receiving data.</param>
173/// <param name="baseWidth"> The width of the CUDA array.</param>
174/// <param name="baseHeight"> The height of the CUDA array.</param>
175/// <param name="nbInRow"> Number of elements in one row.</param>
176/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
177/// <param name="stream"> The stream where the kernel will be executed.</param>
178template<typename T> void removePitch(T* data_in, T* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels, cudaStream_t& stream);
179/// <summary>
180/// Launch the CUDA kernel "uShort_2_uChar".
181/// </summary>
182/// <param name="data_in"> The data to convert.</param>
183/// <param name="data_out"> The CUDA array that is receiving data.</param>
184/// <param name="baseWidth"> The width of the CUDA array.</param>
185/// <param name="baseHeight"> The height of the CUDA array.</param>
186/// <param name="nbInRow"> Number of elements in one row.</param>
187/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
188/// <param name="stream"> The stream where the kernel will be executed.</param>
189void uShort2uChar(unsigned short* data_in, unsigned char* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels,cudaStream_t& stream);
190/// <summary>
191/// Launch the CUDA kernel "uShort_2_Float".
192/// </summary>
193/// <param name="data_in"> The data to convert.</param>
194/// <param name="data_out"> The CUDA array that is receiving data.</param>
195/// <param name="baseWidth"> The width of the CUDA array.</param>
196/// <param name="baseHeight"> The height of the CUDA array.</param>
197/// <param name="nbInRow"> Number of elements in one row.</param>
198/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
199/// <param name="stream"> The stream where the kernel will be executed.</param>
200void uShort2Float(unsigned short* data_in, float* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels, cudaStream_t& stream);
201/// <summary>
202/// Launch the CUDA kernel "scale_data_array".
203/// </summary>
204/// <typeparam name="T"> The type of the array data.</typeparam>
205/// <param name="data_in"> The data to scale.</param>
206/// <param name="data_out"> The CUDA array that is receiving data.</param>
207/// <param name="baseWidth"> The width of the CUDA array.</param>
208/// <param name="baseHeight"> The height of the CUDA array.</param>
209/// <param name="nbInRow"> Number of elements in one row.</param>
210/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
211/// <param name="scale"> The scaling factor. </param>
212/// <param name="stream"> The stream where the kernel will be executed. </param>
213template<typename T> void scaleDataArray(T* data_in, T* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels, float scale, cudaStream_t& stream);
214/// <summary>
215/// Launch the CUDA kernel "add_offset_data_array".
216/// </summary>
217/// <typeparam name="T"> The type of the array data.</typeparam>
218/// <param name="data_in"> The data to add an offset.</param>
219/// <param name="data_out"> The CUDA array that is receiving data.</param>
220/// <param name="baseWidth"> The width of the CUDA array.</param>
221/// <param name="baseHeight"> The height of the CUDA array.</param>
222/// <param name="nbInRow"> Number of elements in one row.</param>
223/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
224/// <param name="offset"> The offset to add. </param>
225/// <param name="stream"> The stream where the kernel will be executed. </param>
226template<typename T> void addOffsetDataArray(T* data_in, T* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels, float offset, cudaStream_t& stream);
227
228/// <summary>
229/// Launch the CUDA kernel "scale_data_array_uchannel".
230/// </summary>
231/// <typeparam name="T"> The type of the array data.</typeparam>
232/// <param name="data_in"> The data to scale.</param>
233/// <param name="data_out"> The CUDA array that is receiving data.</param>
234/// <param name="baseWidth"> The width of the CUDA array.</param>
235/// <param name="baseHeight"> The height of the CUDA array.</param>
236/// <param name="nbInRow"> Number of elements in one row.</param>
237/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
238/// <param name="nbOfChannel"> The index of the channel that will be copied.</param>
239/// <param name="scale"> The scaling factor. </param>
240/// <param name="stream"> The stream where the kernel will be executed.</param>
241template<typename T> void scaleDataArrayUChannel(T* data_in, T* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels, size_t nbOfChannel, float scale, cudaStream_t& stream);
242/// <summary>
243/// Launch the CUDA kernel "scale_add_data_array_uchannel".
244/// </summary>
245/// <typeparam name="T"> The type of the array data.</typeparam>
246/// <param name="data_in"> The data to scale.</param>
247/// <param name="data_out"> The CUDA array that is receiving data.</param>
248/// <param name="baseWidth"> The width of the CUDA array.</param>
249/// <param name="baseHeight"> The height of the CUDA array.</param>
250/// <param name="nbInRow"> Number of elements in one row.</param>
251/// <param name="channels"> Number of channels. (Ex: RGBA = 4).</param>
252/// <param name="nbOfChannel"> The index of the channel that will be copied.</param>
253/// <param name="scale"> The scaling factor. </param>
254/// <param name="offset"> The offset to add.</param>
255/// <param name="stream"> The stream where the kernel will be executed.</param>
256template<typename T> void scaleAddDataArrayUChannel(T* data_in, T* data_out, size_t baseWidth, size_t baseHeight, size_t nbInRow, size_t channels, size_t nbOfChannel, float scale, float offset, cudaStream_t& stream);
257/// <summary>
258/// Launch the kernel temporal_consistency_adjustement then copy the current depth into the previous depth memory.
259/// </summary>
260/// <typeparam name="T"> The type of the array data.</typeparam>
261/// <param name="prev_depth"> The previous depth map to compare with.</param>
262/// <param name="curr_depth"> The current depth map that will be adjusted.</param>
263/// <param name="baseWidth"> The width of the CUDA array.</param>
264/// <param name="baseHeight"> The height of the CUDA array.</param>
265/// <param name="sizePatch"> The size of the patch where the comparison around the pixels will be done.</param>
266/// <param name="treshold"> The treshold value in meters.</param>
267/// <param name="adjustementFactor"> The factor where 1.0 will correct the current depth with only the previous one, 0.5 will correct equally the current depth with both the current and the previous depth, and 0.0 will apply no ccorection. </param>
268/// <param name="stream"> The stream where the kernel will be executed. </param>
269template<typename T> void temporalConsistencyAdjustement(T* prev_depth, T* curr_depth, size_t baseWidth, size_t baseHeight, unsigned int sizePatch, float treshold, float adjustementFactor, cudaStream_t& stream);
270/*----------------------------------------------*/
271
272
273[[noreturn]] void unreachable_kernel();
274
275#endif // !KERNEL_CUDA_H