// luma.gl // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors import type {Texture} from '@luma.gl/core'; import type {ShaderPass, ShaderPassPipeline} from '@luma.gl/shadertools'; import {depthAwareBlur} from './depth-aware-blur'; import {depthHelpers} from './screen-space-shader-helpers'; export type SSAOShaderPassPipelineOptions = { normalSource?: 'reconstruct-from-depth' | 'normal-texture'; /** Fractional intermediate framebuffer resolution. Defaults to full resolution. */ resolutionScale?: number; }; type SSAOUniforms = { nearPlane: number; farPlane: number; radius: number; bias: number; intensity: number; useNormalTexture: number; }; type SSAOBindings = {depthTexture?: Texture; normalTexture?: Texture}; const ssaoEvaluateSource = /* wgsl */ `\ ${depthHelpers} struct ssaoEvaluateUniforms { nearPlane: f32, farPlane: f32, radius: f32, bias: f32, intensity: f32, useNormalTexture: f32, }; @group(0) @binding(auto) var ssaoEvaluate: ssaoEvaluateUniforms; @group(0) @binding(auto) var depthTexture: texture_depth_2d; @group(0) @binding(auto) var depthTextureSampler: sampler; @group(0) @binding(auto) var normalTexture: texture_2d; @group(0) @binding(auto) var normalTextureSampler: sampler; fn ssaoEvaluate_sampleColor( sourceTexture: texture_2d, sourceTextureSampler: sampler, texSize: vec2f, texCoord: vec2f ) -> vec4f { let sceneCoord = advancedSceneUV(texCoord); let depth = textureSample(depthTexture, depthTextureSampler, sceneCoord); let dimensions = vec2f(textureDimensions(depthTexture)); let linearDepth = advancedLinearDepth(depth, ssaoEvaluate.nearPlane, ssaoEvaluate.farPlane); let reconstructedNormal = advancedDepthNormal(depthTexture, depthTextureSampler, sceneCoord); let textureNormal = normalize(textureSample(normalTexture, normalTextureSampler, sceneCoord).xyz * 2.0 - 1.0); let normal = normalize(mix(reconstructedNormal, textureNormal, ssaoEvaluate.useNormalTexture)); let angle = fract(sin(dot(texCoord * dimensions, vec2f(12.9898, 78.233))) * 43758.5453) * 6.2831853; var occlusion = 0.0; for (var index: i32 = 0; index < 12; index++) { let sampleAngle = angle + f32(index) * 2.399963; let sampleRadius = (0.25 + 0.75 * f32(index + 1) / 12.0) * ssaoEvaluate.radius; let direction = vec2f(cos(sampleAngle), sin(sampleAngle)); let sampleUv = clamp(texCoord + direction * sampleRadius / dimensions, vec2f(0.0), vec2f(1.0)); let sampleDepth = textureSampleLevel(depthTexture, depthTextureSampler, advancedSceneUV(sampleUv), 0); let sampleLinearDepth = advancedLinearDepth(sampleDepth, ssaoEvaluate.nearPlane, ssaoEvaluate.farPlane); let rangeWeight = smoothstep(ssaoEvaluate.radius * 2.0, 0.0, abs(sampleLinearDepth - linearDepth)); let horizonWeight = max(dot(normal, normalize(vec3f(direction, 0.35))), 0.15); occlusion += select(0.0, rangeWeight * horizonWeight, sampleLinearDepth + ssaoEvaluate.bias < linearDepth); } let ambient = select(clamp(1.0 - occlusion / 12.0 * ssaoEvaluate.intensity, 0.0, 1.0), 1.0, depth >= 0.99999); return vec4f(vec3f(ambient), 1.0); } `; export const ssaoEvaluate = { name: 'ssaoEvaluate', source: ssaoEvaluateSource, bindingLayout: [ {name: 'depthTexture', group: 0}, {name: 'normalTexture', group: 0} ], props: {} as Partial & SSAOBindings, uniforms: {} as SSAOUniforms, bindings: {} as SSAOBindings, uniformTypes: { nearPlane: 'f32', farPlane: 'f32', radius: 'f32', bias: 'f32', intensity: 'f32', useNormalTexture: 'f32' }, propTypes: { nearPlane: {value: 0.1}, farPlane: {value: 200}, radius: {value: 7, min: 1, softMax: 32}, bias: {value: 0.03, min: 0, softMax: 0.2}, intensity: {value: 1.35, min: 0, softMax: 4}, useNormalTexture: {value: 0, min: 0, max: 1, private: true} }, passes: [{sampler: true}] } as const satisfies ShaderPass; export function createSSAOShaderPassPipeline( options: SSAOShaderPassPipelineOptions = {} ): ShaderPassPipeline<'ssaoRaw' | 'ssaoScratch' | 'ssaoBlurred'> { const scale = options.resolutionScale ?? 1; const useNormalTexture = options.normalSource === 'normal-texture' ? 1 : 0; const evaluateInputs: Record = {sourceTexture: 'previous'}; if (!useNormalTexture) { evaluateInputs['normalTexture'] = 'previous'; } return { name: 'ssaoShaderPassPipeline', renderTargets: { ssaoRaw: {scale: [scale, scale], format: 'rgba8unorm'}, ssaoScratch: {scale: [scale, scale], format: 'rgba8unorm'}, ssaoBlurred: {scale: [scale, scale], format: 'rgba8unorm'} }, steps: [ { shaderPass: ssaoEvaluate, inputs: evaluateInputs, output: 'ssaoRaw', uniforms: {useNormalTexture} }, { shaderPass: depthAwareBlur, inputs: {sourceTexture: 'ssaoRaw'}, output: 'ssaoScratch', uniforms: {direction: [1, 0]} }, { shaderPass: depthAwareBlur, inputs: {sourceTexture: 'ssaoScratch'}, output: 'ssaoBlurred', uniforms: {direction: [0, 1]} }, { shaderPass: ssaoComposite, inputs: {sourceTexture: 'previous', ambientOcclusionTexture: 'ssaoBlurred'}, output: 'previous' } ] }; } export const ssaoComposite = { name: 'ssaoComposite', source: /* wgsl */ `\ struct ssaoCompositeUniforms { debugMode: f32, }; @group(0) @binding(auto) var ssaoComposite: ssaoCompositeUniforms; @group(0) @binding(auto) var ambientOcclusionTexture: texture_2d; @group(0) @binding(auto) var ambientOcclusionTextureSampler: sampler; fn ssaoComposite_sampleColor( sourceTexture: texture_2d, sourceTextureSampler: sampler, texSize: vec2f, texCoord: vec2f ) -> vec4f { let color = textureSample(sourceTexture, sourceTextureSampler, texCoord); let ambient = textureSample(ambientOcclusionTexture, ambientOcclusionTextureSampler, texCoord).r; if (ssaoComposite.debugMode > 0.5) { return vec4f(vec3f(ambient), 1.0); } let rawOcclusion = clamp(1.0 - ambient, 0.0, 1.0); let contactOcclusion = smoothstep(0.03, 0.5, rawOcclusion); return vec4f(color.rgb * (1.0 - contactOcclusion * 0.48), color.a); }`, bindingLayout: [{name: 'ambientOcclusionTexture', group: 0}], uniformTypes: {debugMode: 'f32'}, propTypes: {debugMode: {value: 0, min: 0, max: 1, private: true}}, passes: [{sampler: true}] } as const satisfies ShaderPass;