Skip to content

Commit

Permalink
Use Pin for keep-alive variables
Browse files Browse the repository at this point in the history
  • Loading branch information
KYovchevski committed Apr 23, 2024
1 parent d784ef6 commit c1522a9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
27 changes: 14 additions & 13 deletions src/ispc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use ispc_rt::ispc_module;

use std::rc::Rc;
use std::{pin::Pin, rc::Rc};

use crate::CachedWeight;
pub use downsample_ispc::*;
Expand All @@ -25,10 +25,10 @@ pub(crate) struct WeightCollection {
ispc_representation: downsample_ispc::WeightCollection,

// Keep these because we need to keep them in memory
_starts: Vec<u32>,
_weight_counts: Vec<u32>,
_weights: Vec<Rc<Vec<f32>>>,
_weights_ptrs: Vec<*const f32>,
_starts: Pin<Vec<u32>>,
_weight_counts: Pin<Vec<u32>>,
_weights: Pin<Vec<Rc<Vec<f32>>>>,
_weights_ptrs: Pin<Vec<*const f32>>,
}

impl WeightCollection {
Expand All @@ -51,10 +51,10 @@ impl WeightCollection {
weight_counts: counts.as_ptr(),
values: weights_ptrs.as_ptr(),
},
_starts: starts,
_weight_counts: counts,
_weights: weights,
_weights_ptrs: weights_ptrs,
_starts: Pin::new(starts),
_weight_counts: Pin::new(counts),
_weights: Pin::new(weights),
_weights_ptrs: Pin::new(weights_ptrs),
})
}

Expand All @@ -66,8 +66,9 @@ impl WeightCollection {
pub(crate) struct Weights {
ispc_representation: SampleWeights,

_horizontal_weights: Rc<WeightCollection>,
_vertical_weights: Rc<WeightCollection>,
// Need to be kept alive because the ispc_representation holds pointers to them
_horizontal_weights: Pin<Rc<WeightCollection>>,
_vertical_weights: Pin<Rc<WeightCollection>>,
}

impl Weights {
Expand All @@ -80,8 +81,8 @@ impl Weights {
vertical_weights: vertical_weights.ispc_representation(),
horizontal_weights: horizontal_weights.ispc_representation(),
},
_vertical_weights: vertical_weights,
_horizontal_weights: horizontal_weights,
_vertical_weights: Pin::new(vertical_weights),
_horizontal_weights: Pin::new(horizontal_weights),
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl NormalMapFormat {
}

impl From<NormalMapFormat> for ispc::NormalMapFormat {
fn from(value: NormalMapFormat) -> i32 {
fn from(value: NormalMapFormat) -> ispc::NormalMapFormat {
match value {
NormalMapFormat::R8g8b8 => ispc::NormalMapFormat_R8g8b8,
NormalMapFormat::R8g8TangentSpaceReconstructedZ => {
Expand Down Expand Up @@ -257,11 +257,6 @@ fn precompute_lanczos_weights(
};

ispc::Weights::new(width_weights, height_weights)

// ispc::SampleWeights {
// vertical_weights: height_weights.ispc_representation(),
// horizontal_weights: width_weights.ispc_representation(),
// }
}

/// Version of [downsample] which allows for a custom filter scale, thus trading between speed and final image quality.
Expand Down

0 comments on commit c1522a9

Please sign in to comment.