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

Removed synchronous wait in GLB json loading causing spikes #716

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 2 additions & 33 deletions Runtime/Scripts/GLTFSceneImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,7 @@ private async Task LoadJson(string jsonFilePath)
var dataLoader2 = _options.DataLoader as IDataLoader2;
if (IsMultithreaded && dataLoader2 != null)
{
Thread loadThread = new Thread(() => _gltfStream.Stream = dataLoader2.LoadStream(jsonFilePath));
loadThread.Priority = ThreadPriority.Highest;
loadThread.Start();
RunCoroutineSync(WaitUntilEnum(new WaitUntil(() => !loadThread.IsAlive)));
await Task.Run(() => _gltfStream.Stream = dataLoader2.LoadStream(jsonFilePath));
}
else
#endif
Expand All @@ -610,10 +607,7 @@ private async Task LoadJson(string jsonFilePath)
#if !WINDOWS_UWP && !UNITY_WEBGL
if (IsMultithreaded)
{
Thread parseJsonThread = new Thread(() => GLTFParser.ParseJson(_gltfStream.Stream, out _gltfRoot, _gltfStream.StartPosition));
parseJsonThread.Priority = ThreadPriority.Highest;
parseJsonThread.Start();
RunCoroutineSync(WaitUntilEnum(new WaitUntil(() => !parseJsonThread.IsAlive)));
await Task.Run(() => GLTFParser.ParseJson(_gltfStream.Stream, out _gltfRoot, _gltfStream.StartPosition));
if (_gltfRoot == null)
{
throw new GLTFLoadException($"Failed to parse glTF (File: {_gltfFileName})");
Expand Down Expand Up @@ -1433,30 +1427,5 @@ protected async Task YieldOnTimeoutAndThrowOnLowMemory()
await _options.AsyncCoroutineHelper.YieldOnTimeout();
}
}

protected IEnumerator WaitUntilEnum(WaitUntil waitUntil)
{
yield return waitUntil;
}

private static void RunCoroutineSync(IEnumerator streamEnum)
{
var stack = new Stack<IEnumerator>();
stack.Push(streamEnum);
while (stack.Count > 0)
{
var enumerator = stack.Pop();
if (enumerator.MoveNext())
{
stack.Push(enumerator);
var subEnumerator = enumerator.Current as IEnumerator;
if (subEnumerator != null)
{
stack.Push(subEnumerator);
}
}
}
}

}
}