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ยง
- Egui
Integration - Draws tessellated egui output with the built-in lit pipeline.
- Geometry
Counts ๐ - How much geometry a frame holds: the two numbers the packing and the draw ranges both need, and which must agree between them.
- Material
Key ๐ - What one material bind group was built from.
- Screen
Descriptor - The target a UI frame is drawn into and the density it is drawn at.
- Source
- Which context a texture belongs to.
- Texture
Key ๐ - One texture of one source.
- Texture
Slot ๐ - 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:
Float32x2thenUnorm8x4. - 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
primitivescontributes. - mesh_of ๐
- The mesh of
primitive, orNonefor a paint callback. - pack_
geometry ๐ - Pack
primitivesintoverticesandindices, and filldrawswith 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.