From patchwork Tue Jun 22 13:46:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12688 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 F3727C321B for ; Tue, 22 Jun 2021 13:47:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B23586893C; Tue, 22 Jun 2021 15:47:15 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="u+eUkAja"; dkim-atps=neutral Received: from mail-lj1-x22e.google.com (mail-lj1-x22e.google.com [IPv6:2a00:1450:4864:20::22e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8489660292 for ; Tue, 22 Jun 2021 15:47:14 +0200 (CEST) Received: by mail-lj1-x22e.google.com with SMTP id u11so19285704ljh.2 for ; Tue, 22 Jun 2021 06:47:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=toJkMwr37AcuRN1/3qMxHgEcqlJJsuiq1b1YEw63lPk=; b=u+eUkAjaJGEFrra6wa6saS9zp9nt8MST9TbwiQ/+0aLj/rtn2iOGojqr+fQ7XiYvoR Rcb4ob/cqq2sYF1FKw3fT3i2q++6o39DISjwc8s0qMaHjYEU/KAhAwEtQG2/wLONCiKr 1mClsdLNA95LIQhLXa2K1PnynaDNa5lk1eNuwX5fqazGLkTVDgV3mIsjsVyG7RHTrDvv 3dSUVTsVP3BvFbnlIIuMJ9jWoBGL1Now2KszliI3fSxC4CzyxoM+RAoVovwE0v0x7nYD 9Uwi4Ca0sbm2ggpJxeO8uNpyVfbrxBjmHDqc3Xc0Krj2QuT441dW2ghhoS/nRcTh/ZtQ dFig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=toJkMwr37AcuRN1/3qMxHgEcqlJJsuiq1b1YEw63lPk=; b=NDG0yQjGEZIesW0ejYB39sl8fBKurw8dM/zhtJE65ZEIhbwJ1tbEwDfSdnMSNrxpQn gFmDXw8baAAuinPl4tCxg+1GzohRAwLX8NlTiKhWKkzZ4NRxCwcuUmohkXaG0oNSUXEw ALiMr3uQHzIRh8gZlm4kqh+5SjtVOeLAe5CLFZ3yR8hTTwHEZjbXgCChA5srt11bi2zu FbK5jUc7VJ3phYEQNazeDM2DXyYafOMbwuXW6bQPhp/cPmQLfjCWURazouJyr2hwAnSP beUQSevTxADEUgbZrANkVO07X5kDE9R140kRwoN/fJ9xV6nlFFdKcMbPdUsW2nfp+PDB nlYA== X-Gm-Message-State: AOAM5332vpHpzkucS6k/P//vyO0cfuGS2iL3nVj6e0gdVnyblXnfgpqS L7Jl+1mLEGGAWjbjUeqphZPRfsFV/P/pog== X-Google-Smtp-Source: ABdhPJyCnNOKuamRlJeETEX/D/ZSr7LFdWtaogQdKsBC9gIOvylCG+GTT5MzOX9g9xg9XKICBsDwhA== X-Received: by 2002:a2e:9788:: with SMTP id y8mr3327363lji.100.1624369633926; Tue, 22 Jun 2021 06:47:13 -0700 (PDT) Received: from localhost.localdomain ([85.249.44.185]) by smtp.googlemail.com with ESMTPSA id b19sm163125ljo.37.2021.06.22.06.47.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Jun 2021 06:47:13 -0700 (PDT) From: Andrey Konovalov To: libcamera-devel@lists.libcamera.org, laurent.pinchart@ideasonboard.com Date: Tue, 22 Jun 2021 16:46:47 +0300 Message-Id: <20210622134652.1279260-5-andrey.konovalov@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210622134652.1279260-1-andrey.konovalov@linaro.org> References: <20210622134652.1279260-1-andrey.konovalov@linaro.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 4/9] qcam: viewfinder_gl: Copy the shaders for RAW8 Bayer format from motmot 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: , Cc: morgan@casual-effects.com, Morgan McGuire Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Morgan McGuire This commit copies the shaders from: https://github.com/motmot/libcamiface/commit/e36d51580510c211afc0430141085eb7a77d811b Signed-off-by: Andrey Konovalov Acked-by: Paul Elder Acked-by: Laurent Pinchart --- src/qcam/assets/shader/bayer_8.frag | 124 ++++++++++++++++++++++++++++ src/qcam/assets/shader/bayer_8.vert | 70 ++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 src/qcam/assets/shader/bayer_8.frag create mode 100644 src/qcam/assets/shader/bayer_8.vert diff --git a/src/qcam/assets/shader/bayer_8.frag b/src/qcam/assets/shader/bayer_8.frag new file mode 100644 index 00000000..707e76ed --- /dev/null +++ b/src/qcam/assets/shader/bayer_8.frag @@ -0,0 +1,124 @@ +/* +From http://jgt.akpeters.com/papers/McGuire08/ + +Efficient, High-Quality Bayer Demosaic Filtering on GPUs + +Morgan McGuire + +This paper appears in issue Volume 13, Number 4. +--------------------------------------------------------- +Copyright (c) 2008, Morgan McGuire. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +//Pixel Shader + +/** Monochrome RGBA or GL_LUMINANCE Bayer encoded texture.*/ +uniform sampler2D source; +varying vec4 center; +varying vec4 yCoord; +varying vec4 xCoord; + +void main(void) { + #define fetch(x, y) texture2D(source, vec2(x, y)).r + + float C = texture2D(source, center.xy).r; // ( 0, 0) + const vec4 kC = vec4( 4.0, 6.0, 5.0, 5.0) / 8.0; + + // Determine which of four types of pixels we are on. + vec2 alternate = mod(floor(center.zw), 2.0); + + vec4 Dvec = vec4( + fetch(xCoord[1], yCoord[1]), // (-1,-1) + fetch(xCoord[1], yCoord[2]), // (-1, 1) + fetch(xCoord[2], yCoord[1]), // ( 1,-1) + fetch(xCoord[2], yCoord[2])); // ( 1, 1) + + vec4 PATTERN = (kC.xyz * C).xyzz; + + // Can also be a dot product with (1,1,1,1) on hardware where that is + // specially optimized. + // Equivalent to: D = Dvec[0] + Dvec[1] + Dvec[2] + Dvec[3]; + Dvec.xy += Dvec.zw; + Dvec.x += Dvec.y; + + vec4 value = vec4( + fetch(center.x, yCoord[0]), // ( 0,-2) + fetch(center.x, yCoord[1]), // ( 0,-1) + fetch(xCoord[0], center.y), // (-1, 0) + fetch(xCoord[1], center.y)); // (-2, 0) + + vec4 temp = vec4( + fetch(center.x, yCoord[3]), // ( 0, 2) + fetch(center.x, yCoord[2]), // ( 0, 1) + fetch(xCoord[3], center.y), // ( 2, 0) + fetch(xCoord[2], center.y)); // ( 1, 0) + + // Even the simplest compilers should be able to constant-fold these to + // avoid the division. + // Note that on scalar processors these constants force computation of some + // identical products twice. + const vec4 kA = vec4(-1.0, -1.5, 0.5, -1.0) / 8.0; + const vec4 kB = vec4( 2.0, 0.0, 0.0, 4.0) / 8.0; + const vec4 kD = vec4( 0.0, 2.0, -1.0, -1.0) / 8.0; + + // Conserve constant registers and take advantage of free swizzle on load + #define kE (kA.xywz) + #define kF (kB.xywz) + + value += temp; + + // There are five filter patterns (identity, cross, checker, + // theta, phi). Precompute the terms from all of them and then + // use swizzles to assign to color channels. + // + // Channel Matches + // x cross (e.g., EE G) + // y checker (e.g., EE B) + // z theta (e.g., EO R) + // w phi (e.g., EO R) + #define A (value[0]) + #define B (value[1]) + #define D (Dvec.x) + #define E (value[2]) + #define F (value[3]) + + // Avoid zero elements. On a scalar processor this saves two MADDs + // and it has no effect on a vector processor. + PATTERN.yzw += (kD.yz * D).xyy; + + PATTERN += (kA.xyz * A).xyzx + (kE.xyw * E).xyxz; + PATTERN.xw += kB.xw * B; + PATTERN.xz += kF.xz * F; + + gl_FragColor.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)); +} diff --git a/src/qcam/assets/shader/bayer_8.vert b/src/qcam/assets/shader/bayer_8.vert new file mode 100644 index 00000000..3354860f --- /dev/null +++ b/src/qcam/assets/shader/bayer_8.vert @@ -0,0 +1,70 @@ +/* +From http://jgt.akpeters.com/papers/McGuire08/ + +Efficient, High-Quality Bayer Demosaic Filtering on GPUs + +Morgan McGuire + +This paper appears in issue Volume 13, Number 4. +--------------------------------------------------------- +Copyright (c) 2008, Morgan McGuire. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +//Vertex Shader + + +/** (w,h,1/w,1/h) */ +uniform vec4 sourceSize; + +/** Pixel position of the first red pixel in the */ +/** Bayer pattern. [{0,1}, {0, 1}]*/ +uniform vec2 firstRed; + +/** .xy = Pixel being sampled in the fragment shader on the range [0, 1] + .zw = ...on the range [0, sourceSize], offset by firstRed */ +varying vec4 center; + +/** center.x + (-2/w, -1/w, 1/w, 2/w); These are the x-positions */ +/** of the adjacent pixels.*/ +varying vec4 xCoord; + +/** center.y + (-2/h, -1/h, 1/h, 2/h); These are the y-positions */ +/** of the adjacent pixels.*/ +varying vec4 yCoord; + +void main(void) { + center.xy = gl_MultiTexCoord0.xy; + center.zw = gl_MultiTexCoord0.xy * sourceSize.xy + firstRed; + + vec2 invSize = sourceSize.zw; + xCoord = center.x + vec4(-2.0 * invSize.x, + -invSize.x, invSize.x, 2.0 * invSize.x); + yCoord = center.y + vec4(-2.0 * invSize.y, + -invSize.y, invSize.y, 2.0 * invSize.y); + + gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; +}