Skip to content

Commit

Permalink
Create buffers each frame
Browse files Browse the repository at this point in the history
  • Loading branch information
beaufortfrancois committed Nov 9, 2023
1 parent cbe9f4c commit f7978db
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/sample/computeBoids/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,11 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
const computePassDescriptor = {};

let querySet: GPUQuerySet | undefined = undefined;
let resolveBuffer: GPUBuffer | undefined = undefined;
let resultBuffer: GPUBuffer | undefined = undefined;
if (hasTimestampQuery) {
querySet = device.createQuerySet({
type: 'timestamp',
count: 4,
});
resolveBuffer = device.createBuffer({
size: 4 * BigInt64Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC,
});
resultBuffer = device.createBuffer({
size: 4 * BigInt64Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
});
renderPassDescriptor.timestampWrites = {
querySet,
beginningOfPassWriteIndex: 0,
Expand Down Expand Up @@ -284,7 +274,17 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
passEncoder.end();
}

if (hasTimestampQuery && resultBuffer.mapState == 'unmapped') {
let resolveBuffer: GPUBuffer | undefined = undefined;
let resultBuffer: GPUBuffer | undefined = undefined;
if (hasTimestampQuery) {
resolveBuffer = device.createBuffer({
size: 4 * BigInt64Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC,
});
resultBuffer = device.createBuffer({
size: 4 * BigInt64Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
});
commandEncoder.resolveQuerySet(querySet, 0, 4, resolveBuffer, 0);
commandEncoder.copyBufferToBuffer(
resolveBuffer,
Expand All @@ -297,7 +297,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {

device.queue.submit([commandEncoder.finish()]);

if (hasTimestampQuery && resultBuffer.mapState == 'unmapped') {
if (hasTimestampQuery) {
resultBuffer.mapAsync(GPUMapMode.READ).then(() => {
const times = new BigInt64Array(resultBuffer.getMappedRange());
const renderPassDuration = Number(times[1] - times[0]);
Expand Down

0 comments on commit f7978db

Please sign in to comment.