Skip to content

Commit

Permalink
[glsl-in] Generate Expression::Literal.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblandy committed May 11, 2023
1 parent 412d5aa commit 959c84f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/front/glsl/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{borrow::Cow, fmt};
use super::{builtins::MacroCall, context::ExprPos, Span};
use crate::{
AddressSpace, BinaryOperator, Binding, Constant, Expression, Function, GlobalVariable, Handle,
Interpolation, Sampling, StorageAccess, Type, UnaryOperator,
Interpolation, Literal, Sampling, StorageAccess, Type, UnaryOperator,
};

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -121,7 +121,7 @@ pub enum HirExprKind {
base: Handle<HirExpr>,
field: String,
},
Constant(Handle<Constant>),
Literal(Literal),
Binary {
left: Handle<HirExpr>,
op: BinaryOperator,
Expand Down
38 changes: 10 additions & 28 deletions src/front/glsl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use super::{
};
use crate::{
front::{Emitter, Typifier},
AddressSpace, Arena, BinaryOperator, Block, Constant, Expression, FastHashMap,
FunctionArgument, Handle, LocalVariable, RelationalFunction, ScalarKind, ScalarValue, Span,
Statement, Type, TypeInner, VectorSize,
AddressSpace, Arena, BinaryOperator, Block, Expression, FastHashMap, FunctionArgument, Handle,
Literal, LocalVariable, RelationalFunction, ScalarKind, ScalarValue, Span, Statement, Type,
TypeInner, VectorSize,
};
use std::{convert::TryFrom, ops::Index};

Expand Down Expand Up @@ -560,8 +560,8 @@ impl Context {

frontend.field_selection(self, pos, body, base, field, meta)?
}
HirExprKind::Constant(constant) if pos != ExprPos::Lhs => {
self.add_expression(Expression::Constant(constant), meta, body)
HirExprKind::Literal(literal) if pos != ExprPos::Lhs => {
self.add_expression(Expression::Literal(literal), meta, body)
}
HirExprKind::Binary { left, op, right } if pos != ExprPos::Lhs => {
let (mut left, left_meta) =
Expand Down Expand Up @@ -1253,24 +1253,14 @@ impl Context {
self.add_expression(Expression::Load { pointer }, meta, body)
};

let make_constant_inner = |kind, width| {
let value = match kind {
ScalarKind::Sint => crate::ScalarValue::Sint(1),
ScalarKind::Uint => crate::ScalarValue::Uint(1),
ScalarKind::Float => crate::ScalarValue::Float(1.0),
ScalarKind::Bool => return None,
};

Some(crate::ConstantInner::Scalar { width, value })
};
let res = match *frontend.resolve_type(self, left, meta)? {
TypeInner::Scalar { kind, width } => {
let ty = TypeInner::Scalar { kind, width };
make_constant_inner(kind, width).map(|i| (ty, i, None, None))
Literal::one(kind, width).map(|i| (ty, i, None, None))
}
TypeInner::Vector { size, kind, width } => {
let ty = TypeInner::Vector { size, kind, width };
make_constant_inner(kind, width).map(|i| (ty, i, Some(size), None))
Literal::one(kind, width).map(|i| (ty, i, Some(size), None))
}
TypeInner::Matrix {
columns,
Expand All @@ -1282,12 +1272,12 @@ impl Context {
rows,
width,
};
make_constant_inner(ScalarKind::Float, width)
Literal::one(ScalarKind::Float, width)
.map(|i| (ty, i, Some(rows), Some(columns)))
}
_ => None,
};
let (ty_inner, inner, rows, columns) = match res {
let (ty_inner, literal, rows, columns) = match res {
Some(res) => res,
None => {
frontend.errors.push(Error {
Expand All @@ -1300,15 +1290,7 @@ impl Context {
}
};

let constant_1 = frontend.module.constants.append(
Constant {
name: None,
specialization: None,
inner,
},
Default::default(),
);
let mut right = self.add_expression(Expression::Constant(constant_1), meta, body);
let mut right = self.add_expression(Expression::Literal(literal), meta, body);

// Glsl allows pre/postfixes operations on vectors and matrices, so if the
// target is either of them change the right side of the addition to be splatted
Expand Down
47 changes: 24 additions & 23 deletions src/front/glsl/parser/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
token::{Token, TokenValue},
Error, Frontend, Result, Span,
},
ArraySize, BinaryOperator, Block, Constant, ConstantInner, Handle, ScalarValue, Type,
ArraySize, BinaryOperator, Block, Constant, ConstantInner, Handle, Literal, ScalarValue, Type,
TypeInner, UnaryOperator,
};

Expand All @@ -21,20 +21,30 @@ impl<'source> ParsingContext<'source> {
) -> Result<Handle<HirExpr>> {
let mut token = self.bump(frontend)?;

let (width, value) = match token.value {
TokenValue::IntConstant(int) => (
(int.width / 8) as u8,
let literal = match token.value {
TokenValue::IntConstant(int) => {
if int.width != 32 {
frontend.errors.push(Error {
kind: ErrorKind::SemanticError("Unsupported non-32bit integer".into()),
meta: token.meta,
});
}
if int.signed {
ScalarValue::Sint(int.value as i64)
Literal::I32(int.value as i32)
} else {
ScalarValue::Uint(int.value)
},
),
TokenValue::FloatConstant(float) => (
(float.width / 8) as u8,
ScalarValue::Float(float.value as f64),
),
TokenValue::BoolConstant(value) => (1, ScalarValue::Bool(value)),
Literal::U32(int.value as u32)
}
}
TokenValue::FloatConstant(float) => {
if float.width != 32 {
frontend.errors.push(Error {
kind: ErrorKind::SemanticError("Unsupported floating-point value (expected single-precision floating-point number)".into()),
meta: token.meta,
});
}
Literal::F32(float.value)
}
TokenValue::BoolConstant(value) => Literal::Bool(value),
TokenValue::LeftParen => {
let expr = self.parse_expression(frontend, ctx, stmt, body)?;
let meta = self.expect(frontend, TokenValue::RightParen)?.meta;
Expand All @@ -59,18 +69,9 @@ impl<'source> ParsingContext<'source> {
}
};

let handle = frontend.module.constants.fetch_or_append(
Constant {
name: None,
specialization: None,
inner: ConstantInner::Scalar { width, value },
},
token.meta,
);

Ok(stmt.hir_exprs.append(
HirExpr {
kind: HirExprKind::Constant(handle),
kind: HirExprKind::Literal(literal),
meta: token.meta,
},
Default::default(),
Expand Down
34 changes: 17 additions & 17 deletions tests/out/wgsl/bevy-pbr-frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn getDistanceAttenuation(distanceSquare: f32, inverseRangeSquared: f32) -> f32
let _e68 = attenuation;
_ = distanceSquare_1;
let _e73 = distanceSquare_1;
return ((_e68 * 1.0) / max(_e73, 0.0010000000474974513));
return ((_e68 * 1.0) / max(_e73, 0.001));
}

fn D_GGX(roughness: f32, NoH: f32, h: vec3<f32>) -> f32 {
Expand Down Expand Up @@ -331,13 +331,13 @@ fn fresnel(f0_3: vec3<f32>, LoH: f32) -> vec3<f32> {
f0_4 = f0_3;
LoH_1 = LoH;
_ = f0_4;
_ = vec3<f32>((50.0 * 0.33000001311302185));
_ = vec3<f32>((50.0 * 0.33));
let _e49 = f0_4;
_ = dot(_e49, vec3<f32>((50.0 * 0.33000001311302185)));
_ = dot(_e49, vec3<f32>((50.0 * 0.33)));
_ = f0_4;
_ = vec3<f32>((50.0 * 0.33000001311302185));
_ = vec3<f32>((50.0 * 0.33));
let _e62 = f0_4;
f90_4 = clamp(dot(_e62, vec3<f32>((50.0 * 0.33000001311302185))), 0.0, 1.0);
f90_4 = clamp(dot(_e62, vec3<f32>((50.0 * 0.33))), 0.0, 1.0);
_ = f0_4;
_ = f90_4;
_ = LoH_1;
Expand Down Expand Up @@ -474,8 +474,8 @@ fn EnvBRDFApprox(f0_7: vec3<f32>, perceptual_roughness: f32, NoV_6: f32) -> vec3
f0_8 = f0_7;
perceptual_roughness_1 = perceptual_roughness;
NoV_7 = NoV_6;
c0_ = vec4<f32>(-(1.0), -(0.027499999850988388), -(0.5720000267028809), 0.02199999988079071);
c1_ = vec4<f32>(1.0, 0.042500000447034836, 1.0399999618530273, -(0.03999999910593033));
c0_ = vec4<f32>(-(1.0), -(0.0275), -(0.572), 0.022);
c1_ = vec4<f32>(1.0, 0.0425, 1.04, -(0.04));
let _e62 = perceptual_roughness_1;
let _e64 = c0_;
let _e66 = c1_;
Expand All @@ -484,20 +484,20 @@ fn EnvBRDFApprox(f0_7: vec3<f32>, perceptual_roughness: f32, NoV_6: f32) -> vec3
let _e71 = r;
_ = (_e69.x * _e71.x);
let _e76 = NoV_7;
_ = (-(9.279999732971191) * _e76);
_ = (-(9.28) * _e76);
let _e80 = NoV_7;
_ = exp2((-(9.279999732971191) * _e80));
_ = exp2((-(9.28) * _e80));
let _e83 = r;
let _e85 = r;
let _e90 = NoV_7;
_ = (-(9.279999732971191) * _e90);
_ = (-(9.28) * _e90);
let _e94 = NoV_7;
let _e98 = r;
let _e101 = r;
a004_ = ((min((_e83.x * _e85.x), exp2((-(9.279999732971191) * _e94))) * _e98.x) + _e101.y);
a004_ = ((min((_e83.x * _e85.x), exp2((-(9.28) * _e94))) * _e98.x) + _e101.y);
let _e109 = a004_;
let _e112 = r;
AB = ((vec2<f32>(-(1.0399999618530273), 1.0399999618530273) * vec2<f32>(_e109)) + _e112.zw);
AB = ((vec2<f32>(-(1.04), 1.04) * vec2<f32>(_e109)) + _e112.zw);
let _e116 = f0_8;
let _e117 = AB;
let _e121 = AB;
Expand All @@ -522,7 +522,7 @@ fn perceptualRoughnessToRoughness(perceptualRoughness: f32) -> f32 {
perceptualRoughness_1 = perceptualRoughness;
_ = perceptualRoughness_1;
let _e45 = perceptualRoughness_1;
clampedPerceptualRoughness = clamp(_e45, 0.08900000154972076, 1.0);
clampedPerceptualRoughness = clamp(_e45, 0.089, 1.0);
let _e50 = clampedPerceptualRoughness;
let _e51 = clampedPerceptualRoughness;
return (_e50 * _e51);
Expand Down Expand Up @@ -592,9 +592,9 @@ fn luminance(v_1: vec3<f32>) -> f32 {
_ = (&global_7.emissive);
v_2 = v_1;
_ = v_2;
_ = vec3<f32>(0.2125999927520752, 0.7152000069618225, 0.0722000002861023);
_ = vec3<f32>(0.2126, 0.7152, 0.0722);
let _e47 = v_2;
return dot(_e47, vec3<f32>(0.2125999927520752, 0.7152000069618225, 0.0722000002861023));
return dot(_e47, vec3<f32>(0.2126, 0.7152, 0.0722));
}

fn change_luminance(c_in: vec3<f32>, l_out: f32) -> vec3<f32> {
Expand Down Expand Up @@ -1177,13 +1177,13 @@ fn main_1() {
_ = V_3;
let _e183 = N_2;
let _e184 = V_3;
NdotV_4 = max(dot(_e183, _e184), 0.0010000000474974513);
NdotV_4 = max(dot(_e183, _e184), 0.001);
let _e190 = global_6.reflectance;
let _e192 = global_6.reflectance;
let _e195 = metallic;
let _e199 = output_color;
let _e201 = metallic;
F0_4 = (vec3<f32>((((0.1599999964237213 * _e190) * _e192) * (1.0 - _e195))) + (_e199.xyz * vec3<f32>(_e201)));
F0_4 = (vec3<f32>((((0.16 * _e190) * _e192) * (1.0 - _e195))) + (_e199.xyz * vec3<f32>(_e201)));
let _e206 = output_color;
let _e209 = metallic;
diffuseColor_4 = (_e206.xyz * vec3<f32>((1.0 - _e209)));
Expand Down

0 comments on commit 959c84f

Please sign in to comment.