feat(draworder): cross-type draw order + context menu

DRAWORDER now affects rendering immediately (bump geometry epoch so the
sort cache rebuilds) and front/back anchor to the block's full effective
sort range instead of only existing table entries, so bring-to-front
actually lands on top. The render fallback sorts table-absent entities by
their own handle.

Draw order now applies across entity types, not just within a type. A
per-entity draw-order rank (SortEntitiesTable override or own handle,
ranked per block) is encoded as a small signed clip-z bias in the wire,
hatch, image, and 3DFACE pipelines. The shared LessEqual depth buffer then
orders fills, lines, rasters and 2D solids against each other regardless of
pass order — without touching model coordinates. 3D meshes keep real
geometric depth; wipeouts stay always-on-top.

Adds a Draw Order submenu to the viewport context menu (Bring to Front,
Send to Back, Bring Above/Send Under Object). The above/under items run an
interactive reference-object pick, then reorder the captured selection.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Hakan Seven 2026-05-30 21:29:01 +03:00
commit 09e7e90aae
19 changed files with 469 additions and 36 deletions

View file

@ -29,6 +29,10 @@ use iced::wgpu::util::DeviceExt;
pub struct Face3DVertex {
pub position: [f32; 3],
pub color: [f32; 4],
/// Normalized draw-order depth in (0,1) for 2D fills / 3DFACE quads;
/// applied as a small clip-z bias in the shader. 0.0 for true 3D mesh
/// faces (PolyfaceMesh / PolygonMesh) so their real depth is preserved.
pub draw_depth: f32,
}
impl Face3DVertex {
@ -47,6 +51,11 @@ impl Face3DVertex {
shader_location: 1,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: 28,
shader_location: 2,
format: wgpu::VertexFormat::Float32,
},
],
}
}
@ -84,7 +93,15 @@ impl Face3DGpu {
face3d_wires: &[WireModel],
all_wires: &[WireModel],
keep_3d_mesh_fills: bool,
depth_map: &std::collections::HashMap<u64, f32>,
) -> Self {
let depth_of = |w: &WireModel| -> f32 {
w.name
.parse::<u64>()
.ok()
.and_then(|h| depth_map.get(&h).copied())
.unwrap_or(0.0)
};
let mut verts_3d: Vec<Face3DVertex> = Vec::with_capacity(face3d_wires.len() * 6);
let mut verts_2d: Vec<Face3DVertex> = Vec::new();
@ -97,10 +114,12 @@ impl Face3DGpu {
}
let [r, g, b, a] = wire.color;
let fill_color = [r * 0.45, g * 0.45, b * 0.45, a];
let depth = depth_of(wire);
let p = &wire.key_vertices;
let v = |i: usize| Face3DVertex {
position: p[i],
color: fill_color,
draw_depth: depth,
};
verts_3d.push(v(0));
verts_3d.push(v(1));
@ -132,18 +151,23 @@ impl Face3DGpu {
continue;
}
let fill_color = [r * 0.45, g * 0.45, b * 0.45, a];
// True 3D surface: keep real depth (no draw-order bias) so
// hidden-surface shading is preserved.
for &position in &wire.fill_tris {
verts_3d.push(Face3DVertex {
position,
color: fill_color,
draw_depth: 0.0,
});
}
} else {
let fill_color = [r, g, b, a];
let depth = depth_of(wire);
for &position in &wire.fill_tris {
verts_2d.push(Face3DVertex {
position,
color: fill_color,
draw_depth: depth,
});
}
}

View file

@ -64,7 +64,10 @@ pub struct HatchInstance {
pub boundary_count: u32, // 92
pub family_offset: u32, // 96
pub family_count: u32, // 100
pub _pad0: [u32; 2], // 104 (pad to 112 = 16-byte stride)
/// Signed draw-order depth (-1,1); 0.0 = neutral. Applied as a clip-z
/// bias in the vertex shader so this fill orders against other types.
pub draw_depth: f32, // 104
pub _pad0: u32, // 108 (pad to 112 = 16-byte stride)
}
const _: () = assert!(std::mem::size_of::<HatchInstance>() == 112);
@ -274,7 +277,8 @@ impl HatchBatchedGpu {
boundary_count,
family_offset,
family_count,
_pad0: [0; 2],
draw_depth: h.draw_depth,
_pad0: 0,
});
continue;
}
@ -312,7 +316,8 @@ impl HatchBatchedGpu {
boundary_count,
family_offset,
family_count,
_pad0: [0; 2],
draw_depth: h.draw_depth,
_pad0: 0,
});
}

View file

@ -45,7 +45,10 @@ impl ImageVertex {
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct ImageParams {
opacity: f32,
_pad: [f32; 3],
/// Signed draw-order depth (-1,1); applied as a clip-z bias in the shader
/// so the raster orders against other entity types. 0.0 = neutral.
draw_depth: f32,
_pad: [f32; 2],
} // 16 bytes
// ── Per-image GPU handle ──────────────────────────────────────────────────
@ -118,7 +121,8 @@ impl ImageGpu {
// ── Opacity uniform ───────────────────────────────────────────────
let params = ImageParams {
opacity: model.opacity.clamp(0.0, 1.0),
_pad: [0.0; 3],
draw_depth: model.draw_depth,
_pad: [0.0; 2],
};
let _params_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("image.params"),

View file

@ -646,10 +646,11 @@ impl Pipeline {
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
// binding 2: ImageParams uniform
// binding 2: ImageParams uniform (read in vertex for the
// draw-order z bias and in fragment for opacity).
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
@ -886,15 +887,30 @@ impl Pipeline {
}
}
pub fn upload_wires(&mut self, device: &wgpu::Device, wires: &[WireModel]) {
self.gpu_wires = wires.iter().map(|w| WireGpu::new(device, w)).collect();
pub fn upload_wires(
&mut self,
device: &wgpu::Device,
wires: &[WireModel],
depth_map: &std::collections::HashMap<u64, f32>,
) {
let depth_of = |w: &WireModel| -> f32 {
w.name
.parse::<u64>()
.ok()
.and_then(|h| depth_map.get(&h).copied())
.unwrap_or(0.0)
};
self.gpu_wires = wires
.iter()
.map(|w| WireGpu::new(device, w, depth_of(w)))
.collect();
// Full-brightness copies of selected wires, drawn on top of everything
// in the selection overlay pass so they're always visible.
self.gpu_selected_wires = wires
.iter()
.filter(|w| w.selected)
.map(|w| WireGpu::new(device, w))
.map(|w| WireGpu::new(device, w, depth_of(w)))
.collect();
}
@ -971,10 +987,11 @@ impl Pipeline {
face3d_wires: &[WireModel],
all_wires: &[WireModel],
wireframe_only: bool,
depth_map: &std::collections::HashMap<u64, f32>,
) {
// Edge buffer is always built from `face3d_wires`, so 3DFACE
// outlines stay on the screen regardless of mode.
self.gpu_face3d_edges = WireGpu::from_batch(device, face3d_wires);
self.gpu_face3d_edges = WireGpu::from_batch(device, face3d_wires, depth_map);
// Fill buffer split: 3D quads + PolyfaceMesh / PolygonMesh face
// tris go to `vertex_buffer_3d` (gated by `keep_3d_mesh_fills`);
// 2D fills (text-LOD greek, MultiLeader background) go to
@ -996,6 +1013,7 @@ impl Pipeline {
face3d_wires,
all_wires,
keep_3d_mesh_fills,
depth_map,
));
}
}

View file

@ -71,6 +71,9 @@ pub struct WireInstance {
pub pattern_length: f32,
pub pat0: [f32; 4],
pub pat1: [f32; 4],
/// Normalized draw-order depth in (0,1); applied as a small clip-z bias
/// in the shader so this wire orders against other entity types.
pub draw_depth: f32,
}
impl WireInstance {
@ -124,6 +127,11 @@ impl WireInstance {
shader_location: 8,
format: wgpu::VertexFormat::Float32x4,
}, // pat1
wgpu::VertexAttribute {
offset: 76,
shader_location: 9,
format: wgpu::VertexFormat::Float32,
}, // draw_depth
],
}
}
@ -158,7 +166,7 @@ fn pack_color(color: [f32; 4]) -> [u8; 4] {
]
}
fn emit_wire_instances(wire: &WireModel, color: [f32; 4]) -> Vec<WireInstance> {
fn emit_wire_instances(wire: &WireModel, color: [f32; 4], draw_depth: f32) -> Vec<WireInstance> {
let color_u8 = pack_color(color);
let pat0 = [
wire.pattern[0],
@ -222,14 +230,27 @@ fn emit_wire_instances(wire: &WireModel, color: [f32; 4]) -> Vec<WireInstance> {
pattern_length: wire.pattern_length,
pat0,
pat1,
draw_depth,
});
}
instances
}
/// Looks up a wire's draw-order depth from the per-entity map using the
/// handle encoded in its `name`. Falls back to 0.0 (transient / preview
/// wires that carry no document handle).
fn wire_draw_depth(wire: &WireModel, depth_map: &std::collections::HashMap<u64, f32>) -> f32 {
wire
.name
.parse::<u64>()
.ok()
.and_then(|h| depth_map.get(&h).copied())
.unwrap_or(0.0)
}
impl WireGpu {
pub fn new(device: &wgpu::Device, wire: &WireModel) -> Self {
let mut g = Self::build(device, wire, wire.color);
pub fn new(device: &wgpu::Device, wire: &WireModel, draw_depth: f32) -> Self {
let mut g = Self::build(device, wire, wire.color, draw_depth);
g.vp_scissor = wire.vp_scissor;
g.is_3d_mesh_edge = !wire.fill_tris.is_empty();
g
@ -238,7 +259,11 @@ impl WireGpu {
/// Merge multiple WireModels into GPU instance buffers, chunked to fit the
/// 256 MB GPU limit. Each wire keeps its own color and pattern — they live
/// per-instance.
pub fn from_batch(device: &wgpu::Device, wires: &[WireModel]) -> Vec<Self> {
pub fn from_batch(
device: &wgpu::Device,
wires: &[WireModel],
depth_map: &std::collections::HashMap<u64, f32>,
) -> Vec<Self> {
let total_segs: usize = wires.iter().map(|w| w.points.len().saturating_sub(1)).sum();
if total_segs == 0 {
return vec![];
@ -251,7 +276,7 @@ impl WireGpu {
// across cores.
let instances: Vec<WireInstance> = wires
.par_iter()
.map(|wire| emit_wire_instances(wire, wire.color))
.map(|wire| emit_wire_instances(wire, wire.color, wire_draw_depth(wire, depth_map)))
.reduce(Vec::new, |mut acc, mut chunk| {
if acc.is_empty() {
chunk
@ -286,8 +311,8 @@ impl WireGpu {
.collect()
}
fn build(device: &wgpu::Device, wire: &WireModel, color: [f32; 4]) -> Self {
let instances = emit_wire_instances(wire, color);
fn build(device: &wgpu::Device, wire: &WireModel, color: [f32; 4], draw_depth: f32) -> Self {
let instances = emit_wire_instances(wire, color, draw_depth);
let label = format!("wire.ibuf.{}", wire.name);
let instance_buffer = instance_buffer_mapped(device, &label, &instances);