Skip to content

Commit

Permalink
Add PopGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Feb 20, 2024
1 parent 94db840 commit c115336
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ match-wildcard-for-single-variants = "deny"
flat-map-option = "deny"
semicolon-if-nothing-returned = "deny"
manual-assert = "deny"
needless-pass-by-value = "deny"
trivially-copy-pass-by-ref = "deny"

[profile.dev]
Expand Down
54 changes: 28 additions & 26 deletions dunge_shader/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where
"reentrant in a shader function isn't allowed",
);

push();
let pop = push();
let Out { place, color } = f().output();
let mut compl = Compiler::default();
let mut binds = Bindings::default();
Expand Down Expand Up @@ -79,10 +79,10 @@ where
};

let out = required.into_iter().map(eval).collect();
let res = vs.inner.compose(fsty, out);
vs.inner.ret(res);
let res = vs.0.compose(fsty, out);
vs.0.ret(res);
let mut args = inputs.into_iter();
vs.inner.build(Stage::Vertex, &mut args, Return::Ty(fsty))
vs.0.build(Stage::Vertex, &mut args, Return::Ty(fsty))
};

let compl = vs.compl;
Expand All @@ -93,7 +93,7 @@ where
..Default::default()
};

pop();
_ = pop;
Module::new(cx, nm)
}

Expand Down Expand Up @@ -178,7 +178,7 @@ impl Eval<Vs> for Ret<ReadIndex, u32> {
type Out = u32;

fn eval(self, en: &mut Vs) -> Expr {
en.inner.argument(self.get().id)
en.get_entry().argument(self.get().id)
}
}

Expand All @@ -198,7 +198,7 @@ impl<O> Eval<Vs> for Ret<ReadVertex, O> {
type Out = O;

fn eval(self, en: &mut Vs) -> Expr {
let en = &mut en.inner;
let en = en.get_entry();
let arg = en.argument(self.get().id);
en.access_index(arg, self.get().index)
}
Expand All @@ -222,7 +222,7 @@ where
type Out = O;

fn eval(self, en: &mut Vs) -> Expr {
let en = &mut en.inner;
let en = en.get_entry();
let id = self.get().id;
match O::VALUE_TYPE {
ValueType::Scalar(_) | ValueType::Vector(_) => en.argument(id),
Expand Down Expand Up @@ -334,17 +334,23 @@ thread_local! {
static STACK: RefCell<Frames> = RefCell::default();
}

fn push() {
fn push() -> PopGuard {
STACK.with(|s| {
let mut s = s.borrow_mut();
let id = s.count;
s.count += 1;
s.stack.push(id);
});

PopGuard
}

fn pop() {
STACK.with(|s| s.borrow_mut().stack.pop());
struct PopGuard;

impl Drop for PopGuard {
fn drop(&mut self) {
STACK.with(|s| s.borrow_mut().stack.pop());
}
}

fn top() -> Option<u32> {
Expand Down Expand Up @@ -502,23 +508,19 @@ pub(crate) trait GetEntry {
fn get_entry(&mut self) -> &mut Entry;
}

pub struct Vs {
inner: Entry,
}
pub struct Vs(Entry);

impl Vs {
fn new(compl: Compiler) -> Self {
Self {
inner: Entry::new(compl),
}
Self(Entry::new(compl))
}
}

impl GetEntry for Vs {
const STAGE: Stage = Stage::Vertex;

fn get_entry(&mut self) -> &mut Entry {
&mut self.inner
&mut self.0
}
}

Expand Down Expand Up @@ -651,13 +653,13 @@ impl Entry {
}
}

fn push(&mut self) {
fn push(&mut self) -> PopGuard {
self.stack.push();
push();
push()
}

fn pop(&mut self) -> Statements {
pop();
fn pop(&mut self, pop: PopGuard) -> Statements {
_ = pop;
self.stack.pop()
}

Expand Down Expand Up @@ -868,10 +870,10 @@ impl<'a, E> Branch<'a, E> {
B: FnOnce(&mut Self) -> Option<Expr>,
{
let a_branch = {
self.en.get_entry().push();
let pop = self.en.get_entry().push();
let a = a(self.entry());
let en = self.en.get_entry();
let mut s = en.pop();
let mut s = en.pop(pop);
let st = Statement::Store {
pointer: self.expr.0,
value: a.0,
Expand All @@ -882,10 +884,10 @@ impl<'a, E> Branch<'a, E> {
};

let b_branch = {
self.en.get_entry().push();
let pop = self.en.get_entry().push();
let b = b(self);
let en = self.en.get_entry();
let mut s = en.pop();
let mut s = en.pop(pop);
if let Some(b) = b {
let st = Statement::Store {
pointer: self.expr.0,
Expand Down

0 comments on commit c115336

Please sign in to comment.