Skip to main content

GpuPlugin

Trait GpuPlugin 

Source
pub trait GpuPlugin: Send + 'static {
    // Provided methods
    fn priority(&self) -> i32 { ... }
    fn type_name(&self) -> &'static str { ... }
    fn init_gpu(&mut self, _device: &Device) { ... }
    fn on_device_recreated(&mut self, _device: &Device, _queue: &Queue) { ... }
    fn pre_prepare(
        &mut self,
        _device: &Device,
        _queue: &Queue,
        _ctx: &GpuFrameContext<'_>,
    ) -> Vec<CommandBuffer> { ... }
    fn post_paint(
        &mut self,
        _device: &Device,
        _queue: &Queue,
        _targets: &PostPaintTargets<'_>,
        _ctx: &GpuFrameContext<'_>,
    ) -> Vec<CommandBuffer> { ... }
}
Expand description

A plugin that encodes GPU work into the per-frame command stream.

Register with ViewportRuntime::with_gpu_plugin. Each frame, after runtime.step() and before renderer.prepare(), the host calls ViewportRuntime::pre_prepare, which invokes every registered plugin’s pre_prepare in ascending priority order and returns the concatenated command buffers for submission.

Registration model: GPU plugins are multi-instance, like RuntimePlugin and unlike item-type plugins. Registering two of the same type runs both in priority order; the runtime does not deduplicate. This is intentional and frozen behavior.

§Example

use viewport_lib::runtime::{GpuFrameContext, GpuPlugin, gpu_phase};

struct WarpPlugin {
    pipeline: Option<wgpu::ComputePipeline>,
    bind_group: Option<wgpu::BindGroup>,
}

impl GpuPlugin for WarpPlugin {
    fn priority(&self) -> i32 { gpu_phase::PRE_PREPARE }

    fn init_gpu(&mut self, device: &wgpu::Device) {
        // build pipeline and bind group, store on self
    }

    fn pre_prepare(
        &mut self,
        device: &wgpu::Device,
        _queue: &wgpu::Queue,
        _ctx: &GpuFrameContext<'_>,
    ) -> Vec<wgpu::CommandBuffer> {
        let mut enc = device.create_command_encoder(&Default::default());
        {
            let mut pass = enc.begin_compute_pass(&Default::default());
            pass.set_pipeline(self.pipeline.as_ref().unwrap());
            pass.set_bind_group(0, self.bind_group.as_ref().unwrap(), &[]);
            pass.dispatch_workgroups(64, 1, 1);
        }
        vec![enc.finish()]
    }
}

Provided Methods§

Source

fn priority(&self) -> i32

Ascending priority. Lower runs first. Use gpu_phase constants as base values. Defaults to gpu_phase::PRE_PREPARE since that is the only active band today.

Source

fn type_name(&self) -> &'static str

A stable name for this plugin, used to key per-plugin timing in RuntimeStats. Defaults to the concrete type name; override to give a friendlier label.

Source

fn init_gpu(&mut self, _device: &Device)

Build pipelines, persistent buffers, and bind group layouts.

Called once before the plugin’s first pre_prepare. If a new GPU plugin is registered after the runtime has already run, every plugin’s init_gpu is invoked again on the next frame; implementations should either be idempotent or guard their own one-time setup.

Source

fn on_device_recreated(&mut self, _device: &Device, _queue: &Queue)

Called when the wgpu device is recreated, e.g. after device loss or a host-driven reset. Every cached buffer, texture, bind group, or pipeline the plugin built against the old device is now invalid and must be rebuilt against device / queue.

The host invokes this via ViewportRuntime::notify_device_recreated; the runtime does not detect device loss on its own. After this call returns, init_gpu is also re-invoked on every plugin before the next pre_prepare, so a typical implementation can simply drop its cached resources here and let init_gpu rebuild them.

Source

fn pre_prepare( &mut self, _device: &Device, _queue: &Queue, _ctx: &GpuFrameContext<'_>, ) -> Vec<CommandBuffer>

Encode work that runs before renderer.prepare().

CPU plugins have already stepped this frame; their outputs are visible through any shared state the consumer wired. The renderer has not started its own work yet, so anything written here is observable by prepare()’s passes (cluster build, shadow render, etc.).

Source

fn post_paint( &mut self, _device: &Device, _queue: &Queue, _targets: &PostPaintTargets<'_>, _ctx: &GpuFrameContext<'_>, ) -> Vec<CommandBuffer>

Encode work that runs after renderer.paint_to().

The plugin receives views of the just-rendered color, depth, and (optionally) pick-id targets and may sample them in a compute or fullscreen render pass. Typical uses: custom AO, motion blur, screen-space outlines, color grading LUTs.

To modify the color target, write to a sibling texture the plugin owns and let the host composite it during the final blit. The lib does not loop a plugin’s output back into the rendered color.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§