Skip to content

Commit

Permalink
Merge pull request #6 from float3/main
Browse files Browse the repository at this point in the history
my edits
  • Loading branch information
pema99 authored Feb 11, 2024
2 parents 5a4576e + 55418b0 commit e975571
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 227 deletions.
62 changes: 13 additions & 49 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
name = "glsl2hlsl"
version = "0.1.0"
authors = ["Pema Malling <[email protected]>"]
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
glsl = "6.0.0"
glsl = "7.0.0"
ureq = { version = "2.1.1", features = ["json"] }
serde = { version = "1.0.126", features = ["derive"] }
serde_json = "1.0.64"
md5 = "0.7.0"

[workspace]
members=["glsl2hlsl-wasm"]
members=["glsl2hlsl-wasm"]

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
8 changes: 2 additions & 6 deletions glsl2hlsl-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "glsl2hlsl-wasm"
version = "0.1.0"
authors = ["Pema Malling <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]
Expand All @@ -28,8 +28,4 @@ console_error_panic_hook = { version = "0.1.6", optional = true }
wee_alloc = { version = "0.4.5", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.3.13"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
wasm-bindgen-test = "0.3.13"
4 changes: 1 addition & 3 deletions glsl2hlsl-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ pub fn download(json: String, extract_props: bool, raymarch: bool) {
let shader = make_shader(&json).unwrap();
let files = get_files(&shader, extract_props, raymarch);
for f in files.iter() {
unsafe {
download_file(&f.name, &f.contents);
}
download_file(&f.name, &f.contents);
}
}
1 change: 1 addition & 0 deletions glsl2hlsl-wasm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(dead_code)]
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
Expand Down
55 changes: 23 additions & 32 deletions src/glsl2hlsl/preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl ShaderProp {
}
}

fn eval_vector_vals(fid: &Identifier, exps: &Vec<Expr>) -> Option<Vec<f32>> {
fn eval_vector_vals(fid: &Identifier, exps: &[Expr]) -> Option<Vec<f32>> {
let vals = exps
.iter()
.flat_map(eval_shallow_const_expr)
Expand Down Expand Up @@ -58,7 +58,7 @@ fn eval_shallow_const_expr(e: &Expr) -> Option<Expr> {
match eval_vector_vals(&fid, &exps) {
Some(vals) => Some(Expr::FunCall(
FunIdentifier::Identifier(fid),
vals.iter().map(|x| Expr::FloatConst(-*x as f32)).collect(),
vals.iter().map(|x| Expr::FloatConst(-*x)).collect(),
)),
_ => Some(e.clone()),
}
Expand Down Expand Up @@ -99,14 +99,12 @@ fn eval_shallow_const_expr(e: &Expr) -> Option<Expr> {
BinaryOp::Div => Some(valsl.iter().zip(valsr.iter()).map(|(l, r)| l / r).collect()),
_ => None,
};
if let Some(vals) = vals {
Some(Expr::FunCall(
vals.map(|vals| {
Expr::FunCall(
FunIdentifier::Identifier(fidl),
vals.iter().map(|x| Expr::FloatConst(*x)).collect(),
))
} else {
None
}
)
})
}
_ => Some(e.clone()),
},
Expand Down Expand Up @@ -184,29 +182,22 @@ pub fn process_globals(tu: &mut TranslationUnit, extract_props: bool) -> Vec<Sha
let mut res = Vec::new();

fn is_const(ty: &TypeQualifierSpec) -> bool {
match ty {
TypeQualifierSpec::Storage(StorageQualifier::Const) => true,
_ => false,
}
matches!(ty, TypeQualifierSpec::Storage(StorageQualifier::Const))
}

if extract_props {
for decl in tu.0 .0.iter_mut() {
match decl {
ExternalDeclaration::Declaration(Declaration::InitDeclaratorList(ref mut idl)) => {
match (&idl.head.name, &idl.head.initializer, &mut idl.head.ty.qualifier) {
(Some(name), Some(Initializer::Simple(init)), Some(qual)) => {
if qual.qualifiers.0.iter().any(is_const) {
if let Some(prop) = extract_prop(name.as_str(), init.as_ref()) {
res.push(prop);
idl.head.initializer = None;
}
}
if let ExternalDeclaration::Declaration(Declaration::InitDeclaratorList(ref mut idl)) = decl {
if let (Some(name), Some(Initializer::Simple(init)), Some(qual)) =
(&idl.head.name, &idl.head.initializer, &mut idl.head.ty.qualifier)
{
if qual.qualifiers.0.iter().any(is_const) {
if let Some(prop) = extract_prop(name.as_str(), init.as_ref()) {
res.push(prop);
idl.head.initializer = None;
}
_ => {}
}
}
_ => {}
}
}
}
Expand All @@ -223,15 +214,15 @@ pub fn process_macros(s: String, extract_props: bool) -> (String, HashMap<usize,

push_sym();
for (i, line) in s.lines().enumerate() {
if line.trim_start().starts_with("#") {
if line.trim_start().starts_with('#') {
// Marker declaration
buff.push_str(format!("float __LINE{}__;\n", i).as_str());

// Parse def, extract prop
let def = Preprocessor::parse(line.trim_start()).unwrap();
let prop = match def {
Preprocessor::Define(PreprocessorDefine::ObjectLike { ref ident, ref value }) if extract_props => {
Statement::parse(&value)
Statement::parse(value)
.ok()
.and_then(|x| if let Statement::Simple(s) = x { Some(s) } else { None })
.and_then(|x| {
Expand All @@ -241,7 +232,7 @@ pub fn process_macros(s: String, extract_props: bool) -> (String, HashMap<usize,
None
}
})
.or(Expr::parse(&value).ok())
.or(Expr::parse(value).ok())
.and_then(|x| extract_prop(ident.as_str(), &x))
}
_ => None,
Expand All @@ -258,7 +249,7 @@ pub fn process_macros(s: String, extract_props: bool) -> (String, HashMap<usize,
}
} else {
buff.push_str(line);
buff.push_str("\n");
buff.push('\n');
}
}

Expand Down Expand Up @@ -288,7 +279,7 @@ pub fn replace_macros(s: String, defs: HashMap<usize, String>) -> String {
}
} else {
buff.push_str(line);
buff.push_str("\n");
buff.push('\n');
}
}

Expand All @@ -310,7 +301,7 @@ fn find_param<'a>(fdef: &'a mut FunctionDefinition, lut: Vec<&str>) -> Option<Pr
.tail
.iter_mut()
.find(|x| lut.contains(&x.ident.ident.0.to_lowercase().as_str()))
.map(|x| PropDeclaration::SingleNoType(x));
.map(PropDeclaration::SingleNoType);
if let Some(ref name) = decl.head.name {
if lut.contains(&name.0.to_lowercase().as_str()) {
Some(PropDeclaration::Single(&mut decl.head))
Expand Down Expand Up @@ -342,7 +333,7 @@ fn find_param<'a>(fdef: &'a mut FunctionDefinition, lut: Vec<&str>) -> Option<Pr
.iter_mut()
.find_map(|stmt| get_statement_decls(stmt, lut)),
}
};
}

fdef.statement
.statement_list
Expand Down Expand Up @@ -415,7 +406,7 @@ fn remove_param(fdef: &mut FunctionDefinition, name: &str) {
}

fn remove_param_stmt_vec(stmts: &mut Vec<Statement>, name: &str) {
stmts.drain_filter(|x| remove_param_stmt(x, name)).for_each(|_x| ());
stmts.extract_if(|x| remove_param_stmt(x, name)).for_each(|_x| ());
}

remove_param_stmt_vec(&mut fdef.statement.statement_list, name);
Expand Down
Loading

0 comments on commit e975571

Please sign in to comment.