Skip to content

Commit 05201f9

Browse files
authored
WebGPURenderer: Fix approach to clear() (#26046)
1 parent c4befe1 commit 05201f9

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

examples/jsm/renderers/webgpu/WebGPUBackground.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,14 @@ class WebGPUBackground {
1515
this.boxMesh = null;
1616
this.boxMeshNode = null;
1717

18-
this.forceClear = false;
19-
20-
}
21-
22-
clear() {
23-
24-
this.forceClear = true;
25-
2618
}
2719

2820
update( scene, renderList, renderState ) {
2921

3022
const renderer = this.renderer;
3123
const background = ( scene.isScene === true ) ? scene.backgroundNode || this.properties.get( scene ).backgroundNode || scene.background : null;
3224

33-
let forceClear = this.forceClear;
25+
let forceClear = false;
3426

3527
if ( background === null ) {
3628

examples/jsm/renderers/webgpu/WebGPURenderer.js

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class WebGPURenderer {
146146
this._animation = new WebGPUAnimation();
147147

148148
this._currentRenderState = null;
149+
this._lastRenderState = null;
149150

150151
this._opaqueSort = null;
151152
this._transparentSort = null;
@@ -434,6 +435,8 @@ class WebGPURenderer {
434435
nodeFrame.renderId = previousRenderId;
435436
this._currentRenderState = previousRenderState;
436437

438+
this._lastRenderState = renderState;
439+
437440
}
438441

439442
setAnimationLoop( callback ) {
@@ -688,9 +691,77 @@ class WebGPURenderer {
688691

689692
}
690693

691-
clear() {
694+
clear( color = true, depth = true, stencil = true ) {
695+
696+
const renderState = this._currentRenderState || this._lastRenderState;
697+
if ( renderState === null ) return;
698+
699+
depth = depth && renderState.depth;
700+
stencil = stencil && renderState.stencil;
701+
702+
const descriptorGPU = renderState.descriptorGPU;
703+
const colorAttachment = descriptorGPU.colorAttachments[ 0 ];
704+
705+
// @TODO: Include render target in clear operation.
706+
if ( this._parameters.antialias === true ) {
707+
708+
colorAttachment.view = this._colorBuffer.createView();
709+
colorAttachment.resolveTarget = this._context.getCurrentTexture().createView();
710+
711+
} else {
712+
713+
colorAttachment.view = this._context.getCurrentTexture().createView();
714+
colorAttachment.resolveTarget = undefined;
715+
716+
}
717+
718+
descriptorGPU.depthStencilAttachment.view = this._depthBuffer.createView();
719+
720+
if ( color ) {
721+
722+
colorAttachment.loadOp = GPULoadOp.Clear;
723+
colorAttachment.clearValue = { r: this._clearColor.r, g: this._clearColor.g, b: this._clearColor.b, a: this._clearAlpha };
724+
725+
}
726+
727+
if ( depth ) {
728+
729+
descriptorGPU.depthStencilAttachment.depthLoadOp = GPULoadOp.Clear;
730+
descriptorGPU.depthStencilAttachment.depthClearValue = this._clearDepth;
731+
732+
}
733+
734+
if ( stencil ) {
735+
736+
descriptorGPU.depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear;
737+
descriptorGPU.depthStencilAttachment.stencilClearValue = this._clearStencil;
738+
739+
}
740+
741+
renderState.encoderGPU = this._device.createCommandEncoder( {} );
742+
renderState.currentPassGPU = renderState.encoderGPU.beginRenderPass( renderState.descriptorGPU );
743+
744+
renderState.currentPassGPU.end();
745+
746+
this._device.queue.submit( [ renderState.encoderGPU.finish() ] );
747+
748+
}
749+
750+
clearColor() {
751+
752+
this.clear( true, false, false );
753+
754+
}
755+
756+
clearDepth() {
757+
758+
this.clear( false, true, false );
759+
760+
}
761+
762+
clearStencil() {
692763

693-
if ( this._background ) this._background.clear();
764+
this.clear( false, false, true );
694765

695766
}
696767

0 commit comments

Comments
 (0)