From dc6ab429f28062694eed68afdd6203ea304364dd Mon Sep 17 00:00:00 2001 From: karim Date: Tue, 20 Oct 2026 00:43:11 +0200 Subject: [PATCH] Stocken behoben: auf macOS keine JSON-Szene/3D-Modell mehr nach jeder Aenderung aufbauen (niemand liest sie); Auswahl-Hervorhebung ueber eigenen kleinen Befehl, unveraendertes Layout nicht neu senden --- src-tauri/render2d/src/chunks.rs | 15 ++++++++--- src-tauri/src/lib.rs | 25 +++++++++++++++++- src-tauri/src/native_embed.rs | 9 +++++++ src/plan/glPlan/nativeGl.ts | 24 ++++++++++++------ src/plan/useNativePlanRenderer.ts | 42 +++++++++++++++++++------------ 5 files changed, 87 insertions(+), 28 deletions(-) diff --git a/src-tauri/render2d/src/chunks.rs b/src-tauri/render2d/src/chunks.rs index dfe9335..ead6bba 100644 --- a/src-tauri/render2d/src/chunks.rs +++ b/src-tauri/render2d/src/chunks.rs @@ -205,6 +205,8 @@ pub struct ChunkRenderer { uniform: Option<(wgpu::Buffer, wgpu::BindGroup, u64)>, buffers: HashMap, layout: Vec, + /// Auswahl-Hervorhebung: eine Geometrie zuoberst, unabhaengig vom Layout gesetzt. + highlight: Option, msaa: Option<(wgpu::Texture, (u32, u32))>, format: wgpu::TextureFormat, samples: u32, @@ -389,6 +391,7 @@ impl ChunkRenderer { uniform: None, buffers: HashMap::new(), layout: Vec::new(), + highlight: None, msaa: None, format, samples, @@ -522,6 +525,10 @@ impl ChunkRenderer { (tmp.into_iter().map(|(_, i)| i).collect(), groups) } + pub fn set_highlight(&mut self, geom: Option) { + self.highlight = geom; + } + pub fn set_layout(&mut self, layout: Vec) { self.layout = layout; } @@ -550,7 +557,7 @@ impl ChunkRenderer { let stride = (std::mem::size_of::() as u64).div_ceil(align) * align; let mut blocks: Vec = Vec::new(); let vp = [vw as f32, vh as f32]; - for g in &self.layout { + for g in self.layout.iter().chain(self.highlight.iter()) { if g.fill_pos.is_none() || g.fill_idx.is_none() { continue; } @@ -560,7 +567,7 @@ impl ChunkRenderer { blocks.push(Globals { view_proj: proj, color: c, viewport_px: vp, stroke_px: 0.0, stroke_scale: 1.0 }); } } - for g in &self.layout { + for g in self.layout.iter().chain(self.highlight.iter()) { if g.line_vtx.is_none() || g.line_idx.is_none() { continue; } @@ -698,7 +705,7 @@ impl ChunkRenderer { }); let mut block = 0u32; pass.set_pipeline(&self.fill_pipeline); - for g in &self.layout { + for g in self.layout.iter().chain(self.highlight.iter()) { let (Some(p), Some(i)) = (g.fill_pos, g.fill_idx) else { continue }; let (Some(pb), Some(ib)) = (self.buffers.get(&p), self.buffers.get(&i)) else { block += g.fill_batches.len() as u32; @@ -713,7 +720,7 @@ impl ChunkRenderer { } } pass.set_pipeline(&self.line_pipeline); - for g in &self.layout { + for g in self.layout.iter().chain(self.highlight.iter()) { let (Some(p), Some(i)) = (g.line_vtx, g.line_idx) else { continue }; let (Some(pb), Some(ib)) = (self.buffers.get(&p), self.buffers.get(&i)) else { block += g.line_batches.len() as u32; diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e9567b9..2b4bcde 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -157,6 +157,21 @@ fn native2d_patch(app: tauri::AppHandle, request: tauri::ipc::Request<'_>) -> Re } } +#[tauri::command] +fn native2d_highlight(app: tauri::AppHandle, geom: Option, delete: Vec) -> Result<(), String> { + #[cfg(all(feature = "native2d", target_os = "macos"))] + { + let geom = match geom { + Some(v) => Some(serde_json::from_value(v).map_err(|e| e.to_string())?), + None => None, + }; + native_embed::set_highlight(&app, geom, delete); + } + #[cfg(not(all(feature = "native2d", target_os = "macos")))] + let _ = (app, geom, delete); + Ok(()) +} + #[tauri::command] fn native2d_layout(app: tauri::AppHandle, layout: serde_json::Value, delete: Vec) -> Result<(), String> { #[cfg(all(feature = "native2d", target_os = "macos"))] @@ -197,9 +212,15 @@ fn native2d_frame(app: tauri::AppHandle, view: serde_json::Value, paper_scale: f Ok(()) } +/// +/// macOS: die eingebettete 2D-Fläche (`native_embed`) bekommt ihre Daten über +/// Chunk-Uploads, und es gibt dort kein natives 3D-Fenster — dort also `false`, +/// sonst würde die Webview nach JEDER Plan-/Projektänderung den ganzen Plan bzw. +/// das 3D-Modell als JSON aufbauen, nur damit Rust es verwirft (bei Kataster- +/// Importen sekundenlanges Stocken). #[tauri::command] fn native_viewport_enabled() -> bool { - cfg!(any(feature = "native2d", feature = "native3d")) + cfg!(all(any(feature = "native2d", feature = "native3d"), not(target_os = "macos"))) } #[cfg_attr(mobile, tauri::mobile_entry_point)] @@ -248,6 +269,7 @@ pub fn run() { native2d_embed_hide, native2d_upload, native2d_patch, + native2d_highlight, native2d_layout, native2d_frame, native2d_texts, @@ -264,6 +286,7 @@ pub fn run() { native2d_embed_hide, native2d_upload, native2d_patch, + native2d_highlight, native2d_layout, native2d_frame, native2d_texts, diff --git a/src-tauri/src/native_embed.rs b/src-tauri/src/native_embed.rs index 1cb53de..4af42a8 100644 --- a/src-tauri/src/native_embed.rs +++ b/src-tauri/src/native_embed.rs @@ -240,3 +240,12 @@ pub fn patch(app: &AppHandle, from: u32, to: u32, ranges: &[(u64, u64)], bytes: None => false, } } + +/// Auswahl-Hervorhebung setzen (eigene Geometrie zuoberst) und neu zeichnen. +pub fn set_highlight(app: &AppHandle, geom: Option, delete: Vec) { + if let Some(e) = app.state::().0.lock().unwrap().as_mut() { + e.renderer.delete(&delete); + e.renderer.set_highlight(geom); + draw(e); + } +} diff --git a/src/plan/glPlan/nativeGl.ts b/src/plan/glPlan/nativeGl.ts index 787cc59..e7fda2c 100644 --- a/src/plan/glPlan/nativeGl.ts +++ b/src/plan/glPlan/nativeGl.ts @@ -162,14 +162,25 @@ export class NativeGl implements BufferGl { for (const id of deleted) this.live.delete(id); } - /** Geometrien (in Zeichenreihenfolge) → Layout für Rust, plus freizugebende Puffer. */ - takeLayout(geometries: readonly GpuGeometry[]): { layout: NativeChunkLayout[]; delete: number[] } { - const id = (b: WebGLBuffer | null): number | null => (b ? (b as unknown as NativeBuffer).id : null); + /** Ausstehende Uploads senden; liefert die freizugebenden Puffer-IDs. */ + takeUploads(): number[] { const del = this.pendingDelete; this.pendingDelete = []; - // Erst die Uploads (in Reihenfolge VOR dem Layout-Aufruf des Aufrufers). this.flushUploads(del); - const layout = geometries.map((g) => ({ + return del; + } + + /** Geometrien (in Zeichenreihenfolge) → Layout für Rust, plus freizugebende Puffer. */ + takeLayout(geometries: readonly GpuGeometry[]): { layout: NativeChunkLayout[]; delete: number[] } { + // Erst die Uploads (in Reihenfolge VOR dem Layout-Aufruf des Aufrufers). + const del = this.takeUploads(); + return { layout: geometries.map((g) => this.layoutOf(g)), delete: del }; + } + + /** Layout-Eintrag einer einzelnen Geometrie. */ + layoutOf(g: GpuGeometry): NativeChunkLayout { + const id = (b: WebGLBuffer | null): number | null => (b ? (b as unknown as NativeBuffer).id : null); + return { fill_pos: id(g.fill.positionBuffer), fill_idx: id(g.fill.indexBuffer), fill_batches: g.fill.batches.map((b) => ({ start: b.startIndex, count: b.indexCount, color: b.color })), @@ -184,7 +195,6 @@ export class NativeGl implements BufferGl { })), origin: [g.origin?.x ?? 0, g.origin?.y ?? 0] as [number, number], greyed: !!g.greyed, - })); - return { layout, delete: del }; + }; } } diff --git a/src/plan/useNativePlanRenderer.ts b/src/plan/useNativePlanRenderer.ts index 73f4b7e..fa90771 100644 --- a/src/plan/useNativePlanRenderer.ts +++ b/src/plan/useNativePlanRenderer.ts @@ -193,6 +193,7 @@ export function useNativePlanRenderer( if (remove.length) void invoke('native2d_texts', { upsert: [], remove }).catch(() => {}); lastPlan.current = null; highlightGeom.current = null; + lastSentGeoms.current = null; setGpuTexts(false); const { delete: del } = glRef.current.takeLayout([]); void invoke('native2d_layout', { layout: [], delete: del }).catch(() => {}); @@ -205,18 +206,16 @@ export function useNativePlanRenderer( }; }, [enabled, canvasRef, syncRect]); - /** Layout = Plan-Chunks + (optional) Auswahl-Hervorhebung zuoberst. */ + /** Zuletzt gesendete Geometrie-Liste: unverändert → kein erneuter Layout-Versand. */ + const lastSentGeoms = useRef(null); const takeFullLayout = useCallback(() => { const gl = glRef.current!; const geoms = cacheRef.current?.geometries ?? []; - const hl = highlightGeom.current; - const res = gl.takeLayout(hl ? [...geoms, hl] : geoms); - if (hl && res.layout.length) { - const dpr = typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1; - const last = res.layout[res.layout.length - 1]; - last.line_batches = last.line_batches.map((b) => ({ ...b, stroke_mm: 2.5 * dpr, device_px: true })); - } - return res; + const prev = lastSentGeoms.current; + const same = !!prev && prev.length === geoms.length && prev.every((g, i) => g === geoms[i]); + lastSentGeoms.current = geoms.slice(); + const res = gl.takeLayout(geoms); + return { ...res, unchanged: same && res.delete.length === 0 }; }, []); /** @@ -248,10 +247,18 @@ export function useNativePlanRenderer( } highlightGeom.current = compilePrimitivesToGpu(gl, lines, lastPxPerMeter.current); } - const { layout, delete: del } = takeFullLayout(); - void invoke('native2d_layout', { layout, delete: del }).catch(() => {}); + const del = gl.takeUploads(); + const hl = highlightGeom.current; + let geom = null; + if (hl) { + const dpr = typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1; + const e = gl.layoutOf(hl); + e.line_batches = e.line_batches.map((b) => ({ ...b, stroke_mm: 2.5 * dpr, device_px: true })); + geom = e; + } + void invoke('native2d_highlight', { geom, delete: del }).catch(() => {}); }, - [takeFullLayout], + [], ); const updateGeometry = useCallback((plan: Plan, pxPerMeter = 0) => { @@ -272,12 +279,15 @@ export function useNativePlanRenderer( cache.dispose(); } lastPxPerMeter.current = pxPerMeter; - const { layout, delete: del } = takeFullLayout(); + const { layout, delete: del, unchanged } = takeFullLayout(); lastPlan.current = plan; // Direkt (nicht über sendFrame): Reihenfolge nach den Puffer-Uploads ist wichtig. - void invoke('native2d_layout', { layout, delete: del }).catch((e) => - console.debug('native2d_layout fehlgeschlagen', e), - ); + // Unverändertes Layout (kein Chunk neu, nichts freigegeben) nicht erneut senden. + if (!unchanged) { + void invoke('native2d_layout', { layout, delete: del }).catch((e) => + console.debug('native2d_layout fehlgeschlagen', e), + ); + } pushTexts(plan); }, [pushTexts, takeFullLayout]);