Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/typegpu/src/tgsl/forOfUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ export function getRangeSnippets(
const { value, dataType } = iterableSnippet;

if (isTgpuRange(value)) {
const dataType = value.start >= 0 && value.step >= 0 ? u32 : i32;

return {
start: snip(value.start, i32, 'constant'),
end: snip(value.end, i32, 'constant'),
step: snip(value.step, i32, 'constant'),
start: snip(value.start, dataType, 'constant'),
end: snip(value.end, dataType, 'constant'),
step: snip(value.step, dataType, 'constant'),
comparison: value.step < 0 ? '>' : '<',
Comment thread
cieplypolar marked this conversation as resolved.
Outdated
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/typegpu/src/tgsl/wgslGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ ${this.ctx.pre}else ${alternate}`;

if (isTgpuRange(iterableSnippet.value)) {
bodyStr = this._block(blockified, {
[originalLoopVarName]: snip(index, u32, 'runtime'),
[originalLoopVarName]: snip(index, range.start.dataType, 'runtime'), // range.start, .end , .step have the same dataType
});
} else {
this.ctx.indent();
Expand Down
2 changes: 1 addition & 1 deletion packages/typegpu/tests/std/range.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('on the GPU', () => {
expect(tgpu.resolve([main])).toMatchInlineSnapshot(`
"fn main() -> f32 {
var result = 0f;
for (var i = 0i; i < 8i; i += 2i) {
for (var i = 0u; i < 8u; i += 2u) {
result += f32(i);
}
for (var i = 10i; i > -10i; i += -1i) {
Expand Down
24 changes: 24 additions & 0 deletions packages/typegpu/tests/tgsl/wgslGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,30 @@ describe('wgslGenerator', () => {
`);
});

it('handles "for ... of ..." over `std.range`', ({ root }) => {
const testFn = tgpu.fn(
[],
d.f32,
)(() => {
'use gpu';
for (const bounce of std.range(12)) {
const test = d.u32(2) + bounce;
return d.f32(test);
}
return 0;
});

expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
"fn testFn() -> f32 {
for (var i = 0u; i < 12u; i += 1u) {
let test = (2u + i);
return f32(test);
}
return 0f;
}"
`);
});

it('creates correct resources for lazy values and slots', () => {
const testFn = tgpu.fn([], d.vec4u)(() => lazyV4u.$);

Expand Down
Loading