Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 21, 2024
1 parent e6747a7 commit 6e56f56
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 136 deletions.
11 changes: 7 additions & 4 deletions dunge/src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
group::BoundTexture, shader::Shader, state::State, texture::Sampler, uniform::Uniform,
Group,
},
std::{any::TypeId, fmt, marker::PhantomData, sync::Arc},
std::{any::TypeId, error, fmt, marker::PhantomData, sync::Arc},
wgpu::{
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindingResource, Device,
},
Expand Down Expand Up @@ -58,9 +58,10 @@ pub struct GroupHandler<G> {
shader_id: usize,
id: usize,
layout: Arc<BindGroupLayout>,
ty: PhantomData<G>,
ty: PhantomData<fn(G)>,
}

#[derive(Debug)]
pub struct ForeignShader;

impl fmt::Display for ForeignShader {
Expand All @@ -69,6 +70,8 @@ impl fmt::Display for ForeignShader {
}
}

impl error::Error for ForeignShader {}

pub trait Binding {
fn binding(&self) -> Bind;
}
Expand Down Expand Up @@ -104,10 +107,10 @@ impl Binding for SharedBinding {

pub type Update = Result<(), ForeignShader>;

pub(crate) fn update<G>(
pub(crate) fn update<G, H>(
state: &State,
uni: &mut UniqueBinding,
handler: &GroupHandler<G>,
handler: &GroupHandler<H>,
group: &G,
) -> Update
where
Expand Down
23 changes: 18 additions & 5 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use {
crate::{
bind::{self, Binder, GroupHandler, UniqueBinding, Update, Visit},
draw::Draw,
instance::Row,
layer::{Layer, Options},
mesh::{self, Mesh},
shader::Shader,
sl::IntoModule,
state::State,
texture::{self, CopyBuffer, CopyBufferView, Filter, Make, MapResult, Mapped, Sampler},
texture::{
self, CopyBuffer, CopyBufferView, DrawTexture, Filter, Make, MapResult, Mapped, Sampler,
},
uniform::{IntoValue, Uniform, Value},
Vertex,
Group, Vertex,
},
std::{error, fmt, future::IntoFuture, sync::Arc},
};
Expand Down Expand Up @@ -91,17 +94,27 @@ impl Context {
view.map(&self.0, tx, rx).await
}

pub fn update_group<G>(
pub fn update_group<G, H>(
&self,
uni: &mut UniqueBinding,
handler: &GroupHandler<G>,
handler: &GroupHandler<H>,
group: &G,
) -> Update
where
G: Visit,
G: Visit<Projection = H::Projection>,
H: Group,
{
bind::update(&self.0, uni, handler, group)
}

pub fn draw_to<T, D>(&self, texture: &T, draw: D)
where
T: DrawTexture,
D: Draw,
{
let view = texture.draw_texture().render_view();
self.0.draw(view, draw);
}
}

/// An error returned from the [`Context`] constructor.
Expand Down
12 changes: 6 additions & 6 deletions dunge/src/draw.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use crate::state::Frame;

pub trait Draw {
fn draw(&self, frame: Frame);
fn draw(&mut self, frame: Frame);
}

impl<D> Draw for &D
impl<D> Draw for &mut D
where
D: Draw + ?Sized,
{
fn draw(&self, frame: Frame) {
fn draw(&mut self, frame: Frame) {
(**self).draw(frame);
}
}

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

impl<D> Draw for Func<D>
where
D: Fn(Frame),
D: FnMut(Frame),
{
fn draw(&self, frame: Frame) {
fn draw(&mut self, frame: Frame) {
(self.0)(frame);
}
}
Expand Down
24 changes: 16 additions & 8 deletions dunge/src/el.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use {
crate::{
context::Context,
state::Render,
time::{Fps, Time},
update::{Close, Update},
update::Update,
window::View,
},
std::{cell::Cell, error, fmt, ops, time::Duration},
Expand Down Expand Up @@ -92,7 +91,6 @@ where

// Initial state
let mut active = false;
let mut render = Render::default();
let mut time = Time::now();
let mut fps = Fps::default();
move |ev, target| match ev {
Expand Down Expand Up @@ -185,7 +183,7 @@ where
ctrl.fps = fps;
}

if update.update(&ctrl).close() {
if let Then::Close = update.update(&ctrl).flow() {
log::debug!("close");
target.exit();
return;
Expand All @@ -195,7 +193,7 @@ where
match ctrl.view.output() {
Ok(output) => {
let rv = output.render_view();
cx.state().draw(&mut render, rv, &update);
cx.state().draw(rv, &mut update);
output.present();
}
Err(SurfaceError::Timeout) => log::info!("suface error: timeout"),
Expand Down Expand Up @@ -278,14 +276,24 @@ pub struct Key {
pub text: Option<SmolStr>,
}

pub trait Flow {
fn flow(self) -> Then;
}

impl Flow for () {
fn flow(self) -> Then {
Then::Run
}
}

#[derive(Clone, Copy)]
pub enum Then {
Run,
Close,
}

impl Close for Then {
fn close(self) -> bool {
matches!(self, Self::Close)
impl Flow for Then {
fn flow(self) -> Self {
self
}
}
81 changes: 17 additions & 64 deletions dunge/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use {
crate::{
color::Rgba,
context::{Context, Error},
context::Error,
draw::Draw,
format::Format,
layer::{Layer, SetLayer},
texture::{CopyBuffer, CopyTexture, DrawTexture},
texture::{CopyBuffer, CopyTexture},
},
std::sync::atomic::{self, AtomicUsize},
wgpu::{Color, CommandEncoder, Device, Instance, LoadOp, Queue, TextureView},
Expand Down Expand Up @@ -87,28 +87,24 @@ impl State {
self.shader_ids.fetch_add(1, atomic::Ordering::Relaxed)
}

pub fn draw<D>(&self, render: &mut Render, view: RenderView, draw: D)
pub fn draw<D>(&self, view: RenderView, mut draw: D)
where
D: Draw,
{
use wgpu::CommandEncoderDescriptor;

self.queue.submit([]);
draw.draw(render.0.make(&self.device, view));
let buffers = render.0.drain().map(CommandEncoder::finish);
self.queue.submit(buffers);
}
}
let mut encoder = {
let desc = CommandEncoderDescriptor::default();
self.device.create_command_encoder(&desc)
};

#[derive(Default)]
pub struct Render(Encoders);
draw.draw(Frame {
view,
encoder: &mut encoder,
});

impl Render {
pub fn draw_to<T, D>(&mut self, cx: &Context, texture: &T, draw: D)
where
T: DrawTexture,
D: Draw,
{
let view = texture.draw_texture().render_view();
cx.state().draw(self, view, draw);
self.queue.submit([encoder.finish()]);
}
}

Expand Down Expand Up @@ -139,20 +135,10 @@ impl From<Rgba> for Options {

pub struct Frame<'v, 'e> {
view: RenderView<'v>,
device: &'e Device,
encoders: &'e mut Encoders,
id: usize,
encoder: &'e mut CommandEncoder,
}

impl Frame<'_, '_> {
pub fn subframe<'e, 'v, T>(&'e mut self, texture: &'v T) -> Frame<'v, 'e>
where
T: DrawTexture,
{
let view = texture.draw_texture().render_view();
self.encoders.make(self.device, view)
}

pub fn layer<'p, V, I, O>(&'p mut self, layer: &'p Layer<V, I>, opts: O) -> SetLayer<'p, V, I>
where
O: Into<Options>,
Expand All @@ -179,48 +165,15 @@ impl Frame<'_, '_> {
..Default::default()
};

let encoder = self.encoders.get_mut(self.id);
let pass = encoder.begin_render_pass(&desc);
let pass = self.encoder.begin_render_pass(&desc);
layer.set(pass)
}

pub fn copy_texture<T>(&mut self, buffer: &CopyBuffer, texture: &T)
where
T: CopyTexture,
{
let encoder = self.encoders.get_mut(self.id);
buffer.copy_texture(texture.copy_texture(), encoder);
}
}

#[derive(Default)]
struct Encoders(Vec<CommandEncoder>);

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

let encoder = {
let desc = CommandEncoderDescriptor::default();
device.create_command_encoder(&desc)
};

let id = self.0.len();
self.0.push(encoder);
Frame {
view,
device,
encoders: self,
id,
}
}

fn get_mut(&mut self, id: usize) -> &mut CommandEncoder {
&mut self.0[id]
}

fn drain(&mut self) -> impl Iterator<Item = CommandEncoder> + '_ {
self.0.drain(..)
buffer.copy_texture(texture.copy_texture(), self.encoder);
}
}

Expand Down
44 changes: 19 additions & 25 deletions dunge/src/update.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
use crate::{draw::Draw, el::Control, state::Frame};

pub trait Close {
fn close(self) -> bool;
}

impl Close for () {
fn close(self) -> bool {
false
}
}
use crate::{
draw::Draw,
el::{Control, Flow},
state::Frame,
};

pub trait Update: Draw {
type Close: Close;
fn update(&mut self, ctrl: &Control) -> Self::Close;
type Flow: Flow;
fn update(&mut self, ctrl: &Control) -> Self::Flow;
}

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

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

impl<U, C, D> Update for Func<U, D>
impl<U, F, D> Update for Func<U, D>
where
U: FnMut(&Control) -> C,
C: Close,
D: Fn(Frame),
U: FnMut(&Control) -> F,
F: Flow,
D: FnMut(Frame),
{
type Close = C;
type Flow = F;

fn update(&mut self, ctrl: &Control) -> Self::Close {
fn update(&mut self, ctrl: &Control) -> Self::Flow {
(self.0)(ctrl)
}
}
Expand Down
Loading

0 comments on commit 6e56f56

Please sign in to comment.