oarust 0.20.0

GPU-first semantic computing runtime written in Rust
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
use crate::{Error, Result, runtime::dna::kernel::ShaderArtifact};

use super::super::physical::DeviceLimits;

/// Custom pipeline families — render passes in [`render`], UI raster
/// families in [`ui`]. The generic descriptor-driven builder lives in this
/// module; each family only declares what differs.
pub(crate) mod render;
pub(crate) mod ui;

// ── Graphics pipeline descriptors ────────────────────────────────────────────

/// Push-constant block contract for an owned pipeline layout.
#[derive(Clone, Copy)]
pub(crate) struct GraphicsPush {
	/// Block size in bytes; validated against the device limit when the
	/// caller supplies one.
	pub(crate) size: u32,
	/// Stages that read the block.
	pub(crate) stages: vk::ShaderStageFlagBits,
}

/// Where a graphics pipeline's layout comes from.
#[derive(Clone)]
pub(crate) enum GraphicsLayoutSource {
	/// Create and own a layout from descriptor set layouts and a push block.
	Owned {
		/// Descriptor set layouts in set order (empty for push-only layouts).
		set_layouts: Vec<vk::DescriptorSetLayout>,
		/// Push-constant block contract.
		push: GraphicsPush,
	},
	/// Borrow a caller-owned layout; the pipeline never destroys it. Used by
	/// the Surface transparent pass (shares the opaque layout) and by every
	/// UI raster family (shares the compositor's per-heap layouts).
	External(vk::PipelineLayout),
}

/// Vertex-fetch configuration for a graphics pipeline.
#[derive(Clone, Copy)]
pub(crate) enum GraphicsVertexInput {
	/// MeshVertex, all four attributes: position(0,f3,off0), normal(1,f3,off12),
	/// uv(2,f2,off24), color(3,f4,off32) — stride 48.
	MeshVertex,
	/// Vertex-color lit variant: position(0,f3,off0), normal(1,f3,off12),
	/// color(2,f4,off32) — stride 48. The lit shader skips the uv attribute
	/// and reads vertex color at location 2.
	MeshVertexLit,
	/// No vertex buffers — the vertex shader derives geometry from
	/// SV_VertexID (fullscreen triangle / instanced quads).
	Vertexless,
}

/// Depth-attachment behavior for a graphics pipeline.
#[derive(Clone, Copy)]
pub(crate) enum GraphicsDepth {
	/// No depth test or write.
	Disabled,
	/// Depth test and write with LESS compare (opaque 3D pass).
	WriteAndTest,
	/// Depth test with LESS compare, no write (back-to-front transparent pass).
	TestOnly,
}

/// Fixed-function blend state for the single color attachment.
#[derive(Clone, Copy)]
pub(crate) enum GraphicsBlend {
	/// Blend disabled — the fragment value replaces the attachment, matching
	/// a compute path that stored into the compose image directly.
	Opaque,
	/// `src + dst * (1 - src.a)` — fragments return premultiplied alpha,
	/// reproducing the compute `uiSourceOver` result exactly.
	PremultipliedAlpha,
	/// `dst * src` — coverage masks multiply into the attachment.
	Modulate,
	/// `src + dst` — contributions accumulate into the attachment.
	Additive,
}

/// One graphics pipeline described declaratively.
///
/// Every render or UI pass reduces to this record: which shaders, where the
/// layout comes from, vertex fetch, depth behavior, blend mode, and
/// attachment formats. [`GraphicsPipeline::create`] owns all create-info
/// assembly, shader-module lifetime, and failure cleanup — passes differ only
/// in the fields of this record.
pub(crate) struct GraphicsPipelineDesc {
	/// Diagnostic name used in creation errors.
	pub(crate) name: &'static str,
	/// SPIR-V bytes for the vertex stage.
	pub(crate) vertex_spv: &'static [u8],
	/// SPIR-V bytes for the fragment stage.
	pub(crate) fragment_spv: &'static [u8],
	/// Owned or borrowed pipeline layout.
	pub(crate) layout: GraphicsLayoutSource,
	/// Vertex-fetch state.
	pub(crate) vertex: GraphicsVertexInput,
	/// Depth test/write behavior.
	pub(crate) depth: GraphicsDepth,
	/// Color-attachment blend state.
	pub(crate) blend: GraphicsBlend,
	/// Single color attachment format.
	pub(crate) color_format: vk::Format,
	/// Depth attachment format; `UNDEFINED` when the pass has no depth target.
	pub(crate) depth_format: vk::Format,
}

// ── Graphics pipeline ─────────────────────────────────────────────────────────

/// Owned graphics pipeline created from a [`GraphicsPipelineDesc`].
///
/// Holds the pipeline handle and its layout. A borrowed layout
/// ([`GraphicsLayoutSource::External`]) is retained for binding but never
/// destroyed with the pipeline; an owned layout retires in `Drop`.
///
/// Uses Vulkan 1.3 dynamic rendering (no render-pass object), dynamic
/// viewport and scissor, triangle-list assembly, fill rasterization with no
/// culling, single-sampled output, and one color attachment.
pub(crate) struct GraphicsPipeline {
	handle: vk::Pipeline,
	layout: vk::PipelineLayout,
	owns_layout: bool,
	device: std::sync::Arc<vk::Device>,
}

impl GraphicsPipeline {
	/// Create a graphics pipeline described by `desc`.
	///
	/// `max_push_constants_size`, when supplied, is validated against the push
	/// block of an owned layout; borrowed layouts carry their owner's contract.
	///
	/// # Errors
	///
	/// Returns an error when the device push-constant limit is too small for
	/// an owned layout, when the SPIR-V blobs are malformed, or when layout,
	/// shader-module, or pipeline creation fails.
	pub(crate) fn create(
		device: &std::sync::Arc<vk::Device>,
		max_push_constants_size: Option<u32>,
		cache: vk::PipelineCache,
		desc: &GraphicsPipelineDesc,
	) -> Result<Self> {
		let (layout, owns_layout) = match &desc.layout {
			GraphicsLayoutSource::External(layout) => (*layout, false),
			GraphicsLayoutSource::Owned { set_layouts, push } => {
				if max_push_constants_size.is_some_and(|limit| push.size > limit) {
					return Err(Error::missing_capability(format!(
						"{} pipeline requires {} bytes of push constants",
						desc.name, push.size
					)));
				}
				let push_range = vk::PushConstantRange::new()
					.set_stage_flags(push.stages)
					.set_offset(0)
					.set_size(push.size);
				let layout_info = vk::PipelineLayoutCreateInfo::new()
					.set_set_layouts(set_layouts)
					.map_err(Error::from)?
					.set_push_constant_ranges(std::slice::from_ref(&push_range));
				let layout =
					unsafe { device.create_pipeline_layout(&(layout_info).map_err(Error::from)?, None) }
						.map_err(|s| {
							Error::backend_failure("Vulkan", "graphics pipeline-layout creation", s)
						})?;
				(layout, true)
			}
		};
		let release_layout = |device: &vk::Device| {
			if owns_layout {
				destroy_pipeline_layout(device, layout);
			}
		};

		let vert_words = decode_spirv_bytes(desc.vertex_spv).inspect_err(|_| release_layout(device))?;
		let frag_words =
			decode_spirv_bytes(desc.fragment_spv).inspect_err(|_| release_layout(device))?;
		let vert_module =
			create_shader_module(device, &vert_words).inspect_err(|_| release_layout(device))?;
		let frag_module = match create_shader_module(device, &frag_words) {
			Ok(m) => m,
			Err(e) => {
				unsafe { device.destroy_shader_module(vert_module, None) }?;
				release_layout(device);
				return Err(e);
			}
		};

		let handle = match create_graphics_pipeline_handle(
			device,
			layout,
			vert_module,
			frag_module,
			desc,
			cache,
		) {
			Ok(h) => h,
			Err(e) => {
				release_layout(device);
				return Err(e);
			}
		};
		Ok(Self {
			handle,
			layout,
			owns_layout,
			device: std::sync::Arc::clone(device),
		})
	}

	pub(crate) const fn raw(&self) -> vk::Pipeline {
		self.handle
	}

	pub(crate) const fn layout(&self) -> vk::PipelineLayout {
		self.layout
	}
}

impl Drop for GraphicsPipeline {
	fn drop(&mut self) {
		destroy_pipeline(&self.device, self.handle);
		if self.owns_layout {
			destroy_pipeline_layout(&self.device, self.layout);
		}
	}
}

/// Assemble the create-info chain and create one graphics pipeline, then
/// destroy both shader modules regardless of outcome.
fn create_graphics_pipeline_handle(
	device: &vk::Device,
	layout: vk::PipelineLayout,
	vert_module: vk::ShaderModule,
	frag_module: vk::ShaderModule,
	desc: &GraphicsPipelineDesc,
	cache: vk::PipelineCache,
) -> Result<vk::Pipeline> {
	let stages = [
		vk::PipelineShaderStageCreateInfo::new()
			.set_stage(vk::ShaderStageFlagBits::VERTEX_BIT)
			.set_module(vert_module)
			.set_name(c"main"),
		vk::PipelineShaderStageCreateInfo::new()
			.set_stage(vk::ShaderStageFlagBits::FRAGMENT_BIT)
			.set_module(frag_module)
			.set_name(c"main"),
	];

	let attribute = |location: u32, format: vk::Format, offset: u32| {
		vk::VertexInputAttributeDescription::new()
			.set_location(location)
			.set_binding(0)
			.set_format(format)
			.set_offset(offset)
	};
	let binding = vk::VertexInputBindingDescription::new()
		.set_binding(0)
		.set_stride(48)
		.set_input_rate(vk::VertexInputRate::VERTEX);
	let attributes: Vec<vk::VertexInputAttributeDescription> = match desc.vertex {
		GraphicsVertexInput::Vertexless => Vec::new(),
		// position, normal, color at location 2 — the lit shader consumes no uv.
		GraphicsVertexInput::MeshVertexLit => vec![
			attribute(0, vk::Format::R32G32B32_SFLOAT, 0),
			attribute(1, vk::Format::R32G32B32_SFLOAT, 12),
			attribute(2, vk::Format::R32G32B32A32_SFLOAT, 32),
		],
		// position, normal, uv, color.
		GraphicsVertexInput::MeshVertex => vec![
			attribute(0, vk::Format::R32G32B32_SFLOAT, 0),
			attribute(1, vk::Format::R32G32B32_SFLOAT, 12),
			attribute(2, vk::Format::R32G32_SFLOAT, 24),
			attribute(3, vk::Format::R32G32B32A32_SFLOAT, 32),
		],
	};
	let vertex_input = if attributes.is_empty() {
		vk::PipelineVertexInputStateCreateInfo::new()
	} else {
		vk::PipelineVertexInputStateCreateInfo::new()
			.set_vertex_binding_descriptions(std::slice::from_ref(&binding))
			.map_err(Error::from)?
			.set_vertex_attribute_descriptions(&attributes)
			.map_err(Error::from)?
	};

	let assembly = vk::PipelineInputAssemblyStateCreateInfo::new()
		.set_topology(vk::PrimitiveTopology::TRIANGLE_LIST);
	let viewport_state = vk::PipelineViewportStateCreateInfo::new()
		.set_viewport_count(1)
		.set_scissor_count(1);
	let raster = vk::PipelineRasterizationStateCreateInfo::new()
		.set_polygon_mode(vk::PolygonMode::FILL)
		.set_cull_mode(vk::CullModeFlagBits::NONE)
		.set_front_face(vk::FrontFace::COUNTER_CLOCKWISE)
		.set_line_width(1.0);
	let multisample = vk::PipelineMultisampleStateCreateInfo::new()
		.set_rasterization_samples(vk::SampleCountFlagBits::_1_BIT);

	let depth_stencil = match desc.depth {
		GraphicsDepth::Disabled => vk::PipelineDepthStencilStateCreateInfo::new(),
		GraphicsDepth::WriteAndTest => vk::PipelineDepthStencilStateCreateInfo::new()
			.set_depth_test_enable(u32::from(true))
			.set_depth_write_enable(u32::from(true))
			.set_depth_compare_op(vk::CompareOp::LESS)
			.set_min_depth_bounds(0.0)
			.set_max_depth_bounds(1.0),
		GraphicsDepth::TestOnly => vk::PipelineDepthStencilStateCreateInfo::new()
			.set_depth_test_enable(u32::from(true))
			.set_depth_write_enable(u32::from(false))
			.set_depth_compare_op(vk::CompareOp::LESS)
			.set_min_depth_bounds(0.0)
			.set_max_depth_bounds(1.0),
	};

	let write_mask = vk::ColorComponentFlagBits::R_BIT
		| vk::ColorComponentFlagBits::G_BIT
		| vk::ColorComponentFlagBits::B_BIT
		| vk::ColorComponentFlagBits::A_BIT;
	let blend_attachment = match desc.blend {
		GraphicsBlend::Opaque => {
			vk::PipelineColorBlendAttachmentState::new().set_color_write_mask(write_mask)
		}
		GraphicsBlend::PremultipliedAlpha => vk::PipelineColorBlendAttachmentState::new()
			.set_blend_enable(u32::from(true))
			.set_src_color_blend_factor(vk::BlendFactor::ONE)
			.set_dst_color_blend_factor(vk::BlendFactor::ONE_MINUS_SRC_ALPHA)
			.set_color_blend_op(vk::BlendOp::ADD)
			.set_src_alpha_blend_factor(vk::BlendFactor::ONE)
			.set_dst_alpha_blend_factor(vk::BlendFactor::ONE_MINUS_SRC_ALPHA)
			.set_alpha_blend_op(vk::BlendOp::ADD)
			.set_color_write_mask(write_mask),
		GraphicsBlend::Modulate => vk::PipelineColorBlendAttachmentState::new()
			.set_blend_enable(u32::from(true))
			.set_src_color_blend_factor(vk::BlendFactor::ZERO)
			.set_dst_color_blend_factor(vk::BlendFactor::SRC_COLOR)
			.set_color_blend_op(vk::BlendOp::ADD)
			.set_src_alpha_blend_factor(vk::BlendFactor::ZERO)
			.set_dst_alpha_blend_factor(vk::BlendFactor::SRC_ALPHA)
			.set_alpha_blend_op(vk::BlendOp::ADD)
			.set_color_write_mask(write_mask),
		GraphicsBlend::Additive => vk::PipelineColorBlendAttachmentState::new()
			.set_blend_enable(u32::from(true))
			.set_src_color_blend_factor(vk::BlendFactor::ONE)
			.set_dst_color_blend_factor(vk::BlendFactor::ONE)
			.set_color_blend_op(vk::BlendOp::ADD)
			.set_src_alpha_blend_factor(vk::BlendFactor::ONE)
			.set_dst_alpha_blend_factor(vk::BlendFactor::ONE)
			.set_alpha_blend_op(vk::BlendOp::ADD)
			.set_color_write_mask(write_mask),
	};
	let color_blend = vk::PipelineColorBlendStateCreateInfo::new()
		.set_attachments(std::slice::from_ref(&blend_attachment))
		.map_err(Error::from)?;

	let dynamic_states = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR];
	let dynamic = vk::PipelineDynamicStateCreateInfo::new()
		.set_dynamic_states(&dynamic_states)
		.map_err(Error::from)?;
	let color_formats = [desc.color_format];
	let mut rendering = vk::PipelineRenderingCreateInfo::new()
		.set_color_attachment_formats(&color_formats)
		.map_err(Error::from)?
		.set_depth_attachment_format(desc.depth_format);
	let pipeline_info = vk::GraphicsPipelineCreateInfo::new()
		.push_next(&mut rendering)
		.map_err(Error::from)?
		.set_stages(&stages)
		.map_err(Error::from)?
		.set_vertex_input_state(&vertex_input)
		.set_input_assembly_state(&assembly)
		.set_viewport_state(&viewport_state)
		.set_rasterization_state(&raster)
		.set_multisample_state(&multisample)
		.set_depth_stencil_state(&depth_stencil)
		.set_color_blend_state(&color_blend)
		.set_dynamic_state(&dynamic)
		.set_layout(layout);

	let mut pipelines = [vk::Pipeline::default(); 1];
	// SAFETY: the create-info chain and output buffer remain live and writable
	// for the call; partial output is destroyed on failure.
	let result = unsafe {
		device.create_graphics_pipelines(
			cache,
			std::slice::from_ref(&pipeline_info),
			None,
			&mut pipelines,
		)
	};
	unsafe {
		let _ = device.destroy_shader_module(vert_module, None);
		let _ = device.destroy_shader_module(frag_module, None);
	}
	match result {
		Ok(vk::Result::SUCCESS) => Ok(pipelines[0]),
		result => {
			for handle in pipelines
				.into_iter()
				.filter(|handle| *handle != vk::Pipeline::default())
			{
				destroy_pipeline(device, handle);
			}
			Err(Error::backend_dispatch_failure(
				"Vulkan",
				"graphics-pipeline creation",
				result,
			))
		}
	}
}

pub(crate) fn decode_spirv_bytes(bytes: &[u8]) -> Result<Vec<u32>> {
	if bytes.len() < 4 || !bytes.len().is_multiple_of(4) {
		return Err(Error::backend_failure(
			"Vulkan",
			"graphics shader SPIR-V byte length",
			std::io::Error::other("not a multiple of 4"),
		));
	}
	Ok(
		bytes
			.chunks(4)
			.map(|c| {
				let mut word = [0u8; 4];
				word.copy_from_slice(c);
				u32::from_le_bytes(word)
			})
			.collect(),
	)
}

fn create_shader_module(device: &vk::Device, words: &[u32]) -> Result<vk::ShaderModule> {
	let info = vk::ShaderModuleCreateInfo::new().set_code(words);
	unsafe { device.create_shader_module(&info, None) }
		.map_err(|s| Error::backend_failure("Vulkan", "graphics shader-module creation", s))
}

// ── Pipeline cache persistence ────────────────────────────────────────────────

/// File name of the legacy raw driver blob below `var/vk`, retained only as a
/// migration source for the scoped `.vkcache` envelope.
const LEGACY_PIPELINE_CACHE_FILE: &str = "pipeline.vcache";

/// Path of the legacy raw pipeline cache — read once for import, never written.
pub(in crate::runtime) fn legacy_pipeline_cache_path() -> crate::Path {
	crate::Path::var()
		.join("vk")
		.join(LEGACY_PIPELINE_CACHE_FILE)
}

/// Owned `VkPipelineCache` optionally seeded with persisted driver data.
///
/// Vulkan requires external synchronization for every host access to one
/// pipeline cache. Serialized single-pipeline creation shares this cache under
/// the device creation lock; parallel preload gives each worker its own cache
/// and merges worker caches back only after every worker has joined.
pub(in crate::runtime::device) struct PipelineCache {
	handle: vk::PipelineCache,
	device: std::sync::Arc<vk::Device>,
	initial_data_bytes: usize,
}

impl PipelineCache {
	/// Create a pipeline cache seeded with `initial_data` (empty means cold).
	///
	/// # Errors
	/// Returns an error when `vkCreatePipelineCache` rejects the seed data or
	/// fails on the device.
	pub(in crate::runtime::device) fn new(
		device: std::sync::Arc<vk::Device>,
		initial_data: &[u8],
	) -> Result<Self> {
		let info = vk::PipelineCacheCreateInfo::new()
			.set_initial_data(initial_data)
			.map_err(Error::from)?;
		// SAFETY: the seed slice outlives the call, and the retained device
		// outlives the returned cache.
		let handle = unsafe { device.create_pipeline_cache(&info, None) }
			.map_err(|s| Error::backend_failure("Vulkan", "pipeline-cache creation", s))?;
		Ok(Self {
			handle,
			device,
			initial_data_bytes: initial_data.len(),
		})
	}

	/// Open a pipeline cache seeded with persisted driver data.
	///
	/// The authoritative source is the scope's `.vkcache` driver section. A
	/// legacy `pipeline.vcache` is imported only when no envelope exists for
	/// this scope, and only after its standard Vulkan header validates against
	/// the selected device. An absent, mismatched or corrupt seed still
	/// produces a live cold cache; `None` is returned only when the driver
	/// cannot create any cache, in which case callers run uncached.
	pub(in crate::runtime::device) fn open(
		device: &std::sync::Arc<vk::Device>,
		physical: &super::super::physical::DevicePhysical,
	) -> Option<Self> {
		let scope = super::vkcache::VkCacheScope::new(physical);
		let path = scope.path();
		let (initial_data, source) = if path.is_file() {
			// A present envelope is authoritative: no legacy import, even when
			// the driver section is absent or fails validation.
			let data = super::vkcache::VkCacheFile::open(&path, &scope)
				.ok()
				.and_then(|file| {
					file
						.section(super::vkcache::SECTION_DRIVER_PIPELINE_CACHE)
						.filter(|section| section.version == 1)
						.map(|section| section.bytes.clone())
				})
				.filter(|bytes| super::vkcache::driver_blob_matches_scope(bytes, &scope))
				.unwrap_or_default();
			(data, path.display().to_string())
		} else {
			let legacy = legacy_pipeline_cache_path();
			let data = crate::Filesystem::read_binary(&legacy)
				.ok()
				.filter(|bytes| super::vkcache::driver_blob_matches_scope(bytes, &scope))
				.unwrap_or_default();
			(data, legacy.display().to_string())
		};
		match Self::new(std::sync::Arc::clone(device), &initial_data) {
			Ok(cache) => {
				if !initial_data.is_empty() {
					crate::log_info!(
						crate::LogComponent::COMPUTE,
						"pipeline cache: loaded {} bytes from {}",
						initial_data.len(),
						source
					);
				}
				Some(cache)
			}
			Err(_) if !initial_data.is_empty() => {
				crate::log_warn!(
					crate::LogComponent::COMPUTE,
					"pipeline cache: driver rejected persisted data; starting cold"
				);
				Self::new(std::sync::Arc::clone(device), &[]).ok()
			}
			Err(_) => None,
		}
	}

	/// Serialize the driver-owned cache blob.
	pub(in crate::runtime::device) fn data(&self) -> Result<Vec<u8>> {
		// SAFETY: `handle` is a live cache created on this device.
		unsafe { self.device.get_pipeline_cache_data(self.handle) }
			.map_err(|s| Error::backend_failure("Vulkan", "pipeline-cache data query", s))
	}

	/// Merge externally synchronized worker caches into this cache.
	///
	/// Callers must have joined every thread that produced `sources`; no host
	/// may touch source or destination concurrently with the merge.
	pub(in crate::runtime::device) fn merge(&self, sources: &[vk::PipelineCache]) -> Result<()> {
		if sources.is_empty() {
			return Ok(());
		}
		// SAFETY: the caller joins all producer threads before merging, so
		// sources and destination are externally synchronized.
		unsafe { self.device.merge_pipeline_caches(self.handle, sources) }
			.map_err(|s| Error::backend_failure("Vulkan", "pipeline-cache merge", s))
	}

	pub(in crate::runtime::device) const fn raw(&self) -> vk::PipelineCache {
		self.handle
	}

	pub(in crate::runtime::device) const fn initial_data_bytes(&self) -> usize {
		self.initial_data_bytes
	}
}

impl Drop for PipelineCache {
	fn drop(&mut self) {
		// SAFETY: the handle belongs to `device` and is destroyed at most once.
		let _ = unsafe { self.device.destroy_pipeline_cache(self.handle, None) };
	}
}

// ── Compute pipeline ──────────────────────────────────────────────────────────

pub(crate) struct ComputePipeline {
	handle: vk::Pipeline,
	layout: vk::PipelineLayout,
	max_dispatch_group_count: [u32; 3],
	push_constant_size: u32,
	device: std::sync::Arc<vk::Device>,
}

impl ComputePipeline {
	pub(in crate::runtime::device) fn new(
		device: std::sync::Arc<vk::Device>,
		descriptor_layout: vk::DescriptorSetLayout,
		artifact: &'static ShaderArtifact,
		limits: DeviceLimits,
		cache: vk::PipelineCache,
	) -> Result<Self> {
		Self::from_words(
			device,
			descriptor_layout,
			&artifact.spirv_words()?,
			artifact.workgroup_size,
			limits,
			cache,
		)
	}

	/// Consume validated SPIR-V synchronously; the module does not borrow its words.
	/// Callers own target, descriptor, capability and validation admission.
	pub(in crate::runtime::device) fn from_words(
		device: std::sync::Arc<vk::Device>,
		descriptor_layout: vk::DescriptorSetLayout,
		words: &[u32],
		workgroup_size: [u32; 3],
		limits: DeviceLimits,
		cache: vk::PipelineCache,
	) -> Result<Self> {
		let push_constant_size = crate::runtime::dna::kernel::reflect_push_constant_size(words)?;
		validate_artifact_limits(workgroup_size, push_constant_size, limits)?;

		let descriptor_layouts = [descriptor_layout];
		let push_range = vk::PushConstantRange::new()
			.set_stage_flags(vk::ShaderStageFlagBits::COMPUTE_BIT)
			.set_offset(0)
			.set_size(push_constant_size);
		let push_ranges = [push_range];
		let layout_info = vk::PipelineLayoutCreateInfo::new()
			.set_set_layouts(&descriptor_layouts)
			.map_err(Error::from)?
			.set_push_constant_ranges(&push_ranges);
		// SAFETY: the shared descriptor layout is live and the reflected push range
		// fits the queried device limit and Vulkan's four-byte alignment requirements.
		let layout =
			unsafe { device.create_pipeline_layout(&(layout_info).map_err(Error::from)?, None) }
				.map_err(|source| {
					Error::backend_failure("Vulkan", "compute pipeline-layout creation", source)
				})?;

		let module_info = vk::ShaderModuleCreateInfo::new().set_code(words);
		// SAFETY: the caller's spirv-val and reflection admission proved a complete
		// SPIR-V word stream; the device remains live through pipeline creation.
		let module = match unsafe { device.create_shader_module(&module_info, None) } {
			Ok(module) => module,
			Err(source) => {
				destroy_pipeline_layout(&device, layout);
				return Err(Error::backend_failure(
					"Vulkan",
					"compute shader-module creation",
					source,
				));
			}
		};

		let stage = vk::PipelineShaderStageCreateInfo::new()
			.set_stage(vk::ShaderStageFlagBits::COMPUTE_BIT)
			.set_module(module)
			.set_name(c"main");
		let pipeline_info = vk::ComputePipelineCreateInfo::new()
			.set_stage(stage)
			.set_layout(layout);
		// SAFETY: the module contains the reflected compute entry point, and the
		// pipeline layout exactly matches its validated descriptor and push ABI.
		let mut pipelines = [vk::Pipeline::default(); 1];
		let pipeline_result =
			unsafe { device.create_compute_pipelines(cache, &[pipeline_info], None, &mut pipelines) };
		// The module is no longer needed after pipeline creation, successful or not.
		unsafe {
			let _ = device.destroy_shader_module(module, None);
		}
		let handle = match pipeline_result {
			Ok(vk::Result::SUCCESS) => pipelines[0],
			result => {
				for pipeline in pipelines
					.into_iter()
					.filter(|handle| *handle != vk::Pipeline::default())
				{
					destroy_pipeline(&device, pipeline);
				}
				destroy_pipeline_layout(&device, layout);
				return Err(Error::backend_dispatch_failure(
					"Vulkan",
					"compute-pipeline creation",
					result,
				));
			}
		};

		Ok(Self {
			handle,
			layout,
			max_dispatch_group_count: limits.max_compute_work_group_count,
			push_constant_size,
			device,
		})
	}

	pub(in crate::runtime::device) fn destroy(&mut self, device: &vk::Device) {
		destroy_pipeline(device, self.handle);
		destroy_pipeline_layout(device, self.layout);
		self.handle = vk::Pipeline::NULL;
		self.layout = vk::PipelineLayout::NULL;
	}

	pub(in crate::runtime::device) const fn raw(&self) -> vk::Pipeline {
		self.handle
	}

	pub(in crate::runtime::device) const fn layout(&self) -> vk::PipelineLayout {
		self.layout
	}

	pub(crate) const fn max_dispatch_group_count(&self) -> [u32; 3] {
		self.max_dispatch_group_count
	}

	pub(crate) const fn push_constant_size(&self) -> u32 {
		self.push_constant_size
	}
}

impl Drop for ComputePipeline {
	fn drop(&mut self) {
		// Registry teardown explicitly destroys pipelines before descriptor layouts;
		// owned specializations instead retire with the last recorded command.
		if self.handle != vk::Pipeline::NULL {
			destroy_pipeline(&self.device, self.handle);
		}
		if self.layout != vk::PipelineLayout::NULL {
			destroy_pipeline_layout(&self.device, self.layout);
		}
	}
}

pub(in crate::runtime::device) fn validate_artifact_limits(
	workgroup_size: [u32; 3],
	push_constant_size: u32,
	limits: DeviceLimits,
) -> Result<()> {
	let invocation_count = workgroup_size
		.into_iter()
		.try_fold(1_u32, u32::checked_mul)
		.ok_or_else(|| Error::missing_capability("shader workgroup size overflows u32"))?;
	if push_constant_size > limits.max_push_constants_size
		|| invocation_count > limits.max_compute_work_group_invocations
		|| workgroup_size
			.into_iter()
			.zip(limits.max_compute_work_group_size)
			.any(|(required, available)| required > available)
	{
		return Err(Error::missing_capability(
			"selected device cannot admit the reflected compute shader ABI",
		));
	}
	Ok(())
}

fn destroy_pipeline(device: &vk::Device, pipeline: vk::Pipeline) {
	// SAFETY: the handle belongs to `device` and is destroyed at most once after use.
	let _ = unsafe { device.destroy_pipeline(pipeline, None) };
}

fn destroy_pipeline_layout(device: &vk::Device, layout: vk::PipelineLayout) {
	// SAFETY: the layout belongs to `device` and no live pipeline uses it.
	let _ = unsafe { device.destroy_pipeline_layout(layout, None) };
}