Skip to content

Commit

Permalink
dynamic performane downgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
gnikoloff committed Dec 19, 2024
1 parent 808cc8a commit 290d120
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 120 deletions.
248 changes: 150 additions & 98 deletions src/app/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
RenderPassType,
placeholderFunc,
} from '../renderer/types'
import { TextureDebugMeshType } from '../types'
import {
BLIT_PASS_REVEAL_ANIM_DURATION_MS,
ENVIRONMENT_CUBE_TEXTURE_FACE_URLS,
Expand Down Expand Up @@ -56,6 +55,7 @@ import {
} from './constants'

import { lerp } from '../renderer/math/math'
import { TextureDebugMeshType } from '../types'
import LineDebugDrawable from './debug/LineDebugDrawable'
import TexturesDebugContainer from './debug/textures-debug/TexturesDebugContainer'
import DebugStatsContainer from './debug/timings-debug/DebugStatsContainer'
Expand Down Expand Up @@ -144,6 +144,9 @@ export default class Renderer extends RenderingContext {

private resizeCounter = 0

private viewportWidth = 0
private viewportHeight = 0

public set sunPositionX(v: number) {
this.lightingManager.sunPositionX = v
}
Expand All @@ -160,12 +163,10 @@ export default class Renderer extends RenderingContext {
this.lightingManager.sunIntensity = v
}

private _ssaoEnabled = true
public set ssaoEnabled(v: boolean) {
;(
this.renderPassComposer.getPass(
RenderPassType.DirectionalAmbientLighting
) as DirectionalAmbientLightRenderPass
).ssaoMixFactor = v ? 1 : 0
this._ssaoEnabled = v
this.recreateRenderComposer()
}

public set ssaoKernelSize(v: number) {
Expand Down Expand Up @@ -302,25 +303,18 @@ export default class Renderer extends RenderingContext {
).debugMissedIntersections = v
}

private _ssrEnabled = true

public set ssrEnabled(v: boolean) {
this.renderPassComposer.getPass(RenderPassType.Reflection).enabled = v
this.renderPassComposer
.getPass(RenderPassType.TAAResolve)
.clearInputTextures()
.addInputTextures([
v
? RENDER_PASS_COMPUTED_REFLECTIONS_TEXTURE
: RENDER_PASS_LIGHTING_RESULT_TEXTURE,
RENDER_PASS_VELOCITY_TEXTURE,
])
this._ssrEnabled = v
this.recreateRenderComposer()
}

private _bloomEnabled = true
public set bloomEnabled(v: boolean) {
this.renderPassComposer.getPass(RenderPassType.BloomDownsample).enabled = v
this.renderPassComposer.getPass(RenderPassType.BloomUpsample).enabled = v
;(
this.renderPassComposer.getPass(RenderPassType.Blit) as BlitRenderPass
).bloomEnabled = v
this._bloomEnabled = v

this.recreateRenderComposer()
}

public set bloomFilterRadius(v: number) {
Expand Down Expand Up @@ -513,7 +507,10 @@ export default class Renderer extends RenderingContext {
})
}

private recreateRenderComposer(width: number, height: number) {
private recreateRenderComposer(
width = this.viewportWidth,
height = this.viewportHeight
) {
this.renderPassComposer?.destroy()

this.renderPassComposer = new RenderPassComposer()
Expand All @@ -536,17 +533,33 @@ export default class Renderer extends RenderingContext {
RENDER_PASS_DEPTH_STENCIL_TEXTURE,
])

const ssaoRenderPass = new SSAORenderPass(width, height)
.setCamera(this.mainCamera)
.addInputTextures([
RENDER_PASS_NORMAL_METALLIC_ROUGHNESS_TEXTURE,
RENDER_PASS_DEPTH_STENCIL_TEXTURE,
])
.addOutputTexture(RENDER_PASS_SSAO_TEXTURE)
let ssaoRenderPass: SSAORenderPass
let ssaoBlurRenderPass: SSAOBlurRenderPass

if (this._ssaoEnabled) {
ssaoRenderPass = new SSAORenderPass(width, height)
.addInputTextures([
RENDER_PASS_NORMAL_METALLIC_ROUGHNESS_TEXTURE,
RENDER_PASS_DEPTH_STENCIL_TEXTURE,
])
.addOutputTexture(RENDER_PASS_SSAO_TEXTURE)
.setCamera(this.mainCamera)

ssaoBlurRenderPass = new SSAOBlurRenderPass(width, height)
.addInputTexture(RENDER_PASS_SSAO_TEXTURE)
.addOutputTexture(RENDER_PASS_SSAO_BLUR_TEXTURE)
}

const dirAmbientLightRenderPassInputTexNames = [
RENDER_PASS_NORMAL_METALLIC_ROUGHNESS_TEXTURE,
RENDER_PASS_ALBEDO_REFLECTANCE_TEXTURE,
RENDER_PASS_DEPTH_STENCIL_TEXTURE,
RENDER_PASS_DIRECTIONAL_LIGHT_DEPTH_TEXTURE,
]

const ssaoBlurRenderPass = new SSAOBlurRenderPass(width, height)
.addInputTexture(RENDER_PASS_SSAO_TEXTURE)
.addOutputTexture(RENDER_PASS_SSAO_BLUR_TEXTURE)
if (this._ssaoEnabled) {
dirAmbientLightRenderPassInputTexNames.push(RENDER_PASS_SSAO_TEXTURE)
}

const directionalAmbientLightRenderPass =
new DirectionalAmbientLightRenderPass(
Expand All @@ -555,15 +568,11 @@ export default class Renderer extends RenderingContext {
height
)
.setCamera(this.mainCamera)
.addInputTextures([
RENDER_PASS_NORMAL_METALLIC_ROUGHNESS_TEXTURE,
RENDER_PASS_ALBEDO_REFLECTANCE_TEXTURE,
RENDER_PASS_DEPTH_STENCIL_TEXTURE,
RENDER_PASS_SSAO_BLUR_TEXTURE,
RENDER_PASS_DIRECTIONAL_LIGHT_DEPTH_TEXTURE,
])
.addInputTextures(dirAmbientLightRenderPassInputTexNames)
.addOutputTexture(RENDER_PASS_LIGHTING_RESULT_TEXTURE)

directionalAmbientLightRenderPass.ssaoMixFactor = this._ssaoEnabled ? 1 : 0

if (this.envDiffuseTexture) {
directionalAmbientLightRenderPass.setDiffuseIBLTexture(
this.envDiffuseTexture
Expand All @@ -581,16 +590,21 @@ export default class Renderer extends RenderingContext {
)
}

const pointLightNonInstancedTexInputNames = [
RENDER_PASS_NORMAL_METALLIC_ROUGHNESS_TEXTURE,
RENDER_PASS_ALBEDO_REFLECTANCE_TEXTURE,
RENDER_PASS_DEPTH_STENCIL_TEXTURE,
RENDER_PASS_LIGHTING_RESULT_TEXTURE,
]

if (this._ssaoEnabled) {
pointLightNonInstancedTexInputNames.push(RENDER_PASS_SSAO_TEXTURE)
}

const pointLightsNonInstancedNonCulledRenderPass =
new PointLightsNonCulledRenderPass(width, height)
.setCamera(this.mainCamera)
.addInputTextures([
RENDER_PASS_NORMAL_METALLIC_ROUGHNESS_TEXTURE,
RENDER_PASS_ALBEDO_REFLECTANCE_TEXTURE,
RENDER_PASS_DEPTH_STENCIL_TEXTURE,
RENDER_PASS_SSAO_BLUR_TEXTURE,
RENDER_PASS_LIGHTING_RESULT_TEXTURE,
])
.addInputTextures(pointLightNonInstancedTexInputNames)
.addOutputTexture(RENDER_PASS_LIGHTING_RESULT_TEXTURE)

const pointLightsStencilMaskPass = new PointLightsMaskPass(width, height)
Expand All @@ -600,15 +614,20 @@ export default class Renderer extends RenderingContext {
.setLightsBuffer(this.lightingManager.gpuBuffer)
.updateLightsMaskBindGroup()

const pointLightsRenderPassInputTexNames = [
RENDER_PASS_NORMAL_METALLIC_ROUGHNESS_TEXTURE,
RENDER_PASS_ALBEDO_REFLECTANCE_TEXTURE,
RENDER_PASS_DEPTH_STENCIL_TEXTURE,
RENDER_PASS_LIGHTING_RESULT_TEXTURE,
]

if (this._ssaoEnabled) {
pointLightsRenderPassInputTexNames.push(RENDER_PASS_SSAO_TEXTURE)
}

const pointLightsRenderPass = new PointLightsRenderPass(width, height)
.setCamera(this.mainCamera)
.addInputTextures([
RENDER_PASS_NORMAL_METALLIC_ROUGHNESS_TEXTURE,
RENDER_PASS_ALBEDO_REFLECTANCE_TEXTURE,
RENDER_PASS_DEPTH_STENCIL_TEXTURE,
RENDER_PASS_SSAO_BLUR_TEXTURE,
RENDER_PASS_LIGHTING_RESULT_TEXTURE,
])
.addInputTextures(pointLightsRenderPassInputTexNames)
.addOutputTexture(RENDER_PASS_LIGHTING_RESULT_TEXTURE)

const transparentRenderPass = new TransparentRenderPass(width, height)
Expand All @@ -633,70 +652,104 @@ export default class Renderer extends RenderingContext {
RENDER_PASS_DEPTH_STENCIL_TEXTURE,
])

const hiZCopyDepthComputePass = new HiZCopyDepthComputePass(width, height)
.addInputTexture(RENDER_PASS_DEPTH_STENCIL_TEXTURE)
.addOutputTexture(RENDER_PASS_HI_Z_DEPTH_TEXTURE)
let hiZCopyDepthComputePass: HiZCopyDepthComputePass
let hiZDepthComputePass: HiZDepthComputePass
let reflectionsComputePass: ReflectionComputePass

const hiZDepthComputePass = new HiZDepthComputePass(width, height)
.addInputTexture(RENDER_PASS_HI_Z_DEPTH_TEXTURE)
.addOutputTexture(RENDER_PASS_HI_Z_DEPTH_TEXTURE)
if (this._ssrEnabled) {
hiZCopyDepthComputePass = new HiZCopyDepthComputePass(width, height)
.addInputTexture(RENDER_PASS_DEPTH_STENCIL_TEXTURE)
.addOutputTexture(RENDER_PASS_HI_Z_DEPTH_TEXTURE)

const reflectionsComputePass = new ReflectionComputePass(width, height)
.setCamera(this.mainCamera)
.addInputTextures([
RENDER_PASS_LIGHTING_RESULT_TEXTURE,
RENDER_PASS_NORMAL_METALLIC_ROUGHNESS_TEXTURE,
RENDER_PASS_ALBEDO_REFLECTANCE_TEXTURE,
RENDER_PASS_HI_Z_DEPTH_TEXTURE,
])
.addOutputTexture(RENDER_PASS_COMPUTED_REFLECTIONS_TEXTURE)
hiZDepthComputePass = new HiZDepthComputePass(width, height)
.addInputTexture(RENDER_PASS_HI_Z_DEPTH_TEXTURE)
.addOutputTexture(RENDER_PASS_HI_Z_DEPTH_TEXTURE)
reflectionsComputePass = new ReflectionComputePass(width, height)
.setCamera(this.mainCamera)
.addInputTextures([
RENDER_PASS_LIGHTING_RESULT_TEXTURE,
RENDER_PASS_NORMAL_METALLIC_ROUGHNESS_TEXTURE,
RENDER_PASS_ALBEDO_REFLECTANCE_TEXTURE,
RENDER_PASS_HI_Z_DEPTH_TEXTURE,
])
.addOutputTexture(RENDER_PASS_COMPUTED_REFLECTIONS_TEXTURE)
}

const taaResolveRenderPass = new TAAResolveRenderPass(width, height)
.addInputTextures([
RENDER_PASS_COMPUTED_REFLECTIONS_TEXTURE,
this._ssrEnabled
? RENDER_PASS_COMPUTED_REFLECTIONS_TEXTURE
: RENDER_PASS_LIGHTING_RESULT_TEXTURE,
RENDER_PASS_VELOCITY_TEXTURE,
])
.addOutputTexture(RENDER_PASS_TAA_RESOLVE_TEXTURE)

const bloomDownscaleRenderPass = new BloomDownscaleRenderPass(width, height)
.addInputTexture(RENDER_PASS_TAA_RESOLVE_TEXTURE)
.addOutputTexture(RENDER_PASS_BLOOM_TEXTURE)
let bloomDownscaleRenderPass: BloomDownscaleRenderPass
let bloomUpscaleRenderPass: BloomUpscaleRenderPass

if (this._bloomEnabled) {
bloomDownscaleRenderPass = new BloomDownscaleRenderPass(width, height)
.addInputTexture(RENDER_PASS_TAA_RESOLVE_TEXTURE)
.addOutputTexture(RENDER_PASS_BLOOM_TEXTURE)

bloomUpscaleRenderPass = new BloomUpscaleRenderPass(width, height)
.addInputTexture(RENDER_PASS_BLOOM_TEXTURE)
.addOutputTexture(RENDER_PASS_BLOOM_TEXTURE)
}

const bloomUpscaleRenderPass = new BloomUpscaleRenderPass(width, height)
.addInputTexture(RENDER_PASS_BLOOM_TEXTURE)
.addOutputTexture(RENDER_PASS_BLOOM_TEXTURE)
const blitRenderPassInputs: string[] = []

if (this._bloomEnabled) {
blitRenderPassInputs.push(RENDER_PASS_BLOOM_TEXTURE)
}
blitRenderPassInputs.push(RENDER_PASS_TAA_RESOLVE_TEXTURE)

const blitRenderPass = new BlitRenderPass(
width,
height,
this.resizeCounter > 0
).addInputTextures([
RENDER_PASS_BLOOM_TEXTURE,
RENDER_PASS_TAA_RESOLVE_TEXTURE,
])
).addInputTextures(blitRenderPassInputs)

blitRenderPass.bloomEnabled = this._bloomEnabled

this.renderPassComposer.addPass(shadowRenderPass).addPass(gbufferRenderPass)

if (this._ssaoEnabled) {
this.renderPassComposer
.addPass(ssaoRenderPass)
.addPass(ssaoBlurRenderPass)
}

this.renderPassComposer
.addPass(shadowRenderPass)
.addPass(gbufferRenderPass)
.addPass(ssaoRenderPass)
.addPass(ssaoBlurRenderPass)
.addPass(directionalAmbientLightRenderPass)
.addPass(pointLightsNonInstancedNonCulledRenderPass)
.addPass(pointLightsStencilMaskPass)
.addPass(pointLightsRenderPass)
.addPass(transparentRenderPass)
.addPass(skyboxRenderPass)
.addPass(hiZCopyDepthComputePass)
.addPass(hiZDepthComputePass)
.addPass(reflectionsComputePass)
.addPass(taaResolveRenderPass)
.addPass(bloomDownscaleRenderPass)
.addPass(bloomUpscaleRenderPass)
// .addPass(debugBBoxesPass)
.addPass(blitRenderPass)

if (this._ssrEnabled) {
this.renderPassComposer
.addPass(hiZCopyDepthComputePass)
.addPass(hiZDepthComputePass)
.addPass(reflectionsComputePass)
}

this.renderPassComposer.addPass(taaResolveRenderPass)

if (this._bloomEnabled) {
this.renderPassComposer
.addPass(bloomDownscaleRenderPass)
.addPass(bloomUpscaleRenderPass)
}

this.renderPassComposer.addPass(blitRenderPass)
}

public resize(w: number, h: number) {
this.viewportWidth = w
this.viewportHeight = h

this.debugCamera.onResize(w, h)
this.mainCamera.onResize(w, h)

Expand Down Expand Up @@ -749,10 +802,11 @@ export default class Renderer extends RenderingContext {
RENDER_PASS_NORMAL_METALLIC_ROUGHNESS_TEXTURE
)
)
.setTextureGBufferSection(
TextureDebugMeshType.AO,
this.renderPassComposer.getTexture(RENDER_PASS_SSAO_BLUR_TEXTURE)
)
// .setTextureGBufferSection(
// TextureDebugMeshType.AO,
// this.renderPassComposer.getTexture(RENDER_PASS_SSAO_BLUR_TEXTURE) ||
// TextureLoader.dummyR16FTexture
// )
.setTextureGBufferSection(
TextureDebugMeshType.Reflectance,
this.renderPassComposer.getTexture(
Expand Down Expand Up @@ -814,9 +868,7 @@ export default class Renderer extends RenderingContext {
const fpsAverageStat = this.fpsDisplayAverage.get()
const gpuAverageStat = this.gpuAverage.get()

if (fpsAverageStat > 70) {
this.mainCameraCtrl.speed = 20
}
this.mainCameraCtrl.speed = fpsAverageStat > 70 ? 20 : 40

if (RenderingContext.supportsGPUTimestampQuery) {
this.timingDebugContainer.setDisplayValue(
Expand Down
2 changes: 2 additions & 0 deletions src/app/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,5 @@ export const MAIN_CAMERA_LOAD_ANIM_EASE: EaseType = 'quad_Out'
export const FIREWORK_PARTICLES_LOAD_ANIM_DELAY_MS = 1000
export const FIREWORK_PARTICLES_LOAD_ANIM_DURATION_MS = 500
export const FIREWORK_PARTICLES_LOAD_ANIM_EASE: EaseType = 'quad_Out'

export const PROFILE_MAX_FRAMES_COUNT = 70
2 changes: 0 additions & 2 deletions src/app/lighting/LightingSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,6 @@ export default class LightingSystem extends LightingManager {
)
}

console.log(this.lightsCount)

// this.particlesLength = this.particles.length;

this.updateGPUBuffer()
Expand Down
Loading

0 comments on commit 290d120

Please sign in to comment.