[glsl-in] set initializer of local variables

This commit is contained in:
teoxoy authored and Teodor Tanasoaia committed 2023-10-12 13:13:26 +02:00
1 parent fe7bc01d27
commit 0e4ac2a98c
19 files changed
+127 -116

No files matched your search

+1
View File
@@ -395,6 +395,7 @@ pub struct DeclarationContext<'ctx, 'qualifiers, 'a> {
qualifiers: TypeQualifiers<'qualifiers>,
/// Indicates a global declaration
external: bool,
is_inside_loop: bool,
ctx: &'ctx mut Context<'a>,
}
+16 -8
View File
@@ -75,7 +75,7 @@ impl<'source> ParsingContext<'source> {
global_ctx: &mut Context,
) -> Result<()> {
if self
.parse_declaration(frontend, global_ctx, true)?
.parse_declaration(frontend, global_ctx, true, false)?
.is_none()
{
let token = self.bump(frontend)?;
@@ -246,18 +246,23 @@ impl<'source> ParsingContext<'source> {
})
.transpose()?;
let maybe_const_expr = if is_global_const {
init
let (decl_initializer, late_initializer) = if is_global_const {
(init, None)
} else if ctx.external {
init.and_then(|expr| ctx.ctx.lift_up_const_expression(expr).ok())
let decl_initializer =
init.and_then(|expr| ctx.ctx.lift_up_const_expression(expr).ok());
(decl_initializer, None)
} else {
None
// init.filter(|expr| ctx.ctx.expressions.is_const(*expr))
let decl_initializer = init.filter(|expr| ctx.ctx.expressions.is_const(*expr));
let late_initializer = (decl_initializer.is_none() || ctx.is_inside_loop)
.then_some(init)
.flatten();
(decl_initializer, late_initializer)
};
let pointer = ctx.add_var(frontend, ty, name, maybe_const_expr, meta)?;
let pointer = ctx.add_var(frontend, ty, name, decl_initializer, meta)?;
if let Some(value) = init.filter(|_| maybe_const_expr.is_none()) {
if let Some(value) = late_initializer {
ctx.ctx.emit_restart();
ctx.ctx.body.push(Statement::Store { pointer, value }, meta);
}
@@ -287,6 +292,7 @@ impl<'source> ParsingContext<'source> {
frontend: &mut Frontend,
ctx: &mut Context,
external: bool,
is_inside_loop: bool,
) -> Result<Option<Span>> {
//declaration:
// function_prototype SEMICOLON
@@ -343,6 +349,7 @@ impl<'source> ParsingContext<'source> {
frontend,
&mut context,
&mut None,
false,
)?;
frontend.add_function(context, name, result, meta);
@@ -385,6 +392,7 @@ impl<'source> ParsingContext<'source> {
let mut ctx = DeclarationContext {
qualifiers,
external,
is_inside_loop,
ctx,
};
+28 -12
View File
@@ -41,10 +41,11 @@ impl<'source> ParsingContext<'source> {
frontend: &mut Frontend,
ctx: &mut Context,
terminator: &mut Option<usize>,
is_inside_loop: bool,
) -> Result<Option<Span>> {
// Type qualifiers always identify a declaration statement
if self.peek_type_qualifier(frontend) {
return self.parse_declaration(frontend, ctx, false);
return self.parse_declaration(frontend, ctx, false, is_inside_loop);
}
// Type names can identify either declaration statements or type constructors
@@ -60,7 +61,7 @@ impl<'source> ParsingContext<'source> {
self.backtrack(token)?;
if declaration {
return self.parse_declaration(frontend, ctx, false);
return self.parse_declaration(frontend, ctx, false, is_inside_loop);
}
}
@@ -132,7 +133,9 @@ impl<'source> ParsingContext<'source> {
self.expect(frontend, TokenValue::RightParen)?;
let accept = ctx.new_body(|ctx| {
if let Some(more_meta) = self.parse_statement(frontend, ctx, &mut None)? {
if let Some(more_meta) =
self.parse_statement(frontend, ctx, &mut None, is_inside_loop)?
{
meta.subsume(more_meta);
}
Ok(())
@@ -140,7 +143,9 @@ impl<'source> ParsingContext<'source> {
let reject = ctx.new_body(|ctx| {
if self.bump_if(frontend, TokenValue::Else).is_some() {
if let Some(more_meta) = self.parse_statement(frontend, ctx, &mut None)? {
if let Some(more_meta) =
self.parse_statement(frontend, ctx, &mut None, is_inside_loop)?
{
meta.subsume(more_meta);
}
}
@@ -252,7 +257,12 @@ impl<'source> ParsingContext<'source> {
break
}
_ => {
self.parse_statement(frontend, ctx, &mut case_terminator)?;
self.parse_statement(
frontend,
ctx,
&mut case_terminator,
is_inside_loop,
)?;
}
}
}
@@ -347,7 +357,7 @@ impl<'source> ParsingContext<'source> {
meta.subsume(expr_meta);
if let Some(body_meta) = self.parse_statement(frontend, ctx, &mut None)? {
if let Some(body_meta) = self.parse_statement(frontend, ctx, &mut None, true)? {
meta.subsume(body_meta);
}
Ok(())
@@ -369,7 +379,7 @@ impl<'source> ParsingContext<'source> {
let loop_body = ctx.new_body(|ctx| {
let mut terminator = None;
self.parse_statement(frontend, ctx, &mut terminator)?;
self.parse_statement(frontend, ctx, &mut terminator, true)?;
let mut stmt = ctx.stmt_ctx();
@@ -425,7 +435,7 @@ impl<'source> ParsingContext<'source> {
if self.bump_if(frontend, TokenValue::Semicolon).is_none() {
if self.peek_type_name(frontend) || self.peek_type_qualifier(frontend) {
self.parse_declaration(frontend, ctx, false)?;
self.parse_declaration(frontend, ctx, false, false)?;
} else {
let mut stmt = ctx.stmt_ctx();
let expr = self.parse_expression(frontend, ctx, &mut stmt)?;
@@ -508,7 +518,7 @@ impl<'source> ParsingContext<'source> {
meta.subsume(self.expect(frontend, TokenValue::RightParen)?.meta);
let loop_body = ctx.with_body(loop_body, |ctx| {
if let Some(stmt_meta) = self.parse_statement(frontend, ctx, &mut None)? {
if let Some(stmt_meta) = self.parse_statement(frontend, ctx, &mut None, true)? {
meta.subsume(stmt_meta);
}
Ok(())
@@ -533,8 +543,13 @@ impl<'source> ParsingContext<'source> {
let mut block_terminator = None;
let block = ctx.new_body(|ctx| {
let block_meta =
self.parse_compound_statement(meta, frontend, ctx, &mut block_terminator)?;
let block_meta = self.parse_compound_statement(
meta,
frontend,
ctx,
&mut block_terminator,
is_inside_loop,
)?;
meta.subsume(block_meta);
Ok(())
})?;
@@ -568,6 +583,7 @@ impl<'source> ParsingContext<'source> {
frontend: &mut Frontend,
ctx: &mut Context,
terminator: &mut Option<usize>,
is_inside_loop: bool,
) -> Result<Span> {
ctx.symbol_table.push_scope();
@@ -580,7 +596,7 @@ impl<'source> ParsingContext<'source> {
break;
}
let stmt = self.parse_statement(frontend, ctx, terminator)?;
let stmt = self.parse_statement(frontend, ctx, terminator, is_inside_loop)?;
if let Some(stmt_meta) = stmt {
meta.subsume(stmt_meta);
@@ -0,0 +1,7 @@
void main() {
vec4 sum = vec4(0);
for (int i = 0; i < 4; i++) {
vec4 a = vec4(1);
sum += a;
}
}
+1 -2
View File
@@ -8,10 +8,9 @@ var<private> gl_GlobalInvocationID: vec3<u32>;
fn collatz_iterations(n: u32) -> u32 {
var n_1: u32;
var i: u32;
var i: u32 = 0u;
n_1 = n;
i = 0u;
loop {
let _e7 = n_1;
if !((_e7 != 1u)) {
+1 -2
View File
@@ -1,7 +1,6 @@
fn main_1() {
var a: f32;
var a: f32 = 1.0;
a = 1.0;
return;
}
+1 -3
View File
@@ -1,8 +1,6 @@
fn main_1() {
var a: mat4x4<f32>;
var a: mat4x4<f32> = mat4x4<f32>(vec4<f32>(1.0, 0.0, 0.0, 0.0), vec4<f32>(0.0, 1.0, 0.0, 0.0), vec4<f32>(0.0, 0.0, 1.0, 0.0), vec4<f32>(0.0, 0.0, 0.0, 1.0));
a = mat4x4<f32>(vec4<f32>(1.0, 0.0, 0.0, 0.0), vec4<f32>(0.0, 1.0, 0.0, 0.0), vec4<f32>(0.0, 0.0, 1.0, 0.0), vec4<f32>(0.0, 0.0, 0.0, 1.0));
return;
}
@fragment
@@ -1,7 +1,6 @@
fn main_1() {
var a: vec4<f32>;
var a: vec4<f32> = vec4(1.0);
a = vec4(1.0);
a.x = 2.0;
return;
}
+1 -2
View File
@@ -1,7 +1,6 @@
fn main_1() {
var i: i32;
var i: i32 = 0;
i = 0;
loop {
let _e2 = i;
if !((_e2 < 1)) {
+6 -12
View File
@@ -328,8 +328,8 @@ fn EnvBRDFApprox(f0_7: vec3<f32>, perceptual_roughness: f32, NoV_6: f32) -> vec3
var f0_8: vec3<f32>;
var perceptual_roughness_1: f32;
var NoV_7: f32;
var c0_: vec4<f32>;
var c1_: vec4<f32>;
var c0_: vec4<f32> = vec4<f32>(-1.0, -0.0275, -0.572, 0.022);
var c1_: vec4<f32> = vec4<f32>(1.0, 0.0425, 1.04, -0.04);
var r: vec4<f32>;
var a004_: f32;
var AB: vec2<f32>;
@@ -337,8 +337,6 @@ 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.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_;
@@ -638,7 +636,7 @@ fn dir_light(light_2: DirectionalLight, roughness_10: f32, NdotV_2: f32, normal:
var NoH_5: f32;
var LoH_7: f32;
var diffuse_1: vec3<f32>;
var specularIntensity_3: f32;
var specularIntensity_3: f32 = 1.0;
var specular_2: vec3<f32>;
light_3 = light_2;
@@ -678,7 +676,6 @@ fn dir_light(light_2: DirectionalLight, roughness_10: f32, NdotV_2: f32, normal:
let _e124 = LoH_7;
let _e125 = Fd_Burley(_e121, _e122, _e123, _e124);
diffuse_1 = (_e116 * _e125);
specularIntensity_3 = 1.0;
let _e138 = F0_3;
let _e139 = roughness_11;
let _e140 = half_vector;
@@ -716,9 +713,9 @@ fn main_1() {
var F0_4: vec3<f32>;
var diffuseColor_4: vec3<f32>;
var R_4: vec3<f32>;
var light_accum: vec3<f32>;
var i: i32;
var i_1: i32;
var light_accum: vec3<f32> = vec3(0.0);
var i: i32 = 0;
var i_1: i32 = 0;
var diffuse_ambient: vec3<f32>;
var specular_ambient: vec3<f32>;
@@ -825,8 +822,6 @@ fn main_1() {
let _e217 = V_3;
let _e219 = N_2;
R_4 = reflect(-(_e217), _e219);
light_accum = vec3(0.0);
i = 0;
loop {
let _e227 = i;
let _e228 = global_2.NumLights;
@@ -854,7 +849,6 @@ fn main_1() {
i = (_e236 + 1);
}
}
i_1 = 0;
loop {
let _e264 = i_1;
let _e265 = global_2.NumLights;
+10 -20
View File
@@ -1,25 +1,15 @@
fn main_1() {
var i: i32;
var i2_: vec2<i32>;
var i3_: vec3<i32>;
var i4_: vec4<i32>;
var u: u32;
var u2_: vec2<u32>;
var u3_: vec3<u32>;
var u4_: vec4<u32>;
var f2_: vec2<f32>;
var f4_: vec4<f32>;
var i: i32 = 0;
var i2_: vec2<i32> = vec2(0);
var i3_: vec3<i32> = vec3(0);
var i4_: vec4<i32> = vec4(0);
var u: u32 = 0u;
var u2_: vec2<u32> = vec2(0u);
var u3_: vec3<u32> = vec3(0u);
var u4_: vec4<u32> = vec4(0u);
var f2_: vec2<f32> = vec2(0.0);
var f4_: vec4<f32> = vec4(0.0);
i = 0;
i2_ = vec2(0);
i3_ = vec3(0);
i4_ = vec4(0);
u = 0u;
u2_ = vec2(0u);
u3_ = vec3(0u);
u4_ = vec4(0u);
f2_ = vec2(0.0);
f4_ = vec4(0.0);
let _e33 = f4_;
u = pack4x8snorm(_e33);
let _e36 = f4_;
+2 -4
View File
@@ -8,11 +8,9 @@ const NUM_VECS: i32 = 42;
var<uniform> global: Data;
fn function() -> vec4<f32> {
var sum: vec4<f32>;
var i: i32;
var sum: vec4<f32> = vec4(0.0);
var i: i32 = 0;
sum = vec4(0.0);
i = 0;
loop {
let _e9 = i;
if !((_e9 < NUM_VECS)) {
+2 -4
View File
@@ -38,14 +38,12 @@ var<private> array_2d: array<array<f32, 2>, 2>;
var<private> array_toomanyd: array<array<array<array<array<array<array<f32, 2>, 2>, 2>, 2>, 2>, 2>, 2>;
fn main_1() {
var positions: array<vec3<f32>, 2>;
var strct: TestStruct;
var positions: array<vec3<f32>, 2> = array<vec3<f32>, 2>(vec3<f32>(-1.0, 1.0, 0.0), vec3<f32>(-1.0, -1.0, 0.0));
var strct: TestStruct = TestStruct(1.0, 2.0);
var from_input_array: vec4<f32>;
var a_1: f32;
var b: f32;
positions = array<vec3<f32>, 2>(vec3<f32>(-1.0, 1.0, 0.0), vec3<f32>(-1.0, -1.0, 0.0));
strct = TestStruct(1.0, 2.0);
let _e35 = in_array_2[1];
from_input_array = _e35;
let _e41 = array_2d[0][0];
+5 -14
View File
@@ -280,24 +280,18 @@ fn testUnaryOpMat(a_16: mat3x3<f32>) {
}
fn testStructConstructor() {
var tree: BST;
var tree: BST = BST(1);
tree = BST(1);
return;
}
fn testNonScalarToScalarConstructor() {
var f: f32;
var f: f32 = 1.0;
f = 1.0;
return;
}
fn testArrayConstructor() {
var tree_1: array<f32, 1>;
var tree_1: array<f32, 1> = array<f32, 1>(0.0);
tree_1 = array<f32, 1>(0.0);
return;
}
fn testFreestandingConstructor() {
@@ -305,10 +299,9 @@ fn testFreestandingConstructor() {
}
fn testNonImplicitCastVectorCast() {
var a_18: u32;
var a_18: u32 = 1u;
var b_16: vec4<i32>;
a_18 = 1u;
let _e3 = a_18;
b_16 = vec4(i32(_e3));
return;
@@ -393,11 +386,9 @@ fn testLength() {
fn testConstantLength(a_24: array<f32, 4>) {
var a_25: array<f32, 4>;
var len_1: i32;
var len_1: i32 = 4;
a_25 = a_24;
len_1 = 4;
return;
}
fn indexConstantNonConstantIndex(i: i32) {
@@ -0,0 +1,30 @@
fn main_1() {
var sum: vec4<f32> = vec4(0.0);
var i: i32 = 0;
var a: vec4<f32> = vec4(1.0);
loop {
let _e6 = i;
if !((_e6 < 4)) {
break;
}
{
let _e15 = vec4(1.0);
a = _e15;
let _e17 = sum;
let _e18 = a;
sum = (_e17 + _e18);
}
continuing {
let _e10 = i;
i = (_e10 + 1);
}
}
return;
}
@compute @workgroup_size(1, 1, 1)
fn main() {
main_1();
return;
}
+8 -17
View File
@@ -1,22 +1,13 @@
fn main_1() {
var splat: mat2x2<f32>;
var normal: mat2x2<f32>;
var from_matrix: mat2x4<f32>;
var a: mat2x2<f32>;
var b: mat2x2<f32>;
var c: mat3x3<f32>;
var d: mat3x3<f32>;
var e: mat4x4<f32>;
var splat: mat2x2<f32> = mat2x2<f32>(vec2<f32>(1.0, 0.0), vec2<f32>(0.0, 1.0));
var normal: mat2x2<f32> = mat2x2<f32>(vec2<f32>(1.0, 1.0), vec2<f32>(2.0, 2.0));
var from_matrix: mat2x4<f32> = mat2x4<f32>(vec4<f32>(1.0, 0.0, 0.0, 0.0), vec4<f32>(0.0, 1.0, 0.0, 0.0));
var a: mat2x2<f32> = mat2x2<f32>(vec2<f32>(1.0, 2.0), vec2<f32>(3.0, 4.0));
var b: mat2x2<f32> = mat2x2<f32>(vec2<f32>(1.0, 2.0), vec2<f32>(3.0, 4.0));
var c: mat3x3<f32> = mat3x3<f32>(vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(1.0, 1.0, 1.0), vec3<f32>(1.0, 1.0, 1.0));
var d: mat3x3<f32> = mat3x3<f32>(vec3<f32>(2.0, 2.0, 1.0), vec3<f32>(1.0, 1.0, 1.0), vec3<f32>(1.0, 1.0, 1.0));
var e: mat4x4<f32> = mat4x4<f32>(vec4<f32>(2.0, 2.0, 1.0, 1.0), vec4<f32>(1.0, 1.0, 2.0, 2.0), vec4<f32>(1.0, 1.0, 1.0, 1.0), vec4<f32>(1.0, 1.0, 1.0, 1.0));
splat = mat2x2<f32>(vec2<f32>(1.0, 0.0), vec2<f32>(0.0, 1.0));
normal = mat2x2<f32>(vec2<f32>(1.0, 1.0), vec2<f32>(2.0, 2.0));
from_matrix = mat2x4<f32>(vec4<f32>(1.0, 0.0, 0.0, 0.0), vec4<f32>(0.0, 1.0, 0.0, 0.0));
a = mat2x2<f32>(vec2<f32>(1.0, 2.0), vec2<f32>(3.0, 4.0));
b = mat2x2<f32>(vec2<f32>(1.0, 2.0), vec2<f32>(3.0, 4.0));
c = mat3x3<f32>(vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(1.0, 1.0, 1.0), vec3<f32>(1.0, 1.0, 1.0));
d = mat3x3<f32>(vec3<f32>(2.0, 2.0, 1.0), vec3<f32>(1.0, 1.0, 1.0), vec3<f32>(1.0, 1.0, 1.0));
e = mat4x4<f32>(vec4<f32>(2.0, 2.0, 1.0, 1.0), vec4<f32>(1.0, 1.0, 2.0, 2.0), vec4<f32>(1.0, 1.0, 1.0, 1.0), vec4<f32>(1.0, 1.0, 1.0, 1.0));
return;
}
@fragment
+3 -6
View File
@@ -1,8 +1,8 @@
fn main_1() {
var a: vec4<f32>;
var b: vec4<f32>;
var a: vec4<f32> = vec4(1.0);
var b: vec4<f32> = vec4(2.0);
var m: mat4x4<f32>;
var i: i32;
var i: i32 = 5;
var ceilOut: vec4<f32>;
var roundOut: vec4<f32>;
var floorOut: vec4<f32>;
@@ -48,14 +48,11 @@ fn main_1() {
var smoothStepVector: vec4<f32>;
var smoothStepMixed: vec4<f32>;
a = vec4(1.0);
b = vec4(2.0);
let _e6 = a;
let _e7 = b;
let _e8 = a;
let _e9 = b;
m = mat4x4<f32>(vec4<f32>(_e6.x, _e6.y, _e6.z, _e6.w), vec4<f32>(_e7.x, _e7.y, _e7.z, _e7.w), vec4<f32>(_e8.x, _e8.y, _e8.z, _e8.w), vec4<f32>(_e9.x, _e9.y, _e9.z, _e9.w));
i = 5;
let _e35 = a;
ceilOut = ceil(_e35);
let _e39 = a;
+3 -6
View File
@@ -1,12 +1,11 @@
fn main_1() {
var scalar_target: i32;
var scalar: i32;
var scalar: i32 = 1;
var vec_target: vec2<u32>;
var vec: vec2<u32>;
var vec: vec2<u32> = vec2(1u);
var mat_target: mat4x3<f32>;
var mat: mat4x3<f32>;
var mat: mat4x3<f32> = mat4x3<f32>(vec3<f32>(1.0, 0.0, 0.0), vec3<f32>(0.0, 1.0, 0.0), vec3<f32>(0.0, 0.0, 1.0), vec3<f32>(0.0, 0.0, 0.0));
scalar = 1;
let _e3 = scalar;
scalar = (_e3 + 1);
scalar_target = _e3;
@@ -14,7 +13,6 @@ fn main_1() {
let _e8 = (_e6 - 1);
scalar = _e8;
scalar_target = _e8;
vec = vec2(1u);
let _e14 = vec;
vec = (_e14 - vec2(1u));
vec_target = _e14;
@@ -22,7 +20,6 @@ fn main_1() {
let _e21 = (_e18 + vec2(1u));
vec = _e21;
vec_target = _e21;
mat = mat4x3<f32>(vec3<f32>(1.0, 0.0, 0.0), vec3<f32>(0.0, 1.0, 0.0), vec3<f32>(0.0, 0.0, 1.0), vec3<f32>(0.0, 0.0, 0.0));
let _e32 = mat;
let _e34 = vec3(1.0);
mat = (_e32 + mat4x3<f32>(_e34, _e34, _e34, _e34));
+1 -2
View File
@@ -1,9 +1,8 @@
fn CalcShadowPCF1_(T_P_t_TextureDepth: texture_depth_2d, S_P_t_TextureDepth: sampler_comparison, t_ProjCoord: vec3<f32>) -> f32 {
var t_ProjCoord_1: vec3<f32>;
var t_Res: f32;
var t_Res: f32 = 0.0;
t_ProjCoord_1 = t_ProjCoord;
t_Res = 0.0;
let _e6 = t_Res;
let _e7 = t_ProjCoord_1;
let _e9 = t_ProjCoord_1;