pub trait GpuPlugin: Send + 'static {
// Provided methods
fn priority(&self) -> i32 { ... }
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.
§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§
Sourcefn priority(&self) -> i32
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.
Sourcefn init_gpu(&mut self, _device: &Device)
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.
Sourcefn on_device_recreated(&mut self, _device: &Device, _queue: &Queue)
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.
Sourcefn pre_prepare(
&mut self,
_device: &Device,
_queue: &Queue,
_ctx: &GpuFrameContext<'_>,
) -> Vec<CommandBuffer>
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.).
Sourcefn post_paint(
&mut self,
_device: &Device,
_queue: &Queue,
_targets: &PostPaintTargets<'_>,
_ctx: &GpuFrameContext<'_>,
) -> Vec<CommandBuffer>
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".