From patchwork Wed Oct 15 01:22:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bryan O'Donoghue X-Patchwork-Id: 24674 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 33674C3334 for ; Wed, 15 Oct 2025 01:23:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 932D960686; Wed, 15 Oct 2025 03:23:39 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="OZ2d4EmQ"; dkim-atps=neutral Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D611560626 for ; Wed, 15 Oct 2025 03:23:24 +0200 (CEST) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-b4aed12cea3so947566766b.1 for ; Tue, 14 Oct 2025 18:23:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1760491404; x=1761096204; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3sraFz/ECz/08gX1UxD7qCQLpBOGxyxdQpxiRp03TY8=; b=OZ2d4EmQIeZUTnxdSmZc0bpalXJvXMjGMWVsx2DvAqf2ic5Ee8pbJ8/NLMzY0J3H5C hJVWQbWDgySNNf7m8uTHbp/OfOWCH/eJ71S4l8+Q4c6UmS17SOGA6isZ8M2lbbqBWbTX M3Ifl3SA1a3MkKpKZ9BjzxWKsGqI9MbIQyi6B0fz7ZAVuk6oRdt3mJJmEUxQkX2MGkvT gXlR3v7pOUf2hcH5BTpXKe4fcLDz4Ba3lr0VvQoANDehUWcpJHWfYGntHsgQgcSVXXxP 6TjeBJHWP8dRlsN0rlUCe3GgMh2IfGpHYfuTLuHZRuwFx5xYfEHG5jdht1bftIaZ1mRm 5yww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760491404; x=1761096204; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3sraFz/ECz/08gX1UxD7qCQLpBOGxyxdQpxiRp03TY8=; b=Sh5recVQss2rUpA8pqQYbd5VhbUwO/i7MwFJALAU38EtgSkZSnj9GmKSka29IUmuve UMnIKDVGcxOQHAcdJ6NcioWx2k/gDGLhhmXiE8/M2UIECPAY8FOpvYScPeoDg6tNObz5 wDDpqTMhLa2P5sDXNAiWwinkLuUUkOAt87zwSSr04OVgxY6SdKpcCUceqycjjWcACheh 6A2ErW/9V4udTHZmxhuVATH8JNt5X4flKoNbTeY/waLivHIXHAYnGHkvgluZ2f9KBHPy oFnTgDVuKeTHtFYUbJHjE+6scPZkoM0IBt052Y4VmsXKzJoyk96wKm+7AS98tK8NB3vo AxnQ== X-Gm-Message-State: AOJu0Yz6JnPe+bVH8wU93RtJLRFfGrGhfk/4/Cq0apNhG94rxosncWcu atnw0cVVhsv+GFy4GEzx9pQDWWDWv4Yg57wzHrr9xI7RyWXTqnCn4wJFFJImh+KaQv2L0FKJGYF nsBEx X-Gm-Gg: ASbGncu/n+rtZEkQMbRJUUF6YefQrjh4MroaKadkOE4Y5JBiyr8xqxjPABGfQ+IHsam 8YSH/0DWOgUW1vMwrGA7eEWwMwEuSqvCy15mTmYK0552xiV34reuJNTsPusq1dJrkmFytDO+FP9 GnRFxArFeZbVLxlKMPGBv3JsoY6ZKOcvsh2TRee7sHz3hcPSLny3F9Ru1IMahsCqzk5FoEWpT5k xABbmtPg7cIoPL5/FIKTZGAfzv59OMQjl7nDr20m71nDt7J4r3lKrzvAOJ7JkQaiIY40wII2FDI u4jkzobhx4thPDzVrB4ngDjpnhnjUeWDcpRhG+Vfpg99X2vbKr5kly3r9f0sK0Av4QcKvDBStkd h0os8AOhOeZ9abXFi0cCg4rLKlMsZiIKDYc3U2yGxz5+Vw0MFkFkAqy5YbHXS+BHfFe1CTBMfWG JPnq7wOueY5Jt0r57ffy4/CszpKQeomdeCRQ+DeVQN X-Google-Smtp-Source: AGHT+IGKEo/LXzvl1qrZ5I8z5+saAM5ZJ/Yjlnw0Ds112IFVoUeE20d9QlJrzLNmjBZiiew/0TIeFg== X-Received: by 2002:a17:907:da2:b0:b3c:de0e:7091 with SMTP id a640c23a62f3a-b50aa28f291mr2863547266b.8.1760491404045; Tue, 14 Oct 2025 18:23:24 -0700 (PDT) Received: from inspiron14p-linux.nxsw.local (188-141-3-146.dynamic.upc.ie. [188.141.3.146]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b5ccd7b202dsm98348466b.82.2025.10.14.18.23.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Oct 2025 18:23:23 -0700 (PDT) From: Bryan O'Donoghue To: libcamera-devel@lists.libcamera.org Cc: hdegoede@redhat.com, mzamazal@redhat.com, bryan.odonoghue@linaro.org, bod.linux@nxsw.ie Subject: [PATCH v3 29/39] libcamera: shaders: Extend debayer shaders to apply RGB gain values on output Date: Wed, 15 Oct 2025 02:22:41 +0100 Message-ID: <20251015012251.17508-30-bryan.odonoghue@linaro.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251015012251.17508-1-bryan.odonoghue@linaro.org> References: <20251015012251.17508-1-bryan.odonoghue@linaro.org> MIME-Version: 1.0 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 Sender: "libcamera-devel" Extend out the bayer fragment shaders to take 3 x 256 byte inputs as textures from the CPU. We then use an index to the table to recover the colour-corrected values provided by the SoftIPA thread. Signed-off-by: Bryan O'Donoghue Reviewed-by: Milan Zamazal --- .../internal/shaders/bayer_1x_packed.frag | 56 +++++++++++++++++ .../internal/shaders/bayer_unpacked.frag | 62 ++++++++++++++++++- 2 files changed, 117 insertions(+), 1 deletion(-) diff --git a/include/libcamera/internal/shaders/bayer_1x_packed.frag b/include/libcamera/internal/shaders/bayer_1x_packed.frag index 19b13ad0..90bd6457 100644 --- a/include/libcamera/internal/shaders/bayer_1x_packed.frag +++ b/include/libcamera/internal/shaders/bayer_1x_packed.frag @@ -65,6 +65,10 @@ uniform vec2 tex_step; uniform vec2 tex_bayer_first_red; uniform sampler2D tex_y; +uniform sampler2D red_param; +uniform sampler2D green_param; +uniform sampler2D blue_param; +uniform mat3 ccm; void main(void) { @@ -212,5 +216,57 @@ void main(void) vec3(patterns.y, C, patterns.x) : vec3(patterns.wz, C)); +#if defined(APPLY_CCM_PARAMETERS) + /* + * CCM is a 3x3 in the format + * + * +--------------+----------------+---------------+ + * | RedRedGain | RedGreenGain | RedBlueGain | + * +--------------+----------------+---------------+ + * | GreenRedGain | GreenGreenGain | GreenBlueGain | + * +--------------+----------------+---------------+ + * | BlueRedGain | BlueGreenGain | BlueBlueGain | + * +--------------+----------------+---------------+ + * + * Rout = RedRedGain * Rin + RedGreenGain * Gin + RedBlueGain * Bin + * Gout = GreenRedGain * Rin + GreenGreenGain * Gin + GreenBlueGain * Bin + * Bout = BlueRedGain * Rin + BlueGreenGain * Gin + BlueBlueGain * Bin + * + * We upload to the GPU without transposition glUniformMatrix3f(.., .., GL_FALSE, ccm); + * + * CPU + * float ccm [] = { + * RedRedGain, RedGreenGain, RedBlueGain, + * GreenRedGain, GreenGreenGain, GreenBlueGain, + * BlueRedGain, BlueGreenGain, BlueBlueGain, + * }; + * + * GPU + * ccm = { + * RedRedGain, GreenRedGain, BlueRedGain, + * RedGreenGain, GreenGreenGain, BlueGreenGain, + * RedBlueGain, GreenBlueGain, BlueBlueGain, + * } + * + * However the indexing for the mat data-type is column major hence + * ccm[0][0] = RedRedGain, ccm[0][1] = RedGreenGain, ccm[0][2] = RedBlueGain + * + */ + float rin, gin, bin; + rin = rgb.r; + gin = rgb.g; + bin = rgb.b; + + rgb.r = (rin * ccm[0][0]) + (gin * ccm[0][1]) + (bin * ccm[0][2]); + rgb.g = (rin * ccm[1][0]) + (gin * ccm[1][1]) + (bin * ccm[1][2]); + rgb.b = (rin * ccm[2][0]) + (gin * ccm[2][1]) + (bin * ccm[2][2]); + +#elif defined(APPLY_RGB_PARAMETERS) + /* Apply bayer params */ + rgb.r = texture2D(red_param, vec2(rgb.r, 0.5)).r; + rgb.g = texture2D(green_param, vec2(rgb.g, 0.5)).g; + rgb.b = texture2D(blue_param, vec2(rgb.b, 0.5)).b; +#endif + gl_FragColor = vec4(rgb, 1.0); } diff --git a/include/libcamera/internal/shaders/bayer_unpacked.frag b/include/libcamera/internal/shaders/bayer_unpacked.frag index aa7a1b00..5955c2ea 100644 --- a/include/libcamera/internal/shaders/bayer_unpacked.frag +++ b/include/libcamera/internal/shaders/bayer_unpacked.frag @@ -21,11 +21,17 @@ precision highp float; /** Monochrome RGBA or GL_LUMINANCE Bayer encoded texture.*/ uniform sampler2D tex_y; +uniform sampler2D red_param; +uniform sampler2D green_param; +uniform sampler2D blue_param; varying vec4 center; varying vec4 yCoord; varying vec4 xCoord; +uniform mat3 ccm; void main(void) { + vec3 rgb; + #define fetch(x, y) texture2D(tex_y, vec2(x, y)).r float C = texture2D(tex_y, center.xy).r; // ( 0, 0) @@ -97,11 +103,65 @@ void main(void) { PATTERN.xw += kB.xw * B; PATTERN.xz += kF.xz * F; - gl_FragColor.rgb = (alternate.y == 0.0) ? + rgb = (alternate.y == 0.0) ? ((alternate.x == 0.0) ? vec3(C, PATTERN.xy) : vec3(PATTERN.z, C, PATTERN.w)) : ((alternate.x == 0.0) ? vec3(PATTERN.w, C, PATTERN.z) : vec3(PATTERN.yx, C)); + +#if defined(APPLY_CCM_PARAMETERS) + /* + * CCM is a 3x3 in the format + * + * +--------------+----------------+---------------+ + * | RedRedGain | RedGreenGain | RedBlueGain | + * +--------------+----------------+---------------+ + * | GreenRedGain | GreenGreenGain | GreenBlueGain | + * +--------------+----------------+---------------+ + * | BlueRedGain | BlueGreenGain | BlueBlueGain | + * +--------------+----------------+---------------+ + * + * Rout = RedRedGain * Rin + RedGreenGain * Gin + RedBlueGain * Bin + * Gout = GreenRedGain * Rin + GreenGreenGain * Gin + GreenBlueGain * Bin + * Bout = BlueRedGain * Rin + BlueGreenGain * Gin + BlueBlueGain * Bin + * + * We upload to the GPU without transposition glUniformMatrix3f(.., .., GL_FALSE, ccm); + * + * CPU + * float ccm [] = { + * RedRedGain, RedGreenGain, RedBlueGain, + * GreenRedGain, GreenGreenGain, GreenBlueGain, + * BlueRedGain, BlueGreenGain, BlueBlueGain, + * }; + * + * GPU + * ccm = { + * RedRedGain, GreenRedGain, BlueRedGain, + * RedGreenGain, GreenGreenGain, BlueGreenGain, + * RedBlueGain, GreenBlueGain, BlueBlueGain, + * } + * + * However the indexing for the mat data-type is column major hence + * ccm[0][0] = RedRedGain, ccm[0][1] = RedGreenGain, ccm[0][2] = RedBlueGain + * + */ + float rin, gin, bin; + rin = rgb.r; + gin = rgb.g; + bin = rgb.b; + + rgb.r = (rin * ccm[0][0]) + (gin * ccm[0][1]) + (bin * ccm[0][2]); + rgb.g = (rin * ccm[1][0]) + (gin * ccm[1][1]) + (bin * ccm[1][2]); + rgb.b = (rin * ccm[2][0]) + (gin * ccm[2][1]) + (bin * ccm[2][2]); + +#elif defined(APPLY_RGB_PARAMETERS) + /* Apply bayer params */ + rgb.r = texture2D(red_param, vec2(rgb.r, 0.5)).r; + rgb.g = texture2D(red_param, vec2(rgb.g, 0.5)).g; + rgb.b = texture2D(red_param, vec2(rgb.b, 0.5)).b; +#endif + + gl_FragColor.rgb = rgb; }