From fabb3a1460c5c24161e88225317fb4b336899355 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 17:28:01 +0400 Subject: perf(render): reuse archives across effect material phases --- apps/fparkan-game/src/effect_materials.rs | 99 +++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 apps/fparkan-game/src/effect_materials.rs (limited to 'apps/fparkan-game/src/effect_materials.rs') diff --git a/apps/fparkan-game/src/effect_materials.rs b/apps/fparkan-game/src/effect_materials.rs new file mode 100644 index 00000000..bb3c6b0e --- /dev/null +++ b/apps/fparkan-game/src/effect_materials.rs @@ -0,0 +1,99 @@ +use super::{ + solid_environment_texture, vulkan_texture_from_rgba_mips, PreparedEffectMaterial, + VulkanStaticMaterial, +}; +use fparkan_assets::load_effect_material_phases; +use fparkan_resource::CachedResourceRepository; +use fparkan_vfs::DirectoryVfs; +use std::{ + collections::{hash_map::Entry, HashMap}, + path::Path, + sync::Arc, +}; + +/// Reuses archive reads and material selectors during a scene preparation pass. +pub(super) struct EffectMaterials { + repository: CachedResourceRepository, + materials: HashMap, +} + +impl EffectMaterials { + pub(super) fn new(root: &Path) -> Self { + Self { + repository: CachedResourceRepository::new(Arc::new(DirectoryVfs::new(root))), + materials: HashMap::new(), + } + } + + pub(super) fn prepare( + &mut self, + archive: &str, + name: &str, + materials: &mut Vec, + ) -> Result<&PreparedEffectMaterial, String> { + let entry = match self.materials.entry(format!("{archive}/{name}")) { + Entry::Occupied(entry) => return Ok(entry.into_mut()), + Entry::Vacant(entry) => entry, + }; + let key = entry.key(); + let (document, phases) = load_effect_material_phases(&self.repository, archive, name) + .map_err(|error| format!("resolve FX MAT0 {key}: {error}"))?; + let mut phase_material_indices = Vec::with_capacity(document.phases.len()); + let mut phase_uv_transforms = Vec::with_capacity(document.phases.len()); + let mut phase_native_flag_40 = Vec::with_capacity(document.phases.len()); + let mut phase_directional_rgb = Vec::with_capacity(document.phases.len()); + let mut phase_additive_rgb = Vec::with_capacity(document.phases.len()); + for (mips, phase) in phases { + let texture = vulkan_texture_from_rgba_mips(mips, "FX material")?; + let index = materials.len(); + let material_index = u16::try_from(index).map_err(|_| { + "FX effects exceed the available 16-bit material selector space".to_string() + })?; + materials.push(VulkanStaticMaterial { + material_index, + pipeline_category: phase.pipeline_category, + texture, + directional_rgb: phase.directional_rgb, + additive_rgb: phase.additive_rgb, + uv_transform: phase.page_uv_transform, + detail_texture: Some(solid_environment_texture([128, 128, 128, 255])), + overlay_texture: None, + overlay_detail_texture: None, + // FX descriptor opacity is carried by each frame's vertex alpha. + // Keep MAT0 alpha at one so it does not multiply the opacity a + // second time. + diffuse_alpha: 1.0, + overlay_diffuse_alpha: None, + overlay_directional_rgb: None, + overlay_additive_rgb: None, + specular_rgb: phase.specular_rgb, + specular_power: phase.power, + overlay_specular_rgb: None, + overlay_specular_power: None, + detail_uv_transform: [0.0, 0.0, 1.0, 1.0], + overlay_uv_transform: None, + overlay_detail_uv_transform: None, + unlit: true, + sky_nebula_stars: false, + lightmap_mode: false, + sky_far_depth: false, + screen_tl: false, + }); + phase_material_indices.push(index); + phase_uv_transforms.push(phase.page_uv_transform); + phase_native_flag_40.push(phase.native_flag_40); + phase_directional_rgb.push(phase.directional_rgb); + phase_additive_rgb.push(phase.additive_rgb); + } + let prepared = PreparedEffectMaterial { + initial_pipeline_category: materials[phase_material_indices[0]].pipeline_category, + document, + phase_material_indices, + phase_uv_transforms, + phase_native_flag_40, + phase_directional_rgb, + phase_additive_rgb, + }; + Ok(entry.insert(prepared)) + } +} -- cgit v1.2.3