// Web Worker: descarga el ONNX oficial de MoGe-2 ViT-S (revisión fijada), crea la sesión ORT-web y ejecuta la inferencia. // Las imágenes llegan como píxeles (ArrayBuffer) desde la página: no se envían a ningún servidor. import * as ort from 'onnxruntime-web/webgpu'; import { MODEL } from './config.js'; import { postprocess } from './geometry.js'; // Glue + binario WASM servidos por este Space (public/wasm), mismas versiones que el paquete npm fijado. ort.env.wasm.wasmPaths = new URL('../wasm/', self.location.href).href; let session = null, device = null, busy = false; const post = (m, t) => self.postMessage(m, t || []); async function sha256(buf) { const h = new Uint8Array(await crypto.subtle.digest('SHA-256', buf)); return Array.from(h, (b) => b.toString(16).padStart(2, '0')).join(''); } async function fetchModel() { const cacheName = 'geometria3d-lab-models-v1'; let cache = null; try { cache = await caches.open(cacheName); } catch { /* sin Cache API */ } const cached = cache && (await cache.match(MODEL.url)); if (cached) { const b = await cached.arrayBuffer(); if (b.byteLength === MODEL.bytes) return { buf: b, fromCache: true }; } const res = await fetch(MODEL.url, { credentials: 'omit' }); if (!res.ok) throw new Error(`Descarga del modelo falló: HTTP ${res.status}`); const total = +res.headers.get('content-length') || MODEL.bytes; const reader = res.body.getReader(); const out = new Uint8Array(MODEL.bytes); let got = 0, last = 0; for (;;) { const { done, value } = await reader.read(); if (done) break; if (got + value.length > out.length) throw new Error('Tamaño del modelo inesperado'); out.set(value, got); got += value.length; const now = performance.now(); if (now - last > 200) { post({ type: 'progress', loaded: got, total }); last = now; } } if (got !== MODEL.bytes) throw new Error(`Tamaño del modelo inesperado (${got} B)`); if (cache) { try { await cache.put(MODEL.url, new Response(out, { headers: { 'content-type': 'application/octet-stream' } })); } catch { /* cuota */ } } return { buf: out.buffer, fromCache: false }; } async function load({ webgpu, threads }) { const t0 = performance.now(); const { buf, fromCache } = await fetchModel(); const tDl = performance.now(); post({ type: 'progress', loaded: MODEL.bytes, total: MODEL.bytes, verifying: true }); const digest = await sha256(buf); if (digest !== MODEL.sha256) throw new Error('El SHA-256 del modelo no coincide con la revisión fijada'); ort.env.wasm.numThreads = self.crossOriginIsolated ? Math.max(1, Math.min(4, threads || 4)) : 1; let warn = null; if (webgpu && 'gpu' in navigator) { try { const adapter = await navigator.gpu.requestAdapter(); if (!adapter) throw new Error('sin adaptador WebGPU'); session = await ort.InferenceSession.create(new Uint8Array(buf), { executionProviders: ['webgpu'], graphOptimizationLevel: 'all' }); device = 'webgpu'; } catch (e) { warn = `WebGPU no disponible (${String(e.message || e).slice(0, 120)}); uso WASM/CPU.`; session = null; } } if (!session) { session = await ort.InferenceSession.create(new Uint8Array(buf), { executionProviders: ['wasm'], graphOptimizationLevel: 'all' }); device = 'wasm'; } post({ type: 'loaded', device, threads: ort.env.wasm.numThreads, fromCache, downloadMs: tDl - t0, initMs: performance.now() - tDl, warn, inputs: session.inputNames, outputs: session.outputNames }); } async function infer({ id, pixels, W, H, numTokens }) { const t0 = performance.now(); const image = new ort.Tensor('float32', new Float32Array(pixels), [1, 3, H, W]); const tokens = new ort.Tensor('int64', BigInt64Array.from([BigInt(numTokens)]), []); const out = await session.run({ image, num_tokens: tokens }); const t1 = performance.now(); const points = out.points.data, normal = out.normal.data, mask = out.mask.data, scale = out.scale.data[0]; if (out.points.dims[1] !== H || out.points.dims[2] !== W) throw new Error(`Forma inesperada ${out.points.dims}`); const geo = postprocess(points, mask, W, H, scale); const t2 = performance.now(); const nrm = new Float32Array(normal); post({ type: 'result', id, geo: { ...geo }, normal: nrm, inferMs: t1 - t0, postMs: t2 - t1, device, numTokens }, [geo.points.buffer, geo.depth.buffer, geo.valid.buffer, nrm.buffer]); for (const k of Object.keys(out)) out[k].dispose?.(); } self.onmessage = async ({ data }) => { if (busy) { post({ type: 'error', id: data.id, message: 'Ya hay un trabajo en curso; espera a que termine.' }); return; } busy = true; try { if (data.type === 'load') await load(data); else if (data.type === 'infer') { if (!session) throw new Error('Primero carga el modelo'); await infer(data); } } catch (e) { post({ type: 'error', id: data.id, stage: data.type, message: String(e?.message || e) }); } finally { busy = false; } };