Skip to content

Commit e53d5d1

Browse files
committed
Reuse the same render pass
1 parent b63af71 commit e53d5d1

1 file changed

Lines changed: 73 additions & 51 deletions

File tree

src/webgpu/p5.RendererWebGPU.js

Lines changed: 73 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ function rendererWebGPU(p5, fn) {
3737
constructor(pInst, w, h, isMainCanvas, elt) {
3838
super(pInst, w, h, isMainCanvas, elt)
3939

40-
this.renderPass = {};
40+
this.activeRenderPass = null;
41+
this.activeRenderPassEncoder = null;
4142

4243
this.samplers = new Map();
4344

@@ -204,6 +205,66 @@ function rendererWebGPU(p5, fn) {
204205
return this.currentCanvasColorTextureView;
205206
}
206207

208+
_beginActiveRenderPass() {
209+
if (this.activeRenderPass) return;
210+
211+
// Use framebuffer texture if active, otherwise use canvas texture
212+
const activeFramebuffer = this.activeFramebuffer();
213+
214+
const colorAttachment = {
215+
view: activeFramebuffer
216+
? (activeFramebuffer.aaColorTexture
217+
? activeFramebuffer.aaColorTextureView
218+
: activeFramebuffer.colorTextureView)
219+
: this._getCanvasColorTextureView(),
220+
loadOp: "load",
221+
storeOp: "store",
222+
// If using multisampled texture, resolve to non-multisampled texture
223+
resolveTarget: activeFramebuffer && activeFramebuffer.aaColorTexture
224+
? activeFramebuffer.colorTextureView
225+
: undefined,
226+
};
227+
228+
// Use framebuffer depth texture if active, otherwise use canvas depth texture
229+
const depthTextureView = activeFramebuffer
230+
? (activeFramebuffer.aaDepthTexture
231+
? activeFramebuffer.aaDepthTextureView
232+
: activeFramebuffer.depthTextureView)
233+
: this.depthTextureView;
234+
const renderPassDescriptor = {
235+
colorAttachments: [colorAttachment],
236+
depthStencilAttachment: depthTextureView
237+
? {
238+
view: depthTextureView,
239+
depthLoadOp: "load",
240+
depthStoreOp: "store",
241+
depthClearValue: 1.0,
242+
stencilLoadOp: "load",
243+
stencilStoreOp: "store",
244+
depthReadOnly: false,
245+
stencilReadOnly: false,
246+
}
247+
: undefined,
248+
};
249+
const commandEncoder = this.device.createCommandEncoder();
250+
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
251+
this.activeRenderPassEncoder = commandEncoder;
252+
this.activeRenderPass = passEncoder;
253+
}
254+
255+
_finishActiveRenderPass() {
256+
if (!this.activeRenderPass) return;
257+
258+
const commandEncoder = this.activeRenderPassEncoder;
259+
const passEncoder = this.activeRenderPass;
260+
passEncoder.end();
261+
262+
// Store the command encoder for later submission
263+
this._pendingCommandEncoders.push(commandEncoder.finish());
264+
this.activeRenderPassEncoder = null;
265+
this.activeRenderPass = null;
266+
}
267+
207268
clear(...args) {
208269
const _r = args[0] || 0;
209270
const _g = args[1] || 0;
@@ -215,6 +276,8 @@ function rendererWebGPU(p5, fn) {
215276
this._frameState = FRAME_STATE.UNPROMOTED;
216277
}
217278

279+
this._finishActiveRenderPass();
280+
218281
const commandEncoder = this.device.createCommandEncoder();
219282

220283
// Use framebuffer texture if active, otherwise use canvas texture
@@ -269,6 +332,7 @@ function rendererWebGPU(p5, fn) {
269332
* occlude anything subsequently drawn.
270333
*/
271334
clearDepth(depth = 1) {
335+
this._finishActiveRenderPass();
272336
const commandEncoder = this.device.createCommandEncoder();
273337

274338
// Use framebuffer texture if active, otherwise use canvas texture
@@ -359,7 +423,6 @@ function rendererWebGPU(p5, fn) {
359423
const loc = attr.location;
360424
if (!this.registerEnabled.has(loc)) {
361425
// TODO
362-
// this.renderPass.setVertexBuffer(loc, buffer);
363426
this.registerEnabled.add(loc);
364427
}
365428
}
@@ -833,6 +896,7 @@ function rendererWebGPU(p5, fn) {
833896
}
834897

835898
_resetBuffersBeforeDraw() {
899+
this._finishActiveRenderPass();
836900
// Set state to PENDING - we'll decide on first draw
837901
this._frameState = FRAME_STATE.PENDING;
838902

@@ -1133,6 +1197,7 @@ function rendererWebGPU(p5, fn) {
11331197
}
11341198

11351199
flushDraw() {
1200+
this._finishActiveRenderPass();
11361201
// Only submit if we actually had any draws
11371202
if (this._hasPendingDraws) {
11381203
// Create a copy of pending command encoders
@@ -1255,48 +1320,8 @@ function rendererWebGPU(p5, fn) {
12551320
this._promoteToFramebufferWithoutCopy();
12561321
}
12571322

1258-
const commandEncoder = this.device.createCommandEncoder();
1259-
1260-
// Use framebuffer texture if active, otherwise use canvas texture
1261-
const activeFramebuffer = this.activeFramebuffer();
1262-
1263-
const colorAttachment = {
1264-
view: activeFramebuffer
1265-
? (activeFramebuffer.aaColorTexture
1266-
? activeFramebuffer.aaColorTextureView
1267-
: activeFramebuffer.colorTextureView)
1268-
: this._getCanvasColorTextureView(),
1269-
loadOp: "load",
1270-
storeOp: "store",
1271-
// If using multisampled texture, resolve to non-multisampled texture
1272-
resolveTarget: activeFramebuffer && activeFramebuffer.aaColorTexture
1273-
? activeFramebuffer.colorTextureView
1274-
: undefined,
1275-
};
1276-
1277-
// Use framebuffer depth texture if active, otherwise use canvas depth texture
1278-
const depthTextureView = activeFramebuffer
1279-
? (activeFramebuffer.aaDepthTexture
1280-
? activeFramebuffer.aaDepthTextureView
1281-
: activeFramebuffer.depthTextureView)
1282-
: this.depthTextureView;
1283-
const renderPassDescriptor = {
1284-
colorAttachments: [colorAttachment],
1285-
depthStencilAttachment: depthTextureView
1286-
? {
1287-
view: depthTextureView,
1288-
depthLoadOp: "load",
1289-
depthStoreOp: "store",
1290-
depthClearValue: 1.0,
1291-
stencilLoadOp: "load",
1292-
stencilStoreOp: "store",
1293-
depthReadOnly: false,
1294-
stencilReadOnly: false,
1295-
}
1296-
: undefined,
1297-
};
1298-
1299-
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
1323+
this._beginActiveRenderPass();
1324+
const passEncoder = this.activeRenderPass;
13001325
const currentShader = this._curShader;
13011326
passEncoder.setPipeline(currentShader.getPipeline(this._shaderOptions({ mode })));
13021327

@@ -1438,17 +1463,12 @@ function rendererWebGPU(p5, fn) {
14381463
passEncoder.draw(geometry.lineVertices.length / 3, count, 0, 0);
14391464
}
14401465

1441-
passEncoder.end();
1442-
1443-
// Store the command encoder for later submission
1444-
this._pendingCommandEncoders.push(commandEncoder.finish());
1445-
14461466
// Mark that we have pending draws that need submission
14471467
this._hasPendingDraws = true;
14481468

1449-
if (this._pendingCommandEncoders.length > 50) {
1469+
/*if (this._pendingCommandEncoders.length > 50) {
14501470
this.flushDraw();
1451-
}
1471+
}*/
14521472
}
14531473

14541474
//////////////////////////////////////////////
@@ -2063,6 +2083,7 @@ function rendererWebGPU(p5, fn) {
20632083
}
20642084

20652085
_clearClipBuffer() {
2086+
this._finishActiveRenderPass();
20662087
const commandEncoder = this.device.createCommandEncoder();
20672088

20682089
const activeFramebuffer = this.activeFramebuffer();
@@ -2643,6 +2664,7 @@ ${hookUniformFields}}
26432664
}
26442665

26452666
_clearFramebufferTextures(framebuffer) {
2667+
this._finishActiveRenderPass();
26462668
const commandEncoder = this.device.createCommandEncoder();
26472669

26482670
// Clear the color texture (and multisampled texture if it exists)

0 commit comments

Comments
 (0)