crcbl_render/lib.rs
1//! Renderer: the 3D frame — meshes, materials, lights, effects — built on
2//! `crcbl-frame`'s render graph and UI pass, everything above the HAL seam.
3//!
4//! The graph, the transient pool, the texture uploads, the pass timers and the
5//! UI pass are [`crcbl_frame`]'s, re-exported here under the paths they have
6//! always had ([`graph`], [`transient`], [`texture`], [`timing`], [`ui_pass`]),
7//! so a program that draws only a UI can depend on that crate alone.
8//!
9//! ```text
10//! camera + light ──▶ ForwardRenderer ──adds passes──▶ RenderGraph
11//! │ compile()
12//! ▼
13//! ordered passes + exact barriers + transients
14//! │ execute()
15//! ▼
16//! crcbl-hal ──▶ crcbl-vk / null / …
17//! ```
18//!
19//! # The one rule
20//!
21//! **No manual barriers outside the graph, ever** — stage 2
22//! §2.4, and it is the reason this crate exists rather than the frame loop
23//! living in each sample. Passes say what they read and write;
24//! [`graph::RenderGraph::compile`] works out the transitions, the layout
25//! changes, the transient aliasing and the final states, and
26//! [`graph::CompiledGraph::execute`] is the only code in the engine that calls
27//! [`pipeline_barrier`](crcbl_hal::CommandEncoder::pipeline_barrier) during a
28//! frame.
29//!
30//! The barriers outside it are the **startup uploads**: [`mesh_pool`]'s staging
31//! copy into the geometry pools, [`texture`]'s for every image ([`ui_pass`]'s
32//! glyph atlas and [`sprite_pass`]'s sheets), and [`draw_gen`]'s copy zeroing
33//! the LOD hysteresis state, which is device-local because a shader writes it
34//! and so cannot be zeroed by a host write. Each runs before any frame exists
35//! and has no graph to belong to; each is called out at the call site. The one
36//! that also runs later is [`texture`]'s — between two frames, never inside
37//! one, on an image no frame names yet, and blocking until its copy lands — when
38//! [`ForwardRenderer::replace_page`](forward::ForwardRenderer::replace_page)
39//! reloads a material page or
40//! [`ForwardRenderer::set_grass`](forward::ForwardRenderer::set_grass) replaces
41//! a field. There are no others, and a barrier recorded during a frame from
42//! anywhere but [`graph::CompiledGraph::execute`] is a bug.
43//!
44//! # Nothing here knows a backend
45//!
46//! This crate depends on `crcbl-hal`, `crcbl-core`, `crcbl-frame`,
47//! `crcbl-shaders`, `glam`, and the two description-only crates whose data it turns into pixels — `crcbl-ui`
48//! for [`ui_pass`]'s draw list and `crcbl-sprite` for [`sprite_pass`]'s sample
49//! modes, neither of which depends on a backend or on this crate.
50//! It contains no `ash`, no `crcbl-vk`, and no `#[cfg(target_os = …)]`, per
51//! the HAL rules in `docs/notes/backends.md` — which also name the render graph
52//! specifically as living above the seam rather than in it. The graph compiles
53//! identically against [`NullBackend`](crcbl_hal::null), which is what makes the
54//! graph-compile suite `docs/notes/process.md` calls a non-negotiable anchor
55//! run on every machine, with no ICD in the room.
56//!
57//! # Two tiers, one renderer
58//!
59//! Topic 03's rule is that **the lesser path (once "Tier B") is a
60//! constraint on data layout, not a separate renderer**, and no pass here reads
61//! a push constant. [`forward`]'s camera has been in a uniform buffer since P1
62//! for exactly that reason; [`sprite_pass`] followed it; and [`ui_pass`] — which
63//! draws every sample's HUD, and which WebGPU's lack of push constants had made
64//! this crate's one permutation axis, with a `ConstantDelivery` enum and a
65//! second `.slang` — joined them in 2026-08.
66//!
67//! So the tier costs one indirection where a push constant would have served,
68//! and buys one artifact per shader, one code path per pass, and nothing to
69//! select between at device-open time. [`ui_pass`]'s own docs carry the trade in
70//! full, including what the split had cost.
71//!
72//! # What this crate is *not*, at P1
73//!
74//! No backend registry (see below), no material *system*, no scene, no
75//! scheduler. [`material_table`] is §3.2's table and only that — a row of
76//! factors and one base-colour texture layer that an instance's id selects; the
77//! authoring, templates, permutations and the other texture slots a material
78//! system means are `docs/plan/37-materials.md`'s and are not here.
79//!
80//! The one thing on that list that has since moved whole is culling:
81//! [`cull`] is §3.3's frustum test and the CPU reference `cull.slang` is checked
82//! against, [`draw_gen`] runs that shader and turns its survivors into indirect
83//! draw arguments, and [`forward`] draws from those rather than recording a draw
84//! per object. The graph is still a linear pass list with computed barriers,
85//! which is what §2.4's risk list asks for by name: "No multi-queue scheduling,
86//! no reordering. Resist."
87//!
88//! ## The registry deliberately did not move here
89//!
90//! `crcbl-vk`'s crate docs floated moving the GPU backend registry into this
91//! crate at P1.3. It did not move, because it would make `crcbl-render` depend
92//! on `crcbl-vk` — and `crcbl-vk`'s end-to-end suite depends on `crcbl-render`,
93//! since the only honest way to test a render graph against real Vulkan is to
94//! run the real graph. That is a dependency cycle through dev-dependencies:
95//! legal in Cargo, and a confusing thing to have created in exchange for moving
96//! four lines out of the crate whose entire job is to be the one name a game
97//! depends on. The registry stays in `crcbl`.
98//!
99//! # Example
100//!
101//! ```
102//! use crcbl_hal::null::NullInstance;
103//! use crcbl_hal::{DeviceDesc, Instance, QueueKind, ResourceState};
104//! use crcbl_render::graph::RenderGraph;
105//! use crcbl_render::transient::{TransientImageDesc, TransientPool};
106//!
107//! let instance = NullInstance::gpu_driven();
108//! let adapter = instance.adapters().remove(0);
109//! let device = instance.create_device(&DeviceDesc::for_adapter(adapter.id))?;
110//! let queue = device.queue(QueueKind::Graphics).expect("always present");
111//!
112//! // The pool outlives the graph: it owns the physical images, and it is what
113//! // remembers what the last frame left them in.
114//! let mut pool = TransientPool::new();
115//!
116//! let mut graph = RenderGraph::new(queue);
117//! let scene = graph.create_image("scene", crcbl_render::targets::scene_color((64, 48)));
118//! graph
119//! .add_render_pass("clear")
120//! .clear_color(scene, [0.0, 0.0, 0.0, 1.0])
121//! .execute(|_| {});
122//!
123//! let compiled = graph.compile(&pool)?;
124//! assert_eq!(compiled.passes().len(), 1);
125//! // Nothing has ever used this pool, so the transient really is undefined and
126//! // the graph transitions it before use. A second frame through the same pool
127//! // would start from `ColorAttachment` — which is the barrier that orders it
128//! // after this one.
129//! let barriers = compiled.passes()[0].barriers();
130//! assert_eq!(barriers.images[0].from, ResourceState::Undefined);
131//! assert_eq!(barriers.images[0].to, ResourceState::ColorAttachment);
132//! # Ok::<(), Box<dyn std::error::Error>>(())
133//! ```
134
135// The frame core's modules, under the paths this crate has always given them:
136// the graph, the transient pool, the texture uploads, the mip chains and the
137// frame counters live in `crcbl-frame`, which a program that draws only a UI
138// depends on without the rest of this crate.
139pub use crcbl_frame::{counters, graph, mip, texture, transient};
140
141mod atlas_view;
142mod bind_group_cache;
143mod bloom;
144pub mod camera;
145pub mod cluster_pool;
146mod contact_shadows;
147pub mod cull;
148pub mod cull_stats;
149pub mod debug_draw;
150pub mod draw_gen;
151pub mod effects;
152mod exposure;
153pub mod fly;
154pub mod forward;
155mod fxaa;
156pub mod grass;
157pub mod grid;
158pub mod hiz;
159pub mod instance_pool;
160pub mod layers;
161pub mod light;
162pub mod light_grid;
163pub mod material_table;
164pub mod menu;
165pub mod mesh_pool;
166pub mod nine_slice;
167pub mod occlusion_cull;
168pub mod orbit;
169pub mod pass_stats;
170mod probe;
171
172/// The GPU pass that fills a probe's visibility map: a depth cube per probe,
173/// resolved into the octahedral layer `mesh.slang` reads.
174mod probe_capture;
175
176/// The compute pass that reads the irradiance probes' reflective
177/// shadow map into every probe row of a volume the scene asked to be updated.
178mod probe_gather;
179
180/// The static geometry a probe's visibility map is captured from, and the
181/// console variable that decides whether the frame reads one.
182mod probe_visibility;
183
184mod cmaa2;
185/// The reflective shadow map: the sun's near cascade drawn a second time as
186/// what it reflects, where it is and which way it faces.
187pub mod rsm;
188pub mod scene;
189pub mod shadow;
190pub mod skinning;
191mod sky_pass;
192mod split;
193pub mod sprite_pass;
194mod ssao;
195mod ssr;
196pub mod stack;
197pub mod targets;
198pub mod timing;
199pub mod ui_pass;
200mod upscale;
201mod volumetric;
202mod water;
203
204pub use camera::{Atmosphere, Camera, DirectionalLight, Fog, Projection, Sky, ViewRay};
205pub use cluster_pool::{ClusterPool, ClusterRange};
206pub use counters::{FrameCounters, INDIRECT, UNKNOWN};
207/// Re-exported because [`SheetDesc::sample`] is spelled in it: a crate whose
208/// public API names a foreign type and does not export it is one every caller
209/// has to add a dependency for.
210///
211/// [`NineSlice`] and [`Rect`] are here for the same reason —
212/// [`NineSliceSource`]'s fields are spelled in both.
213pub use crcbl_sprite::{NineSlice, Rect, SampleMode};
214/// Re-exported for the same reason: [`menu_skin`] returns a [`MenuSkin`], and
215/// a caller that can name this crate's menu art should not have to add a second
216/// dependency to lay out and draw the menu it skins. It is also what lets an
217/// end-to-end suite that depends on this crate take a golden image of the real
218/// menu rather than of a replica.
219pub use crcbl_ui::menu::{Menu, MenuItem, MenuItemLayout, MenuLayout, MenuSkin, MenuStyle};
220pub use crcbl_ui::text::FontAtlas;
221/// Re-exported for the same reason as [`Menu`]: [`MenuSkin`]'s buttons are a
222/// [`ButtonSkin`] drawn per [`ButtonState`], and its insets are
223/// [`SkinInsets`].
224pub use crcbl_ui::{ButtonSkin, ButtonState, SkinInsets};
225/// Re-exported because [`ForwardRenderer::set_water`] is spelled in them, on
226/// [`SampleMode`]'s terms below: a caller setting water should not need a
227/// dependency of its own to name what it sets.
228pub use crcbl_water::{BodyError, Medium, WaterBody};
229pub use cull::{Aabb, Frustum, visible_instances};
230pub use cull_stats::{ClusterCull, CullStats, CullStatsRing, OcclusionCull};
231pub use debug_draw::{DebugDraw, DebugVertex, frustum_corners};
232pub use draw_gen::{
233 DrawGen, DrawGenDesc, FrameCull, GeneratedDraws, OcclusionFrame, PyramidInputs, Selection,
234};
235pub use effects::{Antialiasing, EffectOverride, EffectRequest, RenderEffects, ray_tracing_note};
236pub use exposure::{ExposureAdaptation, ExposureBuffers};
237pub use fly::{Flyer, LOOK, SPEED, TURN};
238pub use forward::{
239 DEFAULT_ANISOTROPY, DebugView, EXPOSURE_MAX, EXPOSURE_MIN, ForwardOverlayTargets,
240 ForwardRenderer, FrameTargets, MAX_VIEWS, MIN_RENDER_SCALE, SCENE_CLEAR, SHADOW_LOD_BIAS,
241 SkinnedInstanceDesc, ViewBackground, ViewDesc, ViewId, ViewLighting, ViewMask, ViewTarget,
242};
243pub use graph::{
244 Attachment, BufferId, CompiledGraph, CompiledPass, GraphBarriers, GraphBufferBarrier,
245 GraphError, GraphImageBarrier, ImageId, ImportField, ImportedBuffer, ImportedImage,
246 InitialClaim, PassBuilder, PassContext, PassKind, RenderGraph,
247};
248pub use instance_pool::{
249 Instance, InstanceHandle, InstancePool, InstancePoolDesc, InstancePoolError,
250};
251pub use layers::{Layer, LayerStack, Parallax};
252pub use light::{Light, PointLight, RectLight, SpotLight, sun_row};
253pub use light_grid::{FROXEL_CAPACITY, FrameView, Grid, LightGrid, LightGridDesc};
254pub use material_table::{
255 Material, MaterialHandle, MaterialTable, MaterialTableDesc, MaterialTableError,
256};
257pub use menu::menu_skin;
258pub use mesh_pool::{
259 Mesh, MeshHandle, MeshPool, MeshPoolDesc, MeshPoolError, MeshRange, MeshUpload, UPLOAD_TIMEOUT,
260};
261pub use nine_slice::{NineQuads, NineSliceSource, SliceQuad};
262pub use occlusion_cull::OcclusionCulling;
263pub use orbit::OrbitCamera;
264pub use pass_stats::PassStats;
265pub use scene::{
266 Capacities, Geometry, InstanceDesc, MeshDesc, PageDesc, PageKind, ProbeGrid, ProbeUpdate,
267 SceneBoundsError, SceneDesc,
268};
269pub use shadow::Cascades;
270pub use skinning::{
271 SkinRange, SkinnedMesh, SkinnedRegion, Skinning, SkinningDesc, SkinningError, blend,
272 normal_basis, skin_vertex,
273};
274pub use sprite_pass::{
275 ATLAS_FORMAT, ATLAS_FORMATS, AtlasDesc, AtlasSlot, CONSTANTS_SIZE, INSTANCE_STRIDE,
276 SAMPLE_PIXEL, SAMPLE_SMOOTH, SheetDesc, SheetError, SheetId, SlotCopy, Sprite, SpriteConstants,
277 SpriteInstance, SpriteRenderer, sheet_lane,
278};
279pub use ssao::{r_ssao_bent_normals, r_ssao_blur_passes, r_ssao_slices};
280pub use stack::{
281 AmbientOcclusionPass, AntialiasingPass, AutoExposurePass, BloomPass, CameraStack,
282 ContactShadowsPass, ReflectionsPass, ShadowsPass, StackError, VolumetricFogPass,
283};
284pub use texture::{
285 ClearedTextureDesc, TexturePatch, UploadedTexture, upload_cleared_texture, upload_texture,
286 upload_texture_layers, upload_texture_mip_layers,
287};
288pub use timing::{FrameTimings, MAX_TIMED_PASSES, PassTimers, PassTiming};
289pub use transient::{TransientBufferDesc, TransientImageDesc, TransientPool, TransientUse};
290pub use ui_pass::{UiRenderer, UiTexture};
291pub use volumetric::FroxelBuffers;
292
293/// Everything this crate exposes to the debug console.
294///
295/// One list per crate, gathered by the engine at one seam — debug-console
296/// decision 2 in `docs/notes/tooling.md`, whose worked example in the plan was
297/// this crate. It holds [`debug_draw::r_debug_draw`], the switch the debug draw
298/// layer reads once a frame, the occlusion chain's own — `ssao::r_ssao_slices`
299/// and `ssao::r_ssao_blur_passes`, which `crate::ssao`'s header argues are two
300/// variables rather than one quality knob, `ssao::r_ssao_intensity` and
301/// `ssao::r_ssao_radius`, which that header argues are not on their ladder at
302/// all because neither moves the budget, and `ssao::r_ssao_bent_normals`, which
303/// is the one of them that is on by default because it is the rung rather than
304/// a purchase on top of it, and `ssao::r_ssao_split`, which is not a knob on the
305/// effect at all but the comparison seam `crate::split` describes — and the
306/// shadow cadence's two,
307/// `shadow::r_shadow_cadence` and `shadow::r_shadow_faces`, which
308/// `crate::shadow`'s cadence module argues are two for the same kind of reason:
309/// how long a map may be held and how many may be redrawn at once are different
310/// purchases — and the shadow filter's two, `shadow::r_shadow_filter` and
311/// `shadow::r_shadow_split`, which are that module's answer to the occlusion
312/// chain's technique pair: which rung of topic 45's filter
313/// ladder runs, and the seam that puts it beside the one that ships — and the
314/// sun's two bias counts, `shadow::r_shadow_bias` and
315/// `shadow::r_shadow_normal_offset`, which are two variables for the reason
316/// topic 45's seventh decision gives: one moves the compared
317/// depth towards the light and the other moves the receiver sideways along its
318/// own normal, so acne and peter-panning are pulled by different numbers and a
319/// single knob could not show the pair moving against each other.
320/// `tests/console_table.rs` is
321/// what keeps this list in step with what the source actually declares.
322#[must_use]
323pub fn console_table() -> crcbl_console::Table {
324 crcbl_console::table![
325 debug_draw::r_debug_draw,
326 ssao::r_ssao_slices,
327 ssao::r_ssao_blur_passes,
328 ssao::r_ssao_intensity,
329 ssao::r_ssao_bent_normals,
330 ssao::r_ssao_radius,
331 ssao::r_ssao_technique,
332 ssao::r_ssao_split,
333 occlusion_cull::r_occlusion_cull,
334 occlusion_cull::r_small_feature_px,
335 probe_visibility::r_probe_visibility,
336 rsm::r_probe_bounce,
337 shadow::r_shadow_cadence,
338 shadow::r_shadow_faces,
339 shadow::r_shadow_filter,
340 shadow::r_shadow_split,
341 shadow::r_shadow_bias,
342 shadow::r_shadow_normal_offset
343 ]
344}