3D engine that AI agents drive
Render the albedo and object ID passes with the beauty pass's float depth (#934)
Nguyen Xuan An6f92d4973d59
diff --git a/bench/flicker/probe.mjs b/bench/flicker/probe.mjsindex b67da119..0173cf40 100644--- a/bench/flicker/probe.mjs+++ b/bench/flicker/probe.mjs@@ -1,4 +1,5 @@// Drives the flicker viewer in headless Chrome on the GPU and prints how many pixels flicker at each camera spot.+// Add pass=albedo to a config (with facets=2) to draw the frame through the albedo render pass instead of the beauty pass (issue #934).// node bench/flicker/probe.mjs --url http://127.0.0.1:5210 --asset /@fs/D:/path/file.glb --out artifacts/flicker \// --spots spots.json --configs "reversed=1,nearFit=1;reversed=0,nearFit=0"// spots.json: [{ "name": "roofs", "eye": [x, y, z], "target": [x, y, z] }]. Without --spots it prints the scene bounds and exits.diff --git a/bench/flicker/viewer.js b/bench/flicker/viewer.jsindex 294a0833..d4d2ec46 100644--- a/bench/flicker/viewer.js+++ b/bench/flicker/viewer.js@@ -40,7 +40,7 @@ function paintTrianglesApart(parts) {triangleNumber++;const hue = ((triangleNumber * 0.618034) % 1);const colour = options.triangleIds- ? [triangleNumber & 255, (triangleNumber >> 8) & 255, (triangleNumber >> 16) & 255].map(byte => (byte / 255) ** DISPLAY_GAMMA)+ ? [triangleNumber & 255, (triangleNumber >> 8) & 255, (triangleNumber >> 16) & 255].map(byte => options.pass ? byte / 255 : (byte / 255) ** DISPLAY_GAMMA): [0, 1 / 3, 2 / 3].map(offset => 0.35 + 0.65 * (0.5 + 0.5 * Math.cos(2 * Math.PI * (hue + offset))));for (let corner = 0; corner < 3; corner++) {const source = indices[triangle * 3 + corner] * 3;@@ -101,6 +101,9 @@ const options = {facets: parameters.get('facets') === '1' || parameters.get('facets') === '2',// facets=2: the colour of every triangle is its own number, drawn unlit, so the frame tells exactly which face is shown at a pixel.triangleIds: parameters.get('facets') === '2',+ // pass=albedo draws the frame through the albedo render pass (issue #934). The pass writes the colour as it is, with no display curve,+ // so with facets=2 the triangle numbers are stored linear and the probe reads them back unchanged.+ pass: parameters.get('pass') === 'albedo' ? 'albedo' : null,// hits=N keeps the N nearest faces per ray in the ray cast (default 6, at most 64): a deeper stack than that shows up as offRay.// samples=N sets the MSAA sample count. A facets=2 frame defaults to 1: the resolve of 4 samples blends the ids of faces that share a pixel into// another valid id, which the ray through the pixel centre cannot match (see colourStepDetails); one sample shows the face under the centre.@@ -187,6 +190,7 @@ function frameFor(jitterPixels = 0) {if (!options.nearFit) frame.near = 0.1;if (jitterPixels) frame.shift = [jitterPixels / canvas.width, 0];if (effectName) Object.assign(frame, effectSettings());+ if (options.pass) frame.pass = options.pass;return frame;}@@ -241,8 +245,9 @@ async function build() {resolveCoplanar: options.offset ? resolveAndReport : false,...(effectName ? { depth: true } : {}),});+ if (options.pass) installRenderPasses(renderer);if (effectName) {- installRenderPasses(renderer);+ if (!options.pass) installRenderPasses(renderer);installCameraEffects(renderer);installLineArt(renderer);}@@ -293,7 +298,7 @@ function readFrame(context) {// edge blends two owners into a colour that is close to both, which the four-neighbour test reads as no edge, so this count also// leaves out pixels with a colour step anywhere within two pixels.// The bytes an unlit face of this colour shows: the triangle number that painted it, back from the gamma curve.-const shownByte = colour => colour.map(channel => Math.round(255 * channel ** (1 / DISPLAY_GAMMA)));+const shownByte = colour => colour.map(channel => Math.round(255 * (options.pass ? channel : channel ** (1 / DISPLAY_GAMMA))));// The ground-truth probe (ground-truth.js): one frame, compared with a float64 ray cast that cannot z-fight. Needs flat colours// per part (ids=1) or per triangle (facets=1); the trap scenes bring their own. Returns the counts of compareWithGroundTruthdiff --git a/changes/934-pass-float-depth.md b/changes/934-pass-float-depth.mdnew file mode 100644index 00000000..6da0a137--- /dev/null+++ b/changes/934-pass-float-depth.md@@ -0,0 +1 @@+- The albedo, object ID and other render passes now draw with the same reversed-Z 32-bit float depth as the beauty pass, so near-flush faces no longer flip in those buffers where the beauty image shows them steady (#934)diff --git a/src/render-passes.js b/src/render-passes.jsindex 560b029a..eecb08f4 100644--- a/src/render-passes.js+++ b/src/render-passes.js@@ -49,7 +49,8 @@ out vec4 outputColor;void main() { outputColor = vec4(texelFetch(sourceTexture, ivec2(gl_FragCoord.xy), 0).rgb, 1.0); }`;// A float target keeps the pass values as half floats (frame.passFloat) for 16-bit PNG and EXR output; the shader writes the same values.-function createPassTarget(context, width, height, float = false) {+// With reversed-Z (reversed-depth.js) the beauty pass keeps depth in a 32-bit float buffer, so a pass draws into the same format.+function createPassTarget(context, width, height, float = false, floatDepth = false) {if (float && !context.getExtension('EXT_color_buffer_float')) throw new Error('16-bit PNG and EXR output need EXT_color_buffer_float. Use format png.');const texture = context.createTexture();context.bindTexture(context.TEXTURE_2D, texture);@@ -59,7 +60,7 @@ function createPassTarget(context, width, height, float = false) {context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, context.NEAREST);const depth = context.createRenderbuffer();context.bindRenderbuffer(context.RENDERBUFFER, depth);- context.renderbufferStorage(context.RENDERBUFFER, context.DEPTH_COMPONENT24, width, height);+ context.renderbufferStorage(context.RENDERBUFFER, floatDepth ? context.DEPTH_COMPONENT32F : context.DEPTH_COMPONENT24, width, height);const framebuffer = context.createFramebuffer();context.bindFramebuffer(context.FRAMEBUFFER, framebuffer);context.framebufferTexture2D(context.FRAMEBUFFER, context.COLOR_ATTACHMENT0, context.TEXTURE_2D, texture, 0);@@ -67,7 +68,7 @@ function createPassTarget(context, width, height, float = false) {if (context.checkFramebufferStatus(context.FRAMEBUFFER) !== context.FRAMEBUFFER_COMPLETE) {throw new Error('Render pass framebuffer allocation failed. Choose a lower quality preset.');}- return { texture, depth, framebuffer, float };+ return { texture, depth, framebuffer, float, floatDepth };}/**@@ -135,12 +136,13 @@ export function installRenderPasses(renderer) {copyTextureUniform = context.getUniformLocation(copyProgram, 'sourceTexture');}const float = frame.passFloat === true;- if (target && target.float !== float) releaseTargets(context);+ const floatDepth = renderer.reversedDepth === true;+ if (target && (target.float !== float || target.floatDepth !== floatDepth)) releaseTargets(context);if (!target) {const { width, height } = renderer.canvas;- target = createPassTarget(context, width, height, float);+ target = createPassTarget(context, width, height, float, floatDepth);renderer.renderTargets.record('passColor', { width, height, format: float ? 'RGBA16F' : 'RGBA8' });- renderer.renderTargets.record('passDepth', { width, height, format: 'DEPTH_COMPONENT24' });+ renderer.renderTargets.record('passDepth', { width, height, format: floatDepth ? 'DEPTH_COMPONENT32F' : 'DEPTH_COMPONENT24' });}return mode;}diff --git a/tests/render-passes.test.js b/tests/render-passes.test.jsindex 23618afa..3fd8a560 100644--- a/tests/render-passes.test.js+++ b/tests/render-passes.test.js@@ -57,11 +57,13 @@ test('installing camera effects adds two stages once and costs nothing until a ftest('render passes need the addon, reject unknown names and bind per-pass uniforms and manifests', () => {const calls = [];+ const depthFormats = [];+ const recordedTargets = [];const context = {VERTEX_SHADER: 1, FRAGMENT_SHADER: 2, COMPILE_STATUS: 3, LINK_STATUS: 4,TEXTURE_2D: 5, RGBA16F: 6, RGBA8: 7, RGBA: 8, HALF_FLOAT: 9, UNSIGNED_BYTE: 10,TEXTURE_MIN_FILTER: 11, TEXTURE_MAG_FILTER: 12, NEAREST: 13, RENDERBUFFER: 14,- DEPTH_COMPONENT24: 15, FRAMEBUFFER: 16, COLOR_ATTACHMENT0: 17, DEPTH_ATTACHMENT: 18,+ DEPTH_COMPONENT24: 15, DEPTH_COMPONENT32F: 24, FRAMEBUFFER: 16, COLOR_ATTACHMENT0: 17, DEPTH_ATTACHMENT: 18,FRAMEBUFFER_COMPLETE: 19, DEPTH_TEST: 20, CULL_FACE: 21, TEXTURE0: 22, TRIANGLES: 23,getExtension: () => ({}),createShader: () => ({}),@@ -83,7 +85,7 @@ test('render passes need the addon, reject unknown names and bind per-pass unifotexParameteri: () => {},createRenderbuffer: () => ({ kind: 'rb' }),bindRenderbuffer: () => {},- renderbufferStorage: () => {},+ renderbufferStorage: (_target, format) => depthFormats.push(format),createFramebuffer: () => ({ kind: 'fb' }),bindFramebuffer: () => {},framebufferTexture2D: () => {},@@ -126,7 +128,7 @@ test('render passes need the addon, reject unknown names and bind per-pass unifobenchmark: false,program: {},viewProjection: new Float32Array(16),- renderTargets: { record: () => {}, release: () => {} },+ renderTargets: { record: (name, description) => recordedTargets.push([name, description.format]), release: () => {} },requireFeatureNames: () => {},drawsInOpaqueLoop: batch => batch === batchOpaque,createTexture: desc => desc,@@ -151,6 +153,23 @@ test('render passes need the addon, reject unknown names and bind per-pass unifoassert.equal(renderer.passSupport.prepare({ pass: 'aov', passFloat: true }), RENDER_PASS_MODES.aov);assert.ok(renderer.passSupport.framebuffer);assert.ok(renderer.passSupport.texture);+ assert.deepEqual(depthFormats, [context.DEPTH_COMPONENT24], 'without reversed-Z a pass keeps the 24-bit depth');++ // With reversed-Z every auxiliary pass draws into the beauty pass's 32-bit float depth, never into 24 bits (issue #934).+ renderer.reversedDepth = true;+ for (const passName of ['albedo', 'objectId', 'normal', 'depth', 'cryptomatte']) {+ depthFormats.length = 0;+ recordedTargets.length = 0;+ renderer.passSupport.releaseTargets(context);+ renderer.passSupport.prepare({ pass: passName });+ assert.deepEqual(depthFormats, [context.DEPTH_COMPONENT32F], passName);+ assert.deepEqual(recordedTargets.find(([name]) => name === 'passDepth'), ['passDepth', 'DEPTH_COMPONENT32F'], passName);+ }+ depthFormats.length = 0;+ renderer.reversedDepth = false;+ renderer.passSupport.prepare({ pass: 'albedo' });+ assert.deepEqual(depthFormats, [context.DEPTH_COMPONENT24], 'a pass target follows the depth mode of the renderer');+ renderer.passSupport.prepare({ pass: 'aov', passFloat: true });renderer.passSupport.bindFrame({ passFloat: true, previousCamera: { eye: [1, 2, 5], target: [0, 0, 0] } },{ fogDensity: 0.01, fogColor: [0.3, 0.4, 0.5] },