Show a patch.

GET /api/1.1/patches/27002/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 27002,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/27002/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/27002/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20260621-kbingham-awb-saturation-v1-6-b91ea59c6cfb@ideasonboard.com>",
    "date": "2026-06-20T23:00:33",
    "name": "[6/7] softisp: Split AWB from Combined Matrix",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "06810a9237ea57639bb41364a11720f169b0a334",
    "submitter": {
        "id": 4,
        "url": "https://patchwork.libcamera.org/api/1.1/people/4/?format=api",
        "name": "Kieran Bingham",
        "email": "kieran.bingham@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/27002/mbox/",
    "series": [
        {
            "id": 6009,
            "url": "https://patchwork.libcamera.org/api/1.1/series/6009/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=6009",
            "date": "2026-06-20T23:00:27",
            "name": "softisp: Fix Saturation and Black level handling",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/6009/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/27002/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/27002/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>",
        "X-Original-To": "parsemail@patchwork.libcamera.org",
        "Delivered-To": "parsemail@patchwork.libcamera.org",
        "Received": [
            "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 7211BC330A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat, 20 Jun 2026 23:00:49 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2076A656E2;\n\tSun, 21 Jun 2026 01:00:46 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C58C762899\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 21 Jun 2026 01:00:36 +0200 (CEST)",
            "from [192.168.0.240]\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 1113018A2;\n\tSun, 21 Jun 2026 01:00:00 +0200 (CEST)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"WAlsjo1L\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1781996400;\n\tbh=Ibjo4eaudDdOYgNiLtnzyC7qbdq6Mx1seSA2HTgq6CQ=;\n\th=From:Date:Subject:References:In-Reply-To:To:Cc:From;\n\tb=WAlsjo1LTsg/Y+TxpG48P9inSzpo8/2PtCLQtit8CXqXBOv1ZfUV8v035CvbabkBC\n\tx7NU9a6EKdnujVnNMR3SNB8eUKdCfzdJtGFDNusaYkuu7PLerHvjFyKuxA92n+Em1N\n\thGkt2dCyvShG/q0FQ+X8syhynETCSQ/6MqGH+DV4=",
        "From": "Kieran Bingham <kieran.bingham@ideasonboard.com>",
        "Date": "Sun, 21 Jun 2026 00:00:33 +0100",
        "Subject": "[PATCH 6/7] softisp: Split AWB from Combined Matrix",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=\"utf-8\"",
        "Content-Transfer-Encoding": "7bit",
        "Message-Id": "<20260621-kbingham-awb-saturation-v1-6-b91ea59c6cfb@ideasonboard.com>",
        "References": "<20260621-kbingham-awb-saturation-v1-0-b91ea59c6cfb@ideasonboard.com>",
        "In-Reply-To": "<20260621-kbingham-awb-saturation-v1-0-b91ea59c6cfb@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Kieran Bingham <kieran.bingham@ideasonboard.com>, \n\tMilan Zamazal <mzamazal@redhat.com>",
        "X-Mailer": "b4 0.14.2",
        "X-Developer-Signature": "v=1; a=ed25519-sha256; t=1781996434; l=8995;\n\ti=kieran.bingham@ideasonboard.com; s=20260204;\n\th=from:subject:message-id; \n\tbh=Ibjo4eaudDdOYgNiLtnzyC7qbdq6Mx1seSA2HTgq6CQ=;\n\tb=zU/62TsF+OPfE4Rbl/iSEY9v+TlOtzKto93HoUQy95EqYuC6/d8ZPLYmXF/7gwNrVRxHHQZmu\n\tyyM60/8VskSAyyUlNUMJ1r8O8e8P16MHGg1KgvInZTtO3wHkhAvEjfV",
        "X-Developer-Key": "i=kieran.bingham@ideasonboard.com; a=ed25519;\n\tpk=IOxS2C6nWHNjLfkDR71Iesk904i6wJDfEERqV7hDBdY=",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.29",
        "Precedence": "list",
        "List-Id": "<libcamera-devel.lists.libcamera.org>",
        "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>",
        "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>",
        "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>",
        "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>",
        "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "The AWB and CCM are currently combined into a single matrix which gets\npassed to the GPU and CPU ISP implementations. This unfortunately can\ncause saturation bugs between the AWB and CCM in areas of high\nbrightness in a given scene, where one colour channel will saturate\nbefore the others unevenly.\n\nRemove AWB gains from the combined Matrix to allow a specific AWB Gain\nstage to be applied, which is then clamped equally on all channels.\n\nFor CPU ISP the gains are applied and clamped before applying the CCM.\n\nReviewed-by: Milan Zamazal <mzamazal@redhat.com>\nSigned-off-by: Milan Zamazal <mzamazal@redhat.com>\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n src/ipa/simple/algorithms/awb.cpp          | 16 +++-------------\n src/ipa/simple/ipa_context.h               |  5 +----\n src/libcamera/shaders/bayer_1x_packed.frag |  4 ++++\n src/libcamera/shaders/bayer_unpacked.frag  |  4 ++++\n src/libcamera/software_isp/debayer_cpu.cpp | 10 +++++-----\n src/libcamera/software_isp/debayer_egl.cpp |  5 +++++\n src/libcamera/software_isp/debayer_egl.h   |  3 +++\n 7 files changed, 25 insertions(+), 22 deletions(-)",
    "diff": "diff --git a/src/ipa/simple/algorithms/awb.cpp b/src/ipa/simple/algorithms/awb.cpp\nindex f5c88ea6f896ff8a7ffdc328e09a4d4aa99df5e4..05155c83d172d64609053ba940a4c12a2248bb04 100644\n--- a/src/ipa/simple/algorithms/awb.cpp\n+++ b/src/ipa/simple/algorithms/awb.cpp\n@@ -38,15 +38,8 @@ void Awb::prepare(IPAContext &context,\n \t\t  DebayerParams *params)\n {\n \tauto &gains = context.activeState.awb.gains;\n-\tMatrix<float, 3, 3> gainMatrix = { { gains.r(), 0, 0,\n-\t\t\t\t\t     0, gains.g(), 0,\n-\t\t\t\t\t     0, 0, gains.b() } };\n-\tcontext.activeState.combinedMatrix =\n-\t\tgainMatrix * context.activeState.combinedMatrix;\n-\n-\tframeContext.gains.red = gains.r();\n-\tframeContext.gains.blue = gains.b();\n \n+\tframeContext.gains = gains;\n \tparams->gains = gains;\n }\n \n@@ -59,11 +52,8 @@ void Awb::process(IPAContext &context,\n \tconst SwIspStats::Histogram &histogram = stats->yHistogram;\n \tconst uint8_t blackLevel = context.activeState.blc.level;\n \n-\tconst float mdGains[] = {\n-\t\tstatic_cast<float>(frameContext.gains.red),\n-\t\tstatic_cast<float>(frameContext.gains.blue)\n-\t};\n-\tmetadata.set(controls::ColourGains, mdGains);\n+\tmetadata.set(controls::ColourGains, { frameContext.gains.r(),\n+\t\t\t\t\t      frameContext.gains.b() });\n \n \tif (!stats->valid)\n \t\treturn;\ndiff --git a/src/ipa/simple/ipa_context.h b/src/ipa/simple/ipa_context.h\nindex 34f7403a41d690cb3f0c271827ae2e915b6ea49d..8ccfacb46a59cedb5a0ad051d67f7c1f40af4b52 100644\n--- a/src/ipa/simple/ipa_context.h\n+++ b/src/ipa/simple/ipa_context.h\n@@ -71,10 +71,7 @@ struct IPAFrameContext : public FrameContext {\n \t\tdouble gain;\n \t} sensor;\n \n-\tstruct {\n-\t\tdouble red;\n-\t\tdouble blue;\n-\t} gains;\n+\tRGB<float> gains;\n \n \tfloat gamma;\n \tstd::optional<float> contrast;\ndiff --git a/src/libcamera/shaders/bayer_1x_packed.frag b/src/libcamera/shaders/bayer_1x_packed.frag\nindex fbd15b32e5b3b510ba0dd5a75704f9ff3942e00c..6b3f7532c177f277e43c27e498dfadcb9cd4e26c 100644\n--- a/src/libcamera/shaders/bayer_1x_packed.frag\n+++ b/src/libcamera/shaders/bayer_1x_packed.frag\n@@ -65,6 +65,7 @@ uniform vec2 tex_step;\n uniform vec2 tex_bayer_first_red;\n \n uniform sampler2D tex_y;\n+uniform vec3 awb;\n uniform mat3 ccm;\n uniform vec3 blacklevel;\n uniform float gamma;\n@@ -231,6 +232,9 @@ void main(void)\n \t */\n \trgb = (rgb - blacklevel) / (1.0 - blacklevel);\n \n+\t/* Apply AWB gains, and saturate each channel at sensor range */\n+\trgb = clamp(rgb * awb, vec3(0.0), vec3(1.0));\n+\n \t/*\n \t *   CCM is a 3x3 in the format\n \t *\ndiff --git a/src/libcamera/shaders/bayer_unpacked.frag b/src/libcamera/shaders/bayer_unpacked.frag\nindex 0f85e9a4ded7f43ce4fdabf1e045275ae2bc8f53..44535312125d0e7e5cca372f4e8e0f8ad9fce8b3 100644\n--- a/src/libcamera/shaders/bayer_unpacked.frag\n+++ b/src/libcamera/shaders/bayer_unpacked.frag\n@@ -24,6 +24,7 @@ uniform sampler2D       tex_y;\n varying vec4            center;\n varying vec4            yCoord;\n varying vec4            xCoord;\n+uniform vec3            awb;\n uniform mat3            ccm;\n uniform vec3            blacklevel;\n uniform float           gamma;\n@@ -134,6 +135,9 @@ void main(void) {\n      */\n     rgb = (rgb - blacklevel) / (1.0 - blacklevel);\n \n+    /* Apply AWB gains, and saturate each channel at sensor range */\n+    rgb = clamp(rgb * awb, vec3(0.0), vec3(1.0));\n+\n     /*\n      *   CCM is a 3x3 in the format\n      *\ndiff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp\nindex 9d6a08b3333f988570cb5d3f7cea4c117f7c2530..49382b4c2719bf02f6a806c9a9ac560c8daae866 100644\n--- a/src/libcamera/software_isp/debayer_cpu.cpp\n+++ b/src/libcamera/software_isp/debayer_cpu.cpp\n@@ -1011,20 +1011,22 @@ void DebayerCpu::updateLookupTables(const DebayerParams &params)\n \tconst unsigned int gammaTableSize = gammaTable_.size();\n \n \tconst RGB<float> blackIndex = params.blackLevel * kRGBLookupSize;\n+\tconst RGB<float> gains = params.gains;\n \tconst RGB<float> div = (RGB<float>(kRGBLookupSize) - blackIndex).max(1.0);\n \n \tif (ccmEnabled_) {\n \t\tif (gammaUpdateNeeded ||\n-\t\t    matrixChanged(params.combinedMatrix, params_.combinedMatrix)) {\n+\t\t    matrixChanged(params.combinedMatrix, params_.combinedMatrix) ||\n+\t\t    params.gains != params_.gains) {\n \t\t\tauto &red = swapRedBlueGains_ ? blueCcm_ : redCcm_;\n \t\t\tauto &green = greenCcm_;\n \t\t\tauto &blue = swapRedBlueGains_ ? redCcm_ : blueCcm_;\n \t\t\tconst unsigned int redIndex = swapRedBlueGains_ ? 2 : 0;\n \t\t\tconst unsigned int greenIndex = 1;\n \t\t\tconst unsigned int blueIndex = swapRedBlueGains_ ? 0 : 2;\n-\n \t\t\tfor (unsigned int i = 0; i < kRGBLookupSize; i++) {\n-\t\t\t\tconst RGB<float> rgb = ((RGB<float>(i) - blackIndex) * kRGBLookupSize / div).max(0.0);\n+\t\t\t\tconst RGB<float> rgb = (gains * (RGB<float>(i) - blackIndex) * kRGBLookupSize / div)\n+\t\t\t\t\t\t\t       .clamp(0.0, kRGBLookupSize - 1);\n \t\t\t\tred[i].r = std::round(rgb.r() * params.combinedMatrix[redIndex][0]);\n \t\t\t\tred[i].g = std::round(rgb.r() * params.combinedMatrix[greenIndex][0]);\n \t\t\t\tred[i].b = std::round(rgb.r() * params.combinedMatrix[blueIndex][0]);\n@@ -1039,11 +1041,9 @@ void DebayerCpu::updateLookupTables(const DebayerParams &params)\n \t\t}\n \t} else {\n \t\tif (gammaUpdateNeeded || params.gains != params_.gains) {\n-\t\t\tauto &gains = params.gains;\n \t\t\tauto &red = swapRedBlueGains_ ? blue_ : red_;\n \t\t\tauto &green = green_;\n \t\t\tauto &blue = swapRedBlueGains_ ? red_ : blue_;\n-\n \t\t\tfor (unsigned int i = 0; i < kRGBLookupSize; i++) {\n \t\t\t\tconst RGB<float> lutGains =\n \t\t\t\t\t(gains * (RGB<float>(i) - blackIndex) * gammaTableSize / div)\ndiff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp\nindex 5213166578b6aadc26078443181b96a2c59fa9d5..6ba197966d85e1ab3bdc337b9e4ddeb20f1ec2fa 100644\n--- a/src/libcamera/software_isp/debayer_egl.cpp\n+++ b/src/libcamera/software_isp/debayer_egl.cpp\n@@ -115,6 +115,7 @@ int DebayerEGL::getShaderVariableLocations(void)\n \tattributeTexture_ = glGetAttribLocation(programId_, \"textureIn\");\n \n \ttextureUniformBayerDataIn_ = glGetUniformLocation(programId_, \"tex_y\");\n+\tawbUniformDataIn_ = glGetUniformLocation(programId_, \"awb\");\n \tccmUniformDataIn_ = glGetUniformLocation(programId_, \"ccm\");\n \tblackLevelUniformDataIn_ = glGetUniformLocation(programId_, \"blacklevel\");\n \tgammaUniformDataIn_ = glGetUniformLocation(programId_, \"gamma\");\n@@ -128,6 +129,7 @@ int DebayerEGL::getShaderVariableLocations(void)\n \n \tLOG(Debayer, Debug) << \"vertexIn \" << attributeVertex_ << \" textureIn \" << attributeTexture_\n \t\t\t    << \" tex_y \" << textureUniformBayerDataIn_\n+\t\t\t    << \" awb \" << awbUniformDataIn_\n \t\t\t    << \" ccm \" << ccmUniformDataIn_\n \t\t\t    << \" blacklevel \" << blackLevelUniformDataIn_\n \t\t\t    << \" gamma \" << gammaUniformDataIn_\n@@ -504,6 +506,9 @@ void DebayerEGL::setShaderVariableValues(const DebayerParams &params)\n \tglUniform3f(blackLevelUniformDataIn_, params.blackLevel[0], params.blackLevel[1], params.blackLevel[2]);\n \tLOG(Debayer, Debug) << \" blackLevelUniformDataIn_ \" << blackLevelUniformDataIn_ << \" data \" << params.blackLevel;\n \n+\tglUniform3f(awbUniformDataIn_, params.gains[0], params.gains[1], params.gains[2]);\n+\tLOG(Debayer, Debug) << \" awbUniformDataIn_ \" << awbUniformDataIn_ << \" data \" << params.gains;\n+\n \t/*\n \t * Gamma\n \t */\ndiff --git a/src/libcamera/software_isp/debayer_egl.h b/src/libcamera/software_isp/debayer_egl.h\nindex fbd5430e4270678cf05d59cc98dff48315d2bb04..4c7c864726b7d5de1e92d4e74b0bc92860dc04c0 100644\n--- a/src/libcamera/software_isp/debayer_egl.h\n+++ b/src/libcamera/software_isp/debayer_egl.h\n@@ -90,6 +90,9 @@ private:\n \n \tGLint textureUniformBayerDataIn_;\n \n+\t/* Per-frame AWB gains */\n+\tGLint awbUniformDataIn_;\n+\n \t/* Represent per-frame CCM as a uniform vector of floats 3 x 3 */\n \tGLint ccmUniformDataIn_;\n \n",
    "prefixes": [
        "6/7"
    ]
}