From 8ddd2620f0374bd8fbcebe2fabff7f38d4789de8 Mon Sep 17 00:00:00 2001 From: Codeboy-cn Date: Thu, 12 Dec 2024 23:39:32 +0800 Subject: [PATCH] chore(post): refine postBase rendererPassState remove autoSetColorTexture add getLastRenderTexture auto set GPUContext.lastRenderPassState --- .../passRenderer/post/PostRenderer.ts | 3 ++ src/gfx/renderJob/post/BloomPost.ts | 8 ++--- src/gfx/renderJob/post/DepthOfFieldPost.ts | 15 ++++---- src/gfx/renderJob/post/FXAAPost.ts | 9 +++++ src/gfx/renderJob/post/GBufferPost.ts | 4 --- src/gfx/renderJob/post/GTAOPost.ts | 6 +--- src/gfx/renderJob/post/GlobalFog.ts | 1 - src/gfx/renderJob/post/GodRayPost.ts | 6 +--- src/gfx/renderJob/post/OutlinePost.ts | 6 +--- src/gfx/renderJob/post/PostBase.ts | 34 +++++-------------- src/gfx/renderJob/post/SSGIPost.ts | 6 +--- src/gfx/renderJob/post/SSRPost.ts | 8 ++--- src/gfx/renderJob/post/TAAPost.ts | 6 +--- 13 files changed, 36 insertions(+), 76 deletions(-) diff --git a/src/gfx/renderJob/passRenderer/post/PostRenderer.ts b/src/gfx/renderJob/passRenderer/post/PostRenderer.ts index 0be0a34a..40ddd066 100644 --- a/src/gfx/renderJob/passRenderer/post/PostRenderer.ts +++ b/src/gfx/renderJob/passRenderer/post/PostRenderer.ts @@ -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); diff --git a/src/gfx/renderJob/post/BloomPost.ts b/src/gfx/renderJob/post/BloomPost.ts index e88d2a09..f5e5cf34 100644 --- a/src/gfx/renderJob/post/BloomPost.ts +++ b/src/gfx/renderJob/post/BloomPost.ts @@ -28,10 +28,6 @@ export class BloomPost extends PostBase { RT_BloomUp: VirtualTexture[]; RT_BloomDown: VirtualTexture[]; RT_threshold: VirtualTexture; - /** - * @internal - */ - rendererPassState: RendererPassState; /** * @internal */ @@ -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); @@ -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); diff --git a/src/gfx/renderJob/post/DepthOfFieldPost.ts b/src/gfx/renderJob/post/DepthOfFieldPost.ts index 695cd9f9..229f2ea0 100644 --- a/src/gfx/renderJob/post/DepthOfFieldPost.ts +++ b/src/gfx/renderJob/post/DepthOfFieldPost.ts @@ -44,10 +44,6 @@ export class DepthOfFieldPost extends PostBase { * @internal */ blurTexture2: VirtualTexture; - /** - * @internal - */ - rendererPassState: RendererPassState; /** * @internal */ @@ -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; } @@ -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() { @@ -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(); @@ -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; diff --git a/src/gfx/renderJob/post/FXAAPost.ts b/src/gfx/renderJob/post/FXAAPost.ts index ecd79a1c..3bae780e 100644 --- a/src/gfx/renderJob/post/FXAAPost.ts +++ b/src/gfx/renderJob/post/FXAAPost.ts @@ -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. @@ -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); + }); + } } diff --git a/src/gfx/renderJob/post/GBufferPost.ts b/src/gfx/renderJob/post/GBufferPost.ts index bb54f2a0..2a0bd294 100644 --- a/src/gfx/renderJob/post/GBufferPost.ts +++ b/src/gfx/renderJob/post/GBufferPost.ts @@ -39,10 +39,6 @@ export class GBufferPost extends PostBase { */ outTexture: VirtualTexture; - /** - * @internal - */ - rendererPassState: RendererPassState; rtFrame: RTFrame; view: View3D; gBufferTexture: RenderTexture; diff --git a/src/gfx/renderJob/post/GTAOPost.ts b/src/gfx/renderJob/post/GTAOPost.ts index 9b6675f9..ce28190e 100644 --- a/src/gfx/renderJob/post/GTAOPost.ts +++ b/src/gfx/renderJob/post/GTAOPost.ts @@ -32,10 +32,6 @@ export class GTAOPost extends PostBase { * @internal */ gtaoTexture: VirtualTexture; - /** - * @internal - */ - rendererPassState: RendererPassState; /** * @internal */ @@ -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; diff --git a/src/gfx/renderJob/post/GlobalFog.ts b/src/gfx/renderJob/post/GlobalFog.ts index 9dc6056f..cbf3ad05 100644 --- a/src/gfx/renderJob/post/GlobalFog.ts +++ b/src/gfx/renderJob/post/GlobalFog.ts @@ -32,7 +32,6 @@ export class GlobalFog extends PostBase { public fogOpTexture: VirtualTexture; private fogCompute: ComputeShader; private fogUniform: UniformGPUBuffer; - private rendererPassState: RendererPassState; constructor() { super(); diff --git a/src/gfx/renderJob/post/GodRayPost.ts b/src/gfx/renderJob/post/GodRayPost.ts index a39cb8f6..3df42a2c 100644 --- a/src/gfx/renderJob/post/GodRayPost.ts +++ b/src/gfx/renderJob/post/GodRayPost.ts @@ -23,10 +23,6 @@ export class GodRayPost extends PostBase { * @internal */ godRayTexture: VirtualTexture; - /** - * @internal - */ - rendererPassState: RendererPassState; /** * @internal */ @@ -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; diff --git a/src/gfx/renderJob/post/OutlinePost.ts b/src/gfx/renderJob/post/OutlinePost.ts index 2bb1b9ea..a45a9c04 100644 --- a/src/gfx/renderJob/post/OutlinePost.ts +++ b/src/gfx/renderJob/post/OutlinePost.ts @@ -44,10 +44,6 @@ export class OutlinePost extends PostBase { * @internal */ private lowTex: VirtualTexture; - /** - * @internal - */ - private rendererPassState: RendererPassState; /** * @internal */ @@ -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); diff --git a/src/gfx/renderJob/post/PostBase.ts b/src/gfx/renderJob/post/PostBase.ts index 660cde78..da19cfd8 100644 --- a/src/gfx/renderJob/post/PostBase.ts +++ b/src/gfx/renderJob/post/PostBase.ts @@ -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 @@ -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; protected virtualTexture: Map; @@ -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) { @@ -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; diff --git a/src/gfx/renderJob/post/SSGIPost.ts b/src/gfx/renderJob/post/SSGIPost.ts index 483aef17..67d81560 100644 --- a/src/gfx/renderJob/post/SSGIPost.ts +++ b/src/gfx/renderJob/post/SSGIPost.ts @@ -42,10 +42,6 @@ export class SSGIPost extends PostBase { oldTexture: VirtualTexture; combineTexture: VirtualTexture; - /** - * @internal - */ - rendererPassState: RendererPassState; /** * @internal */ @@ -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()); } { diff --git a/src/gfx/renderJob/post/SSRPost.ts b/src/gfx/renderJob/post/SSRPost.ts index 8095ceaf..d28bf039 100644 --- a/src/gfx/renderJob/post/SSRPost.ts +++ b/src/gfx/renderJob/post/SSRPost.ts @@ -48,10 +48,6 @@ export class SSRPost extends PostBase { * @internal */ finalTexture: VirtualTexture; - /** - * @internal - */ - rendererPassState: RendererPassState; /** * @internal */ @@ -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); @@ -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); diff --git a/src/gfx/renderJob/post/TAAPost.ts b/src/gfx/renderJob/post/TAAPost.ts index 7fbbf4ed..c07e7a3f 100644 --- a/src/gfx/renderJob/post/TAAPost.ts +++ b/src/gfx/renderJob/post/TAAPost.ts @@ -41,10 +41,6 @@ export class TAAPost extends PostBase { */ taaTexture: VirtualTexture; outTexture: VirtualTexture; - /** - * @internal - */ - rendererPassState: RendererPassState; /** * @internal */ @@ -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);