Skip to main content

khora_data/ecs/systems/
gpu_material_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 material sync — uploads each entity's material (uniforms +
16//! PBR textures + group-2 bind group) to the device cache and tags the
17//! entity with `HandleComponent<GpuMaterial>`.
18//!
19//! Runs in [`TickPhase::PreExtract`], after `gpu_mesh_sync` (so every
20//! rendered entity already has a `HandleComponent<GpuMesh>`) and before
21//! `RenderFlow` projects the world. Decoded CPU textures are read from the
22//! shared `Assets<CpuTexture>` sub-store of the [`AssetStore`](crate::gpu::AssetStore),
23//! populated by the SDK layer that owns the `AssetService` — this system
24//! performs no asset loading itself, keeping the data layer free of any
25//! `khora-io` dependency.
26
27use std::sync::Arc;
28
29use khora_core::lane::OutputDeck;
30use khora_core::renderer::traits::PipelineSystem;
31use khora_core::renderer::GraphicsDevice;
32use khora_core::Runtime;
33
34use crate::ecs::{DataSystemRegistration, TickPhase, World};
35use crate::ProjectionRegistry;
36
37fn gpu_material_sync_system(world: &mut World, runtime: &Runtime, _deck: &mut OutputDeck) {
38    let Some(proj) = runtime.resources.get::<ProjectionRegistry>() else {
39        return;
40    };
41    let Some(device) = runtime.backends.get::<Arc<dyn GraphicsDevice>>() else {
42        return;
43    };
44    // The per-variant group-2 material layout is owned by the PipelineSystem
45    // backend, so each GpuMaterial's bind group shares the exact layout the
46    // lit pipeline binds for that material's variant.
47    let Some(pipeline_system) = runtime.resources.get::<Arc<dyn PipelineSystem>>() else {
48        return;
49    };
50    proj.sync_materials(world, device.as_ref(), pipeline_system.as_ref());
51}
52
53inventory::submit! {
54    DataSystemRegistration {
55        name: "gpu_material_sync",
56        phase: TickPhase::PreExtract,
57        run: gpu_material_sync_system,
58        order_hint: 1,
59        runs_after: &["gpu_mesh_sync"],
60    }
61}