Skip to content

Commit

Permalink
Fix HDR irradiance filtering not rendering if scene not rendering yet (
Browse files Browse the repository at this point in the history
…#16160)

* move timer stats to thinEngine

* fix case

* PR Feedback

* Add Irradiance texture support in Env format

* Fix HDR irradiance filtering not rendering if scene not rendering yet

* pr feedback

* Fix PR feedback... or mostly the PR thanks @bghgary
  • Loading branch information
sebavan authored Feb 8, 2025
1 parent ddc789f commit 496d7a4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 46 deletions.
28 changes: 10 additions & 18 deletions packages/dev/core/src/Materials/Textures/Filtering/hdrFiltering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Constants } from "../../../Engines/constants";
import { EffectWrapper, EffectRenderer } from "../../../Materials/effectRenderer";
import type { Nullable } from "../../../types";
import type { RenderTargetWrapper } from "../../../Engines/renderTargetWrapper";
import { Logger } from "../../../Misc/logger";

import { ShaderLanguage } from "core/Materials/shaderLanguage";

Expand Down Expand Up @@ -207,27 +206,20 @@ export class HDRFiltering {
* This has to be done once the map is loaded, and has not been prefiltered by a third party software.
* See http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf for more information
* @param texture Texture to filter
* @param onFinished Callback when filtering is done
* @returns Promise called when prefiltering is done
*/
public prefilter(texture: BaseTexture, onFinished: Nullable<() => void> = null): Promise<void> {
public async prefilter(texture: BaseTexture): Promise<void> {
if (!this._engine._features.allowTexturePrefiltering) {
Logger.Warn("HDR prefiltering is not available in WebGL 1., you can use real time filtering instead.");
return Promise.reject("HDR prefiltering is not available in WebGL 1., you can use real time filtering instead.");
throw new Error("HDR prefiltering is not available in WebGL 1., you can use real time filtering instead.");
}

return new Promise((resolve) => {
this._effectRenderer = new EffectRenderer(this._engine);
this._effectWrapper = this._createEffect(texture);
this._effectWrapper.effect.executeWhenCompiled(() => {
this._prefilterInternal(texture);
this._effectRenderer.dispose();
this._effectWrapper.dispose();
resolve();
if (onFinished) {
onFinished();
}
});
});
this._effectRenderer = new EffectRenderer(this._engine);
this._effectWrapper = this._createEffect(texture);

await this._effectWrapper.effect.whenCompiledAsync();

this._prefilterInternal(texture);
this._effectRenderer.dispose();
this._effectWrapper.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Constants } from "../../../Engines/constants";
import { EffectWrapper, EffectRenderer } from "../../../Materials/effectRenderer";
import type { Nullable } from "../../../types";
import type { RenderTargetWrapper } from "../../../Engines/renderTargetWrapper";
import { Logger } from "../../../Misc/logger";

import { ShaderLanguage } from "core/Materials/shaderLanguage";
import { IblCdfGenerator } from "../../../Rendering/iblCdfGenerator";
Expand Down Expand Up @@ -199,40 +198,29 @@ export class HDRIrradianceFiltering {
* This has to be done once the map is loaded, and has not been prefiltered by a third party software.
* See http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf for more information
* @param texture Texture to filter
* @param onFinished Callback when filtering is done
* @returns Promise called when prefiltering is done
*/
public prefilter(texture: BaseTexture, onFinished: Nullable<() => void> = null): Promise<BaseTexture> {
public async prefilter(texture: BaseTexture): Promise<BaseTexture> {
if (!this._engine._features.allowTexturePrefiltering) {
Logger.Warn("HDR prefiltering is not available in WebGL 1., you can use real time filtering instead.");
return Promise.reject("HDR prefiltering is not available in WebGL 1., you can use real time filtering instead.");
throw new Error("HDR prefiltering is not available in WebGL 1., you can use real time filtering instead.");
}
let cdfGeneratedPromise: Promise<Nullable<BaseTexture>> = Promise.resolve(null);

if (this.useCdf) {
this._cdfGenerator = new IblCdfGenerator(this._engine);
this._cdfGenerator.iblSource = texture;
cdfGeneratedPromise = new Promise((resolve) => {
this._cdfGenerator.onGeneratedObservable.addOnce(() => {
resolve(null);
});
});

await this._cdfGenerator.renderWhenReady();
}

return cdfGeneratedPromise.then(() => {
return new Promise((resolve) => {
this._effectRenderer = new EffectRenderer(this._engine);
this._effectWrapper = this._createEffect(texture);
this._effectWrapper.effect.executeWhenCompiled(() => {
const irradianceTexture = this._prefilterInternal(texture);
this._effectRenderer.dispose();
this._effectWrapper.dispose();
this._cdfGenerator?.dispose();
resolve(irradianceTexture);
if (onFinished) {
onFinished();
}
});
});
});
this._effectRenderer = new EffectRenderer(this._engine);
this._effectWrapper = this._createEffect(texture);
await this._effectWrapper.effect.whenCompiledAsync();

const irradianceTexture = this._prefilterInternal(texture);
this._effectRenderer.dispose();
this._effectWrapper.dispose();
this._cdfGenerator?.dispose();

return irradianceTexture;
}
}
10 changes: 10 additions & 0 deletions packages/dev/core/src/Materials/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,16 @@ export class Effect implements IDisposable {
return this._allFallbacksProcessed;
}

/**
* Wait until compilation before fulfilling.
* @returns a promise to wait for completion.
*/
public whenCompiledAsync(): Promise<Effect> {
return new Promise((resolve) => {
this.executeWhenCompiled(resolve);
});
}

/**
* Adds a callback to the onCompiled observable and call the callback immediately if already ready.
* @param func The callback to be used.
Expand Down
2 changes: 1 addition & 1 deletion packages/tools/tests/test/visualization/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,7 @@
},
{
"title": "Command encoder order in WebGPU 1",
"playgroundId": "#064AUB#121",
"playgroundId": "#064AUB#125",
"referenceImage": "CommandEncodedWebGPU1.png",
"excludedEngines": ["webgl1"]
},
Expand Down

0 comments on commit 496d7a4

Please sign in to comment.