Skip CAS HDR conversions for LDR input and use half-precision in WGSL#8509
Skip CAS HDR conversions for LDR input and use half-precision in WGSL#8509mvaligursky merged 1 commit intomainfrom
Conversation
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
There was a problem hiding this comment.
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
hdrSceneproperty toRenderPassComposeto control whether theCAS_HDRdefine is emitted, which selects between real and identity tone-mapping functions - Set
hdrScenefromhdrFormat !== PIXELFORMAT_RGBA8insetupComposePass - Update GLSL and WGSL CAS shaders to use
#ifdef CAS_HDRbranching for tone-mapping functions, and convert WGSL intermediate values tohalf3/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 | |||
There was a problem hiding this comment.
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.
| // reversible LDR <-> HDR tone mapping, as CAS needs LDR input | |
| // reversible LDR <-> HDR tone mapping (HDR only), as CAS needs LDR input |
| @@ -8,33 +8,38 @@ export default /* wgsl */` | |||
| uniform sharpness: f32; | |||
|
|
|||
| // reversible LDR <-> HDR tone mapping, as CAS needs LDR input | |||
There was a problem hiding this comment.
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.
| // reversible LDR <-> HDR tone mapping, as CAS needs LDR input | |
| // reversible LDR <-> HDR tone mapping (HDR only), as CAS needs LDR input |
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:
hdrSceneproperty toRenderPassComposeto track whether the scene texture is HDRhdrScenebased onhdrFormatinRenderPassCameraFrame.setupComposePassCAS_HDRshader define only when the scene is HDRtoSDR/toHDRbecome identity functions — the compiler eliminates them, avoiding redundant tone mapping mathhalf3/halftypes for CAS intermediate calculations in the WGSL shaderPerformance:
toSDR/toHDRReinhard tone mapping entirely (6 divides + 4 max operations removed)