Skip to content
Snippets Groups Projects
Commit 185b9d7e authored by Sebastian Hahta's avatar Sebastian Hahta
Browse files

updated interface with automatic cpu/gpu transfer

parent f90f6872
No related branches found
No related tags found
2 merge requests!105CUDA optical flow smoothing,!103feature/frame class
Pipeline #13267 passed
This commit is part of merge request !105. Comments created here will be created in the context of that merge request.
...@@ -27,55 +27,57 @@ public: ...@@ -27,55 +27,57 @@ public:
return available_[_channelIdx(channel)]; return available_[_channelIdx(channel)];
} }
/* @brief Get reference to the channel data. /* @brief get reference to the channel contents
* @param Channel type * @param channel type
* @param Output parameter * @returns const reference to channel data
* @param User of the method sets data. NOTE: Only works on unused
* channels or if reset() was called previously (TODO).
* @returns True, if output parameter contains valid data
* *
* Methods automatically copy between host/gpu if the data is only available * Methods automatically copy between host/gpu if the data is only available
* in the other. Results are cached. * in the other. Results are cached. Result is valid only if hasChannel() is
* true.
*/ */
bool getChannel(const ftl::rgbd::channel_t& channel, cv::Mat& out, bool set=false) const cv::Mat& getChannel(const ftl::rgbd::channel_t& channel)
{ {
auto idx = _channelIdx(channel); auto idx = _channelIdx(channel);
bool retval = available_[idx] & mask_host; if (!(available_[idx] & mask_host))
if (!retval)
{ {
if (available_[idx] & mask_gpu) if (available_[idx] & mask_gpu)
{ {
channels_gpu_[idx].download(channels_host_[idx]); channels_gpu_[idx].download(channels_host_[idx]);
retval = true; available_[idx] |= mask_host;
set = true;
} }
} }
if (set) { available_[idx] |= mask_host; } return channels_host_[idx];
}
out = channels_host_[idx]; cv::Mat& setChannel(const ftl::rgbd::channel_t& channel)
return retval; {
auto idx = _channelIdx(channel);
available_[idx] = mask_host;
return channels_host_[idx];
} }
bool getChannel(const ftl::rgbd::channel_t& channel, cv::cuda::GpuMat& out, bool set=false) const cv::cuda::GpuMat& getChannelGpu(const ftl::rgbd::channel_t& channel)
{ {
auto idx = _channelIdx(channel); auto idx = _channelIdx(channel);
bool retval = available_[idx] & mask_gpu; if (!(available_[idx] & mask_gpu))
if (!retval)
{ {
if (available_[idx] & mask_host) if (available_[idx] & mask_host)
{ {
channels_gpu_[idx].upload(channels_host_[idx]); channels_gpu_[idx].upload(channels_host_[idx]);
retval = true; available_[idx] |= mask_gpu;
set = true;
} }
} }
if (set) { available_[idx] |= mask_host; } return channels_gpu_[idx];
}
out = channels_gpu_[idx]; cv::cuda::GpuMat& setChannelGpu(const ftl::rgbd::channel_t& channel)
return retval; {
auto idx = _channelIdx(channel);
available_[idx] = mask_gpu;
return channels_gpu_[idx];
} }
private: private:
...@@ -94,7 +96,8 @@ private: ...@@ -94,7 +96,8 @@ private:
case kChanConfidence: return 7; case kChanConfidence: return 7;
case kChanFlow: return 8; case kChanFlow: return 8;
case kChanEnergy: return 9; case kChanEnergy: return 9;
default: return 0; // should not happen (error) // should not happen (error); returned index is kChanNone
default: return 0;
} }
} }
......
...@@ -173,9 +173,8 @@ bool StereoVideoSource::capture(int64_t ts) { ...@@ -173,9 +173,8 @@ bool StereoVideoSource::capture(int64_t ts) {
bool StereoVideoSource::retrieve() { bool StereoVideoSource::retrieve() {
auto &frame = frames_[0]; auto &frame = frames_[0];
frame.reset(); frame.reset();
cv::cuda::GpuMat left, right; auto &left = frame.setChannelGpu(ftl::rgbd::kChanLeft);
frame.getChannel(ftl::rgbd::kChanLeft, left, true); auto &right = frame.setChannelGpu(ftl::rgbd::kChanRight);
frame.getChannel(ftl::rgbd::kChanRight, right, true);
lsrc_->get(left, right, calib_, stream2_); lsrc_->get(left, right, calib_, stream2_);
#ifdef HAVE_OPTFLOW #ifdef HAVE_OPTFLOW
...@@ -209,18 +208,15 @@ void StereoVideoSource::swap() { ...@@ -209,18 +208,15 @@ void StereoVideoSource::swap() {
bool StereoVideoSource::compute(int n, int b) { bool StereoVideoSource::compute(int n, int b) {
auto &frame = frames_[1]; auto &frame = frames_[1];
cv::cuda::GpuMat left, right, disp, depth; auto &left = frame.getChannelGpu(ftl::rgbd::kChanLeft);
frame.getChannel(ftl::rgbd::kChanLeft, left); auto &right = frame.getChannelGpu(ftl::rgbd::kChanRight);
frame.getChannel(ftl::rgbd::kChanRight, right);
const ftl::rgbd::channel_t chan = host_->getChannel(); const ftl::rgbd::channel_t chan = host_->getChannel();
if (left.empty() || right.empty()) return false; if (left.empty() || right.empty()) return false;
if (chan == ftl::rgbd::kChanDepth) { if (chan == ftl::rgbd::kChanDepth) {
frame.getChannel(ftl::rgbd::kChanDepth, depth, true); auto &depth = frame.setChannelGpu(ftl::rgbd::kChanDepth);
frame.getChannel(ftl::rgbd::kChanDisparity, disp, true); auto &disp = frame.setChannelGpu(ftl::rgbd::kChanDisparity);
if (depth.empty()) depth = cv::cuda::GpuMat(left.size(), CV_32FC1); if (depth.empty()) depth = cv::cuda::GpuMat(left.size(), CV_32FC1);
if (disp.empty()) disp = cv::cuda::GpuMat(left.size(), CV_32FC1); if (disp.empty()) disp = cv::cuda::GpuMat(left.size(), CV_32FC1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment