Skip to content

Commit

Permalink
chore(post): refine postBase rendererPassState
Browse files Browse the repository at this point in the history
remove autoSetColorTexture
add getLastRenderTexture
auto set GPUContext.lastRenderPassState
  • Loading branch information
Codeboy-cn committed Dec 12, 2024
1 parent 3d62dc6 commit 8ddd262
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 76 deletions.
3 changes: 3 additions & 0 deletions src/gfx/renderJob/passRenderer/post/PostRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export class PostRenderer extends RendererBase {
this.postList.forEach((v) => {
if (v.enable) {
v.render(view, command);
if (v.rendererPassState) {
GPUContext.lastRenderPassState = v.rendererPassState;
}
}
});
GPUContext.endCommandEncoder(command);
Expand Down
8 changes: 2 additions & 6 deletions src/gfx/renderJob/post/BloomPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export class BloomPost extends PostBase {
RT_BloomUp: VirtualTexture[];
RT_BloomDown: VirtualTexture[];
RT_threshold: VirtualTexture;
/**
* @internal
*/
rendererPassState: RendererPassState;
/**
* @internal
*/
Expand Down Expand Up @@ -131,7 +127,7 @@ export class BloomPost extends PostBase {
private createThreshouldCompute() {
this.thresholdCompute = new ComputeShader(threshold);

this.autoSetColorTexture('inTex', this.thresholdCompute);
this.thresholdCompute.setSamplerTexture('inTex', this.getLastRenderTexture());
this.thresholdCompute.setStorageTexture(`outTex`, this.RT_threshold);
this.thresholdCompute.setUniformBuffer('bloomCfg', this.bloomSetting);
this.thresholdCompute.workerSizeX = Math.ceil(this.RT_threshold.width / 8);
Expand Down Expand Up @@ -199,7 +195,7 @@ export class BloomPost extends PostBase {

this.postCompute = new ComputeShader(post);

this.autoSetColorTexture('_MainTex', this.postCompute);
this.postCompute.setSamplerTexture('_MainTex', this.getLastRenderTexture());
this.postCompute.setSamplerTexture(`_BloomTex`, this.RT_BloomUp[N - 2]);
this.postCompute.setStorageTexture(`outTex`, this.RT_threshold);
this.postCompute.setUniformBuffer('bloomCfg', this.bloomSetting);
Expand Down
15 changes: 7 additions & 8 deletions src/gfx/renderJob/post/DepthOfFieldPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ export class DepthOfFieldPost extends PostBase {
* @internal
*/
blurTexture2: VirtualTexture;
/**
* @internal
*/
rendererPassState: RendererPassState;
/**
* @internal
*/
Expand All @@ -68,13 +64,13 @@ export class DepthOfFieldPost extends PostBase {
/**
* @internal
*/
onAttach(view: View3D,) {
public onAttach(view: View3D,) {
Engine3D.setting.render.postProcessing.depthOfView.enable = true;
}
/**
* @internal
*/
onDetach(view: View3D,) {
public onDetach(view: View3D,) {
Engine3D.setting.render.postProcessing.depthOfView.enable = false;
}

Expand Down Expand Up @@ -137,6 +133,9 @@ export class DepthOfFieldPost extends PostBase {

this.outTexture = output;
}

// set first input texture
this.blurComputes[0].setSamplerTexture('inTex', this.getLastRenderTexture());
}

private createResource() {
Expand Down Expand Up @@ -168,7 +167,7 @@ export class DepthOfFieldPost extends PostBase {
/**
* @internal
*/
render(view: View3D, command: GPUCommandEncoder) {
public render(view: View3D, command: GPUCommandEncoder) {
if (!this.blurComputes) {
this.createResource();
this.createBlurCompute();
Expand All @@ -179,7 +178,7 @@ export class DepthOfFieldPost extends PostBase {
}
this.rendererPassState = WebGPUDescriptorCreator.createRendererPassState(this.rtFrame, null);
}
this.autoSetColorTexture('inTex', this.blurComputes[0]);


let cfg = Engine3D.setting.render.postProcessing.depthOfView;
cfg.far = Math.max(cfg.near, cfg.far) + 0.0001;
Expand Down
9 changes: 9 additions & 0 deletions src/gfx/renderJob/post/FXAAPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { View3D } from '../../../core/View3D';
import { FXAAShader } from '../../../assets/shader/post/FXAAShader';
import { ViewQuad } from '../../../core/ViewQuad';
import { RenderTexture } from '../../../textures/RenderTexture';
import { GPUContext } from '../GPUContext';
/**
* FXAA(fast approximate antialiasing)
* A deformation anti-aliasing method that pays more attention to performance.
Expand Down Expand Up @@ -48,4 +49,12 @@ export class FXAAPost extends PostBase {
onDetach(view: View3D,) {
Engine3D.setting.render.postProcessing.fxaa.enable = false;
}

public render(view: View3D, command: GPUCommandEncoder) {
this.compute(view);
this.rtViewQuad.forEach((viewQuad, k) => {
let lastTexture = GPUContext.lastRenderPassState.getLastRenderTexture();
viewQuad.renderToViewQuad(view, viewQuad, command, lastTexture);
});
}
}
4 changes: 0 additions & 4 deletions src/gfx/renderJob/post/GBufferPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ export class GBufferPost extends PostBase {
*/
outTexture: VirtualTexture;

/**
* @internal
*/
rendererPassState: RendererPassState;
rtFrame: RTFrame;
view: View3D;
gBufferTexture: RenderTexture;
Expand Down
6 changes: 1 addition & 5 deletions src/gfx/renderJob/post/GTAOPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ export class GTAOPost extends PostBase {
* @internal
*/
gtaoTexture: VirtualTexture;
/**
* @internal
*/
rendererPassState: RendererPassState;
/**
* @internal
*/
Expand Down Expand Up @@ -168,7 +164,7 @@ export class GTAOPost extends PostBase {
this.gtaoCompute.setStorageBuffer('aoBuffer', this.aoBuffer);
let rtFrame = GBufferFrame.getGBufferFrame(GBufferFrame.colorPass_GBuffer);
this.gtaoCompute.setSamplerTexture(`gBufferTexture`, rtFrame.getCompressGBufferTexture());
this.autoSetColorTexture('inTex', this.gtaoCompute);
this.gtaoCompute.setSamplerTexture('inTex', this.getLastRenderTexture());
this.gtaoCompute.setStorageTexture(`outTex`, this.gtaoTexture);

this.gtaoSetting = gtaoSetting;
Expand Down
1 change: 0 additions & 1 deletion src/gfx/renderJob/post/GlobalFog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class GlobalFog extends PostBase {
public fogOpTexture: VirtualTexture;
private fogCompute: ComputeShader;
private fogUniform: UniformGPUBuffer;
private rendererPassState: RendererPassState;

constructor() {
super();
Expand Down
6 changes: 1 addition & 5 deletions src/gfx/renderJob/post/GodRayPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ export class GodRayPost extends PostBase {
* @internal
*/
godRayTexture: VirtualTexture;
/**
* @internal
*/
rendererPassState: RendererPassState;
/**
* @internal
*/
Expand Down Expand Up @@ -108,7 +104,7 @@ export class GodRayPost extends PostBase {

let rtFrame = GBufferFrame.getGBufferFrame(GBufferFrame.colorPass_GBuffer);
this.godRayCompute.setSamplerTexture(`gBufferTexture`, rtFrame.getCompressGBufferTexture());
this.autoSetColorTexture('inTex', this.godRayCompute);
this.godRayCompute.setSamplerTexture('inTex', this.getLastRenderTexture());
this.godRayCompute.setStorageTexture(`outTex`, this.godRayTexture);

let shadowRenderer = Engine3D.getRenderJob(view).shadowMapPassRenderer;
Expand Down
6 changes: 1 addition & 5 deletions src/gfx/renderJob/post/OutlinePost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ export class OutlinePost extends PostBase {
* @internal
*/
private lowTex: VirtualTexture;
/**
* @internal
*/
private rendererPassState: RendererPassState;
/**
* @internal
*/
Expand Down Expand Up @@ -197,7 +193,7 @@ export class OutlinePost extends PostBase {
//blend
this.blendCompute = new ComputeShader(OutLineBlendColor_cs);
this.blendCompute.setStorageBuffer('outlineSetting', this.outlineSetting);
this.autoSetColorTexture('inTex', this.blendCompute);
this.blendCompute.setSamplerTexture('inTex', this.getLastRenderTexture());
this.blendCompute.setSamplerTexture(`lowTex`, this.lowTex);
this.blendCompute.setStorageTexture(`outlineTex`, this.outlineTex);

Expand Down
34 changes: 8 additions & 26 deletions src/gfx/renderJob/post/PostBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { View3D } from '../../../core/View3D';
import { Reference } from '../../../util/Reference';
import { CResizeEvent } from '../../../event/CResizeEvent';
import { webGPUContext } from '../../graphics/webGpu/Context3D';
import { RendererPassState } from '../passRenderer/state/RendererPassState';
/**
* @internal
* Base class for post-processing effects
Expand All @@ -19,6 +20,7 @@ import { webGPUContext } from '../../graphics/webGpu/Context3D';
export class PostBase {
public enable: boolean = true;
public postRenderer: PostRenderer;
public rendererPassState: RendererPassState;
protected rtViewQuad: Map<string, ViewQuad>;
protected virtualTexture: Map<string, VirtualTexture>;

Expand All @@ -43,7 +45,7 @@ export class PostBase {
return viewQuad;
}

protected getOutTexture(): Texture {
protected getLastRenderTexture(): Texture {
let colorTexture: Texture;
let renderTargets = GPUContext.lastRenderPassState.renderTargets;
if (renderTargets.length > 0) {
Expand All @@ -54,35 +56,15 @@ export class PostBase {
return colorTexture;
}

protected autoSetColorTexture(name: string, compute: ComputeShader): void {
let input = this.getOutTexture() as VirtualTexture;
compute.setSamplerTexture(name, input);
}
/**
* @internal
*/
public compute(view: View3D) { }
/**
* @internal
*/

public onAttach(view: View3D) { }
/**
* @internal
*/

public onDetach(view: View3D) { }
public onResize() {

}
/**
* @internal
*/
public render(view: View3D, command: GPUCommandEncoder) {
this.compute(view);
this.rtViewQuad.forEach((viewQuad, k) => {
let lastTexture = GPUContext.lastRenderPassState.getLastRenderTexture();
viewQuad.renderToViewQuad(view, viewQuad, command, lastTexture);
});
}
public onResize() {}

public render(view: View3D, command: GPUCommandEncoder) {}

public destroy(force?: boolean) {
this.postRenderer = null;
Expand Down
6 changes: 1 addition & 5 deletions src/gfx/renderJob/post/SSGIPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ export class SSGIPost extends PostBase {
oldTexture: VirtualTexture;
combineTexture: VirtualTexture;

/**
* @internal
*/
rendererPassState: RendererPassState;
/**
* @internal
*/
Expand Down Expand Up @@ -201,7 +197,7 @@ export class SSGIPost extends PostBase {
this.ssgiCompute.setStorageTexture(`newTexture`, this.newTexture);
this.ssgiCompute.setUniformBuffer('globalUniform', globalUniform.uniformGPUBuffer);
this.ssgiCompute.setStorageBuffer('updateBuffer', this.updateBuffer);
this.autoSetColorTexture('inTex', this.ssgiCompute);
this.ssgiCompute.setSamplerTexture('inTex', this.getLastRenderTexture());
}

{
Expand Down
8 changes: 2 additions & 6 deletions src/gfx/renderJob/post/SSRPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ export class SSRPost extends PostBase {
* @internal
*/
finalTexture: VirtualTexture;
/**
* @internal
*/
rendererPassState: RendererPassState;
/**
* @internal
*/
Expand Down Expand Up @@ -182,7 +178,7 @@ export class SSRPost extends PostBase {
this.SSR_IS_Compute.setStorageBuffer(`rayTraceBuffer`, this.rayTraceData);
this.SSR_IS_Compute.setStorageBuffer(`ssrColorData`, this.ssrColorData);
this.SSR_IS_Compute.setStorageBuffer(`historyPosition`, this.historyPosition);
this.SSR_IS_Compute.setSamplerTexture(`colorMap`, this.getOutTexture());
this.SSR_IS_Compute.setSamplerTexture(`colorMap`, this.getLastRenderTexture());

this.SSR_IS_Compute.setStorageTexture(`outTex`, this.isRetTexture);

Expand All @@ -202,7 +198,7 @@ export class SSRPost extends PostBase {
let gBufferTexture = rtFrame.getCompressGBufferTexture();

this.SSR_Blend_Compute.setSamplerTexture("gBufferTexture", gBufferTexture);
this.SSR_Blend_Compute.setSamplerTexture("colorMap", this.getOutTexture());
this.SSR_Blend_Compute.setSamplerTexture("colorMap", this.getLastRenderTexture());
this.SSR_Blend_Compute.setSamplerTexture(`ssrMap`, input);
this.SSR_Blend_Compute.setStorageTexture(`outTex`, this.finalTexture);

Expand Down
6 changes: 1 addition & 5 deletions src/gfx/renderJob/post/TAAPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export class TAAPost extends PostBase {
*/
taaTexture: VirtualTexture;
outTexture: VirtualTexture;
/**
* @internal
*/
rendererPassState: RendererPassState;
/**
* @internal
*/
Expand Down Expand Up @@ -161,7 +157,7 @@ export class TAAPost extends PostBase {
let rtFrame = GBufferFrame.getGBufferFrame(GBufferFrame.colorPass_GBuffer);
computeShader.setSamplerTexture(`preColorTex`, this.preColorTex);
computeShader.setSamplerTexture(`gBufferTexture`, rtFrame.getCompressGBufferTexture());
this.autoSetColorTexture('inTex', computeShader);
computeShader.setSamplerTexture('inTex', this.getLastRenderTexture());
computeShader.setStorageTexture(`outTex`, this.taaTexture);

computeShader.workerSizeX = Math.ceil(this.taaTexture.width / 8);
Expand Down

0 comments on commit 8ddd262

Please sign in to comment.