Skip to content

Commit

Permalink
fix: Major performance improvement when loading glTFs with many KTX t…
Browse files Browse the repository at this point in the history
…extures
  • Loading branch information
atteneder committed Nov 10, 2021
1 parent fd7339a commit 44141f2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] -
### Added
- Added warning when more than two UV sets are supposed to be imported (not supported yet)
### Changed
- Major performance improvement when loading glTFs with many KTX textures
### Fixed
- Correct import of interleaved float RGBA vertex colors (thanks [@mikejurka][mikejurka] for #266)
- Corrected potential pitfall by incorrect UV import job handling (thanks [@mikejurka][mikejurka] for reporting)
Expand Down
48 changes: 33 additions & 15 deletions Runtime/Scripts/GltfImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -883,20 +883,32 @@ async Task<bool> WaitForTextureDownloads() {

#if KTX_UNITY
async Task<bool> WaitForKtxDownloads() {
var tasks = new Task<bool>[ktxDownloadTasks.Count];
var i = 0;
foreach( var dl in ktxDownloadTasks ) {
var www = await dl.Value;
if(www.success) {
var ktxContext = new KtxLoadContext(dl.Key,www.data);
bool forceSampleLinear = imageGamma!=null && !imageGamma[ktxContext.imageIndex];
var textureResult = await ktxContext.LoadKtx(forceSampleLinear);
images[ktxContext.imageIndex] = textureResult.texture;
} else {
logger?.Error(LogCode.TextureDownloadFailed,www.error,dl.Key.ToString());
return false;
}
tasks[i] = ProcessKtxDownload(dl.Key, dl.Value);
i++;
}
await Task.WhenAll(tasks);
foreach (var task in tasks) {
if (!task.Result) return false;
}
return true;
}

async Task<bool> ProcessKtxDownload(int index, Task<IDownload> downloadTask) {
var www = await downloadTask;
if(www.success) {
var ktxContext = new KtxLoadContext(index,www.data);
var forceSampleLinear = imageGamma!=null && !imageGamma[ktxContext.imageIndex];
var textureResult = await ktxContext.LoadKtx(forceSampleLinear);
images[ktxContext.imageIndex] = textureResult.texture;
return true;
} else {
logger?.Error(LogCode.TextureDownloadFailed,www.error,index.ToString());
return false;
}
}
#endif // KTX_UNITY

void LoadBuffer( int index, Uri url ) {
Expand Down Expand Up @@ -1207,12 +1219,18 @@ async Task<bool> Prepare() {
#if KTX_UNITY
if(ktxLoadContextsBuffer!=null) {

for (int i = 0; i < ktxLoadContextsBuffer.Count; i++)
{
var ktxTasks = new Task<KtxUnity.TextureResult>[ktxLoadContextsBuffer.Count];
for (var i = 0; i < ktxLoadContextsBuffer.Count; i++) {
var ktx = ktxLoadContextsBuffer[i];
var forceSampleLinear = imageGamma!=null && !imageGamma[ktx.imageIndex];
ktxTasks[i] = ktx.LoadKtx(forceSampleLinear);
await deferAgent.BreakPoint();
}
await Task.WhenAll(ktxTasks);

for (var i = 0; i < ktxLoadContextsBuffer.Count; i++) {
var ktx = ktxLoadContextsBuffer[i];
bool forceSampleLinear = imageGamma!=null && !imageGamma[ktx.imageIndex];
var textureResult = await ktx.LoadKtx(forceSampleLinear);
images[ktx.imageIndex] = textureResult.texture;
images[ktx.imageIndex] = ktxTasks[i].Result.texture;
}
ktxLoadContextsBuffer.Clear();
}
Expand Down

0 comments on commit 44141f2

Please sign in to comment.