Skip to content

Commit 872d71d

Browse files
committed
made geometry writer work async
1 parent c21e4cc commit 872d71d

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

12_MeshLoaders/main.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
5858
if (parser.present("--savepath"))
5959
{
6060
auto tmp = path(parser.get<std::string>("--savepath"));
61-
61+
6262
if (tmp.empty() || !tmp.has_filename())
6363
return logFail("Invalid path has been specified in --savepath argument");
6464

@@ -215,6 +215,17 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
215215
return retval;
216216
}
217217

218+
inline bool onAppTerminated() override
219+
{
220+
if (m_saveGeomTaskFuture.valid())
221+
{
222+
m_logger->log("Waiting for geometry writer to finish writing...", ILogger::ELL_INFO);
223+
m_saveGeomTaskFuture.wait();
224+
}
225+
226+
return device_base_t::onAppTerminated();
227+
}
228+
218229
protected:
219230
const video::IGPURenderpass::SCreationParams::SSubpassDependency* getDefaultSubpassDependencies() const override
220231
{
@@ -310,9 +321,12 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
310321

311322
// TODO: do it async
312323
if (m_saveGeom)
313-
writeGeometry(
314-
const_cast<ICPUPolygonGeometry*>(geometries[0].get()),
315-
m_specifiedGeomSavePath.value_or((m_saveGeomPrefixPath / path(m_modelPath).filename()).generic_string())
324+
m_saveGeomTaskFuture = std::async(
325+
std::launch::async,
326+
[this, geometries] { writeGeometry(
327+
geometries[0],
328+
m_specifiedGeomSavePath.value_or((m_saveGeomPrefixPath / path(m_modelPath).filename()).generic_string())
329+
); }
316330
);
317331

318332
using aabb_t = hlsl::shapes::AABB<3,double>;
@@ -447,11 +461,14 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
447461
return true;
448462
}
449463

450-
void writeGeometry(ICPUPolygonGeometry* geometry, const std::string& savePath)
464+
void writeGeometry(smart_refctd_ptr<const ICPUPolygonGeometry> geometry, const std::string& savePath)
451465
{
452-
IAssetWriter::SAssetWriteParams params{ reinterpret_cast<IAsset*>(geometry) };
453-
m_logger->log("Saving mesh to %S", ILogger::ELL_INFO, savePath.c_str());
454-
m_assetMgr->writeAsset(savePath, params);
466+
IAsset* assetPtr = const_cast<IAsset*>(static_cast<const IAsset*>(geometry.get()));
467+
IAssetWriter::SAssetWriteParams params{ assetPtr };
468+
m_logger->log("Saving mesh to %s", ILogger::ELL_INFO, savePath.c_str());
469+
if (!m_assetMgr->writeAsset(savePath, params))
470+
m_logger->log("Failed to save %s", ILogger::ELL_ERROR, savePath.c_str());
471+
m_logger->log("Mesh successfully saved!", ILogger::ELL_INFO);
455472
}
456473

457474
// Maximum frames which can be simultaneously submitted, used to cycle through our per-frame resources like command buffers
@@ -470,7 +487,8 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
470487
// mutables
471488
std::string m_modelPath;
472489

473-
bool m_saveGeom;
490+
bool m_saveGeom = false;
491+
std::future<void> m_saveGeomTaskFuture;
474492
std::optional<const std::string> m_specifiedGeomSavePath;
475493
nbl::system::path m_saveGeomPrefixPath;
476494
};

0 commit comments

Comments
 (0)