Skip to content

Commit c3404e0

Browse files
committed
Review changes 2
1 parent 39045ba commit c3404e0

File tree

5 files changed

+30
-51
lines changed

5 files changed

+30
-51
lines changed

apps/typegpu-docs/src/content/docs/fundamentals/utils.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ and submits the resulting `GPUCommandBuffer` to the device.
122122

123123
:::caution
124124
Read–write operations always flush the command encoder.
125-
When you call a pipeline with a performance callback, the callback is invoked at the end of the batch.
125+
When you call a pipeline with a performance callback, the callback is invoked at the end of the batch. The timestamps themselves are not affected by the batching. They are still written at the beginning and/or end of the associated pipeline/render pass.
126126
We've prepared a table showing when a flush occurs (i.e., when a new command encoder is created). Keep this in mind when using `batch`.
127127
:::
128128

packages/typegpu/src/core/pipeline/computePipeline.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -195,27 +195,18 @@ class TgpuComputePipelineImpl implements TgpuComputePipeline {
195195
pass.dispatchWorkgroups(x, y, z);
196196
pass.end();
197197

198-
if (
199-
this._priors.performanceCallback &&
200-
branch[$internal].batchState.ongoingBatch
201-
) {
198+
const hasPerformanceCallback = !!this._priors.performanceCallback;
199+
const isOngoingBatch = branch[$internal].batchState.ongoingBatch;
200+
201+
if (hasPerformanceCallback && isOngoingBatch) {
202202
branch[$internal].batchState.performanceCallbacks.push(() =>
203203
triggerPerformanceCallback({ root: branch, priors: this._priors })
204204
);
205-
} else if (
206-
this._priors.performanceCallback &&
207-
!branch[$internal].batchState.ongoingBatch
208-
) {
209-
branch[$internal].flush();
210-
triggerPerformanceCallback({
211-
root: branch,
212-
priors: this._priors,
213-
});
214-
} else if (
215-
!branch[$internal].batchState.ongoingBatch &&
216-
!this._priors.performanceCallback
217-
) {
205+
} else if (!isOngoingBatch) {
218206
branch[$internal].flush();
207+
if (hasPerformanceCallback) {
208+
triggerPerformanceCallback({ root: branch, priors: this._priors });
209+
}
219210
}
220211
}
221212

packages/typegpu/src/core/pipeline/renderPipeline.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -566,27 +566,18 @@ class TgpuRenderPipelineImpl implements TgpuRenderPipeline {
566566

567567
pass.end();
568568

569-
if (
570-
internals.priors.performanceCallback &&
571-
branch[$internal].batchState.ongoingBatch
572-
) {
569+
const hasPerformanceCallback = !!internals.priors.performanceCallback;
570+
const isOngoingBatch = branch[$internal].batchState.ongoingBatch;
571+
572+
if (hasPerformanceCallback && isOngoingBatch) {
573573
branch[$internal].batchState.performanceCallbacks.push(() =>
574574
triggerPerformanceCallback({ root: branch, priors: internals.priors })
575575
);
576-
} else if (
577-
internals.priors.performanceCallback &&
578-
!branch[$internal].batchState.ongoingBatch
579-
) {
580-
branch[$internal].flush();
581-
triggerPerformanceCallback({
582-
root: branch,
583-
priors: internals.priors,
584-
});
585-
} else if (
586-
!branch[$internal].batchState.ongoingBatch &&
587-
!internals.priors.performanceCallback
588-
) {
576+
} else if (!isOngoingBatch) {
589577
branch[$internal].flush();
578+
if (hasPerformanceCallback) {
579+
triggerPerformanceCallback({ root: branch, priors: internals.priors });
580+
}
590581
}
591582
}
592583

packages/typegpu/src/core/pipeline/timeable.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ export function triggerPerformanceCallback({
159159
0,
160160
);
161161

162-
(async () => {
163-
const result = await querySet.read();
162+
querySet.read().then((result) => {
164163
const start =
165164
result[priors.timestampWrites?.beginningOfPassWriteIndex ?? 0];
166165
const end = result[priors.timestampWrites?.endOfPassWriteIndex ?? 1];
@@ -169,6 +168,6 @@ export function triggerPerformanceCallback({
169168
throw new Error('QuerySet did not return valid timestamps.');
170169
}
171170

172-
await callback(start, end);
173-
})();
171+
callback(start, end);
172+
});
174173
}

packages/typegpu/src/core/root/init.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,7 @@ class TgpuRootImpl extends WithBindingImpl
298298
},
299299

300300
get commandEncoder() {
301-
if (!commandEncoder) {
302-
commandEncoder = device.createCommandEncoder();
303-
}
301+
commandEncoder ??= device.createCommandEncoder();
304302

305303
return commandEncoder;
306304
},
@@ -317,25 +315,25 @@ class TgpuRootImpl extends WithBindingImpl
317315
}
318316

319317
batch(callback: () => void) {
320-
const nestedBatch = this[$internal].batchState.ongoingBatch;
321-
if (nestedBatch) {
318+
const { batchState } = this[$internal];
319+
320+
if (batchState.ongoingBatch) {
322321
throw new Error('Nested batch is not allowed');
323322
}
324323

325-
this[$internal].batchState.ongoingBatch = true;
324+
batchState.ongoingBatch = true;
326325

327326
try {
328327
callback();
329328
} finally {
330329
this[$internal].flush();
331-
for (
332-
const performanceCallback of this[$internal].batchState
333-
.performanceCallbacks
334-
) {
330+
331+
for (const performanceCallback of batchState.performanceCallbacks) {
335332
performanceCallback();
336333
}
337-
this[$internal].batchState.ongoingBatch = false;
338-
this[$internal].batchState.performanceCallbacks = [];
334+
335+
batchState.ongoingBatch = false;
336+
batchState.performanceCallbacks = [];
339337
}
340338
}
341339

0 commit comments

Comments
 (0)