Skip to main content

Module ui

Module ui 

Source
Available on crate feature egui only.
Expand description

An egui backend built on the built-in lit pipeline.

EguiIntegration takes what egui produces each frame โ€” tessellated, clipped meshes plus a set of texture updates โ€” and draws it with the built-in pipeline as ordinary screen-space geometry. Nothing in the shader knows about egui: the UI is premultiplied, textured, screen-space vertices, and eguiโ€™s clip rectangles become scissor rectangles.

// The caller owns the globals: a camera uniform (written every frame with
// `screen_view`), a frame-globals uniform, the view `screen_viewport`
// derives from the same points, and the bind group binding all three.
let variant = ui_variant(/* the target encodes sRGB: */ true, surface);
let pipeline = SpecializedPipeline::create(device, variant);
let camera = uniform_buffer(device, "ui::camera");
let globals = uniform_buffer(device, "ui::globals");
let view = uniform_buffer(device, "ui::view");
let global_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
    label: Some("ui::globals"),
    layout: &pipeline.descriptor().bind_group_layouts(device).global,
    entries: &[
        wgpu::BindGroupEntry {
            binding: CAMERA_BINDING,
            resource: camera.as_entire_binding(),
        },
        wgpu::BindGroupEntry {
            binding: FRAME_BINDING,
            resource: globals.as_entire_binding(),
        },
        wgpu::BindGroupEntry {
            binding: VIEW_BINDING,
            resource: view.as_entire_binding(),
        },
    ],
});

// The camera and the view are written every frame; the UI reads no clock,
// so its frame globals stay at their default.
queue.write_buffer(&camera, 0, screen_view([256.0, 192.0]).as_bytes());
queue.write_buffer(&view, 0, screen_viewport([256.0, 192.0]).as_bytes());
queue.write_buffer(&globals, 0, Globals::default().as_bytes());

// The UI's per-texture resources join the caller's ledger.
let mut graph = ResourceGraph::new();
let camera_id = graph.insert(camera, None);
let globals_id = graph.insert(globals, None);
let view_id = graph.insert(view, None);
let group_id = graph.insert(global_group, None);
graph.add_dependency(&group_id, &camera_id);
graph.add_dependency(&group_id, &globals_id);
graph.add_dependency(&group_id, &view_id);

let mut ui = EguiIntegration::new(device, pipeline);
let mut input = egui::RawInput::default();
input.screen_rect = Some(egui::Rect::from_min_size(
    egui::Pos2::ZERO,
    egui::Vec2::new(256.0, 192.0),
));
let output = ctx.run_ui(input, |ui| {
    ui.label("hello world");
});
// The frame's encoder carries both the UI's staged vertex and index
// uploads and the pass that reads them; `scene` is recorded into it
// after this call.
let mut encoder = device.create_command_encoder(&Default::default());
// The target's physical size and its density, which the clip rectangles
// are scaled by.
let screen = ScreenDescriptor {
    size_in_pixels: [256, 192],
    pixels_per_point: 1.0,
};
ui.update(&mut graph, queue, &mut encoder, ctx, output, screen);
let scene = ui.scene(&graph, &graph.get(&group_id).expect("the group exists"));

Re-exportsยง

use crate::globals::Camera; ๐Ÿ”’
use crate::globals::View; ๐Ÿ”’
use crate::mesh::ChannelEncoding; ๐Ÿ”’
use crate::mesh::MeshInstance; ๐Ÿ”’
use crate::mesh::PositionStreamChannels; ๐Ÿ”’
use crate::mesh::SecondaryFlags; ๐Ÿ”’
use crate::pipeline::BASE_COLOR_SAMPLER_BINDING; ๐Ÿ”’
use crate::pipeline::BASE_COLOR_TEXTURE_BINDING; ๐Ÿ”’
use crate::pipeline::GLOBAL_GROUP; ๐Ÿ”’
use crate::pipeline::INSTANCE_SLOT; ๐Ÿ”’
use crate::pipeline::LitOptions; ๐Ÿ”’
use crate::pipeline::LitVariant; ๐Ÿ”’
use crate::pipeline::LitVertexChannels; ๐Ÿ”’
use crate::pipeline::MATERIAL_GROUP; ๐Ÿ”’
use crate::pipeline::PRIMARY_SLOT; ๐Ÿ”’
use crate::pipeline::SECONDARY_SLOT; ๐Ÿ”’
use crate::pipeline::SpecializedLitPipeline; ๐Ÿ”’
use crate::resources::Rebuild; ๐Ÿ”’
use crate::resources::ResHandle; ๐Ÿ”’
use crate::resources::Resource; ๐Ÿ”’
use crate::resources::ResourceGraph; ๐Ÿ”’
use crate::resources::TextureExt; ๐Ÿ”’
use crate::resources::TextureView; ๐Ÿ”’
use crate::scene::DrawEntry; ๐Ÿ”’
use crate::scene::DrawOrder; ๐Ÿ”’
use crate::scene::DrawRange; ๐Ÿ”’
use crate::scene::Scene; ๐Ÿ”’
use crate::scene::ScissorRect; ๐Ÿ”’
use crate::specialize::SurfaceKey; ๐Ÿ”’
use crate::staging::StagingBuffer; ๐Ÿ”’
use crate::util::Hashed; ๐Ÿ”’
use core::ops::Range; ๐Ÿ”’
use hashbrown::HashMap; ๐Ÿ”’
use zerocopy::IntoBytes; ๐Ÿ”’
use zerocopy::IntoBytes; ๐Ÿ”’

Structsยง

EguiIntegration
Draws tessellated egui output with the built-in lit pipeline.
GeometryCounts ๐Ÿ”’
How much geometry a frame holds: the two numbers the packing and the draw ranges both need, and which must agree between them.
MaterialKey ๐Ÿ”’
What one material bind group was built from.
ScreenDescriptor
The target a UI frame is drawn into and the density it is drawn at.
Source
Which context a texture belongs to.
TextureKey ๐Ÿ”’
One texture of one source.
TextureSlot ๐Ÿ”’
The graph nodes backing one egui texture slot.
UiDraw ๐Ÿ”’
One uploaded primitive: where its indices sit, what it samples and how it is clipped.
UiFrame
One sourceโ€™s contribution to a frame: what to upload and what it owns.

Constantsยง

POSITION_STRIDE ๐Ÿ”’
Bytes one planar-position vertex occupies.
SECONDARY_STRIDE ๐Ÿ”’
Bytes one secondary vertex occupies: Float32x2 then Unorm8x4.
TEXTURE_FORMAT ๐Ÿ”’
eguiโ€™s own texture format: gamma-space RGBA, never sRGB-aware.

Functionsยง

address_mode ๐Ÿ”’
apply_ui_settings ๐Ÿ”’
Apply the UIโ€™s device-independent policy โ€” the output conversion, culling, blending and the overlaid depth behavior โ€” to a standard options set.
default_instance ๐Ÿ”’
A one-element buffer holding the identity MeshInstance.
measure ๐Ÿ”’
Count the vertices and indices every tessellated primitive in primitives contributes.
mesh_of ๐Ÿ”’
The mesh of primitive, or None for a paint callback.
pack_geometry ๐Ÿ”’
Pack primitives into vertices and indices, and fill draws with where each one landed.
scissor_rect ๐Ÿ”’
eguiโ€™s clip rectangle as a scissor rectangle, in whole pixels.
screen_view
A world-to-clip matrix taking eguiโ€™s tessellated points to clip space.
screen_viewport
The view to bind beside screen_view.
texture_bytes ๐Ÿ”’
Bytes per texel of TEXTURE_FORMAT, derived from the format.
texture_filter ๐Ÿ”’
ui_channels
The per-vertex channels a UI mesh is packed with.
ui_variant
The pipeline variant a UI is drawn with.