khora_lanes/asset_lane/loading/
texture_loader_lane.rs1use super::AssetLoaderLane;
18use anyhow::{Context, Result};
19use khora_core::{
20 math::Extent3D,
21 renderer::api::{
22 resource::{CpuTexture, TextureDimension, TextureUsage},
23 util::{SampleCount, TextureFormat},
24 },
25};
26
27#[derive(Clone)]
29pub struct TextureLoaderLane;
30
31impl AssetLoaderLane<CpuTexture> for TextureLoaderLane {
32 fn load(
33 &self,
34 bytes: &[u8],
35 ) -> Result<CpuTexture, Box<dyn std::error::Error + Send + Sync + 'static>> {
36 let img = image::load_from_memory(bytes).context("Failed to decode image from memory")?;
38
39 let rgba_img = img.to_rgba8();
41 let (width, height) = rgba_img.dimensions();
42
43 Ok(CpuTexture {
44 pixels: rgba_img.into_raw(),
45 size: Extent3D {
46 width,
47 height,
48 depth_or_array_layers: 1,
49 },
50 format: TextureFormat::Rgba8UnormSrgb,
51 mip_level_count: 1,
52 sample_count: SampleCount::X1,
53 dimension: TextureDimension::D2,
54 usage: TextureUsage::COPY_DST | TextureUsage::TEXTURE_BINDING,
55 })
56 }
57}
58
59impl khora_core::lane::Lane for TextureLoaderLane {
60 fn strategy_name(&self) -> &'static str {
61 "TextureLoader"
62 }
63
64 fn lane_kind(&self) -> khora_core::lane::LaneKind {
65 khora_core::lane::LaneKind::Asset
66 }
67
68 fn as_any(&self) -> &dyn std::any::Any {
69 self
70 }
71
72 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
73 self
74 }
75}