@@ -7,6 +7,7 @@ shader_files = files([
'bayer_unpacked.frag',
'bayer_unpacked.vert',
'identity.vert',
+ 'mono.frag',
])
# Generate header from shaders
new file mode 100644
@@ -0,0 +1,168 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (C) 2026, Alain CousiniƩ
+ *
+ * Generic single fragment shader for all monochrome formats in libcamera.
+ */
+
+#ifdef GL_ES
+precision highp float;
+#endif
+
+/* Automatic pipeline mode deduction with build safety check */
+#if defined(RAW8) || defined(RAW10) || defined(RAW12) || defined(RAW16)
+ #define PIPELINE_MONO_UNPACKED
+#elif defined(RAW10P) || defined(RAW12P)
+ #define PIPELINE_MONO_PACKED
+#else
+ #error Invalid or unsupported monochrome format
+#endif
+
+/* Texture coordinates geometrically corrected by identity.vert */
+varying vec2 textureOut;
+
+/* Standard uniforms supplied by the CPU (debayer_egl.cpp) */
+uniform sampler2D tex_y;
+#if defined(PIPELINE_MONO_PACKED)
+uniform vec2 tex_size; /* Native [width, height] of the texture */
+#endif
+uniform float gamma;
+uniform float contrastExp;
+uniform vec3 blacklevel;
+#if defined(APPLY_LSC)
+uniform sampler2D lsc_tex; /* Grids of LSC gains sent from the CPU */
+#endif
+
+/* ==========================================
+ * UNIFIED PIXEL EXTRACTION FUNCTION
+ * ========================================== */
+float readNativePixel(vec2 texOut)
+{
+ float pixelVal = 0.0;
+
+ /* -------------------------------------------------------------------------
+ * CASE UNPACKED MONOCHROME
+ * -------------------------------------------------------------------------
+ * - RAW8: Transmitted as 1 byte per pixel. Sampled natively via GL_LUMINANCE.
+ * [ Byte 0: P0 (8-bit) ]
+ *
+ * - RAW10/RAW12/RAW16: Stored across 2 contiguous bytes (16 bits) per pixel,
+ * aligned Little-Endian. Imported into OpenGL as a 2-channel GL_RG texture.
+ * [ Byte 0 (texel.r): LSB (P0_low) ] -> Parsed as (texel.r * 255.0)
+ * [ Byte 1 (texel.g): MSB (P0_high) ] -> Parsed as (texel.g * 255.0)
+ * ------------------------------------------------------------------------- */
+ #if defined(PIPELINE_MONO_UNPACKED)
+ #if defined(RAW8)
+ pixelVal = floor(texture2D(tex_y, texOut).r * 255.0 + 0.5);
+ #else
+ vec4 texel = texture2D(tex_y, texOut);
+ float byteLow = floor(texel.r * 255.0 + 0.5);
+ float byteHigh = floor(texel.g * 255.0 + 0.5);
+ pixelVal = (byteHigh * 256.0) + byteLow;
+ #endif
+
+ /* -------------------------------------------------------------------------
+ * CASE PACKED MONOCHROME
+ * -------------------------------------------------------------------------
+ * Pixels are grouped into sequential macro-blocks to optimize MIPI bandwidth.
+ * MSB bytes are sent sequentially, followed by shared trailing LSB bytes.
+ * Sampled in OpenGL via a 1-channel GL_LUMINANCE texture.
+ *
+ * - RAW10P (4 Pixels into 5 Bytes):
+ * [Byte 0: P0_high(8b)] [Byte 1: P1_high(8b)] [Byte 2: P2_high(8b)] [Byte 3: P3_high(8b)]
+ * [Byte 4: P3_low(2b) | P2_low(2b) | P1_low(2b) | P0_low(2b)]
+ * BLOCK_PIXELS = 4.0, BLOCK_BYTES = 5.0, LSB_SIZE = 2.0, LSB_MASK = 4.0
+ *
+ * - RAW12P (2 Pixels into 3 Bytes):
+ * [Byte 0: P0_high(8b)] [Byte 1: P1_high(8b)]
+ * [Byte 2: P1_low(4b) | P0_low(4b)]
+ * BLOCK_PIXELS = 2.0, BLOCK_BYTES = 3.0, LSB_SIZE = 4.0, LSB_MASK = 16.0
+ * ------------------------------------------------------------------------- */
+ #elif defined(PIPELINE_MONO_PACKED)
+
+ /* Scale normalized coordinates up to actual texture dimensions */
+ vec2 v_pixelCoords = texOut * tex_size;
+
+ float px = floor(v_pixelCoords.x);
+ float py = floor(v_pixelCoords.y);
+
+ float blockIndex = floor(px / BLOCK_PIXELS);
+ float idx = mod(px, BLOCK_PIXELS);
+ float blockStart = blockIndex * BLOCK_BYTES;
+
+ float normY = (py + 0.5) / tex_size.y;
+ float xMSB = (blockStart + idx + 0.5) / TEX_WIDTH;
+ float byteHigh = floor(texture2D(tex_y, vec2(xMSB, normY)).r * 255.0 + 0.5);
+
+ #if defined(RAW10P)
+ /* 4 pixels in 5 bytes: Byte 4 holds [P3_low(2b) | P2_low(2b) | P1_low(2b) | P0_low(2b)] */
+ float b4 = floor(texture2D(tex_y, vec2((blockStart + 4.5) / TEX_WIDTH, normY)).r * 255.0 + 0.5);
+ float lsbBits = 0.0;
+
+ if (idx < 0.5) {
+ lsbBits = mod(b4, 4.0);
+ } else if (idx < 1.5) {
+ lsbBits = mod(floor(b4 / 4.0), 4.0);
+ } else if (idx < 2.5) {
+ lsbBits = mod(floor(b4 / 16.0), 4.0);
+ } else {
+ lsbBits = floor(b4 / 64.0);
+ }
+ pixelVal = (byteHigh * 4.0) + lsbBits;
+
+ #elif defined(RAW12P)
+ /* 2 pixels in 3 bytes: Byte 2 holds [P1_low(4b) | P0_low(4b)] */
+ float b2 = floor(texture2D(tex_y, vec2((blockStart + 2.5) / TEX_WIDTH, normY)).r * 255.0 + 0.5);
+ float lsbBits = 0.0;
+
+ if (idx < 0.5) {
+ lsbBits = mod(b2, 16.0);
+ } else {
+ lsbBits = floor(b2 / 16.0);
+ }
+ pixelVal = (byteHigh * 16.0) + lsbBits;
+
+ #endif
+ #endif
+ return pixelVal;
+}
+
+/* ==========================================
+ * GENERIC MAIN PIPELINE
+ * ========================================== */
+void main(void)
+{
+ /* Read raw pixel value via the unified generic function */
+ float rawValue = readNativePixel(textureOut);
+
+ /* Step 1: Normalization and Black Level Subtraction */
+ float normalizedIntensity = rawValue * INV_MAX_VAL;
+ normalizedIntensity = clamp(normalizedIntensity - blacklevel.r, 0.0, 1.0);
+
+ /* Step 2: Lens Shading Correction */
+ #if defined(APPLY_LSC)
+ /* Sample the LSC lookup grid texture using normalized output coordinates */
+ vec4 lscGains = texture2D(lsc_tex, textureOut);
+
+ /*
+ * In libcamera's SoftISP LSC implementation, values are stored in UQ2.6 format
+ * inside an 8-bit texture. We upscale the normalized value back to its real range [0..4]
+ * We sample the first channel (red) since the signal is uniform across channels for grayscale.
+ */
+ float lscGain = lscGains.r * 4.0;
+
+ /* Apply the spatial attenuation correction factor */
+ normalizedIntensity = clamp(normalizedIntensity * lscGain, 0.0, 1.0);
+ #endif
+
+ /* Step 3: Contrast Enhancement (S-Curve) */
+ float centerDist = 1.0 - abs(2.0 * normalizedIntensity - 1.0);
+ float singlePow = 0.5 * pow(centerDist, contrastExp);
+ normalizedIntensity = mix(singlePow, 1.0 - singlePow, step(0.5, normalizedIntensity));
+
+ /* Step 4: Gamma Correction */
+ vec3 rgb = pow(vec3(normalizedIntensity), vec3(gamma));
+
+ /* Step 5: Texture Output (Grayscale is uniform, no color swapping needed) */
+ gl_FragColor = vec4(rgb, 1.0);
+}
Add mono.frag, a single fragment shader for all monochrome formats, selected through defines: RAW8, RAW10, RAW12 and RAW16 (unpacked, 2 bytes per pixel little endian for the last three) and RAW10P and RAW12P (CSI-2 packed). For packed formats, the shader finds the most significant byte of the pixel and the shared least significant byte of its block from tex_size. It applies the black level, the optional shading correction (red channel of the gain table, as the signal is the same on all channels), the S-shaped contrast curve and the gamma, then writes the grey value as RGB. identity.vert is reused as suggested in the v6 review: no dedicated vertex shader is needed. The precision declaration is the same as in the Bayer shaders. The shader is wired into the GPU debayer in patch 4/6. Signed-off-by: Alain CousiniƩ <alain.cousinie@laposte.net> --- src/libcamera/shaders/meson.build | 1 + src/libcamera/shaders/mono.frag | 168 ++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 src/libcamera/shaders/mono.frag