Skip to content
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

Add replay renderer bindings to get CUDA device pointers. #2011

Merged
merged 6 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/esp/bindings/SimBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,10 @@ void initSimBindings(py::module& m) {
[](AbstractReplayRenderer& self, const std::string& filePath) {
self.preloadFile(filePath);
},
R"(Load an composite file that the renderer will use in-place of simulation assets to improve memory usage and performance.)")
.def("environment_count", &AbstractReplayRenderer::environmentCount,
"Get the batch size.")
R"(Load a composite file that the renderer will use in-place of simulation assets to improve memory usage and performance.)")
.def_property_readonly("environment_count",
&AbstractReplayRenderer::environmentCount,
"Get the batch size.")
.def("sensor_size", &AbstractReplayRenderer::sensorSize,
"Get the resolution of a sensor.")
.def("clear_environment", &AbstractReplayRenderer::clearEnvironment,
Expand All @@ -420,7 +421,7 @@ void initSimBindings(py::module& m) {
static_cast<void (AbstractReplayRenderer::*)(
Magnum::GL::AbstractFramebuffer&)>(
&AbstractReplayRenderer::render),
R"(Render all sensors onto the main framebuffer.)")
R"(Render all sensors onto the specified framebuffer.)")
.def(
"set_sensor_transforms_from_keyframe",
&AbstractReplayRenderer::setSensorTransformsFromKeyframe,
Expand All @@ -430,9 +431,21 @@ void initSimBindings(py::module& m) {
.def("set_environment_keyframe",
&AbstractReplayRenderer::setEnvironmentKeyframe,
R"(Set the keyframe for a specific environment.)")
.def_static("environment_grid_size",
&AbstractReplayRenderer::environmentGridSize,
R"(Dimensions of the environment grid.)");
.def_static(
"environment_grid_size", &AbstractReplayRenderer::environmentGridSize,
R"(Get the dimensions (tile counts) of the environment grid.)")
.def(
0mdc marked this conversation as resolved.
Show resolved Hide resolved
"cuda_color_buffer_device_pointer",
[](AbstractReplayRenderer& self) {
return py::capsule(self.getCudaColorBufferDevicePointer());
},
R"(Retrieve the color buffer as a CUDA device pointer.)")
.def(
"cuda_depth_buffer_device_pointer",
[](AbstractReplayRenderer& self) {
return py::capsule(self.getCudaColorBufferDevicePointer());
},
R"(Retrieve the depth buffer as a CUDA device pointer.)");
}

} // namespace sim
Expand Down
12 changes: 11 additions & 1 deletion src/esp/sim/AbstractReplayRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

Expand Down Expand Up @@ -104,5 +104,15 @@ void AbstractReplayRenderer::render(
return doRender(framebuffer);
}

const void* AbstractReplayRenderer::getCudaColorBufferDevicePointer() {
ESP_ERROR() << "CUDA device pointer only available with the batch renderer.";
return nullptr;
}

const void* AbstractReplayRenderer::getCudaDepthBufferDevicePointer() {
ESP_ERROR() << "CUDA device pointer only available with the batch renderer.";
return nullptr;
}

} // namespace sim
} // namespace esp
8 changes: 7 additions & 1 deletion src/esp/sim/AbstractReplayRenderer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

Expand Down Expand Up @@ -102,6 +102,12 @@ class AbstractReplayRenderer {
// Assumes the framebuffer color & depth is cleared
void render(Magnum::GL::AbstractFramebuffer& framebuffer);

// Retrieve the color buffer as a CUDA device pointer. */
virtual const void* getCudaColorBufferDevicePointer();

// Retrieve the depth buffer as a CUDA device pointer. */
virtual const void* getCudaDepthBufferDevicePointer();

private:
/* Implementation of all public API is in the private do*() functions,
similarly to how e.g. Magnum plugin interfaces work. The public API does
Expand Down
33 changes: 32 additions & 1 deletion src/esp/sim/BatchReplayRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

Expand Down Expand Up @@ -244,5 +244,36 @@ void BatchReplayRenderer::doRender(
renderer_->draw(framebuffer);
}

const void* BatchReplayRenderer::getCudaColorBufferDevicePointer() {
#ifdef ESP_BUILD_WITH_CUDA
CORRADE_ASSERT(standalone_,
"ReplayBatchRenderer::colorCudaBufferDevicePointer(): can use "
"this function only "
"with a standalone renderer",
nullptr);
return static_cast<gfx_batch::RendererStandalone&>(*renderer_)
.colorCudaBufferDevicePointer();
#else
ESP_ERROR() << "Failed to retrieve device pointer because CUDA is not "
"available in this build.";
return nullptr;
#endif
}

const void* BatchReplayRenderer::getCudaDepthBufferDevicePointer() {
#ifdef ESP_BUILD_WITH_CUDA
CORRADE_ASSERT(standalone_,
"ReplayBatchRenderer::getCudaDepthBufferDevicePointer(): can "
"use this function only "
"with a standalone renderer",
nullptr);
return static_cast<gfx_batch::RendererStandalone&>(*renderer_)
.depthCudaBufferDevicePointer();
#else
ESP_ERROR() << "Failed to retrieve device pointer because CUDA is not "
0mdc marked this conversation as resolved.
Show resolved Hide resolved
"available in this build.";
return nullptr;
#endif
}
} // namespace sim
} // namespace esp
6 changes: 5 additions & 1 deletion src/esp/sim/BatchReplayRenderer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

Expand All @@ -24,6 +24,10 @@ class BatchReplayRenderer : public AbstractReplayRenderer {

~BatchReplayRenderer() override;

const void* getCudaColorBufferDevicePointer() override;

const void* getCudaDepthBufferDevicePointer() override;

private:
void doPreloadFile(Corrade::Containers::StringView filename) override;

Expand Down
2 changes: 1 addition & 1 deletion src/esp/sim/ClassicReplayRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

Expand Down
2 changes: 1 addition & 1 deletion src/esp/sim/ClassicReplayRenderer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

Expand Down
22 changes: 21 additions & 1 deletion src/tests/BatchReplayRendererTest.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

#include "Corrade/Utility/Assert.h"
#include "Magnum/DebugTools/Screenshot.h"
#include "Magnum/Magnum.h"
#include "Magnum/Trade/AbstractImageConverter.h"
Expand All @@ -13,6 +14,7 @@
#include "esp/physics/objectManagers/RigidObjectManager.h"
#include "esp/sensor/CameraSensor.h"
#include "esp/sensor/Sensor.h"
#include "esp/sim/AbstractReplayRenderer.h"
#include "esp/sim/BatchReplayRenderer.h"
#include "esp/sim/ClassicReplayRenderer.h"
#include "esp/sim/Simulator.h"
Expand Down Expand Up @@ -186,6 +188,24 @@ void BatchReplayRendererTest::testIntegration() {
Cr::Utility::Path::join(screenshotDir, groundTruthImageFile),
(Mn::DebugTools::CompareImageToFile{maxThreshold, meanThreshold}));
}

const auto colorPtr = renderer->getCudaColorBufferDevicePointer();
const auto depthPtr = renderer->getCudaDepthBufferDevicePointer();
bool isBatchRenderer =
dynamic_cast<esp::sim::BatchReplayRenderer*>(renderer.get());
#ifdef ESP_BUILD_WITH_CUDA
if (isBatchRenderer) {
CORRADE_VERIFY(colorPtr);
CORRADE_VERIFY(depthPtr);
} else {
// Not implemented in ClassicReplayRenderer
CORRADE_VERIFY(!colorPtr);
CORRADE_VERIFY(!depthPtr);
}
#else
CORRADE_VERIFY(!colorPtr);
CORRADE_VERIFY(!depthPtr);
#endif
}

} // namespace
Expand Down