Shader: Add support for numerical fors
This commit is contained in:
@@ -109,6 +109,63 @@ fn main()
|
||||
)");
|
||||
}
|
||||
}
|
||||
|
||||
WHEN("using [unroll] attribute on numerical for")
|
||||
{
|
||||
std::string_view sourceCode = R"(
|
||||
const LightCount = 3;
|
||||
|
||||
[layout(std140)]
|
||||
struct Light
|
||||
{
|
||||
color: vec4<f32>
|
||||
}
|
||||
|
||||
[layout(std140)]
|
||||
struct LightData
|
||||
{
|
||||
lights: [Light; LightCount]
|
||||
}
|
||||
|
||||
external
|
||||
{
|
||||
[set(0), binding(0)] data: uniform<LightData>
|
||||
}
|
||||
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let color = (0.0).xxxx;
|
||||
|
||||
[unroll]
|
||||
for i in 0 -> 10 : 2
|
||||
{
|
||||
color += data.lights[i].color;
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
Nz::ShaderAst::StatementPtr shader;
|
||||
REQUIRE_NOTHROW(shader = Nz::ShaderLang::Parse(sourceCode));
|
||||
|
||||
ExpectOutput(*shader, {}, R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let color: vec4<f32> = (0.000000).xxxx;
|
||||
let i: i32 = 0;
|
||||
color += data.lights[i].color;
|
||||
let i: i32 = 2;
|
||||
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;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
WHEN("using [unroll] attribute on for-each")
|
||||
{
|
||||
|
||||
@@ -88,6 +88,160 @@ OpStore
|
||||
OpBranch
|
||||
OpLabel
|
||||
OpReturn
|
||||
OpFunctionEnd)");
|
||||
}
|
||||
|
||||
WHEN("using a for range")
|
||||
{
|
||||
std::string_view nzslSource = R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let x = 0;
|
||||
for v in 0 -> 10
|
||||
{
|
||||
x += v;
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
|
||||
|
||||
|
||||
ExpectGLSL(*shader, R"(
|
||||
void main()
|
||||
{
|
||||
int x = 0;
|
||||
int v = 0;
|
||||
int to = 10;
|
||||
while (v < to)
|
||||
{
|
||||
x += v;
|
||||
v += 1;
|
||||
}
|
||||
|
||||
}
|
||||
)");
|
||||
|
||||
ExpectNZSL(*shader, R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let x: i32 = 0;
|
||||
for v in 0 -> 10
|
||||
{
|
||||
x += v;
|
||||
}
|
||||
|
||||
}
|
||||
)");
|
||||
|
||||
ExpectSpirV(*shader, R"(
|
||||
OpFunction
|
||||
OpLabel
|
||||
OpVariable
|
||||
OpVariable
|
||||
OpVariable
|
||||
OpStore
|
||||
OpStore
|
||||
OpStore
|
||||
OpBranch
|
||||
OpLabel
|
||||
OpLoad
|
||||
OpLoad
|
||||
OpSLessThan
|
||||
OpLoopMerge
|
||||
OpBranchConditional
|
||||
OpLabel
|
||||
OpLoad
|
||||
OpLoad
|
||||
OpIAdd
|
||||
OpStore
|
||||
OpLoad
|
||||
OpIAdd
|
||||
OpStore
|
||||
OpBranch
|
||||
OpLabel
|
||||
OpReturn
|
||||
OpFunctionEnd)");
|
||||
}
|
||||
|
||||
WHEN("using a for range with step")
|
||||
{
|
||||
std::string_view nzslSource = R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let x = 0;
|
||||
for v in 0 -> 10 : 2
|
||||
{
|
||||
x += v;
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
|
||||
|
||||
|
||||
ExpectGLSL(*shader, R"(
|
||||
void main()
|
||||
{
|
||||
int x = 0;
|
||||
int v = 0;
|
||||
int to = 10;
|
||||
int step = 2;
|
||||
while (v < to)
|
||||
{
|
||||
x += v;
|
||||
v += step;
|
||||
}
|
||||
|
||||
}
|
||||
)");
|
||||
|
||||
ExpectNZSL(*shader, R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let x: i32 = 0;
|
||||
for v in 0 -> 10 : 2
|
||||
{
|
||||
x += v;
|
||||
}
|
||||
|
||||
}
|
||||
)");
|
||||
|
||||
ExpectSpirV(*shader, R"(
|
||||
OpFunction
|
||||
OpLabel
|
||||
OpVariable
|
||||
OpVariable
|
||||
OpVariable
|
||||
OpVariable
|
||||
OpStore
|
||||
OpStore
|
||||
OpStore
|
||||
OpStore
|
||||
OpBranch
|
||||
OpLabel
|
||||
OpLoad
|
||||
OpLoad
|
||||
OpSLessThan
|
||||
OpLoopMerge
|
||||
OpBranchConditional
|
||||
OpLabel
|
||||
OpLoad
|
||||
OpLoad
|
||||
OpIAdd
|
||||
OpStore
|
||||
OpLoad
|
||||
OpLoad
|
||||
OpIAdd
|
||||
OpStore
|
||||
OpBranch
|
||||
OpLabel
|
||||
OpReturn
|
||||
OpFunctionEnd)");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user