Skip to main content

Engine

Struct Engine 

Source
pub struct Engine {
    pub renderer: Renderer,
    pub scene_manager: SceneManager,
    pub assets: AssetServer,
    pub input: Input,
    /* private fields */
}
Expand description

The core engine instance that orchestrates all rendering subsystems.

Engine is a pure engine implementation without window management, making it suitable for integration with various windowing systems and platforms.

§Components

  • renderer: The rendering subsystem handling GPU operations
  • scene_manager: Manages multiple scenes and active scene selection
  • assets: Central asset storage for geometries, materials, textures, etc.
  • input: Unified input state (keyboard, mouse, touch)

§Lifecycle

  1. Create with Engine::new or Engine::default
  2. Initialize GPU with Engine::init
  3. Update each frame with Engine::update
  4. Render using Renderer::begin_frame

Fields§

§renderer: Renderer§scene_manager: SceneManager§assets: AssetServer§input: Input

Implementations§

Source§

impl Engine

Source

pub fn new( init_config: RendererInitConfig, settings: RendererSettings, ) -> Engine

Creates a new engine instance with the specified configuration.

This only creates the engine configuration. GPU resources are not allocated until init is called.

§Arguments
  • init_config - Static GPU initialization parameters
  • settings - Runtime rendering settings
Source

pub async fn init<W>( &mut self, window: W, width: u32, height: u32, ) -> Result<(), Error>
where W: HasWindowHandle + HasDisplayHandle + Send + Sync + 'static,

Initializes GPU resources with the given window.

This method must be called before any rendering can occur. It accepts any type that implements the raw window handle traits, making it compatible with various windowing libraries.

§Arguments
  • window - A window that provides display and window handles
  • width - Initial surface width in pixels
  • height - Initial surface height in pixels
§Errors

Returns an error if GPU initialization fails due to:

  • No compatible GPU adapter found
  • Device request failed (unsupported features/limits)
  • Surface configuration failed
Source

pub async fn init_headless( &mut self, width: u32, height: u32, format: Option<PixelFormat>, ) -> Result<(), Error>

Initializes the GPU context in headless (offscreen) mode.

No window or surface is created. An offscreen render target of the specified dimensions is allocated instead, suitable for server-side rendering, automated testing, and GPU readback.

§Arguments
  • width — Render target width in pixels.
  • height — Render target height in pixels.
  • format — Desired pixel format. Pass None for the default Rgba8Unorm (sRGB). Use Some(Rgba16Float) for HDR readback.
§Errors

Returns an error if GPU initialization fails.

Source

pub fn readback_pixels(&mut self) -> Result<Vec<u8>, Error>

Reads back the current headless render target as raw pixel data.

The returned Vec<u8> contains tightly-packed pixel data whose byte count per pixel matches the headless texture format. A staging buffer is cached internally to avoid per-frame allocation.

§Errors

Returns an error if the renderer is not initialised or not in headless mode.

Source

pub fn submit_to_stream(&self, stream: &mut ReadbackStream) -> Result<(), Error>

Submits the current headless frame to a ReadbackStream (non-blocking).

Returns [ReadbackError::RingFull] if all ring-buffer slots are in-flight. The caller may skip the frame or drain with ReadbackStream::try_recv first.

Source

pub fn submit_to_stream_blocking( &self, stream: &mut ReadbackStream, ) -> Result<(), Error>

Submits the current headless frame to a ReadbackStream, blocking when the ring buffer is full.

Completed frames are stashed internally and can be retrieved via ReadbackStream::try_recv or ReadbackStream::try_recv_into. max_stash_size caps the number of unconsumed stashed frames to prevent unbounded memory growth.

Source

pub fn flush_stream( &self, stream: &mut ReadbackStream, ) -> Result<Vec<ReadbackFrame>, Error>

Flushes a ReadbackStream, blocking until all in-flight frames are returned.

Source

pub fn poll_device(&self)

Drives pending GPU callbacks without blocking.

Call this once per frame in a readback-stream loop so that map_async callbacks fire and frames become available.

Source

pub fn time(&self) -> f32

Returns the total elapsed time in seconds since the engine started.

Source

pub fn frame_time(&self) -> FrameTime

Source

pub fn frame_count(&self) -> u64

Returns the total number of frames rendered since startup.

Source

pub fn size(&self) -> (u32, u32)

Returns the current surface/window size in pixels as (width, height).

Source

pub fn resize(&mut self, width: u32, height: u32)

Handles window resize events.

This method should be called whenever the window size changes. It updates the renderer’s surface configuration and camera aspect ratios.

§Arguments
  • width - New width in pixels
  • height - New height in pixels
Source

pub fn update(&mut self, dt: f32)

Updates the engine state for the current frame.

This method should be called once per frame before rendering. It:

  • Processes completed background asset loads
  • Updates the total elapsed time and frame counter
  • Runs scene logic and animations
  • Resets per-frame input state
§Arguments
  • dt - Delta time since the last frame in seconds
Source

pub fn maybe_prune(&mut self)

Performs periodic resource cleanup.

This method should be called after each frame to release unused GPU resources and prevent memory leaks. It uses internal heuristics to avoid expensive cleanup operations on every frame.

Source

pub fn render_active_scene(&mut self) -> bool

Renders the active scene using the active camera.

This is a convenience method that combines scene lookup, camera extraction, and frame rendering into a single call. It avoids the split-borrow issues that arise when accessing the renderer and scene manager separately.

Returns true if a frame was successfully rendered, false if rendering was skipped (no active scene, no active camera, etc.).

Trait Implementations§

Source§

impl Default for Engine

Source§

fn default() -> Engine

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Freeze for Engine

§

impl !RefUnwindSafe for Engine

§

impl !Send for Engine

§

impl !Sync for Engine

§

impl Unpin for Engine

§

impl UnsafeUnpin for Engine

§

impl !UnwindSafe for Engine

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more