diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 07:00:08 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 07:00:08 +0300 |
| commit | dcba82d1cdf4a70ab879d8af0a623a10deecb544 (patch) | |
| tree | 990640d51dc917b59baf0c02a7d7bdba12557cef /adapters/fparkan-render-vulkan/src/ffi | |
| parent | 5be1b800d028ba30c5e2a4ff84983c700b4e9175 (diff) | |
| download | fparkan-dcba82d1cdf4a70ab879d8af0a623a10deecb544.tar.xz fparkan-dcba82d1cdf4a70ab879d8af0a623a10deecb544.zip | |
feat(factory): build newly loaded projects without restarting the mission
Transfer a saved design from the original designer panel, prepare it with the regular AniMesh scene builder, and queue the selected factory order. Appended world batches keep existing draw handles and gain the normal cockpit, walking animation, weapon and shield bindings.
The Field Camp window witness adds a one-gun Unit DAT after mission start, uses the normal transfer and order controls, supplies the factory with a scripted test fixture, and captures the released unit with Vulkan validation clean. The two licensed factory tests also verify the assembled component records and weapon control.
Diffstat (limited to 'adapters/fparkan-render-vulkan/src/ffi')
4 files changed, 268 insertions, 55 deletions
diff --git a/adapters/fparkan-render-vulkan/src/ffi/preview_mesh.rs b/adapters/fparkan-render-vulkan/src/ffi/preview_mesh.rs index 3ff1e4db..1626e05e 100644 --- a/adapters/fparkan-render-vulkan/src/ffi/preview_mesh.rs +++ b/adapters/fparkan-render-vulkan/src/ffi/preview_mesh.rs @@ -109,26 +109,9 @@ impl VulkanSmokeRenderer { (self.draw_ranges.len(), self.material_alphas.len()), |mesh| (mesh.range_start, mesh.material_start), ); - let neutral = |rgba8: [u8; 4]| VulkanStaticTexture { - width: 1, - height: 1, - rgba8: rgba8.to_vec(), - mip_levels: Vec::new(), - }; - let detail = neutral([128, 128, 128, 255]); - let overlay = neutral([255, 255, 255, 0]); - let sources = materials - .iter() - .flat_map(|material| { - [ - &material.texture, - material.detail_texture.as_ref().unwrap_or(&detail), - material.overlay_texture.as_ref().unwrap_or(&overlay), - material.overlay_detail_texture.as_ref().unwrap_or(&detail), - ] - }) - .collect::<Vec<_>>(); - let (sources, slots) = deduplicate_texture_sources(&sources); + let detail = detail_texture(); + let overlay = overlay_texture(); + let (sources, slots) = material_texture_sources(materials, &detail, &overlay); let mut pipeline_states = self.draw_range_base_pipeline_states[..range_start].to_vec(); pipeline_states.extend(mesh.draw_ranges.iter().map(|range| range.pipeline_state)); let (preview, resources) = { @@ -205,9 +188,27 @@ impl VulkanSmokeRenderer { self.draw_ranges.truncate(range_start); self.draw_ranges.extend(&mesh.draw_ranges); + self.replace_draw_state( + range_start, + material_start, + &mesh.draw_ranges, + local_materials, + ); + self.draw_range_base_pipeline_states = pipeline_states; + self.replace_material_state(material_start, materials); + Ok(range_start..self.draw_ranges.len()) + } + + pub(super) fn replace_draw_state( + &mut self, + range_start: usize, + material_start: usize, + ranges: &[crate::ffi::VulkanStaticDrawRange], + local_materials: Vec<usize>, + ) { self.draw_range_capacities.truncate(range_start); self.draw_range_capacities - .extend(mesh.draw_ranges.iter().map(|range| range.index_count)); + .extend(ranges.iter().map(|range| range.index_count)); self.draw_texture_indices.truncate(range_start); self.draw_texture_indices.extend( local_materials @@ -216,7 +217,6 @@ impl VulkanSmokeRenderer { ); self.reflection_planes.truncate(range_start); self.reflection_planes.resize(self.draw_ranges.len(), None); - self.draw_range_base_pipeline_states = pipeline_states; self.draw_range_sort_keys.truncate(range_start); self.draw_range_sort_keys .resize(self.draw_ranges.len(), None); @@ -227,7 +227,13 @@ impl VulkanSmokeRenderer { .truncate(range_start); self.draw_range_diffuse_alpha_overrides .resize(self.draw_ranges.len(), None); + } + pub(super) fn replace_material_state( + &mut self, + material_start: usize, + materials: &[VulkanStaticMaterial], + ) { self.material_alphas.truncate(material_start); self.material_lighting.truncate(material_start); self.material_uv_transforms.truncate(material_start); @@ -274,10 +280,45 @@ impl VulkanSmokeRenderer { self.material_selectors.push(material.material_index); self.material_unlit.push(material.encoded_material_mode()); } - Ok(range_start..self.draw_ranges.len()) } } +pub(super) fn detail_texture() -> VulkanStaticTexture { + neutral_texture([128, 128, 128, 255]) +} + +pub(super) fn overlay_texture() -> VulkanStaticTexture { + neutral_texture([255, 255, 255, 0]) +} + +fn neutral_texture(rgba8: [u8; 4]) -> VulkanStaticTexture { + VulkanStaticTexture { + width: 1, + height: 1, + rgba8: rgba8.to_vec(), + mip_levels: Vec::new(), + } +} + +pub(super) fn material_texture_sources<'a>( + materials: &'a [VulkanStaticMaterial], + detail: &'a VulkanStaticTexture, + overlay: &'a VulkanStaticTexture, +) -> (Vec<&'a VulkanStaticTexture>, Vec<usize>) { + let sources = materials + .iter() + .flat_map(|material| { + [ + &material.texture, + material.detail_texture.as_ref().unwrap_or(detail), + material.overlay_texture.as_ref().unwrap_or(overlay), + material.overlay_detail_texture.as_ref().unwrap_or(detail), + ] + }) + .collect::<Vec<_>>(); + deduplicate_texture_sources(&sources) +} + pub(super) fn preview_rect((width, height): (u32, u32), viewport: [f32; 4]) -> vk::Rect2D { let [left, top, right, bottom] = viewport; let x = (left * width as f32).round() as u32; diff --git a/adapters/fparkan-render-vulkan/src/ffi/scene_append.rs b/adapters/fparkan-render-vulkan/src/ffi/scene_append.rs new file mode 100644 index 00000000..cffcb353 --- /dev/null +++ b/adapters/fparkan-render-vulkan/src/ffi/scene_append.rs @@ -0,0 +1,183 @@ +//! Installing additional batches in the shared world buffers during a mission. + +use super::preview_mesh::{detail_texture, material_texture_sources, overlay_texture}; +use super::{ + create_static_mesh_index_buffer, create_static_mesh_vertex_buffer, create_static_texture_image, + create_swapchain_resources, destroy_allocated_buffer, destroy_allocated_image, + destroy_swapchain_resources, resolve_draw_texture_indices, sorted_world_draw_order, + validate_geometry_upload, vk, RollbackOnDrop, VulkanSmokeRenderer, VulkanSmokeRendererError, + VulkanStaticMaterial, +}; +use crate::ffi::VulkanStaticMesh; + +impl VulkanSmokeRenderer { + /// Extends the world scene while preserving existing draw and material handles. + /// Transient geometry past `scratch_start` is regenerated by the caller's + /// next frame. The separate model viewport can be uploaded again when needed. + pub fn append_scene_mesh( + &mut self, + mesh: &VulkanStaticMesh, + materials: &[VulkanStaticMaterial], + scratch_start: usize, + draw_before: usize, + ) -> Result<(), VulkanSmokeRendererError> { + let invalid = |context| VulkanSmokeRendererError::InvalidStaticMesh { context }; + validate_geometry_upload( + &mesh.vertices, + &mesh.indices, + &mesh.draw_ranges, + &[], + self.vertex_count, + self.index_capacity, + )?; + let (range_start, material_start) = self.preview_mesh.as_ref().map_or( + (self.draw_ranges.len(), self.material_alphas.len()), + |preview| (preview.range_start, preview.material_start), + ); + if draw_before >= range_start + || scratch_start > mesh.indices.len() + || mesh.draw_ranges.len() <= range_start + || self + .scene_append_order + .is_some_and(|(_, anchor)| anchor != draw_before) + { + return Err(invalid("invalid scene extension boundaries")); + } + let added = &mesh.draw_ranges[range_start..]; + let local_materials = resolve_draw_texture_indices(added, materials).map_err(invalid)?; + let detail = detail_texture(); + let overlay = overlay_texture(); + let (sources, new_slots) = material_texture_sources(materials, &detail, &overlay); + let mut states = self.draw_range_base_pipeline_states[..range_start].to_vec(); + states.extend(added.iter().map(|range| range.pipeline_state)); + let (vertex, index, images, slots, resources) = { + let instance = self.instance_ref()?; + let device = self.device_ref()?; + // SAFETY: Replacing resources requires all work on the live device to finish. + unsafe { device.device().device_wait_idle() }.map_err(|result| { + VulkanSmokeRendererError::VulkanOperation { + context: "vkDeviceWaitIdle(world scene extension)", + result, + } + })?; + let vertex = RollbackOnDrop::new( + create_static_mesh_vertex_buffer(instance, device, mesh)?, + |buffer| destroy_allocated_buffer(device, &buffer), + ); + let index = RollbackOnDrop::new( + create_static_mesh_index_buffer(instance, device, mesh)?, + |buffer| destroy_allocated_buffer(device, &buffer), + ); + let mut images = + RollbackOnDrop::new(Vec::with_capacity(sources.len()), |images: Vec<_>| { + for image in images { + destroy_allocated_image(device, &image); + } + }); + let pending = images.value.as_mut().expect("uncommitted scene textures"); + for source in sources { + pending.push(create_static_texture_image( + instance, + device, + self.command_pool, + source, + )?); + } + let (mut views, mut slots) = self.material_image_views(None); + let image_offset = views.len(); + views.extend(pending.iter().map(|image| image.view)); + slots.extend(new_slots.into_iter().map(|slot| image_offset + slot)); + let resources = create_swapchain_resources( + instance, + device, + self.swapchain_ref()?, + self.command_pool, + self.pipeline_cache, + vertex.value.as_ref().expect("uncommitted scene vertices"), + index.value.as_ref().expect("uncommitted scene indices"), + &views, + &slots, + &states, + mesh.draw_ranges + .iter() + .any(|range| range.terrain_reflection), + self.depth_request, + false, + )?; + ( + vertex.commit(), + index.commit(), + images.commit(), + slots, + resources, + ) + }; + + let old_resources = self.swapchain_resources.replace(resources); + let old_vertices = self.vertex_buffer.replace(vertex); + let old_indices = self.index_buffer.replace(index); + let old_preview = self.preview_mesh.take(); + let device = self.device_ref()?; + if let Some(resources) = old_resources { + destroy_swapchain_resources(device, self.command_pool, resources); + } + for buffer in [old_vertices, old_indices].into_iter().flatten() { + destroy_allocated_buffer(device, &buffer); + } + if let Some(preview) = old_preview { + preview.destroy(device); + } + self.model_preview = None; + self.images_in_flight.fill(vk::Fence::null()); + self.last_readback_image_index = None; + self.textures.extend(images); + self.texture_slot_indices = slots; + self.vertex_count = mesh.vertices.len(); + self.index_capacity = mesh.indices.len(); + self.draw_ranges.truncate(range_start); + for (current, original) in self.draw_ranges.iter_mut().zip(&mesh.draw_ranges) { + if current.first_index as usize >= scratch_start { + current.first_index = original.first_index; + current.index_count = 0; + } + } + self.draw_ranges + .extend(added.iter().map(|range| crate::ffi::VulkanStaticDrawRange { + index_count: 0, + ..*range + })); + self.replace_draw_state(range_start, material_start, added, local_materials); + self.replace_material_state(material_start, materials); + self.draw_range_base_pipeline_states = states; + self.scene_append_order + .get_or_insert((range_start, draw_before)); + self.material_specular_dirty = true; + self.draw_order_dirty = true; + Ok(()) + } + + /// Recomputes the stable command-recording order. Without explicit + /// backgrounds, opaque world ranges followed by the sorted transparent + /// world ranges replace the existing world slots. With explicit + /// backgrounds, opaque ranges compact into the world slots and sorted + /// transparent ranges are inserted after the last background; unrelated + /// fixed ranges keep relative order but may shift slots. The transparent + /// queue matches the native descending key comparator. + pub(super) fn rebuild_draw_order(&mut self) { + let mut seed_order = (0..self.draw_ranges.len()).collect::<Vec<_>>(); + if let Some((start, before)) = self.scene_append_order { + let end = self + .preview_mesh + .as_ref() + .map_or(seed_order.len(), |preview| preview.range_start); + seed_order[before..end].rotate_right(end - start); + } + self.draw_order = sorted_world_draw_order( + seed_order, + &self.draw_ranges, + &self.draw_range_world_queue, + &self.draw_range_sort_keys, + &self.world_transparent_background_ranges, + ); + } +} diff --git a/adapters/fparkan-render-vulkan/src/ffi/smoke.rs b/adapters/fparkan-render-vulkan/src/ffi/smoke.rs index 2111e6ff..97deb2f1 100644 --- a/adapters/fparkan-render-vulkan/src/ffi/smoke.rs +++ b/adapters/fparkan-render-vulkan/src/ffi/smoke.rs @@ -25,6 +25,8 @@ use crate::shader::validate_triangle_shaders; #[path = "preview_mesh.rs"] mod preview_mesh; +#[path = "scene_append.rs"] +mod scene_append; use preview_mesh::preview_rect; #[cfg(test)] @@ -417,6 +419,7 @@ impl VulkanSmokeRenderer { draw_order: (0..create_info.mesh.draw_ranges.len()).collect(), draw_range_sort_keys: vec![None; create_info.mesh.draw_ranges.len()], draw_order_dirty: false, + scene_append_order: None, draw_range_world_queue: vec![None; create_info.mesh.draw_ranges.len()], draw_range_diffuse_alpha_overrides: vec![None; create_info.mesh.draw_ranges.len()], world_transparent_background_ranges: Vec::new(), @@ -1850,27 +1853,6 @@ impl VulkanSmokeRenderer { Ok(()) } - /// Recomputes the stable command-recording order. Without explicit - /// backgrounds, opaque world ranges followed by the sorted transparent - /// world ranges replace the existing world slots. With explicit - /// backgrounds, opaque ranges compact into the world slots and sorted - /// transparent ranges are inserted after the last background; unrelated - /// fixed ranges keep relative order but may shift slots. The transparent - /// queue matches the native descending key comparator. - fn rebuild_draw_order(&mut self) { - let range_count = self.draw_ranges.len(); - if self.draw_order.len() != range_count { - self.draw_order = (0..range_count).collect(); - } - self.draw_order = sorted_world_draw_order( - range_count, - &self.draw_ranges, - &self.draw_range_world_queue, - &self.draw_range_sort_keys, - &self.world_transparent_background_ranges, - ); - } - fn record_command_buffer( &mut self, image_index: usize, @@ -2833,16 +2815,15 @@ fn world_material_pipeline_states( /// after the last active background; fixed passes keep their relative order but /// can shift slots. Zero-index-count ranges are inactive and are omitted. fn sorted_world_draw_order( - range_count: usize, + mut next: Vec<usize>, ranges: &[super::VulkanStaticDrawRange], world_queue: &[Option<bool>], keys: &[Option<f32>], transparent_background_ranges: &[usize], ) -> Vec<usize> { - let mut next = (0..range_count).collect::<Vec<_>>(); let mut world_opaque = Vec::new(); let mut world_transparent = Vec::new(); - for range_index in 0..range_count { + for &range_index in &next { if ranges .get(range_index) .is_none_or(|range| range.index_count == 0) @@ -2919,8 +2900,8 @@ fn sorted_world_draw_order( // transparent slots, compact opaque ranges into the world slots, then // insert the sorted transparent queue after the explicit background group. let mut opaque_cursor = 0usize; - let mut next_without_transparency = Vec::with_capacity(range_count); - for slot in 0..range_count { + let mut next_without_transparency = Vec::with_capacity(next.len()); + for slot in next { let active_world = ranges.get(slot).is_some_and(|range| range.index_count != 0); match world_queue.get(slot).copied().flatten() { Some(true) if active_world => {} @@ -3460,7 +3441,7 @@ mod tests { }) .collect::<Vec<_>>(); let order = sorted_world_draw_order( - 5, + (0..5).collect(), &ranges, &[None, Some(true), None, Some(true), Some(false)], &[None, Some(2.0), None, Some(9.0), Some(-3.0)], @@ -3469,7 +3450,7 @@ mod tests { assert_eq!(order, vec![0, 4, 2, 3, 1]); let order = sorted_world_draw_order( - 5, + (0..5).collect(), &ranges, &[Some(true), Some(false), None, None, None], &[Some(2.0), None, None, None, None], @@ -3523,7 +3504,7 @@ mod tests { }, ]; let order = sorted_world_draw_order( - 3, + (0..3).collect(), &ranges, &[None, Some(true), Some(true)], &[None, Some(100.0), Some(1.0)], @@ -3549,7 +3530,13 @@ mod tests { alpha_test_reference: 0, }) .collect::<Vec<_>>(); - let order = sorted_world_draw_order(4, &ranges, &[Some(true); 4], &[Some(1.0); 4], &[]); + let order = sorted_world_draw_order( + (0..4).collect(), + &ranges, + &[Some(true); 4], + &[Some(1.0); 4], + &[], + ); assert_eq!(order, vec![2, 3, 1, 0]); } @@ -3654,7 +3641,7 @@ mod tests { sort_keys[CANOPY_NEAR] = Some(2.0); sort_keys[CANOPY_FAR] = Some(9.0); let order = sorted_world_draw_order( - ranges.len(), + (0..ranges.len()).collect(), &ranges, &world_queue, &sort_keys, @@ -3717,7 +3704,7 @@ mod tests { sort_keys[EMPTY_CONTROL_FX] = Some(30.0); let order = sorted_world_draw_order( - ranges.len(), + (0..ranges.len()).collect(), &ranges, &world_queue, &sort_keys, diff --git a/adapters/fparkan-render-vulkan/src/ffi/smoke_types.rs b/adapters/fparkan-render-vulkan/src/ffi/smoke_types.rs index 33917925..db3ef651 100644 --- a/adapters/fparkan-render-vulkan/src/ffi/smoke_types.rs +++ b/adapters/fparkan-render-vulkan/src/ffi/smoke_types.rs @@ -1606,6 +1606,8 @@ pub struct VulkanSmokeRenderer { pub(super) draw_texture_indices: Vec<usize>, /// Stable range handles used when recording the native pass order. pub(super) draw_order: Vec<usize>, + /// Additional world batches and their insertion point in the original pass order. + pub(super) scene_append_order: Option<(usize, usize)>, /// Native camera-sort values returned by the draw-item camera callback. pub(super) draw_range_sort_keys: Vec<Option<f32>>, /// Whether the stable order must be rebuilt before the next command |
