2026-06-22 13:12:27 +04:00
|
|
|
#![forbid(unsafe_code)]
|
2026-06-23 22:32:50 +04:00
|
|
|
#![cfg_attr(
|
|
|
|
|
test,
|
|
|
|
|
allow(
|
|
|
|
|
clippy::cast_possible_truncation,
|
|
|
|
|
clippy::cast_possible_wrap,
|
|
|
|
|
clippy::cast_precision_loss,
|
|
|
|
|
clippy::expect_used,
|
|
|
|
|
clippy::float_cmp,
|
|
|
|
|
clippy::identity_op,
|
|
|
|
|
clippy::too_many_lines,
|
|
|
|
|
clippy::uninlined_format_args,
|
|
|
|
|
clippy::map_unwrap_or,
|
|
|
|
|
clippy::needless_raw_string_hashes,
|
|
|
|
|
clippy::semicolon_if_nothing_returned,
|
|
|
|
|
clippy::type_complexity,
|
|
|
|
|
clippy::panic,
|
|
|
|
|
clippy::unwrap_used
|
|
|
|
|
)
|
|
|
|
|
)]
|
2026-06-22 13:12:27 +04:00
|
|
|
#![allow(clippy::print_stderr, clippy::print_stdout)]
|
2026-07-04 01:50:59 +04:00
|
|
|
//! `FParkan` render-planning composition root.
|
2026-06-22 13:12:27 +04:00
|
|
|
|
2026-06-23 22:32:50 +04:00
|
|
|
use fparkan_assets::PreparedVisual;
|
|
|
|
|
use fparkan_platform::WindowPort;
|
2026-07-18 09:31:32 +04:00
|
|
|
use fparkan_platform_winit::{window_native_handles, WinitWindow, WinitWindowPlan};
|
2026-06-22 13:12:27 +04:00
|
|
|
use fparkan_render::{
|
2026-07-18 06:16:44 +04:00
|
|
|
build_commands, CameraSnapshot, DrawId, GpuMaterialId, GpuMeshId, IndexRange, RenderBackend,
|
|
|
|
|
RenderCommand, RenderCommandList, RenderPhase, RenderProfile, RenderSnapshot,
|
|
|
|
|
RenderSnapshotDraw,
|
2026-06-22 13:12:27 +04:00
|
|
|
};
|
2026-07-18 09:31:32 +04:00
|
|
|
use fparkan_render_vulkan::{
|
|
|
|
|
project_msh_to_static_mesh, VulkanPlanningBackend, VulkanSmokeFrameOutcome,
|
|
|
|
|
VulkanSmokeRenderer, VulkanSmokeRendererCreateInfo, VulkanStaticMesh,
|
|
|
|
|
};
|
2026-06-22 13:12:27 +04:00
|
|
|
use fparkan_runtime::{
|
2026-07-18 09:36:29 +04:00
|
|
|
create, frame, load_mission, load_mission_static_preview, loaded_mission_assets, EngineConfig,
|
|
|
|
|
EngineMode, EngineServices, MissionAssets, MissionObjectDraft, MissionRequest,
|
2026-06-22 13:12:27 +04:00
|
|
|
};
|
|
|
|
|
use fparkan_vfs::DirectoryVfs;
|
2026-07-18 09:24:45 +04:00
|
|
|
#[cfg(test)]
|
|
|
|
|
use fparkan_world::OriginalObjectId;
|
2026-06-22 13:12:27 +04:00
|
|
|
use fparkan_world::WorldSnapshot;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::sync::Arc;
|
2026-07-18 09:31:32 +04:00
|
|
|
use winit::application::ApplicationHandler;
|
|
|
|
|
use winit::dpi::PhysicalSize as WinitPhysicalSize;
|
|
|
|
|
use winit::event::WindowEvent;
|
|
|
|
|
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
|
|
|
|
|
use winit::window::{Window, WindowId};
|
2026-06-22 13:12:27 +04:00
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let raw_args = std::env::args().skip(1).collect::<Vec<_>>();
|
|
|
|
|
let code = match run(&raw_args) {
|
|
|
|
|
Ok(output) => {
|
|
|
|
|
println!("{output}");
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
eprintln!("{err}");
|
|
|
|
|
2
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
std::process::exit(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run(args: &[String]) -> Result<String, String> {
|
|
|
|
|
let args = Args::parse(args)?;
|
|
|
|
|
let services = EngineServices::new(Arc::new(DirectoryVfs::new(&args.root)));
|
|
|
|
|
let mut engine = create(
|
|
|
|
|
EngineConfig {
|
|
|
|
|
mode: EngineMode::Rendered,
|
|
|
|
|
},
|
|
|
|
|
services,
|
|
|
|
|
)
|
|
|
|
|
.map_err(|err| err.to_string())?;
|
2026-07-18 09:36:29 +04:00
|
|
|
let request = MissionRequest {
|
|
|
|
|
key: args.mission.clone(),
|
|
|
|
|
};
|
|
|
|
|
let loaded = if args.backend == RenderBackendMode::StaticVulkan {
|
|
|
|
|
load_mission_static_preview(&mut engine, request)
|
|
|
|
|
} else {
|
|
|
|
|
load_mission(&mut engine, request)
|
|
|
|
|
}
|
2026-06-22 13:12:27 +04:00
|
|
|
.map_err(|err| err.to_string())?;
|
|
|
|
|
|
2026-07-18 09:31:32 +04:00
|
|
|
if args.backend == RenderBackendMode::StaticVulkan {
|
|
|
|
|
let mission_assets = loaded_mission_assets(&engine)
|
|
|
|
|
.ok_or_else(|| "mission assets are unavailable after loading".to_string())?;
|
|
|
|
|
let model = mission_assets.models.first().ok_or_else(|| {
|
|
|
|
|
"mission has no mesh-backed model for the static Vulkan bridge".to_string()
|
|
|
|
|
})?;
|
|
|
|
|
let mesh = project_msh_to_static_mesh(&model.validated)
|
|
|
|
|
.map_err(|err| format!("project mission MSH for Vulkan: {err}"))?;
|
|
|
|
|
return run_static_vulkan_mode(mesh, args.frames, &args.mission, loaded.object_count);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-25 04:18:32 +04:00
|
|
|
let mut backend = VulkanPlanningBackend::new();
|
2026-06-23 22:05:16 +04:00
|
|
|
let _request = WinitWindow::default_render_request();
|
|
|
|
|
let window = WinitWindow::synthetic(1280, 720);
|
|
|
|
|
let _ = window.drawable_size();
|
|
|
|
|
let _ = window.handle();
|
2026-06-22 13:12:27 +04:00
|
|
|
let mut last_draw_count = 0usize;
|
|
|
|
|
let mut last_tick = 0u64;
|
|
|
|
|
let mut last_hash = [0u8; 32];
|
|
|
|
|
for _ in 0..args.frames {
|
|
|
|
|
let result = frame(&mut engine).map_err(|err| err.to_string())?;
|
|
|
|
|
last_tick = result.snapshot.tick.0;
|
|
|
|
|
last_hash = result.snapshot.hash.0;
|
2026-06-23 22:05:16 +04:00
|
|
|
let mission_assets = loaded_mission_assets(&engine);
|
2026-07-18 09:21:58 +04:00
|
|
|
let mission_drafts = fparkan_runtime::loaded_mission_object_drafts(&engine);
|
|
|
|
|
let commands =
|
|
|
|
|
render_snapshot_commands_with_assets(&result.snapshot, mission_assets, mission_drafts)
|
|
|
|
|
.map_err(|err| format!("render snapshot: {err}"))?;
|
2026-06-22 13:12:27 +04:00
|
|
|
last_draw_count = commands
|
|
|
|
|
.commands
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|command| matches!(command, RenderCommand::Draw(_)))
|
|
|
|
|
.count();
|
|
|
|
|
backend
|
|
|
|
|
.execute(&commands)
|
|
|
|
|
.map_err(|err| format!("render backend: {err}"))?;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 22:05:16 +04:00
|
|
|
let capture_report = backend.report();
|
|
|
|
|
|
2026-06-22 13:12:27 +04:00
|
|
|
Ok(format!(
|
2026-07-04 01:50:59 +04:00
|
|
|
"{{\"report_kind\":\"render-planning\",\"backend\":\"vulkan-planning\",\"window\":\"synthetic\",\"mission\":{},\"objects\":{},\"frames\":{},\"tick\":{},\"draws\":{},\"submission_plans\":{},\"last_command_capture_bytes\":{},\"hash\":{}}}",
|
2026-06-22 13:12:27 +04:00
|
|
|
json_string(&args.mission),
|
|
|
|
|
loaded.object_count,
|
|
|
|
|
args.frames,
|
|
|
|
|
last_tick,
|
|
|
|
|
last_draw_count,
|
2026-06-25 06:57:26 +04:00
|
|
|
capture_report.execution.submission_plans,
|
|
|
|
|
capture_report.execution.last_capture_size,
|
2026-06-22 13:12:27 +04:00
|
|
|
json_hash(&last_hash)
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:31:32 +04:00
|
|
|
fn run_static_vulkan_mode(
|
|
|
|
|
mesh: VulkanStaticMesh,
|
|
|
|
|
target_frames: u64,
|
|
|
|
|
mission: &str,
|
|
|
|
|
object_count: usize,
|
|
|
|
|
) -> Result<String, String> {
|
|
|
|
|
let event_loop = EventLoop::new().map_err(|err| format!("winit event loop: {err}"))?;
|
|
|
|
|
event_loop.set_control_flow(ControlFlow::Poll);
|
|
|
|
|
let mut app = StaticVulkanApp::new(mesh, target_frames, mission, object_count);
|
|
|
|
|
if let Err(err) = event_loop.run_app(&mut app) {
|
|
|
|
|
app.error = Some(format!("winit event loop: {err}"));
|
|
|
|
|
}
|
|
|
|
|
app.finish()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct StaticVulkanApp {
|
|
|
|
|
mesh: VulkanStaticMesh,
|
|
|
|
|
target_frames: u64,
|
|
|
|
|
mission: String,
|
|
|
|
|
object_count: usize,
|
|
|
|
|
window_id: Option<WindowId>,
|
|
|
|
|
window: Option<Window>,
|
|
|
|
|
renderer: Option<VulkanSmokeRenderer>,
|
|
|
|
|
frames_presented: u64,
|
|
|
|
|
output: Option<String>,
|
|
|
|
|
error: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl StaticVulkanApp {
|
|
|
|
|
fn new(mesh: VulkanStaticMesh, target_frames: u64, mission: &str, object_count: usize) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
mesh,
|
|
|
|
|
target_frames,
|
|
|
|
|
mission: mission.to_string(),
|
|
|
|
|
object_count,
|
|
|
|
|
window_id: None,
|
|
|
|
|
window: None,
|
|
|
|
|
renderer: None,
|
|
|
|
|
frames_presented: 0,
|
|
|
|
|
output: None,
|
|
|
|
|
error: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn schedule_next_redraw(&self) {
|
|
|
|
|
if let Some(window) = self.window.as_ref() {
|
|
|
|
|
window.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn complete(&mut self, event_loop: &ActiveEventLoop) {
|
|
|
|
|
let Some(renderer) = self.renderer.take() else {
|
|
|
|
|
self.error = Some("native Vulkan renderer was not initialized".to_string());
|
|
|
|
|
event_loop.exit();
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let report = match renderer.shutdown() {
|
|
|
|
|
Ok(report) => report,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
self.error = Some(err.to_string());
|
|
|
|
|
event_loop.exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
self.window.take();
|
|
|
|
|
if report.validation.warning_count != 0 || report.validation.error_count != 0 {
|
|
|
|
|
self.error = Some(format!(
|
|
|
|
|
"native Vulkan validation must stay clean (warnings={}, errors={})",
|
|
|
|
|
report.validation.warning_count, report.validation.error_count
|
|
|
|
|
));
|
|
|
|
|
event_loop.exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.output = Some(format!(
|
|
|
|
|
"{{\"report_kind\":\"rendered-static-mission\",\"backend\":\"vulkan-static\",\"window\":\"native\",\"mission\":{},\"objects\":{},\"frames\":{},\"swapchain\":[{},{}],\"swapchain_images\":{},\"validation_warnings\":{},\"validation_errors\":{},\"readback_bytes\":{},\"readback_hash\":{}}}",
|
|
|
|
|
json_string(&self.mission),
|
|
|
|
|
self.object_count,
|
|
|
|
|
self.frames_presented,
|
|
|
|
|
report.renderer_report.swapchain_extent.0,
|
|
|
|
|
report.renderer_report.swapchain_extent.1,
|
|
|
|
|
report.renderer_report.swapchain_image_count,
|
|
|
|
|
report.validation.warning_count,
|
|
|
|
|
report.validation.error_count,
|
|
|
|
|
report.renderer_report.readback_byte_count,
|
|
|
|
|
report.renderer_report.readback_fnv1a64,
|
|
|
|
|
));
|
|
|
|
|
event_loop.exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn finish(self) -> Result<String, String> {
|
|
|
|
|
self.output.ok_or_else(|| {
|
|
|
|
|
self.error.unwrap_or_else(|| {
|
|
|
|
|
"native Vulkan mode exited before producing a report".to_string()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ApplicationHandler for StaticVulkanApp {
|
|
|
|
|
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
|
|
|
|
if self.window.is_some() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let plan = match WinitWindowPlan::smoke().validate() {
|
|
|
|
|
Ok(plan) => plan,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
self.error = Some(err.to_string());
|
|
|
|
|
event_loop.exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let attributes = Window::default_attributes()
|
|
|
|
|
.with_title("FParkan static mission Vulkan")
|
|
|
|
|
.with_inner_size(WinitPhysicalSize::new(plan.width, plan.height));
|
|
|
|
|
let window = match event_loop.create_window(attributes) {
|
|
|
|
|
Ok(window) => window,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
self.error = Some(format!("winit window: {err}"));
|
|
|
|
|
event_loop.exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let Some(native_handles) = window_native_handles(&window) else {
|
|
|
|
|
self.error = Some("winit window does not expose native handles".to_string());
|
|
|
|
|
event_loop.exit();
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let size = window.inner_size();
|
|
|
|
|
let renderer = match VulkanSmokeRenderer::new(&VulkanSmokeRendererCreateInfo {
|
|
|
|
|
application_name: "fparkan-game".to_string(),
|
|
|
|
|
native_handles,
|
|
|
|
|
drawable_extent: (size.width.max(1), size.height.max(1)),
|
|
|
|
|
render_request: WinitWindow::default_render_request(),
|
|
|
|
|
enable_validation: true,
|
|
|
|
|
mesh: self.mesh.clone(),
|
|
|
|
|
materials: Vec::new(),
|
|
|
|
|
bootstrap_progress: None,
|
|
|
|
|
}) {
|
|
|
|
|
Ok(renderer) => renderer,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
self.error = Some(err.to_string());
|
|
|
|
|
event_loop.exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
self.window_id = Some(window.id());
|
|
|
|
|
self.renderer = Some(renderer);
|
|
|
|
|
self.window = Some(window);
|
|
|
|
|
self.schedule_next_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn window_event(
|
|
|
|
|
&mut self,
|
|
|
|
|
event_loop: &ActiveEventLoop,
|
|
|
|
|
window_id: WindowId,
|
|
|
|
|
event: WindowEvent,
|
|
|
|
|
) {
|
|
|
|
|
if Some(window_id) != self.window_id {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
match event {
|
|
|
|
|
WindowEvent::CloseRequested => {
|
|
|
|
|
self.error = Some("native Vulkan window closed before completion".to_string());
|
|
|
|
|
event_loop.exit();
|
|
|
|
|
}
|
|
|
|
|
WindowEvent::Resized(size) => {
|
|
|
|
|
if let Some(renderer) = self.renderer.as_mut() {
|
|
|
|
|
renderer.request_resize((size.width, size.height));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
WindowEvent::RedrawRequested => {
|
|
|
|
|
let Some(renderer) = self.renderer.as_mut() else {
|
|
|
|
|
self.error = Some("native Vulkan renderer was not initialized".to_string());
|
|
|
|
|
event_loop.exit();
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
match renderer.draw_frame() {
|
|
|
|
|
Ok(VulkanSmokeFrameOutcome::Presented) => {
|
|
|
|
|
self.frames_presented = self.frames_presented.saturating_add(1);
|
|
|
|
|
}
|
|
|
|
|
Ok(
|
|
|
|
|
VulkanSmokeFrameOutcome::Recreated | VulkanSmokeFrameOutcome::ZeroExtent,
|
|
|
|
|
) => {}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
self.error = Some(err.to_string());
|
|
|
|
|
event_loop.exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if self.frames_presented >= self.target_frames {
|
|
|
|
|
self.complete(event_loop);
|
|
|
|
|
} else {
|
|
|
|
|
self.schedule_next_redraw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
|
|
|
|
|
if self.output.is_none() && self.error.is_none() {
|
|
|
|
|
self.schedule_next_redraw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 22:32:50 +04:00
|
|
|
#[cfg(test)]
|
2026-07-18 06:16:44 +04:00
|
|
|
fn render_snapshot_commands(snapshot: &WorldSnapshot) -> Result<RenderCommandList, String> {
|
2026-07-18 09:21:58 +04:00
|
|
|
render_snapshot_commands_with_assets(snapshot, None, None)
|
2026-06-23 22:05:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render_snapshot_commands_with_assets(
|
|
|
|
|
snapshot: &WorldSnapshot,
|
|
|
|
|
mission_assets: Option<&MissionAssets>,
|
2026-07-18 09:21:58 +04:00
|
|
|
mission_drafts: Option<&[MissionObjectDraft]>,
|
2026-07-18 06:16:44 +04:00
|
|
|
) -> Result<RenderCommandList, String> {
|
2026-07-18 09:21:58 +04:00
|
|
|
let render_snapshot = render_snapshot_with_assets(snapshot, mission_assets, mission_drafts);
|
2026-07-18 06:16:44 +04:00
|
|
|
build_commands(&render_snapshot, RenderProfile::default()).map_err(|err| err.to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render_snapshot_with_assets(
|
|
|
|
|
snapshot: &WorldSnapshot,
|
|
|
|
|
mission_assets: Option<&MissionAssets>,
|
2026-07-18 09:21:58 +04:00
|
|
|
mission_drafts: Option<&[MissionObjectDraft]>,
|
2026-07-18 06:16:44 +04:00
|
|
|
) -> RenderSnapshot {
|
|
|
|
|
let mut draws = Vec::with_capacity(snapshot.objects.len());
|
2026-06-22 13:12:27 +04:00
|
|
|
for (index, handle) in snapshot.objects.iter().enumerate() {
|
2026-07-18 09:19:56 +04:00
|
|
|
let visuals = mission_assets.map_or(&[][..], |assets| assets.visuals_for_object(index));
|
|
|
|
|
let visual_count = visuals.len().max(1);
|
|
|
|
|
for visual_index in 0..visual_count {
|
|
|
|
|
let prepared = mission_assets.and_then(|assets| {
|
|
|
|
|
visuals
|
|
|
|
|
.get(visual_index)
|
|
|
|
|
.and_then(|visual_id| assets.visual_by_id(*visual_id))
|
|
|
|
|
});
|
|
|
|
|
let mesh = prepared.map_or_else(
|
2026-06-23 22:05:16 +04:00
|
|
|
|| GpuMeshId(u64::from(handle.slot) + 1),
|
2026-07-18 09:19:56 +04:00
|
|
|
|visual| GpuMeshId(visual.id.raw()),
|
|
|
|
|
);
|
|
|
|
|
let material = prepared
|
|
|
|
|
.and_then(PreparedVisual::primary_material_id)
|
|
|
|
|
.map_or(GpuMaterialId(1), |material_id| {
|
|
|
|
|
GpuMaterialId(material_id.raw())
|
|
|
|
|
});
|
|
|
|
|
let stable_order = if visual_count == 1 {
|
|
|
|
|
u64::from(handle.slot)
|
|
|
|
|
} else {
|
|
|
|
|
u64::from(handle.slot)
|
|
|
|
|
.wrapping_mul(1_000_003)
|
|
|
|
|
.wrapping_add(u64::try_from(visual_index).unwrap_or(u64::MAX))
|
|
|
|
|
};
|
|
|
|
|
let draw_id = snapshot
|
|
|
|
|
.tick
|
|
|
|
|
.0
|
|
|
|
|
.wrapping_mul(1_000_003)
|
|
|
|
|
.wrapping_add(stable_order);
|
|
|
|
|
draws.push(RenderSnapshotDraw {
|
|
|
|
|
id: DrawId(draw_id),
|
|
|
|
|
phase: RenderPhase::Opaque,
|
2026-07-18 09:24:45 +04:00
|
|
|
object_id: mission_drafts
|
|
|
|
|
.and_then(|drafts| drafts.get(index))
|
|
|
|
|
.and_then(|draft| draft.original_id),
|
2026-07-18 09:19:56 +04:00
|
|
|
mesh,
|
|
|
|
|
material_slots: vec![material],
|
|
|
|
|
material_index: 0,
|
|
|
|
|
pipeline_state: fparkan_render::LegacyPipelineState::default(),
|
2026-07-18 09:21:58 +04:00
|
|
|
transform: mission_drafts
|
|
|
|
|
.and_then(|drafts| drafts.get(index))
|
|
|
|
|
.map_or_else(
|
|
|
|
|
|| identity_transform(index_to_f32(index)),
|
|
|
|
|
mission_position_scale_transform,
|
|
|
|
|
),
|
2026-07-18 09:19:56 +04:00
|
|
|
range: IndexRange { start: 0, count: 3 },
|
|
|
|
|
stable_order,
|
2026-06-23 22:32:50 +04:00
|
|
|
});
|
2026-07-18 09:19:56 +04:00
|
|
|
}
|
2026-07-18 06:16:44 +04:00
|
|
|
}
|
|
|
|
|
RenderSnapshot {
|
|
|
|
|
camera: CameraSnapshot::default(),
|
|
|
|
|
draws,
|
2026-06-22 13:12:27 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:21:58 +04:00
|
|
|
fn mission_position_scale_transform(draft: &MissionObjectDraft) -> [f32; 16] {
|
|
|
|
|
[
|
|
|
|
|
draft.scale[0],
|
|
|
|
|
0.0,
|
|
|
|
|
0.0,
|
|
|
|
|
draft.position[0],
|
|
|
|
|
0.0,
|
|
|
|
|
draft.scale[1],
|
|
|
|
|
0.0,
|
|
|
|
|
draft.position[1],
|
|
|
|
|
0.0,
|
|
|
|
|
0.0,
|
|
|
|
|
draft.scale[2],
|
|
|
|
|
draft.position[2],
|
|
|
|
|
0.0,
|
|
|
|
|
0.0,
|
|
|
|
|
0.0,
|
|
|
|
|
1.0,
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 13:12:27 +04:00
|
|
|
fn identity_transform(x: f32) -> [f32; 16] {
|
|
|
|
|
[
|
|
|
|
|
1.0, 0.0, 0.0, x, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn index_to_f32(index: usize) -> f32 {
|
|
|
|
|
u16::try_from(index).map_or(f32::from(u16::MAX), f32::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
|
struct Args {
|
|
|
|
|
root: PathBuf,
|
|
|
|
|
mission: String,
|
|
|
|
|
frames: u64,
|
2026-07-18 09:31:32 +04:00
|
|
|
backend: RenderBackendMode,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
|
enum RenderBackendMode {
|
|
|
|
|
Planning,
|
|
|
|
|
StaticVulkan,
|
2026-06-22 13:12:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Args {
|
|
|
|
|
fn parse(args: &[String]) -> Result<Self, String> {
|
|
|
|
|
let mut root = None;
|
|
|
|
|
let mut mission = None;
|
|
|
|
|
let mut frames = 1;
|
2026-07-18 09:31:32 +04:00
|
|
|
let mut backend = RenderBackendMode::Planning;
|
2026-06-22 13:12:27 +04:00
|
|
|
let mut iter = args.iter();
|
|
|
|
|
while let Some(arg) = iter.next() {
|
|
|
|
|
match arg.as_str() {
|
|
|
|
|
"--root" => {
|
|
|
|
|
root = Some(
|
|
|
|
|
iter.next()
|
|
|
|
|
.map(PathBuf::from)
|
|
|
|
|
.ok_or_else(|| "--root requires a path".to_string())?,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
"--mission" => {
|
|
|
|
|
mission = Some(
|
|
|
|
|
iter.next()
|
|
|
|
|
.cloned()
|
|
|
|
|
.ok_or_else(|| "--mission requires a path".to_string())?,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
"--frames" => {
|
|
|
|
|
frames = iter
|
|
|
|
|
.next()
|
|
|
|
|
.ok_or_else(|| "--frames requires a value".to_string())?
|
|
|
|
|
.parse()
|
|
|
|
|
.map_err(|_| "--frames must be an integer".to_string())?;
|
|
|
|
|
}
|
2026-07-18 09:31:32 +04:00
|
|
|
"--backend" => {
|
|
|
|
|
backend = match iter
|
|
|
|
|
.next()
|
|
|
|
|
.ok_or_else(|| "--backend requires a value".to_string())?
|
|
|
|
|
.as_str()
|
|
|
|
|
{
|
|
|
|
|
"planning" => RenderBackendMode::Planning,
|
|
|
|
|
"static-vulkan" => RenderBackendMode::StaticVulkan,
|
|
|
|
|
_ => return Err("--backend must be planning or static-vulkan".to_string()),
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-06-22 13:12:27 +04:00
|
|
|
_ => return Err(usage()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let root = root.ok_or_else(|| "missing --root".to_string())?;
|
|
|
|
|
let mission = mission.ok_or_else(|| "missing --mission".to_string())?;
|
|
|
|
|
if frames == 0 {
|
|
|
|
|
return Err("--frames must be greater than zero".to_string());
|
|
|
|
|
}
|
|
|
|
|
Ok(Self {
|
|
|
|
|
root,
|
|
|
|
|
mission,
|
|
|
|
|
frames,
|
2026-07-18 09:31:32 +04:00
|
|
|
backend,
|
2026-06-22 13:12:27 +04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn json_string(value: &str) -> String {
|
|
|
|
|
let mut out = String::with_capacity(value.len() + 2);
|
|
|
|
|
out.push('"');
|
|
|
|
|
for ch in value.chars() {
|
|
|
|
|
match ch {
|
|
|
|
|
'"' => out.push_str("\\\""),
|
|
|
|
|
'\\' => out.push_str("\\\\"),
|
|
|
|
|
'\n' => out.push_str("\\n"),
|
|
|
|
|
'\r' => out.push_str("\\r"),
|
|
|
|
|
'\t' => out.push_str("\\t"),
|
|
|
|
|
c if c.is_control() => {
|
|
|
|
|
use std::fmt::Write as _;
|
2026-06-23 22:05:16 +04:00
|
|
|
let _ = write!(out, "\\u{:04x}", c as u32);
|
2026-06-22 13:12:27 +04:00
|
|
|
}
|
|
|
|
|
c => out.push(c),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out.push('"');
|
|
|
|
|
out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn json_hash(hash: &[u8; 32]) -> String {
|
|
|
|
|
let mut out = String::from("\"");
|
|
|
|
|
for byte in hash {
|
|
|
|
|
use std::fmt::Write as _;
|
|
|
|
|
let _ = write!(out, "{byte:02x}");
|
|
|
|
|
}
|
|
|
|
|
out.push('"');
|
|
|
|
|
out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn usage() -> String {
|
2026-07-18 09:31:32 +04:00
|
|
|
"usage: fparkan-game --root <path> --mission <path> [--frames <n>] [--backend <planning|static-vulkan>]".to_string()
|
2026-06-22 13:12:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2026-07-18 09:19:56 +04:00
|
|
|
use fparkan_assets::AssetId;
|
2026-06-22 13:12:27 +04:00
|
|
|
use fparkan_world::{ObjectHandle, StateHash, Tick};
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
|
|
fn strings(values: &[&str]) -> Vec<String> {
|
|
|
|
|
values.iter().map(|value| (*value).to_string()).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn parses_required_args() {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
Args::parse(&strings(&[
|
|
|
|
|
"--root",
|
|
|
|
|
"testdata/IS",
|
|
|
|
|
"--mission",
|
|
|
|
|
"MISSIONS/Autodemo.00/data.tma",
|
|
|
|
|
"--frames",
|
|
|
|
|
"3",
|
|
|
|
|
])),
|
|
|
|
|
Ok(Args {
|
|
|
|
|
root: PathBuf::from("testdata/IS"),
|
|
|
|
|
mission: "MISSIONS/Autodemo.00/data.tma".to_string(),
|
|
|
|
|
frames: 3,
|
2026-07-18 09:31:32 +04:00
|
|
|
backend: RenderBackendMode::Planning,
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn parses_static_vulkan_backend() {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
Args::parse(&strings(&[
|
|
|
|
|
"--root",
|
|
|
|
|
"testdata/IS",
|
|
|
|
|
"--mission",
|
|
|
|
|
"MISSIONS/Autodemo.00/data.tma",
|
|
|
|
|
"--backend",
|
|
|
|
|
"static-vulkan",
|
|
|
|
|
])),
|
|
|
|
|
Ok(Args {
|
|
|
|
|
root: PathBuf::from("testdata/IS"),
|
|
|
|
|
mission: "MISSIONS/Autodemo.00/data.tma".to_string(),
|
|
|
|
|
frames: 1,
|
|
|
|
|
backend: RenderBackendMode::StaticVulkan,
|
2026-06-22 13:12:27 +04:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn render_commands_follow_snapshot_order() -> Result<(), String> {
|
|
|
|
|
let snapshot = WorldSnapshot {
|
|
|
|
|
tick: Tick(7),
|
|
|
|
|
objects: vec![
|
|
|
|
|
ObjectHandle {
|
|
|
|
|
generation: 1,
|
|
|
|
|
slot: 2,
|
|
|
|
|
},
|
|
|
|
|
ObjectHandle {
|
|
|
|
|
generation: 1,
|
|
|
|
|
slot: 5,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
events: Vec::new(),
|
|
|
|
|
hash: StateHash([0; 32]),
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-18 06:16:44 +04:00
|
|
|
let commands = render_snapshot_commands(&snapshot)?;
|
2026-06-22 13:12:27 +04:00
|
|
|
|
|
|
|
|
assert_eq!(commands.commands.len(), 4);
|
2026-07-18 06:16:44 +04:00
|
|
|
assert!(matches!(
|
|
|
|
|
commands.commands[0],
|
|
|
|
|
fparkan_render::RenderCommand::BeginFrame
|
|
|
|
|
));
|
|
|
|
|
assert!(matches!(
|
|
|
|
|
commands.commands[3],
|
|
|
|
|
fparkan_render::RenderCommand::EndFrame
|
|
|
|
|
));
|
|
|
|
|
let fparkan_render::RenderCommand::Draw(first) = &commands.commands[1] else {
|
2026-06-22 13:12:27 +04:00
|
|
|
return Err("expected draw".to_string());
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(first.mesh, GpuMeshId(3));
|
|
|
|
|
assert_eq!(first.stable_order, 2);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:19:56 +04:00
|
|
|
#[test]
|
|
|
|
|
fn render_snapshot_retains_every_prepared_visual_for_an_object() -> Result<(), String> {
|
|
|
|
|
let snapshot = WorldSnapshot {
|
|
|
|
|
tick: Tick(3),
|
|
|
|
|
objects: vec![ObjectHandle {
|
|
|
|
|
generation: 1,
|
|
|
|
|
slot: 7,
|
|
|
|
|
}],
|
|
|
|
|
events: Vec::new(),
|
|
|
|
|
hash: StateHash([0; 32]),
|
|
|
|
|
};
|
|
|
|
|
let first = prepared_visual(101, 201);
|
|
|
|
|
let second = prepared_visual(102, 202);
|
|
|
|
|
let assets = MissionAssets {
|
|
|
|
|
visuals: vec![first, second],
|
|
|
|
|
object_visuals: vec![vec![AssetId::new(101), AssetId::new(102)]],
|
|
|
|
|
..MissionAssets::default()
|
|
|
|
|
};
|
2026-07-18 09:21:58 +04:00
|
|
|
let commands = render_snapshot_commands_with_assets(&snapshot, Some(&assets), None)?;
|
2026-07-18 09:19:56 +04:00
|
|
|
let draws: Vec<_> = commands
|
|
|
|
|
.commands
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|command| match command {
|
|
|
|
|
RenderCommand::Draw(draw) => Some(draw),
|
|
|
|
|
_ => None,
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
assert_eq!(draws.len(), 2);
|
|
|
|
|
assert_eq!(draws[0].mesh, GpuMeshId(101));
|
|
|
|
|
assert_eq!(draws[1].mesh, GpuMeshId(102));
|
|
|
|
|
assert_eq!(draws[0].material, GpuMaterialId(201));
|
|
|
|
|
assert_eq!(draws[1].material, GpuMaterialId(202));
|
|
|
|
|
assert_ne!(draws[0].stable_order, draws[1].stable_order);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:21:58 +04:00
|
|
|
#[test]
|
|
|
|
|
fn render_snapshot_uses_mission_position_and_scale_without_interpreting_orientation() {
|
|
|
|
|
let draft = MissionObjectDraft {
|
|
|
|
|
original_id: None,
|
|
|
|
|
resource_name_raw: Vec::new(),
|
|
|
|
|
identity_or_clan_raw: 0,
|
|
|
|
|
position: [10.0, 20.0, 30.0],
|
|
|
|
|
orientation_raw: [1.0, 2.0, 3.0],
|
|
|
|
|
scale: [2.0, 3.0, 4.0],
|
|
|
|
|
visual_ids: Vec::new(),
|
|
|
|
|
properties: Vec::new(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(
|
|
|
|
|
mission_position_scale_transform(&draft),
|
|
|
|
|
[2.0, 0.0, 0.0, 10.0, 0.0, 3.0, 0.0, 20.0, 0.0, 0.0, 4.0, 30.0, 0.0, 0.0, 0.0, 1.0,]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:24:45 +04:00
|
|
|
#[test]
|
|
|
|
|
fn render_snapshot_preserves_mission_original_object_id() -> Result<(), String> {
|
|
|
|
|
let snapshot = WorldSnapshot {
|
|
|
|
|
tick: Tick(1),
|
|
|
|
|
objects: vec![ObjectHandle {
|
|
|
|
|
generation: 1,
|
|
|
|
|
slot: 0,
|
|
|
|
|
}],
|
|
|
|
|
events: Vec::new(),
|
|
|
|
|
hash: StateHash([0; 32]),
|
|
|
|
|
};
|
|
|
|
|
let drafts = vec![MissionObjectDraft {
|
|
|
|
|
original_id: Some(OriginalObjectId(42)),
|
|
|
|
|
resource_name_raw: Vec::new(),
|
|
|
|
|
identity_or_clan_raw: 0,
|
|
|
|
|
position: [0.0; 3],
|
|
|
|
|
orientation_raw: [0.0; 3],
|
|
|
|
|
scale: [1.0; 3],
|
|
|
|
|
visual_ids: Vec::new(),
|
|
|
|
|
properties: Vec::new(),
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
let commands = render_snapshot_commands_with_assets(&snapshot, None, Some(&drafts))?;
|
|
|
|
|
let RenderCommand::Draw(draw) = &commands.commands[1] else {
|
|
|
|
|
return Err("expected draw".to_string());
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(draw.object_id, Some(OriginalObjectId(42)));
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:19:56 +04:00
|
|
|
fn prepared_visual(id: u64, material: u64) -> PreparedVisual {
|
|
|
|
|
PreparedVisual {
|
|
|
|
|
id: AssetId::new(id),
|
|
|
|
|
mesh: None,
|
|
|
|
|
model_id: None,
|
|
|
|
|
wear_id: None,
|
|
|
|
|
model_nodes: 0,
|
|
|
|
|
model_slots: 0,
|
|
|
|
|
model_batches: 0,
|
|
|
|
|
material_count: 1,
|
|
|
|
|
material_ids: vec![AssetId::new(material)],
|
|
|
|
|
texture_ids: Vec::new(),
|
|
|
|
|
lightmap_ids: Vec::new(),
|
|
|
|
|
texture_count: 0,
|
|
|
|
|
lightmap_count: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 13:12:27 +04:00
|
|
|
#[test]
|
2026-06-22 15:55:37 +04:00
|
|
|
#[ignore = "requires licensed corpus"]
|
2026-06-22 13:12:27 +04:00
|
|
|
fn selected_is_and_is2_missions_produce_approved_render_captures() {
|
|
|
|
|
for case in [
|
|
|
|
|
RenderCase {
|
|
|
|
|
root: "IS",
|
|
|
|
|
mission: "MISSIONS/CAMPAIGN/CAMPAIGN.00/Mission.01/data.tma",
|
2026-07-18 07:09:24 +04:00
|
|
|
expected: "{\"report_kind\":\"render-planning\",\"backend\":\"vulkan-planning\",\"window\":\"synthetic\",\"mission\":\"MISSIONS/CAMPAIGN/CAMPAIGN.00/Mission.01/data.tma\",\"objects\":33,\"frames\":1,\"tick\":1,\"draws\":33,\"submission_plans\":1,\"last_command_capture_bytes\":2008,\"hash\":\"ca17cc76e55c45e83c1c9c1c088e84bf1a698be91a7730943210fe27596af841\"}",
|
2026-06-22 13:12:27 +04:00
|
|
|
},
|
|
|
|
|
RenderCase {
|
|
|
|
|
root: "IS2",
|
|
|
|
|
mission: "MISSIONS/Campaign/CAMPAIGN.00/Mission.02/data.tma",
|
2026-07-18 07:09:24 +04:00
|
|
|
expected: "{\"report_kind\":\"render-planning\",\"backend\":\"vulkan-planning\",\"window\":\"synthetic\",\"mission\":\"MISSIONS/Campaign/CAMPAIGN.00/Mission.02/data.tma\",\"objects\":10,\"frames\":1,\"tick\":1,\"draws\":10,\"submission_plans\":1,\"last_command_capture_bytes\":605,\"hash\":\"5d720b3ab690076a398a79a404850bbeaee2e33811b5bb570ec8a96d4a7a2fc4\"}",
|
2026-06-22 13:12:27 +04:00
|
|
|
},
|
|
|
|
|
] {
|
|
|
|
|
assert_eq!(
|
2026-06-22 17:29:33 +04:00
|
|
|
run(&render_args(&licensed_root(case.root), case.mission)),
|
2026-06-22 13:12:27 +04:00
|
|
|
Ok(case.expected.to_string())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn json_hash_is_hex() {
|
|
|
|
|
let mut hash = [0; 32];
|
|
|
|
|
hash[0] = 0xab;
|
|
|
|
|
hash[31] = 0xcd;
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
json_hash(&hash),
|
|
|
|
|
"\"ab000000000000000000000000000000000000000000000000000000000000cd\""
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
|
struct RenderCase {
|
|
|
|
|
root: &'static str,
|
|
|
|
|
mission: &'static str,
|
|
|
|
|
expected: &'static str,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render_args(root: &Path, mission: &str) -> Vec<String> {
|
|
|
|
|
vec![
|
|
|
|
|
"--root".to_string(),
|
|
|
|
|
root.to_str().expect("utf8 root").to_string(),
|
|
|
|
|
"--mission".to_string(),
|
|
|
|
|
mission.to_string(),
|
|
|
|
|
"--frames".to_string(),
|
|
|
|
|
"1".to_string(),
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 17:29:33 +04:00
|
|
|
fn licensed_root(name: &str) -> PathBuf {
|
|
|
|
|
let variable = match name {
|
|
|
|
|
"IS" => "FPARKAN_CORPUS_PART1_ROOT",
|
|
|
|
|
"IS2" => "FPARKAN_CORPUS_PART2_ROOT",
|
|
|
|
|
_ => panic!("unknown licensed corpus part: {name}"),
|
|
|
|
|
};
|
|
|
|
|
let root = std::env::var_os(variable)
|
|
|
|
|
.map(PathBuf::from)
|
|
|
|
|
.unwrap_or_else(|| panic!("{variable} is required for licensed corpus tests"));
|
|
|
|
|
assert!(
|
|
|
|
|
root.is_dir(),
|
|
|
|
|
"licensed corpus root is missing: {}",
|
|
|
|
|
root.display()
|
|
|
|
|
);
|
|
|
|
|
root
|
2026-06-22 13:12:27 +04:00
|
|
|
}
|
|
|
|
|
}
|