Skip to content

Conversation

@mrdoob
Copy link
Owner

@mrdoob mrdoob commented Nov 7, 2025

Related issue: #32181

Description

This PR applies the same optimizations made to the WebGL VSM shadow code to the WebGPU renderer's VSMShadowFilter:

  • Variable clarity: Extract mean variable to avoid repeated distribution.x references
  • Early return optimization: Exit immediately when hardShadow == 1.0 (fully lit pixels), skipping unnecessary VSM calculations
  • Improved naming: Rename distanced and softnessProbabilityp_max for consistency
  • Numerical stability: Correct variance epsilon from 0 to 0.0000001 to prevent division by zero
  • Pre-computed constant: Simplify from .div(0.95 - 0.3) to .div(0.65)
  • Removed redundancy: Eliminate outer clamp() call as values are already in valid range
  • Better documentation: Add clarifying comments for each calculation step

Benefits

  • Performance: Fewer operations per pixel, especially for fully lit areas
  • Stability: Better numerical handling prevents potential division by zero
  • Maintainability: Clearer code with improved variable names and comments
  • Consistency: WebGPU and WebGL renderers now use the same optimized VSM logic

Visual output remains identical.

Port optimizations from PR #32181 to the WebGPU renderer:
- Extract mean variable for clarity
- Add early return optimization for fully lit pixels
- Improve variable naming (d, p_max)
- Correct variance epsilon from 0 to 0.0000001
- Pre-compute constant (0.65 instead of 0.95 - 0.3)
- Remove redundant outer clamp
- Add clarifying comments

These changes improve performance by reducing operations per pixel
while maintaining identical visual output.
@mrdoob mrdoob added this to the r182 milestone Nov 7, 2025
@github-actions
Copy link

github-actions bot commented Nov 7, 2025

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 356.24
86.47
356.24
86.47
+0 B
+0 B
WebGPU 610.61
171.3
610.57
171.28
-39 B
-17 B
WebGPU Nodes 609.22
171.04
609.18
171.02
-39 B
-20 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 487.87
121.22
487.87
121.22
+0 B
+0 B
WebGPU 679.39
186.53
679.35
186.51
-39 B
-15 B
WebGPU Nodes 621.11
169.71
621.07
169.69
-39 B
-16 B

@mrdoob mrdoob merged commit 000d4fb into dev Nov 7, 2025
10 checks passed
@mrdoob mrdoob deleted the claude/port-webgpu-render-011CUsxE9cKq5aPDnd4WNZBt branch November 7, 2025 06:32
let softnessProbability = variance.div( variance.add( distance.mul( distance ) ) ); // Chebeyshevs inequality
softnessProbability = clamp( sub( softnessProbability, 0.3 ).div( 0.95 - 0.3 ) );
occlusion.assign( clamp( max( hardShadow, softnessProbability ) ) );
return float( 1.0 );
Copy link
Collaborator

@Mugen87 Mugen87 Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line triggers a new warning in the VSM demo webgpu_shadowmap_vsm:

THREE.TSL: Return statement used in an inline 'Fn()'. Define a layout struct to allow return values.

/cc @sunag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants