From patchwork Sun Nov 29 15:24:11 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alain_Cousini=C3=A9?= X-Patchwork-Id: 29106 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 7E47FC3360 for ; Sun, 29 Nov 2026 15:24:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 20EF16AA43; Sun, 29 Nov 2026 16:24:54 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=laposte.net header.i=@laposte.net header.b="k3du34Xa"; dkim-atps=neutral Received: from smtp-outgoing-1802.laposte.net (smtp-outgoing-1802.laposte.net [160.92.124.103]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 898C06AA2B for ; Sun, 29 Nov 2026 16:24:43 +0100 (CET) X-mail-filterd: {"version":"1.9.7", "queueID":"4jcrYP15wSz10MQN", "contextId": "1b1365f2-eae0-4cb9-9862-fc06179f8740"} Received: from outgoing-mail.laposte.net (localhost.localdomain [127.0.0.1]) by mlpnf0120.laposte.net (SMTP Server) with ESMTP id 4jcrYP15wSz10MQN; Sun, 29 Nov 2026 16:24:41 +0100 (CET) X-mail-filterd: {"version":"1.9.7", "queueID":"4jcrYN5yy4z10MQ4", "contextId": "584c99ca-85a6-4af9-944d-1004623691b3"} X-lpn-mailing: LEGIT X-lpn-spamrating: 36 X-lpn-spamlevel: not-spam Received: from debian.home (m-dj.hd.free.fr [82.67.82.126]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mlpnf0120.laposte.net (SMTP Server) with ESMTPSA id 4jcrYN5yy4z10MQ4; Sun, 29 Nov 2026 16:24:40 +0100 (CET) From: =?utf-8?q?Alain_Cousini=C3=A9?= To: libcamera-devel@lists.libcamera.org, Milan Zamazal Cc: Hans de Goede , Bryan O'Donoghue , =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= , Laurent Pinchart , Jai Luthra , =?utf-8?q?Alain_Cousini=C3=A9?= Subject: [PATCH v8 1/6] libcamera: software_isp: Add support for monochrome formats - Shader Date: Sun, 29 Nov 2026 16:24:11 +0100 Message-ID: <20261129152416.174279-2-alain.cousinie@laposte.net> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261129152416.174279-1-alain.cousinie@laposte.net> References: <20261129152416.174279-1-alain.cousinie@laposte.net> MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=laposte.net; s=lpn-wlmd; t=1795965883; bh=/tB3sh4gPi1rTkHEnvlYPIy8pYELz3ehNol3gGWFQGw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Type:Content-Transfer-Encoding; b=k3du34XaJ7UfpmNPBoobZVlAUBwA/gJZZJiJFnPnawuSNrxIFMoaRQ7rKGhkEJZtTHd26spMl3frBg271J9dPYfGXTL4ItpqCT8lraTHGKSEyq+dUMFgyxFbb4DkRoTxUVUZctK0LJkL0HqrZffxMIq4Ia3iKe3oFI/WzZPeTu4k/bdMsp7u6vezbYQGeLZ02t6sFXT7QQUMXgCgoh1KaaVMXXVpKb5ibmhNshsvBinsY/FpkUVm9p76toGWBbHRcl5r/w3RarxB+DFbpUdtMjE8TbcrAdCSQlGJg7A+0xTDlG94BSiQAffQ8qgrTWpU4z86Qdm7X4Gp6uWVaiYbUQ==; X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org 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Ʃ --- 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 diff --git a/src/libcamera/shaders/meson.build b/src/libcamera/shaders/meson.build index c409ff9b0..e6df53da1 100644 --- a/src/libcamera/shaders/meson.build +++ b/src/libcamera/shaders/meson.build @@ -7,6 +7,7 @@ shader_files = files([ 'bayer_unpacked.frag', 'bayer_unpacked.vert', 'identity.vert', + 'mono.frag', ]) # Generate header from shaders diff --git a/src/libcamera/shaders/mono.frag b/src/libcamera/shaders/mono.frag new file mode 100644 index 000000000..96815c244 --- /dev/null +++ b/src/libcamera/shaders/mono.frag @@ -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); +}