Pebble
A modular ECS framework for building render engines in Rust. Pebble provides the application loop, plugin system, resource management, and a GPU asset pipeline — but makes no rendering decisions for you. Batching, depth, post-processing, shaders, and draw calls are all yours to own.
New to Pebble? Learn Pebble is a hands-on tutorial book — in the style of learn-wgpu — that builds up a real windowed, textured, camera-driven scene one chapter at a time. This Readme is the dense reference version of the same material.
[!WARNING] Pebble is built primarily for my own projects. It is shared publicly and you are free to use it, but expect breaking changes without notice. My own use cases drive priorities over external feature requests.
Design philosophy
Most graphics frameworks force you into their renderer. Pebble does the opposite: it gives you the plumbing and gets out of the way.
- Bring your own graphics API. Implement the
Backend+FrameOperationstraits for wgpu, Metal, Vulkan, or anything else. - Bring your own windowing. Implement
WindowProvider+WindowRunnerfor winit, SDL2, or a headless context. - Bring your own assets. Implement
Asset<B>to describe how a CPU-side value becomes a GPU-side value. Pebble handles the dirty queue, retry logic, and dependency ordering automatically. - Compose with plugins. Everything — windowing, the backend, asset types, game logic — is a
Plugin. Your engine is just a list of plugins wired to anApp.
Core concepts
App and plugins
new
.add_plugin
.add_plugin
.add_plugin
.build
.run;
build() runs all plugin registrations and validates that every declared resource dependency has a provider. There's no dedicated "run once at startup" stage or step — see .once() below for that. run() hands the app to the runner installed by your window plugin.
Systems and stages
Systems are plain Rust functions. Parameters are declared in the function signature and fetched automatically:
Systems are registered at a SystemStage that determines when they run each tick:
| Stage | Purpose |
|---|---|
PreUpdate |
Before main logic (e.g. input, time) |
Update |
Main game logic |
PostUpdate |
After main logic |
PreRender |
Prepare render data, poll backend |
AssetSync |
Upload CPU assets to the GPU backend |
AssetSyncDeps |
Upload assets that depend on other GPU assets |
Render |
Issue draw calls |
PostRender |
Present the frame |
AssetSync and AssetSyncDeps are prioritized: they're run to convergence (repeated until a full pass inserts no new resources) at the very front of every tick and again after every other stage, so newly queued asset/resource work is drained immediately rather than waiting for the next tick's front pass.
Systems that declare a hard requirement — a bare Res<T>/ResMut<T> parameter — are checked before their stage runs. What happens when the resource isn't there yet depends on whether anything has registered it as eventually arriving:
- If some plugin called
app.provides::<T>()(GraphicsPlugindoes this for the GPU backend;LazyResourcePlugindoes it for the type it constructs), the system just waits quietly and is retried next tick. - Otherwise,
Apppanics immediately, naming both the resource and the offending system, and suggestingapp.provides::<T>()as the fix if the timing really is expected. This catches the common case — forgettingapp.add_resource(...)— without also catching every legitimate "this arrives asynchronously" resource.
Wrapping a system in .run_if::<ResourceExists<T>>() (or any other condition — see Run conditions) fully exempts it from this check — the condition is trusted to gate correctly.
Run once
There's no separate "Startup" stage — instead, .once() turns "have I already done this" into the system's own return value, on whichever stage you register it:
app.add_system;
Return None to mean "not ready, call me again next tick"; return Some(()) to mean "done" — the system is retired permanently and never invoked again, no matter how many ticks that took. This is what replaces manually tracking a Local<bool> "already ran" flag, and it composes with the same hard-requirement checking as any other system: a bare Res<T> param is still checked (wait if provided, panic if not) before the function is even called.
Queries
Query<Q> wraps an hecs query. Iterate it directly with &mut query, or use the lookup helpers when you don't need the whole result set:
query.get(entity, |item| ...)— fetch components for one knownEntitywithout scanning the rest of the query.query.single()/query.get_single()— expect exactly one match (the player, the active camera).singlepanics if that's not true;get_singlereturnsNoneinstead.
Include Entity in Q (e.g. Query<(Entity, &Health)>) if you need the entity id back out alongside its components.
Run conditions
.run_if::<C>() gates a system (or a whole tuple passed to add_systems) behind a RunCondition, so its SystemParams are only fetched — and its body only runs — when the condition holds. It's re-checked every tick:
app.add_systems;
Built-in conditions: ResourceExists<T>, plus And<A, B> / Or<A, B> for combining conditions. Implement RunCondition yourself for anything else.
Resources
Resources are singleton values stored in the ECS world. Any hecs::Component type can be a resource:
app.add_resource;
// In a system:
Option<Res<T>> is used when a resource may not exist yet — the system receives None and can skip its work gracefully. This is the standard way to wait for things like the GPU backend, which arrives asynchronously after startup.
Events
Events<T> is a double-buffered queue: an event sent during tick N stays visible to every reader for the rest of N and all of N + 1, then is dropped — so a reader running anywhere in either tick sees it exactly once, regardless of whether it runs before or after the writer that sent it.
app.;
Each EventReader<T> keeps its own private read cursor (like Local<T>), so multiple independent readers of the same event type don't interfere with each other.
Async systems & background tasks
BackgroundTasksPlugin::new(worker_count) registers a small worker-thread pool (Res<BackgroundTasks>) for offloading work off the main thread. Three ways to use it, depending on what you need back:
| I want... | Use | Result delivery |
|---|---|---|
| A blocking closure run off-thread, native only | BackgroundTasks::spawn_blocking |
poll the returned TaskHandle<T> yourself |
A future (async/.await) run off-thread, web-compatible |
BackgroundTasks::spawn_async |
poll the returned TaskHandle<T> yourself |
| A whole system that's fire-and-forget async, no result needed | .detach() |
nothing — genuinely fire-and-forget |
| A future whose result should show up as an ordinary event | AsyncEventWriter<T> |
automatic — arrives on EventReader<T> |
spawn_blocking is the odd one out: it doesn't work on web (there are no OS threads to block on in a browser tab), which is why it's named after what makes it platform-specific rather than being the plain spawn.
For actual async/.await work (not just a blocking closure), BackgroundTasks::spawn_async drives a future to completion the same way — on a worker thread natively, on the browser's microtask queue on web, so it doesn't need to be Send there.
A task that panics doesn't just vanish: on native, spawn_blocking/spawn_async catch the panic, log it via tracing::error! with the message, and the worker thread keeps running (one bad task no longer permanently costs the pool a thread). TaskHandle::try_recv() still returns None for a panicked task — same as "still pending," for callers that don't care why — but TaskHandle::poll() returns TaskStatus::Panicked(message) instead, so code that needs to tell the two apart can:
match handle.poll
A whole system can also be fire-and-forget async via .detach(): the system runs synchronously as usual (fetching its SystemParams), but instead of doing work directly it returns a future, which the scheduler hands to BackgroundTasks::spawn_async and moves on from immediately:
+ Send + 'static
app.add_system;
A real async fn can't be used directly as a system — its returned future borrows every parameter, so it's never 'static on its own. Extract the owned pieces you need in the ordinary (synchronous) function body, then move only those into the async move block you return.
.detach() is genuinely fire-and-forget: nothing delivers the result back automatically. When you need the result, AsyncEventWriter<T> is the friendlier alternative — it combines BackgroundTasks::spawn_async with the event system so a background task's result arrives as a normal T event once it resolves, no manual polling required. It sits next to EventWriter<T> in the same reader/writer vocabulary — EventWriter::send enqueues an event now, AsyncEventWriter::spawn enqueues one once the future resolves:
app.;
WGPUBackend::readback_buffer/readback_buffer_as::<T> (in the built-in wgpu module — see Using the built-in wgpu module) return exactly this kind of future: a GPU→CPU buffer copy, driven off the main thread and delivered as an event once the GPU finishes mapping it.
The asset pipeline
The Asset<B> trait describes how a CPU-side source type is converted to a processed type using backend B:
B is generic — it need not be a GPU backend. Use B = () for CPU-to-CPU transforms (decompression, format conversion), or any other service type for audio, networking, etc.
Registering AssetPlugin::<B, T>::new() wires up the full pipeline automatically:
Assets<T::Source>— stores raw CPU data, tracks a dirty queue.ProcessedAssets<T>— stores the converted results, indexed by the same handles.- A sync system on
AssetSyncthat drains the dirty queue each tick, callingT::uploadfor each pending entry.
If upload returns None the handle is re-queued for the next tick. If a Deps resource is missing the whole sync system waits until it appears. No manual ordering or callbacks needed.
A handle that keeps requeuing forever — a permanently missing Deps resource, upload that always returns None — doesn't stay invisible: after 300 ticks stuck, the pipeline escalates from a quiet debug! to a warn! naming the asset and how long it's been retrying, repeating every 300 ticks after that rather than either going silent again or spamming every tick. LazyResource construction gets the same treatment.
Lazy resources
LazyResource<B> complements Asset<B> for resources that have exactly one instance in the whole app and need a device to be constructed, but don't come from authored data and don't need a Handle or a pool entry.
Register with LazyResourcePlugin. The plugin adds a system to AssetSyncDeps that waits for B and all Deps to be present as resources, calls construct once, inserts the result via Res<T>, and never runs again. If construct returns None it retries next tick.
.add_plugin
Good candidates: depth textures, camera uniform buffers, shared bind group layouts — anything that is one-of-a-kind and needs a backend before it can exist. If you need more than one instance of something, use Asset<B> + Handle instead.
Quick start
Add to Cargo.toml:
[]
= "0.13"
The minimal application — clear the screen to a colour:
use *;
MyWindow implements WindowProvider + WindowRunner. MyBackend implements Backend + FrameOperations. See the examples below for complete, runnable implementations using wgpu and winit.
Examples
The examples are standalone crates that share a examples/common crate providing a wgpu + winit backend implementation, except ecs_basics, which needs no window or graphics backend at all. They are ordered by complexity and each has a step-by-step README, except ecs_basics, which is documented inline.
| Example | Description |
|---|---|
| ecs_basics | No window/backend: components, resources, queries, commands, Local<T>, LazyResource, and run_if |
| clear_screen | Minimal app: open a window and clear it each frame |
| hello_triangle | Draw a triangle using the asset pipeline |
| textured_quad | Texture mapping and asset-to-asset dependencies |
| orbit_camera | 3D camera, depth buffer, lazy resources, and the full plugin system |
| wgpu_showcase | The same kind of scene as textured_quad, built with the built-in pebble::wgpu module instead of a hand-rolled Backend/Asset — see Using the built-in wgpu module |
Run any example from its directory:
Compiled shaders (SPIR-V) are pre-built in
examples/assets/shaders/compiled/. If you modify the GLSL sources, recompile them withpython3 examples/compile_shaders.py.
Using the built-in wgpu module
pebble::wgpu is a ready-made Backend/FrameOperations implementation on top of wgpu, plus a higher-level, descriptor-based layer for materials, meshes, and textures — a much shorter path than implementing Asset/Backend by hand (see Implementing a backend below for that path). One plugin replaces WindowPlugin + GraphicsPlugin + RenderPlugin + one AssetPlugin per asset type:
use *;
use ;
new
.add_plugin
.add_system
.add_system
.build
.run;
WGPUPlugin registers the mesh, material, material-instance, texture, texture-array, cubemap, compute, and sampler asset pipelines all at once — describe what you want with a *Descriptor (MeshDescriptor, MaterialDescriptor, TextureDescriptor::from_file(...), ...) and insert it into the matching Assets<T>, the same way you would with a hand-rolled Asset type. See the wgpu_showcase example for a complete scene, and WGPUBackend::readback_buffer/readback_buffer_as::<T> (covered in Async systems & background tasks) for GPU→CPU readback.
Profiler overlay (optional)
Enable the profiler Cargo feature for an opt-in CPU frame-timing/telemetry plugin with an egui-rendered overlay:
= { = "0.13", = ["profiler"] }
use ;
new
.add_plugin
.add_plugin // anywhere after WGPUPlugin
// ...
.build
.run;
Res<Profiler> gives you fps()/frame_time() anywhere, plus custom timed sections from any system:
The overlay draws in its own pass on top of whatever your own render systems already drew — no changes needed to your existing rendering code. Works on native and web (via a small, sound unsafe impl Send/Sync specific to wasm32's single-threaded execution model — see the module docs for why). CPU-side timing only for now; GPU timestamp-query sections are a planned follow-up, not in this version.
Off by default — egui/egui-wgpu aren't pulled into your build unless you enable the feature.
Implementing a backend
To use Pebble with your own graphics API, implement two traits:
FrameOperations — represents one acquired frame:
Backend — manages the swapchain and device:
init always delivers the backend through an InitSender, whether you do it synchronously (call sender.send before returning) or asynchronously (spawn a thread/task and call sender.send when ready). The framework polls the channel each PreRender tick until the backend arrives.
AcquireError::Transient (swapchain out of date, and similar) just skips the frame and retries next tick — normal, expected, no action needed. AcquireError::Fatal means acquire itself judged the failure unrecoverable (a lost device, a destroyed surface); RenderPlugin logs it once, stops calling acquire again, and inserts a RenderFailure resource instead of silently retrying a dead backend forever. Nothing panics or exits on your behalf — only your application knows whether the right response is an error screen, a full backend re-init, or something else — so react to it explicitly wherever that decision belongs:
app.add_system;
Web/wasm
Pebble targets wasm32-unknown-unknown as well as native — cargo build --target wasm32-unknown-unknown builds the library, and the built-in pebble::wgpu backend already branches internally where the two platforms need different handling (GPU backend selection, buffer-mapping/readback driven by the browser's microtask queue instead of a worker thread, and so on).
To run in a browser:
- Add a
<canvas id="wgpu_canvas"></canvas>to yourindex.html—pebble::wgpu::window::WinitWindowlooks for that element by id and renders into it. - Pulling in
web-sys/wasm-bindgen/wasm-bindgen-futures(already wasm32-only dependencies of this crate) and bundling withwasm-bindgen/trunk/wasm-packis up to your own build setup — Pebble doesn't prescribe one. BackgroundTasksPlugin's worker-thread pool has no OS threads to spawn on web, soBackgroundTasks::spawn_blocking(a blocking closure) queues jobs that never run there.BackgroundTasks::spawn_async(and everything built on it —.detach(),AsyncEventWriter<T>,WGPUBackend::readback_buffer) is web-compatible: it drives the future through the browser's microtask queue instead of a worker thread.pebble::wgpu::window::WinitWindowinstalls aconsole_error_panic_hookautomatically, so a panic shows up as a real message and Rust-side stack trace in the browser's console instead of an opaque trap — no setup needed if you're using it. Building your ownWindowProviderfor web instead means installing one yourself.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.