[RFC,v2,25/26] libcamera: shaders: bayer_glr16_to_rgba.frag: Use center.zw to derive 3x3 texel center
diff mbox series

Message ID 20261118095735.170587-26-bryan.odonoghue@linaro.org
State New
Headers show
Series
  • gpuisp multipass + fixes
Related show

Commit Message

Bryan O'Donoghue Nov. 18, 2026, 9:57 a.m. UTC
Currently both center.zw and XCoord/YCoord are used to determine where the
sample texel should be. We end up sampling in the range 0.0f..1.0f so when
the sample lands on a line the XCoord/YCoord and center.zw can disagree on
what is being sampled.

Switch entirely to center.zw using a similar algorithm blc-normalise logic;
specifically to add 0.5f to the calculated center byte before dividing -
thus ensuring we always hit in the middle of the range of the texel we are
targetting when dividing down to 0.0f..1.0f not at the edge.

As a result we can drop XCoord/YCoord.

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
---
 .../shaders/bayer_glr16f_to_rgba.frag         | 51 ++++++++++++++++---
 src/libcamera/shaders/bayer_unpacked.vert     | 14 -----
 2 files changed, 44 insertions(+), 21 deletions(-)

Patch
diff mbox series

diff --git a/src/libcamera/shaders/bayer_glr16f_to_rgba.frag b/src/libcamera/shaders/bayer_glr16f_to_rgba.frag
index c613e9842..a3cd71a4b 100644
--- a/src/libcamera/shaders/bayer_glr16f_to_rgba.frag
+++ b/src/libcamera/shaders/bayer_glr16f_to_rgba.frag
@@ -22,11 +22,11 @@  precision highp float;
 /** Monochrome RGBA or GL_LUMINANCE Bayer encoded texture.*/
 uniform sampler2D	tex_y;
 varying vec4		center;
-varying vec4		yCoord;
-varying vec4		xCoord;
 uniform mat3		ccm;
 uniform float		gamma;
 uniform float		contrastExp;
+uniform vec2		tex_size;
+uniform vec2		tex_bayer_first_red;
 
 struct samples_3x3 {
 	float row0a, row0b, row0c;
@@ -50,14 +50,51 @@  float fetch(float x, float y)
 
 void sample_3x3(inout samples_3x3 samples)
 {
+	/*
+	 *  @pos comes from bayer_unpacked.vert
+	 *  which then feeds into bayer_1x_packed_to_blc_glr16f.frag
+	 *
+	 *  Where center.zw = textureIn * tex_size + tex_bayer_first_red;
+	 *
+	 *  @texel_center has 0.5 added to it to ensure when we convert
+	 *  from a texel to a float in the range 0.0f..1.0f that we land
+	 *  in the middle of the texel we are sampling before divinding
+	 *  down to the 0.0..1.0f range.
+	 *
+	 *  @texel_step because texture2D() above annoyingly operates on
+	 *  the range 0.0f..1.0f so we have to convert every texel
+	 *  into a number between 0.0f and 1.0f
+	 *
+	 *  @xCoord and yCoord taking xCoord as the example.
+	 *  texel_center.x is the center of the pixel which we added 0.5f
+	 *                 to ensure it is center as a fraction of the
+	 *                 image width.
+	 *  texel_step.x is the width of one pixel as a fraction 1.0f / width
+	 *  xCoord therefore is the vector of the surrounding pixels centered.
+	 *
+	 *  e.g.
+	 *  Width 1928, sample pixel 100:
+	 *
+	 *  texel_center.x = 100.5 / 1928
+	 *  texel_step.x = 1 / 1928
+	 *  xCoord => adjacent pixels (98.5, 99.5, 101.5, 102.5) / 1928
+	 *
+	 *  The division by width is necessary because we sample between 0.0f..1.0f
+	 */
+	vec2 pos = floor(center.zw);
+	vec2 texel_center = (pos - tex_bayer_first_red + 0.5) / tex_size;
+	vec2 texel_step = 1.0 / tex_size;
+	vec4 xCoord = texel_center.x + vec4(-2.0, -1.0, 1.0, 2.0) * texel_step.x;
+	vec4 yCoord = texel_center.y + vec4(-2.0, -1.0, 1.0, 2.0) * texel_step.y;
+
 	samples.row0a = fetch(xCoord[1], yCoord[2]);
-	samples.row0b = fetch(center.x,  yCoord[2]);
+	samples.row0b = fetch(texel_center.x, yCoord[2]);
 	samples.row0c = fetch(xCoord[2], yCoord[2]);
-	samples.row1a = fetch(xCoord[1], center.y);
-	samples.center = fetch(center.x, center.y);
-	samples.row1c = fetch(xCoord[2], center.y);
+	samples.row1a = fetch(xCoord[1], texel_center.y);
+	samples.center = fetch(texel_center.x, texel_center.y);
+	samples.row1c = fetch(xCoord[2], texel_center.y);
 	samples.row2a = fetch(xCoord[1], yCoord[1]);
-	samples.row2b = fetch(center.x,  yCoord[1]);
+	samples.row2b = fetch(texel_center.x, yCoord[1]);
 	samples.row2c = fetch(xCoord[2], yCoord[1]);
 }
 
diff --git a/src/libcamera/shaders/bayer_unpacked.vert b/src/libcamera/shaders/bayer_unpacked.vert
index 423dde0fa..72a6d1bc2 100644
--- a/src/libcamera/shaders/bayer_unpacked.vert
+++ b/src/libcamera/shaders/bayer_unpacked.vert
@@ -22,7 +22,6 @@  attribute vec2 textureIn;
 uniform mat4 proj_matrix;
 
 uniform vec2 tex_size;  /* The texture size in pixels */
-uniform vec2 tex_step;
 
 /** Pixel position of the first red pixel in the */
 /**  Bayer pattern.  [{0,1}, {0, 1}]*/
@@ -32,24 +31,11 @@  uniform vec2            tex_bayer_first_red;
     .zw = ...on the range [0, sourceSize], offset by firstRed */
 varying vec4            center;
 
-/** center.x + (-2/w, -1/w, 1/w, 2/w); These are the x-positions */
-/** of the adjacent pixels.*/
-varying vec4            xCoord;
-
-/** center.y + (-2/h, -1/h, 1/h, 2/h); These are the y-positions */
-/** of the adjacent pixels.*/
-varying vec4            yCoord;
-
 uniform float stride_factor;
 
 void main(void) {
     center.xy = vec2(textureIn.x * stride_factor, textureIn.y);
     center.zw = textureIn * tex_size + tex_bayer_first_red;
 
-    xCoord = center.x + vec4(-2.0 * tex_step.x,
-                             -tex_step.x, tex_step.x, 2.0 * tex_step.x);
-    yCoord = center.y + vec4(-2.0 * tex_step.y,
-                              -tex_step.y, tex_step.y, 2.0 * tex_step.y);
-
     gl_Position = proj_matrix * vertexIn;
 }