Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(quality): the cheaper dynamic GI reaches a level put in force after the stack was built - it is written on the effects each frame, not on the stack as it is made; on a phone the switch did nothing (14.9 ms) and now gives 12.3

yhh0e7364d5db25
diff --git a/sdk/src/postprocess/volumeSystem.ts b/sdk/src/postprocess/volumeSystem.ts
index db9f33dfe..b5815469b 100644
--- a/sdk/src/postprocess/volumeSystem.ts
+++ b/sdk/src/postprocess/volumeSystem.ts
@@ -197,6 +197,25 @@ export function withTemporalAntialiasing(api: PostProcessAPI, effects: ResolvedE
return out;
}
+/**
+ * The effects with the dynamic GI set to its cheaper way, where the quality level in force asks for it (see `QualityProfile.dynamicGIReduced`).
+ *
+ * It is done to the effects and not to the stack as it is built: the stack is built once and a level is put in force after it, and what is
+ * written here is part of what tells a stack its effects changed. It only ever makes the volume's own numbers cheaper.
+ */
+export function withReducedDynamicGI(api: PostProcessAPI, effects: ResolvedEffects): ResolvedEffects {
+ const gi = effects.get('dynamicGI');
+ if (!gi || !gi.enabled || !api.dynamicGIReduced) return effects;
+ const uniforms = new Map(gi.uniforms);
+ uniforms.set('u_halfRes', 1);
+ uniforms.set('u_probeStride', Math.max(uniforms.get('u_probeStride') ?? 2, 4));
+ uniforms.set('u_cardSlices', Math.max(uniforms.get('u_cardSlices') ?? 4, 8));
+ uniforms.set('u_probeDensity', Math.min(uniforms.get('u_probeDensity') ?? 1, 2 / 3));
+ const out: ResolvedEffects = new Map(effects);
+ out.set('dynamicGI', { ...gi, uniforms });
+ return out;
+}
+
/** Let a camera's volume stack go, when it has one. */
function releaseStack(api: PostProcessAPI, camera: Entity): void {
const existing = api.volumeStacks.get(camera);
@@ -212,7 +231,7 @@ function applyBlendedEffects(
blended: ResolvedEffects,
perspective: boolean,
): void {
- const effects = withTemporalAntialiasing(api, blended, perspective);
+ const effects = withReducedDynamicGI(api, withTemporalAntialiasing(api, blended, perspective));
if (effects.size === 0) {
const existing = api.volumeStacks.get(camera);
if (existing) {
@@ -275,14 +294,6 @@ function applyBlendedEffects(
} else if (def.native) {
stack.addNativePass(effectType, def.native);
for (const u of def.uniforms) stack.setUniform(effectType, u.name, effectData.uniforms.get(u.name) ?? uniformInUnits(u).defaultValue);
- // The quality level's say over what the dynamic GI spends: its cheaper way where the level asks, and never dearer than the volume set.
- if (effectType === 'dynamicGI' && api.dynamicGIReduced) {
- const held = (name: string, fallback: number): number => effectData.uniforms.get(name) ?? fallback;
- stack.setUniform(effectType, 'u_halfRes', 1);
- stack.setUniform(effectType, 'u_probeStride', Math.max(held('u_probeStride', 2), 4));
- stack.setUniform(effectType, 'u_cardSlices', Math.max(held('u_cardSlices', 4), 8));
- stack.setUniform(effectType, 'u_probeDensity', Math.min(held('u_probeDensity', 1), 2 / 3));
- }
} else if (def.compute) {
// The params block is the effect's uniforms in their listed order, each at the volume's value or the effect's own default.
stack.addComputePass(effectType, {
diff --git a/sdk/tests/quality-default.test.ts b/sdk/tests/quality-default.test.ts
index 28bffe6a4..300f9db34 100644
--- a/sdk/tests/quality-default.test.ts
+++ b/sdk/tests/quality-default.test.ts
@@ -3,7 +3,7 @@
import { describe, expect, it } from 'vitest';
import { QualityController, defaultQualityMode, defaultQualityProfile } from '../src/render/quality';
import { browserIsHandheld } from '../src/platform/web';
-import { withTemporalAntialiasing } from '../src/postprocess/volumeSystem';
+import { withReducedDynamicGI, withTemporalAntialiasing } from '../src/postprocess/volumeSystem';
import type { PostProcessAPI } from '../src/postprocess/PostProcessAPI';
describe('the level a project gets that names none', () => {
@@ -40,3 +40,28 @@ describe('the temporal smoothing a level asks for', () => {
expect(withTemporalAntialiasing(asking, listed, false).has('taa')).toBe(true);
});
});
+
+describe('the cheaper dynamic GI a level asks for', () => {
+ const effect = (uniforms: Array<[string, number]>) => new Map([['dynamicGI', { enabled: true, uniforms: new Map(uniforms), textures: new Map(), colors: new Map() }]]);
+ const asking = { dynamicGIReduced: true } as unknown as PostProcessAPI;
+ const full = { dynamicGIReduced: false } as unknown as PostProcessAPI;
+
+ it('is written on the effects, so a level put in force after the stack was built still reaches it', () => {
+ const u = withReducedDynamicGI(asking, effect([])).get('dynamicGI')!.uniforms;
+ expect([u.get('u_halfRes'), u.get('u_probeStride'), u.get('u_cardSlices')]).toEqual([1, 4, 8]);
+ expect(u.get('u_probeDensity')).toBeCloseTo(2 / 3);
+ });
+
+ it('never makes what the volume set dearer, and leaves the volume itself alone', () => {
+ const authored = effect([['u_probeStride', 8], ['u_cardSlices', 2], ['u_probeDensity', 0.5]]);
+ const u = withReducedDynamicGI(asking, authored).get('dynamicGI')!.uniforms;
+ expect([u.get('u_probeStride'), u.get('u_cardSlices'), u.get('u_probeDensity')]).toEqual([8, 8, 0.5]);
+ expect(authored.get('dynamicGI')!.uniforms.has('u_halfRes')).toBe(false);
+ });
+
+ it('does nothing at a level that runs it whole, or with no dynamic GI listed', () => {
+ const authored = effect([]);
+ expect(withReducedDynamicGI(full, authored)).toBe(authored);
+ expect(withReducedDynamicGI(asking, new Map()).size).toBe(0);
+ });
+});