From patchwork Thu Jun 18 12:22:27 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bryan O'Donoghue X-Patchwork-Id: 26947 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 86C2FC330A for ; Thu, 18 Jun 2026 12:23:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9746B656D2; Thu, 18 Jun 2026 14:23:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="rcJylJk8"; dkim-atps=neutral Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 50FAB62C70 for ; Thu, 18 Jun 2026 14:23:08 +0200 (CEST) Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-490ace40f4bso8810835e9.3 for ; Thu, 18 Jun 2026 05:23:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1781785388; x=1782390188; 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=1GdJ+uFZzBx4VjBnnpCYkMDySJLH5xPvIqmnSSSWAlo=; b=rcJylJk8NM3QPIU/ACfBHgWLhOq128giTgFDLYX/DWHFGaGJhvMEf8ftKSqr71//Zp w45ZVz/eV3ey/qLEERi7oPIB78u3VG622amMJb1ricGgIwZovas2LRiFfVjFYFNWgXcy /OLfvLztsuh28fyqvLTTxbgcCQdYk4Qa4MB7P/KkcasEhf4zi7jaDI7OJr5wVX/qdcSa 2kNy4mp86z2teRIqZZg6Zopj2b3J0ijq9/Tc5Si5uAJWB0FVvGIEeiDyDVSHaZPO4odv sA4EEDNVYlmijpWAXHWE74+wvhNFdmAwyYLl+C230rYdC++7lGHGRzY+m6BBZddCUZko 5Gmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781785388; x=1782390188; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=1GdJ+uFZzBx4VjBnnpCYkMDySJLH5xPvIqmnSSSWAlo=; b=jMFkK+EXXO9voF0PDju9r6jp8xxxVkN79Jig5EN41FYJkcx4nquEiTBvTU55vXO8uj rublQZZgRZTPZcBRul/Je/OQuUsfr78UfGujHPTYfq4FFO8deTaVrxMvNOjUrBLlXYUC Lubdm/k7jWtNv/95VaLHeswQZnaivz9Dme9xdB5EZEZ9BCmzH3+4AFZD1j3ncYB64oS1 5jHk8DUN8PijLbHjWGPSroz1K9GWu6CSXLd5QUFtLVvtcxd0ksplGrHqqnqLbmdWR69F mRlo1iEm6algX7xbiNTpxWzc7+aqjPovY7Bqm7Zygy4ar9m9jkAJ9piB6wfZMJX+Q9k6 Nx7w== X-Gm-Message-State: AOJu0YyIi6/DIkDd5lRHKRZEf8FEfJJ2HpnXM4cRW+13TB86BH+oPkPT pzqJ13YNxyky+5l6IOMq7mHGcMk1T1tnmVGAM0v3Z1NHnQucYn6+4c6ckSKmKVrUqlDRZ1tlBmr sXdp6ZxE= X-Gm-Gg: AfdE7ckXq0tBZbJJi9Uqx2MJPDfNi5ZWZTJKVLo70n6Z+ngCPjpHbBIeOFajX/ivuIP lfIIw1yBW0ojcFJGowCqWG122wX+WD37mGvbGAaicmZAtEqzmqTAodg+v45po9h+evbHZWNanky 4jExOGhjRokNXkOSKMeLmx5B6wWz/oVMQWWLfUvP7nrXWABISLtWfbMadOr/dW1SUwRlO+YTOBl nwaNGWN2uoaFcqTdeiywLJcNOf8tI3qaPIzNPkHP/MnXGgJrQlvNIyW6hSVVPf+TFhpfErFhyd+ DT6dIKZEifHJaMf+mhc7DMOEh+pjICWnghOTQtyhN6h6Ksr/ttFv5is66f41jvXY3MuJbUjhTXm J4aLek4N3orO9K4OwIZB+DxGt/eYhSB3/8/saCMYpYoQlwvLtbuqQVj2qnCRkB4bAzRAqemAA/p WxPEu1ZZjQFa+vCVU9tt4CckYLVQrk X-Received: by 2002:a05:600c:630e:b0:490:d38c:7836 with SMTP id 5b1f17b1804b1-492381e8599mr54707075e9.3.1781785387785; Thu, 18 Jun 2026 05:23:07 -0700 (PDT) Received: from inspiron14p-linux ([109.76.144.236]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4922fa3a4easm275198015e9.3.2026.06.18.05.23.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jun 2026 05:23:07 -0700 (PDT) From: Bryan O'Donoghue To: libcamera-devel@lists.libcamera.org Cc: bryan.odonoghue@linaro.org, pavel@ucw.cz Subject: [PATCH 14/30] libcamera: software_isp: gpu: Add GpuIspShaderPassBlcNormalise Date: Thu, 18 Jun 2026 13:22:27 +0100 Message-ID: <20260618122245.946138-15-bryan.odonoghue@linaro.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260618122245.946138-1-bryan.odonoghue@linaro.org> References: <20260618122245.946138-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" Make GpuIspShaderPassBlcNormalise which takes existing logic to select fragment/vertex shaders based on the incoming CSI2 packign but, select new shaders whose task in life is to apply our blacklevel normalisation step and output a normalised 16 bit float. The unpacked 16f will then be fed into the existing unpacked demosiac shader. This patch compiles but doesn't do anything useful yet. Signed-off-by: Bryan O'Donoghue --- ...gpu_pipeline_shader_pass_blc_normalise.cpp | 228 ++++++++++++++++++ .../gpu_pipeline_shader_pass_blc_normalise.h | 55 +++++ src/libcamera/software_isp/meson.build | 1 + 3 files changed, 284 insertions(+) create mode 100644 src/libcamera/software_isp/gpu_pipeline_shader_pass_blc_normalise.cpp create mode 100644 src/libcamera/software_isp/gpu_pipeline_shader_pass_blc_normalise.h diff --git a/src/libcamera/software_isp/gpu_pipeline_shader_pass_blc_normalise.cpp b/src/libcamera/software_isp/gpu_pipeline_shader_pass_blc_normalise.cpp new file mode 100644 index 000000000..8e8d7b9d6 --- /dev/null +++ b/src/libcamera/software_isp/gpu_pipeline_shader_pass_blc_normalise.cpp @@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Linaro Ltd + * + * Authors: + * Bryan O'Donoghue + * + * GpuIspShaderPassBlcNormalise base class + */ + +#include + +#include +#include +#include + +#include +#include +#include + +#include "libcamera/internal/bayer_format.h" +#include "libcamera/internal/egl.h" +#include "libcamera/internal/software_isp/debayer_params.h" + +#include "gpu_pipeline_shader_pass_blc_normalise.h" + +/** + * \file software_isp.cpp + * \brief Simple software ISP implementation + */ + +namespace libcamera { + +LOG_DEFINE_CATEGORY(GpuShaderBlc) + +int GpuIspShaderPassBlcNormalise::start() +{ + return 0; +} + +void GpuIspShaderPassBlcNormalise::stop() +{ +} + +int GpuIspShaderPassBlcNormalise::selectShaders(struct ShaderConfig &shaderCfg, [[maybe_unused]]PixelFormat &inputFormat, [[maybe_unused]]PixelFormat &outputFormat) +{ + /* Pixel location parameters */ + glFormat_ = GL_LUMINANCE; + bytesPerPixel_ = 1; + shaderStridePixels_ = passInputCfg_.stride; + + /* Shader selection */ + switch (inputFormat) { + case libcamera::formats::SBGGR8: + case libcamera::formats::SGBRG8: + case libcamera::formats::SGRBG8: + case libcamera::formats::SRGGB8: + shaderCfg.fragmentShaderData = bayer_unpacked_to_blc_glr16f_frag; + shaderCfg.fragmentShaderDataLen = bayer_unpacked_to_blc_glr16f_frag_len; + shaderCfg.vertexShaderData = bayer_unpacked_vert; + shaderCfg.vertexShaderDataLen = bayer_unpacked_vert_len; + break; + case libcamera::formats::SBGGR10_CSI2P: + case libcamera::formats::SGBRG10_CSI2P: + case libcamera::formats::SGRBG10_CSI2P: + case libcamera::formats::SRGGB10_CSI2P: + egl_.pushEnv(shaderCfg.shaderEnv, "#define RAW10P"); + if (BayerFormat::fromPixelFormat(inputFormat).packing == BayerFormat::Packing::None) { + shaderCfg.fragmentShaderData = bayer_unpacked_to_blc_glr16f_frag; + shaderCfg.fragmentShaderDataLen = bayer_unpacked_to_blc_glr16f_frag_len; + shaderCfg.vertexShaderData = bayer_unpacked_vert; + shaderCfg.vertexShaderDataLen = bayer_unpacked_vert_len; + glFormat_ = GL_RG; + bytesPerPixel_ = 2; + } else { + shaderCfg.fragmentShaderData = bayer_1x_packed_to_blc_glr16f_frag; + shaderCfg.fragmentShaderDataLen = bayer_1x_packed_to_blc_glr16f_frag_len; + shaderCfg.vertexShaderData = identity_vert; + shaderCfg.vertexShaderDataLen = identity_vert_len; + shaderStridePixels_ = passInputCfg_.size.width; + } + break; + case libcamera::formats::SBGGR12_CSI2P: + case libcamera::formats::SGBRG12_CSI2P: + case libcamera::formats::SGRBG12_CSI2P: + case libcamera::formats::SRGGB12_CSI2P: + egl_.pushEnv(shaderCfg.shaderEnv, "#define RAW12P"); + if (BayerFormat::fromPixelFormat(inputFormat).packing == BayerFormat::Packing::None) { + shaderCfg.fragmentShaderData = bayer_unpacked_to_blc_glr16f_frag; + shaderCfg.fragmentShaderDataLen = bayer_unpacked_to_blc_glr16f_frag_len; + shaderCfg.vertexShaderData = bayer_unpacked_vert; + shaderCfg.vertexShaderDataLen = bayer_unpacked_vert_len; + glFormat_ = GL_RG; + bytesPerPixel_ = 2; + } else { + shaderCfg.fragmentShaderData = bayer_1x_packed_to_blc_glr16f_frag; + shaderCfg.fragmentShaderDataLen = bayer_1x_packed_to_blc_glr16f_frag_len; + shaderCfg.vertexShaderData = identity_vert; + shaderCfg.vertexShaderDataLen = identity_vert_len; + shaderStridePixels_ = passInputCfg_.size.width; + } + break; + }; + + return 0; +} + +int GpuIspShaderPassBlcNormalise::getShaderVariableLocations(void) +{ + attributeVertex_ = glGetAttribLocation(programId_, "vertexIn"); + attributeTexture_ = glGetAttribLocation(programId_, "textureIn"); + textureUniformBayerDataIn_ = glGetUniformLocation(programId_, "tex_y"); + textureUniformStep_ = glGetUniformLocation(programId_, "tex_step"); + textureUniformSize_ = glGetUniformLocation(programId_, "tex_size"); + textureUniformStrideFactor_ = glGetUniformLocation(programId_, "stride_factor"); + textureUniformBayerFirstRed_ = glGetUniformLocation(programId_, "tex_bayer_first_red"); + textureUniformProjMatrix_ = glGetUniformLocation(programId_, "proj_matrix"); + + LOG(GpuShaderBlc, Debug) << "vertexIn " << attributeVertex_ << " textureIn " << attributeTexture_ + << " tex_y " << textureUniformBayerDataIn_ + << " tex_step " << textureUniformStep_ + << " tex_size " << textureUniformSize_ + << " stride_factor " << textureUniformStrideFactor_ + << " tex_bayer_first_red " << textureUniformBayerFirstRed_ + << " proj_matrix " << textureUniformProjMatrix_; + + /* TODO: trap errors */ + return 0; +} + +void GpuIspShaderPassBlcNormalise::setShaderVariableValues(const DebayerParams ¶ms, eGLImage &eglImageIn) +{ + /* + * Raw Bayer 8-bit, and packed raw Bayer 10-bit/12-bit formats + * are stored in a GL_LUMINANCE texture. The texture width is + * equal to the stride. + */ + GLfloat firstRed[] = { firstRed_x_, firstRed_y_ }; + GLfloat imgSize[] = { (GLfloat)passInputCfg_.size.width, + (GLfloat)passInputCfg_.size.height }; + GLfloat Step[] = { static_cast(bytesPerPixel_) / (passInputCfg_.stride - 1), + 1.0f / (passInputCfg_.size.height - 1) }; + GLfloat Stride = (GLfloat)passInputCfg_.size.width / (shaderStridePixels_ / bytesPerPixel_); + /* + * Scale input to output size, keeping the aspect ratio and preferring + * cropping over black bars. + */ + GLfloat scale = std::max((GLfloat)passInputCfg_.window.width / passInputCfg_.size.width, + (GLfloat)passInputCfg_.window.height / passInputCfg_.size.height); + GLfloat trans = -(1.0f - scale); + GLfloat projMatrix[] = { + scale, 0, 0, 0, + 0, scale, 0, 0, + 0, 0, 1, 0, + trans, trans, 0, 1 + }; + /* Static const coordinates */ + static const GLfloat vcoordinates[4][2] = { + { -1.0f, -1.0f }, + { -1.0f, +1.0f }, + { +1.0f, +1.0f }, + { +1.0f, -1.0f }, + }; + static const GLfloat tcoordinates[4][2] = { + { 0.0f, 0.0f }, + { 0.0f, 1.0f }, + { 1.0f, 1.0f }, + { 1.0f, 0.0f }, + }; + + /* vertexIn - bayer_8.vert */ + glEnableVertexAttribArray(attributeVertex_); + glVertexAttribPointer(attributeVertex_, 2, GL_FLOAT, GL_TRUE, + 2 * sizeof(GLfloat), vcoordinates); + + /* textureIn - bayer_8.vert */ + glEnableVertexAttribArray(attributeTexture_); + glVertexAttribPointer(attributeTexture_, 2, GL_FLOAT, GL_TRUE, + 2 * sizeof(GLfloat), tcoordinates); + + /* + * Set the sampler2D to the respective texture unit for each texutre + * To simultaneously sample multiple textures we need to use multiple + * texture units + */ + glUniform1i(textureUniformBayerDataIn_, eglImageIn.texture_unit_uniform_id_); + + /* + * These values are: + * firstRed = tex_bayer_first_red - bayer_8.vert + * imgSize = tex_size - bayer_8.vert + * step = tex_step - bayer_8.vert + * Stride = stride_factor identity.vert + * textureUniformProjMatri = No scaling + */ + glUniform2fv(textureUniformBayerFirstRed_, 1, firstRed); + glUniform2fv(textureUniformSize_, 1, imgSize); + glUniform2fv(textureUniformStep_, 1, Step); + glUniform1f(textureUniformStrideFactor_, Stride); + glUniformMatrix4fv(textureUniformProjMatrix_, 1, GL_FALSE, projMatrix); + + LOG(GpuShaderBlc, Debug) << "vertexIn " << attributeVertex_ << " textureIn " << attributeTexture_ + << " tex_y " << textureUniformBayerDataIn_ + << " tex_step " << textureUniformStep_ + << " tex_size " << textureUniformSize_ + << " stride_factor " << textureUniformStrideFactor_ + << " tex_bayer_first_red " << textureUniformBayerFirstRed_; + + LOG(GpuShaderBlc, Debug) << "textureUniformY_ = 0 " + << " firstRed.x " << firstRed[0] + << " firstRed.y " << firstRed[1] + << " textureUniformSize_.width " << imgSize[0] + << " textureUniformSize_.height " << imgSize[1] + << " textureUniformStep_.x " << Step[0] + << " textureUniformStep_.y " << Step[1] + << " textureUniformStrideFactor_ " << Stride + << " textureUniformProjMatrix_ " << textureUniformProjMatrix_; + + /* + * 0 = Red, 1 = Green, 2 = Blue + */ + glUniform3f(blackLevelUniformDataIn_, params.blackLevel[0], params.blackLevel[1], params.blackLevel[2]); + LOG(GpuShaderBlc, Debug) << " blackLevelUniformDataIn_ " << blackLevelUniformDataIn_ << " data " << params.blackLevel; + + return; +} + +} /* namespace libcamera */ diff --git a/src/libcamera/software_isp/gpu_pipeline_shader_pass_blc_normalise.h b/src/libcamera/software_isp/gpu_pipeline_shader_pass_blc_normalise.h new file mode 100644 index 000000000..64ea2fc11 --- /dev/null +++ b/src/libcamera/software_isp/gpu_pipeline_shader_pass_blc_normalise.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Linaro Ltd + * + * Authors: + * Bryan O'Donoghue + * + * GpuIspPipelineShaderPass base class + */ + +#pragma once + +#include + +#include +#include +#include + +#include +#include + +#include "libcamera/internal/egl.h" +#include "libcamera/internal/software_isp/debayer_params.h" + +#include "gpu_pipeline_shader_pass.h" + +namespace libcamera { + +class FrameBuffer; + +class GpuIspShaderPassBlcNormalise : public GpuIspShaderPass +{ +public: + GpuIspShaderPassBlcNormalise(eGL& egl) : GpuIspShaderPass(egl) {}; + + int start(); + void stop(); + + /* Things that every ISP pipeline pass will need to do */ + int getShaderVariableLocations(void); + void setShaderVariableValues(const DebayerParams ¶ms, eGLImage &eglImageIn); + int selectShaders(struct ShaderConfig &shaderCfg, PixelFormat &inputFormat, PixelFormat &outputFormat); + const char *name() const override { return "GpuIspShaderPassBlcNormalise"; } + +private: + /* Shader parameters */ + GLint textureUniformStep_; + GLint textureUniformSize_; + + /* Black Level compensation */ + GLint blackLevelUniformDataIn_; + +}; + +} /* namespace libcamera */ diff --git a/src/libcamera/software_isp/meson.build b/src/libcamera/software_isp/meson.build index 3e7d21318..e9caa3629 100644 --- a/src/libcamera/software_isp/meson.build +++ b/src/libcamera/software_isp/meson.build @@ -34,6 +34,7 @@ if mesa_works 'software_isp_pipeline_gpu.cpp', 'gpu_pipeline_shader_pass.cpp', 'gpu_pipeline_shader_pass_demosiac.cpp', + 'gpu_pipeline_shader_pass_blc_normalise.cpp', ]) libcamera_deps += [ libegl,