khora_data/ecs/systems/gpu_mesh_sync.rs
1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! CPU → GPU mesh sync — uploads any newly-spawned `HandleComponent<Mesh>`
16//! to the device cache and tags the entity with `HandleComponent<GpuMesh>`.
17//!
18//! Runs in [`TickPhase::PreExtract`] so it lands before `RenderFlow`
19//! projects the world. Replaces the previous hardcoded `proj.sync_all`
20//! call in the engine tick.
21
22use std::sync::Arc;
23
24use khora_core::lane::OutputDeck;
25use khora_core::renderer::GraphicsDevice;
26use khora_core::Runtime;
27
28use crate::ecs::{DataSystemRegistration, TickPhase, World};
29use crate::ProjectionRegistry;
30
31fn gpu_mesh_sync_system(world: &mut World, runtime: &Runtime, _deck: &mut OutputDeck) {
32 let Some(proj) = runtime.resources.get::<ProjectionRegistry>() else {
33 return;
34 };
35 let Some(device) = runtime.backends.get::<Arc<dyn GraphicsDevice>>() else {
36 return;
37 };
38 proj.sync_all(world, device.as_ref());
39}
40
41inventory::submit! {
42 DataSystemRegistration {
43 name: "gpu_mesh_sync",
44 phase: TickPhase::PreExtract,
45 run: gpu_mesh_sync_system,
46 order_hint: 0,
47 runs_after: &[],
48 }
49}