Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

3.3 KB
Raw Blame
// Bevy's TonyMcMapface tone-mapping table for the playable host (docs/playables.feature PA10): bevy_core_pipeline's
// tony_mc_mapface.ktx2 (48³, E5B9G9R9 floats, zstd; MIT, Tomasz Stachowiak) resampled to N³ and stored as sRGB-encoded
// RGB bytes, which host/tonemap.ts samples as Bevy's tonemapping.wesl does.
// `bun tools/tony.ts N OUT.bin` (N = 48 keeps Bevy's own size).
import { readdirSync } from "node:fs";
import { homedir } from "node:os";

const [nArg = "32", out = "src/host/tony.bin"] = process.argv.slice(2);
const N = Number(nArg);
const reg = `${homedir()}/.cargo/registry/src`;
const src = readdirSync(reg).map((d) => `${reg}/${d}/bevy_core_pipeline-0.20.0/src/tonemapping/luts/tony_mc_mapface.ktx2`).find((f) => Bun.file(f).size > 0);
if (!src) throw new Error("tony: bevy_core_pipeline 0.20.0 isn't in ~/.cargo/registry (build raea once)");
const ktx = new DataView(await Bun.file(src).arrayBuffer());
const u32 = (o: number) => ktx.getUint32(o, true);
const [format, D, superc] = [u32(12), u32(20), u32(44)];
if (format !== 123 || D !== 48 || superc !== 2) throw new Error(`tony: expected 48³ E5B9G9R9 with zstd, got format ${format}, ${D}³, supercompression ${superc}`);
// the one level's index: offset and length (u64s, small enough for a Number)
const off = Number(ktx.getBigUint64(80, true)), len = Number(ktx.getBigUint64(88, true));
const data = Bun.zstdDecompressSync(new Uint8Array(ktx.buffer, off, len));
const px = new DataView(data.buffer, data.byteOffset, data.byteLength);

/** E5B9G9R9: three 9-bit mantissas over one shared 5-bit exponent (bias 15) */
function texel(x: number, y: number, z: number): [number, number, number] {
  const v = px.getUint32(((z * D + y) * D + x) * 4, true);
  const s = 2 ** ((v >>> 27) - 15 - 9);
  return [(v & 511) * s, ((v >>> 9) & 511) * s, ((v >>> 18) & 511) * s];
}
/** the table at p in [0, 1]³, trilinear as the GPU's sampler reads it (texel centres at (i + 0.5) / D) */
function sample(p: [number, number, number]): number[] {
  const f = p.map((c) => Math.min(D - 1, Math.max(0, c * D - 0.5)));
  const i0 = f.map(Math.floor), t = f.map((c, k) => c - i0[k]);
  const i1 = i0.map((c) => Math.min(D - 1, c + 1));
  const out = [0, 0, 0];
  for (let corner = 0; corner < 8; corner++) {
    const pick = [corner & 1, (corner >> 1) & 1, (corner >> 2) & 1];
    const w = pick.reduce((m, b, k) => m * (b ? t[k] : 1 - t[k]), 1);
    const c = texel(pick[0] ? i1[0] : i0[0], pick[1] ? i1[1] : i0[1], pick[2] ? i1[2] : i0[2]);
    for (let k = 0; k < 3; k++) out[k] += w * c[k];
  }
  return out;
}
const srgb = (c: number) => (c <= 0.0031308 ? 12.92 * c : 1.055 * Math.pow(c, 1 / 2.4) - 0.055);

// Bevy reads the table at uv = e * (D - 1) / D + 0.5 / D for e = x / (x + 1) in [0, 1]: texel i holds e = i / (D - 1).
// The new table keeps that layout at its own size, so the host samples it the same way with N for D.
const at = (e: number) => e * (D - 1) / D + 0.5 / D;
const bytes = new Uint8Array(N * N * N * 3);
for (let z = 0; z < N; z++)
  for (let y = 0; y < N; y++)
    for (let x = 0; x < N; x++) {
      const c = sample([at(x / (N - 1)), at(y / (N - 1)), at(z / (N - 1))]);
      const o = ((z * N + y) * N + x) * 3;
      for (let k = 0; k < 3; k++) bytes[o + k] = Math.round(255 * srgb(Math.min(1, Math.max(0, c[k]))));
    }
await Bun.write(out, bytes);
console.log(`tony: ${N}³ from ${D}³, ${bytes.length} bytes to ${out}`);