Skip to content

Commit

Permalink
Window
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 6, 2024
1 parent 3f1cf5c commit 81e90a4
Show file tree
Hide file tree
Showing 10 changed files with 461 additions and 32 deletions.
20 changes: 8 additions & 12 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
mesh::{self, Mesh},
shader::Shader,
sl::IntoModule,
state::{Render, State, View},
state::{Render, RenderView, State},
texture::{
self, CopyBuffer, CopyBufferView, DrawTexture, Format, Make, MapResult, Mapped, Sampler,
},
Expand All @@ -19,17 +19,13 @@ use {
pub struct Context(Arc<State>);

impl Context {
pub async fn new() -> Result<Self, Error> {
use wgpu::{Backends, Instance, InstanceDescriptor};

let desc = InstanceDescriptor {
backends: Backends::PRIMARY,
..Default::default()
};
pub(crate) fn new(state: State) -> Self {
Self(Arc::new(state))
}

let instance = Instance::new(desc);
let state = State::new(&instance).await?;
Ok(Self(Arc::new(state)))
#[cfg(feature = "winit")]
pub(crate) fn state(&self) -> &State {
&self.0
}

pub fn make_shader<M, A>(&self, module: M) -> Shader<M::Vertex>
Expand Down Expand Up @@ -83,7 +79,7 @@ impl Context {
T: DrawTexture,
D: Draw,
{
let view = View::from_texture(texture.draw_texture());
let view = RenderView::from_texture(texture.draw_texture());
self.0.draw(render, view, draw)
}

Expand Down
12 changes: 6 additions & 6 deletions dunge/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ where
}
}

pub fn from_fn<F>(f: F) -> impl Draw
pub fn from_fn<D>(draw: D) -> impl Draw
where
F: Fn(Frame),
D: Fn(Frame),
{
struct Func<F>(F);
struct Func<D>(D);

impl<F> Draw for Func<F>
impl<D> Draw for Func<D>
where
F: Fn(Frame),
D: Fn(Frame),
{
fn draw(&self, frame: Frame) {
(self.0)(frame);
}
}

Func(f)
Func(draw)
}
42 changes: 42 additions & 0 deletions dunge/src/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use {
crate::{
context::{self, Context},
state::State,
},
wgpu::Instance,
};

#[cfg(feature = "winit")]
use crate::window::{self, Window, WindowBuilder};

fn instance() -> Instance {
use wgpu::{Backends, InstanceDescriptor};

let desc = InstanceDescriptor {
backends: Backends::PRIMARY,
..Default::default()
};

Instance::new(desc)
}

pub async fn context() -> Result<Context, context::Error> {
let instance = instance();
let state = State::new(&instance).await?;
Ok(Context::new(state))
}

#[cfg(feature = "winit")]
pub fn window() -> WindowBuilder {
WindowBuilder::new()
}

#[cfg(feature = "winit")]
impl WindowBuilder {
pub async fn make(self) -> Result<Window, window::Error> {
let instance = instance();
let state = State::new(&instance).await?;
let cx = Context::new(state);
self.build(cx, &instance)
}
}
8 changes: 8 additions & 0 deletions dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ pub mod color;
pub mod context;
pub mod draw;
pub mod group;
mod init;
pub mod layer;
pub mod mesh;
pub mod shader;
pub mod state;
pub mod texture;
pub mod update;
pub mod vertex;

#[cfg(feature = "winit")]
mod time;
#[cfg(feature = "winit")]
pub mod window;

pub use {
crate::init::context,
dunge_macros::{Group, Vertex},
dunge_shader::{group::Group, sl, types, vertex::Vertex},
glam,
};

#[cfg(feature = "winit")]
pub use crate::init::window;
29 changes: 21 additions & 8 deletions dunge/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ use {
texture::{CopyBuffer, CopyTexture, DrawTexture, Format, Texture},
},
std::sync::atomic::{self, AtomicUsize},
wgpu::{Adapter, Color, CommandEncoder, Device, Instance, LoadOp, Queue, TextureView},
wgpu::{Color, CommandEncoder, Device, Instance, LoadOp, Queue, TextureView},
};

#[cfg(feature = "winit")]
use {crate::window::Output, wgpu::Adapter};

pub(crate) struct State {
#[cfg(feature = "winit")]
adapter: Adapter,
device: Device,
queue: Queue,
Expand Down Expand Up @@ -52,14 +56,15 @@ impl State {
};

Ok(Self {
#[cfg(feature = "winit")]
adapter,
device,
queue,
shader_ids: AtomicUsize::default(),
})
}

#[allow(dead_code)]
#[cfg(feature = "winit")]
pub fn adapter(&self) -> &Adapter {
&self.adapter
}
Expand All @@ -76,7 +81,7 @@ impl State {
self.shader_ids.fetch_add(1, atomic::Ordering::Relaxed)
}

pub fn draw<D>(&self, render: &mut Render, view: View, draw: D)
pub fn draw<D>(&self, render: &mut Render, view: RenderView, draw: D)
where
D: Draw,
{
Expand Down Expand Up @@ -109,7 +114,7 @@ impl Options {
}

pub struct Frame<'v, 'e> {
view: View<'v>,
view: RenderView<'v>,
device: &'e Device,
encoders: &'e mut Encoders,
id: usize,
Expand All @@ -120,7 +125,7 @@ impl Frame<'_, '_> {
where
T: DrawTexture,
{
let view = View::from_texture(texture.draw_texture());
let view = RenderView::from_texture(texture.draw_texture());
self.encoders.make(self.device, view)
}

Expand Down Expand Up @@ -163,7 +168,7 @@ impl Frame<'_, '_> {
struct Encoders(Vec<CommandEncoder>);

impl Encoders {
fn make<'e, 'v>(&'e mut self, device: &'e Device, view: View<'v>) -> Frame<'v, 'e> {
fn make<'e, 'v>(&'e mut self, device: &'e Device, view: RenderView<'v>) -> Frame<'v, 'e> {
use wgpu::CommandEncoderDescriptor;

let encoder = {
Expand Down Expand Up @@ -191,16 +196,24 @@ impl Encoders {
}

#[derive(Clone, Copy)]
pub(crate) struct View<'v> {
pub(crate) struct RenderView<'v> {
txview: &'v TextureView,
format: Format,
}

impl<'v> View<'v> {
impl<'v> RenderView<'v> {
pub fn from_texture(texture: &'v Texture) -> Self {
Self {
txview: texture.view(),
format: texture.format(),
}
}

#[cfg(feature = "winit")]
pub fn from_output(output: &'v Output) -> Self {
Self {
txview: output.view(),
format: output.format(),
}
}
}
34 changes: 34 additions & 0 deletions dunge/src/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::{draw::Draw, state::Frame};

pub trait Update: Draw {
fn update(&mut self);
}

pub fn from_fn<U, D>(update: U, draw: D) -> impl Update
where
U: FnMut(),
D: Fn(Frame),
{
struct Func<U, D>(U, D);

impl<U, D> Draw for Func<U, D>
where
D: Fn(Frame),
{
fn draw(&self, frame: Frame) {
(self.1)(frame);
}
}

impl<U, D> Update for Func<U, D>
where
U: FnMut(),
D: Fn(Frame),
{
fn update(&mut self) {
(self.0)();
}
}

Func(update, draw)
}
Loading

0 comments on commit 81e90a4

Please sign in to comment.