Skip to main content

ViewportRenderer

Struct ViewportRenderer 

Source
pub struct ViewportRenderer { /* private fields */ }
Expand description

Renderer wrapping all GPU resources and providing prepare() and paint() methods.

Implementations§

Source§

impl ViewportRenderer

Source

pub fn pick_scene_gpu( &mut self, device: &Device, queue: &Queue, cursor: Vec2, frame: &FrameData, ) -> Option<GpuPickHit>

GPU object-ID pick: renders the scene to an offscreen R32Uint texture and reads back the single pixel under cursor.

This is O(1) in mesh complexity : every object is rendered with a flat u32 ID, and only one pixel is read back. For triangle-level queries (barycentric scalar probe, exact world position), use the CPU crate::interaction::picking::pick_scene_cpu path instead.

The pipeline is lazily initialized on first call : zero overhead when this method is never invoked.

§Arguments
  • device : wgpu device
  • queue : wgpu queue
  • cursor : cursor position in viewport-local pixels (top-left origin)
  • frame : current grouped frame data (camera, scene surfaces, viewport size)
§Returns

Some(GpuPickHit) if an object is under the cursor, None if empty space.

Source§

impl ViewportRenderer

Source

pub fn prepare( &mut self, device: &Device, queue: &Queue, frame: &FrameData, ) -> FrameStats

Upload per-frame data to GPU buffers and render the shadow pass. Call before paint().

Returns crate::FrameStats with per-frame timing and upload metrics.

Source§

impl ViewportRenderer

Source

pub fn paint(&self, render_pass: &mut RenderPass<'static>, frame: &FrameData)

Issue draw calls for the viewport. Call inside a wgpu::RenderPass.

This method requires a 'static render pass (as provided by egui’s CallbackTrait). For non-static render passes (iced, manual wgpu), use paint_to.

Source

pub fn paint_to<'rp>( &'rp self, render_pass: &mut RenderPass<'rp>, frame: &FrameData, )

Issue draw calls into a render pass with any lifetime.

Identical to paint but accepts a render pass with a non-'static lifetime, making it usable from iced, raw wgpu, or any framework that creates its own render pass.

Source

pub fn prepare_ldr_dyn_res( &mut self, encoder: &mut CommandEncoder, device: &Device, frame: &FrameData, ) -> bool

Render the scene into an intermediate dyn-res texture for the LDR callback render path (e.g. eframe’s CallbackTrait).

Call from CallbackTrait::prepare after prepare, passing the egui_encoder. If current_render_scale < 1.0, the full scene is drawn into a scaled intermediate texture and true is returned. Call paint_dyn_res_blit from CallbackTrait::paint instead of paint.

If scale is 1.0 or above, nothing is encoded and false is returned. Call paint as normal.

The egui_encoder is submitted before the surface render pass begins, so the intermediate texture is fully written before the blit reads it.

Source

pub fn paint_dyn_res_blit( &self, render_pass: &mut RenderPass<'static>, frame: &FrameData, )

Blit the dyn-res intermediate texture into the provided render pass.

Call from CallbackTrait::paint when prepare_ldr_dyn_res returned true for the same frame. Emits a fullscreen upscale quad into render_pass.

Source

pub fn render_viewport( &mut self, device: &Device, queue: &Queue, output_view: &TextureView, id: ViewportId, frame: &FrameData, ) -> CommandBuffer

High-level HDR render for a single viewport identified by id.

Unlike render, this method does not call prepare internally. The caller must have already called prepare_scene and prepare_viewport for id before invoking this.

This is the right entry point for multi-viewport frames:

  1. Call prepare_scene once.
  2. Call prepare_viewport for each viewport.
  3. Call render_viewport for each viewport with its own output_view.

Returns a wgpu::CommandBuffer ready to submit.

Source

pub fn render( &mut self, device: &Device, queue: &Queue, output_view: &TextureView, frame: &FrameData, ) -> CommandBuffer

High-level HDR render method. Handles the full post-processing pipeline: scene -> HDR texture -> (bloom) -> (SSAO) -> tone map -> output_view.

When frame.post_process.enabled is false, falls back to a simple LDR render pass targeting output_view directly.

Returns a CommandBuffer ready to submit.

Source

pub fn render_offscreen( &mut self, device: &Device, queue: &Queue, frame: &FrameData, width: u32, height: u32, ) -> Vec<u8>

Render a frame to an offscreen texture and return raw RGBA bytes.

Creates a temporary wgpu::Texture render target of the given dimensions, runs all render passes (shadow, scene, post-processing) into it via render(), then copies the result back to CPU memory.

No OS window or wgpu::Surface is required. The caller is responsible for initialising the wgpu adapter with compatible_surface: None and for constructing a valid FrameData (including viewport_size matching width/height).

Returns width * height * 4 bytes in RGBA8 layout. The caller encodes to PNG/EXR independently : no image codec dependency in this crate.

Source§

impl ViewportRenderer

Source

pub fn new(device: &Device, target_format: TextureFormat) -> Self

Create a new renderer with default settings (no MSAA). Call once at application startup.

Source

pub fn with_sample_count( device: &Device, target_format: TextureFormat, sample_count: u32, ) -> Self

Create a new renderer with the specified MSAA sample count (1, 2, or 4).

When using MSAA (sample_count > 1), the caller must create multisampled color and depth textures and use them as render pass attachments with the final surface texture as the resolve target.

Source

pub fn resources(&self) -> &ViewportGpuResources

Access the underlying GPU resources (e.g. for mesh uploads).

Source

pub fn last_frame_stats(&self) -> FrameStats

Performance counters from the last completed frame.

Source

pub fn disable_gpu_driven_culling(&mut self)

Disable GPU-driven culling, reverting to the direct draw path.

Has no effect when the device does not support INDIRECT_FIRST_INSTANCE (culling is already disabled on those devices).

Source

pub fn enable_gpu_driven_culling(&mut self)

Re-enable GPU-driven culling after a call to disable_gpu_driven_culling.

Has no effect when the device does not support INDIRECT_FIRST_INSTANCE.

Source

pub fn set_runtime_mode(&mut self, mode: RuntimeMode)

Set the runtime mode controlling internal default behavior.

  • [RuntimeMode::Interactive]: full picking rate, full quality (default).
  • [RuntimeMode::Playback]: picking throttled to reduce CPU overhead during animation.
  • [RuntimeMode::Paused]: full picking rate, full quality.
  • [RuntimeMode::Capture]: full quality, intended for screenshot/export workflows.
Source

pub fn runtime_mode(&self) -> RuntimeMode

Return the current runtime mode.

Source

pub fn set_performance_policy(&mut self, policy: PerformancePolicy)

Set the performance policy controlling target FPS, render scale bounds, and permitted quality reductions.

The internal adaptation controller activates when policy.allow_dynamic_resolution is true and policy.target_fps is Some. It adjusts render_scale within [min_render_scale, max_render_scale] each frame based on total_frame_ms.

Source

pub fn performance_policy(&self) -> PerformancePolicy

Return the active performance policy.

Source

pub fn set_render_scale(&mut self, scale: f32)

Manually set the render scale.

Effective when performance_policy.allow_dynamic_resolution is false. When dynamic resolution is enabled the adaptation controller overrides this value each frame.

The value is clamped to [policy.min_render_scale, policy.max_render_scale].

Has no effect on the HDR render path (render / render_viewport with PostProcessSettings::enabled = true). See allow_dynamic_resolution.

Source

pub fn set_target_fps(&mut self, fps: Option<f32>)

Set the target frame rate used to compute [FrameStats::missed_budget].

Convenience wrapper that updates performance_policy.target_fps.

Source

pub fn resources_mut(&mut self) -> &mut ViewportGpuResources

Mutable access to the underlying GPU resources (e.g. for mesh uploads).

Source

pub fn upload_environment_map( &mut self, device: &Device, queue: &Queue, pixels: &[f32], width: u32, height: u32, )

Upload an equirectangular HDR environment map and precompute IBL textures.

pixels is row-major RGBA f32 data (4 floats per texel), width×height. This rebuilds camera bind groups so shaders immediately see the new textures.

Source

pub fn create_viewport(&mut self, device: &Device) -> ViewportId

Create a new viewport slot and return its handle.

The returned ViewportId is stable for the lifetime of the renderer. Pass it to prepare_viewport, paint_viewport, and render_viewport each frame.

Also set CameraFrame::viewport_index to id.0 when building the FrameData for this viewport:

let id = renderer.create_viewport(&device);
let frame = FrameData {
    camera: CameraFrame::from_camera(&cam, size).with_viewport_index(id.0),
    ..Default::default()
};
Source

pub fn destroy_viewport(&mut self, id: ViewportId)

Release the heavy GPU texture memory (HDR targets, OIT, bloom, SSAO) held by id.

The slot index is not reclaimed : future calls with this ViewportId will lazily recreate the texture resources as needed. This is useful when a viewport is hidden or minimised and you want to reduce VRAM pressure without invalidating the handle.

Source

pub fn prepare_scene( &mut self, device: &Device, queue: &Queue, frame: &FrameData, scene_effects: &SceneEffects<'_>, )

Prepare shared scene data. Call once per frame, before any prepare_viewport calls.

frame provides the scene content (frame.scene) and the primary camera used for shadow cascade framing (frame.camera). In a multi-viewport setup use any one viewport’s FrameData here : typically the perspective view : as the shadow framing reference.

scene_effects carries the scene-global effects: lighting, environment map, and compute filters. Obtain it by constructing SceneEffects directly or via EffectsFrame::split.

Source

pub fn prepare_viewport( &mut self, device: &Device, queue: &Queue, id: ViewportId, frame: &FrameData, )

Prepare per-viewport GPU state (camera, clip planes, overlays, axes).

Call once per viewport per frame, after prepare_scene.

id must have been obtained from create_viewport. frame.camera.viewport_index must equal id.0; use CameraFrame::with_viewport_index when building the frame.

Source

pub fn paint_viewport( &self, render_pass: &mut RenderPass<'static>, id: ViewportId, frame: &FrameData, )

Issue draw calls for id into a 'static render pass (as provided by egui callbacks).

This is the method to use from an egui/eframe CallbackTrait::paint implementation. Call prepare_scene and prepare_viewport first (in CallbackTrait::prepare), then set the render pass viewport/scissor to confine drawing to the correct quadrant, and call this method.

For non-'static render passes (winit, iced, manual wgpu), use paint_viewport_to.

Source

pub fn paint_viewport_to<'rp>( &'rp self, render_pass: &mut RenderPass<'rp>, id: ViewportId, frame: &FrameData, )

Issue draw calls for id into a render pass with any lifetime.

Identical to paint_viewport but accepts a render pass with a non-'static lifetime, making it usable from winit, iced, or raw wgpu where the encoder creates its own render pass.

Auto Trait Implementations§

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>

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

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

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

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

Converts &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)

Converts &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> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,