From 9f5d815e932c9c01265fec2f5a079470d73d6cc8 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 12 Jan 2025 23:35:00 +0100 Subject: zero copy texture write --- shared/src/resources.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'shared/src/resources.rs') diff --git a/shared/src/resources.rs b/shared/src/resources.rs index ba45284..40a9cad 100644 --- a/shared/src/resources.rs +++ b/shared/src/resources.rs @@ -19,6 +19,7 @@ use anyhow::Result; use glam::{Affine3A, Vec2, Vec3A}; use log::warn; use std::{ + borrow::Cow, collections::BTreeMap, io::{Read, Write}, }; @@ -40,7 +41,7 @@ pub struct LightPart { #[derive(Debug, Default, Clone)] pub struct EnvironmentPart { - pub skybox: Option>, + pub skybox: Option>>, pub sun: Option<(Vec3A, Vec3A)>, } @@ -69,15 +70,15 @@ pub struct MeshPart { pub va_transmission: Option>>, pub va_alpha: Option>>, pub va_emission: Option>>, - pub tex_normal: Option>, - pub tex_roughness: Option>, - pub tex_metallic: Option>, - pub tex_albedo: Option>, - pub tex_transmission: Option>, - pub tex_alpha: Option>, - pub tex_emission: Option>, - pub tex_thickness: Option>, - pub tex_occlusion: Option>, + pub tex_normal: Option>>, + pub tex_roughness: Option>>, + pub tex_metallic: Option>>, + pub tex_albedo: Option>>, + pub tex_transmission: Option>>, + pub tex_alpha: Option>>, + pub tex_emission: Option>>, + pub tex_thickness: Option>>, + pub tex_occlusion: Option>>, } #[derive(Debug, Default, Clone)] @@ -97,7 +98,7 @@ pub struct CollisionPart { pub struct PrefabIndex(pub BTreeMap>); #[derive(Debug, Clone)] -pub struct Image(pub Vec); +pub struct Image<'a>(pub Cow<'a, [u8]>); impl ReadWrite for PrefabIndex { fn write(&self, w: &mut dyn Write) -> Result<()> { @@ -331,12 +332,14 @@ fn write_kv(w: &mut dyn Write, key: &[u8], value: &[u8]) -> Result<()> { Ok(()) } -impl ReadWrite for Image { +impl<'a> ReadWrite for Image<'a> { fn write(&self, w: &mut dyn Write) -> Result<()> { self.0.write(w) } - fn read(r: &mut dyn Read) -> Result { - Ok(Self( as ReadWrite>::read(r)?)) + Ok(Self( as ReadWrite>::read(r)?.into())) + } + fn write_alloc<'b>(&'b self) -> Cow<'b, [u8]> { + self.0.write_alloc() } } -- cgit v1.2.3-70-g09d2