game_render3d#
The bridge from the game core to render3d: entities with a mesh as scene instances -- crate root lib/game_render3d/src/lib.rs
game_render3d -- the bridge from the game core (lib/game) to render3d: entities with a mesh are
instances of a render3d scene, only what changed is written, the scene's origin follows the camera
MeshRef the component: a render3d mesh and material (an entity with one is drawn, at its
Pose, or at the origin without one)
Bridge::sync the world into the scene: new MeshRefs are added (Scene::add), changed ones
switched (set_mesh, set_material), gone ones removed (Scene::remove), poses that
changed written (set_transform) at the frame's alpha (Pose::interpolate); a pose
between two ticks is written every frame, a standing one once
Bridge::follow the scene's origin (Scene::set_origin) follows the camera's eye in steps of
origin_step metres (512 unless set): f32 on the GPU stays fine near the camera
Bridge::retire_* / collect meshes and materials the game is done with go from the renderer
(Renderer::remove_mesh / remove_material) once no instance of the bridge uses them
camera, transform a game View as render3d::Camera, a game Transform as render3d::Transform
(G7b part 2) Model, Image glTF models and images as lib/game assets (Assets<Model>, loaded in the background);
Models / Textures put them on a renderer once Ready and take them off when unloaded;
ModelRef (a component) and Bridge::sync_models: an entity gets its meshes when its model is
up (models.rs)
Why a crate of its own and not a module of lib/game behind a feature: a game's server is built from the
same workspace as its client, and Cargo unifies a crate's features over everything one `cargo build`
builds (build_rs.py builds an app and its BINS together). A feature "render3d" of lib/game would put
render3d, gpu and their link directives into the server's program; a crate the server does not name
never enters its graph. The library has no features besides std either (docs/CRATES.md).
What the bridge reads: the versions of the MeshRef and Pose columns (SparseSet::changed_since), the
MeshRef column's removals (a sweep over its own entries only when something left), World::pose_mark
(the poses that moved in the latest tick: between two states, written every frame). Per frame that is
a scan of one u64 per pose while something moved in the latest tick (none when no column changed), plus
the work for what changed. The bridge keeps per entity index its instance and the transform it wrote
last: a write that would change nothing is left out.
game_render3d crate root#
struct MeshRefwhat an entity looks like: a mesh and a material of the renderer the scene belongs tomesh: MeshId-- the mesh (Renderer::add_mesh)material: MaterialId-- the material (Renderer::add_material)
struct SyncStatswhat a sync didadded: u32-- instances added (Scene::add)removed: u32-- instances removed (Scene::remove)switched: u32-- meshes or materials switched (Scene::set_mesh, set_material)moved: u32-- transforms written (Scene::set_transform)visited: u32-- poses looked at (changed since the last sync, or between two ticks)failed: u32-- entities render3d refused (a mesh or material it does not have, a transform that is not finite): left out, the others go on; tried again by the next sync (a pose: when it changes again)
/// the origin's step unless set (metres): f32 has a step of 0.06 mm at 1 km from the origin
ORIGIN_STEP: f64 = 512.0
struct Bridgethe world's entities with a MeshRef as instances of one scene (see the module header)fn transform(t: &game::Transform) -> render3d::Transforma game transform as render3d's (the position stays f64)fn camera(v: &View, fov_y: f64, aspect: f64, near: f64, far: f64) -> Cameraa game view through a lens: vertical field of view (radians), width / height, near, far (f64::INFINITY: none)
impl Bridge
fn new() -> Bridgeno instances yetfn len(&self) -> u32instances the bridge holdsfn is_empty(&self) -> boolnonefn instance(&self, e: Entity) -> Option<InstanceId>the instance of entity efn mesh_users(&self, m: MeshId) -> u32instances using mesh mfn material_users(&self, m: MaterialId) -> u32instances using material mfn sync(&mut self, world: &World, scene: &mut Scene, alpha: f64) -> Result<SyncStats>the world into the scene for a frame atalpha(FixedTick::alpha; Pose::interpolate) -> what it did. The scene is the one the bridge has filled so far (another one after reset). An entity render3d refuses (a mesh or material it does not have, a removed one, a transform that is not finite) is left out and counted (SyncStats::failed); the others go on, and the next sync tries it again (so far nothing else fails: the Result stays for what would not be one entity's)fn reset(&mut self)the scene went with its device (GpuContent::detach): forget every instance, the next sync adds everything to the new scenefn clear(&mut self, scene: &mut Scene) -> Result<()>every instance of the bridge goes from the scene (the next sync adds them again)fn retire_mesh(&mut self, m: MeshId)the game is done with mesh m: collect removes it from the renderer once no instance of the bridge uses it (no entity may get a MeshRef with it afterwards)fn retire_material(&mut self, m: MaterialId)the game is done with material m (see retire_mesh)fn collect(&mut self, r: &mut Renderer) -> Result<u32>meshes and materials retired and not used by any instance of the bridge go from the renderer (Renderer::remove_mesh, remove_material; after a sync, before the next prepare) -> how many went. One the renderer refuses (another scene uses it, a mesh takes it as a detail level) stays retired for the next call; the first refusal is the error when nothing wentfn retired(&self) -> (usize, usize)meshes and materials retired and still waiting (in use)fn set_origin_step(&mut self, step: f64)the step the origin moves in (metres, a power of two keeps positions exact; at least 1)fn origin_step(&self) -> f64the origin's stepfn follow(&mut self, scene: &mut Scene, eye: [f64; 3]) -> boolthe scene's origin follows the eye: once the eye is more than one step from the origin on an axis, the origin moves to the eye rounded to the step (Scene::set_origin uploads every instance once) -> it moved
game_render3d/src/models.rs#
Source: lib/game_render3d/src/models.rs
models -- (G7b part 2) assets for render3d: glTF models and images through lib/game's Assets, on the GPU once
ready, and entities that draw a model (docs/GAMECORE.md "Assets")
Model (an Asset) a .glb / .gltf as lib/gltf reads it (its buffers and images through the VFS beside
it), decoded on the loader's thread
Image (an Asset) a PNG or JPEG as straight rgba rows
ModelRef (a component) an entity draws the model of its handle at its Pose, every instance of the model's
scene (Bridge::sync_models); nothing while the model loads or failed
Models the models on a Renderer: update(&assets, &changes, &mut renderer) puts what became
Ready on the GPU (meshes, textures, materials) and takes off what was unloaded
(Renderer::remove_mesh / remove_material / remove_texture; what instances still use
comes off with the next update, waiting()); parts(id)
Textures the same for images: texture(id) -> TextureId once it is up
Bridge::sync_models entities with a ModelRef as instances: they appear when their model is up, follow
their poses (only what changed), go with the entity, the component or the model
A model's lights and cameras are not placed (render3d::gltf::upload does that for a whole scene); a node's shear
is dropped as there. An entity's transform and a part's compose as translation, rotation and per-axis scale
(exact for uniform scales; a non-uniform scale under a rotated part would need shear).
struct Modela glTF model (an Asset): lib/gltf's Documentdoc: ::gltf::Document
struct Imagean image (an Asset): straight rgba rowswidth: u32height: u32rgba: Vec<u8>
/// the largest image an Image asset takes (pixels)
IMAGE_PIXELS: u64 = 1 << 26
struct ModelRefwhat an entity looks like: the model of a handle (all instances of its default scene), at its Posemodel: Handle<Model>
struct Parta part of a model on the GPU: one instance of its scenemesh: MeshIdmaterial: MaterialIdlocal: render3d::Transform-- where the scene puts it (model space)
struct ModelStatswhat an update of Models diduploaded: u32removed: u32models: u32-- models on the GPU now, their partsparts: u32
struct Modelsthe models of an Assets<Model> on one renderer (see the module header)
impl Models
fn new() -> Modelsfn update(&mut self, assets: &Assets<Model>, changes: &[Change], r: &mut Renderer) -> Result<ModelStats>once a frame, after Assets::update (its changes) and before the bridge: what became Ready goes up, what was unloaded comes off. The first update on a renderer (and the first after reset) puts every Ready model up. A model the renderer refuses counts as not up (its entities show nothing); the first error is returned after everything else was done. What the renderer will not take off yet (the bridge's instances still use it until sync_models) comes off with the next updatefn parts(&self, id: AssetId) -> Option<&[Part]>the parts of a model that is upfn serial(&self, id: AssetId) -> Option<u64>which upload a model that is up came from (another one after a reload or a new renderer)fn made(&self, id: AssetId) -> Option<(&[MeshId], &[MaterialId], &[TextureId])>the meshes, materials and textures a model made on the rendererfn version(&self) -> u64counts every change of what is upfn len(&self) -> usizemodels up nowfn is_empty(&self) -> boolfn waiting(&self) -> usizemeshes, materials and textures of models that came off and wait for the renderer to let them go (the next update, once no instance uses them)fn reset(&mut self)the renderer went with its device: forget what was up (the next update puts every Ready model up again)struct Texturesthe images of an Assets<Image> on one renderer (sRGB colour textures unless set otherwise)linear: bool-- the images are data, not colour (normal maps, masks): linearnearest: bool
impl Textures
fn new() -> Texturesfn update(&mut self, assets: &Assets<Image>, changes: &[Change], r: &mut Renderer) -> Result<u32>once a frame after Assets::update: Ready images up, unloaded ones off -> textures put up now. A texture that a material still names stays on the renderer until the material lets it go; each update tries again. An image the renderer refuses gives the first such error after the others went up, and is tried again by the next update (one refused image stopped every later one of the frame for good)fn texture(&self, id: AssetId) -> Option<TextureId>the texture of an image once it is upfn waiting(&self) -> usizetextures of images that came off and wait for the renderer to let them gofn reset(&mut self)the renderer went: forget every texture (the next update puts them up again)
impl Bridge
fn sync_models(&mut self, world: &World, scene: &mut Scene, alpha: f64, models: &Models) -> Result<SyncStats>the entities with a ModelRef into the scene for a frame atalpha(see the module header): an entity whose model is up (Models) gets an instance per part, at its Pose composed with the part's place; a model that is not up shows nothing; a pose that changed moves every part. Instances it adds are the bridge's (clear, reset); the counts as sync's, an entity render3d refuses left out as therefn model_entities(&self) -> u32entities drawn through sync_models now