Bind pre-existing storage buffer to TSL pipeline?

I have an algorithm I am using that is written with “raw” WebGPU WGSL code (i.e. no threejs involvment). The algorithm ultimately sorts some data that is stored in a storage buffer, I create the storage buffer like so:

const myBuffer = device.createBuffer({
  size: data.byteLength,
  usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
  mappedAtCreation: true,
});

I want to later use this storage buffer on a vertexNode/fragmentNode on, for example, a MeshBasicNodeMaterial.

e.g. I’d like to be able to do something like the following (pseudocode, this doesn’t exist!)

const myData = existingBuffer(myBuffer);
const vertexShader = wgslFn(
  `...`,
  [myData],
);

I really don’t want to have to copy the data back to the CPU, just to use TSL to create another storage buffer, the data is already on the GPU; all that needs to happen “under the hood” is for this buffer to be bound to the pipeline and for it to be declared in the shader code. Equally, I do not want to have to rewrite this reasonably complex algorithm entirely in TSL compute shaders.

Does anyone have experience with this / know if it is possible at all? Alternatively, would it be possible to access the underlying GPUBuffer that TSL creates, so I could pass that to my custom code?

1 Like