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.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
import constants::WORLD_CACHE_SIZE;
import bevy_pbr::render::utils::{rand_f, rand_vec2f};
import bevy_render::maths::orthonormalize;
import package::realtime::bindings::{
world_cache,
constants,
};
enable wgpu_ray_query;
/// Maximum amount of frames a cell can live for without being queried
const WORLD_CACHE_CELL_LIFETIME: u32 = 10u;
/// Maximum amount of attempts to find a cache entry after a hash collision
const WORLD_CACHE_MAX_SEARCH_STEPS: u32 = 3u;
/// Marker value for an empty cell
const WORLD_CACHE_EMPTY_CELL: u32 = 0u;
@if(!WORLD_CACHE_NON_ATOMIC_LIFE_BUFFER)
fn query_world_cache(world_position_in: vec3<f32>, world_normal: vec3<f32>, view_position: vec3<f32>, ray_t: f32, cell_lifetime: u32, rng: ptr<function, u32>) -> vec3<f32> {
return query_world_cache_found(world_position_in, world_normal, view_position, ray_t, cell_lifetime, rng).rgb;
}
/// raea: the cell's light, and in w 1 when the cell holds light, 0 when the query found none: the cell was made by this
/// query, has had no update yet, or is lost to collisions. Read as light, such a miss is a zero, and a lamp shade's
/// lining, whose bounces chain from cell to cell, lost a third of them (photo 1's desk, 0.82 of the path tracer's)
@if(!WORLD_CACHE_NON_ATOMIC_LIFE_BUFFER)
fn query_world_cache_found(world_position_in: vec3<f32>, world_normal: vec3<f32>, view_position: vec3<f32>, ray_t: f32, cell_lifetime: u32, rng: ptr<function, u32>) -> vec4<f32> {
var world_position = world_position_in;
var cell_size = get_cell_size(world_position, view_position, ray_t, rng);
@if(!NO_JITTER_WORLD_CACHE) {
// raea (11-25): while the camera holds still with a steady cache (world_cache_still), no jitter and one LOD band
// (get_cell_size), so a surface has one key a cell and each cell converges. With them, a held frame kept making
// keys (a jittered point, a band picked by chance), and the young cells' few samples kept MALL RAT's held court
// patchy where a frozen cache wasn't (raea light steady). A moving camera keeps both, which hide the cells' edges
if !world_cache_still() {
// Jitter query point, which essentially blurs the cache a bit so it's not so grid-like
// https://tomclabault.github.io/blog/2025/regir, jitter_world_position_tangent_plane
let TBN = orthonormalize(world_normal);
let offset = (rand_vec2f(rng) * 2.0 - 1.0) * cell_size * 0.5;
world_position += offset.x * TBN[0] + offset.y * TBN[1];
cell_size = get_cell_size(world_position, view_position, ray_t, rng);
}
}
let world_position_quantized = bitcast<vec3<u32>>(quantize_position(world_position, cell_size));
let world_normal_quantized = bitcast<vec3<u32>>(quantize_normal(world_normal));
// raea: the plane the surface lies in, along its normal, in quarter cells, and the cell's size, are part of a
// cell's key. Without the plane, surfaces facing the same way across a cavity the size of a cell (a lamp shade's
// lining on one side, its dark cladding on the other) shared cells, and the lining lost a sixth of its light
// (LightBench emitter.tsx cavity-8: 0.84 of the path tracer's at 15 cm cells). The jitter stays in the tangent
// plane, so a flat surface keeps one plane. Without the size, cells of two LOD levels could share a key
// Measured from the cell's centre, not the world's origin: from the origin, a curved surface a metre or more away
// changed planes every half degree of its normal, cut into strips of cells too thin to be found again
let cell_centre = (quantize_position(world_position, cell_size) + 0.5) * cell_size;
let plane = bitcast<u32>(i32(floor(dot(world_position - cell_centre, world_normal) / (cell_size * 0.25))));
let level = bitcast<u32>(cell_size);
var key = compute_key(world_position_quantized, world_normal_quantized, plane, level);
let checksum = compute_checksum(world_position_quantized, world_normal_quantized, plane, level);
for (var i = 0u; i < WORLD_CACHE_MAX_SEARCH_STEPS; i++) {
let cas = atomicCompareExchangeWeak(&world_cache.checksums[key], WORLD_CACHE_EMPTY_CELL, checksum);
let existing_checksum = cas.old_value;
// Cell already exists or is empty - reset lifetime
if existing_checksum == checksum || existing_checksum == WORLD_CACHE_EMPTY_CELL {
@if(!WORLD_CACHE_QUERY_ATOMIC_MAX_LIFETIME)
atomicStore(&world_cache.life[key], cell_life(cell_lifetime));
@else
atomicMax(&world_cache.life[key], cell_life(cell_lifetime));
}
if existing_checksum == checksum {
// Cache entry already exists - get radiance
let radiance = world_cache.radiance[key];
return vec4(radiance.rgb, select(0.0, 1.0, radiance.a > 0.0));
} else if existing_checksum == WORLD_CACHE_EMPTY_CELL && cas.exchanged {
// Cell is empty - initialize it
world_cache.geometry_data[key].world_position = world_position;
world_cache.geometry_data[key].world_normal = world_normal;
return vec4(0.0);
} else {
// Collision - linear probe to next entry
key += 1u;
}
}
return vec4(0.0);
}
fn get_cell_size(world_position: vec3<f32>, view_position: vec3<f32>, ray_t: f32, rng: ptr<function, u32>) -> f32 {
let camera_distance = distance(view_position, world_position) / constants.world_cache_position_lod_scale;
let lod_f = log2(1.0 + camera_distance);
let lod_fract = fract(lod_f);
let coin = rand_f(rng);
// raea: one band while the camera holds (world_cache_still); the random pick between two bands otherwise
let lod = floor(lod_f) + select(select(0.0, 1.0, coin < lod_fract * lod_fract * lod_fract), select(0.0, 1.0, lod_fract > 0.5), world_cache_still());
return constants.world_cache_position_base_cell_size * exp2(lod);
}
// raea (11-25): bit 1 of constants.world_cache_steady (prepare.rs): a steady cache with the camera where it was last frame
// raea (11-25): a queried cell's life, SolariLighting::world_cache_cell_lifetime where set (bits 16-31 of
// constants.world_cache_steady), else the caller's (Solari's 10)
fn cell_life(caller: u32) -> u32 { let own = constants.world_cache_steady >> 16u; return select(caller, own, own != 0u); }
fn world_cache_still() -> bool { return (constants.world_cache_steady & 2u) != 0u; }
fn quantize_position(world_position: vec3<f32>, quantization_factor: f32) -> vec3<f32> {
return floor(world_position / quantization_factor + 0.0001);
}
fn quantize_normal(world_normal: vec3<f32>) -> vec3<f32> {
return floor(world_normal + 0.0001);
}
fn compute_key(world_position: vec3<u32>, world_normal: vec3<u32>, plane: u32, level: u32) -> u32 {
var key = pcg_hash(world_position.x);
key = pcg_hash(key + world_position.y);
key = pcg_hash(key + world_position.z);
key = pcg_hash(key + world_normal.x);
key = pcg_hash(key + world_normal.y);
key = pcg_hash(key + world_normal.z);
key = pcg_hash(key + plane);
key = pcg_hash(key + level);
return wrap_key(key);
}
fn compute_checksum(world_position: vec3<u32>, world_normal: vec3<u32>, plane: u32, level: u32) -> u32 {
var key = iqint_hash(world_position.x);
key = iqint_hash(key + world_position.y);
key = iqint_hash(key + world_position.z);
key = iqint_hash(key + world_normal.x);
key = iqint_hash(key + world_normal.y);
key = iqint_hash(key + world_normal.z);
key = iqint_hash(key + plane);
key = iqint_hash(key + level);
return max(key, 1u); // 0u is reserved for WORLD_CACHE_EMPTY_CELL
}
fn pcg_hash(input: u32) -> u32 {
let state = input * 747796405u + 2891336453u;
let word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return (word >> 22u) ^ word;
}
fn iqint_hash(input: u32) -> u32 {
let n = (input << 13u) ^ input;
return n * (n * n * 15731u + 789221u) + 1376312589u;
}
fn wrap_key(key: u32) -> u32 {
return key & (u32(WORLD_CACHE_SIZE) - 1u);
}