The kernel language built so a coding agent can write, see inside, and tune world code fast.
A world's systems (a planet's heights, erosion, climate, its surfaces) are written as kernels in brokernel, and one kernel source compiles through brass to SPIR-V for the GPU and to native code for the CPU. The same function that fills a terrain tile on the GPU answers a height query for physics, the camera or placement on the CPU, so there is no second copy to keep in step.
world code (an app's .bk files)
│ brokernel: parse, check, AD / quadrature, lower
▼
brass MIR (KernelBuilder)
├── SpirvTarget → SPIR-V (compute, vertex, fragment) for Vulkan
└── brass native → x64 / arm64 functions for the CPU
- brass does the code generation. Its SPIR-V target lowers MIR built with
KernelBuilderto verified SPIR-V 1.5/1.6 for compute, vertex and fragment stages. brokernel is a frontend over it, as bronze (JavaScript) and brasslua (Lua) are frontends over brass's CPU backends. - bro.kernel (
src/api/) exposes the compiler, CPU/GPU kernel execution, function harnesses, probes, knobs, and constant/layout reflection to bronze.
brokernel is a statically typed language in the family of WGSL, designed for rapid world iteration by a coding agent:
-
Named probes (
probe("label", expr)). Wrap any scalar or vector subexpression inprobe("cu", cu)inside any function, compute kernel, or fragment stage. It returns its argument unchanged and compiles to a zero-cost no-op in normal builds. When probed (kernel.probe(...),fn.probe(...),CompiledKernel::probe_cpu/probe_gpu, or fragment stage probing at@binding(4, 23)), it records the exact intermediate value at the target cell or pixel ({ cu: 0.041, ridge: 0.72, ... }) without debug packing into color outputs. -
Knobs without source edits (
@knob const). Mark a tunable constant@knob const CU_CAP: f32 = 0.06;. Override any set of knobs at compile time or on a compiled module/kernel (mod.withKnobs({ CU_CAP: 0.04 }),k.knobs({ CU_CAP: 0.04 }).dispatch(...),CompileResult::with_knobs) to sweep parameters in-process without rewriting.bkfiles.mod.knobsreflects all declared knobs and their current values. -
Forward-mode automatic differentiation (
grad).grad(f, x, ...extra)differentiates any functionfin scope with respect to its first parameterx(f32,f64, orvec2..vec4for scalar-returningf), returning the exact analytical derivative or gradient vector$\nabla f(x)$ . Terrain slopes, B-spline derivatives, soil gradients, and flow directions stay in sync with the height function automatically. -
Quadrature and footprint moments (
expect,moments,cell_expect).-
expect(f, mean, sigma, ...extra)evaluates$\mathbb{E}[f(X)]$ for$X \sim \mathcal{N}(\text{mean}, \sigma^2)$ via 3-point Gauss-Hermite quadrature (for scalar or vector returns). -
moments(f, mean, sigma, ...extra)returnsvec2(E[f(X)], Var[f(X)])from the same 3 quadrature points. -
cell_expect(f, center, du, dv, ...extra)integratesf(center + s*du + t*dv, ...)over$[-0.5, 0.5]^2$ via$2\times 2$ Gauss-Legendre quadrature for coarse LOD cell averages.
-
-
Tests over functions, not pixels (
mod.fn(name)). Call anypub fndirectly from JavaScript without writing a@kernelwrapper by hand:-
f.eval(...args)evaluates one point on the CPU. -
f.run(count, ...args)runscountinvocations on the CPU where each argument can be either a broadcast scalar/vector or a per-invocationTypedArray. -
f.dispatch(count, ...args)runscountinvocations on the GPU withKernelBuffers for per-invocation arguments.
-
-
Modules by name & re-exports (
pub import/pub use).-
importanduseare synonyms (import a::b::*;,use a::b::{x, y};,from a::b import x, y;). - Prefixing with
pub(pub import sub::*;,pub use sub::{foo, BAR};,pub from sub import foo;) re-exports those functions, structs, constants, and descriptor bindings through the module, so a facade module can split internals across files without breaking callers. -
bro.kernel.resolve(fn)andbro.kernel.registerModule(name, source)(andCompiler::set_module_resolverin C++) resolve imported modules on demand by name ("world::fields"), so callers never have to thread a{ modules }map by hand.
-
-
Shared declarations with JS (
mod.consts,mod.enums,mod.structLayout,bro.kernel.registerConstants).- Every
pub const(and re-exportedpub const) in a module is reflected onmod.constsas a JS number, boolean, or array (CompileResult::constantsin C++). -
pub enum Name : i32|u32 { ... }declarations (andpub newtype Name = T;, compile-timesizeof<T>()/offsetof<S, field>()) provide strong nominal types, reflected onmod.enums(CompileResult::enumsin C++). -
bro.kernel.registerConstants(name, decls)generates and registers a.bkconstants/enums module from JS values,[type, value]tuples, or{ __enum: 'i32'|'u32', ...variants }objects. -
mod.structLayout(name)returns{ name, byteSize, std140Size, std140Align, std430Size, std430Align, fields: [{ name, type, byteOffset, std140Offset, std430Offset }] }so host code and kernels share one source of truth for buffer layouts.
- Every
-
Runtime diagnostics & parity (
check,trap_non_finite,probe_if,compare_cpu_gpu).-
check("label", cond)returnscondas abooland records the first failing(x, y, z)cell coordinate when compiled withenable_checks/{ enableChecks: true }or inspected viakernel.check(...)/CompiledKernel::check_cpu/check_gpu(which also traps NaN/Inf store and return writes whentrap_non_finite/trapNonFiniteis enabled). -
probe_if("label", cond, expr)captures up tomaxSamplesconditional samples with coordinates viakernel.probeIf(...)/probe_if_cpu/probe_if_gpu. -
compare_cpu_gpu/kernel.compareCpuGpu(grid, ...args, { relTol, absTol, ulpTol })reportsworst_coord(worstCoord),cpu_val(cpuVal), andgpu_val(gpuVal).
-
-
Standalone CLI checker (
brokernel-check).- Type-checks and compiles
.bkfiles or directories (-I <dir>,--resolve <prefix>=<dir>,--verify,--no-spirv,--enable-checks,--json).
- Type-checks and compiles
- Generics. Write a noise or filter once over its dimension and scalar type.
- GPU code is f32; f64 is the CPU's. A kernel built for the GPU that
reaches
f64is a compile error at thef64site. Precision far from the origin comes from anchors: compute an anchor position inf64on the CPU and passf32offsets to the GPU. A kernel marked@cpuhas no GPU build and may usef64freely (e.g., planetary height queries for physics and camera). - Grids, stencils and bounded stores. A kernel is written per point of a
grid (
grid<1..3>); neighbour reads arestencil<T, D>the compiler sees;read_store/write_store/rw_storecarry their element bounds on both CPU and GPU so out-of-bounds reads return zero and out-of-bounds writes do nothing. Grid linear indices (g.linear_index(),g.total_cells()) are 64-bit. - Vertex and fragment stages (
@vertex,@fragment). Stage inputs at@location(n)(@flatsupported), outputs viaset_vertex_output,set_frag_output,set_frag_color,set_vertex_position,set_frag_depth,set_frag_depth_less(a depth never above the rasterized one, so early depth tests still run), and builtinsvertex_index(),instance_index(),frag_coord(),front_facing(),discard_fragment(). - Descriptor resources (
@binding(set, binding)). Combined samplers (sampler2d<T>,sampler_cube<T>, shadow samplers), separatetexture*andsampler, storage images (image2d<T>,@format(...)),uniform<T>(std140), andbuffer<T>/read_buffer<T>(std430). Module-scope@binding(set, binding) pub var camera: uniform<Camera>;declarations are bound automatically by any stage that reaches them. - Fast math & CPU vectorization. GPU builds use GLSL.std.450 fast math; CPU
builds use FMA and Cephes polynomials (
src/lowering_math.cpp), vectorizing grid loops 8-wide (or 4-wide for 64-bit) on AVX2 hosts (src/lowering_ops.cpp).
Source files use the .bk extension.
Compiler::compile_source (and everything built on it: compile_file,
with_knobs, with_sources, probed variants, bro.kernel.compile) loads a
result from disk when brokernel::set_compile_cache({dir, max_bytes}) names a
directory (include/brokernel/compile_cache.h; off by default, and per
compiler with Compiler::set_use_compile_cache(false)).
- Key: brass's build (
brass::cache::KeyBuilder::add_brass_build: brass's version and binary, the target, the host's AVX/AVX2/FMA), brokernel's build (the binary holding brokernel's code: path, size, mtime; orCompileCacheConfig::build_stamp) and version, the format of an entry's blobs, the source, its file name, everyCompileOptionsfield (knob overrides in any order) and every module the compile read (the import as asked, where it resolved, a hash of its source), given by name or found by a resolver or search path alike. A lookup resolves each again as the compiler would now, without parsing, so one source compiled against different modules of the same names (a scene stage beside each of its hooks) keeps an entry for each, and a module the compile never reads (one registered for other sources) is no part of its key. Which modules a source asks for is kept beside its entries (the last 8 lists, for a source whose modules import different ones). Only successful compiles are stored. - A hit is the compile: every
CompileResultandCompiledKernelfield (SPIR-V, reflection, types, constants, probes, costs, hashes, diagnostics), with source, options, modules, search paths and resolver from the compiler. A probed variant is compiled when first asked for. - CPU builds are native code in the cache: a kernel's CPU build, made on
its first CPU run whether its compile hit or missed, is compiled into a
shared library image through
brass::cache::NativeModuleCache(keyed by the entry, the module records, the kernel and its CPU options) and loaded; a later run, in this process or another, loads the image and compiles nothing. Only when no image is stored does a hit's kernel compile the source again for that kernel (a@cpukernel's too), once: the image is stored then. A build that cannot be an image (one calling a function it does not define, which no kernel does today) is compiled in memory by KernelJit as without a cache.compile_cache_stats()countscpu_loads,cpu_storesandcpu_in_memory. Through the JS API with a wait pump, a@cpukernel's CPU build begins on a thread of its own as its compile ends, and its firsteval/runwaits for it pumped;module.cpuReady()is a promise settled once every one is built, the frames going on. One KernelJit compile makes a kernel's point and grid functions both (they share a module).CompileResult::cache_status(Off/Hit/Miss) andcache_note(why it missed) say which;compile_cache_stats()counts them. - Store: the directory is a brass artifact cache
(
brass::cache::ArtifactCache, brassdocs/artifact_cache.md): one<key>.bacfile per entry (a compile'smodulesandresultblobs; a CPU build's image, and its metadata beside it), written to a temporary file and renamed, so threads and processes share a directory; both key digests and a checksum in the header make a truncated, damaged, colliding or foreign entry a miss. Images load from private copies underload/. A hit marks its entry used; a store that takes the directory pastmax_bytes(whole 4 KiB units, images included) has a background thread delete the least recently used entries down to 7/8 of it, andflush_compile_cache()waits for that trim. Files of the cache brokernel kept before (*.bkc) are never read and are deleted when a cache is set on their directory. Deleting the directory is always safe.
| Path | What |
|---|---|
include/brokernel/brokernel.h |
public C++ API: compile_source / compile_file / Compiler, CompileResult (with_knobs, constants, probes), CompiledKernel (CPU/GPU dispatch, point eval, probe_cpu, probe_gpu, compare_cpu_gpu) |
include/brokernel/{ast,types,sema,lowering,module_loader,diagnostics}.h |
compiler stages and ModuleRegistry |
include/brokernel/compile_cache.h, src/compile_cache.cpp, src/compile_result_io.cpp |
the on-disk compile cache over brass's artifact cache: configuration, keys and lookup around compile_source, CPU builds as cached native images, a result's byte encoding |
src/lexer.cpp, src/parser.cpp |
source to AST (import / use, pub import, @knob, probe) |
src/sema*.cpp, src/types.cpp |
name resolution, re-exports, generics, type checking, forward-mode AD (src/sema_ad.cpp), quadrature (expect, moments, cell_expect) |
src/lowering_*.cpp |
AST to brass MIR: GPU SPIR-V entry points, CPU point/grid functions, and CPU/GPU/fragment probes |
src/stdlib_*.cpp |
built-in modules std::math, std::noise, std::filter, std::sphere, std::color |
src/api/api.h, src/api/api*.cpp |
bronze JS bindings (bro.kernel): compile, compileAsync (off-thread compiles under a host wait pump, api_async.cpp), resolve, registerModule, KernelModule (kernel, fn, cpuReady, consts, knobs, withKnobs, structLayout), Kernel (eval, run, dispatch, probe, knobs), KernelBuffer |
tests/ |
unit and parity tests for lexer/parser, sema, modules/generics, SPIR-V stages, CPU/GPU parity, math, FastNoise2 parity, and agent wishlist features (test_wishlist.cpp) |
bench/bench_hillclimb*.cpp |
parity and speed harness for std::noise, 2D stencils, and planetary f64 height fields against FastNoise2 and glslc |
Windows:
cmake -B build
cmake --build build --config Release
ctest --test-dir build -C Release --output-on-failureLinux / macOS:
cmake -B build-release -DCMAKE_BUILD_TYPE=Release
cmake --build build-release
ctest --test-dir build-release --output-on-failureMIT (see LICENSE).