Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Feb 27, 2024
1 parent c115336 commit d8f5d1c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 37 deletions.
6 changes: 3 additions & 3 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ impl Context {

/// An error returned from the [context](Context) constructor.
#[derive(Debug)]
pub enum MakeContextError {
pub enum FailedMakeContext {
BackendSelection,
RequestDevice(wgpu::RequestDeviceError),
}

impl fmt::Display for MakeContextError {
impl fmt::Display for FailedMakeContext {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::BackendSelection => write!(f, "failed to select backend"),
Expand All @@ -135,7 +135,7 @@ impl fmt::Display for MakeContextError {
}
}

impl error::Error for MakeContextError {
impl error::Error for FailedMakeContext {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::BackendSelection => None,
Expand Down
8 changes: 4 additions & 4 deletions dunge/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
context::{Context, MakeContextError},
context::{Context, FailedMakeContext},
state::State,
},
wgpu::Instance,
Expand All @@ -9,7 +9,7 @@ use {
#[cfg(feature = "winit")]
use crate::{element::Element, window::WindowBuilder};

pub(crate) async fn make() -> Result<(Context, Instance), MakeContextError> {
pub(crate) async fn make() -> Result<(Context, Instance), FailedMakeContext> {
use wgpu::{Backends, InstanceDescriptor, InstanceFlags};

let backends;
Expand Down Expand Up @@ -44,8 +44,8 @@ pub(crate) async fn make() -> Result<(Context, Instance), MakeContextError> {
///
/// # Errors
/// Returns an error when the context could not be created.
/// See [`MakeContextError`] for details.
pub async fn context() -> Result<Context, MakeContextError> {
/// See [`FailedMakeContext`] for details.
pub async fn context() -> Result<Context, FailedMakeContext> {
make().await.map(|(cx, _)| cx)
}

Expand Down
2 changes: 1 addition & 1 deletion dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub mod prelude {

pub use {
crate::{
context::{Context, MakeContextError},
context::{Context, FailedMakeContext},
draw::{draw, Draw},
format::Format,
init::context,
Expand Down
8 changes: 4 additions & 4 deletions dunge/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::{
color::Rgba,
context::MakeContextError,
context::FailedMakeContext,
draw::Draw,
format::Format,
layer::{Layer, SetLayer},
Expand All @@ -23,7 +23,7 @@ pub(crate) struct State {
}

impl State {
pub async fn new(instance: &Instance) -> Result<Self, MakeContextError> {
pub async fn new(instance: &Instance) -> Result<Self, FailedMakeContext> {
let adapter = {
use wgpu::{PowerPreference, RequestAdapterOptions};

Expand All @@ -35,7 +35,7 @@ impl State {
instance
.request_adapter(&options)
.await
.ok_or(MakeContextError::BackendSelection)?
.ok_or(FailedMakeContext::BackendSelection)?
};

let backend = adapter.get_info().backend;
Expand All @@ -58,7 +58,7 @@ impl State {
adapter
.request_device(&desc, None)
.await
.map_err(MakeContextError::RequestDevice)?
.map_err(FailedMakeContext::RequestDevice)?
};

Ok(Self {
Expand Down
8 changes: 4 additions & 4 deletions dunge/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use {
crate::{
context::{Context, MakeContextError},
context::{Context, FailedMakeContext},
el::Loop,
element::Element,
format::Format,
Expand Down Expand Up @@ -320,8 +320,8 @@ impl From<CreateSurfaceError> for Error {
}
}

impl From<MakeContextError> for Error {
fn from(v: MakeContextError) -> Self {
impl From<FailedMakeContext> for Error {
fn from(v: FailedMakeContext) -> Self {
Self(ErrorKind::Context(v))
}
}
Expand Down Expand Up @@ -356,5 +356,5 @@ enum ErrorKind {
EventLoop(EventLoopError),
Os(OsError),
Surface(CreateSurfaceError),
Context(MakeContextError),
Context(FailedMakeContext),
}
1 change: 1 addition & 0 deletions dunge_shader/src/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use {
std::{marker::PhantomData, ops},
};

#[must_use]
pub struct Ret<A, O> {
a: A,
t: PhantomData<O>,
Expand Down
35 changes: 14 additions & 21 deletions dunge_shader/src/texture.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
use {
crate::{
eval::{Eval, Expr, GetEntry},
eval::{Eval, Expr, Fs, GetEntry},
ret::Ret,
types,
},
naga::{Expression, SampleLevel},
std::marker::PhantomData,
};

type Tex<T, S, C, O, E> = Ret<Samp<T, S, C, E>, types::Vec4<O>>;
type Tex<T, S, C, O> = Ret<Samp<T, S, C>, types::Vec4<O>>;

pub const fn texture_sample<T, S, C, E>(tex: T, sam: S, crd: C) -> Tex<T, S, C, f32, E>
/// Performs the [`textureSample`](https://www.w3.org/TR/WGSL/#texturesample) function.
pub const fn texture_sample<T, S, C>(tex: T, sam: S, crd: C) -> Tex<T, S, C, f32>
where
T: Eval<E, Out = types::Texture2d<f32>>,
S: Eval<E, Out = types::Sampler>,
C: Eval<E, Out = types::Vec2<f32>>,
T: Eval<Fs, Out = types::Texture2d<f32>>,
S: Eval<Fs, Out = types::Sampler>,
C: Eval<Fs, Out = types::Vec2<f32>>,
{
Ret::new(Samp {
tex,
sam,
crd,
e: PhantomData,
})
Ret::new(Samp { tex, sam, crd })
}

pub struct Samp<T, S, C, E> {
pub struct Samp<T, S, C> {
tex: T,
sam: S,
crd: C,
e: PhantomData<E>,
}

impl<T, S, C, F, E> Eval<E> for Ret<Samp<T, S, C, E>, types::Vec4<F>>
impl<T, S, C, F> Eval<Fs> for Ret<Samp<T, S, C>, types::Vec4<F>>
where
T: Eval<E, Out = types::Texture2d<F>>,
S: Eval<E, Out = types::Sampler>,
C: Eval<E, Out = types::Vec2<f32>>,
E: GetEntry,
T: Eval<Fs, Out = types::Texture2d<F>>,
S: Eval<Fs, Out = types::Sampler>,
C: Eval<Fs, Out = types::Vec2<f32>>,
{
type Out = types::Vec4<F>;

fn eval(self, en: &mut E) -> Expr {
fn eval(self, en: &mut Fs) -> Expr {
let Samp { tex, sam, crd, .. } = self.get();
let ex = Sampled {
tex: tex.eval(en),
Expand Down

0 comments on commit d8f5d1c

Please sign in to comment.