[RFC,v2,5/8] libcamera: shaders: Add LSC support
diff mbox series

Message ID 20260420183949.110548-6-mzamazal@redhat.com
State New
Headers show
Series
  • LSC for SoftISP simple pipeline
Related show

Commit Message

Milan Zamazal April 20, 2026, 6:39 p.m. UTC
From: Xander Pronk <xander.c.pronk@gmail.com>

Lens shading correction should be applied after black level
subtraction (in order to make the computations with meaningful values)
and before white balance (especially before white balance stats are
computed).

Note that lens shading correction depends on temperature, which depends
on white balance, so there is a chicken-egg problem.  Currently, we
determine white balance and temperature from the preceding frame.

TODO: It's unknown why the LSC is computed in the shader the way it is.

Co-developed-by: Rick ten Wolde <rick_libcamera@wolde.info>
Signed-off-by: Rick ten Wolde <rick_libcamera@wolde.info>
Signed-off-by: Xander Pronk <xander.c.pronk@gmail.com>
Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
---
 src/libcamera/shaders/bayer_1x_packed.frag | 8 ++++++++
 src/libcamera/shaders/bayer_unpacked.frag  | 8 ++++++++
 2 files changed, 16 insertions(+)

Comments

Milan Zamazal April 20, 2026, 6:43 p.m. UTC | #1
Milan Zamazal <mzamazal@redhat.com> writes:

> From: Xander Pronk <xander.c.pronk@gmail.com>
>
> Lens shading correction should be applied after black level
> subtraction (in order to make the computations with meaningful values)
> and before white balance (especially before white balance stats are
> computed).
>
> Note that lens shading correction depends on temperature, which depends
> on white balance, so there is a chicken-egg problem.  Currently, we
> determine white balance and temperature from the preceding frame.
>
> TODO: It's unknown why the LSC is computed in the shader the way it is.
>
> Co-developed-by: Rick ten Wolde <rick_libcamera@wolde.info>
> Signed-off-by: Rick ten Wolde <rick_libcamera@wolde.info>
> Signed-off-by: Xander Pronk <xander.c.pronk@gmail.com>
> Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
> ---
>  src/libcamera/shaders/bayer_1x_packed.frag | 8 ++++++++
>  src/libcamera/shaders/bayer_unpacked.frag  | 8 ++++++++
>  2 files changed, 16 insertions(+)
>
> diff --git a/src/libcamera/shaders/bayer_1x_packed.frag b/src/libcamera/shaders/bayer_1x_packed.frag
> index 23747f78a..efc2fec72 100644
> --- a/src/libcamera/shaders/bayer_1x_packed.frag
> +++ b/src/libcamera/shaders/bayer_1x_packed.frag
> @@ -70,6 +70,10 @@ uniform vec3 blacklevel;
>  uniform float gamma;
>  uniform float contrastExp;
>  
> +#if defined(DO_LSC)
> +uniform sampler2D lsc_tex;
> +#endif
> +
>  float apply_contrast(float value)
>  {
>  	// Apply simple S-curve
> @@ -227,6 +231,10 @@ void main(void)
>  
>  	rgb = rgb - blacklevel;
>  
> +	#if defined(DO_LSC)
> +	rgb = rgb + rgb * 3.0 * texture2D(lsc_tex, textureOut).rgb;

Could anybody explain this formula, please?

> +	#endif
> +
>  	/*
>  	 *   CCM is a 3x3 in the format
>  	 *
> diff --git a/src/libcamera/shaders/bayer_unpacked.frag b/src/libcamera/shaders/bayer_unpacked.frag
> index 1b85196ae..cc82e656c 100644
> --- a/src/libcamera/shaders/bayer_unpacked.frag
> +++ b/src/libcamera/shaders/bayer_unpacked.frag
> @@ -29,6 +29,10 @@ uniform vec3            blacklevel;
>  uniform float           gamma;
>  uniform float           contrastExp;
>  
> +#if defined(DO_LSC)
> +uniform sampler2D lsc_tex;
> +#endif
> +
>  float apply_contrast(float value)
>  {
>      // Apply simple S-curve
> @@ -130,6 +134,10 @@ void main(void) {
>  
>      rgb = rgb - blacklevel;
>  
> +    #if defined(DO_LSC)
> +    rgb = rgb + rgb * 3.0 * texture2D(lsc_tex, center.xy).rgb;
> +    #endif
> +
>      /*
>       *   CCM is a 3x3 in the format
>       *

Patch
diff mbox series

diff --git a/src/libcamera/shaders/bayer_1x_packed.frag b/src/libcamera/shaders/bayer_1x_packed.frag
index 23747f78a..efc2fec72 100644
--- a/src/libcamera/shaders/bayer_1x_packed.frag
+++ b/src/libcamera/shaders/bayer_1x_packed.frag
@@ -70,6 +70,10 @@  uniform vec3 blacklevel;
 uniform float gamma;
 uniform float contrastExp;
 
+#if defined(DO_LSC)
+uniform sampler2D lsc_tex;
+#endif
+
 float apply_contrast(float value)
 {
 	// Apply simple S-curve
@@ -227,6 +231,10 @@  void main(void)
 
 	rgb = rgb - blacklevel;
 
+	#if defined(DO_LSC)
+	rgb = rgb + rgb * 3.0 * texture2D(lsc_tex, textureOut).rgb;
+	#endif
+
 	/*
 	 *   CCM is a 3x3 in the format
 	 *
diff --git a/src/libcamera/shaders/bayer_unpacked.frag b/src/libcamera/shaders/bayer_unpacked.frag
index 1b85196ae..cc82e656c 100644
--- a/src/libcamera/shaders/bayer_unpacked.frag
+++ b/src/libcamera/shaders/bayer_unpacked.frag
@@ -29,6 +29,10 @@  uniform vec3            blacklevel;
 uniform float           gamma;
 uniform float           contrastExp;
 
+#if defined(DO_LSC)
+uniform sampler2D lsc_tex;
+#endif
+
 float apply_contrast(float value)
 {
     // Apply simple S-curve
@@ -130,6 +134,10 @@  void main(void) {
 
     rgb = rgb - blacklevel;
 
+    #if defined(DO_LSC)
+    rgb = rgb + rgb * 3.0 * texture2D(lsc_tex, center.xy).rgb;
+    #endif
+
     /*
      *   CCM is a 3x3 in the format
      *