Shader: Fix scoping on unrolled for-each

This commit is contained in:
SirLynix 2022-04-14 12:46:29 +02:00
parent b52a380839
commit 02ffbcc50b
2 changed files with 53 additions and 18 deletions

View File

@ -1428,6 +1428,7 @@ namespace Nz::ShaderAst
} }
auto multi = std::make_unique<MultiStatement>(); auto multi = std::make_unique<MultiStatement>();
multi->sourceLocation = node.sourceLocation;
auto Unroll = [&](auto dummy) auto Unroll = [&](auto dummy)
{ {
@ -1442,6 +1443,7 @@ namespace Nz::ShaderAst
PushScope(); PushScope();
auto innerMulti = std::make_unique<MultiStatement>(); auto innerMulti = std::make_unique<MultiStatement>();
innerMulti->sourceLocation = node.sourceLocation;
auto constant = ShaderBuilder::Constant(counter); auto constant = ShaderBuilder::Constant(counter);
constant->sourceLocation = node.sourceLocation; constant->sourceLocation = node.sourceLocation;
@ -1603,14 +1605,23 @@ namespace Nz::ShaderAst
for (UInt32 i = 0; i < arrayType.length; ++i) for (UInt32 i = 0; i < arrayType.length; ++i)
{ {
PushScope();
auto innerMulti = std::make_unique<MultiStatement>();
innerMulti->sourceLocation = node.sourceLocation;
auto accessIndex = ShaderBuilder::AccessIndex(CloneExpression(expr), ShaderBuilder::Constant(i)); auto accessIndex = ShaderBuilder::AccessIndex(CloneExpression(expr), ShaderBuilder::Constant(i));
Validate(*accessIndex); Validate(*accessIndex);
auto elementVariable = ShaderBuilder::DeclareVariable(node.varName, std::move(accessIndex)); auto elementVariable = ShaderBuilder::DeclareVariable(node.varName, std::move(accessIndex));
Validate(*elementVariable); Validate(*elementVariable);
multi->statements.emplace_back(std::move(elementVariable)); innerMulti->statements.emplace_back(std::move(elementVariable));
multi->statements.emplace_back(Unscope(CloneStatement(node.statement))); innerMulti->statements.emplace_back(Unscope(CloneStatement(node.statement)));
multi->statements.emplace_back(ShaderBuilder::Scoped(std::move(innerMulti)));
PopScope();
} }
} }

View File

@ -159,16 +159,31 @@ fn main()
fn main() fn main()
{ {
let color: vec4[f32] = (0.000000).xxxx; let color: vec4[f32] = (0.000000).xxxx;
let i: i32 = 0; {
color += data.lights[i].color; let i: i32 = 0;
let i: i32 = 2; color += data.lights[i].color;
color += data.lights[i].color; }
let i: i32 = 4;
color += data.lights[i].color; {
let i: i32 = 6; let i: i32 = 2;
color += data.lights[i].color; color += data.lights[i].color;
let i: i32 = 8; }
color += data.lights[i].color;
{
let i: i32 = 4;
color += data.lights[i].color;
}
{
let i: i32 = 6;
color += data.lights[i].color;
}
{
let i: i32 = 8;
color += data.lights[i].color;
}
} }
)"); )");
} }
@ -219,12 +234,21 @@ fn main()
fn main() fn main()
{ {
let color: vec4[f32] = (0.000000).xxxx; let color: vec4[f32] = (0.000000).xxxx;
let light: Light = data.lights[0]; {
color += light.color; let light: Light = data.lights[0];
let light: Light = data.lights[1]; color += light.color;
color += light.color; }
let light: Light = data.lights[2];
color += light.color; {
let light: Light = data.lights[1];
color += light.color;
}
{
let light: Light = data.lights[2];
color += light.color;
}
} }
)"); )");
} }