2023-01-16 11:58:59 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
use vulkan_rs::prelude::*;
|
|
|
|
|
2023-01-16 12:27:54 +00:00
|
|
|
#[cfg(feature = "audio")]
|
|
|
|
use audio::*;
|
|
|
|
|
2023-01-16 11:58:59 +00:00
|
|
|
pub trait ContextInterface: Send + Sync {
|
|
|
|
fn device(&self) -> &Arc<Device>;
|
|
|
|
fn queue(&self) -> &Arc<Mutex<Queue>>;
|
|
|
|
|
|
|
|
fn format(&self) -> VkFormat;
|
|
|
|
fn image_layout(&self) -> VkImageLayout;
|
|
|
|
|
|
|
|
fn image_count(&self) -> usize;
|
|
|
|
fn images(&self) -> TargetMode<Vec<Arc<Image>>>;
|
|
|
|
fn width(&self) -> u32;
|
|
|
|
fn height(&self) -> u32;
|
2023-01-16 12:27:54 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "audio")]
|
2025-02-27 12:52:14 +00:00
|
|
|
fn sound_handler(&mut self) -> &mut SoundHandler;
|
2023-01-16 11:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum TargetMode<T> {
|
|
|
|
Mono(T),
|
|
|
|
Stereo(T, T),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> TargetMode<T> {
|
|
|
|
pub fn mono(&self) -> &T {
|
|
|
|
match self {
|
|
|
|
TargetMode::Mono(s) => s,
|
|
|
|
TargetMode::Stereo(_, _) => panic!("Expected another target mode"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mono_mut(&mut self) -> &mut T {
|
|
|
|
match self {
|
|
|
|
TargetMode::Mono(s) => s,
|
|
|
|
TargetMode::Stereo(_, _) => panic!("Expected another target mode"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stereo(&self) -> (&T, &T) {
|
|
|
|
match self {
|
|
|
|
TargetMode::Mono(_) => panic!("Expected another target mode"),
|
|
|
|
TargetMode::Stereo(l, r) => (l, r),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stereo_mut(&mut self) -> (&mut T, &mut T) {
|
|
|
|
match self {
|
|
|
|
TargetMode::Mono(_) => panic!("Expected another target mode"),
|
|
|
|
TargetMode::Stereo(l, r) => (l, r),
|
|
|
|
}
|
|
|
|
}
|
2023-01-21 13:14:43 +00:00
|
|
|
|
2023-01-22 15:02:46 +00:00
|
|
|
pub fn chain<'a, R>(&'a self, other: &'a TargetMode<R>) -> TargetMode<(&'a T, &'a R)> {
|
|
|
|
match (self, other) {
|
|
|
|
(TargetMode::Mono(mono_self), TargetMode::Mono(mono_other)) => {
|
|
|
|
TargetMode::Mono((mono_self, mono_other))
|
|
|
|
}
|
|
|
|
(
|
|
|
|
TargetMode::Stereo(left_self, right_self),
|
|
|
|
TargetMode::Stereo(left_other, right_other),
|
|
|
|
) => TargetMode::Stereo((left_self, left_other), (right_self, right_other)),
|
|
|
|
|
|
|
|
_ => panic!("Incompatible TargetModes"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 13:14:43 +00:00
|
|
|
pub fn execute<F, R>(&self, mut f: F) -> anyhow::Result<TargetMode<R>>
|
|
|
|
where
|
|
|
|
F: FnMut(&T) -> anyhow::Result<R>,
|
|
|
|
{
|
|
|
|
Ok(match self {
|
|
|
|
TargetMode::Mono(s) => TargetMode::Mono(f(s)?),
|
|
|
|
TargetMode::Stereo(l, r) => TargetMode::Stereo(f(l)?, f(r)?),
|
|
|
|
})
|
|
|
|
}
|
2023-01-22 15:02:46 +00:00
|
|
|
|
|
|
|
pub fn execute_into<F, R>(self, mut f: F) -> anyhow::Result<TargetMode<R>>
|
|
|
|
where
|
|
|
|
F: FnMut(T) -> anyhow::Result<R>,
|
|
|
|
{
|
|
|
|
Ok(match self {
|
|
|
|
TargetMode::Mono(s) => TargetMode::Mono(f(s)?),
|
|
|
|
TargetMode::Stereo(l, r) => TargetMode::Stereo(f(l)?, f(r)?),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Unfold {
|
|
|
|
type Output;
|
|
|
|
|
|
|
|
fn unfold(self) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_unfold {
|
|
|
|
([<$($var:ident: $type:ident,)+>, $rhs_var:ident: $rhs_type:ident]) => {
|
|
|
|
paste::paste! {
|
|
|
|
impl<'a, $($type,)+ $rhs_type: 'a> Unfold for TargetMode<(&'a ($($type,)+), &'a $rhs_type)> {
|
|
|
|
type Output = TargetMode<($(&'a $type,)+ &'a $rhs_type)>;
|
|
|
|
|
|
|
|
fn unfold(self) -> Self::Output {
|
|
|
|
match self {
|
|
|
|
TargetMode::Mono( (($($var,)+), $rhs_var) ) => TargetMode::Mono(($($var,)+ &$rhs_var)),
|
|
|
|
TargetMode::Stereo( ( ( $( [<l_ $var>], )+ ) , [<l_ $rhs_var>] ), ( ( $( [<r_ $var>], )+ ) , [<r_ $rhs_var>] ) )
|
|
|
|
=> TargetMode::Stereo( ( $( [<l_ $var>], )+ &[<l_ $rhs_var>] ), ( $( [<r_ $var>], )+ &[<r_ $rhs_var>] ) ),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2023-01-16 11:58:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 15:02:46 +00:00
|
|
|
impl_unfold!([<t: T, r: R,>, s: S]);
|
|
|
|
impl_unfold!([<t: T, r: R, u: U,>, s: S]);
|
|
|
|
impl_unfold!([<t: T, r: R, u: U, v: V,>, s: S]);
|
|
|
|
impl_unfold!([<t: T, r: R, u: U, v: V, w: W,>, s: S]);
|
|
|
|
impl_unfold!([<t: T, r: R, u: U, v: V, w: W, x: X,>, s: S]);
|
|
|
|
impl_unfold!([<t: T, r: R, u: U, v: V, w: W, x: X, y: Y,>, s: S]);
|
|
|
|
impl_unfold!([<t: T, r: R, u: U, v: V, w: W, x: X, y: Y, z: Z,>, s: S]);
|
|
|
|
impl_unfold!([<t: T, r: R, u: U, v: V, w: W, x: X, y: Y, z: Z, q: Q,>, s: S]);
|
|
|
|
|
2023-01-16 11:58:59 +00:00
|
|
|
impl<T: Clone> Clone for TargetMode<T> {
|
|
|
|
fn clone(&self) -> TargetMode<T> {
|
|
|
|
match self {
|
|
|
|
TargetMode::Mono(t) => TargetMode::Mono(t.clone()),
|
|
|
|
TargetMode::Stereo(lhs, rhs) => TargetMode::Stereo(lhs.clone(), rhs.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-21 13:14:43 +00:00
|
|
|
|
|
|
|
impl<T> From<T> for TargetMode<T> {
|
|
|
|
fn from(value: T) -> Self {
|
|
|
|
TargetMode::Mono(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<(T, T)> for TargetMode<T> {
|
|
|
|
fn from((lhs, rhs): (T, T)) -> Self {
|
|
|
|
TargetMode::Stereo(lhs, rhs)
|
|
|
|
}
|
|
|
|
}
|