Skip to content

Commit d58e53f

Browse files
ldcWVeundersanderLawrence Chenerikwijmans
authored
Cleanup and development of flag to enable/disable rendering (#1308)
Add createRenderer flag to enable/disable OpenGL rendering Co-authored-by: Eric Undersander <eundersander@gmail.com> Co-authored-by: Lawrence Chen <ldchen@fb.com> Co-authored-by: Erik Wijmans <ewijmans2@gmail.com>
1 parent 6345a4c commit d58e53f

24 files changed

Lines changed: 316 additions & 91 deletions

examples/tutorials/colabs/replay_tutorial.ipynb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@
8383
"source": [
8484
"\n",
8585
"\n",
86-
"def make_configuration():\n",
86+
"def make_configuration(settings):\n",
87+
" make_video_during_sim = False\n",
88+
" if \"make_video_during_sim\" in settings:\n",
89+
" make_video_during_sim = settings[\"make_video_during_sim\"]\n",
90+
"\n",
8791
" # simulator configuration\n",
8892
" backend_cfg = habitat_sim.SimulatorConfiguration()\n",
8993
" backend_cfg.scene_id = os.path.join(\n",
@@ -95,6 +99,7 @@
9599
" # Enable gfx replay save. See also our call to sim.gfx_replay_manager.save_keyframe()\n",
96100
" # below.\n",
97101
" backend_cfg.enable_gfx_replay_save = True\n",
102+
" backend_cfg.create_renderer = make_video_during_sim\n",
98103
"\n",
99104
" sensor_cfg = habitat_sim.CameraSensorSpec()\n",
100105
" sensor_cfg.resolution = [544, 720]\n",
@@ -216,14 +221,15 @@
216221
" args, _ = parser.parse_known_args()\n",
217222
" show_video = args.show_video\n",
218223
" make_video = args.make_video\n",
224+
" make_video_during_sim = False\n",
219225
"else:\n",
220226
" show_video = False\n",
221227
" make_video = False\n",
222228
"\n",
223229
"if make_video and not os.path.exists(output_path):\n",
224230
" os.mkdir(output_path)\n",
225231
"\n",
226-
"cfg = make_configuration()\n",
232+
"cfg = make_configuration({\"make_video_during_sim\": make_video_during_sim})\n",
227233
"sim = None\n",
228234
"replay_filepath = \"./replay.json\"\n",
229235
"\n",
@@ -290,7 +296,7 @@
290296
" duration=1.0,\n",
291297
" agent_vel=np.array([0.5, 0.0, 0.0]),\n",
292298
" look_rotation_vel=25.0,\n",
293-
" get_frames=make_video,\n",
299+
" get_frames=make_video_during_sim,\n",
294300
")"
295301
]
296302
},
@@ -333,7 +339,7 @@
333339
" duration=2.0,\n",
334340
" agent_vel=np.array([0.0, 0.0, -0.4]),\n",
335341
" look_rotation_vel=-5.0,\n",
336-
" get_frames=make_video,\n",
342+
" get_frames=make_video_during_sim,\n",
337343
")"
338344
]
339345
},
@@ -361,7 +367,7 @@
361367
" duration=2.0,\n",
362368
" agent_vel=np.array([0.4, 0.0, 0.0]),\n",
363369
" look_rotation_vel=-10.0,\n",
364-
" get_frames=make_video,\n",
370+
" get_frames=make_video_during_sim,\n",
365371
")"
366372
]
367373
},
@@ -381,7 +387,7 @@
381387
"outputs": [],
382388
"source": [
383389
"\n",
384-
"if make_video:\n",
390+
"if make_video_during_sim:\n",
385391
" vut.make_video(\n",
386392
" observations,\n",
387393
" \"rgba_camera\",\n",

examples/tutorials/nb_python/replay_tutorial.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@
7070
# %%
7171

7272

73-
def make_configuration():
73+
def make_configuration(settings):
74+
make_video_during_sim = False
75+
if "make_video_during_sim" in settings:
76+
make_video_during_sim = settings["make_video_during_sim"]
77+
7478
# simulator configuration
7579
backend_cfg = habitat_sim.SimulatorConfiguration()
7680
backend_cfg.scene_id = os.path.join(
@@ -82,6 +86,7 @@ def make_configuration():
8286
# Enable gfx replay save. See also our call to sim.gfx_replay_manager.save_keyframe()
8387
# below.
8488
backend_cfg.enable_gfx_replay_save = True
89+
backend_cfg.create_renderer = make_video_during_sim
8590

8691
sensor_cfg = habitat_sim.CameraSensorSpec()
8792
sensor_cfg.resolution = [544, 720]
@@ -177,14 +182,15 @@ def configure_lighting(sim):
177182
args, _ = parser.parse_known_args()
178183
show_video = args.show_video
179184
make_video = args.make_video
185+
make_video_during_sim = False
180186
else:
181187
show_video = False
182188
make_video = False
183189

184190
if make_video and not os.path.exists(output_path):
185191
os.mkdir(output_path)
186192

187-
cfg = make_configuration()
193+
cfg = make_configuration({"make_video_during_sim": make_video_during_sim})
188194
sim = None
189195
replay_filepath = "./replay.json"
190196

@@ -225,7 +231,7 @@ def configure_lighting(sim):
225231
duration=1.0,
226232
agent_vel=np.array([0.5, 0.0, 0.0]),
227233
look_rotation_vel=25.0,
228-
get_frames=make_video,
234+
get_frames=make_video_during_sim,
229235
)
230236

231237
# %% [markdown]
@@ -255,7 +261,7 @@ def configure_lighting(sim):
255261
duration=2.0,
256262
agent_vel=np.array([0.0, 0.0, -0.4]),
257263
look_rotation_vel=-5.0,
258-
get_frames=make_video,
264+
get_frames=make_video_during_sim,
259265
)
260266

261267
# %% [markdown]
@@ -270,14 +276,14 @@ def configure_lighting(sim):
270276
duration=2.0,
271277
agent_vel=np.array([0.4, 0.0, 0.0]),
272278
look_rotation_vel=-10.0,
273-
get_frames=make_video,
279+
get_frames=make_video_during_sim,
274280
)
275281

276282
# %% [markdown]
277283
# ## End the episode. Render the episode observations to a video.
278284
# %%
279285

280-
if make_video:
286+
if make_video_during_sim:
281287
vut.make_video(
282288
observations,
283289
"rgba_camera",

habitat_sim/simulator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ def __init__(self, sim: Simulator, agent: Agent, sensor_id: str) -> None:
539539

540540
self._spec = self._sensor_object.specification()
541541

542-
self._sim.renderer.bind_render_target(self._sensor_object)
542+
if self._sim.renderer is not None:
543+
self._sim.renderer.bind_render_target(self._sensor_object)
543544

544545
if self._spec.gpu2gpu_transfer:
545546
assert cuda_enabled, "Must build habitat sim with cuda for gpu2gpu-transfer"
@@ -605,6 +606,7 @@ def __init__(self, sim: Simulator, agent: Agent, sensor_id: str) -> None:
605606
)
606607

607608
def draw_observation(self) -> None:
609+
assert self._sim.renderer is not None
608610
# see if the sensor is attached to a scene graph, otherwise it is invalid,
609611
# and cannot make any observation
610612
if not self._sensor_object.object:
@@ -615,6 +617,7 @@ def draw_observation(self) -> None:
615617
self._sim.renderer.draw(self._sensor_object, self._sim)
616618

617619
def _draw_observation_async(self) -> None:
620+
assert self._sim.renderer is not None
618621
if (
619622
self._spec.sensor_type == SensorType.SEMANTIC
620623
and self._sim.get_active_scene_graph()
@@ -669,6 +672,7 @@ def _draw_observation_async(self) -> None:
669672
)
670673

671674
def get_observation(self) -> Union[ndarray, "Tensor"]:
675+
assert self._sim.renderer is not None
672676
tgt = self._sensor_object.render_target
673677

674678
if self._spec.gpu2gpu_transfer:
@@ -682,7 +686,6 @@ def get_observation(self) -> Union[ndarray, "Tensor"]:
682686

683687
obs = self._buffer.flip(0) # type: ignore[union-attr]
684688
else:
685-
686689
if self._spec.sensor_type == SensorType.SEMANTIC:
687690
tgt.read_frame_object_id(self.view)
688691
elif self._spec.sensor_type == SensorType.DEPTH:

src/esp/assets/ResourceManager.cpp

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,15 @@ void ResourceManager::buildImporters() {
124124

125125
} // buildImporters
126126

127+
bool ResourceManager::getCreateRenderer() const {
128+
return metadataMediator_->getCreateRenderer();
129+
}
130+
127131
void ResourceManager::initDefaultPrimAttributes() {
132+
if (!getCreateRenderer()) {
133+
return;
134+
}
135+
128136
// by this point, we should have a GL::Context so load the bb primitive.
129137
// TODO: replace this completely with standard mesh (i.e. treat the bb
130138
// wireframe cube no differently than other primivite-based rendered
@@ -996,7 +1004,9 @@ void ResourceManager::buildPrimitiveAssetData(
9961004
// compute the mesh bounding box
9971005
primMeshData->BB = computeMeshBB(primMeshData.get());
9981006

999-
primMeshData->uploadBuffersToGPU(false);
1007+
if (getCreateRenderer()) {
1008+
primMeshData->uploadBuffersToGPU(false);
1009+
}
10001010

10011011
// make MeshMetaData
10021012
int meshStart = nextMeshID_++;
@@ -1100,7 +1110,9 @@ scene::SceneNode* ResourceManager::createRenderAssetInstancePTex(
11001110
for (int iMesh = start; iMesh <= end; ++iMesh) {
11011111
auto* pTexMeshData = dynamic_cast<PTexMeshData*>(meshes_.at(iMesh).get());
11021112

1103-
pTexMeshData->uploadBuffersToGPU(false);
1113+
if (getCreateRenderer()) {
1114+
pTexMeshData->uploadBuffersToGPU(false);
1115+
}
11041116

11051117
for (int jSubmesh = 0; jSubmesh < pTexMeshData->getSize(); ++jSubmesh) {
11061118
scene::SceneNode& node = instanceRoot->createChild();
@@ -1160,7 +1172,9 @@ bool ResourceManager::loadRenderAssetIMesh(const AssetInfo& info) {
11601172

11611173
for (int meshIDLocal = 0; meshIDLocal < instanceMeshes.size();
11621174
++meshIDLocal) {
1163-
instanceMeshes[meshIDLocal]->uploadBuffersToGPU(false);
1175+
if (getCreateRenderer()) {
1176+
instanceMeshes[meshIDLocal]->uploadBuffersToGPU(false);
1177+
}
11641178
meshes_.emplace(meshStart + meshIDLocal,
11651179
std::move(instanceMeshes[meshIDLocal]));
11661180

@@ -1204,7 +1218,7 @@ scene::SceneNode* ResourceManager::createRenderAssetInstanceIMesh(
12041218
// That means One CANNOT query the data like e.g.,
12051219
// meshes_.at(iMesh)->getMeshData()->hasAttribute(Mn::Trade::MeshAttribute::Tangent)
12061220
// It will SEGFAULT!
1207-
createDrawable(*(meshes_.at(iMesh)->getMagnumGLMesh()), // render mesh
1221+
createDrawable(meshes_.at(iMesh)->getMagnumGLMesh(), // render mesh
12081222
meshAttributeFlags, // mesh attribute flags
12091223
node, // scene node
12101224
creation.lightSetupKey, // lightSetup key
@@ -1239,7 +1253,8 @@ bool ResourceManager::loadRenderAssetGeneral(const AssetInfo& info) {
12391253
assimpmetadata->configuration().setValue("ImportColladaIgnoreUpDirection",
12401254
"true");
12411255
#endif
1242-
{
1256+
1257+
if (requiresTextures_) {
12431258
Cr::PluginManager::PluginMetadata* const metadata =
12441259
importerManager_.metadata("BasisImporter");
12451260
Mn::GL::Context& context = Mn::GL::Context::current();
@@ -1460,6 +1475,8 @@ bool ResourceManager::buildTrajectoryVisualization(
14601475
// compute the mesh bounding box
14611476
visMeshData->BB = computeMeshBB(visMeshData.get());
14621477

1478+
ESP_CHECK(getCreateRenderer(),
1479+
"buildTrajectoryVisualization requires a renderer");
14631480
visMeshData->uploadBuffersToGPU(false);
14641481

14651482
// make MeshMetaData
@@ -1507,8 +1524,13 @@ int ResourceManager::loadNavMeshVisualization(esp::nav::PathFinder& pathFinder,
15071524
DrawableGroup* drawables) {
15081525
int navMeshPrimitiveID = ID_UNDEFINED;
15091526

1510-
if (!pathFinder.isLoaded())
1527+
if (!pathFinder.isLoaded()) {
15111528
return navMeshPrimitiveID;
1529+
}
1530+
1531+
if (!getCreateRenderer()) {
1532+
return navMeshPrimitiveID;
1533+
}
15121534

15131535
// create the mesh
15141536
std::vector<Magnum::UnsignedInt> indices;
@@ -1795,7 +1817,9 @@ void ResourceManager::loadMeshes(Importer& importer,
17951817
// compute the mesh bounding box
17961818
gltfMeshData->BB = computeMeshBB(gltfMeshData.get());
17971819

1798-
gltfMeshData->uploadBuffersToGPU(false);
1820+
if (getCreateRenderer()) {
1821+
gltfMeshData->uploadBuffersToGPU(false);
1822+
}
17991823
meshes_.emplace(meshStart + iMesh, std::move(gltfMeshData));
18001824
}
18011825
} // ResourceManager::loadMeshes
@@ -2058,7 +2082,15 @@ void ResourceManager::addComponent(
20582082
// Add a drawable if the object has a mesh and the mesh is loaded
20592083
if (meshIDLocal != ID_UNDEFINED) {
20602084
const int meshID = metaData.meshIndex.first + meshIDLocal;
2061-
Magnum::GL::Mesh& mesh = *meshes_.at(meshID)->getMagnumGLMesh();
2085+
Magnum::GL::Mesh* mesh = meshes_.at(meshID)->getMagnumGLMesh();
2086+
if (getCreateRenderer()) {
2087+
CORRADE_ASSERT(mesh,
2088+
"::addComponent() : GL mesh expected but not found", );
2089+
} else {
2090+
CORRADE_ASSERT(!mesh,
2091+
"addComponent() : encountered unexpected GL mesh with "
2092+
"createRenderer==false", );
2093+
}
20622094
Mn::ResourceKey materialKey = meshTransformNode.materialID;
20632095

20642096
gfx::Drawable::Flags meshAttributeFlags{};
@@ -2115,20 +2147,20 @@ void ResourceManager::addPrimitiveToDrawables(int primitiveID,
21152147
// so do not need to worry about the tangent or bitangent.
21162148
// it might be changed in the future.
21172149
gfx::Drawable::Flags meshAttributeFlags{};
2118-
createDrawable(*primitive_meshes_.at(primitiveID), // render mesh
2119-
meshAttributeFlags, // meshAttributeFlags
2120-
node, // scene node
2121-
NO_LIGHT_KEY, // lightSetup key
2122-
WHITE_MATERIAL_KEY, // material key
2123-
drawables); // drawable group
2150+
createDrawable(primitive_meshes_.at(primitiveID).get(), // render mesh
2151+
meshAttributeFlags, // meshAttributeFlags
2152+
node, // scene node
2153+
NO_LIGHT_KEY, // lightSetup key
2154+
WHITE_MATERIAL_KEY, // material key
2155+
drawables); // drawable group
21242156
}
21252157

21262158
void ResourceManager::removePrimitiveMesh(int primitiveID) {
21272159
CHECK(primitive_meshes_.count(primitiveID));
21282160
primitive_meshes_.erase(primitiveID);
21292161
}
21302162

2131-
void ResourceManager::createDrawable(Mn::GL::Mesh& mesh,
2163+
void ResourceManager::createDrawable(Mn::GL::Mesh* mesh,
21322164
gfx::Drawable::Flags& meshAttributeFlags,
21332165
scene::SceneNode& node,
21342166
const Mn::ResourceKey& lightSetupKey,
@@ -2508,5 +2540,6 @@ void ResourceManager::createConvexHullDecomposition(
25082540
}
25092541
}
25102542
#endif
2543+
25112544
} // namespace assets
25122545
} // namespace esp

src/esp/assets/ResourceManager.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ namespace assets {
8888
*/
8989
class ResourceManager {
9090
public:
91+
bool getCreateRenderer() const;
92+
9193
/** @brief Stores references to a set of drawable elements */
9294
using DrawableGroup = gfx::DrawableGroup;
9395
/** @brief Convenience typedef for Importer class */
@@ -488,7 +490,7 @@ class ResourceManager {
488490
* gfx::Drawable.
489491
*/
490492

491-
void createDrawable(Mn::GL::Mesh& mesh,
493+
void createDrawable(Mn::GL::Mesh* mesh,
492494
gfx::Drawable::Flags& meshAttributeFlags,
493495
scene::SceneNode& node,
494496
const Mn::ResourceKey& lightSetupKey,

src/esp/gfx/Drawable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace esp {
1111
namespace gfx {
1212
uint64_t Drawable::drawableIdCounter = 0;
1313
Drawable::Drawable(scene::SceneNode& node,
14-
Magnum::GL::Mesh& mesh,
14+
Magnum::GL::Mesh* mesh,
1515
DrawableType type,
1616
DrawableGroup* group /* = nullptr */)
1717
: Magnum::SceneGraph::Drawable3D{node, group},

0 commit comments

Comments
 (0)