cushy::graphics

Trait RenderOperation

Source
pub trait RenderOperation:
    Send
    + Sync
    + 'static {
    type DrawInfo;
    type Prepared: Debug + Send + Sync + 'static;

    // Required methods
    fn new(graphics: &mut Graphics<'_>) -> Self;
    fn prepare(
        &mut self,
        context: Self::DrawInfo,
        region: Rect<Px>,
        opacity: ZeroToOne,
        graphics: &mut Graphics<'_>,
    ) -> Self::Prepared;
    fn render(
        &self,
        prepared: &Self::Prepared,
        region: Rect<Px>,
        opacity: ZeroToOne,
        graphics: &mut RenderingGraphics<'_, '_>,
    );

    // Provided methods
    fn finish(
        &mut self,
        prepared: &[Prepared<Self::Prepared>],
        graphics: &mut Graphics<'_>,
    ) { ... }
    fn batch_prepared(
        &mut self,
        first: &mut Self::Prepared,
        other: Self::Prepared,
    ) -> Result<(), Self::Prepared> { ... }
}
Expand description

A custom wgpu-powered rendering operation.

§How custom rendering ops work

When Graphics::draw/Graphics::draw_with are invoked for the first time for a given RenderOperation implementor, new() is called to create a shared instance of this rendering operation. The new function is a good location to put any initialization that can be shared amongst all drawing calls, as it is invoked only once for each surface.

After an existing or newly-created operation is located, prepare() is invoked passing through the DrawInfo provided to the draw call. The result of this function is stored.

When the graphics is presented, render() is invoked providing the data returned from the prepare() function.

Required Associated Types§

Source

type DrawInfo

Data that is provided to the prepare() function. This is passed through from the draw/draw_with invocation.

Source

type Prepared: Debug + Send + Sync + 'static

Data that is created in the prepare() function, and provided to the render() function.

Required Methods§

Source

fn new(graphics: &mut Graphics<'_>) -> Self

Returns a new instance of this render operation.

Source

fn prepare( &mut self, context: Self::DrawInfo, region: Rect<Px>, opacity: ZeroToOne, graphics: &mut Graphics<'_>, ) -> Self::Prepared

Prepares this operation to be rendered in region in graphics.

This operation’s will automatically be clipped to the available space for the context it is being drawn to. The render operation should project itself into region and only use the clip rect as an optimization. To test that this is handled correctly, try placing whatever is being rendered in a Scroll widget and ensure that as the contents are clipped, the visible area shows the correct contents.

Source

fn render( &self, prepared: &Self::Prepared, region: Rect<Px>, opacity: ZeroToOne, graphics: &mut RenderingGraphics<'_, '_>, )

Render preprared to graphics at region with opacity.

This operation’s will automatically be clipped to the available space for the context it is being drawn to. The render operation should project itself into region and only use the clip rect as an optimization. To test that this is handled correctly, try placing whatever is being rendered in a Scroll widget and ensure that as the contents are clipped, the visible area shows the correct contents.

Provided Methods§

Source

fn finish( &mut self, prepared: &[Prepared<Self::Prepared>], graphics: &mut Graphics<'_>, )

Finish any operations needed before render operations begin.

This function is invoked once after all prepare functions have been invoked, but before the render functions begin.

Source

fn batch_prepared( &mut self, first: &mut Self::Prepared, other: Self::Prepared, ) -> Result<(), Self::Prepared>

Batches other into first, if possible.

This function allows render operations to participate in automatic batching performed by Cushy/Kludgine. If the same render operation type is drawn sequentially multiple times, this function is invoked with the first and second results of the prepare function. If this function returns Ok(()), self is expected to have been updated to draw both the self and other operations.

If this function returns Err, the returned preparation is enqueued.

The provided implementation returns Err(other) which avoids batching.

§Errors

This function returns an error if the render operations cannot be batched.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§