Skip to content

Skip CAS HDR conversions for LDR input and use half-precision in WGSL#8509

Merged
mvaligursky merged 1 commit intomainfrom
mv-cas-ldr-optimization
Mar 6, 2026
Merged

Skip CAS HDR conversions for LDR input and use half-precision in WGSL#8509
mvaligursky merged 1 commit intomainfrom
mv-cas-ldr-optimization

Conversation

@mvaligursky
Copy link
Contributor

@mvaligursky mvaligursky commented Mar 6, 2026

Skip unnecessary HDR-to-LDR tone mapping in the CAS (Contrast Adaptive Sharpening) shader when the scene texture is already LDR (RGBA8), and use half-precision types for the CAS computation in WGSL.

Changes:

  • Add hdrScene property to RenderPassCompose to track whether the scene texture is HDR
  • Set hdrScene based on hdrFormat in RenderPassCameraFrame.setupComposePass
  • Conditionally define CAS_HDR shader define only when the scene is HDR
  • When LDR, toSDR/toHDR become identity functions — the compiler eliminates them, avoiding redundant tone mapping math
  • Use half3/half types for CAS intermediate calculations in the WGSL shader

Performance:

  • LDR scenes skip the toSDR/toHDR Reinhard tone mapping entirely (6 divides + 4 max operations removed)
  • WGSL CAS computation uses half-precision for reduced register pressure and better ALU throughput on capable GPUs

When the scene texture is already LDR (RGBA8), the toSDR/toHDR tone mapping
in the CAS shader is unnecessary. This adds a CAS_HDR define that is only
set for HDR scenes, with identity functions for the LDR path. Also uses
half-precision types for the CAS computation in WGSL.

Made-with: Cursor
@mvaligursky mvaligursky self-assigned this Mar 6, 2026
@mvaligursky mvaligursky requested review from a team and Copilot March 6, 2026 14:44
@mvaligursky mvaligursky merged commit 6a6ad8d into main Mar 6, 2026
10 checks passed
@mvaligursky mvaligursky deleted the mv-cas-ldr-optimization branch March 6, 2026 14:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the Contrast Adaptive Sharpening (CAS) post-processing shader in two ways: (1) it skips unnecessary HDR↔LDR Reinhard tone mapping when the scene texture is already in LDR format (RGBA8), and (2) uses half-precision (half3/half) types for the CAS intermediate calculations in the WGSL shader.

Changes:

  • Add hdrScene property to RenderPassCompose to control whether the CAS_HDR define is emitted, which selects between real and identity tone-mapping functions
  • Set hdrScene from hdrFormat !== PIXELFORMAT_RGBA8 in setupComposePass
  • Update GLSL and WGSL CAS shaders to use #ifdef CAS_HDR branching for tone-mapping functions, and convert WGSL intermediate values to half3/half

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/extras/render-passes/render-pass-compose.js Adds _hdrScene field, getter/setter with shader-dirty invalidation, and emits CAS_HDR define when sharpening is enabled and scene is HDR; updates shader cache key
src/extras/render-passes/render-pass-camera-frame.js Sets composePass.hdrScene based on whether the format is HDR (not PIXELFORMAT_RGBA8)
src/scene/shader-lib/glsl/chunks/render-pass/frag/compose/compose-cas.js Wraps HDR tone-mapping functions in #ifdef CAS_HDR; #else branch provides identity functions
src/scene/shader-lib/wgsl/chunks/render-pass/frag/compose/compose-cas.js Same #ifdef CAS_HDR branching as GLSL; also converts intermediate CAS values to half3/half

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -8,9 +8,14 @@ export default /* glsl */`
uniform float sharpness;

// reversible LDR <-> HDR tone mapping, as CAS needs LDR input
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

The comment // reversible LDR <-> HDR tone mapping, as CAS needs LDR input (line 10 in the GLSL file and same in WGSL) now precedes a #ifdef CAS_HDR / #else block. In the #else (LDR) branch, no tone mapping is performed at all — toSDR and toHDR are identity functions. The comment is therefore misleading in the LDR case. It should be updated to clarify that the tone mapping is only applied for HDR scenes, for example: // reversible LDR <-> HDR tone mapping (HDR only), as CAS needs LDR input.

Suggested change
// reversible LDR <-> HDR tone mapping, as CAS needs LDR input
// reversible LDR <-> HDR tone mapping (HDR only), as CAS needs LDR input

Copilot uses AI. Check for mistakes.
@@ -8,33 +8,38 @@ export default /* wgsl */`
uniform sharpness: f32;

// reversible LDR <-> HDR tone mapping, as CAS needs LDR input
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

The comment // reversible LDR <-> HDR tone mapping, as CAS needs LDR input (line 10) now precedes a #ifdef CAS_HDR / #else block. In the #else (LDR) branch, no tone mapping is performed at all — toSDR and toHDR are identity functions. The comment is therefore misleading in the LDR case. It should be updated to clarify that the tone mapping is only applied for HDR scenes, for example: // reversible LDR <-> HDR tone mapping (HDR only), as CAS needs LDR input.

Suggested change
// reversible LDR <-> HDR tone mapping, as CAS needs LDR input
// reversible LDR <-> HDR tone mapping (HDR only), as CAS needs LDR input

Copilot uses AI. Check for mistakes.
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.

2 participants