From patchwork Wed Nov 18 09:57:12 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: 28967 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 D9C14BDE17 for ; Wed, 18 Nov 2026 09:58:00 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9555B6A8C1; Wed, 18 Nov 2026 10:57:59 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="mD4XwrQ7"; dkim-atps=neutral Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 227C26A7EA for ; Wed, 18 Nov 2026 10:57:54 +0100 (CET) Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-4a7b8abe209so156832f8f.0 for ; Wed, 18 Nov 2026 01:57:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1794995874; x=1795600674; 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:content-type; bh=Ur2XLfwoeFuqH1nFOEOZ3nxmycfg2lV0KINd0LFocGI=; b=mD4XwrQ7p6MJ8s+qp5/+YNnrNlu5hJ54UzG8LPefHGj4EmT6L2JVnxnc13YQjEzELi rf+fFNFvv2ReCIvI+BJlpbMWHTeLbvxSFeh0vD4MuHHwTcSVjStKQnh82cn5vSS1ltjE oE+F/blLyys/xl83Uo8BTasrF2YwSwcDR4lLL5hIw/4l7ohbTPTGnW0liLWDi1EXd7/S tpGMICnkPi766Ij4hpIycOtZpjEW4n5y2ykWHHBulLbwbAMAwAwbLOt7EMuc+4SjXJqb 6T4QVL0IxiotPR1qrzEdZUAH0kHjA8MCPg9hXSxhKl1Fg3UuNmB4p3YCKgle3Uy1q+XB 82fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794995874; x=1795600674; 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:content-type; bh=Ur2XLfwoeFuqH1nFOEOZ3nxmycfg2lV0KINd0LFocGI=; b=Tq2lwI8SE8LW2rrkGZIWKdw+oBbdPNvbUPpf0AsdH8I8yLaTZVJCGZuIqv/SfCxh8O NK1EZ8JZmNtG2Bpg3Le3enskC3wkCocbhKmV5Jdg1BUoqys7y+ORJaqL8vUj1yGhzcTs AzpsD8ak8nVXYyJ86h1BLvVnfW47gOq31dCAxVyOlJyOID54ivgV8kBhQwKbQrTfCggl zut+zuJM4QPKNNWMYBZ4cSFaJhKlC5B80kMq5vutd1ompiRzsTjbE0/Nr5tQAtDBt4IQ FKv6DuYmSnuiC9QDFCktbuSDQlgZXcgj4dx6LAzXgOpU+emErViArWwgb9tYUtioLvHv Ya7Q== X-Gm-Message-State: APuU4Z8e8RMsLXVm2PrMlAp+fysg9mPZhHk69HtbkP/mSxbHsMbVid6r Ph3ZbH0AJCaHw/RHuAHEw03oXO2FZFzeb4d/BueUoHBMZq9AwWKee+RIEp1o8OSuTj4S5hwnuLL x5SlhMFI= X-Gm-Gg: AZFdTZetJzjrJhhrR5Cf16K1iUf4EQ5aWwsrH9LKgtEmigIO5pmHPSjnuFZBVJyVmaA Pr/M8zDiOKb24rknrF0ZazffB9H0F2fvHGoO/UD2WSFq/627G8cxKFSRpOk9G+H2vDoifIcP31O 86F2WPgrME7LJJCmPxpuPTstc2gnrGpWUBSzqF6j0n8wxC1jRD5lvB1FoDrQ113J229aImoPfa9 4lu3zLSJ7bkPWUGbrQ08BZRnJTeNZgt5m9+1WriUHKn+AYVc5e0scl0EVwJshfL7DSBGMIZ+Wvz djeUxLzLUqlLDhc+/wkGAzjM0/Mdfa3eTr8oc8gCJD27BJQePtFIyKgDI0KU2DduNtca4JcMpcW 37y5iJSkR2vdYizuk2aKJ0+n3CE5yLx97PGcPbD3QfyYtDkAicAYnLzjYxnIS6EFlnQS+J88lTv vnc7H8kFXJAgbDBMA7RDKwi9g9zPPKgWZzcMw+41HzLcSgxJNkNGpzEEFjQWlbBrV+FJR6zpsmF /sbmE3VYRdo+jdATuULIaUFkCTRCy2b4BrANTEWFCY5/qIn+IJ+yyT4MnQxqSQr8h/laT8T6gsP +GrP21pFTgmNdincgRSTnAk0zUX9SXKaaZ//QU/w5E6LMg== X-Received: by 2002:a05:6000:1a8d:b0:48d:fdd0:6ce8 with SMTP id ffacd0b85a97d-4a636582533mr4649514f8f.3.1794995873534; Wed, 18 Nov 2026 01:57:53 -0800 (PST) Received: from inspiron14p-linux ([109.76.187.112]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4a655f9f4a7sm3940871f8f.34.2026.11.18.01.57.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Nov 2026 01:57:52 -0800 (PST) From: Bryan O'Donoghue To: libcamera-devel@lists.libcamera.org Cc: rstrode@redhat.com, robert.mader@collabora.com, barnabas.pocze@ideasonboard.com, Bryan O'Donoghue Subject: [RFC PATCH v2 05/26] libcamera: software_isp: gpu: Switch to using GpuIspShaderPassDemosiac Date: Wed, 18 Nov 2026 10:57:12 +0100 Message-ID: <20261118095735.170587-6-bryan.odonoghue@linaro.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261118095735.170587-1-bryan.odonoghue@linaro.org> References: <20261118095735.170587-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" Switch to using GpuIspShaderPassDemosiac as the first instance of our multi-pass GPUISP implementation. Subsequent patches will move elements of this leviathan into discreet elements with an objective of eliminating register spill, separation of concerns and de-spaghettification. Code red patches are always best. Signed-off-by: Bryan O'Donoghue --- .../gpu_pipeline_shader_pass_demosiac.cpp | 29 ++ .../gpu_pipeline_shader_pass_demosiac.h | 9 + .../software_isp_instance_gpu.cpp | 352 +----------------- .../software_isp/software_isp_instance_gpu.h | 48 +-- 4 files changed, 57 insertions(+), 381 deletions(-) diff --git a/src/libcamera/software_isp/gpu_pipeline_shader_pass_demosiac.cpp b/src/libcamera/software_isp/gpu_pipeline_shader_pass_demosiac.cpp index 1c821c9fd..8c46e5795 100644 --- a/src/libcamera/software_isp/gpu_pipeline_shader_pass_demosiac.cpp +++ b/src/libcamera/software_isp/gpu_pipeline_shader_pass_demosiac.cpp @@ -48,6 +48,10 @@ int GpuIspShaderPassDemosiac::initShaders(PixelFormat inputFormat, PixelFormat o /* Specify GL_OES_EGL_image_external */ egl_.pushEnv(shaderEnv, "#extension GL_OES_EGL_image_external: enable"); + /* LSC flag */ + if (lscEnabled_) + egl_.pushEnv(shaderEnv, "#define APPLY_LSC"); + /* * Tell shaders how to re-order output taking account of how the pixels * are actually stored by EGL. @@ -173,6 +177,15 @@ int GpuIspShaderPassDemosiac::initShaders(PixelFormat inputFormat, PixelFormat o return -ENODEV; } + if (lscEnabled_) { + constexpr unsigned int gridSize = DebayerParams::kLscGridSize; + constexpr unsigned int stride = gridSize * DebayerParams::kLscBytesPerCell; + + eglImageLscLookup_.emplace(GL_RGBA, gridSize, gridSize, stride, + GL_TEXTURE2, 2); + egl_.createTexture2D(*eglImageLscLookup_, nullptr, GL_LINEAR); + } + return getShaderVariableLocations(); } @@ -188,6 +201,7 @@ int GpuIspShaderPassDemosiac::getShaderVariableLocations(void) blackLevelUniformDataIn_ = glGetUniformLocation(programId_, "blacklevel"); gammaUniformDataIn_ = glGetUniformLocation(programId_, "gamma"); contrastExpUniformDataIn_ = glGetUniformLocation(programId_, "contrastExp"); + textureUniformLsc_ = glGetUniformLocation(programId_, "lsc_tex"); textureUniformStep_ = glGetUniformLocation(programId_, "tex_step"); textureUniformSize_ = glGetUniformLocation(programId_, "tex_size"); @@ -305,6 +319,13 @@ void GpuIspShaderPassDemosiac::setShaderVariableValues(eGLImage &eglImageIn, con glUniformMatrix3fv(ccmUniformDataIn_, 1, GL_FALSE, ccm.data().data()); LOG(GpuShaderDemosiac, Debug) << " ccmUniformDataIn_ " << ccmUniformDataIn_ << " data " << params.combinedMatrix; + /* LSC table */ + if (lscEnabled_ && params.lscLutVersion != lscLutVersion_) { + egl_.updateTexture2D(*eglImageLscLookup_, params.lscLut.data()); + lscLutVersion_ = params.lscLutVersion; + glUniform1i(textureUniformLsc_, eglImageLscLookup_->texture_unit_uniform_id_); + } + /* * 0 = Red, 1 = Green, 2 = Blue */ @@ -329,4 +350,12 @@ void GpuIspShaderPassDemosiac::setShaderVariableValues(eGLImage &eglImageIn, con return; } +void GpuIspShaderPassDemosiac::stop() +{ + eglImageLscLookup_.reset(); + lscLutVersion_.reset(); + + GpuIspShaderPass::stop(); +} + } diff --git a/src/libcamera/software_isp/gpu_pipeline_shader_pass_demosiac.h b/src/libcamera/software_isp/gpu_pipeline_shader_pass_demosiac.h index eccab94f9..97f84d444 100644 --- a/src/libcamera/software_isp/gpu_pipeline_shader_pass_demosiac.h +++ b/src/libcamera/software_isp/gpu_pipeline_shader_pass_demosiac.h @@ -38,6 +38,9 @@ public: int getShaderVariableLocations(void) override; void setShaderVariableValues(eGLImage &eglImageIn, const DebayerParams ¶ms) override; + void setLscEnabled(bool enabled) { lscEnabled_ = enabled; } + void stop() override; + const char *name() const override { return "GpuIspShaderPassDemosiac"; } private: /* Shader parameters */ @@ -68,6 +71,12 @@ private: /* Contrast */ GLint contrastExpUniformDataIn_; + + /* Lens shading correction */ + bool lscEnabled_ = false; + std::optional eglImageLscLookup_; + std::optional lscLutVersion_; + GLint textureUniformLsc_; }; } /* namespace libcamera */ diff --git a/src/libcamera/software_isp/software_isp_instance_gpu.cpp b/src/libcamera/software_isp/software_isp_instance_gpu.cpp index b71413fca..4cec8f598 100644 --- a/src/libcamera/software_isp/software_isp_instance_gpu.cpp +++ b/src/libcamera/software_isp/software_isp_instance_gpu.cpp @@ -43,7 +43,7 @@ namespace libcamera { * \param[in] display The EGL display to use */ SoftwareIspInstanceGpu::SoftwareIspInstanceGpu(std::unique_ptr stats, const CameraManager &cm, EGLDisplay display) - : SoftwareIspInstance(cm), stats_(std::move(stats)), egl_(display) + : SoftwareIspInstance(cm), stats_(std::move(stats)), egl_(display), gpuIspShaderPassDemosiac_(egl_) { } @@ -97,184 +97,9 @@ int SoftwareIspInstanceGpu::getInputConfig(PixelFormat inputFormat, DebayerInput return -EINVAL; } -int SoftwareIspInstanceGpu::getShaderVariableLocations(void) -{ - attributeVertex_ = glGetAttribLocation(programId_, "vertexIn"); - attributeTexture_ = glGetAttribLocation(programId_, "textureIn"); - - textureUniformBayerDataIn_ = glGetUniformLocation(programId_, "tex_y"); - awbUniformDataIn_ = glGetUniformLocation(programId_, "awb"); - ccmUniformDataIn_ = glGetUniformLocation(programId_, "ccm"); - blackLevelUniformDataIn_ = glGetUniformLocation(programId_, "blacklevel"); - gammaUniformDataIn_ = glGetUniformLocation(programId_, "gamma"); - contrastExpUniformDataIn_ = glGetUniformLocation(programId_, "contrastExp"); - - 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"); - - textureUniformLsc_ = glGetUniformLocation(programId_, "lsc_tex"); - - LOG(SoftwareIspInstance, Debug) << "vertexIn " << attributeVertex_ << " textureIn " << attributeTexture_ - << " tex_y " << textureUniformBayerDataIn_ - << " awb " << awbUniformDataIn_ - << " ccm " << ccmUniformDataIn_ - << " blacklevel " << blackLevelUniformDataIn_ - << " gamma " << gammaUniformDataIn_ - << " contrastExp " << contrastExpUniformDataIn_ - << " tex_step " << textureUniformStep_ - << " tex_size " << textureUniformSize_ - << " stride_factor " << textureUniformStrideFactor_ - << " tex_bayer_first_red " << textureUniformBayerFirstRed_ - << " proj_matrix " << textureUniformProjMatrix_ - << " tex_lsc " << textureUniformLsc_; - return 0; -} - int SoftwareIspInstanceGpu::initBayerShaders(PixelFormat inputFormat, PixelFormat outputFormat) { - std::span fragmentShaderData; - std::span vertexShaderData; - std::vector shaderEnv; - GLenum err; - - /* Target gles 100 glsl requires "#version x" as first directive in shader */ - egl_.pushEnv(shaderEnv, "#version 100"); - - /* Specify GL_OES_EGL_image_external */ - egl_.pushEnv(shaderEnv, "#extension GL_OES_EGL_image_external: enable"); - - if (lscEnabled_) - egl_.pushEnv(shaderEnv, "#define APPLY_LSC"); - - /* - * Tell shaders how to re-order output taking account of how the pixels - * are actually stored by EGL. - */ - switch (outputFormat) { - case formats::ARGB8888: - case formats::XRGB8888: - break; - case formats::ABGR8888: - case formats::XBGR8888: - egl_.pushEnv(shaderEnv, "#define SWAP_BLUE"); - break; - default: - LOG(SoftwareIspInstance, Error) << "Unsupported output format"; - return -EINVAL; - } - - /* Pixel location parameters */ - glFormat_ = GL_LUMINANCE; - bytesPerPixel_ = 1; - shaderStridePixels_ = inputConfig_.stride; - - switch (inputFormat) { - case libcamera::formats::SBGGR8: - case libcamera::formats::SBGGR10_CSI2P: - case libcamera::formats::SBGGR12_CSI2P: - firstRed_x_ = 1.0; - firstRed_y_ = 1.0; - break; - case libcamera::formats::SGBRG8: - case libcamera::formats::SGBRG10_CSI2P: - case libcamera::formats::SGBRG12_CSI2P: - firstRed_x_ = 0.0; - firstRed_y_ = 1.0; - break; - case libcamera::formats::SGRBG8: - case libcamera::formats::SGRBG10_CSI2P: - case libcamera::formats::SGRBG12_CSI2P: - firstRed_x_ = 1.0; - firstRed_y_ = 0.0; - break; - case libcamera::formats::SRGGB8: - case libcamera::formats::SRGGB10_CSI2P: - case libcamera::formats::SRGGB12_CSI2P: - firstRed_x_ = 0.0; - firstRed_y_ = 0.0; - break; - default: - LOG(SoftwareIspInstance, Error) << "Unsupported input format"; - return -EINVAL; - }; - - /* Shader selection */ - switch (inputFormat) { - case libcamera::formats::SBGGR8: - case libcamera::formats::SGBRG8: - case libcamera::formats::SGRBG8: - case libcamera::formats::SRGGB8: - fragmentShaderData = bayer_unpacked_frag; - vertexShaderData = bayer_unpacked_vert; - break; - case libcamera::formats::SBGGR10_CSI2P: - case libcamera::formats::SGBRG10_CSI2P: - case libcamera::formats::SGRBG10_CSI2P: - case libcamera::formats::SRGGB10_CSI2P: - egl_.pushEnv(shaderEnv, "#define RAW10P"); - if (BayerFormat::fromPixelFormat(inputFormat).packing == BayerFormat::Packing::None) { - fragmentShaderData = bayer_unpacked_frag; - vertexShaderData = bayer_unpacked_vert; - glFormat_ = GL_RG; - bytesPerPixel_ = 2; - } else { - fragmentShaderData = bayer_1x_packed_frag; - vertexShaderData = identity_vert; - shaderStridePixels_ = width_; - } - break; - case libcamera::formats::SBGGR12_CSI2P: - case libcamera::formats::SGBRG12_CSI2P: - case libcamera::formats::SGRBG12_CSI2P: - case libcamera::formats::SRGGB12_CSI2P: - egl_.pushEnv(shaderEnv, "#define RAW12P"); - if (BayerFormat::fromPixelFormat(inputFormat).packing == BayerFormat::Packing::None) { - fragmentShaderData = bayer_unpacked_frag; - vertexShaderData = bayer_unpacked_vert; - glFormat_ = GL_RG; - bytesPerPixel_ = 2; - } else { - fragmentShaderData = bayer_1x_packed_frag; - vertexShaderData = identity_vert; - shaderStridePixels_ = width_; - } - break; - }; - - if (egl_.compileVertexShader(vertexShaderId_, vertexShaderData, - shaderEnv)) { - LOG(SoftwareIspInstance, Error) << "Compile vertex shader fail"; - return -ENODEV; - } - utils::scope_exit vShaderGuard([&] { glDeleteShader(vertexShaderId_); }); - - if (egl_.compileFragmentShader(fragmentShaderId_, fragmentShaderData, - shaderEnv)) { - LOG(SoftwareIspInstance, Error) << "Compile fragment shader fail"; - return -ENODEV; - } - utils::scope_exit fShaderGuard([&] { glDeleteShader(fragmentShaderId_); }); - - if (egl_.linkProgram(programId_, vertexShaderId_, fragmentShaderId_)) { - LOG(SoftwareIspInstance, Error) << "Linking program fail"; - return -ENODEV; - } - - egl_.dumpShaderSource(vertexShaderId_); - egl_.dumpShaderSource(fragmentShaderId_); - - /* Ensure we set the programId_ */ - glUseProgram(programId_); - err = glGetError(); - if (err != GL_NO_ERROR) { - LOG(SoftwareIspInstance, Error) << "Use program error " << err; - return -ENODEV; - } - - return getShaderVariableLocations(); + return gpuIspShaderPassDemosiac_.initShaders(inputFormat, outputFormat); } int SoftwareIspInstanceGpu::configure(const StreamConfiguration &inputCfg, @@ -300,7 +125,7 @@ int SoftwareIspInstanceGpu::configure(const StreamConfiguration &inputCfg, return -EINVAL; } - lscEnabled_ = lscEnabled; + gpuIspShaderPassDemosiac_.setLscEnabled(lscEnabled); inputConfig_.stride = inputCfg.stride; inputPixelFormat_ = inputCfg.pixelFormat; @@ -347,6 +172,12 @@ int SoftwareIspInstanceGpu::configure(const StreamConfiguration &inputCfg, inputBufferCount_ = inputCfg.bufferCount; outputBufferCount_ = outputCfg.bufferCount; + /* Configure for one pass */ + PassConfig rawSensorIn = { inputCfg.size, inputConfig_.stride, inputPixelFormat_, window_ }; + PassConfig rgbaOut = { outputCfg.size, outputConfig_.stride, outputPixelFormat_, Rectangle(nativeOutputSize_) }; + + gpuIspShaderPassDemosiac_.configure(rawSensorIn, rgbaOut); + return 0; } @@ -384,131 +215,6 @@ uint32_t SoftwareIspInstanceGpu::preferredInputStride(const PixelFormat &inputFo return info.stride(size.width, 0, 256); } -void SoftwareIspInstanceGpu::setShaderVariableValues(eGLImage &eglImageIn, const DebayerParams ¶ms) -{ - /* - * 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)width_, - (GLfloat)height_ }; - GLfloat Step[] = { static_cast(bytesPerPixel_) / (inputConfig_.stride - 1), - 1.0f / (height_ - 1) }; - GLfloat Stride = (GLfloat)width_ / (shaderStridePixels_ / bytesPerPixel_); - /* - * Scale the output size from the native size the algorithm produces for - * the input size. Keep the aspect ratio and prefer cropping over black - * bars. - */ - GLfloat scale = std::max((GLfloat)outputSize_.width / nativeOutputSize_.width, - (GLfloat)outputSize_.height / nativeOutputSize_.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(SoftwareIspInstance, Debug) << "vertexIn " << attributeVertex_ << " textureIn " << attributeTexture_ - << " tex_y " << textureUniformBayerDataIn_ - << " tex_step " << textureUniformStep_ - << " tex_size " << textureUniformSize_ - << " stride_factor " << textureUniformStrideFactor_ - << " tex_bayer_first_red " << textureUniformBayerFirstRed_; - - LOG(SoftwareIspInstance, 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_; - - /* Pre-transpose matrix for GLES 2.0. */ - Matrix ccm = params.combinedMatrix.transpose(); - glUniformMatrix3fv(ccmUniformDataIn_, 1, GL_FALSE, ccm.data().data()); - LOG(SoftwareIspInstance, Debug) << " ccmUniformDataIn_ " << ccmUniformDataIn_ << " data " << params.combinedMatrix; - - if (lscEnabled_ && params.lscLutVersion != lscLutVersion_) { - egl_.updateTexture2D(*eglImageLscLookup_, params.lscLut.data()); - lscLutVersion_ = params.lscLutVersion; - glUniform1i(textureUniformLsc_, eglImageLscLookup_->texture_unit_uniform_id_); - LOG(SoftwareIspInstance, Debug) << "LSC texture updated to version " << lscLutVersion_.value_or(-1); - } - - /* - * 0 = Red, 1 = Green, 2 = Blue - */ - glUniform3f(blackLevelUniformDataIn_, params.blackLevel[0], params.blackLevel[1], params.blackLevel[2]); - LOG(SoftwareIspInstance, Debug) << " blackLevelUniformDataIn_ " << blackLevelUniformDataIn_ << " data " << params.blackLevel; - - glUniform3f(awbUniformDataIn_, params.gains[0], params.gains[1], params.gains[2]); - LOG(SoftwareIspInstance, Debug) << " awbUniformDataIn_ " << awbUniformDataIn_ << " data " << params.gains; - - /* - * Gamma - */ - glUniform1f(gammaUniformDataIn_, params.gamma); - LOG(SoftwareIspInstance, Debug) << " gammaUniformDataIn_ " << gammaUniformDataIn_ << " data " << params.gamma; - - /* - * Contrast - */ - glUniform1f(contrastExpUniformDataIn_, params.contrastExp); - LOG(SoftwareIspInstance, Debug) << " contrastExpUniformDataIn_ " << contrastExpUniformDataIn_ << " data " << params.contrastExp; - - return; -} - eGLImage *SoftwareIspInstanceGpu::getCachedInputFrameBuffer(FrameBuffer *input, std::optional *inMapped, std::optional *inDmaSyncer) { const SharedFD &fd = input->planes()[0].fd; @@ -529,7 +235,9 @@ eGLImage *SoftwareIspInstanceGpu::getCachedInputFrameBuffer(FrameBuffer *input, LOG(SoftwareIspInstance, Debug) << "Input cache " << inputBufferCount_ << " exceeded evicted entry"; } - eglImageInCache_.emplace_back(fd, std::make_unique(glFormat_, inputConfig_.stride / bytesPerPixel_, height_, inputConfig_.stride, GL_TEXTURE0, 0)); + eglImageInCache_.emplace_back(fd, std::make_unique(gpuIspShaderPassDemosiac_.glFormat_, + inputConfig_.stride / gpuIspShaderPassDemosiac_.getBytesPerPixel(), + height_, inputConfig_.stride, GL_TEXTURE0, 0)); eglImageIn = eglImageInCache_.back().second.get(); if (egl_.createInputDMABufTexture2D(*eglImageIn, input->planes()[0].fd.get()) == 0) @@ -596,6 +304,7 @@ eGLImage *SoftwareIspInstanceGpu::getCachedOutputFrameBuffer(FrameBuffer *output int SoftwareIspInstanceGpu::processGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional *inMapped, std::optional *inDmaSyncer) { + int pipelineResult = 0; eGLImage *eglImageIn; eGLImage *eglImageOut; @@ -606,22 +315,9 @@ int SoftwareIspInstanceGpu::processGPU(FrameBuffer *input, FrameBuffer *output, if (!eglImageOut) return -ENOMEM; - egl_.attachTextureToFBO(*eglImageOut); - setShaderVariableValues(*eglImageIn, params); - - glViewport(0, 0, width_, height_); - glClear(GL_COLOR_BUFFER_BIT); - glDrawArrays(GL_TRIANGLE_FAN, 0, DEBAYER_OPENGL_COORDS); + pipelineResult = gpuIspShaderPassDemosiac_.process(*eglImageIn, *eglImageOut, width_, height_, params); - GLenum err = glGetError(); - if (err != GL_NO_ERROR) { - LOG(eGL, Error) << "Drawing scene fail " << err; - return -ENODEV; - } else { - egl_.flushOutput(); - } - - return 0; + return pipelineResult; } void SoftwareIspInstanceGpu::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms) @@ -691,18 +387,6 @@ int SoftwareIspInstanceGpu::start() if (initBayerShaders(inputPixelFormat_, outputPixelFormat_)) return -EINVAL; - if (lscEnabled_) { - constexpr unsigned int gridSize = DebayerParams::kLscGridSize; - constexpr unsigned int stride = gridSize * DebayerParams::kLscBytesPerCell; - eglImageLscLookup_.emplace(GL_RGBA, - gridSize, - gridSize, - stride, - GL_TEXTURE2, - 2); - egl_.createTexture2D(*eglImageLscLookup_, nullptr, GL_LINEAR); - } - return 0; } @@ -711,11 +395,7 @@ void SoftwareIspInstanceGpu::stop() eglImageOutCache_.clear(); eglImageInCache_.clear(); - eglImageLscLookup_.reset(); - lscLutVersion_.reset(); - - if (programId_) - glDeleteProgram(programId_); + gpuIspShaderPassDemosiac_.stop(); egl_.resetEGLContext(); } diff --git a/src/libcamera/software_isp/software_isp_instance_gpu.h b/src/libcamera/software_isp/software_isp_instance_gpu.h index d729b80e1..4a9de99ad 100644 --- a/src/libcamera/software_isp/software_isp_instance_gpu.h +++ b/src/libcamera/software_isp/software_isp_instance_gpu.h @@ -29,11 +29,11 @@ #include #include "software_isp_instance.h" +#include "gpu_pipeline_shader_pass_demosiac.h" namespace libcamera { #define DEBAYER_EGL_MIN_SIMPLE_RGB_GAIN_TEXTURE_UNITS 4 -#define DEBAYER_OPENGL_COORDS 4 class CameraManager; @@ -72,62 +72,20 @@ private: eGLImage *getCachedInputFrameBuffer(FrameBuffer *input, std::optional *inMapped, std::optional *inDmaSyncer); eGLImage *getCachedOutputFrameBuffer(FrameBuffer *output); - /* Shader program identifiers */ - GLuint vertexShaderId_ = 0; - GLuint fragmentShaderId_ = 0; - GLuint programId_ = 0; - /* Pointer to object representing input texture */ std::deque>> eglImageInCache_; std::deque>> eglImageOutCache_; unsigned int inputBufferCount_; unsigned int outputBufferCount_; - /* LSC lookup table */ - bool lscEnabled_; - std::optional eglImageLscLookup_; - std::optional lscLutVersion_; - - /* Shader parameters */ - float firstRed_x_; - float firstRed_y_; - GLint attributeVertex_; - GLint attributeTexture_; - GLint textureUniformStep_; - GLint textureUniformSize_; - GLint textureUniformStrideFactor_; - GLint textureUniformBayerFirstRed_; - GLint textureUniformProjMatrix_; - - GLint textureUniformBayerDataIn_; - - /* Per-frame AWB gains */ - GLint awbUniformDataIn_; - - /* Lens shading correction */ - GLint textureUniformLsc_; - - /* Represent per-frame CCM as a uniform vector of floats 3 x 3 */ - GLint ccmUniformDataIn_; - - /* Black Level compensation */ - GLint blackLevelUniformDataIn_; - - /* Gamma */ - GLint gammaUniformDataIn_; - - /* Contrast */ - GLint contrastExpUniformDataIn_; - Size nativeOutputSize_; Rectangle window_; std::unique_ptr stats_; eGL egl_; uint32_t width_; uint32_t height_; - GLint glFormat_; - unsigned int bytesPerPixel_; - uint32_t shaderStridePixels_; + + GpuIspShaderPassDemosiac gpuIspShaderPassDemosiac_; }; } /* namespace libcamera */