Skip to content

Commit

Permalink
cache render passes render / compute pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
gnikoloff committed Dec 19, 2024
1 parent 290d120 commit 15766ad
Show file tree
Hide file tree
Showing 18 changed files with 398 additions and 1,157 deletions.
46 changes: 25 additions & 21 deletions src/app/render-passes/BlitRenderPass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
export default class BlitRenderPass extends RenderPass {
private static readonly BLOOM_MIX_FACTOR = 0.035

private renderPSO: GPURenderPipeline
private static renderPSO: GPURenderPipeline

private texturesBindGroupLayout: GPUBindGroupLayout
private textureBindGroup: GPUBindGroup
private bloomMixFactorBuffer: GPUBuffer
Expand Down Expand Up @@ -110,26 +111,29 @@ export default class BlitRenderPass extends RenderPass {
entries: texturesBindGroupLayoutEntries,
})

const renderPSODescriptor: GPURenderPipelineDescriptor = {
layout: RenderingContext.device.createPipelineLayout({
bindGroupLayouts: [this.texturesBindGroupLayout],
}),
vertex: {
module: vertexShaderModule,
entryPoint: FullScreenVertexShaderEntryFn,
},
fragment: {
module: fragmentShaderModule,
entryPoint: BLIT_FRAGMENT_SHADER_ENTRY_NAME,
targets,
},
primitive: {
topology: 'triangle-list',
cullMode: 'back',
},
}
if (!BlitRenderPass.renderPSO) {
const renderPSODescriptor: GPURenderPipelineDescriptor = {
layout: RenderingContext.device.createPipelineLayout({
bindGroupLayouts: [this.texturesBindGroupLayout],
}),
vertex: {
module: vertexShaderModule,
entryPoint: FullScreenVertexShaderEntryFn,
},
fragment: {
module: fragmentShaderModule,
entryPoint: BLIT_FRAGMENT_SHADER_ENTRY_NAME,
targets,
},
primitive: {
topology: 'triangle-list',
cullMode: 'back',
},
}

this.renderPSO = PipelineStates.createRenderPipeline(renderPSODescriptor)
BlitRenderPass.renderPSO =
PipelineStates.createRenderPipeline(renderPSODescriptor)
}

this.bloomMixFactorBuffer = RenderingContext.device.createBuffer({
label: 'Bloom Mix Factor GPU Buffer',
Expand Down Expand Up @@ -253,7 +257,7 @@ export default class BlitRenderPass extends RenderPass {

RenderingContext.setActiveRenderPass(this.type, renderPassEncoder)

RenderingContext.bindRenderPSO(this.renderPSO)
RenderingContext.bindRenderPSO(BlitRenderPass.renderPSO)
renderPassEncoder.setBindGroup(0, this.textureBindGroup)
renderPassEncoder.draw(6)

Expand Down
40 changes: 23 additions & 17 deletions src/app/render-passes/BloomDownscaleRenderPass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export default class BloomDownscaleRenderPass extends RenderPass {
private static readonly COMPUTE_WORKGROUP_SIZE_X = 8
private static readonly COMPUTE_WORKGROUP_SIZE_Y = 8

private computePSO: GPUComputePipeline
private static computePSO: GPUComputePipeline

private bindGroupLayout: GPUBindGroupLayout
private mipLevelCount: number
private sampler: GPUSampler
Expand Down Expand Up @@ -74,21 +75,26 @@ export default class BloomDownscaleRenderPass extends RenderPass {
entries: bindGroupLayoutEntries,
})

this.computePSO = PipelineStates.createComputePipeline({
label: 'Bloom Downscale Render PSO',
layout: RenderingContext.device.createPipelineLayout({
label: 'Bloom Downscale Render PSO Layout',
bindGroupLayouts: [this.bindGroupLayout],
}),
compute: {
entryPoint: BloomDownscaleShaderEntryFn,
module: PipelineStates.createShaderModule(BloomDownscaleShaderSrc),
constants: {
WORKGROUP_SIZE_X: BloomDownscaleRenderPass.COMPUTE_WORKGROUP_SIZE_X,
WORKGROUP_SIZE_Y: BloomDownscaleRenderPass.COMPUTE_WORKGROUP_SIZE_Y,
},
},
})
if (!BloomDownscaleRenderPass.computePSO) {
BloomDownscaleRenderPass.computePSO =
PipelineStates.createComputePipeline({
label: 'Bloom Downscale Render PSO',
layout: RenderingContext.device.createPipelineLayout({
label: 'Bloom Downscale Render PSO Layout',
bindGroupLayouts: [this.bindGroupLayout],
}),
compute: {
entryPoint: BloomDownscaleShaderEntryFn,
module: PipelineStates.createShaderModule(BloomDownscaleShaderSrc),
constants: {
WORKGROUP_SIZE_X:
BloomDownscaleRenderPass.COMPUTE_WORKGROUP_SIZE_X,
WORKGROUP_SIZE_Y:
BloomDownscaleRenderPass.COMPUTE_WORKGROUP_SIZE_Y,
},
},
})
}
}

public override render(
Expand Down Expand Up @@ -127,7 +133,7 @@ export default class BloomDownscaleRenderPass extends RenderPass {
const computePass = commandEncoder.beginComputePass({
label: 'Bloom Downscale Compute Pass',
})
computePass.setPipeline(this.computePSO)
computePass.setPipeline(BloomDownscaleRenderPass.computePSO)

for (let i = 1; i < this.mipLevelCount; i++) {
bindGroupEntries[0].resource = this.outTextures[0].createView({
Expand Down
42 changes: 24 additions & 18 deletions src/app/render-passes/BloomUpscaleRenderPass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
} from '../shaders/BloomUpscaleShader'

export default class BloomUpscaleRenderPass extends RenderPass {
private renderPSO: GPURenderPipeline
private static renderPSO: GPURenderPipeline

private bindGroupLayout: GPUBindGroupLayout
private sampler: GPUSampler
private filterRadiusBuffer: GPUBuffer
Expand Down Expand Up @@ -99,22 +100,27 @@ export default class BloomUpscaleRenderPass extends RenderPass {
},
},
]
this.renderPSO = PipelineStates.createRenderPipeline({
label: 'Bloom Upscale Render PSO',
layout: RenderingContext.device.createPipelineLayout({
label: 'Bloom Upscale Render PSO Layout',
bindGroupLayouts: [this.bindGroupLayout],
}),
vertex: {
entryPoint: FullScreenVertexShaderEntryFn,
module: PipelineStates.createShaderModule(FullScreenVertexShaderUtils),
},
fragment: {
entryPoint: BloomUpscaleShaderEntryFn,
module: PipelineStates.createShaderModule(BloomUpscaleShaderSrc),
targets: colorTargets,
},
})

if (!BloomUpscaleRenderPass.renderPSO) {
BloomUpscaleRenderPass.renderPSO = PipelineStates.createRenderPipeline({
label: 'Bloom Upscale Render PSO',
layout: RenderingContext.device.createPipelineLayout({
label: 'Bloom Upscale Render PSO Layout',
bindGroupLayouts: [this.bindGroupLayout],
}),
vertex: {
entryPoint: FullScreenVertexShaderEntryFn,
module: PipelineStates.createShaderModule(
FullScreenVertexShaderUtils
),
},
fragment: {
entryPoint: BloomUpscaleShaderEntryFn,
module: PipelineStates.createShaderModule(BloomUpscaleShaderSrc),
targets: colorTargets,
},
})
}
}

public override render(
Expand Down Expand Up @@ -163,7 +169,7 @@ export default class BloomUpscaleRenderPass extends RenderPass {
})

const renderPass = commandEncoder.beginRenderPass(renderPassDesc)
renderPass.setPipeline(this.renderPSO)
renderPass.setPipeline(BloomUpscaleRenderPass.renderPSO)

bindGroupEntries[0].resource = inputs[0].createView({
baseMipLevel: i,
Expand Down
Loading

0 comments on commit 15766ad

Please sign in to comment.