3D engine that AI agents drive
Native tracer: confirm alpha cutout hits as ray query candidates instead of stepping (issue #626)
Nguyen Xuan Ana44dd43adbdb
diff --git a/bench/native-tracer/cutout-gpu.mjs b/bench/native-tracer/cutout-gpu.mjsnew file mode 100644index 00000000..3ad23f3c--- /dev/null+++ b/bench/native-tracer/cutout-gpu.mjs@@ -0,0 +1,42 @@+// The cutout scene of cutout.mjs on the browser's gpu (WebGL2) or webgpu backend, for the FLIP comparison with the native render.+// node bench/native-tracer/cutout-gpu.mjs <output directory> [--backend gpu|webgpu] [--leaves 300] [--samples 64] [--width 640] [--height 360]+// Writes cutout-<backend>.png. Run it through a gpu-lock job.+import { mkdir, writeFile } from 'node:fs/promises';+import { constants, setPriority } from 'node:os';+import { resolve } from 'node:path';+import { chromium } from 'playwright';+import { createServer } from 'vite';++const argumentsGiven = process.argv.slice(2);+const outputDirectory = resolve(argumentsGiven[0]);++function valueAfter(flag, fallback) {+ const index = argumentsGiven.indexOf(flag);+ return index >= 0 ? argumentsGiven[index + 1] : fallback;+}++const backend = valueAfter('--backend', 'gpu');+const leafCount = Number(valueAfter('--leaves', 300));+const options = { width: Number(valueAfter('--width', 640)), height: Number(valueAfter('--height', 360)), samples: Number(valueAfter('--samples', 64)),+ bounces: 4, seed: 0, fireflyClamp: 20, backend };+const CHROME = process.env.CHROME_PATH ?? 'C:/Program Files/Google/Chrome/Application/chrome.exe';++setPriority(0, constants.priority.PRIORITY_BELOW_NORMAL);+await mkdir(outputDirectory, { recursive: true });+const server = await createServer({ server: { host: '127.0.0.1', port: 0 }, logLevel: 'silent' });+await server.listen();+const browser = await chromium.launch({ executablePath: CHROME, headless: true, args: ['--enable-webgl', '--enable-unsafe-webgpu', '--ignore-gpu-blocklist'] });+try {+ const page = await browser.newPage({ viewport: { width: 960, height: 540 } });+ page.on('pageerror', error => console.log('PAGE ERROR', error.message));+ page.setDefaultTimeout(0);+ await page.goto(`http://127.0.0.1:${server.httpServer.address().port}/bench/reference/harness.html`, { timeout: 180000 });+ await page.waitForFunction(() => window.renderCutoutScene, null, { timeout: 180000 });+ const started = performance.now();+ const { image, report } = await page.evaluate(([count, given]) => window.renderCutoutScene(count, given), [leafCount, options]);+ await writeFile(`${outputDirectory}/cutout-${backend}.png`, Buffer.from(image, 'base64'));+ console.log(JSON.stringify({ backend, seconds: (performance.now() - started) / 1000, report }).slice(0, 400));+} finally {+ await browser.close();+ await server.close();+}diff --git a/bench/native-tracer/cutout-scene.js b/bench/native-tracer/cutout-scene.jsnew file mode 100644index 00000000..32ac9ee3--- /dev/null+++ b/bench/native-tracer/cutout-scene.js@@ -0,0 +1,57 @@+// The cutout test scene shared by the Node run (cutout.mjs) and the browser harness: a ground plane under masked leaf cards.+const TEXTURE_SIZE = 64;++// A green disc in a transparent square: texels outside the disc have alpha 0, so a cutout that is ignored shows squares.+function leafTexture(solid = false) {+ const data = new Uint8Array(TEXTURE_SIZE * TEXTURE_SIZE * 4);+ for (let row = 0; row < TEXTURE_SIZE; row++) {+ for (let column = 0; column < TEXTURE_SIZE; column++) {+ const offsetX = (column + 0.5) / TEXTURE_SIZE - 0.5;+ const offsetY = (row + 0.5) / TEXTURE_SIZE - 0.5;+ const inside = solid || Math.hypot(offsetX, offsetY) < 0.45;+ data.set([60, 170, 70, inside ? 255 : 0], (row * TEXTURE_SIZE + column) * 4);+ }+ }+ return { image: { data, width: TEXTURE_SIZE, height: TEXTURE_SIZE }, colorSpace: 'srgb', wrapS: 33071 };+}++function quad(center, along, across, size) {+ const corners = [[-1, -1], [1, -1], [1, 1], [-1, 1]].map(([first, second]) => center.map((value, axis) => value + (along[axis] * first + across[axis] * second) * size));+ const normal = [along[1] * across[2] - along[2] * across[1], along[2] * across[0] - along[0] * across[2], along[0] * across[1] - along[1] * across[0]];+ return {+ positions: new Float32Array(corners.flat()),+ normals: new Float32Array([...normal, ...normal, ...normal, ...normal]),+ uvs: new Float32Array([0, 1, 1, 1, 1, 0, 0, 0]),+ indices: new Uint32Array([0, 1, 2, 0, 2, 3]),+ };+}++let randomState = 12345;+function random() {+ randomState = (randomState * 1664525 + 1013904223) >>> 0;+ return randomState / 4294967296;+}++function tiltedAxes() {+ const angle = random() * Math.PI * 2;+ const tilt = (random() - 0.5) * 1.6;+ const along = [Math.cos(angle), 0, Math.sin(angle)];+ const across = [-Math.sin(angle) * Math.cos(tilt), Math.sin(tilt), Math.cos(angle) * Math.cos(tilt)];+ return { along, across };+}++export const CUTOUT_CAMERA = { eye: [0, 3.2, 6], target: [0, 0.8, 0], fov: 45 };+export const CUTOUT_ENVIRONMENT = { keyColor: [3, 2.8, 2.5], lightDirection: [0.4, 0.8, 0.35], pointLights: [], areaLights: [] };++export function buildCutoutParts(leafCount, masked = true, solidTexture = false) {+ randomState = 12345;+ const texture = leafTexture(solidTexture);+ const parts = [{ name: 'ground', geometry: quad([0, 0, 0], [1, 0, 0], [0, 0, -1], 8), material: { color: [0.6, 0.55, 0.45], roughness: 0.9 } }];+ for (let leaf = 0; leaf < leafCount; leaf++) {+ const { along, across } = tiltedAxes();+ const center = [(random() - 0.5) * 5, 0.2 + random() * 1.6, (random() - 0.5) * 5];+ parts.push({ name: `leaf-${leaf}`, geometry: quad(center, along, across, 0.35),+ material: { color: [1, 1, 1], roughness: 0.7, ...(masked ? { map: texture, alphaMode: 'mask', alphaCutoff: 0.5 } : {}) } });+ }+ return parts;+}diff --git a/bench/native-tracer/cutout.mjs b/bench/native-tracer/cutout.mjsindex e4bfef26..8159f8bf 100644--- a/bench/native-tracer/cutout.mjs+++ b/bench/native-tracer/cutout.mjs@@ -20,61 +20,16 @@ function valueAfter(flag, fallback) {return index >= 0 ? argumentsGiven[index + 1] : fallback;}+const label = argumentsGiven.includes('--opaque') ? '-opaque' : argumentsGiven.includes('--solid') ? '-solid' : '';const leafCount = Number(valueAfter('--leaves', 300));const options = { width: Number(valueAfter('--width', 960)), height: Number(valueAfter('--height', 540)),samples: Number(valueAfter('--samples', 64)), bounces: 4, seed: 0, fireflyClamp: 20 };-const TEXTURE_SIZE = 64;+import { CUTOUT_CAMERA, CUTOUT_ENVIRONMENT, buildCutoutParts } from './cutout-scene.js';-// A green disc in a transparent square: texels outside the disc have alpha 0, so a cutout that is ignored shows squares.-function leafTexture() {- const data = new Uint8Array(TEXTURE_SIZE * TEXTURE_SIZE * 4);- for (let row = 0; row < TEXTURE_SIZE; row++) {- for (let column = 0; column < TEXTURE_SIZE; column++) {- const offsetX = (column + 0.5) / TEXTURE_SIZE - 0.5;- const offsetY = (row + 0.5) / TEXTURE_SIZE - 0.5;- const inside = Math.hypot(offsetX, offsetY) < 0.45;- data.set([60, 170, 70, inside ? 255 : 0], (row * TEXTURE_SIZE + column) * 4);- }- }- return { image: { data, width: TEXTURE_SIZE, height: TEXTURE_SIZE }, colorSpace: 'srgb', wrapS: 33071 };-}--function quad(center, along, across, size) {- const corners = [[-1, -1], [1, -1], [1, 1], [-1, 1]].map(([first, second]) => center.map((value, axis) => value + (along[axis] * first + across[axis] * second) * size));- const normal = [along[1] * across[2] - along[2] * across[1], along[2] * across[0] - along[0] * across[2], along[0] * across[1] - along[1] * across[0]];- return {- positions: new Float32Array(corners.flat()),- normals: new Float32Array([...normal, ...normal, ...normal, ...normal]),- uvs: new Float32Array([0, 1, 1, 1, 1, 0, 0, 0]),- indices: new Uint32Array([0, 1, 2, 0, 2, 3]),- };-}--let randomState = 12345;-function random() {- randomState = (randomState * 1664525 + 1013904223) >>> 0;- return randomState / 4294967296;-}--function tiltedAxes() {- const angle = random() * Math.PI * 2;- const tilt = (random() - 0.5) * 1.6;- const along = [Math.cos(angle), 0, Math.sin(angle)];- const across = [-Math.sin(angle) * Math.cos(tilt), Math.sin(tilt), Math.cos(angle) * Math.cos(tilt)];- return { along, across };-}--const texture = leafTexture();-const parts = [{ name: 'ground', geometry: quad([0, 0, 0], [1, 0, 0], [0, 0, -1], 8), material: { color: [0.6, 0.55, 0.45], roughness: 0.9 } }];-for (let leaf = 0; leaf < leafCount; leaf++) {- const { along, across } = tiltedAxes();- const center = [(random() - 0.5) * 5, 0.2 + random() * 1.6, (random() - 0.5) * 5];- parts.push({ name: `leaf-${leaf}`, geometry: quad(center, along, across, 0.35),- material: { color: [1, 1, 1], roughness: 0.7, map: texture, alphaMode: 'mask', alphaCutoff: 0.5 } });-}-const environment = { keyColor: [3, 2.8, 2.5], lightDirection: [0.4, 0.8, 0.35], pointLights: [], areaLights: [], skyColor: [0.5, 0.6, 0.8], skyIntensity: 0.6 };-const camera = createCamera({ eye: [0, 3.2, 6], target: [0, 0.8, 0], fov: 45 }, options.width, options.height);+const parts = buildCutoutParts(leafCount, !argumentsGiven.includes('--opaque'), argumentsGiven.includes('--solid'));+const environment = CUTOUT_ENVIRONMENT;+const camera = createCamera(CUTOUT_CAMERA, options.width, options.height);setPriority(0, constants.priority.PRIORITY_BELOW_NORMAL);await mkdir(outputDirectory, { recursive: true });@@ -85,13 +40,13 @@ const nativeSeconds = (performance.now() - nativeStarted) / 1000;const linear = new Float32Array(options.width * options.height * 3);for (let pixel = 0; pixel < options.width * options.height; pixel++) linear.set(traced.rgba.subarray(pixel * 4, pixel * 4 + 3), pixel * 3);const toneMapped = toneMapToRgba8(linear, options.width, options.height, { viewTransform: 'aces', look: null });-await writeFile(`${outputDirectory}/cutout-native.png`, encodePng(toneMapped, options.width, options.height));+await writeFile(`${outputDirectory}/cutout-native${label}.png`, encodePng(toneMapped, options.width, options.height));const cpuStarted = performance.now();-const reference = await renderPathTracedImage({ parts, environment, camera: { eye: [0, 3.2, 6], target: [0, 0.8, 0], fov: 45 } },+const reference = await renderPathTracedImage({ parts, environment, camera: CUTOUT_CAMERA },{ ...options, backend: 'cpu', denoise: false, threads: 8, maxSeconds: 1800 });const cpuSeconds = (performance.now() - cpuStarted) / 1000;-await writeFile(`${outputDirectory}/cutout-cpu.png`, encodePng(reference.rgba, options.width, options.height));+await writeFile(`${outputDirectory}/cutout-cpu${label}.png`, encodePng(reference.rgba, options.width, options.height));const record = { ...options, leafCards: leafCount, triangles: scene.indices.length / 3, nativeSeconds, nativeTracedSeconds: traced.tracedSeconds, cpuSeconds, adapter: traced.adapter };await writeFile(`${outputDirectory}/cutout.json`, JSON.stringify(record, null, 1));diff --git a/bench/reference/harness.js b/bench/reference/harness.jsindex c57d71bc..41ac7bb7 100644--- a/bench/reference/harness.js+++ b/bench/reference/harness.js@@ -485,3 +485,15 @@ window.renderProgressiveCheckpoints = renderProgressiveCheckpoints;window.measureProgressiveFrameTime = measureProgressiveFrameTime;window.loadTraceSceneFor = loadTraceSceneFor;++// The masked leaf cards of bench/native-tracer/cutout-scene.js on the path tracer's gpu or webgpu backend, as the PNG of the+// tone mapped pixels (bench/native-tracer/cutout-gpu.mjs writes it).+window.renderCutoutScene = async function renderCutoutScene(leafCount, options) {+ const [{ buildCutoutParts, CUTOUT_CAMERA, CUTOUT_ENVIRONMENT }, { renderPathTracedImage }] = await Promise.all([import('/bench/native-tracer/cutout-scene.js'), import('/src/path-tracer.js')]);+ const result = await renderPathTracedImage({ parts: buildCutoutParts(leafCount), environment: CUTOUT_ENVIRONMENT, camera: CUTOUT_CAMERA }, { ...options, denoise: false });+ const canvas = document.createElement('canvas');+ canvas.width = result.width;+ canvas.height = result.height;+ canvas.getContext('2d').putImageData(new ImageData(result.rgba, result.width, result.height), 0, 0);+ return { image: canvas.toDataURL('image/png').split(',')[1], report: result.report };+};diff --git a/changes/626-cutouts.md b/changes/626-cutouts.mdnew file mode 100644index 00000000..8172f3c7--- /dev/null+++ b/changes/626-cutouts.md@@ -0,0 +1 @@+- The native tracer asks the hardware about alpha cutouts itself: cutout triangles are their own geometry and the kernel confirms a candidate hit when its colour map alpha reaches the cutoff, so rays no longer step through transparent texels (8 steps at most before).diff --git a/packages/render-native/src/kernel.wgsl b/packages/render-native/src/kernel.wgslindex 20ebadae..cc8389f8 100644--- a/packages/render-native/src/kernel.wgsl+++ b/packages/render-native/src/kernel.wgsl@@ -5,7 +5,6 @@ const TWO_PI: f32 = 6.28318530717959;const TRIANGLE_TEXELS: i32 = 9;const MATERIAL_TEXELS: i32 = 10;const LIGHT_TEXELS: i32 = 6;-const ALPHA_STEP_LIMIT: i32 = 8;const RUSSIAN_ROULETTE_START: i32 = 3;const MINIMUM_ALPHA: f32 = 1e-3;const MIRROR_ALPHA: f32 = 2e-3;@@ -36,6 +35,7 @@ struct Params {@group(0) @binding(7) var<storage, read> environmentTables: array<f32>;// Four planes of pixelCount vec4 each: radiance and depth, albedo, normal, luminance statistics.@group(0) @binding(8) var<storage, read_write> accumulation: array<vec4<f32>>;+@group(0) @binding(9) var<storage, read> primitiveSlots: array<u32>;var<private> rngState: u32 = 1u;@@ -73,22 +73,53 @@ var<private> nearestSlot: i32;var<private> nearestU: f32;var<private> nearestV: f32;-// The hardware ray query replaces the software BVH walk. The acceleration structure holds the triangles of the packed-// table in order, so a primitive index is the same slot the browser kernel finds, and the barycentrics are the weights of-// the second and third corner, as nearestU and nearestV were.+// The hardware ray query replaces the software BVH walk. The acceleration structure holds the triangles of the packed table as+// up to two geometries: the opaque triangles (committed by the hardware as they are found) and, after them, the alpha cutout+// triangles, which arrive as candidates. A candidate is confirmed only when the alpha of its colour map reaches the material's+// cutoff, so the hardware itself finds the nearest solid hit and no ray ever steps through a stack of leaves. `primitiveSlots`+// maps a hardware primitive back to its slot in the packed table (the opaque ones first, then the cutouts, which start at+// params.seedAndAtlas.w), and the barycentrics are the weights of the second and third corner, as nearestU and nearestV were.const RAY_FLAG_TERMINATE_ON_FIRST_HIT: u32 = 4u;+fn primitiveSlot(geometryIndex: u32, primitiveIndex: u32) -> i32 {+ let cutoutStart = params.seedAndAtlas.w;+ return i32(primitiveSlots[primitiveIndex + select(0u, cutoutStart, geometryIndex > 0u)]);+}++// The alpha of the colour map of the triangle in `slot` at barycentric weights (secondWeight, thirdWeight), without touching+// the hit globals.+fn alphaAtSlot(slot: i32, secondWeight: f32, thirdWeight: f32) -> f32 {+ let base = slot * TRIANGLE_TEXELS;+ let firstWeight = 1.0 - secondWeight - thirdWeight;+ let first = triangles[base + 3];+ let second = triangles[base + 4];+ let third = triangles[base + 5];+ let uv = firstWeight * vec2<f32>(first.w, triangles[base + 6].w) + secondWeight * vec2<f32>(second.w, triangles[base + 7].w) + thirdWeight * vec2<f32>(third.w, triangles[base + 8].w);+ let material = i32(triangles[base].w) * MATERIAL_TEXELS;+ let mapRectangle = materials[material + 6];+ if (mapRectangle.z <= 0.0) { return 1.0; }+ return sampleAtlas(mapRectangle, materials[material + 4].y, uv).a;+}+fn traceRay(origin: vec3<f32>, direction: vec3<f32>, limit: f32, anyHit: bool) {nearestDistance = limit;nearestSlot = -1;var query: ray_query;let flags = select(0u, RAY_FLAG_TERMINATE_ON_FIRST_HIT, anyHit);rayQueryInitialize(&query, sceneStructure, RayDesc(flags, 0xFFu, 0.0, limit, origin, direction));- while (rayQueryProceed(&query)) {}+ while (rayQueryProceed(&query)) {+ let candidate = rayQueryGetCandidateIntersection(&query);+ let slot = primitiveSlot(candidate.geometry_index, candidate.primitive_index);+ let material = i32(triangles[slot * TRIANGLE_TEXELS].w) * MATERIAL_TEXELS;+ let cutoff = materials[material + 3].z;+ if (alphaAtSlot(slot, candidate.barycentrics.x, candidate.barycentrics.y) >= cutoff) {+ rayQueryConfirmIntersection(&query);+ }+ }let hit = rayQueryGetCommittedIntersection(&query);if (hit.kind == RAY_QUERY_INTERSECTION_NONE) { return; }nearestDistance = hit.t;- nearestSlot = i32(hit.primitive_index);+ nearestSlot = primitiveSlot(hit.geometry_index, hit.primitive_index);nearestU = hit.barycentrics.x;nearestV = hit.barycentrics.y;}@@ -236,61 +267,17 @@ fn fillHit(origin: vec3<f32>, direction: vec3<f32>, distance: f32) -> f32 {return alpha;}-// The nearest solid hit, stepping through the transparent texels of cutout materials.+// The nearest solid hit; transparent texels of cutout materials never reach here (see `traceRay`).fn intersectScene(origin: vec3<f32>, direction: vec3<f32>) -> bool {- var start = origin;- var travelled = 0.0;- let farDistance = params.environmentRotationAndRays.w;- let surfaceOffset = params.environmentRotationAndRays.z;- for (var attempt = 0; attempt < ALPHA_STEP_LIMIT; attempt++) {- traceRay(start, direction, farDistance, false);- if (nearestSlot < 0) { return false; }- let foundDistance = nearestDistance;- let alpha = fillHit(start, direction, foundDistance);- if (hitCutoff == 0.0 || alpha >= hitCutoff) {- hitDistance += travelled;- return true;- }- let advance = foundDistance + surfaceOffset;- travelled += advance;- start += direction * advance;- }- return false;-}--// The alpha of the colour map at the nearest hit, without touching the hit globals a shadow ray must not disturb.-fn alphaAtNearestHit() -> f32 {- let base = nearestSlot * TRIANGLE_TEXELS;- let secondWeight = nearestU;- let thirdWeight = nearestV;- let firstWeight = 1.0 - secondWeight - thirdWeight;- let first = triangles[base + 3];- let second = triangles[base + 4];- let third = triangles[base + 5];- let uv = firstWeight * vec2<f32>(first.w, triangles[base + 6].w) + secondWeight * vec2<f32>(second.w, triangles[base + 7].w) + thirdWeight * vec2<f32>(third.w, triangles[base + 8].w);- let material = i32(triangles[base].w) * MATERIAL_TEXELS;- let mapRectangle = materials[material + 6];- if (mapRectangle.z <= 0.0) { return 1.0; }- return sampleAtlas(mapRectangle, materials[material + 4].y, uv).a;+ traceRay(origin, direction, params.environmentRotationAndRays.w, false);+ if (nearestSlot < 0) { return false; }+ fillHit(origin, direction, nearestDistance);+ return true;}fn occluded(origin: vec3<f32>, direction: vec3<f32>, limit: f32) -> bool {- var start = origin;- var remaining = limit;- let surfaceOffset = params.environmentRotationAndRays.z;- for (var attempt = 0; attempt < ALPHA_STEP_LIMIT; attempt++) {- traceRay(start, direction, remaining, true);- if (nearestSlot < 0) { return false; }- let material = i32(triangles[nearestSlot * TRIANGLE_TEXELS].w) * MATERIAL_TEXELS;- let cutoff = materials[material + 3].z;- if (cutoff == 0.0) { return true; }- if (alphaAtNearestHit() >= cutoff) { return true; }- let advance = nearestDistance + surfaceOffset;- remaining -= advance;- if (remaining <= 0.0) { return false; }- start += direction * advance;- }- return true;+ traceRay(origin, direction, limit, true);+ return nearestSlot >= 0;}// ---- Frames ----diff --git a/packages/render-native/src/lib.rs b/packages/render-native/src/lib.rsindex 26a038a9..ee9cf4f5 100644--- a/packages/render-native/src/lib.rs+++ b/packages/render-native/src/lib.rs@@ -13,6 +13,7 @@ const KERNEL_SOURCE: &str = include_str!("kernel.wgsl");/// The texel entry point of the light bake, appended to the kernel for a bake only.const TEXEL_SOURCE: &str = include_str!("texel.wgsl");const WORKGROUP_SIZE: u32 = 8;+const MATERIAL_TEXELS: usize = 10;/// Must match `TEXEL_WORKGROUP_SIZE` in texel.wgsl.const TEXEL_WORKGROUP_SIZE: u32 = 64;/// A dispatch is at most 65535 workgroups wide; a larger bake wraps onto the second dimension.@@ -194,6 +195,16 @@ fn open_device() -> Result<(wgpu::Device, wgpu::Queue, wgpu::AdapterInfo)> {Ok((device, queue, adapter.get_info()))}+/// Whether a material takes its alpha from a colour map and cuts at a threshold. A material is `MATERIAL_TEXELS` vec4s: the cutoff+/// is the third float of texel 3 and the width of the colour map's atlas rectangle the third float of texel 6.+fn material_is_cutout(materials: &[f32], material: usize) -> bool {+ let base = material * MATERIAL_TEXELS * 4;+ if base + 7 * 4 > materials.len() {+ return false;+ }+ materials[base + 3 * 4 + 2] > 0.0 && materials[base + 6 * 4 + 2] > 0.0+}+fn prepare_scene(input: &NativeTraceInput, shape: JobShape) -> Result<PreparedScene> {let setup_started = Instant::now();let triangle_count = input.triangle_count as usize;@@ -225,7 +236,7 @@ fn prepare_scene(input: &NativeTraceInput, shape: JobShape) -> Result<PreparedSc];layout_entries.extend((2..=7).map(|binding| storage_entry(binding, true)));layout_entries.push(storage_entry(8, false));- layout_entries.extend((0..shape.extra_buffers.len()).map(|offset| storage_entry(9 + offset as u32, true)));+ layout_entries.extend((0..shape.extra_buffers.len() + 1).map(|offset| storage_entry(9 + offset as u32, true)));let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {label: Some("path tracer"),entries: &layout_entries,@@ -246,16 +257,35 @@ fn prepare_scene(input: &NativeTraceInput, shape: JobShape) -> Result<PreparedSclet compile_seconds = compile_started.elapsed().as_secs_f64();let upload_started = Instant::now();- // The corners of every triangle: the table stores a corner and two edges in its first three texels.- let mut vertices: Vec<f32> = Vec::with_capacity(triangle_count * 9);- for triangle in triangle_data.chunks_exact(TRIANGLE_FLOATS) {+ // The corners of every triangle: the table stores a corner and two edges in its first three texels. Opaque triangles come+ // first and alpha cutout triangles after them, because the hardware takes its opaque flag per geometry; `slot_map`+ // gives the table slot of each hardware primitive (the opaque ones, then the cutouts).+ let mut opaque_vertices: Vec<f32> = Vec::with_capacity(triangle_count * 9);+ let mut cutout_vertices: Vec<f32> = Vec::new();+ let mut slot_map: Vec<u32> = Vec::with_capacity(triangle_count);+ let mut cutout_slots: Vec<u32> = Vec::new();+ for (slot, triangle) in triangle_data.chunks_exact(TRIANGLE_FLOATS).enumerate() {let corner = [triangle[0], triangle[1], triangle[2]];let edge_first = [triangle[4], triangle[5], triangle[6]];let edge_second = [triangle[8], triangle[9], triangle[10]];+ let is_cutout = material_is_cutout(&input.materials, triangle[3] as usize);+ let vertices = if is_cutout { &mut cutout_vertices } else { &mut opaque_vertices };vertices.extend_from_slice(&corner);vertices.extend((0..3).map(|axis| corner[axis] + edge_first[axis]));vertices.extend((0..3).map(|axis| corner[axis] + edge_second[axis]));+ if is_cutout {+ cutout_slots.push(slot as u32);+ } else {+ slot_map.push(slot as u32);+ }}+ let opaque_count = slot_map.len();+ slot_map.extend(cutout_slots);+ let mut vertices = opaque_vertices;+ vertices.extend(cutout_vertices);+ // Word 47 of the parameter block tells the kernel where the cutout primitives start in `slot_map`.+ let mut parameter_bytes = input.parameters.to_vec();+ parameter_bytes[47 * 4..48 * 4].copy_from_slice(&(opaque_count as u32).to_le_bytes());let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {label: Some("triangle corners"),contents: bytemuck::cast_slice(&vertices),@@ -269,6 +299,7 @@ fn prepare_scene(input: &NativeTraceInput, shape: JobShape) -> Result<PreparedScstorage_buffer(&device, "environment colors", bytemuck::cast_slice(&input.environment_colors)),storage_buffer(&device, "environment tables", bytemuck::cast_slice(&input.environment_tables)),];+ buffers.push(storage_buffer(&device, "primitive slots", bytemuck::cast_slice(&slot_map)));let accumulation = device.create_buffer(&wgpu::BufferDescriptor {label: Some("accumulation"),size: shape.accumulation_bytes,@@ -280,21 +311,32 @@ fn prepare_scene(input: &NativeTraceInput, shape: JobShape) -> Result<PreparedSc}let parameters = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {label: Some("parameters"),- contents: &input.parameters[..],+ contents: ¶meter_bytes,usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,});let upload_seconds = upload_started.elapsed().as_secs_f64();let build_started = Instant::now();- let geometry_size = wgpu::BlasTriangleGeometrySizeDescriptor {- vertex_format: wgpu::VertexFormat::Float32x3,- vertex_count: (triangle_count * 3) as u32,- index_format: None,- index_count: None,- // Opaque: the kernel steps through the transparent texels of a cutout itself (see `intersectScene`), so the hardware- // never runs an any-hit shader and every hit it returns is a candidate surface.- flags: wgpu::AccelerationStructureGeometryFlags::OPAQUE,- };+ // Opaque triangles never reach the any-hit loop of the kernel; cutout triangles are candidates it confirms when the alpha+ // of their colour map reaches the material's cutoff (see `traceRay`).+ let mut geometry_sizes: Vec<wgpu::BlasTriangleGeometrySizeDescriptor> = Vec::new();+ let mut geometry_vertex_starts: Vec<u32> = Vec::new();+ for (first_triangle, triangles_in_geometry, flags) in [+ (0, opaque_count, wgpu::AccelerationStructureGeometryFlags::OPAQUE),+ (opaque_count, triangle_count - opaque_count, wgpu::AccelerationStructureGeometryFlags::empty()),+ ] {+ if triangles_in_geometry == 0 {+ continue;+ }+ geometry_vertex_starts.push((first_triangle * 3) as u32);+ geometry_sizes.push(wgpu::BlasTriangleGeometrySizeDescriptor {+ vertex_format: wgpu::VertexFormat::Float32x3,+ vertex_count: (triangles_in_geometry * 3) as u32,+ index_format: None,+ index_count: None,+ flags,+ });+ }let blas = device.create_blas(&wgpu::CreateBlasDescriptor {label: Some("scene"),@@ -302,7 +344,7 @@ fn prepare_scene(input: &NativeTraceInput, shape: JobShape) -> Result<PreparedScupdate_mode: wgpu::AccelerationStructureUpdateMode::Build,},wgpu::BlasGeometrySizeDescriptors::Triangles {- descriptors: vec![geometry_size.clone()],+ descriptors: geometry_sizes.clone(),},);let mut tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {@@ -317,20 +359,25 @@ fn prepare_scene(input: &NativeTraceInput, shape: JobShape) -> Result<PreparedSc0,0xFF,));+ let triangle_geometries: Vec<wgpu::BlasTriangleGeometry> = geometry_sizes+ .iter()+ .zip(&geometry_vertex_starts)+ .map(|(size, first_vertex)| wgpu::BlasTriangleGeometry {+ size,+ vertex_buffer: &vertex_buffer,+ first_vertex: *first_vertex,+ vertex_stride: 12,+ index_buffer: None,+ first_index: None,+ transform_buffer: None,+ transform_buffer_offset: None,+ })+ .collect();let mut build_encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("build") });build_encoder.build_acceleration_structures(std::iter::once(&wgpu::BlasBuildEntry {blas: &blas,- geometry: wgpu::BlasGeometries::TriangleGeometries(vec![wgpu::BlasTriangleGeometry {- size: &geometry_size,- vertex_buffer: &vertex_buffer,- first_vertex: 0,- vertex_stride: 12,- index_buffer: None,- first_index: None,- transform_buffer: None,- transform_buffer_offset: None,- }]),+ geometry: wgpu::BlasGeometries::TriangleGeometries(triangle_geometries),}),std::iter::once(&tlas),);diff --git a/packages/render-native/src/texel.wgsl b/packages/render-native/src/texel.wgslindex 46b428e1..3c07e9eb 100644--- a/packages/render-native/src/texel.wgsl+++ b/packages/render-native/src/texel.wgsl@@ -3,8 +3,8 @@// surface with a cosine weighted direction, so the radiance it returns is the light that arrives at the texel; the bake// multiplies by the albedo. Plane 0 of the accumulation holds the radiance sums, plane 1 the sums of first rays that left// the scene. Same random numbers and weights as `bakeTexels` in src/path-tracer-webgpu-kernel.js.-@group(0) @binding(9) var<storage, read> texelPoints: array<vec4<f32>>;-@group(0) @binding(10) var<storage, read> texelNormals: array<vec4<f32>>;+@group(0) @binding(10) var<storage, read> texelPoints: array<vec4<f32>>;+@group(0) @binding(11) var<storage, read> texelNormals: array<vec4<f32>>;const TEXEL_WORKGROUP_SIZE: u32 = 64u;