Skip to main content

crcbl_frame/
lib.rs

1//! Frame core: the render graph, its transient pool, texture uploads, pass
2//! timers and the UI pass — what a frame needs before it has a scene in it.
3//!
4//! ```text
5//! DrawList ──▶ UiRenderer ──adds passes──▶ RenderGraph
6//!                                               │ compile()
7//!                                               ▼
8//!                  ordered passes + exact barriers + transients
9//!                                               │ execute()
10//!                                               ▼
11//!                             crcbl-hal ──▶ crcbl-vk / null / …
12//! ```
13//!
14//! `crcbl-render` builds the engine's 3D frame on this crate and re-exports
15//! every module of it, so the engine's own callers name these items through
16//! `crcbl_render`. A program that draws only a UI — a status bar, a launcher —
17//! depends on this crate, `crcbl-hal`, a backend and `crcbl-ui`, and on nothing
18//! that knows a mesh, a material or a body of water.
19//!
20//! # The one rule
21//!
22//! **No manual barriers outside the graph, ever** — stage 2 §2.4. Passes say
23//! what they read and write; [`graph::RenderGraph::compile`] works out the
24//! transitions, the layout changes, the transient aliasing and the final
25//! states, and [`graph::CompiledGraph::execute`] is the only code in the engine
26//! that calls [`pipeline_barrier`](crcbl_hal::CommandEncoder::pipeline_barrier)
27//! during a frame.
28//!
29//! The barriers outside it are the **startup uploads** — [`texture`]'s, for
30//! every image a pass creates before its first frame, such as [`ui_pass`]'s
31//! glyph atlas — and `crcbl-render`'s own, which its crate docs list. Each runs
32//! before any frame exists and has no graph to belong to. A barrier recorded
33//! during a frame from anywhere but [`graph::CompiledGraph::execute`] is a bug.
34//!
35//! # Nothing here knows a backend
36//!
37//! This crate depends on `crcbl-hal`, `crcbl-core`, `crcbl-shaders` for the UI
38//! pass's compiled shader, and `crcbl-ui` for the draw lists that pass turns
39//! into pixels. It contains no backend and no `#[cfg(target_os = …)]`, per the
40//! HAL rules in `docs/notes/backends.md`. The graph compiles identically against
41//! [`NullBackend`](crcbl_hal::null), which is what lets the graph-compile suite
42//! run on every machine with no ICD in the room.
43//!
44//! # Example
45//!
46//! ```
47//! use crcbl_frame::graph::RenderGraph;
48//! use crcbl_frame::transient::{TransientImageDesc, TransientPool};
49//! use crcbl_hal::null::NullInstance;
50//! use crcbl_hal::{DeviceDesc, Format, ImageUsage, Instance, QueueKind, ResourceState};
51//!
52//! let instance = NullInstance::gpu_driven();
53//! let adapter = instance.adapters().remove(0);
54//! let device = instance.create_device(&DeviceDesc::for_adapter(adapter.id))?;
55//! let queue = device.queue(QueueKind::Graphics).expect("always present");
56//!
57//! // The pool outlives the graph: it owns the physical images, and it is what
58//! // remembers what the last frame left them in.
59//! let mut pool = TransientPool::new();
60//!
61//! let mut graph = RenderGraph::new(queue);
62//! let target = graph.create_image(
63//!     "target",
64//!     TransientImageDesc::new((64, 48), Format::Rgba8Unorm, ImageUsage::COLOR_ATTACHMENT),
65//! );
66//! graph
67//!     .add_render_pass("clear")
68//!     .clear_color(target, [0.0, 0.0, 0.0, 1.0])
69//!     .execute(|_| {});
70//!
71//! let compiled = graph.compile(&pool)?;
72//! assert_eq!(compiled.passes().len(), 1);
73//! // Nothing has ever used this pool, so the transient really is undefined and
74//! // the graph transitions it before use.
75//! let barriers = compiled.passes()[0].barriers();
76//! assert_eq!(barriers.images[0].from, ResourceState::Undefined);
77//! assert_eq!(barriers.images[0].to, ResourceState::ColorAttachment);
78//! # Ok::<(), Box<dyn std::error::Error>>(())
79//! ```
80
81pub mod counters;
82pub mod graph;
83pub mod mip;
84pub mod texture;
85pub mod timing;
86pub mod transient;
87pub mod ui_pass;
88
89pub use counters::{FrameCounters, INDIRECT, UNKNOWN};
90pub use graph::{
91    Attachment, BufferId, CompiledGraph, CompiledPass, GraphBarriers, GraphBufferBarrier,
92    GraphError, GraphImageBarrier, ImageId, ImportField, ImportedBuffer, ImportedImage,
93    InitialClaim, PassBuilder, PassContext, PassKind, RenderGraph,
94};
95pub use texture::{
96    ClearedTextureDesc, PendingUpload, TexturePatch, UploadBatch, UploadedTexture,
97    require_frame_queue, upload_cleared_texture, upload_texture, upload_texture_layers,
98    upload_texture_mip_layers,
99};
100pub use timing::{FrameTimings, PassTimers, PassTiming};
101pub use transient::{TransientBufferDesc, TransientImageDesc, TransientPool, TransientUse};
102pub use ui_pass::{UiRenderer, UiTexture};