Skip to content

Commit 7d301a0

Browse files
authored
fix: Deref implicit pointers in shell-less entry function output (#2285)
1 parent 1c8de59 commit 7d301a0

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,11 @@ ${this.ctx.pre}}`;
716716
stitch`Property ${key} in object literal has a value of unknown type: '${expr}'`,
717717
);
718718
}
719-
accessed = structType.provideProp(key, concretize(expr.dataType));
719+
// Taking care of abstract numerics and implicit pointers
720+
accessed = structType.provideProp(
721+
key,
722+
unptr(concretize(expr.dataType)) as wgsl.BaseData,
723+
);
720724
}
721725

722726
return [accessed.prop, expr];

packages/typegpu/tests/renderPipeline.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,66 @@ describe('root.createRenderPipeline', () => {
14851485
`);
14861486
});
14871487

1488+
it('derefs implicit pointers in the vertex and fragment outputs', ({ root }) => {
1489+
const pipeline = root.createRenderPipeline({
1490+
vertex: ({ $vertexIndex }) => {
1491+
'use gpu';
1492+
const pos = [d.vec2f(0.0, 0.5), d.vec2f(-0.5, -0.5), d.vec2f(0.5, -0.5)];
1493+
const local = pos[$vertexIndex]!;
1494+
return {
1495+
$position: d.vec4f(local, 0, 1),
1496+
local,
1497+
};
1498+
},
1499+
fragment: () => {
1500+
'use gpu';
1501+
const color = d.vec4f(1, 0, 0, 1);
1502+
const alias = color;
1503+
return {
1504+
color: alias,
1505+
};
1506+
},
1507+
targets: { color: { format: 'rgba8unorm' } },
1508+
});
1509+
1510+
expectTypeOf(pipeline).toEqualTypeOf<TgpuRenderPipeline<{ color: d.Vec4f }>>();
1511+
1512+
const wgsl = tgpu.resolve([pipeline]);
1513+
// vertex
1514+
expect(wgsl).toContain('local: vec2f');
1515+
expect(wgsl).not.toContain('local: ptr<function, vec2f>');
1516+
// fragment
1517+
expect(wgsl).toContain('color: vec4f');
1518+
expect(wgsl).not.toContain('ptr<function, vec4f>');
1519+
1520+
expect(wgsl).toMatchInlineSnapshot(`
1521+
"struct VertexOut {
1522+
@builtin(position) position: vec4f,
1523+
@location(0) local: vec2f,
1524+
}
1525+
1526+
struct VertexIn {
1527+
@builtin(vertex_index) vertexIndex: u32,
1528+
}
1529+
1530+
@vertex fn vertex(_arg_0: VertexIn) -> VertexOut {
1531+
var pos = array<vec2f, 3>(vec2f(0, 0.5), vec2f(-0.5), vec2f(0.5, -0.5));
1532+
let local = (&pos[_arg_0.vertexIndex]);
1533+
return VertexOut(vec4f((*local), 0f, 1f), (*local));
1534+
}
1535+
1536+
struct FragmentOut {
1537+
@location(0) color: vec4f,
1538+
}
1539+
1540+
@fragment fn fragment() -> FragmentOut {
1541+
var color = vec4f(1, 0, 0, 1);
1542+
let alias_1 = (&color);
1543+
return FragmentOut((*alias_1));
1544+
}"
1545+
`);
1546+
});
1547+
14881548
it('generates a struct that matches vertex attributes', ({ root }) => {
14891549
const vertexLayout = tgpu.vertexLayout(d.arrayOf(d.vec3f));
14901550
const pipeline = root.createRenderPipeline({

0 commit comments

Comments
 (0)