Skip to content

Commit

Permalink
PBRBackgroundGUI now loads all 3 background images in parallel at loa…
Browse files Browse the repository at this point in the history
…d and resize, which can brings a little speed
  • Loading branch information
Guillaume Piolat committed Aug 17, 2023
1 parent 600546b commit 0f64c45
Showing 1 changed file with 53 additions and 21 deletions.
74 changes: 53 additions & 21 deletions pbrwidgets/dplug/pbrwidgets/pbrbackgroundgui.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module dplug.pbrwidgets.pbrbackgroundgui;
import dplug.math.box;
import dplug.core.nogc;
import dplug.core.file;
import dplug.core.thread;

// Note: this dependency exist because Key is defined in dplug:window
import dplug.window.window;
Expand Down Expand Up @@ -68,7 +69,7 @@ nothrow:
{}
else
{
loadBackgroundImagesFromStaticData();
loadBackgroundImagesFromStaticData(null);
}

auto skyboxData = cast(ubyte[])(import(skyboxPath));
Expand Down Expand Up @@ -122,7 +123,9 @@ nothrow:
version (decompressImagesLazily)
{
assert(_diffuse is null);
loadBackgroundImagesFromStaticData();

// No thread pool, load images one by one.
loadBackgroundImagesFromStaticData(context.globalThreadPool);
}

_diffuseResized.size(W, H);
Expand Down Expand Up @@ -188,7 +191,8 @@ nothrow:

private:

final void loadBackgroundImagesFromStaticData()
// Can pass a ThreadPool in the case you want parallel image loading (optional).
final void loadBackgroundImagesFromStaticData(ThreadPool* threadPool)
{
auto basecolorData = cast(ubyte[])(import(baseColorPath));
static if (emissivePath)
Expand All @@ -197,7 +201,12 @@ private:
ubyte[] emissiveData = null;
auto materialData = cast(ubyte[])(import(materialPath));
auto depthData = cast(ubyte[])(import(depthPath));
loadBackgroundImages(basecolorData, emissiveData, materialData, depthData);

loadBackgroundImages(basecolorData,
emissiveData,
materialData,
depthData,
threadPool);
}

// CTFE used here so we are allowed to use ~
Expand Down Expand Up @@ -270,7 +279,7 @@ private:
{
// Reload images from disk and update the UI
freeBackgroundImages();
loadBackgroundImages(basecolorData, emissiveData, materialData, depthData);
loadBackgroundImages(basecolorData, emissiveData, materialData, depthData, null);
loadSkybox(skyboxData);
_forceResizeUpdate = true;
setDirtyWhole();
Expand All @@ -294,25 +303,48 @@ private:
void loadBackgroundImages(ubyte[] basecolorData,
ubyte[] emissiveData, // this one can be null
ubyte[] materialData,
ubyte[] depthData)
ubyte[] depthData,
ThreadPool* threadPool)
{
version(Dplug_ProfileUI) context.profiler.category("image").begin("load Diffuse background");
if (emissiveData)
_diffuse = loadImageSeparateAlpha(basecolorData, emissiveData);
else
// Potentially load all 3 background images in parallel, if one threadPool is provided.
void loadOneImage(int i, int threadIndex) nothrow @nogc
{
// fill with zero, no emissive data => zero Emissive
_diffuse = loadImageWithFilledAlpha(basecolorData, 0);
ImageResizer resizer;
if (i == 0)
{
version(Dplug_ProfileUI) context.profiler.category("image").begin("load Diffuse background");
if (emissiveData)
_diffuse = loadImageSeparateAlpha(basecolorData, emissiveData);
else
{
// fill with zero, no emissive data => zero Emissive
_diffuse = loadImageWithFilledAlpha(basecolorData, 0);
}
version(Dplug_ProfileUI) context.profiler.end;
}
if (i == 1)
{
version(Dplug_ProfileUI) context.profiler.begin("load Material background");
_material = loadOwnedImage(materialData);
version(Dplug_ProfileUI) context.profiler.end;
}
if (i == 2)
{
version(Dplug_ProfileUI) context.profiler.begin("load Depth background");
_depth = loadOwnedImageDepth(depthData);
version(Dplug_ProfileUI) context.profiler.end;
}
}
version(Dplug_ProfileUI) context.profiler.end;

version(Dplug_ProfileUI) context.profiler.begin("load Material background");
_material = loadOwnedImage(materialData);
version(Dplug_ProfileUI) context.profiler.end;

version(Dplug_ProfileUI) context.profiler.begin("load Depth background");
_depth = loadOwnedImageDepth(depthData);
version(Dplug_ProfileUI) context.profiler.end;
if (threadPool)
{
threadPool.parallelFor(3, &loadOneImage);
}
else
{
loadOneImage(0, -1);
loadOneImage(1, -1);
loadOneImage(2, -1);
}
}

void loadSkybox(ubyte[] skyboxData)
Expand Down

0 comments on commit 0f64c45

Please sign in to comment.