Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use drawIndirect instead of draw in particles sample #471

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 14 additions & 1 deletion sample/particles/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ const computeBindGroup = device.createBindGroup({
],
});

const drawBuffer = device.createBuffer({
size: 16,
usage: GPUBufferUsage.INDIRECT | GPUBufferUsage.COPY_DST,
});

const aspect = canvas.width / canvas.height;
const projection = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 100.0);
const view = mat4.create();
Expand Down Expand Up @@ -456,13 +461,21 @@ function frame() {
passEncoder.dispatchWorkgroups(Math.ceil(numParticles / 64));
passEncoder.end();
}
device.queue.writeBuffer(
drawBuffer,
0,
new Uint32Array([
6, // vertex count
numParticles, // instance count
])
);
{
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(renderPipeline);
passEncoder.setBindGroup(0, uniformBindGroup);
passEncoder.setVertexBuffer(0, particlesBuffer);
passEncoder.setVertexBuffer(1, quadVertexBuffer);
passEncoder.draw(6, numParticles, 0, 0);
passEncoder.drawIndirect(drawBuffer, 0);
passEncoder.end();
}

Expand Down