Log inGet started
World

Builtin Library

Branchmain
Branchesmainpublish
Commitdd348ac6c3

README.md

lightmap_bake

Bakes a lightmap on the GPU — one thread per lightmap texel of the chunk a dispatch covers. Each thread reads its world position + normal, evaluates the shadowed direct irradiance (every scene light, visibility via a BVH shadow ray), then gathers indirect irradiance by tracing indirectSamples cosine-weighted path chains of up to bounces surface interactions. Every path vertex contributes its emissive plus its albedo (carried on the triangle records by compute.buildBvh) times the shadowed direct light there; a segment that escapes contributes the sky. Direct + indirect accumulate as RGBA into output (A = coverage).

Bindings (see bindings.yaml): output (read_write), texel_positions, texel_normals, bvh_nodes, triangles, lights, bake_params, alpha_masks — all array<vec4<f32>>. bake_params holds the per-bake constants the host packs once; the params uniform the engine appends is read per dispatch.

Dispatch shape

One bake is many dispatches. Each carries its own params: stage (0 = direct light, contact occlusion and the initial indirect state; 1 = indirect chains), itemBase and itemCount (the texels of the chunk), sampleBegin and sampleEnd (the chains of the pass, stage 1), and gridWidth (the chunk's dispatch grid width in workgroups). Stage 1 adds its chains to the sums stage 0 left in output; the pass that reaches indirectSamples finalizes the indirect and direction halves. A texel's result does not depend on how the host split the work: every chain is seeded by (texel, chain index) alone. The host plans the split with Baking.planChunks so a dispatch stays inside a per-frame ray budget.

Usage: dispatch by identity with compute.dispatch("@builtin::systems.globalIllumination.lightmap_bake", ...) (buffers in bindings order) — it resolves to the shader's stable guid and auto-compiles on first use (no setup).