Skip to content

Commit 4d4d79f

Browse files
committed
fix(📀): accept shorthand syntax for buffer bindings
1 parent 72c0363 commit 4d4d79f

4 files changed

Lines changed: 79 additions & 4 deletions

File tree

‎apps/example/src/StorageBufferVertices/StorageBufferVertices.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ export function StorageBufferVertices() {
147147
label: "bind group for objects",
148148
layout: pipeline.getBindGroupLayout(0),
149149
entries: [
150-
{ binding: 0, resource: { buffer: staticStorageBuffer } },
151-
{ binding: 1, resource: { buffer: changingStorageBuffer } },
152-
{ binding: 2, resource: { buffer: vertexStorageBuffer } },
150+
{ binding: 0, resource: staticStorageBuffer },
151+
{ binding: 1, resource: changingStorageBuffer },
152+
{ binding: 2, resource: vertexStorageBuffer },
153153
],
154154
});
155155

‎packages/webgpu/cpp/rnwgpu/api/descriptors/GPUBindGroupEntry.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ template <> struct JSIConverter<std::shared_ptr<rnwgpu::GPUBindGroupEntry>> {
4646
} else if (obj.hasNativeState<rnwgpu::GPUTextureView>(runtime)) {
4747
result->textureView =
4848
obj.getNativeState<rnwgpu::GPUTextureView>(runtime);
49+
} else if (obj.hasNativeState<rnwgpu::GPUBuffer>(runtime)) {
50+
// Support passing GPUBuffer directly as resource (auto-wrap in
51+
// GPUBufferBinding)
52+
auto binding = std::make_shared<rnwgpu::GPUBufferBinding>();
53+
binding->buffer = obj.getNativeState<rnwgpu::GPUBuffer>(runtime);
54+
result->buffer = binding;
4955
} else {
5056
result->buffer = JSIConverter<
5157
std::shared_ptr<rnwgpu::GPUBufferBinding>>::fromJSI(runtime,

‎packages/webgpu/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-wgpu",
3-
"version": "0.5.4",
3+
"version": "0.5.5",
44
"description": "React Native WebGPU",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

‎packages/webgpu/src/__tests__/Device.spec.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,74 @@
11
import { client } from "./setup";
22

3+
describe("createBindGroup", () => {
4+
it("should accept GPUBuffer directly as resource (without wrapper)", async () => {
5+
const result = await client.eval(({ device }) => {
6+
// Create a simple compute shader that uses a storage buffer
7+
const module = device.createShaderModule({
8+
code: `
9+
@group(0) @binding(0) var<storage, read> data: array<f32>;
10+
@compute @workgroup_size(1)
11+
fn main() {
12+
let _ = data[0];
13+
}
14+
`,
15+
});
16+
17+
const pipeline = device.createComputePipeline({
18+
layout: "auto",
19+
compute: { module },
20+
});
21+
22+
const buffer = device.createBuffer({
23+
size: 16,
24+
usage: GPUBufferUsage.STORAGE,
25+
});
26+
27+
// Pass GPUBuffer directly as resource (without { buffer: ... } wrapper)
28+
const bindGroup = device.createBindGroup({
29+
layout: pipeline.getBindGroupLayout(0),
30+
entries: [{ binding: 0, resource: buffer }],
31+
});
32+
33+
return bindGroup !== null && bindGroup !== undefined;
34+
});
35+
expect(result).toBe(true);
36+
});
37+
38+
it("should accept GPUBufferBinding object as resource (with wrapper)", async () => {
39+
const result = await client.eval(({ device }) => {
40+
const module = device.createShaderModule({
41+
code: `
42+
@group(0) @binding(0) var<storage, read> data: array<f32>;
43+
@compute @workgroup_size(1)
44+
fn main() {
45+
let _ = data[0];
46+
}
47+
`,
48+
});
49+
50+
const pipeline = device.createComputePipeline({
51+
layout: "auto",
52+
compute: { module },
53+
});
54+
55+
const buffer = device.createBuffer({
56+
size: 16,
57+
usage: GPUBufferUsage.STORAGE,
58+
});
59+
60+
// Pass GPUBufferBinding object as resource (with { buffer: ... } wrapper)
61+
const bindGroup = device.createBindGroup({
62+
layout: pipeline.getBindGroupLayout(0),
63+
entries: [{ binding: 0, resource: { buffer } }],
64+
});
65+
66+
return bindGroup !== null && bindGroup !== undefined;
67+
});
68+
expect(result).toBe(true);
69+
});
70+
});
71+
372
describe("Device", () => {
473
it("request device (1)", async () => {
574
const result = await client.eval(({ gpu }) =>

0 commit comments

Comments
 (0)