1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "gpu/coredepth/sweep.hpp"
#include "backend/common/coredepth/sphereSweepParams.h"
#include "../surface.hpp"
#include "gpu/memcpy.hpp"
#include "core/transformGeoParams.hpp"
#include "libvideostitch/geometryDef.hpp"
#include "libvideostitch/panoDef.hpp"
#include "backend/cuda/deviceBuffer.hpp"
#include "backend/cuda/deviceBuffer2D.hpp"
#include "backend/cuda/surface.hpp"
#include "backend/cuda/deviceStream.hpp"
#include "cuda/util.hpp"
#include "gpu/buffer.hpp"
#include "kernels/sphereSweepKernel.cu"
#include <math.h>
static const int CudaBlockSize = 16;
namespace VideoStitch {
namespace GPU {
static int numCall = 0;
PotentialValue<struct InputParams6> prepareInputParams(const Core::PanoDefinition& panoDef, int time,
float scale = 1.f) {
struct InputParams6 inputParamsArray;
for (videoreaderid_t videoInputID = 0; videoInputID < panoDef.numVideoInputs(); videoInputID++) {
const Core::InputDefinition& input = panoDef.getVideoInput(videoInputID);
const Core::GeometryDefinition geometry = input.getGeometries().at(time);
Core::TransformGeoParams params(input, geometry, panoDef);
if (geometry.hasDistortion()) {
return PotentialValue<struct InputParams6>({Origin::Stitcher, ErrType::ImplementationError,
"Sphere sweep does not handle distortion parameters in inputs"});
}
float2 center, iscale;
center.x = (float)input.getCenterX(geometry) / scale;
center.y = (float)input.getCenterY(geometry) / scale;
iscale.x = (float)geometry.getHorizontalFocal() / scale;
iscale.y = (float)geometry.getVerticalFocal() / scale;
InputParams& inputParams = inputParamsArray.params[videoInputID];
inputParams.distortion = params.getDistortion();
inputParams.transform = params.getPose();
inputParams.inverseTransform = params.getPoseInverse();
inputParams.scale = iscale;
inputParams.centerShift = center;
inputParams.texWidth = (int)(input.getWidth() / scale);
inputParams.texHeight = (int)(input.getHeight() / scale);
inputParams.cropLeft = (int)(input.getCropLeft() / scale);
inputParams.cropRight = (int)(input.getCropRight() / scale);
inputParams.cropTop = (int)(input.getCropTop() / scale);
inputParams.cropBottom = (int)(input.getCropBottom() / scale);
}
return PotentialValue<struct InputParams6>(inputParamsArray);
}
static read_only image2d_t getSurfaceFromMap(const videoreaderid_t index,
const std::map<videoreaderid_t, Core::SourceSurface*>& surfaces) {
return (surfaces.find(index) != surfaces.end()) ? surfaces.find(index)->second->pimpl->surface->get().texture() : 0;
}
Status splatInputWithDepthIntoPano(const Core::PanoDefinition& panoDef, Core::PanoSurface& pano,
const GPU::Surface& depthSurface,
const std::map<videoreaderid_t, Core::SourceSurface*>& inputSurfaces,
GPU::Stream stream) {
// TODO
int time = 0;
const videoreaderid_t inputID = 0;
Buffer<uint32_t> panoBuffer = pano.pimpl->buffer;
float2 pscale;
pscale.x = Core::TransformGeoParams::computePanoScale(Core::PanoProjection::Equirectangular, pano.getWidth(), 360.f);
pscale.y =
2 * Core::TransformGeoParams::computePanoScale(Core::PanoProjection::Equirectangular, pano.getHeight(), 360.f);
auto potInputParamsArray = prepareInputParams(panoDef, time);
FAIL_RETURN(potInputParamsArray.status());
const InputParams& referenceInput = potInputParamsArray.value().params[inputID];
const float offset = cosf(numCall++ / 20.f * (float)M_PI / 2.f) * 0.2f;
const dim3 dimBlock(CudaBlockSize, CudaBlockSize, 1);
const dim3 dimGrid((unsigned)Cuda::ceilDiv(referenceInput.texWidth, dimBlock.x),
(unsigned)Cuda::ceilDiv(referenceInput.texHeight, dimBlock.y), 1);
Core::splatInputWithDepthIntoPano<<<dimGrid, dimBlock, 0, stream.get()>>>(
panoBuffer.get(), (unsigned)pano.getWidth(), (unsigned)pano.getHeight(), pscale,
getSurfaceFromMap(inputID, inputSurfaces), depthSurface.get().surface(), referenceInput, panoDef.numVideoInputs(),
offset);
Logger::get(Logger::Info) << "SphereSweep frame " << numCall << std::endl;
return Status::OK();
}
Status sphereSweepInput(videoreaderid_t sourceID, int frame, GPU::Surface& dst,
const std::map<videoreaderid_t, Core::SourceSurface*>& inputSurfaces,
const Core::PanoDefinition& panoDef, GPU::Stream& stream, const float scale) {
// debug command line pipeline: just copy input surface to output surface
// via temporary buffer as we don't have a surface->surface copy function
// auto tmpBuf = GPU::uniqueBuffer<uint32_t>(inputDef.getWidth() * inputDef.getHeight(), "tmp bfu");
// GPU::memcpyAsync(tmpBuf.borrow(), *gpuSurf, stream);
// GPU::memcpyAsync(dst, tmpBuf.borrow_const(), stream);
// stream.synchronize()
if (panoDef.numVideoInputs() > maxDepthInputs()) {
return Status{Origin::Stitcher, ErrType::ImplementationError,
"Sphere sweep only implemented up to 6 inputs (hardcoded)"};
}
auto potInputParamsArray = prepareInputParams(panoDef, frame, scale);
FAIL_RETURN(potInputParamsArray.status());
const struct InputParams6 inputParamsArray = potInputParamsArray.releaseValue();
const InputParams& referenceInput = inputParamsArray.params[sourceID];
// Running a kernel that takes > 1s destabilizes the system
// (Display manager resets or kernel panic)
// As the current version is not optimised and works at full resolution it can take several seconds to complete
// --> Tile the work. Each tile should complete in less than 1 second.
const int numBlocks = 16;
// Make sure texture width is a multiple of numBlocks
const int paddedTexWidth = (int)Cuda::ceilDiv(referenceInput.texWidth, numBlocks) * numBlocks;
const int paddedTexHeight = (int)Cuda::ceilDiv(referenceInput.texHeight, numBlocks) * numBlocks;
for (int cx = 0; cx < numBlocks; cx++) {
for (int cy = 0; cy < numBlocks; cy++) {
const dim3 dimBlock(CudaBlockSize, CudaBlockSize, 1);
const dim3 dimGrid((unsigned)Cuda::ceilDiv(paddedTexWidth / numBlocks, dimBlock.x),
(unsigned)Cuda::ceilDiv(paddedTexHeight / numBlocks, dimBlock.y), 1);
Core::sphereSweepInputKernel<<<dimGrid, dimBlock, 0, stream.get()>>>(
dst.get().surface(), (unsigned)dst.width(), (unsigned)dst.height(), nullptr,
getSurfaceFromMap(0, inputSurfaces), getSurfaceFromMap(1, inputSurfaces), getSurfaceFromMap(2, inputSurfaces),
getSurfaceFromMap(3, inputSurfaces), getSurfaceFromMap(4, inputSurfaces), getSurfaceFromMap(5, inputSurfaces),
inputParamsArray, sourceID, panoDef.numVideoInputs(), cx, cy, paddedTexWidth / numBlocks,
paddedTexHeight / numBlocks);
// Force synchronization after tile computation for system stability
stream.synchronize();
}
}
Logger::get(Logger::Info) << "SphereSweep frame " << frame << " input " << sourceID << std::endl;
return Status::OK();
}
Status sphereSweepInputSGM(videoreaderid_t sourceID, int frame, GPU::Surface& dst,
GPU::HostBuffer<unsigned short>& hostCostVolume,
const std::map<videoreaderid_t, Core::SourceSurface*>& inputSurfaces,
const Core::PanoDefinition& panoDef, GPU::Stream& stream, const float scale) {
// debug command line pipeline: just copy input surface to output surface
// via temporary buffer as we don't have a surface->surface copy function
// auto tmpBuf = GPU::uniqueBuffer<uint32_t>(inputDef.getWidth() * inputDef.getHeight(), "tmp bfu");
// GPU::memcpyAsync(tmpBuf.borrow(), *gpuSurf, stream);
// GPU::memcpyAsync(dst, tmpBuf.borrow_const(), stream);
// stream.synchronize()
if (panoDef.numVideoInputs() > maxDepthInputs()) {
return Status{Origin::Stitcher, ErrType::ImplementationError,
"Sphere sweep only implemented up to 6 inputs (hardcoded)"};
}
auto potInputParamsArray = prepareInputParams(panoDef, frame, scale);
FAIL_RETURN(potInputParamsArray.status());
const struct InputParams6 inputParamsArray = potInputParamsArray.releaseValue();
const InputParams& referenceInput = inputParamsArray.params[sourceID];
GPU::UniqueBuffer<unsigned short> devCostVolume;
PROPAGATE_FAILURE_STATUS(
devCostVolume.alloc(referenceInput.texWidth * referenceInput.texHeight * numSphereSweeps(), "SGM score volume"));
// Running a kernel that takes > 1s destabilizes the system
// (Display manager resets or kernel panic)
// As the current version is not optimised and works at full resolution it can take several seconds to complete
// --> Tile the work. Each tile should complete in less than 1 second.
const int numBlocks = 16;
// Make sure texture width is a multiple of numBlocks
const int paddedTexWidth = (int)Cuda::ceilDiv(referenceInput.texWidth, numBlocks) * numBlocks;
const int paddedTexHeight = (int)Cuda::ceilDiv(referenceInput.texHeight, numBlocks) * numBlocks;
for (int cx = 0; cx < numBlocks; cx++) {
for (int cy = 0; cy < numBlocks; cy++) {
const dim3 dimBlock(CudaBlockSize, CudaBlockSize, 1);
const dim3 dimGrid((unsigned)Cuda::ceilDiv(paddedTexWidth / numBlocks, dimBlock.x),
(unsigned)Cuda::ceilDiv(paddedTexHeight / numBlocks, dimBlock.y), 1);
Core::sphereSweepInputKernel<<<dimGrid, dimBlock, 0, stream.get()>>>(
dst.get().surface(), (unsigned)dst.width(), (unsigned)dst.height(), devCostVolume.borrow().devicePtr(),
getSurfaceFromMap(0, inputSurfaces), getSurfaceFromMap(1, inputSurfaces), getSurfaceFromMap(2, inputSurfaces),
getSurfaceFromMap(3, inputSurfaces), getSurfaceFromMap(4, inputSurfaces), getSurfaceFromMap(5, inputSurfaces),
inputParamsArray, sourceID, panoDef.numVideoInputs(), cx, cy, paddedTexWidth / numBlocks,
paddedTexHeight / numBlocks);
// Force synchronization after tile computation for system stability
FAIL_RETURN(stream.synchronize());
}
}
Logger::get(Logger::Info) << "SphereSweep frame " << frame << " input " << sourceID << std::endl;
// copy scoreVolume to host
FAIL_RETURN(GPU::memcpyAsync(
hostCostVolume.hostPtr(), devCostVolume.borrow_const(),
referenceInput.texWidth * referenceInput.texHeight * numSphereSweeps() * sizeof(unsigned short), stream));
stream.synchronize();
Logger::get(Logger::Info) << "SphereSweep score volume copied back to host" << std::endl;
return Status::OK();
}
Status sphereSweepInputDisparityToDepth(videoreaderid_t sourceID, int frame, GPU::Surface& dst,
GPU::HostBuffer<short>& hostDisparity, bool useHostDisparity,
const std::map<videoreaderid_t, Core::SourceSurface*>& surfaces,
const Core::PanoDefinition& panoDef, GPU::Stream& stream, const float scale) {
if (panoDef.numVideoInputs() > maxDepthInputs()) {
return Status{Origin::Stitcher, ErrType::ImplementationError,
"Sphere sweep only implemented up to 6 inputs (hardcoded)"};
}
auto potInputParamsArray = prepareInputParams(panoDef, frame, scale);
FAIL_RETURN(potInputParamsArray.status());
const struct InputParams6 inputParamsArray = potInputParamsArray.releaseValue();
const InputParams& referenceInput = inputParamsArray.params[sourceID];
// copy host disparity to GPU buffer
PotentialValue<GPU::Buffer<short>> potDevBuf =
GPU::Buffer<short>::allocate(referenceInput.texWidth * referenceInput.texHeight, "SGM output disparity");
FAIL_RETURN(potDevBuf.status());
GPU::Buffer<short> devDisparity(potDevBuf.releaseValue());
FAIL_RETURN(GPU::memcpyAsync(devDisparity, hostDisparity.hostPtr(),
referenceInput.texWidth * referenceInput.texHeight * sizeof(short), stream));
stream.synchronize();
// Running a kernel that takes > 1s destabilizes the system
// (Display manager resets or kernel panic)
// As the current version is not optimised and works at full resolution it can take several seconds to complete
// --> Tile the work. Each tile should complete in less than 1 second.
const int numBlocks = 4;
// Make sure texture width is a multiple of numBlocks
const int paddedTexWidth = (int)Cuda::ceilDiv(referenceInput.texWidth, numBlocks) * numBlocks;
const int paddedTexHeight = (int)Cuda::ceilDiv(referenceInput.texHeight, numBlocks) * numBlocks;
for (int cx = 0; cx < numBlocks; cx++) {
for (int cy = 0; cy < numBlocks; cy++) {
const dim3 dimBlock(CudaBlockSize, CudaBlockSize, 1);
const dim3 dimGrid((unsigned)Cuda::ceilDiv(paddedTexWidth / numBlocks, dimBlock.x),
(unsigned)Cuda::ceilDiv(paddedTexHeight / numBlocks, dimBlock.y), 1);
Core::sphereSweepInputDisparityToDepthKernel<<<dimGrid, dimBlock, 0, stream.get()>>>(
dst.get().surface(), (unsigned)dst.width(), (unsigned)dst.height(),
(useHostDisparity) ? devDisparity.devicePtr() : nullptr, getSurfaceFromMap(sourceID, surfaces), cx, cy,
paddedTexWidth / numBlocks, paddedTexHeight / numBlocks);
// Force synchronization after tile computation for system stability
stream.synchronize();
}
}
Logger::get(Logger::Info) << "SphereSweep disparity to depth on input " << sourceID << std::endl;
FAIL_RETURN(devDisparity.release());
return Status();
}
Status sphereSweepInputStep(videoreaderid_t sourceID, int frame, GPU::Surface& dst, GPU::Surface& depthSrcNextLevel,
const std::map<videoreaderid_t, Core::SourceSurface*>& inputSurfaces,
const Core::PanoDefinition& panoDef, GPU::Stream& stream, const float scale) {
// debug command line pipeline: just copy input surface to output surface
// via temporary buffer as we don't have a surface->surface copy function
// auto tmpBuf = GPU::uniqueBuffer<uint32_t>(inputDef.getWidth() * inputDef.getHeight(), "tmp bfu");
// GPU::memcpyAsync(tmpBuf.borrow(), *gpuSurf, stream);
// GPU::memcpyAsync(dst, tmpBuf.borrow_const(), stream);
// stream.synchronize()
if (panoDef.numVideoInputs() > 6) {
return Status{Origin::Stitcher, ErrType::ImplementationError,
"Sphere sweep only implemented for 6 inputs maximum (hardcoded)"};
}
auto potInputParamsArray = prepareInputParams(panoDef, frame, scale);
FAIL_RETURN(potInputParamsArray.status());
const struct InputParams6 inputParamsArray = potInputParamsArray.releaseValue();
const InputParams& referenceInput = inputParamsArray.params[sourceID];
// Running a kernel that takes > 1s destabilizes the system
// (Display manager resets or kernel panic)
// As the current version is not optimised and works at full resolution it can take several seconds to complete
// --> Tile the work. Each tile should complete in less than 1 second.
const int numBlocks = 4;
const int paddedTexWidth = (int)Cuda::ceilDiv(referenceInput.texWidth, numBlocks) * numBlocks;
const int paddedTexHeight = (int)Cuda::ceilDiv(referenceInput.texHeight, numBlocks) * numBlocks;
// search around best depth from lower level pyramid
// search span is in log2, covers [log2(bestDepth) - searchSpan, log2(bestDepth) + searchSpan]
const float searchSpan = scale / 8.f; // decrease searched depths on upper levels
for (int cx = 0; cx < numBlocks; cx++) {
for (int cy = 0; cy < numBlocks; cy++) {
const dim3 dimBlock(CudaBlockSize, CudaBlockSize, 1);
const dim3 dimGrid((unsigned)Cuda::ceilDiv(paddedTexWidth / numBlocks, dimBlock.x),
(unsigned)Cuda::ceilDiv(paddedTexHeight / numBlocks, dimBlock.y), 1);
Core::sphereSweepInputKernelStep<<<dimGrid, dimBlock, 0, stream.get()>>>(
dst.get().surface(), (unsigned)dst.width(), (unsigned)dst.height(), depthSrcNextLevel.get().surface(),
(unsigned)depthSrcNextLevel.width(), (unsigned)depthSrcNextLevel.height(),
getSurfaceFromMap(0, inputSurfaces), getSurfaceFromMap(1, inputSurfaces), getSurfaceFromMap(2, inputSurfaces),
getSurfaceFromMap(3, inputSurfaces), getSurfaceFromMap(4, inputSurfaces), getSurfaceFromMap(5, inputSurfaces),
inputParamsArray, sourceID, panoDef.numVideoInputs(), cx, cy, paddedTexWidth / numBlocks,
paddedTexHeight / numBlocks, searchSpan);
// Force synchronization after tile computation for system stability
stream.synchronize();
}
}
Logger::get(Logger::Info) << "SphereSweep step frame " << frame << " input " << sourceID
<< " search span: " << searchSpan << std::endl;
return Status::OK();
}
int numSphereSweeps() { return numSphereScales; }
int maxDepthInputs() { return NUM_INPUTS; }
} // namespace GPU
} // namespace VideoStitch