Skip to content

Commit cba7408

Browse files
authored
WebGPURenderer: Cache pipeline per render/compute pass. (mrdoob#31066)
1 parent 3a21dee commit cba7408

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/renderers/webgpu/WebGPUBackend.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,8 @@ class WebGPUBackend extends Backend {
13261326
// pipeline
13271327

13281328
const pipelineGPU = this.get( pipeline ).pipeline;
1329-
passEncoderGPU.setPipeline( pipelineGPU );
1329+
1330+
this.pipelineUtils.setPipeline( passEncoderGPU, pipelineGPU );
13301331

13311332
// bind groups
13321333

@@ -1422,7 +1423,7 @@ class WebGPUBackend extends Backend {
14221423
const setPipelineAndBindings = ( passEncoderGPU, currentSets ) => {
14231424

14241425
// pipeline
1425-
passEncoderGPU.setPipeline( pipelineGPU );
1426+
this.pipelineUtils.setPipeline( passEncoderGPU, pipelineGPU );
14261427
currentSets.pipeline = pipelineGPU;
14271428

14281429
// bind groups
@@ -1653,8 +1654,8 @@ class WebGPUBackend extends Backend {
16531654

16541655
} else {
16551656

1656-
// Regular single camera rendering
1657-
if ( renderContextData.currentPass ) {
1657+
// Regular single camera rendering
1658+
if ( renderContextData.currentPass ) {
16581659

16591660
// Handle occlusion queries
16601661
if ( renderContextData.occlusionQuerySet !== undefined ) {

src/renderers/webgpu/utils/WebGPUPipelineUtils.js

+29
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,35 @@ class WebGPUPipelineUtils {
3636
*/
3737
this.backend = backend;
3838

39+
/**
40+
* A Weak Map that tracks the active pipeline for render or compute passes.
41+
*
42+
* @private
43+
* @type {WeakMap<(GPURenderPassEncoder|GPUComputePassEncoder),(GPURenderPipeline|GPUComputePipeline)>}
44+
*/
45+
this._activePipelines = new WeakMap();
46+
47+
}
48+
49+
/**
50+
* Sets the given pipeline for the given pass. The method makes sure to only set the
51+
* pipeline when necessary.
52+
*
53+
* @param {(GPURenderPassEncoder|GPUComputePassEncoder)} pass - The pass encoder.
54+
* @param {(GPURenderPipeline|GPUComputePipeline)} pipeline - The pipeline.
55+
*/
56+
setPipeline( pass, pipeline ) {
57+
58+
const currentPipeline = this._activePipelines.get( pass );
59+
60+
if ( currentPipeline !== pipeline ) {
61+
62+
pass.setPipeline( pipeline );
63+
64+
this._activePipelines.set( pass, pipeline );
65+
66+
}
67+
3968
}
4069

4170
/**

0 commit comments

Comments
 (0)