diff --git a/test/components/Material_Anisotropy.test.ts b/test/components/Material_Anisotropy.test.ts index f4ac7a74..599a0f6d 100644 --- a/test/components/Material_Anisotropy.test.ts +++ b/test/components/Material_Anisotropy.test.ts @@ -1,5 +1,5 @@ -import { test, expect, end, waitUntil, readRegionRGBA16F } from '../util' -import { AnisotropyUtil, BoxGeometry, CameraUtil, Color, DirectLight, Engine3D, KHR_materials_anisotropy, LitMaterial, MeshRenderer, Object3D, Scene3D, SphereGeometry, Time, Vector3, View3D } from '@orillusion/core'; +import { test, expect, end, waitUntil, frames, readRegionRGBA16F } from '../util' +import { AnisotropyUtil, BoxGeometry, CameraUtil, Color, DirectLight, Engine3D, KHR_materials_anisotropy, LitMaterial, MeshRenderer, Object3D, Scene3D, SphereGeometry, Vector3, View3D } from '@orillusion/core'; const engine = await Engine3D.init({ canvasConfig: { devicePixelRatio: 1 } }); engine.setting.render.tonemap.mode = 'None'; @@ -77,9 +77,7 @@ const tonemapPost = (): any => (engine.getRenderJob(view) as any)?.graph?.getPas /** Width and height (px) of the region brighter than half the peak, measured through the brightest pixel. */ async function highlight(strength: number, rotation: number): Promise<{ w: number; h: number; peak: number }> { mat.anisotropyRotation = rotation; mat.anisotropy = strength; - // Software WebGPU in CI can take longer than 500 ms per frame, so wait for rendered frames, not wall-clock time. - const target = Time.frame + 3; - await waitUntil(() => Time.frame >= target, 30000, 10); + await frames(); const t: GPUTexture = tonemapPost().renderTexture.getGPUTexture(); const img = await readRegionRGBA16F(engine.context3D.device, t, 0, 0, t.width, t.height); let peak = -1, px = 0, py = 0; diff --git a/test/components/Material_Iridescence.test.ts b/test/components/Material_Iridescence.test.ts index a0057481..d10f9e11 100644 --- a/test/components/Material_Iridescence.test.ts +++ b/test/components/Material_Iridescence.test.ts @@ -1,4 +1,4 @@ -import { test, expect, end, waitUntil, delay, readRowRGBA16F } from '../util' +import { test, expect, end, waitUntil, frames, readRowRGBA16F } from '../util' import { BoxGeometry, CameraUtil, Color, DirectLight, Engine3D, IridescenceUtil, KHR_materials_iridescence, LitMaterial, MeshRenderer, Object3D, Scene3D, Vector3, View3D } from '@orillusion/core'; const engine = await Engine3D.init({ canvasConfig: { devicePixelRatio: 1 } }); @@ -76,7 +76,7 @@ engine.startRenderView(view); const tonemapPost = (): any => (engine.getRenderJob(view) as any)?.graph?.getPass('PostPass')?.postList?.get('TonemapPost'); async function measure(factor: number, thickness: number, ior = 1.3): Promise { mat.iridescenceIOR = ior; mat.iridescenceThickness = thickness; mat.iridescence = factor; - await delay(450); + await frames(); const t: GPUTexture = tonemapPost().renderTexture.getGPUTexture(); return (await readRowRGBA16F(engine.context3D.device, t, t.width >> 1, t.height >> 1, 1))[0].slice(0, 3); } diff --git a/test/components/Material_Sheen.test.ts b/test/components/Material_Sheen.test.ts index 58758c89..be531258 100644 --- a/test/components/Material_Sheen.test.ts +++ b/test/components/Material_Sheen.test.ts @@ -1,4 +1,4 @@ -import { test, expect, end, waitUntil, delay } from '../util' +import { test, expect, end, waitUntil, frames } from '../util' import { BoxGeometry, CameraUtil, Color, DirectLight, Engine3D, KHR_materials_sheen, LitMaterial, MeshRenderer, Object3D, Scene3D, Vector3, View3D } from '@orillusion/core'; const engine = await Engine3D.init({ canvasConfig: { devicePixelRatio: 1 } }); @@ -87,7 +87,7 @@ async function readCenter(): Promise<[number, number, number]> { async function measure(sheen: Color, roughness: number) { mat.sheenRoughness = roughness; mat.sheenColor = sheen; - await delay(500); // define flip → pipeline rebuild → a few frames + await frames(); return readCenter(); } diff --git a/test/components/PostEffects_Lens.test.ts b/test/components/PostEffects_Lens.test.ts index 7cbd0485..36c6db5a 100644 --- a/test/components/PostEffects_Lens.test.ts +++ b/test/components/PostEffects_Lens.test.ts @@ -1,4 +1,4 @@ -import { test, expect, end, waitUntil, delay, readRowRGBA16F } from '../util' +import { test, expect, end, waitUntil, frames, readRowRGBA16F } from '../util' import { BoxGeometry, CameraUtil, Color, Engine3D, LensPost, LensUtil, MeshRenderer, Object3D, PostProcessingComponent, Scene3D, UnLitMaterial, Vector3, View3D } from '@orillusion/core'; await test('LensUtil: vignette and aberration math', async () => { @@ -54,7 +54,7 @@ await test('LensPost attaches, all-zero settings are a passthrough', async () => await waitUntil(() => (engine.getRenderJob(view) as any)?.graph?.getPass('PostPass')?.postList?.get('TonemapPost')?.renderTexture && lens.renderTexture, 15000); expect(engine.setting.render.postProcessing.lens.enable).tobe(true); off(); - await delay(400); + await frames(); const c = (await px(0.3, 0.5))[0], corner = (await px(0.02, 0.04))[0]; grey = c[0]; console.log('[Lens] grey wall center', c.slice(0, 3).map(v => v.toFixed(4)).join(','), 'corner', corner.slice(0, 3).map(v => v.toFixed(4)).join(',')); @@ -65,7 +65,7 @@ await test('LensPost attaches, all-zero settings are a passthrough', async () => await test('vignette: GPU falloff matches LensUtil at several screen points, and tints toward vignetteColor', async () => { off(); lens.vignetteIntensity = 0.7; lens.vignetteSmoothness = 0.3; lens.vignetteRoundness = 1; - await delay(400); + await frames(); const t = tonemapRT(); const aspect = t.width / t.height; for (const [u, v] of [[0.3, 0.5], [0.1, 0.5], [0.3, 0.1], [0.05, 0.9], [0.5, 0.02]]) { @@ -78,7 +78,7 @@ await test('vignette: GPU falloff matches LensUtil at several screen points, and } lens.vignetteColor = new Color(0.3, 0, 0, 1); lens.vignetteIntensity = 5; // fully vignetted everywhere but the very centre - await delay(400); + await frames(); const tinted = (await px(0.05, 0.1))[0]; expect(tinted[0]).toSubequal(0.3, 0.01); expect(tinted[1] < 0.01 && tinted[2] < 0.01).tobe(true); @@ -87,7 +87,7 @@ await test('vignette: GPU falloff matches LensUtil at several screen points, and await test('chromatic aberration: R and B split at an off-axis edge, none on a flat region', async () => { off(); - await delay(400); + await frames(); const t = tonemapRT(); // Locate the grey→white edge on the middle row (expected near u = 0.75). const row = await px(0.6, 0.5, Math.floor(t.width * 0.3)); @@ -99,7 +99,7 @@ await test('chromatic aberration: R and B split at an off-axis edge, none on a f expect(Math.abs(before[0] - before[2]) < 0.004).tobe(true); // neutral grey without the effect lens.chromaticAberration = 4; - await delay(400); + await frames(); const shiftPx = LensUtil.chromaticOffset(edgeU, 0.5, 4)[0] * t.width; console.log('[Lens] expected red/blue shift', shiftPx.toFixed(2), 'px'); expect(shiftPx > 4).tobe(true); @@ -116,28 +116,29 @@ await test('chromatic aberration: R and B split at an off-axis edge, none on a f await test('grain: adds per-pixel variance, static flag freezes it, off restores a flat image', async () => { off(); lens.grainIntensity = 0.5; - await delay(400); + await frames(); const a = await px(0.2, 0.3, 16); const mean = a.reduce((s, p) => s + p[0], 0) / a.length; const varA = a.reduce((s, p) => s + (p[0] - mean) ** 2, 0) / a.length; console.log('[Lens] grain mean', mean.toFixed(4), 'std', Math.sqrt(varA).toFixed(4)); expect(Math.sqrt(varA) > 0.02).tobe(true); expect(Math.abs(mean - grey) < 0.12).tobe(true); // zero-mean noise + await frames(1); const b = await px(0.2, 0.3, 16); expect(a.map(p => p[0].toFixed(5)).join()).tobe(b.map(p => p[0].toFixed(5)).join()); // grainStatic → identical frames lens.grainStatic = false; - await delay(300); + await frames(); const c = await px(0.2, 0.3, 16); expect(a.map(p => p[0].toFixed(5)).join() !== c.map(p => p[0].toFixed(5)).join()).tobe(true); // animated off(); - await delay(400); + await frames(); const flat = await px(0.2, 0.3, 16); expect(flat.every(p => Math.abs(p[0] - grey) < 0.004)).tobe(true); }) await test('removePost detaches and clears the enable flag', async () => { post.removePost(LensPost); - await delay(200); + await frames(); expect(engine.setting.render.postProcessing.lens.enable).tobe(false); expect((engine.getRenderJob(view) as any).graph.getPass('PostPass').postList.has('LensPost')).tobe(false); }) diff --git a/test/components/PostEffects_Tonemap.test.ts b/test/components/PostEffects_Tonemap.test.ts index 59d44ae7..e6f9860a 100644 --- a/test/components/PostEffects_Tonemap.test.ts +++ b/test/components/PostEffects_Tonemap.test.ts @@ -1,4 +1,4 @@ -import { test, expect, end, waitUntil, delay } from '../util' +import { test, expect, end, waitUntil, frames } from '../util' import { BoxGeometry, CameraUtil, Color, Engine3D, MeshRenderer, Object3D, Scene3D, TonemapMode, TonemapUtil, UnLitMaterial, Vector3, View3D } from '@orillusion/core'; const near = (a: number, b: number, eps: number) => Math.abs(a - b) <= eps; @@ -96,7 +96,7 @@ let hdr: [number, number, number] = [0, 0, 0]; await test("'None' passes the HDR scene color through (values above 1 survive)", async () => { const tm = engine.setting.render.tonemap; tm.mode = 'None'; tm.exposure = 1; - await delay(400); + await frames(); hdr = await readCenter(); console.log('[Tonemap] scene-linear input at center:', hdr.map(v => v.toFixed(4)).join(', ')); expect(hdr[0] > 1.5).tobe(true); // really HDR, not clamped upstream @@ -108,7 +108,7 @@ for (const mode of ['ACES', 'Reinhard', 'Uncharted2', 'AgX', 'Neutral'] as Tonem const tm = engine.setting.render.tonemap; for (const exposure of [1, 0.5]) { tm.mode = mode; tm.exposure = exposure; tm.whitePoint = 4; - await delay(300); + await frames(); const gpu = await readCenter(); const cpu = TonemapUtil.apply(mode, hdr, exposure, 4); console.log(`[Tonemap] ${mode} e=${exposure} gpu=(${gpu.map(v => v.toFixed(4))}) cpu=(${cpu.map(v => v.toFixed(4))})`); @@ -122,12 +122,12 @@ for (const mode of ['ACES', 'Reinhard', 'Uncharted2', 'AgX', 'Neutral'] as Tonem await test('whitePoint drives Reinhard (higher white point → darker highlights)', async () => { const tm = engine.setting.render.tonemap; tm.mode = 'Reinhard'; tm.exposure = 1; tm.whitePoint = 2; - await delay(300); + await frames(); const a = await readCenter(); const cpu = TonemapUtil.apply('Reinhard', hdr, 1, 2); expect(near(a[0], cpu[0], 0.01)).tobe(true); tm.whitePoint = 16; - await delay(300); + await frames(); const b = await readCenter(); expect(b[0] < a[0]).tobe(true); // higher white point → darker highlights tm.mode = 'ACES'; tm.whitePoint = 4; diff --git a/test/components/WaterRenderer.test.ts b/test/components/WaterRenderer.test.ts index e30d3b8d..46107d35 100644 --- a/test/components/WaterRenderer.test.ts +++ b/test/components/WaterRenderer.test.ts @@ -1,5 +1,5 @@ -import { test, end, expect, delay, waitUntil } from '../util' -import { Engine3D, Scene3D, View3D, Camera3D, Object3D, WaterRenderer, WaterMaterial, Vector2, Vector3, DirectLight, GeometryBase, VertexAttributeName, WaterSpectrum, Time } from '@orillusion/core' +import { test, end, expect, delay, waitUntil, frames } from '../util' +import { Engine3D, Scene3D, View3D, Camera3D, Object3D, WaterRenderer, WaterMaterial, Vector2, Vector3, DirectLight, GeometryBase, VertexAttributeName, WaterSpectrum } from '@orillusion/core' /** Height (g) and Jacobian-related stats of an rgba16float texture's channel, read back from the GPU. */ async function channelStats(engine: Engine3D, texture: any, channel: number) { @@ -32,12 +32,6 @@ async function channelStats(engine: Engine3D, texture: any, channel: number) { return { mean, rms: Math.sqrt(sq / n - mean * mean) } } -/** Waits for `count` whole frames begun after the call; a frame is done once the next one starts. Frames with the FFT take seconds under software rendering. */ -async function frames(count: number) { - const until = Time.frame + count + 1 - await waitUntil(() => Time.frame >= until, 30000) -} - /** Flat disc in the xz plane: a center vertex and rings of segments. */ function discGeometry(radius: number, rings = 16, segments = 48) { const count = 1 + rings * segments @@ -101,11 +95,11 @@ await test('WaterRenderer renders and animates without errors', async () => { scene.addChild(obj) expect(water.waterMaterial instanceof WaterMaterial).toEqual(true) expect(water.waterMaterial.transmissionFactor).toEqual(1) - await delay(300) + await frames(1) expect(water.time > 0).toEqual(true) water.timeScale = 0 const t = water.time - await delay(100) + await frames(1) expect(water.time).toEqual(t) engine.dispose() }) @@ -332,6 +326,7 @@ for (const extra of [{}, { useLogDepth: true }, { zPrePass: false, msaa: 4 }]) a cameraObj.y = i % 2 ? -1 : 3 await delay(80) } + await frames(1) expect(water.hasRipples && pondWater.hasRipples).toEqual(true) engine.dispose() }) @@ -398,6 +393,7 @@ for (const extra of [{}, { useLogDepth: true }, { zPrePass: false, msaa: 4 }]) a cameraObj.y = y await delay(200) } + await frames(1) expect(water.hasSimulation).toEqual(true) engine.dispose() }) diff --git a/test/util.ts b/test/util.ts index 84ff1bb3..9d339081 100644 --- a/test/util.ts +++ b/test/util.ts @@ -1,3 +1,5 @@ +import { Time } from '@orillusion/core' + let target = sessionStorage.target.split('/').pop() let result: { [key: string]: any } = {} let totalS = 0, totalF = 0 @@ -185,4 +187,14 @@ async function readRegionRGBA16F(device: GPUDevice, texture: GPUTexture, x: numb return rows; } -export { test, expect, end, delay, waitUntil, readRowRGBA16F, readRegionRGBA16F } \ No newline at end of file +/** + * Wait until `count` whole frames that began after this call have rendered. + * CI runs WebGPU on SwiftShader at ~2 fps or slower, so a wall-clock delay after + * changing a setting may not cover a single frame and a readback sees the old image. + */ +async function frames(count: number = 2) { + const until = Time.frame + count + 1 + await waitUntil(() => Time.frame >= until, 30000, 10) +} + +export { test, expect, end, delay, waitUntil, frames, readRowRGBA16F, readRegionRGBA16F }