Skip to content

Commit

Permalink
[wgsl-in] Split into multiple files (#2207)
Browse files Browse the repository at this point in the history
Make changes suggested in #2075, but put off to a separate PR because they would interfere with reviewing the change:

- Split the new WGSL front end into modules in a logical way.
- Rename `Parser` to `Frontend`.
  • Loading branch information
SparkyPotato authored Jan 31, 2023
1 parent ae049ed commit 67ea8f0
Show file tree
Hide file tree
Showing 28 changed files with 5,837 additions and 5,766 deletions.
16 changes: 8 additions & 8 deletions benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn gather_inputs(folder: &str, extension: &str) -> Vec<Box<[u8]>> {
}

fn parse_glsl(stage: naga::ShaderStage, inputs: &[Box<[u8]>]) {
let mut parser = naga::front::glsl::Parser::default();
let mut parser = naga::front::glsl::Frontend::default();
let options = naga::front::glsl::Options {
stage,
defines: Default::default(),
Expand All @@ -44,12 +44,12 @@ fn frontends(c: &mut Criterion) {
#[cfg(all(feature = "wgsl-in", feature = "serialize", feature = "deserialize"))]
group.bench_function("bin", |b| {
let inputs_wgsl = gather_inputs("tests/in", "wgsl");
let mut parser = naga::front::wgsl::Parser::new();
let mut frontend = naga::front::wgsl::Frontend::new();
let inputs_bin = inputs_wgsl
.iter()
.map(|input| {
let string = std::str::from_utf8(input).unwrap();
let module = parser.parse(string).unwrap();
let module = frontend.parse(string).unwrap();
bincode::serialize(&module).unwrap()
})
.collect::<Vec<_>>();
Expand All @@ -66,10 +66,10 @@ fn frontends(c: &mut Criterion) {
.iter()
.map(|input| std::str::from_utf8(input).unwrap())
.collect::<Vec<_>>();
let mut parser = naga::front::wgsl::Parser::new();
let mut frontend = naga::front::wgsl::Frontend::new();
b.iter(move || {
for &input in inputs.iter() {
parser.parse(input).unwrap();
frontend.parse(input).unwrap();
}
});
});
Expand All @@ -81,7 +81,7 @@ fn frontends(c: &mut Criterion) {
for input in inputs.iter() {
let spv =
unsafe { slice::from_raw_parts(input.as_ptr() as *const u32, input.len() / 4) };
let parser = naga::front::spv::Parser::new(spv.iter().cloned(), &options);
let parser = naga::front::spv::Frontend::new(spv.iter().cloned(), &options);
parser.parse().unwrap();
}
});
Expand All @@ -101,12 +101,12 @@ fn frontends(c: &mut Criterion) {
#[cfg(feature = "wgsl-in")]
fn gather_modules() -> Vec<naga::Module> {
let inputs = gather_inputs("tests/in", "wgsl");
let mut parser = naga::front::wgsl::Parser::new();
let mut frontend = naga::front::wgsl::Frontend::new();
inputs
.iter()
.map(|input| {
let string = std::str::from_utf8(input).unwrap();
parser.parse(string).unwrap()
frontend.parse(string).unwrap()
})
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/bin/naga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
}
ext @ ("vert" | "frag" | "comp") => {
let input = String::from_utf8(input)?;
let mut parser = naga::front::glsl::Parser::default();
let mut parser = naga::front::glsl::Frontend::default();

(
parser
Expand Down
46 changes: 26 additions & 20 deletions src/front/glsl/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
ParameterQualifier,
},
context::Context,
Error, ErrorKind, Parser, Result,
Error, ErrorKind, Frontend, Result,
};
use crate::{
BinaryOperator, Block, Constant, DerivativeAxis, Expression, Handle, ImageClass,
Expand Down Expand Up @@ -1676,7 +1676,7 @@ impl MacroCall {
/// finally returns the final expression with the correct result
pub fn call(
&self,
parser: &mut Parser,
frontend: &mut Frontend,
ctx: &mut Context,
body: &mut Block,
args: &mut [Handle<Expression>],
Expand All @@ -1688,8 +1688,14 @@ impl MacroCall {
args[0]
}
MacroCall::SamplerShadow => {
sampled_to_depth(&mut parser.module, ctx, args[0], meta, &mut parser.errors);
parser.invalidate_expression(ctx, args[0], meta)?;
sampled_to_depth(
&mut frontend.module,
ctx,
args[0],
meta,
&mut frontend.errors,
);
frontend.invalidate_expression(ctx, args[0], meta)?;
ctx.samplers.insert(args[0], args[1]);
args[0]
}
Expand All @@ -1702,7 +1708,7 @@ impl MacroCall {
let mut coords = args[1];

if proj {
let size = match *parser.resolve_type(ctx, coords, meta)? {
let size = match *frontend.resolve_type(ctx, coords, meta)? {
TypeInner::Vector { size, .. } => size,
_ => unreachable!(),
};
Expand Down Expand Up @@ -1748,7 +1754,7 @@ impl MacroCall {

let extra = args.get(2).copied();
let comps =
parser.coordinate_components(ctx, args[0], coords, extra, meta, body)?;
frontend.coordinate_components(ctx, args[0], coords, extra, meta, body)?;

let mut num_args = 2;

Expand Down Expand Up @@ -1795,10 +1801,10 @@ impl MacroCall {
true => {
let offset_arg = args[num_args];
num_args += 1;
match parser.solve_constant(ctx, offset_arg, meta) {
match frontend.solve_constant(ctx, offset_arg, meta) {
Ok(v) => Some(v),
Err(e) => {
parser.errors.push(e);
frontend.errors.push(e);
None
}
}
Expand Down Expand Up @@ -1832,7 +1838,7 @@ impl MacroCall {
if arrayed {
let mut components = Vec::with_capacity(4);

let size = match *parser.resolve_type(ctx, expr, meta)? {
let size = match *frontend.resolve_type(ctx, expr, meta)? {
TypeInner::Vector { size: ori_size, .. } => {
for index in 0..(ori_size as u32) {
components.push(ctx.add_expression(
Expand Down Expand Up @@ -1862,7 +1868,7 @@ impl MacroCall {
body,
));

let ty = parser.module.types.insert(
let ty = frontend.module.types.insert(
Type {
name: None,
inner: TypeInner::Vector {
Expand All @@ -1881,7 +1887,7 @@ impl MacroCall {
}
MacroCall::ImageLoad { multi } => {
let comps =
parser.coordinate_components(ctx, args[0], args[1], None, meta, body)?;
frontend.coordinate_components(ctx, args[0], args[1], None, meta, body)?;
let (sample, level) = match (multi, args.get(2)) {
(_, None) => (None, None),
(true, Some(&arg)) => (Some(arg), None),
Expand All @@ -1901,7 +1907,7 @@ impl MacroCall {
}
MacroCall::ImageStore => {
let comps =
parser.coordinate_components(ctx, args[0], args[1], None, meta, body)?;
frontend.coordinate_components(ctx, args[0], args[1], None, meta, body)?;
ctx.emit_restart(body);
body.push(
crate::Statement::ImageStore {
Expand Down Expand Up @@ -2037,7 +2043,7 @@ impl MacroCall {
body,
),
MacroCall::Mod(size) => {
ctx.implicit_splat(parser, &mut args[1], meta, size)?;
ctx.implicit_splat(frontend, &mut args[1], meta, size)?;

// x - y * floor(x / y)

Expand Down Expand Up @@ -2081,7 +2087,7 @@ impl MacroCall {
)
}
MacroCall::Splatted(fun, size, i) => {
ctx.implicit_splat(parser, &mut args[i], meta, size)?;
ctx.implicit_splat(frontend, &mut args[i], meta, size)?;

ctx.add_expression(
Expression::Math {
Expand All @@ -2105,8 +2111,8 @@ impl MacroCall {
body,
),
MacroCall::Clamp(size) => {
ctx.implicit_splat(parser, &mut args[1], meta, size)?;
ctx.implicit_splat(parser, &mut args[2], meta, size)?;
ctx.implicit_splat(frontend, &mut args[1], meta, size)?;
ctx.implicit_splat(frontend, &mut args[2], meta, size)?;

ctx.add_expression(
Expression::Math {
Expand Down Expand Up @@ -2143,8 +2149,8 @@ impl MacroCall {
return Ok(None);
}
MacroCall::SmoothStep { splatted } => {
ctx.implicit_splat(parser, &mut args[0], meta, splatted)?;
ctx.implicit_splat(parser, &mut args[1], meta, splatted)?;
ctx.implicit_splat(frontend, &mut args[0], meta, splatted)?;
ctx.implicit_splat(frontend, &mut args[1], meta, splatted)?;

ctx.add_expression(
Expression::Math {
Expand Down Expand Up @@ -2202,7 +2208,7 @@ fn texture_call(

/// Helper struct for texture calls with the separate components from the vector argument
///
/// Obtained by calling [`coordinate_components`](Parser::coordinate_components)
/// Obtained by calling [`coordinate_components`](Frontend::coordinate_components)
#[derive(Debug)]
struct CoordComponents {
coordinate: Handle<Expression>,
Expand All @@ -2211,7 +2217,7 @@ struct CoordComponents {
used_extra: bool,
}

impl Parser {
impl Frontend {
/// Helper function for texture calls, splits the vector argument into it's components
fn coordinate_components(
&mut self,
Expand Down
Loading

0 comments on commit 67ea8f0

Please sign in to comment.