Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Commit

Permalink
[glsl-in] Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JCapucho authored and kvark committed May 21, 2021
1 parent 4e95c46 commit 72fca17
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
48 changes: 28 additions & 20 deletions src/front/glsl/ast.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use super::{super::Typifier, constants::ConstantSolver, error::ErrorKind, SourceMetadata};
use super::{
super::{Emitter, Typifier},
constants::ConstantSolver,
error::ErrorKind,
SourceMetadata,
};
use crate::{
front::Emitter, proc::ResolveContext, Arena, BinaryOperator, Binding, Block, Constant,
Expression, FastHashMap, Function, FunctionArgument, GlobalVariable, Handle, Interpolation,
LocalVariable, Module, RelationalFunction, ResourceBinding, Sampling, ShaderStage, Statement,
StorageClass, Type, TypeInner, UnaryOperator,
proc::ResolveContext, Arena, BinaryOperator, Binding, Block, Constant, Expression, FastHashMap,
Function, FunctionArgument, GlobalVariable, Handle, Interpolation, LocalVariable, Module,
RelationalFunction, ResourceBinding, Sampling, ShaderStage, Statement, StorageClass, Type,
TypeInner, UnaryOperator,
};

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -185,17 +190,31 @@ impl<'function> Context<'function> {

this.emit_start();

for &(ref name, handle) in program.constants.iter() {
let expr = this.expressions.append(Expression::Constant(handle));
let var = VariableReference {
expr,
load: None,
mutable: false,
};

this.lookup_global_var_exps.insert(name.into(), var);
}

for &(ref name, lookup) in program.global_variables.iter() {
this.emit_flush(body);
let expr = match lookup {
GlobalLookup::Variable(v) => Expression::GlobalVariable(v),
GlobalLookup::BlockSelect(handle, index) => {
let base = this.add_expression(Expression::GlobalVariable(handle), body);
let base = this.expressions.append(Expression::GlobalVariable(handle));

Expression::AccessIndex { base, index }
}
};

let expr = this.add_expression(expr, body);
let expr = this.expressions.append(expr);
this.emit_start();

let var = VariableReference {
expr,
load: Some(this.add_expression(Expression::Load { pointer: expr }, body)),
Expand All @@ -206,17 +225,6 @@ impl<'function> Context<'function> {
this.lookup_global_var_exps.insert(name.into(), var);
}

for &(ref name, handle) in program.constants.iter() {
let expr = this.add_expression(Expression::Constant(handle), body);
let var = VariableReference {
expr,
load: None,
mutable: false,
};

this.lookup_global_var_exps.insert(name.into(), var);
}

this
}

Expand All @@ -231,9 +239,9 @@ impl<'function> Context<'function> {
pub fn add_expression(&mut self, expr: Expression, body: &mut Block) -> Handle<Expression> {
if expr.needs_pre_emit() {
self.emit_flush(body);
let expr = self.expressions.append(expr);
let handle = self.expressions.append(expr);
self.emit_start();
expr
handle
} else {
self.expressions.append(expr)
}
Expand Down
8 changes: 6 additions & 2 deletions src/front/glsl/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,7 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
let pointer = ctx.add_var(self.program, ty, name, maybe_constant, meta)?;

if let Some((value, _)) = init {
ctx.ctx.emit_flush(ctx.body);
ctx.ctx.emit_start();
ctx.flush_expressions();
ctx.body.push(Statement::Store { pointer, value });
}

Expand Down Expand Up @@ -1615,6 +1614,11 @@ impl<'ctx, 'fun> DeclarationContext<'ctx, 'fun> {
false => program.add_local_var(self.ctx, self.body, decl),
}
}

fn flush_expressions(&mut self) {
self.ctx.emit_flush(self.body);
self.ctx.emit_start()
}
}

fn binding_power(value: &TokenValue) -> Option<(u8, u8)> {
Expand Down
2 changes: 1 addition & 1 deletion src/front/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
/// Helper class to emit expressions
#[allow(dead_code)]
#[derive(Default, Debug)]
pub(crate) struct Emitter {
struct Emitter {
start_len: Option<usize>,
}

Expand Down

0 comments on commit 72fca17

Please sign in to comment.