Skip to content

Commit

Permalink
terrain loading in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
beaumanvienna committed Sep 9, 2024
1 parent f36d79c commit b57839d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 27 deletions.
1 change: 0 additions & 1 deletion engine/renderer/builder/fastgltfBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,6 @@ namespace GfxRenderEngine
{ LOG_CORE_CRITICAL("not supported default branch " + glTFImage.name); },
},
glTFImage.data);
LOG_CORE_INFO("loadTexture end, image index: {0}", imageIndex);
return texture;
};
futures[imageIndex] = Engine::m_Engine->m_PoolSecondary.SubmitTask(loadtexture);
Expand Down
1 change: 0 additions & 1 deletion engine/renderer/builder/terrainBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "renderer/builder/terrainBuilder.h"
#include "auxiliary/file.h"
#include "scene/scene.h"
#include "auxiliary/debug.h"

namespace GfxRenderEngine
{
Expand Down
75 changes: 53 additions & 22 deletions engine/scene/sceneLoaderDeserializeJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ namespace GfxRenderEngine
}
}

m_TerrainInfos.resize(terrainDescriptions.count_elements());
uint terrainCounter = 0;
for (auto terrainDescription : terrainDescriptions)
{
ParseTerrainDescription(terrainDescription, m_SceneDescriptionFile.m_TerrainDescriptions);
ParseTerrainDescription(terrainDescription, m_SceneDescriptionFile.m_TerrainDescriptions,
m_TerrainInfos[terrainCounter]);
++terrainCounter;
}
}

Expand Down Expand Up @@ -280,6 +284,7 @@ namespace GfxRenderEngine
LOG_CORE_CRITICAL("unrecognized scene object '" + std::string(sceneObjectKey) + "'");
}
}
FinalizeTerrainDescriptions();
}

void SceneLoaderJSON::ParseGltfFile(ondemand::object gltfFileJSON, bool fast, SceneLoaderJSON::GltfInfo& gltfInfo)
Expand Down Expand Up @@ -622,7 +627,8 @@ namespace GfxRenderEngine
}

void SceneLoaderJSON::ParseTerrainDescription(ondemand::object terrainDescription,
std::vector<Terrain::TerrainDescription>& terrainDescriptions)
std::vector<Terrain::TerrainDescription>& terrainDescriptions,
TerrainInfo& terrainInfo)
{
std::string filename;

Expand Down Expand Up @@ -656,32 +662,21 @@ namespace GfxRenderEngine
return;
}

TerrainLoaderJSON terrainLoaderJSON(m_Scene);
bool loadSuccessful = terrainLoaderJSON.Deserialize(filename, instanceCount);

if (!loadSuccessful)
auto loadTerrain = [this, filename, instanceCount]()
{
LOG_CORE_CRITICAL("terrain description did not load properly: {0}", filename);
return;
}
TerrainLoaderJSON terrainLoaderJSON(m_Scene);
return terrainLoaderJSON.Deserialize(filename, instanceCount);
};

Terrain::TerrainDescription terrainDescriptionScene(filename);
terrainDescriptions.push_back(terrainDescriptionScene);

std::vector<Terrain::Instance>& terrainInstances = terrainDescriptions.back().m_Instances;
terrainInstances.resize(instanceCount);
terrainInfo.m_LoadFuture = Engine::m_Engine->m_PoolPrimary.SubmitTask(loadTerrain);
terrainInfo.m_Filename = filename;
terrainInfo.m_InstanceCount = instanceCount;
terrainInfo.m_InstanceTransforms.resize(instanceCount);

{
auto name = EngineCore::GetFilenameWithoutPath(filename);
name = EngineCore::GetFilenameWithoutExtension(name);
uint instanceIndex = 0;
for (auto instance : instances)
{
std::string entityName = name + std::string("::" + std::to_string(instanceIndex));
entt::entity entity = m_Scene.m_Dictionary.Retrieve(entityName);
CORE_ASSERT(entity != entt::null, "couldn't find entity");
Terrain::Instance& terrainInstance = terrainInstances[instanceIndex];
terrainInstance.m_Entity = entity;
ondemand::object instanceObjects = instance.value();
for (auto instanceObject : instanceObjects)
{
Expand All @@ -691,7 +686,7 @@ namespace GfxRenderEngine
{
CORE_ASSERT((instanceObject.value().type() == ondemand::json_type::object),
"type must be object");
TransformComponent& transform = m_Scene.m_Registry.get<TransformComponent>(entity);
TransformComponent& transform = terrainInfo.m_InstanceTransforms[instanceIndex];
ParseTransform(instanceObject.value(), transform);
}
else
Expand All @@ -714,4 +709,40 @@ namespace GfxRenderEngine
{
return m_SceneDescriptionFile.m_TerrainDescriptions;
}

void SceneLoaderJSON::FinalizeTerrainDescriptions()
{
for (auto& terrainInfo : m_TerrainInfos)
{
if (!terrainInfo.m_LoadFuture.get())
{
continue;
}
Terrain::TerrainDescription terrainDescriptionScene(terrainInfo.m_Filename);
m_SceneDescriptionFile.m_TerrainDescriptions.push_back(terrainDescriptionScene);

std::vector<Terrain::Instance>& terrainInstances =
m_SceneDescriptionFile.m_TerrainDescriptions.back().m_Instances;
terrainInstances.resize(terrainInfo.m_InstanceCount);

{
auto name = EngineCore::GetFilenameWithoutPath(terrainInfo.m_Filename);
name = EngineCore::GetFilenameWithoutExtension(name);
uint instanceIndex = 0;
for (auto& terrainInstance : terrainInstances)
{
std::string entityName = name + std::string("::" + std::to_string(instanceIndex));
entt::entity entity = m_Scene.m_Dictionary.Retrieve(entityName);
CORE_ASSERT(entity != entt::null, "couldn't find entity");
terrainInstance.m_Entity = entity;
TransformComponent& transform = m_Scene.m_Registry.get<TransformComponent>(entity);
transform.SetScale(terrainInfo.m_InstanceTransforms[instanceIndex].GetScale());
transform.SetRotation(terrainInfo.m_InstanceTransforms[instanceIndex].GetRotation());
transform.SetTranslation(terrainInfo.m_InstanceTransforms[instanceIndex].GetTranslation());

++instanceIndex;
}
}
}
}
} // namespace GfxRenderEngine
14 changes: 13 additions & 1 deletion engine/scene/sceneLoaderJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ namespace GfxRenderEngine
std::vector<TransformComponent> m_InstanceTransforms;
};

struct TerrainInfo
{
std::future<bool> m_LoadFuture;
std::string m_Filename;
int m_InstanceCount{0};
std::vector<TransformComponent> m_InstanceTransforms;
};

private:
void Deserialize(std::string& filepath);

Expand All @@ -84,7 +92,9 @@ namespace GfxRenderEngine
void ParseNodesGltf(ondemand::array nodesJSON, std::string const& gltfFilename, Gltf::Instance& gltfFileInstance,
uint instanceIndex);
void ParseTerrainDescription(ondemand::object terrainDescription,
std::vector<Terrain::TerrainDescription>& terrainDescriptions);
std::vector<Terrain::TerrainDescription>& terrainDescriptions,
TerrainInfo& terrainInfo);
void FinalizeTerrainDescriptions();

glm::vec3 ConvertToVec3(ondemand::array arrayJSON);

Expand Down Expand Up @@ -131,5 +141,7 @@ namespace GfxRenderEngine
Scene& m_Scene;

SceneDescriptionFile m_SceneDescriptionFile;

std::vector<TerrainInfo> m_TerrainInfos;
};
} // namespace GfxRenderEngine
2 changes: 1 addition & 1 deletion engine/scene/terrainLoaderDeserializeJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace GfxRenderEngine

TerrainLoaderJSON::TerrainLoaderJSON(Scene& scene) : m_Scene(scene) {}

bool TerrainLoaderJSON::Deserialize(std::string& filepath, int instanceCount)
bool TerrainLoaderJSON::Deserialize(std::string filepath, int instanceCount)
{
if (!EngineCore::FileExists(filepath))
{
Expand Down
2 changes: 1 addition & 1 deletion engine/scene/terrainLoaderJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace GfxRenderEngine
TerrainLoaderJSON(Scene& scene);
~TerrainLoaderJSON() {}

bool Deserialize(std::string& filepath, int instanceCount);
bool Deserialize(std::string filepath, int instanceCount);

private:
struct TerrainDescriptionFile
Expand Down

0 comments on commit b57839d

Please sign in to comment.