From 68952ded60a2a728daabe4271084572e2ee37328 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Mon, 11 Dec 2023 21:48:29 -0500 Subject: [PATCH] Fix handle allocation --- rend3/src/managers/skeleton.rs | 1 - rend3/src/renderer/mod.rs | 18 ++++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/rend3/src/managers/skeleton.rs b/rend3/src/managers/skeleton.rs index c4a0238e..a98ab0cf 100644 --- a/rend3/src/managers/skeleton.rs +++ b/rend3/src/managers/skeleton.rs @@ -139,7 +139,6 @@ impl SkeletonManager { }) } - #[allow(clippy::too_many_arguments)] pub fn add(&mut self, handle: &SkeletonHandle, internal: InternalSkeleton) { self.global_joint_count += internal.joint_matrices.len(); diff --git a/rend3/src/renderer/mod.rs b/rend3/src/renderer/mod.rs index c5eb7c6e..a89af077 100644 --- a/rend3/src/renderer/mod.rs +++ b/rend3/src/renderer/mod.rs @@ -140,11 +140,12 @@ impl Renderer { /// the mesh alive. #[track_caller] pub fn add_mesh(self: &Arc, mesh: Mesh) -> Result { - let handle = self.resource_handle_allocators.mesh.allocate(self); - let mut encoder = self.device.create_command_encoder(&CommandEncoderDescriptor::default()); let internal_mesh = self.mesh_manager.add(&self.device, &self.queue, &mut encoder, mesh)?; + // Handle allocation must be done _after_ any validation to prevent deletion of a handle that never gets fully added. + let handle = self.resource_handle_allocators.mesh.allocate(self); + self.instructions.push( InstructionKind::AddMesh { handle: handle.clone(), @@ -165,11 +166,12 @@ impl Renderer { /// references alive. #[track_caller] pub fn add_skeleton(self: &Arc, skeleton: Skeleton) -> Result { - let handle = self.resource_handle_allocators.skeleton.allocate(self); - let mut encoder = self.device.create_command_encoder(&CommandEncoderDescriptor::default()); let internal = SkeletonManager::validate_skeleton(&self.device, &mut encoder, &self.mesh_manager, skeleton)?; + // Handle allocation must be done _after_ any validation to prevent deletion of a handle that never gets fully added. + let handle = self.resource_handle_allocators.skeleton.allocate(self); + self.instructions.push( InstructionKind::AddSkeleton { handle: handle.clone(), @@ -189,9 +191,11 @@ impl Renderer { pub fn add_texture_2d(self: &Arc, texture: Texture) -> Result { profiling::scope!("Add Texture 2D"); - let handle = self.resource_handle_allocators.d2_texture.allocate(self); let (cmd_buf, internal_texture) = TextureManager::::add(self, texture, false)?; + // Handle allocation must be done _after_ any validation to prevent deletion of a handle that never gets fully added. + let handle = self.resource_handle_allocators.d2_texture.allocate(self); + self.instructions.push( InstructionKind::AddTexture2D { handle: handle.clone(), @@ -234,9 +238,11 @@ impl Renderer { pub fn add_texture_cube(self: &Arc, texture: Texture) -> Result { profiling::scope!("Add Texture Cube"); - let handle = self.resource_handle_allocators.d2c_texture.allocate(self); let (cmd_buf, internal_texture) = TextureManager::::add(self, texture, true)?; + // Handle allocation must be done _after_ any validation to prevent deletion of a handle that never gets fully added. + let handle = self.resource_handle_allocators.d2c_texture.allocate(self); + self.instructions.push( InstructionKind::AddTextureCube { handle: handle.clone(),