Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 22 additions & 2 deletions packages/typegpu/src/tgpuBindGroupLayout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { isBuffer, type TgpuBuffer, type UniformFlag } from './core/buffer/buffer.ts';
import {
isBufferShorthand,
type TgpuBufferShorthand,
type TgpuMutable,
type TgpuReadonly,
type TgpuUniform,
} from './core/buffer/bufferShorthand.ts';
import {
isUsableAsUniform,
type TgpuBufferMutable,
Expand Down Expand Up @@ -341,6 +348,7 @@ export type LayoutEntryToInput<T extends TgpuLayoutEntry | null> =
TgpuLayoutEntry | null extends T
?
| TgpuBuffer<AnyWgslData>
| TgpuBufferShorthand<AnyWgslData>
| GPUBuffer
| TgpuSampler
| GPUSampler
Expand All @@ -350,10 +358,15 @@ export type LayoutEntryToInput<T extends TgpuLayoutEntry | null> =
| GPUExternalTexture
: // Strict type-checking
T extends TgpuLayoutUniform
? (TgpuBuffer<MemIdentity<UnwrapRuntimeConstructor<T['uniform']>>> & UniformFlag) | GPUBuffer
?
| (TgpuBuffer<MemIdentity<UnwrapRuntimeConstructor<T['uniform']>>> & UniformFlag)
| TgpuUniform<MemIdentity<UnwrapRuntimeConstructor<T['uniform']>>>
| GPUBuffer
: T extends TgpuLayoutStorage
?
| (TgpuBuffer<MemIdentity<UnwrapRuntimeConstructor<T['storage']>>> & StorageFlag)
| TgpuMutable<MemIdentity<UnwrapRuntimeConstructor<T['storage']>>>
| TgpuReadonly<MemIdentity<UnwrapRuntimeConstructor<T['storage']>>>
| GPUBuffer
: T extends TgpuLayoutSampler
? TgpuSampler | GPUSampler
Expand Down Expand Up @@ -682,7 +695,14 @@ export class TgpuBindGroupImpl<
return null;
}

const value = this.entries[key as keyof typeof this.entries];
// Buffer shorthands (uniform/mutable/readonly) are accepted directly
// for buffer-typed entries; unwrap them to their underlying buffer so
// the rest of the bind-group machinery is unchanged.
const entryValue = this.entries[key as keyof typeof this.entries];
const value = (isBufferShorthand(entryValue) ? entryValue.buffer : entryValue) as Exclude<
(typeof this.entries)[keyof typeof this.entries],
TgpuBufferShorthand<AnyWgslData>
>;

if (value === undefined) {
throw new Error(
Expand Down
74 changes: 74 additions & 0 deletions packages/typegpu/tests/bindGroupLayout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,80 @@ describe('TgpuBindGroup', () => {
],
});
});

it('populates a uniform layout with a buffer shorthand', ({ root }) => {
const uniform = root.createUniform(d.vec3f);
const bindGroup = root.createBindGroup(layout, { foo: uniform });

root.unwrap(bindGroup);

expect(root.device.createBindGroup).toBeCalledWith({
label: 'example',
layout: root.unwrap(layout),
entries: [
{
binding: 0,
resource: {
buffer: root.unwrap(uniform.buffer),
},
},
],
});
});
});

describe('simple storage layout', () => {
let layout: TgpuBindGroupLayout<{
foo: { storage: d.Vec3f; access: 'mutable' };
}>;

beforeEach(() => {
layout = tgpu
.bindGroupLayout({
foo: { storage: d.vec3f, access: 'mutable' },
})
.$name('example');
});

it('populates a storage layout with a mutable shorthand', ({ root }) => {
const mutable = root.createMutable(d.vec3f);
const bindGroup = root.createBindGroup(layout, { foo: mutable });

root.unwrap(bindGroup);

expect(root.device.createBindGroup).toBeCalledWith({
label: 'example',
layout: root.unwrap(layout),
entries: [
{
binding: 0,
resource: {
buffer: root.unwrap(mutable.buffer),
},
},
],
});
});

it('populates a storage layout with a readonly shorthand', ({ root }) => {
const readonly = root.createReadonly(d.vec3f);
const bindGroup = root.createBindGroup(layout, { foo: readonly });

root.unwrap(bindGroup);

expect(root.device.createBindGroup).toBeCalledWith({
label: 'example',
layout: root.unwrap(layout),
entries: [
{
binding: 0,
resource: {
buffer: root.unwrap(readonly.buffer),
},
},
],
});
});
});

describe('simple layout with atomics', () => {
Expand Down
Loading