Skip to content

Commit

Permalink
handle empty bind groups
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Feb 11, 2024
1 parent c0bd849 commit ca54f81
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/data-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ export function makeBindGroupLayoutDescriptors(
visibility: resource.entry.visibility | (entry?.visibility || 0),
});
}
return bindGroupLayoutDescriptorsByGroupByBinding.map(v => ({entries: [...v.values()].sort(byBinding) }));
const descriptors = bindGroupLayoutDescriptorsByGroupByBinding.map(v => ({entries: [...v.values()].sort(byBinding) }));
for (let i = 0; i < descriptors.length; ++i) {
if (!descriptors[i]) {
descriptors[i] = { entries: [] };
}
}
return descriptors;
}

function getNamedVariables(reflect: WgslReflect, variables: VariableInfo[]): VariableDefinitions {
Expand Down
Empty file added src/test.ts
Empty file.
127 changes: 125 additions & 2 deletions test/tests/data-definition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,9 @@ describe('data-definition-tests', () => {
},
],
},
undefined,
{
entries: [],
},
{
entries: [
{
Expand Down Expand Up @@ -737,7 +739,7 @@ describe('data-definition-tests', () => {
assertDeepEqual(layouts, expected);
});

it('handles merges stages', () => {
it('handles merged stages', () => {
const code = `
@group(0) @binding(1) var tex2d: texture_2d<f32>;
@group(0) @binding(2) var samp: sampler;
Expand Down Expand Up @@ -785,6 +787,127 @@ describe('data-definition-tests', () => {
assertDeepEqual(layouts, expected);
});

it('handles empty groups', () => {
const code = `
@group(3) @binding(1) var<uniform> u: vec4f;
@compute fn cs() {
_ = u;
}
`;
const d = makeShaderDataDefinitions(code);
const layouts = makeBindGroupLayoutDescriptors(d, {
compute: {
entryPoint: 'cs',
},
});
const expected = [
{
entries: [],
},
{
entries: [],
},
{
entries: [],
},
{
entries: [
{
binding: 1,
visibility: 4,
buffer: {
},
},
],
},
];
assertDeepEqual(layouts, expected);
});

it('handles unnamed entrypoints', () => {
const code = `
@group(0) @binding(1) var tex2d: texture_2d<f32>;
@group(0) @binding(2) var samp: sampler;
@group(0) @binding(3) var<uniform> u1: vec4f;
@group(0) @binding(4) var<uniform> u2: vec4f;
@vertex fn vs() -> @builtin(position) vec4f {
_ = tex2d;
_ = samp;
_ = u1;
}
@fragment fn fs() -> @location(0) vec4f {
_ = tex2d;
_ = samp;
return vec4f(0);
}
@compute fn cs() {
_ = u1;
_ = u2;
}
`;
const d = makeShaderDataDefinitions(code);
{
const layouts = makeBindGroupLayoutDescriptors(d, {
compute: { },
});
const expected = [
{
entries: [
{
binding: 3,
visibility: 4,
buffer: {
},
},
{
binding: 4,
visibility: 4,
buffer: {
},
},
],
},
];
assertDeepEqual(layouts, expected);
}
{
const layouts = makeBindGroupLayoutDescriptors(d, {
vertex: { },
fragment: { },
});
const expected = [
{
entries: [
{
binding: 1,
visibility: 3,
texture: {
sampleType: 'float',
viewDimension: '2d',
multisampled: false,
},
},
{
binding: 2,
visibility: 3,
sampler: {
type: 'filtering',
},
},
{
binding: 3,
visibility: 1,
buffer: {
},
},
],
},
];
assertDeepEqual(layouts, expected);
}
});

});

});

0 comments on commit ca54f81

Please sign in to comment.