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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "inputsMapCubemap.hpp"
#include "gpu/buffer.hpp"
#include "gpu/memcpy.hpp"
#include "gpu/core1/transform.hpp"
#include "libvideostitch/geometryDef.hpp"
#include "libvideostitch/logging.hpp"
#include "libvideostitch/inputDef.hpp"
//#define READBACKSETUPIMAGE
//#define READBACKSETUPIMAGEPERINPUT
#if defined(READBACKSETUPIMAGE) || defined(READBACKSETUPIMAGEPERINPUT)
#ifdef _MSC_VER
static const std::string DEBUG_FOLDER = "";
#else
static const std::string DEBUG_FOLDER = "/tmp/inputs/";
#endif
#include "cuda/error.hpp"
#include "util/pngutil.hpp"
#include "util/pnm.hpp"
#include "util/debugUtils.hpp"
#include "image/unpack.hpp"
#endif
namespace VideoStitch {
namespace Core {
Potential<InputsMapCubemap> InputsMapCubemap::create(const PanoDefinition& pano) {
std::unique_ptr<InputsMapCubemap> inputsMap;
inputsMap.reset(new InputsMapCubemap(pano));
Status status = inputsMap->allocateBuffers();
if (status.ok()) {
return Potential<InputsMapCubemap>(inputsMap.release());
} else {
return Potential<InputsMapCubemap>(status);
}
}
InputsMapCubemap::InputsMapCubemap(const PanoDefinition& pano) : length(pano.getLength()) {}
InputsMapCubemap::~InputsMapCubemap() {}
Status InputsMapCubemap::allocateBuffers() {
FAIL_RETURN(xPos.alloc(length * length, "Setup Buffer"));
FAIL_RETURN(xNeg.alloc(length * length, "Setup Buffer"));
FAIL_RETURN(yPos.alloc(length * length, "Setup Buffer"));
FAIL_RETURN(yNeg.alloc(length * length, "Setup Buffer"));
FAIL_RETURN(zPos.alloc(length * length, "Setup Buffer"));
FAIL_RETURN(zNeg.alloc(length * length, "Setup Buffer"));
return Status::OK();
}
Status InputsMapCubemap::compute(const std::map<readerid_t, Input::VideoReader*>& readers, const PanoDefinition& pano) {
FAIL_RETURN(GPU::memsetToZeroBlocking(xPos.borrow(), length * length * 4));
FAIL_RETURN(GPU::memsetToZeroBlocking(xNeg.borrow(), length * length * 4));
FAIL_RETURN(GPU::memsetToZeroBlocking(yPos.borrow(), length * length * 4));
FAIL_RETURN(GPU::memsetToZeroBlocking(yNeg.borrow(), length * length * 4));
FAIL_RETURN(GPU::memsetToZeroBlocking(zPos.borrow(), length * length * 4));
FAIL_RETURN(GPU::memsetToZeroBlocking(zNeg.borrow(), length * length * 4));
for (auto reader : readers) {
const InputDefinition& inputDef = pano.getInput(reader.second->id);
const size_t bufferSize = (size_t)(inputDef.getWidth() * inputDef.getHeight());
// Create mask buffer
GPU::UniqueBuffer<unsigned char> maskDevBuffer;
FAIL_RETURN(maskDevBuffer.alloc(bufferSize, "MaskSetup"));
const unsigned char* data = inputDef.getMaskPixelDataIfValid();
if (data && inputDef.deletesMaskedPixels()) {
FAIL_RETURN(GPU::memcpyBlocking(maskDevBuffer.borrow(), data, bufferSize));
} else {
FAIL_RETURN(GPU::memsetToZeroBlocking(maskDevBuffer.borrow(), bufferSize));
}
// Update assigned pixels
Transform* t = Transform::create(inputDef);
FAIL_RETURN(t->cubemapMap(xPos.borrow(), xNeg.borrow(), yPos.borrow(), yNeg.borrow(), zPos.borrow(), zNeg.borrow(),
pano, inputDef, reader.second->id, maskDevBuffer.borrow(),
pano.getProjection() == PanoProjection::EquiangularCubemap, GPU::Stream::getDefault()));
FAIL_RETURN(GPU::Stream::getDefault().synchronize());
delete t;
#ifdef READBACKSETUPIMAGEPERINPUT
{
{
const int64_t length = pano.getLength();
GPU::Stream::getDefault().synchronize();
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face+x_" << reader.second->id << ".png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), xPos.borrow_const(), length, length);
}
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face-x_" << reader.second->id << ".png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), xNeg.borrow_const(), length, length);
}
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face+y_" << reader.second->id << ".png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), yPos.borrow_const(), length, length);
}
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face-y_" << reader.second->id << ".png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), yNeg.borrow_const(), length, length);
}
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face+z_" << reader.second->id << ".png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), zPos.borrow_const(), length, length);
}
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face-z_" << reader.second->id << ".png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), zNeg.borrow_const(), length, length);
}
}
}
#endif
}
#ifdef READBACKSETUPIMAGE
{
{
const int64_t width = pano.getWidth();
GPU::Stream::getDefault().synchronize();
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face+x.png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), xPos.borrow_const(), width, width);
}
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face-x.png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), xNeg.borrow_const(), width, width);
}
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face+y.png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), yPos.borrow_const(), width, width);
}
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face-y.png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), yNeg.borrow_const(), width, width);
}
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face+z.png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), zPos.borrow_const(), width, width);
}
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup_face-z.png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), zNeg.borrow_const(), width, width);
}
}
}
#endif
return Status::OK();
}
} // namespace Core
} // namespace VideoStitch