Skip to content

Commit

Permalink
Started to implement Vulkan correlation.
Browse files Browse the repository at this point in the history
For now, only device enumeration works.
macOS (Metal) version is just a basic stub.
  • Loading branch information
zlogic committed Jan 25, 2024
1 parent 889e762 commit 9d10cb7
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 85 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ roots = { version = "0.0.8", default-features = false }
kamadak-exif = { version = "0.5", default-features = false }

[target.'cfg(not(target_os = "macos"))'.dependencies]
ash = { version = "0.37", default-features = false, features = ["loaded"], optional = true }
ash = { version = "0.37", default-features = true, features = ["loaded"] }

[target.'cfg(target_os = "macos")'.dependencies]
metal = { version = "0.27", default-features = false, optional = true }
metal = { version = "0.27", default-features = false }

[profile.release]
strip = true
Expand Down
93 changes: 10 additions & 83 deletions src/correlation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#[cfg(feature = "gpu")]
mod gpu;
#[cfg(target_os = "macos")]
mod metalcorrelation;
#[cfg(not(target_os = "macos"))]
mod vkcorrelation;

#[cfg(target_os = "macos")]
use metalcorrelation as gpu;
#[cfg(not(target_os = "macos"))]
use vkcorrelation as gpu;

use crate::data::{Grid, Point2D};
use nalgebra::{Matrix3, Vector3};
Expand Down Expand Up @@ -137,7 +144,7 @@ impl PointCorrelations {
match gpu::GpuContext::new(
(img1_dimensions.0 as usize, img1_dimensions.1 as usize),
(img2_dimensions.0 as usize, img2_dimensions.1 as usize),
&projection_mode,
projection_mode,
fundamental_matrix,
low_power,
) {
Expand Down Expand Up @@ -234,10 +241,6 @@ impl PointCorrelations {
dir: CorrelationDirection,
) -> Result<(), Box<dyn error::Error>> {
if let Some(gpu_context) = &mut self.gpu_context {
let dir = match dir {
CorrelationDirection::Forward => gpu::CorrelationDirection::Forward,
CorrelationDirection::Reverse => gpu::CorrelationDirection::Reverse,
};
return gpu_context.correlate_images(
img1,
img2,
Expand Down Expand Up @@ -542,10 +545,6 @@ impl PointCorrelations {

fn cross_check_filter(&mut self, scale: f32, dir: CorrelationDirection) {
if let Some(gpu_context) = &mut self.gpu_context {
let dir = match dir {
CorrelationDirection::Forward => gpu::CorrelationDirection::Forward,
CorrelationDirection::Reverse => gpu::CorrelationDirection::Reverse,
};
gpu_context.cross_check_filter(scale, dir);
return;
};
Expand Down Expand Up @@ -723,75 +722,3 @@ pub fn compute_point_data<const KS: usize, const KPC: usize>(

Some(result)
}

#[cfg(not(feature = "gpu"))]
mod gpu {
use nalgebra::Matrix3;
use std::{error, fmt};

use crate::data::Grid;

use super::ProjectionMode;

pub enum CorrelationDirection {
Forward,
Reverse,
}

pub struct GpuContext {}

impl GpuContext {
pub fn new(
_: (usize, usize),
_: (usize, usize),
_: &ProjectionMode,
_: Matrix3<f64>,
_: bool,
) -> Result<GpuContext, Box<dyn error::Error>> {
Err(GpuError::new("Compiled without GPU support").into())
}

pub fn get_device_name(&self) -> &'static str {
"undefined"
}

pub fn cross_check_filter(&mut self, _: f32, _: CorrelationDirection) {}

pub fn complete_process(
&mut self,
) -> Result<Grid<Option<super::Match>>, Box<dyn error::Error>> {
Err(GpuError::new("Compiled without GPU support").into())
}

pub fn correlate_images<PL: super::ProgressListener>(
&mut self,
_: &Grid<u8>,
_: &Grid<u8>,
_: f32,
_: bool,
_: Option<&PL>,
_: CorrelationDirection,
) -> Result<(), Box<dyn error::Error>> {
Err(GpuError::new("Compiled without GPU support").into())
}
}

#[derive(Debug)]
pub struct GpuError {
msg: &'static str,
}

impl GpuError {
fn new(msg: &'static str) -> GpuError {
GpuError { msg }
}
}

impl std::error::Error for GpuError {}

impl fmt::Display for GpuError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
}
68 changes: 68 additions & 0 deletions src/correlation/metalcorrelation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use nalgebra::Matrix3;
use std::{error, fmt};

use crate::data::Grid;

use super::ProjectionMode;

pub enum CorrelationDirection {
Forward,
Reverse,
}

pub struct GpuContext {}

impl GpuContext {
pub fn new(
_: (usize, usize),
_: (usize, usize),
_: ProjectionMode,
_: Matrix3<f64>,
_: bool,
) -> Result<GpuContext, Box<dyn error::Error>> {
Err(GpuError::new("Compiled without GPU support").into())
}

pub fn get_device_name(&self) -> &'static str {
"undefined"
}

pub fn cross_check_filter(&mut self, _: f32, _: CorrelationDirection) {}

pub fn complete_process(
&mut self,
) -> Result<Grid<Option<super::Match>>, Box<dyn error::Error>> {
Err(GpuError::new("Compiled without GPU support").into())
}

pub fn correlate_images<PL: super::ProgressListener>(
&mut self,
_: &Grid<u8>,
_: &Grid<u8>,
_: f32,
_: bool,
_: Option<&PL>,
_: CorrelationDirection,
) -> Result<(), Box<dyn error::Error>> {
Err(GpuError::new("Compiled without GPU support").into())
}
}

#[derive(Debug)]
pub struct GpuError {
msg: &'static str,
}

impl GpuError {
fn new(msg: &'static str) -> GpuError {
GpuError { msg }
}
}

impl std::error::Error for GpuError {}

impl fmt::Display for GpuError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
Loading

0 comments on commit 9d10cb7

Please sign in to comment.