-
Notifications
You must be signed in to change notification settings - Fork 2k
zero copy nitros ros #3537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ashrafk93
wants to merge
2
commits into
realsenseai:ros2-development
Choose a base branch
from
ashrafk93:ashraf/zerocopy-nitors
base: ros2-development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
zero copy nitros ros #3537
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright 2026 RealSense, Inc. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #ifdef BUILD_WITH_NITROS | ||
|
|
||
| #include <rclcpp/rclcpp.hpp> | ||
| #include <std_msgs/msg/header.hpp> | ||
| #include <functional> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| namespace realsense2_camera | ||
| { | ||
| // Thin, GXF-free wrapper around nvidia::isaac_ros::nitros::ManagedNitrosPublisher<NitrosImage>. | ||
| // The heavy Isaac ROS / GXF / CUDA headers are confined to nitros_image_publisher.cpp, so the | ||
| // rest of the wrapper (base_realsense_node.*) only sees this handle and never pulls in NITROS. | ||
| // | ||
| // NOTE on ownership (Isaac ROS release-3.2): NitrosImageBuilder::WithGpuData() hands the pointer | ||
| // to a GXF VideoBuffer whose release callback calls cudaFree(). GXF therefore OWNS the buffer and | ||
| // frees it once downstream is done. We must NOT pass the librealsense frame-pool pointer directly | ||
| // (GXF would cudaFree memory the SDK owns). Instead publish() cudaMalloc's a fresh device buffer, | ||
| // device-to-device copies the frame's GPU pixels into it, and hands that to GXF. This still avoids | ||
| // the GPU->CPU->GPU round-trip to the perception graph (the goal); the D2D copy is the cost of | ||
| // release-3.2's owning model (release-3.4+/main add WithReleaseCallback for a true alias). | ||
| class NitrosImagePublisher | ||
| { | ||
| public: | ||
| // nitros_format is a NITROS supported-type name (e.g. "nitros_image_rgb8"). | ||
| NitrosImagePublisher(rclcpp::Node * node, const std::string & topic, const std::string & nitros_format); | ||
| ~NitrosImagePublisher(); | ||
|
|
||
| // gpu_src : CUDA device pointer to the frame pixels (aliases the SDK frame buffer). | ||
| // size_bytes: number of bytes to copy (width * height * bytes_per_pixel). | ||
| // encoding : sensor_msgs::image_encodings string matching nitros_format (e.g. "rgb8"). | ||
| // The frame's pixels are D2D-copied into a GXF-owned buffer; the caller need not keep the | ||
| // frame alive after this returns (the copy is synchronous). | ||
| void publish(const void * gpu_src, | ||
| uint32_t width, | ||
| uint32_t height, | ||
| size_t size_bytes, | ||
| const std::string & encoding, | ||
| const std_msgs::msg::Header & header); | ||
|
|
||
| private: | ||
| struct Impl; | ||
| std::unique_ptr<Impl> _impl; | ||
| }; | ||
| } // namespace realsense2_camera | ||
|
|
||
| #endif // BUILD_WITH_NITROS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright 2026 RealSense, Inc. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifdef BUILD_WITH_NITROS | ||
|
|
||
| #include "nitros_image_publisher.h" | ||
|
|
||
| #include <isaac_ros_managed_nitros/managed_nitros_publisher.hpp> | ||
| #include <isaac_ros_nitros_image_type/nitros_image.hpp> | ||
| #include <isaac_ros_nitros_image_type/nitros_image_builder.hpp> | ||
|
|
||
| #include <cuda_runtime.h> | ||
| #include <exception> | ||
| #include <utility> | ||
|
|
||
| namespace realsense2_camera | ||
| { | ||
| using nvidia::isaac_ros::nitros::ManagedNitrosPublisher; | ||
| using nvidia::isaac_ros::nitros::NitrosImage; | ||
| using nvidia::isaac_ros::nitros::NitrosImageBuilder; | ||
|
|
||
| struct NitrosImagePublisher::Impl | ||
| { | ||
| std::shared_ptr<ManagedNitrosPublisher<NitrosImage>> pub; | ||
| }; | ||
|
|
||
| NitrosImagePublisher::NitrosImagePublisher( | ||
| rclcpp::Node * node, const std::string & topic, const std::string & nitros_format) | ||
| : _impl(std::make_unique<Impl>()) | ||
| { | ||
| // ManagedNitrosPublisher performs REP-2007/2009 type negotiation internally; we only | ||
| // choose the compatible data format (e.g. "nitros_image_rgb8"). | ||
| _impl->pub = std::make_shared<ManagedNitrosPublisher<NitrosImage>>(node, topic, nitros_format); | ||
| } | ||
|
|
||
| NitrosImagePublisher::~NitrosImagePublisher() = default; | ||
|
|
||
| void NitrosImagePublisher::publish( | ||
| const void * gpu_src, | ||
| uint32_t width, | ||
| uint32_t height, | ||
| size_t size_bytes, | ||
| const std::string & encoding, | ||
| const std_msgs::msg::Header & header) | ||
| { | ||
| // GXF will cudaFree() this buffer once downstream is done, so it must be a fresh cudaMalloc. | ||
| void * dev = nullptr; | ||
| cudaError_t err = cudaMalloc(&dev, size_bytes); | ||
| if (err != cudaSuccess) { | ||
| RCLCPP_WARN( | ||
| rclcpp::get_logger("NitrosImagePublisher"), | ||
| "cudaMalloc(%zu) failed: %s; dropping NITROS frame", size_bytes, cudaGetErrorString(err)); | ||
| return; | ||
| } | ||
| // Synchronous device-to-device copy from the frame's GPU pixels (cudaMemcpyDefault lets UVA | ||
| // resolve the source, which may be device-mapped pinned host memory on a zero-copy build). | ||
| err = cudaMemcpy(dev, gpu_src, size_bytes, cudaMemcpyDefault); | ||
| if (err != cudaSuccess) { | ||
| RCLCPP_WARN( | ||
| rclcpp::get_logger("NitrosImagePublisher"), | ||
| "cudaMemcpy(%zu) failed: %s; dropping NITROS frame", size_bytes, cudaGetErrorString(err)); | ||
| cudaFree(dev); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| NitrosImage img = NitrosImageBuilder() | ||
| .WithHeader(header) | ||
| .WithEncoding(encoding) | ||
| .WithDimensions(height, width) | ||
| .WithGpuData(dev) // ownership transfers to GXF (frees via cudaFree on release) | ||
| .Build(); | ||
| _impl->pub->publish(std::move(img)); | ||
| } catch (const std::exception & e) { | ||
| // Build() throws (e.g. odd dimensions / unsupported encoding) before taking ownership. | ||
| cudaFree(dev); | ||
| RCLCPP_WARN(rclcpp::get_logger("NitrosImagePublisher"), "NitrosImage Build failed: %s", e.what()); | ||
| } | ||
| } | ||
| } // namespace realsense2_camera | ||
|
|
||
| #endif // BUILD_WITH_NITROS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getNitrosImageFormat duplicates rs2->ROS encoding mappings already defined in initializeFormatsMaps; consolidate the mapping to a single source to avoid divergent updates.
Show fix
Details
✨ AI Reasoning
The change added a new function that maps several rs2 pixel formats to sensor_msgs encodings and bytes-per-pixel. The same rs2->encoding mappings already exist in initializeFormatsMaps earlier in the file. This is an introduced, localized duplication of mapping logic: updates to supported formats would need changes in two places. Consolidation into a single authoritative mapping would avoid divergence and reduce maintenance burden.
Reply
@AikidoSec feedback: [FEEDBACK]to get better review comments in the future.Reply
@AikidoSec ignore: [REASON]to ignore this issue.More info