Speicher: kompaktes 12-Byte-Linienvertex (nativ), grobere Rundkappen bei duennen Strichen

This commit is contained in:
karim committed 2026-10-19 22:51:17 +02:00
1 parent 470547071f
commit 072dd4ad92
5 files changed
+107 -11

No files matched your search

+45 -4
View File
@@ -5,6 +5,8 @@
// `PlanGpuRenderer` (glPlanRender.ts), inkl. lokalem Chunk-Ursprung in doppelter
// Genauigkeit.
//
// Linien-Vertices kommen KOMPAKT (12 Byte, s. LINE_PACKED_WGSL).
//
// Die Webview kompiliert weiterhin (Cache/Chunk-Logik bleibt in TypeScript) und
// schickt nur noch die Puffer-BYTES je Chunk einmalig; pro Frame geht nur der
// Ausschnitt (viewBox) hierher. Rust haelt die Puffer — kein JSON, kein
@@ -15,7 +17,7 @@ use std::collections::HashMap;
use bytemuck::{Pod, Zeroable};
use wgpu::util::DeviceExt;
use crate::shaders::{FILL_WGSL, LINE_WGSL};
use crate::shaders::FILL_WGSL;
const SAMPLE_COUNT: u32 = 4;
/// Graustufe fuer gedimmte Linien (== GREY_RGB in glPlanCompile.ts).
@@ -23,6 +25,45 @@ const GREY: f32 = 0.549;
/// Viewbox-Einheiten je Meter (== PX_PER_M).
const PX_PER_M: f64 = 90.0;
/// Linien-Shader fuer das KOMPAKTE Vertex-Format der Webview (12 statt 24 Byte):
/// `position` (2 x f32) + `packed` (4 x snorm8 = bisector.xy, side, miter).
/// Rechnung identisch zu `shaders::LINE_WGSL`.
const LINE_PACKED_WGSL: &str = r#"
struct Globals {
view_proj : mat4x4<f32>,
color : vec4<f32>,
viewport_px : vec2<f32>,
stroke_px : f32,
stroke_scale : f32,
};
@group(0) @binding(0) var<uniform> globals : Globals;
struct VsIn {
@location(0) position : vec2<f32>,
@location(1) packed : vec4<f32>, // (bisector.x, bisector.y, side, miter)
};
@vertex
fn vs_main(in : VsIn) -> @builtin(position) vec4<f32> {
let clip_p = globals.view_proj * vec4<f32>(in.position, 0.0, 1.0);
let clip_n = globals.view_proj * vec4<f32>(in.position + in.packed.xy, 0.0, 1.0);
let dir_px = (clip_n.xy - clip_p.xy) * globals.viewport_px * 0.5;
let len = length(dir_px);
var unit_px = vec2<f32>(0.0, 0.0);
if (len > 1e-6) {
unit_px = dir_px / len;
}
let width_px = max(0.6, globals.stroke_px * globals.stroke_scale) * in.packed.w;
let offset_px = unit_px * (width_px * 0.5) * in.packed.z;
return vec4<f32>(clip_p.xy + offset_px / (globals.viewport_px * 0.5), clip_p.z, clip_p.w);
}
@fragment
fn fs_main() -> @location(0) vec4<f32> {
return globals.color;
}
"#;
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct Globals {
@@ -127,7 +168,7 @@ impl ChunkRenderer {
});
let line_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("chunks.line"),
source: wgpu::ShaderSource::Wgsl(LINE_WGSL.into()),
source: wgpu::ShaderSource::Wgsl(LINE_PACKED_WGSL.into()),
});
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("chunks.globals"),
@@ -195,9 +236,9 @@ impl ChunkRenderer {
let line_pipeline = make(
&line_module,
wgpu::VertexBufferLayout {
array_stride: 24,
array_stride: 12,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x2, 2 => Float32, 3 => Float32],
attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Snorm8x4],
},
"chunks.line.pipeline",
);
+27 -1
View File
@@ -53,7 +53,8 @@ describe('compilePrimitivesToGpu: zusammenhängende Linien-Segmente', () => {
it('verkettet NICHT bei unterschiedlichem Stil', () => {
const single = lineIndexCount([seg(0, 0, 1, 0, 'a')]);
const thick = { ...seg(1, 0, 2, 0, 'a'), weightMm: 0.5 } as Primitive;
expect(lineIndexCount([seg(0, 0, 1, 0, 'a'), thick])).toBe(2 * single);
// Dicke Striche bekommen mehr Kappen-Schritte als dünne → je einzeln zählen.
expect(lineIndexCount([seg(0, 0, 1, 0, 'a'), thick])).toBe(single + lineIndexCount([thick]));
});
});
@@ -182,3 +183,28 @@ describe('computeOrthoMatrix', () => {
expect([...a].every((v, i) => v === b[i])).toBe(false);
});
});
describe('compilePrimitivesToGpu: kompaktes Linien-Vertex-Format (nativ)', () => {
it('liefert 3 Wörter je Vertex statt 6 Floats', () => {
const uploaded: ArrayBufferView[] = [];
const mk = (packed: boolean) =>
({
ARRAY_BUFFER: 1,
ELEMENT_ARRAY_BUFFER: 2,
STATIC_DRAW: 3,
packedLineVertices: packed,
createBuffer: () => ({}),
bindBuffer: () => {},
bufferData: (_t: number, d: ArrayBufferView) => void uploaded.push(d),
deleteBuffer: () => {},
}) as any;
const p = [seg(0, 0, 1, 0, 'a')];
uploaded.length = 0;
compilePrimitivesToGpu(mk(false), p, 0);
const fullBytes = uploaded.find((u) => u.byteLength % 24 === 0 && u.byteLength > 0)!.byteLength;
uploaded.length = 0;
compilePrimitivesToGpu(mk(true), p, 0);
const packedBytes = uploaded.find((u) => u.byteLength % 12 === 0 && u.byteLength > 0)!.byteLength;
expect(packedBytes).toBe(fullBytes / 2);
});
});
+28 -6
View File
@@ -277,8 +277,19 @@ export function compilePrimitivesToGpu(
const fillIdx = new GrowBuffer((n) => new Uint32Array(n));
const fillBatches: GpuGeometry['fill']['batches'] = [];
// Linien-Puffer (interleaved [x,y, nx,ny, side]).
// Linien-Puffer: interleaved [x,y, nx,ny, side, miter] (6 Float32) — oder, wenn
// der Aufrufer es verlangt (nativer Renderer), kompakt 3 Wörter je Vertex:
// [x, y (float32-Bits), nx|ny|side|miter als 4 × snorm8].
const packedLines = gl.packedLineVertices === true;
const lineVerts = new GrowBuffer((n) => new Float32Array(n));
const lineVertsPacked = new GrowBuffer((n) => new Uint32Array(n));
const bitsF32 = new Float32Array(1);
const bitsU32 = new Uint32Array(bitsF32.buffer);
const f2u = (v: number): number => {
bitsF32[0] = v;
return bitsU32[0];
};
const snorm8 = (v: number): number => Math.round(Math.max(-1, Math.min(1, v)) * 127) & 0xff;
const lineIdx = new GrowBuffer((n) => new Uint32Array(n));
// LOKALER URSPRUNG dieser Geometrie: alle Vertices werden relativ zum ersten
@@ -384,10 +395,18 @@ export function compilePrimitivesToGpu(
const leftNormal = (d: Vec2): Vec2 => ({ x: -d.y, y: d.x });
const startIdxLen = lineIdx.length;
const vBase = lineVerts.length / 6;
const vBase = packedLines ? lineVertsPacked.length / 3 : lineVerts.length / 6;
let vcount = 0;
const pushVert = (p: Vec2, nx: number, ny: number, side: number): number => {
lineVerts.push6(p.x, p.y, nx, ny, side, 1);
if (packedLines) {
lineVertsPacked.push3(
f2u(p.x),
f2u(p.y),
(snorm8(nx) | (snorm8(ny) << 8) | (snorm8(side) << 16) | (127 << 24)) >>> 0,
);
} else {
lineVerts.push6(p.x, p.y, nx, ny, side, 1);
}
vcount++;
return vBase + vcount - 1;
};
@@ -415,7 +434,10 @@ export function compilePrimitivesToGpu(
// Randpunkten (voller Versatz in Richtung (cosθ,sinθ)).
const addFanArc = (p: Vec2, aFrom: number, aTo: number) => {
const delta = aTo - aFrom;
const steps = Math.max(1, Math.ceil(Math.abs(delta) / (Math.PI / 10)));
// Dünne Striche (≤ 0.35 mm, nur wenige Pixel breit): grobe Rundung genügt
// und spart ~3× Vertices je Kappe/Ecke (Speicher bei Kataster-Importen).
const stepAngle = strokeMm <= 0.35 ? Math.PI / 3 : Math.PI / 10;
const steps = Math.max(1, Math.ceil(Math.abs(delta) / stepAngle));
const iCenter = pushVert(p, 1, 0, 0);
let prev = pushVert(p, Math.cos(aFrom), Math.sin(aFrom), 1);
for (let s = 1; s <= steps; s++) {
@@ -749,7 +771,7 @@ export function compilePrimitivesToGpu(
}
// Puffer hochladen.
const uploadArray = (data: GrowBuffer<Float32Array>): WebGLBuffer | null => {
const uploadArray = (data: GrowBuffer<Float32Array> | GrowBuffer<Uint32Array>): WebGLBuffer | null => {
if (!data.length) return null;
const buf = gl.createBuffer();
if (!buf) return null;
@@ -773,7 +795,7 @@ export function compilePrimitivesToGpu(
batches: fillBatches,
},
line: {
vertexBuffer: uploadArray(lineVerts),
vertexBuffer: uploadArray(packedLines ? lineVertsPacked : lineVerts),
indexBuffer: uploadIndex(lineIdx),
batches: lineBatches,
},
+6
View File
@@ -117,6 +117,12 @@ export interface ViewBox {
* Renderer (nativeGl.ts) liefert eine Attrappe, die die Bytes an Rust schickt.
*/
export interface BufferGl {
/**
* Linien-Vertices kompakt (12 statt 24 Byte): [x,y als float32, dann 4 Bytes
* snorm8 = nx, ny, side, miter]. Nur der native Renderer (chunks.rs) liest das;
* der WebGL-Pfad bleibt beim 6-Float-Layout.
*/
readonly packedLineVertices?: boolean;
readonly ARRAY_BUFFER: number;
readonly ELEMENT_ARRAY_BUFFER: number;
readonly STATIC_DRAW: number;
+1
View File
@@ -32,6 +32,7 @@ export class NativeGl implements BufferGl {
readonly ARRAY_BUFFER = 0x8892;
readonly ELEMENT_ARRAY_BUFFER = 0x8893;
readonly STATIC_DRAW = 0x88e4;
readonly packedLineVertices = true;
private nextId = 1;
private bound: NativeBuffer | null = null;