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

GPU-to-CPU batch renderer API to render color and depth sensors into images. #2044

Merged
merged 12 commits into from
Jun 13, 2023
14 changes: 9 additions & 5 deletions src/esp/bindings/SimBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,16 @@ void initSimBindings(py::module& m) {
.def(
"render",
[](AbstractReplayRenderer& self,
std::vector<Mn::MutableImageView2D> images) {
self.render(images);
std::vector<Mn::MutableImageView2D> colorImageViews,
std::vector<Mn::MutableImageView2D> depthImageViews) {
0mdc marked this conversation as resolved.
Show resolved Hide resolved
self.render(colorImageViews, depthImageViews);
0mdc marked this conversation as resolved.
Show resolved Hide resolved
},
R"(Render color sensors into the specified image vector (one per environment).
The images are required to be pre-allocated.
Blocks the thread during the GPU-to-CPU memory transfer operation.)")
R"(Render sensors into the specified image vectors (one per environment).
Blocks the thread during the GPU-to-CPU memory transfer operation.
Empty lists can be supplied to skip the copying render targets.
The images are required to be pre-allocated.)",
py::arg("color_images") = std::vector<Mn::MutableImageView2D>{},
py::arg("depth_images") = std::vector<Mn::MutableImageView2D>{})
.def(
"set_sensor_transforms_from_keyframe",
&AbstractReplayRenderer::setSensorTransformsFromKeyframe,
Expand Down
21 changes: 15 additions & 6 deletions src/esp/sim/AbstractReplayRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,21 @@ void AbstractReplayRenderer::setSensorTransformsFromKeyframe(
}

void AbstractReplayRenderer::render(
Cr::Containers::ArrayView<const Mn::MutableImageView2D> imageViews) {
CORRADE_ASSERT(imageViews.size() == doEnvironmentCount(),
"ReplayRenderer::render(): expected" << doEnvironmentCount()
<< "image views but got"
<< imageViews.size(), );
return doRender(imageViews);
Cr::Containers::ArrayView<const Mn::MutableImageView2D> colorImageViews,
Cr::Containers::ArrayView<const Mn::MutableImageView2D> depthImageViews) {
if (colorImageViews.size() > 0) {
ESP_CHECK(colorImageViews.size() == doEnvironmentCount(),
"ReplayRenderer::render(): expected"
<< doEnvironmentCount() << "color image views but got"
<< colorImageViews.size());
}
if (depthImageViews.size() > 0) {
ESP_CHECK(depthImageViews.size() == doEnvironmentCount(),
"ReplayRenderer::render(): expected"
<< doEnvironmentCount() << "depth image views but got"
<< depthImageViews.size());
}
return doRender(colorImageViews, depthImageViews);
}

void AbstractReplayRenderer::render(
Expand Down
11 changes: 8 additions & 3 deletions src/esp/sim/AbstractReplayRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ class AbstractReplayRenderer {
void setSensorTransformsFromKeyframe(unsigned envIndex,
const std::string& prefix);

// Renders and waits for the render to finish
// Renders into the specified CPU-resident image view arrays (one image per
// environment). Waits for the render to finish.
void render(Corrade::Containers::ArrayView<const Magnum::MutableImageView2D>
imageViews);
colorImageViews,
Corrade::Containers::ArrayView<const Magnum::MutableImageView2D>
depthImageViews);

// Assumes the framebuffer color & depth is cleared
void render(Magnum::GL::AbstractFramebuffer& framebuffer);
Expand Down Expand Up @@ -164,7 +167,9 @@ class AbstractReplayRenderer {
/* imageViews.size() is guaranteed to be same as doEnvironmentCount() */
virtual void doRender(
Corrade::Containers::ArrayView<const Magnum::MutableImageView2D>
imageViews) = 0;
colorImageViews,
Corrade::Containers::ArrayView<const Magnum::MutableImageView2D>
depthImageViews) = 0;

virtual void doRender(Magnum::GL::AbstractFramebuffer& framebuffer) = 0;

Expand Down
14 changes: 11 additions & 3 deletions src/esp/sim/BatchReplayRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ void BatchReplayRenderer::doSetSensorTransformsFromKeyframe(
}

void BatchReplayRenderer::doRender(
Cr::Containers::ArrayView<const Mn::MutableImageView2D> imageViews) {
Cr::Containers::ArrayView<const Mn::MutableImageView2D> colorImageViews,
Cr::Containers::ArrayView<const Mn::MutableImageView2D> depthImageViews) {
CORRADE_ASSERT(standalone_,
"BatchReplayRenderer::render(): can use this function only "
"with a standalone renderer", );
Expand All @@ -267,8 +268,15 @@ void BatchReplayRenderer::doRender(
Mn::Vector2i{envIndex % renderer_->tileCount().x(),
envIndex / renderer_->tileCount().x()},
renderer_->tileSize());
static_cast<gfx_batch::RendererStandalone&>(*renderer_)
.colorImageInto(rectangle, imageViews[envIndex]);

if (colorImageViews.size() > 0) {
static_cast<gfx_batch::RendererStandalone&>(*renderer_)
.colorImageInto(rectangle, colorImageViews[envIndex]);
}
if (depthImageViews.size() > 0) {
static_cast<gfx_batch::RendererStandalone&>(*renderer_)
.depthImageInto(rectangle, depthImageViews[envIndex]);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/esp/sim/BatchReplayRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class BatchReplayRenderer : public AbstractReplayRenderer {
const std::string& prefix) override;

void doRender(Corrade::Containers::ArrayView<const Magnum::MutableImageView2D>
imageViews) override;
colorImageViews,
Corrade::Containers::ArrayView<const Magnum::MutableImageView2D>
depthImageViews) override;

void doRender(Magnum::GL::AbstractFramebuffer& framebuffer) override;

Expand Down
27 changes: 22 additions & 5 deletions src/esp/sim/ClassicReplayRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "esp/gfx/RenderTarget.h"
#include "esp/gfx/Renderer.h"
#include "esp/metadata/MetadataMediator.h"
#include "esp/sensor/Sensor.h"
#include "esp/sensor/SensorFactory.h"
#include "esp/sim/SimulatorConfiguration.h"

Expand Down Expand Up @@ -200,7 +201,8 @@ void ClassicReplayRenderer::doSetSensorTransformsFromKeyframe(
}

void ClassicReplayRenderer::doRender(
Cr::Containers::ArrayView<const Mn::MutableImageView2D> imageViews) {
Cr::Containers::ArrayView<const Mn::MutableImageView2D> colorImageViews,
Cr::Containers::ArrayView<const Mn::MutableImageView2D> depthImageViews) {
for (int envIndex = 0; envIndex < config_.numEnvironments; envIndex++) {
auto& sensorMap = getEnvironmentSensors(envIndex);
CORRADE_INTERNAL_ASSERT(sensorMap.size() == 1);
Expand All @@ -211,11 +213,26 @@ void ClassicReplayRenderer::doRender(

auto& sceneGraph = getSceneGraph(envIndex);

auto& sensorType = visualSensor.specification()->sensorType;
Cr::Containers::ArrayView<const Mn::MutableImageView2D> imageViews;
switch (sensorType) {
case esp::sensor::SensorType::Color:
imageViews = colorImageViews;
break;
case esp::sensor::SensorType::Depth:
imageViews = depthImageViews;
break;
default:
break;
}

#ifdef ESP_BUILD_WITH_BACKGROUND_RENDERER
renderer_->enqueueAsyncDrawJob(
visualSensor, sceneGraph, imageViews[envIndex],
esp::gfx::RenderCamera::Flags{
gfx::RenderCamera::Flag::FrustumCulling});
if (imageViews != nullptr) {
renderer_->enqueueAsyncDrawJob(
visualSensor, sceneGraph, imageViews[envIndex],
esp::gfx::RenderCamera::Flags{
gfx::RenderCamera::Flag::FrustumCulling});
}
#else
// TODO what am I supposed to do here?
CORRADE_ASSERT_UNREACHABLE("Not implemented yet, sorry.", );
Expand Down
4 changes: 3 additions & 1 deletion src/esp/sim/ClassicReplayRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class ClassicReplayRenderer : public AbstractReplayRenderer {
const std::string& prefix) override;

void doRender(Corrade::Containers::ArrayView<const Magnum::MutableImageView2D>
imageViews) override;
colorImageViews,
Corrade::Containers::ArrayView<const Magnum::MutableImageView2D>
depthImageViews) override;

void doRender(Magnum::GL::AbstractFramebuffer& framebuffer) override;

Expand Down
Loading