lunar-render 1.0.0

wgpu 2D renderer with sprite batching, text rendering, camera, layers, and custom render passes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! text rendering via a glyph atlas texture.
//!
//! provides a glyph atlas for rasterizing and caching font glyphs using
//! cosmic-text, plus layout functions for positioning text on screen.

use cosmic_text::{Attrs, Buffer, Family, FontSystem, Metrics, Shaping, SwashCache, SwashContent};
use lunar_math::Vec2;
use rustc_hash::FxHashMap as HashMap;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

/// position + size of a glyph in the atlas, plus placement offsets.
#[derive(Clone)]
struct AtlasEntry {
	x: u32,
	y: u32,
	width: u32,
	height: u32,
	left: i32,
	top: i32,
}

/// a single positioned glyph quad for rendering.
#[derive(Debug, Clone, Copy)]
pub struct TextGlyphQuad {
	/// top-left position in game-unit screen space.
	pub position: Vec2,
	/// size in game units.
	pub size: Vec2,
	/// minimum UV coordinate in the atlas (top-left of glyph).
	pub uv_min: Vec2,
	/// maximum UV coordinate in the atlas (bottom-right of glyph).
	pub uv_max: Vec2,
}

/// LRU cache for laid-out text quads, keyed by (font_id, content_hash, font_size_bits, wrap_bits).
///
/// quads are stored with positions relative to origin (0,0). callers add the world position
/// when reading. the cache is capped at `cap` entries; least-recently-used entry is evicted
/// when full. must be cleared when the glyph atlas resets (scale change) since UV coords change.
pub struct TextLayoutCache {
	map: HashMap<[u32; 4], (Vec<TextGlyphQuad>, u64)>,
	lru_gen: u64,
	cap: usize,
}

impl TextLayoutCache {
	pub fn new(cap: usize) -> Self {
		Self {
			map: HashMap::with_capacity_and_hasher(cap + 1, Default::default()),
			lru_gen: 0,
			cap,
		}
	}

	pub fn clear(&mut self) {
		self.map.clear();
	}

	/// retrieve cached origin-relative quads if present, updating their LRU generation.
	pub fn get(
		&mut self,
		font_id: u32,
		content: &str,
		font_size: f32,
		wrap_width: Option<f32>,
	) -> Option<&Vec<TextGlyphQuad>> {
		let key = cache_key(font_id, content, font_size, wrap_width);
		let generation = self.lru_gen;
		if let Some(entry) = self.map.get_mut(&key) {
			entry.1 = generation;
			Some(&entry.0)
		} else {
			None
		}
	}

	/// insert origin-relative quads into the cache, evicting the LRU entry if at capacity.
	pub fn insert(
		&mut self,
		font_id: u32,
		content: &str,
		font_size: f32,
		wrap_width: Option<f32>,
		quads: Vec<TextGlyphQuad>,
	) {
		let key = cache_key(font_id, content, font_size, wrap_width);
		if self.map.len() >= self.cap && !self.map.contains_key(&key) {
			let lru = self.map.iter().min_by_key(|(_, (_, g))| g).map(|(k, _)| *k);
			if let Some(k) = lru {
				self.map.remove(&k);
			}
		}
		self.lru_gen += 1;
		self.map.insert(key, (quads, self.lru_gen));
	}
}

fn cache_key(font_id: u32, content: &str, font_size: f32, wrap_width: Option<f32>) -> [u32; 4] {
	let mut hasher = DefaultHasher::new();
	content.hash(&mut hasher);
	let content_hash = hasher.finish();
	let wrap_bits = wrap_width.map_or(0u32, f32::to_bits);
	[
		font_id,
		(content_hash >> 32) as u32,
		content_hash as u32,
		wrap_bits ^ f32::to_bits(font_size),
	]
}

/// shelf-packed RGBA glyph atlas backed by cosmic-text.
pub struct GlyphAtlas {
	/// atlas texture width in pixels.
	pub width: u32,
	/// atlas texture height in pixels.
	pub height: u32,
	/// packed RGBA pixel data.
	pub pixels: Vec<u8>,
	font_system: FontSystem,
	swash_cache: SwashCache,
	entries: HashMap<cosmic_text::CacheKey, Option<AtlasEntry>>,
	font_families: HashMap<u32, String>,
	cursor_x: u32,
	cursor_y: u32,
	row_height: u32,
	/// physical pixels per game pixel.
	pub scale: f32,
	/// true when the cpu pixel buffer changed since last gpu upload.
	pub dirty: bool,
}

impl GlyphAtlas {
	/// create a new empty atlas.
	pub fn new(width: u32, height: u32) -> Self {
		let font_system = FontSystem::new_with_locale_and_db(
			"en-US".to_string(),
			cosmic_text::fontdb::Database::new(),
		);
		Self {
			width,
			height,
			pixels: vec![0u8; (width * height * 4) as usize],
			font_system,
			swash_cache: SwashCache::new(),
			entries: HashMap::default(),
			font_families: HashMap::default(),
			cursor_x: 0,
			cursor_y: 0,
			row_height: 0,
			scale: 1.0,
			dirty: false,
		}
	}

	/// update the rasterization scale. returns `true` if the scale changed.
	pub fn set_scale(&mut self, scale: f32) -> bool {
		let clamped = scale.clamp(0.25, 8.0);
		let rounded = (clamped * 100.0).round() / 100.0;
		if (self.scale - rounded).abs() > 0.005 {
			self.scale = rounded;
			self.entries.clear();
			self.pixels.fill(0);
			self.swash_cache = SwashCache::new();
			self.cursor_x = 0;
			self.cursor_y = 0;
			self.row_height = 0;
			self.dirty = true;
			true
		} else {
			false
		}
	}

	/// register a font from raw bytes. no-op if already registered.
	pub fn register_font(&mut self, font_id: u32, data: &[u8]) {
		if self.font_families.contains_key(&font_id) {
			return;
		}
		// collect existing face ids before loading
		let before: rustc_hash::FxHashSet<cosmic_text::fontdb::ID> =
			self.font_system.db().faces().map(|f| f.id).collect();
		self.font_system.db_mut().load_font_data(data.to_vec());
		// find newly added face and grab its first family name
		let family_name = self
			.font_system
			.db()
			.faces()
			.find(|f| !before.contains(&f.id))
			.and_then(|f| f.families.first())
			.map(|(name, _)| name.clone());
		match family_name {
			Some(name) => {
				self.font_families.insert(font_id, name);
			}
			None => log::warn!("failed to detect family name for font {font_id}"),
		}
	}

	/// ensure a glyph is in the atlas. stores `None` for zero-size or failed glyphs.
	fn ensure_glyph(&mut self, cache_key: cosmic_text::CacheKey) {
		if self.entries.contains_key(&cache_key) {
			return;
		}
		// collect image data then drop borrow before writing to self.pixels
		let image_data = self
			.swash_cache
			.get_image(&mut self.font_system, cache_key)
			.as_ref()
			.map(|img| (img.data.to_vec(), img.placement, img.content));
		let Some((data, placement, content)) = image_data else {
			self.entries.insert(cache_key, None);
			return;
		};
		let gw = placement.width;
		let gh = placement.height;
		if gw == 0 || gh == 0 || data.is_empty() {
			self.entries.insert(cache_key, None);
			return;
		}

		// wrap to next shelf row if needed
		if self.cursor_x + gw > self.width {
			self.cursor_x = 0;
			self.cursor_y += self.row_height;
			self.row_height = 0;
		}
		if self.cursor_y + gh > self.height {
			log::warn!("glyph atlas full — glyph dropped. increase atlas dimensions.");
			self.entries.insert(cache_key, None);
			return;
		}

		match content {
			SwashContent::Mask => {
				// 1 byte per pixel (alpha coverage)
				for gy in 0..gh {
					for gx in 0..gw {
						let src = data[(gy * gw + gx) as usize];
						let dst =
							((self.cursor_y + gy) * self.width + self.cursor_x + gx) as usize * 4;
						self.pixels[dst] = 0xff;
						self.pixels[dst + 1] = 0xff;
						self.pixels[dst + 2] = 0xff;
						self.pixels[dst + 3] = src;
					}
				}
			}
			SwashContent::Color => {
				// 4 bytes per pixel RGBA
				for gy in 0..gh {
					for gx in 0..gw {
						let src = ((gy * gw + gx) * 4) as usize;
						let dst =
							((self.cursor_y + gy) * self.width + self.cursor_x + gx) as usize * 4;
						self.pixels[dst..dst + 4].copy_from_slice(&data[src..src + 4]);
					}
				}
			}
			SwashContent::SubpixelMask => {
				// treat like Mask, use R channel as alpha
				for gy in 0..gh {
					for gx in 0..gw {
						let src = ((gy * gw + gx) * 3) as usize;
						let alpha = data[src]; // R channel
						let dst =
							((self.cursor_y + gy) * self.width + self.cursor_x + gx) as usize * 4;
						self.pixels[dst] = 0xff;
						self.pixels[dst + 1] = 0xff;
						self.pixels[dst + 2] = 0xff;
						self.pixels[dst + 3] = alpha;
					}
				}
			}
		}

		self.entries.insert(
			cache_key,
			Some(AtlasEntry {
				x: self.cursor_x,
				y: self.cursor_y,
				width: gw,
				height: gh,
				left: placement.left,
				top: placement.top,
			}),
		);

		self.cursor_x += gw;
		if gh > self.row_height {
			self.row_height = gh;
		}
		self.dirty = true;
	}

	/// get the raw pixel data for uploading to the GPU.
	pub fn pixels(&self) -> &[u8] {
		&self.pixels
	}
}

/// look up the font family name for a given font id.
fn family_name_for(atlas: &GlyphAtlas, font_id: u32) -> Option<String> {
	atlas.font_families.get(&font_id).cloned()
}

/// layout a string into an existing vec (cleared first). reuses the vec's allocation.
///
/// `position` is the top-left of the text block in game units.
pub fn layout_text_into(
	atlas: &mut GlyphAtlas,
	font_id: u32,
	text: &str,
	font_size: f32,
	position: Vec2,
	out: &mut Vec<TextGlyphQuad>,
) {
	out.clear();
	let scale = atlas.scale;
	let display_size = font_size * scale;
	let family = family_name_for(atlas, font_id);
	let mut buffer = Buffer::new(
		&mut atlas.font_system,
		Metrics::new(display_size, display_size * 1.2),
	);
	buffer.set_size(&mut atlas.font_system, None, None);
	{
		let attrs = family
			.as_deref()
			.map_or_else(Attrs::new, |n| Attrs::new().family(Family::Name(n)));
		buffer.set_text(&mut atlas.font_system, text, attrs, Shaping::Advanced);
	}
	buffer.shape_until_scroll(&mut atlas.font_system, false);

	for run in buffer.layout_runs() {
		for glyph in run.glyphs {
			let physical = glyph.physical((0.0, run.line_y), 1.0);
			atlas.ensure_glyph(physical.cache_key);
			let Some(Some(entry)) = atlas.entries.get(&physical.cache_key) else {
				continue;
			};
			let entry = entry.clone();
			#[allow(clippy::cast_precision_loss)]
			let bx = physical.x as f32 + entry.left as f32;
			#[allow(clippy::cast_precision_loss)]
			let by = physical.y as f32 - entry.top as f32;
			let game_x = position.x + bx / scale;
			let game_y = position.y + by / scale;
			let game_w = entry.width as f32 / scale;
			let game_h = entry.height as f32 / scale;
			#[allow(clippy::cast_precision_loss)]
			let uv_min = Vec2::new(
				entry.x as f32 / atlas.width as f32,
				entry.y as f32 / atlas.height as f32,
			);
			#[allow(clippy::cast_precision_loss)]
			let uv_max = Vec2::new(
				(entry.x + entry.width) as f32 / atlas.width as f32,
				(entry.y + entry.height) as f32 / atlas.height as f32,
			);
			out.push(TextGlyphQuad {
				position: Vec2::new(game_x, game_y),
				size: Vec2::new(game_w, game_h),
				uv_min,
				uv_max,
			});
		}
	}
}

/// measure the maximum line width of a string in game units.
#[allow(dead_code)]
pub fn measure_text(atlas: &mut GlyphAtlas, font_id: u32, text: &str, font_size: f32) -> f32 {
	let scale = atlas.scale;
	let display_size = font_size * scale;
	let family = family_name_for(atlas, font_id);
	let mut buffer = Buffer::new(
		&mut atlas.font_system,
		Metrics::new(display_size, display_size * 1.2),
	);
	buffer.set_size(&mut atlas.font_system, None, None);
	{
		let attrs = family
			.as_deref()
			.map_or_else(Attrs::new, |n| Attrs::new().family(Family::Name(n)));
		buffer.set_text(&mut atlas.font_system, text, attrs, Shaping::Advanced);
	}
	buffer.shape_until_scroll(&mut atlas.font_system, false);
	buffer
		.layout_runs()
		.map(|r| r.line_w)
		.fold(0.0f32, f32::max)
		/ scale
}

/// layout wrapped text into an existing flat vec (cleared first). reuses the vec's allocation.
///
/// unlike `layout_text_wrapped`, all runs are flattened into a single `out` vec — use this
/// in the hot path to avoid per-frame inner-vec allocations.
#[allow(clippy::too_many_arguments)]
pub fn layout_text_wrapped_into(
	atlas: &mut GlyphAtlas,
	font_id: u32,
	text: &str,
	font_size: f32,
	position: Vec2,
	max_width: f32,
	line_height: f32,
	out: &mut Vec<TextGlyphQuad>,
) {
	out.clear();
	let effective_line_height = if line_height > 0.0 {
		line_height
	} else {
		font_size * 1.25
	};
	let scale = atlas.scale;
	let display_size = font_size * scale;
	let family = family_name_for(atlas, font_id);
	let mut buffer = Buffer::new(
		&mut atlas.font_system,
		Metrics::new(display_size, effective_line_height * scale),
	);
	buffer.set_size(&mut atlas.font_system, Some(max_width * scale), None);
	{
		let attrs = family
			.as_deref()
			.map_or_else(Attrs::new, |n| Attrs::new().family(Family::Name(n)));
		buffer.set_text(&mut atlas.font_system, text, attrs, Shaping::Advanced);
	}
	buffer.shape_until_scroll(&mut atlas.font_system, false);

	for run in buffer.layout_runs() {
		for glyph in run.glyphs {
			let physical = glyph.physical((0.0, run.line_y), 1.0);
			atlas.ensure_glyph(physical.cache_key);
			let Some(Some(entry)) = atlas.entries.get(&physical.cache_key) else {
				continue;
			};
			let entry = entry.clone();
			#[allow(clippy::cast_precision_loss)]
			let bx = physical.x as f32 + entry.left as f32;
			#[allow(clippy::cast_precision_loss)]
			let by = physical.y as f32 - entry.top as f32;
			let game_x = position.x + bx / scale;
			let game_y = position.y + by / scale;
			let game_w = entry.width as f32 / scale;
			let game_h = entry.height as f32 / scale;
			#[allow(clippy::cast_precision_loss)]
			let uv_min = Vec2::new(
				entry.x as f32 / atlas.width as f32,
				entry.y as f32 / atlas.height as f32,
			);
			#[allow(clippy::cast_precision_loss)]
			let uv_max = Vec2::new(
				(entry.x + entry.width) as f32 / atlas.width as f32,
				(entry.y + entry.height) as f32 / atlas.height as f32,
			);
			out.push(TextGlyphQuad {
				position: Vec2::new(game_x, game_y),
				size: Vec2::new(game_w, game_h),
				uv_min,
				uv_max,
			});
		}
	}
}