[v4,2/4] libcamera: software_isp: Use specified LSC grid size in debayering
diff mbox series

Message ID 20261202151305.132644-3-mzamazal@redhat.com
State New
Headers show
Series
  • softisp tuning script for LSC
Related show

Commit Message

Milan Zamazal Dec. 2, 2026, 3:13 p.m. UTC
The lens shading correction texture (grid) has currently the fixed size
of 17x17 cells in software ISP.  Let's make the size flexible instead,
in order to be able to reuse external tuning data, perhaps make GPU
happier with sizes of powers of 2, and to provide size flexibility
generally.

This commit prepares GPU debayering for this change.  lscEnabled_ is
replaced by lscGridSize_.  It is empty in case lens shading correction
is disabled and contains the grid size otherwise.

Software ISP still passes to GPU debayering either an empty grid size or
the grid size of 17x17.  Making the grid size flexible is implemented in
the followup commits.

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
---
 .../internal/software_isp/software_isp.h       |  1 +
 src/libcamera/software_isp/debayer.cpp         |  3 ++-
 src/libcamera/software_isp/debayer.h           |  2 +-
 src/libcamera/software_isp/debayer_cpu.cpp     |  2 +-
 src/libcamera/software_isp/debayer_cpu.h       |  2 +-
 src/libcamera/software_isp/debayer_egl.cpp     | 18 +++++++++---------
 src/libcamera/software_isp/debayer_egl.h       |  4 ++--
 src/libcamera/software_isp/software_isp.cpp    |  7 ++++++-
 8 files changed, 23 insertions(+), 16 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/software_isp/software_isp.h b/include/libcamera/internal/software_isp/software_isp.h
index b64910637..c8ed91949 100644
--- a/include/libcamera/internal/software_isp/software_isp.h
+++ b/include/libcamera/internal/software_isp/software_isp.h
@@ -102,6 +102,7 @@  private:
 	DmaBufAllocator dmaHeap_;
 	bool ccmEnabled_;
 	bool lscEnabled_;
+	std::optional<Size> lscGridSize_;
 
 	std::unique_ptr<ipa::softisp::IPAProxySoftIsp> ipa_;
 	std::deque<FrameBuffer *> queuedInputBuffers_;
diff --git a/src/libcamera/software_isp/debayer.cpp b/src/libcamera/software_isp/debayer.cpp
index 4f6659000..65820eabd 100644
--- a/src/libcamera/software_isp/debayer.cpp
+++ b/src/libcamera/software_isp/debayer.cpp
@@ -112,7 +112,8 @@  Debayer::~Debayer()
  * \param[in] inputCfg The input configuration
  * \param[in] outputCfgs The output configurations
  * \param[in] ccmEnabled Whether a color correction matrix is applied
- * \param[in] lscEnabled Whether lens shading correction grid is provided
+ * \param[in] lscGridSize The size of the lens shading correction grid;
+     empty if the correction is not enabled
  *
  * \return 0 on success, a negative errno on failure
  */
diff --git a/src/libcamera/software_isp/debayer.h b/src/libcamera/software_isp/debayer.h
index e30a05279..246b85b67 100644
--- a/src/libcamera/software_isp/debayer.h
+++ b/src/libcamera/software_isp/debayer.h
@@ -41,7 +41,7 @@  public:
 	virtual int configure(const StreamConfiguration &inputCfg,
 			      const std::vector<std::reference_wrapper<const StreamConfiguration>> &outputCfgs,
 			      bool ccmEnabled,
-			      bool lscEnabled) = 0;
+			      const std::optional<Size> &lscGridSize) = 0;
 
 	virtual std::vector<PixelFormat> formats(PixelFormat inputFormat) = 0;
 
diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp
index 91867baa6..2ac4f142f 100644
--- a/src/libcamera/software_isp/debayer_cpu.cpp
+++ b/src/libcamera/software_isp/debayer_cpu.cpp
@@ -625,7 +625,7 @@  int DebayerCpu::setDebayerFunctions(PixelFormat inputFormat,
 int DebayerCpu::configure(const StreamConfiguration &inputCfg,
 			  const std::vector<std::reference_wrapper<const StreamConfiguration>> &outputCfgs,
 			  bool ccmEnabled,
-			  [[maybe_unused]] bool lscEnabled)
+			  [[maybe_unused]] const std::optional<Size> &lscGridSize)
 {
 	if (getInputConfig(inputCfg.pixelFormat, inputConfig_) != 0) {
 		LOG(Debayer, Error)
diff --git a/src/libcamera/software_isp/debayer_cpu.h b/src/libcamera/software_isp/debayer_cpu.h
index ae570893b..5759ad08f 100644
--- a/src/libcamera/software_isp/debayer_cpu.h
+++ b/src/libcamera/software_isp/debayer_cpu.h
@@ -38,7 +38,7 @@  public:
 	int configure(const StreamConfiguration &inputCfg,
 		      const std::vector<std::reference_wrapper<const StreamConfiguration>> &outputCfgs,
 		      bool ccmEnabled,
-		      bool lscEnabled) override;
+		      const std::optional<Size> &lscGridSize) override;
 	Size patternSize(PixelFormat inputFormat) override;
 	std::vector<PixelFormat> formats(PixelFormat input) override;
 	std::tuple<unsigned int, unsigned int>
diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp
index 33933b11d..ce836f088 100644
--- a/src/libcamera/software_isp/debayer_egl.cpp
+++ b/src/libcamera/software_isp/debayer_egl.cpp
@@ -146,7 +146,7 @@  int DebayerEGL::initBayerShaders(PixelFormat inputFormat, PixelFormat outputForm
 	/* Specify GL_OES_EGL_image_external */
 	egl_.pushEnv(shaderEnv, "#extension GL_OES_EGL_image_external: enable");
 
-	if (lscEnabled_)
+	if (lscGridSize_)
 		egl_.pushEnv(shaderEnv, "#define APPLY_LSC");
 
 	/*
@@ -280,7 +280,7 @@  int DebayerEGL::initBayerShaders(PixelFormat inputFormat, PixelFormat outputForm
 int DebayerEGL::configure(const StreamConfiguration &inputCfg,
 			  const std::vector<std::reference_wrapper<const StreamConfiguration>> &outputCfgs,
 			  [[maybe_unused]] bool ccmEnabled,
-			  bool lscEnabled)
+			  const std::optional<Size> &lscGridSize)
 {
 	if (getInputConfig(inputCfg.pixelFormat, inputConfig_) != 0) {
 		LOG(Debayer, Error)
@@ -300,7 +300,7 @@  int DebayerEGL::configure(const StreamConfiguration &inputCfg,
 		return -EINVAL;
 	}
 
-	lscEnabled_ = lscEnabled;
+	lscGridSize_ = lscGridSize;
 
 	inputConfig_.stride = inputCfg.stride;
 	inputPixelFormat_ = inputCfg.pixelFormat;
@@ -478,7 +478,7 @@  void DebayerEGL::setShaderVariableValues(eGLImage &eglImageIn, const DebayerPara
 	glUniformMatrix3fv(ccmUniformDataIn_, 1, GL_FALSE, ccm.data().data());
 	LOG(Debayer, Debug) << " ccmUniformDataIn_ " << ccmUniformDataIn_ << " data " << params.combinedMatrix;
 
-	if (lscEnabled_ && params.lscLutVersion != lscLutVersion_) {
+	if (lscGridSize_ && params.lscLutVersion != lscLutVersion_) {
 		egl_.updateTexture2D(*eglImageLscLookup_, params.lscLut.data());
 		lscLutVersion_ = params.lscLutVersion;
 		glUniform1i(textureUniformLsc_, eglImageLscLookup_->texture_unit_uniform_id_);
@@ -691,12 +691,12 @@  int DebayerEGL::start()
 	if (initBayerShaders(inputPixelFormat_, outputPixelFormat_))
 		return -EINVAL;
 
-	if (lscEnabled_) {
-		constexpr unsigned int gridSize = DebayerParams::kLscGridSize;
-		constexpr unsigned int stride = gridSize * DebayerParams::kLscBytesPerCell;
+	if (lscGridSize_) {
+		const Size &gridSize = lscGridSize_.value();
+		const unsigned int stride = gridSize.width * DebayerParams::kLscBytesPerCell;
 		eglImageLscLookup_.emplace(GL_RGBA,
-					   gridSize,
-					   gridSize,
+					   gridSize.width,
+					   gridSize.height,
 					   stride,
 					   GL_TEXTURE2,
 					   2);
diff --git a/src/libcamera/software_isp/debayer_egl.h b/src/libcamera/software_isp/debayer_egl.h
index cbbc6a450..977e1871e 100644
--- a/src/libcamera/software_isp/debayer_egl.h
+++ b/src/libcamera/software_isp/debayer_egl.h
@@ -46,7 +46,7 @@  public:
 	int configure(const StreamConfiguration &inputCfg,
 		      const std::vector<std::reference_wrapper<const StreamConfiguration>> &outputCfgs,
 		      bool ccmEnabled,
-		      bool lscEnabled) override;
+		      const std::optional<Size> &lscGridSize) override;
 
 	Size patternSize(PixelFormat inputFormat) override;
 
@@ -84,7 +84,7 @@  private:
 	unsigned int outputBufferCount_;
 
 	/* LSC lookup table */
-	bool lscEnabled_;
+	std::optional<Size> lscGridSize_;
 	std::optional<eGLImage> eglImageLscLookup_;
 	std::optional<uint64_t> lscLutVersion_;
 
diff --git a/src/libcamera/software_isp/software_isp.cpp b/src/libcamera/software_isp/software_isp.cpp
index 627625b4f..88349598a 100644
--- a/src/libcamera/software_isp/software_isp.cpp
+++ b/src/libcamera/software_isp/software_isp.cpp
@@ -171,6 +171,11 @@  SoftwareIsp::SoftwareIsp(PipelineHandler *pipe, const CameraSensor *sensor,
 		return;
 	}
 
+	if (lscEnabled_)
+		lscGridSize_ = Size(DebayerParams::kLscGridSize, DebayerParams::kLscGridSize);
+	else
+		lscGridSize_.reset();
+
 	ipa_->paramsComputed.connect(this, &SoftwareIsp::saveIspParams);
 	ipa_->metadataReady.connect(this,
 				    [this](uint32_t frame, const ControlList &metadata) {
@@ -294,7 +299,7 @@  int SoftwareIsp::configure(const StreamConfiguration &inputCfg,
 	if (ret < 0)
 		return ret;
 
-	ret = debayer_->configure(inputCfg, outputCfgs, ccmEnabled_, lscEnabled_);
+	ret = debayer_->configure(inputCfg, outputCfgs, ccmEnabled_, lscGridSize_);
 	if (ret < 0)
 		return ret;