File tree Expand file tree Collapse file tree 5 files changed +30
-51
lines changed
apps/typegpu-docs/src/content/docs/fundamentals
packages/typegpu/src/core Expand file tree Collapse file tree 5 files changed +30
-51
lines changed Original file line number Diff line number Diff line change @@ -122,7 +122,7 @@ and submits the resulting `GPUCommandBuffer` to the device.
122
122
123
123
:::caution
124
124
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.
126
126
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 ` .
127
127
:::
128
128
Original file line number Diff line number Diff line change @@ -195,27 +195,18 @@ class TgpuComputePipelineImpl implements TgpuComputePipeline {
195
195
pass . dispatchWorkgroups ( x , y , z ) ;
196
196
pass . end ( ) ;
197
197
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 ) {
202
202
branch [ $internal ] . batchState . performanceCallbacks . push ( ( ) =>
203
203
triggerPerformanceCallback ( { root : branch , priors : this . _priors } )
204
204
) ;
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 ) {
218
206
branch [ $internal ] . flush ( ) ;
207
+ if ( hasPerformanceCallback ) {
208
+ triggerPerformanceCallback ( { root : branch , priors : this . _priors } ) ;
209
+ }
219
210
}
220
211
}
221
212
Original file line number Diff line number Diff line change @@ -566,27 +566,18 @@ class TgpuRenderPipelineImpl implements TgpuRenderPipeline {
566
566
567
567
pass . end ( ) ;
568
568
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 ) {
573
573
branch [ $internal ] . batchState . performanceCallbacks . push ( ( ) =>
574
574
triggerPerformanceCallback ( { root : branch , priors : internals . priors } )
575
575
) ;
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 ) {
589
577
branch [ $internal ] . flush ( ) ;
578
+ if ( hasPerformanceCallback ) {
579
+ triggerPerformanceCallback ( { root : branch , priors : internals . priors } ) ;
580
+ }
590
581
}
591
582
}
592
583
Original file line number Diff line number Diff line change @@ -159,8 +159,7 @@ export function triggerPerformanceCallback({
159
159
0 ,
160
160
) ;
161
161
162
- ( async ( ) => {
163
- const result = await querySet . read ( ) ;
162
+ querySet . read ( ) . then ( ( result ) => {
164
163
const start =
165
164
result [ priors . timestampWrites ?. beginningOfPassWriteIndex ?? 0 ] ;
166
165
const end = result [ priors . timestampWrites ?. endOfPassWriteIndex ?? 1 ] ;
@@ -169,6 +168,6 @@ export function triggerPerformanceCallback({
169
168
throw new Error ( 'QuerySet did not return valid timestamps.' ) ;
170
169
}
171
170
172
- await callback ( start , end ) ;
173
- } ) ( ) ;
171
+ callback ( start , end ) ;
172
+ } ) ;
174
173
}
Original file line number Diff line number Diff line change @@ -298,9 +298,7 @@ class TgpuRootImpl extends WithBindingImpl
298
298
} ,
299
299
300
300
get commandEncoder ( ) {
301
- if ( ! commandEncoder ) {
302
- commandEncoder = device . createCommandEncoder ( ) ;
303
- }
301
+ commandEncoder ??= device . createCommandEncoder ( ) ;
304
302
305
303
return commandEncoder ;
306
304
} ,
@@ -317,25 +315,25 @@ class TgpuRootImpl extends WithBindingImpl
317
315
}
318
316
319
317
batch ( callback : ( ) => void ) {
320
- const nestedBatch = this [ $internal ] . batchState . ongoingBatch ;
321
- if ( nestedBatch ) {
318
+ const { batchState } = this [ $internal ] ;
319
+
320
+ if ( batchState . ongoingBatch ) {
322
321
throw new Error ( 'Nested batch is not allowed' ) ;
323
322
}
324
323
325
- this [ $internal ] . batchState . ongoingBatch = true ;
324
+ batchState . ongoingBatch = true ;
326
325
327
326
try {
328
327
callback ( ) ;
329
328
} finally {
330
329
this [ $internal ] . flush ( ) ;
331
- for (
332
- const performanceCallback of this [ $internal ] . batchState
333
- . performanceCallbacks
334
- ) {
330
+
331
+ for ( const performanceCallback of batchState . performanceCallbacks ) {
335
332
performanceCallback ( ) ;
336
333
}
337
- this [ $internal ] . batchState . ongoingBatch = false ;
338
- this [ $internal ] . batchState . performanceCallbacks = [ ] ;
334
+
335
+ batchState . ongoingBatch = false ;
336
+ batchState . performanceCallbacks = [ ] ;
339
337
}
340
338
}
341
339
You can’t perform that action at this time.
0 commit comments