[{"id":17540,"web_url":"https://patchwork.libcamera.org/comment/17540/","msgid":"<YMf527pipG8L46lA@pendragon.ideasonboard.com>","date":"2021-06-15T00:52:43","subject":"Re: [libcamera-devel] [PATCH v3 4/4] qcam: viewfinder_gl: Add\n\tsupport for RAW8 Bayer formats","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Andrey,\n\nThank you for the patch.\n\nThis patch also seems to have ignored quite a few comments from v2. Was\nthat intentional ? Did I miss a reply to the comments ?\n\nv3 drops the vertex shader, which was indeed not used in v2, but I'm not\nsure that the right direction to take, as the idea in Morgan's code was\nto use the free interpolator offered by the vertex shader to perform\ncomputation of coordinates.\n\nOn Fri, Jun 11, 2021 at 07:27:26PM +0300, Andrey Konovalov wrote:\n> All the four Bayer orders are supported.\n> \n> The texture coordinates passed to the fragment shader are ajusted\n> to point to the nearest pixel in the image. This prevents artifacts\n> when the image is scaled from the frame resolution to the window size.\n> \n> Signed-off-by: Andrey Konovalov <andrey.konovalov@linaro.org>\n> ---\n>  src/qcam/assets/shader/bayer_8.frag | 136 ++++++++++++++++++++++++++++\n>  src/qcam/assets/shader/shaders.qrc  |   1 +\n>  src/qcam/viewfinder_gl.cpp          |  37 +++++++-\n>  3 files changed, 172 insertions(+), 2 deletions(-)\n>  create mode 100644 src/qcam/assets/shader/bayer_8.frag\n> \n> diff --git a/src/qcam/assets/shader/bayer_8.frag b/src/qcam/assets/shader/bayer_8.frag\n> new file mode 100644\n> index 00000000..d93ef1da\n> --- /dev/null\n> +++ b/src/qcam/assets/shader/bayer_8.frag\n> @@ -0,0 +1,136 @@\n> +/* SPDX-License-Identifier: BSD-2-Clause */\n> +/*\n> +From http://jgt.akpeters.com/papers/McGuire08/\n> +\n> +Efficient, High-Quality Bayer Demosaic Filtering on GPUs\n> +\n> +Morgan McGuire\n> +\n> +This paper appears in issue Volume 13, Number 4.\n> +---------------------------------------------------------\n> +Copyright (c) 2008, Morgan McGuire. All rights reserved.\n> +\n> +\n> +Modified by Linaro Ltd to integrate it into libcamera, and to\n> +fix the artifacts due to pixel coordinates interpolation.\n> +Copyright (C) 2021, Linaro\n> +*/\n> +\n> +//Pixel Shader\n> +\n> +varying vec2 textureOut;\n> +\n> +/* The texture size: tex_size.xy is in bytes, tex_size.zw is in pixels */\n> +uniform vec4 tex_size;\n> +uniform vec2 tex_step;\n> +\n> +/** Pixel position of the first red pixel in the */\n> +/**  Bayer pattern.  [{0,1}, {0, 1}]*/\n> +uniform vec2            tex_bayer_first_red;\n> +\n> +/** Monochrome RGBA or GL_LUMINANCE Bayer encoded texture.*/\n> +uniform sampler2D       tex_raw;\n> +\n> +void main(void) {\n> +    #define fetch(x, y) texture2D(tex_raw, vec2(x, y)).r\n> +\n> +    /** .xy = Pixel being sampled in the fragment shader on the range [0, 1]\n> +        .zw = ...on the range [0, sourceSize], offset by firstRed */\n> +    vec4            center;\n> +\n> +    /** center.x + (-2/w, -1/w, 1/w, 2/w); These are the x-positions */\n> +    /** of the adjacent pixels.*/\n> +    vec4            xCoord;\n> +\n> +    /** center.y + (-2/h, -1/h, 1/h, 2/h); These are the y-positions */\n> +    /** of the adjacent pixels.*/\n> +    vec4            yCoord;\n> +\n> +    /* Align the center coordinates to the nearest pixel */\n> +    center.zw = floor(textureOut * tex_size.zw);\n> +    center.xy = center.zw * tex_step;\n> +    center.zw += tex_bayer_first_red;\n> +\n> +    xCoord = center.x + vec4(-2.0 * tex_step.x,\n> +                             -tex_step.x, tex_step.x, 2.0 * tex_step.x);\n> +    yCoord = center.y + vec4(-2.0 * tex_step.y,\n> +                              -tex_step.y, tex_step.y, 2.0 * tex_step.y);\n> +\n> +    float C = texture2D(tex_raw, center.xy).r; // ( 0, 0)\n> +    const vec4 kC = vec4( 4.0,  6.0,  5.0,  5.0) / 8.0;\n> +\n> +    // Determine which of four types of pixels we are on.\n> +    vec2 alternate = mod(floor(center.zw), 2.0);\n> +\n> +    vec4 Dvec = vec4(\n> +        fetch(xCoord[1], yCoord[1]),  // (-1,-1)\n> +        fetch(xCoord[1], yCoord[2]),  // (-1, 1)\n> +        fetch(xCoord[2], yCoord[1]),  // ( 1,-1)\n> +        fetch(xCoord[2], yCoord[2])); // ( 1, 1)\n> +\n> +    vec4 PATTERN = (kC.xyz * C).xyzz;\n> +\n> +    // Can also be a dot product with (1,1,1,1) on hardware where that is\n> +    // specially optimized.\n> +    // Equivalent to: D = Dvec[0] + Dvec[1] + Dvec[2] + Dvec[3];\n> +    Dvec.xy += Dvec.zw;\n> +    Dvec.x  += Dvec.y;\n> +\n> +    vec4 value = vec4(\n> +        fetch(center.x, yCoord[0]),   // ( 0,-2)\n> +        fetch(center.x, yCoord[1]),   // ( 0,-1)\n> +        fetch(xCoord[0], center.y),   // (-2, 0)\n> +        fetch(xCoord[1], center.y));  // (-1, 0)\n> +\n> +    vec4 temp = vec4(\n> +        fetch(center.x, yCoord[3]),   // ( 0, 2)\n> +        fetch(center.x, yCoord[2]),   // ( 0, 1)\n> +        fetch(xCoord[3], center.y),   // ( 2, 0)\n> +        fetch(xCoord[2], center.y));  // ( 1, 0)\n> +\n> +    // Even the simplest compilers should be able to constant-fold these to\n> +    // avoid the division.\n> +    // Note that on scalar processors these constants force computation of some\n> +    // identical products twice.\n> +    const vec4 kA = vec4(-1.0, -1.5,  0.5, -1.0) / 8.0;\n> +    const vec4 kB = vec4( 2.0,  0.0,  0.0,  4.0) / 8.0;\n> +    const vec4 kD = vec4( 0.0,  2.0, -1.0, -1.0) / 8.0;\n> +\n> +    // Conserve constant registers and take advantage of free swizzle on load\n> +    #define kE (kA.xywz)\n> +    #define kF (kB.xywz)\n> +\n> +    value += temp;\n> +\n> +    // There are five filter patterns (identity, cross, checker,\n> +    // theta, phi).  Precompute the terms from all of them and then\n> +    // use swizzles to assign to color channels.\n> +    //\n> +    // Channel   Matches\n> +    //   x       cross   (e.g., EE G)\n> +    //   y       checker (e.g., EE B)\n> +    //   z       theta   (e.g., EO R)\n> +    //   w       phi     (e.g., EO R)\n> +    #define A (value[0])\n> +    #define B (value[1])\n> +    #define D (Dvec.x)\n> +    #define E (value[2])\n> +    #define F (value[3])\n> +\n> +    // Avoid zero elements. On a scalar processor this saves two MADDs\n> +    // and it has no effect on a vector processor.\n> +    PATTERN.yzw += (kD.yz * D).xyy;\n> +\n> +    PATTERN += (kA.xyz * A).xyzx + (kE.xyw * E).xyxz;\n> +    PATTERN.xw  += kB.xw * B;\n> +    PATTERN.xz  += kF.xz * F;\n> +\n> +    vec3 rgb = (alternate.y == 0.0) ?\n> +        ((alternate.x == 0.0) ?\n> +            vec3(C, PATTERN.xy) :\n> +            vec3(PATTERN.z, C, PATTERN.w)) :\n> +        ((alternate.x == 0.0) ?\n> +            vec3(PATTERN.w, C, PATTERN.z) :\n> +            vec3(PATTERN.yx, C));\n> +    gl_FragColor = vec4(rgb, 1.0);\n> +}\n> diff --git a/src/qcam/assets/shader/shaders.qrc b/src/qcam/assets/shader/shaders.qrc\n> index d76d65c5..79f44a30 100644\n> --- a/src/qcam/assets/shader/shaders.qrc\n> +++ b/src/qcam/assets/shader/shaders.qrc\n> @@ -5,6 +5,7 @@\n>  \t<file>YUV_2_planes.frag</file>\n>  \t<file>YUV_3_planes.frag</file>\n>  \t<file>YUV_packed.frag</file>\n> +\t<file>bayer_8.frag</file>\n>  \t<file>bayer_1x_packed.frag</file>\n>  \t<file>identity.vert</file>\n>  </qresource>\n> diff --git a/src/qcam/viewfinder_gl.cpp b/src/qcam/viewfinder_gl.cpp\n> index dcfaf973..98b6b39d 100644\n> --- a/src/qcam/viewfinder_gl.cpp\n> +++ b/src/qcam/viewfinder_gl.cpp\n> @@ -36,6 +36,11 @@ static const QList<libcamera::PixelFormat> supportedFormats{\n>  \tlibcamera::formats::RGBA8888,\n>  \tlibcamera::formats::BGR888,\n>  \tlibcamera::formats::RGB888,\n> +\t/* Raw Bayer 8-bit */\n> +\tlibcamera::formats::SBGGR8,\n> +\tlibcamera::formats::SGBRG8,\n> +\tlibcamera::formats::SGRBG8,\n> +\tlibcamera::formats::SRGGB8,\n>  \t/* Raw Bayer 10-bit packed */\n>  \tlibcamera::formats::SBGGR10_CSI2P,\n>  \tlibcamera::formats::SGBRG10_CSI2P,\n> @@ -220,6 +225,30 @@ bool ViewFinderGL::selectFormat(const libcamera::PixelFormat &format)\n>  \t\tfragmentShaderDefines_.append(\"#define RGB_PATTERN bgr\");\n>  \t\tfragmentShaderFile_ = \":RGB.frag\";\n>  \t\tbreak;\n> +\tcase libcamera::formats::SBGGR8:\n> +\t\tfirstRed_.setX(1.0);\n> +\t\tfirstRed_.setY(1.0);\n> +\t\tfragmentShaderFile_ = \":bayer_8.frag\";\n> +\t\ttextureMinMagFilters_ = GL_NEAREST;\n> +\t\tbreak;\n> +\tcase libcamera::formats::SGBRG8:\n> +\t\tfirstRed_.setX(0.0);\n> +\t\tfirstRed_.setY(1.0);\n> +\t\tfragmentShaderFile_ = \":bayer_8.frag\";\n> +\t\ttextureMinMagFilters_ = GL_NEAREST;\n> +\t\tbreak;\n> +\tcase libcamera::formats::SGRBG8:\n> +\t\tfirstRed_.setX(1.0);\n> +\t\tfirstRed_.setY(0.0);\n> +\t\tfragmentShaderFile_ = \":bayer_8.frag\";\n> +\t\ttextureMinMagFilters_ = GL_NEAREST;\n> +\t\tbreak;\n> +\tcase libcamera::formats::SRGGB8:\n> +\t\tfirstRed_.setX(0.0);\n> +\t\tfirstRed_.setY(0.0);\n> +\t\tfragmentShaderFile_ = \":bayer_8.frag\";\n> +\t\ttextureMinMagFilters_ = GL_NEAREST;\n> +\t\tbreak;\n>  \tcase libcamera::formats::SBGGR10_CSI2P:\n>  \t\tfirstRed_.setX(1.0);\n>  \t\tfirstRed_.setY(1.0);\n> @@ -624,6 +653,10 @@ void ViewFinderGL::doRender()\n>  \t\tshaderProgram_.setUniformValue(textureUniformY_, 0);\n>  \t\tbreak;\n>  \n> +\tcase libcamera::formats::SBGGR8:\n> +\tcase libcamera::formats::SGBRG8:\n> +\tcase libcamera::formats::SGRBG8:\n> +\tcase libcamera::formats::SRGGB8:\n>  \tcase libcamera::formats::SBGGR10_CSI2P:\n>  \tcase libcamera::formats::SGBRG10_CSI2P:\n>  \tcase libcamera::formats::SGRBG10_CSI2P:\n> @@ -633,8 +666,8 @@ void ViewFinderGL::doRender()\n>  \tcase libcamera::formats::SGRBG12_CSI2P:\n>  \tcase libcamera::formats::SRGGB12_CSI2P:\n>  \t\t/*\n> -\t\t * Packed raw Bayer 10-bit and 12-bit formats are stored in\n> -\t\t * GL_RED texture.\n> +\t\t * Raw Bayer 8-bit, and packed raw Bayer 10-bit/12-bit formats\n> +\t\t * are stored in GL_RED texture.\n>  \t\t * The texture width is equal to the stride.\n>  \t\t */\n>  \t\tglActiveTexture(GL_TEXTURE0);","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 30CB7C3218\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 15 Jun 2021 00:53:06 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9BF8668932;\n\tTue, 15 Jun 2021 02:53:05 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5BD0B68925\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 15 Jun 2021 02:53:04 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id BBF214A3;\n\tTue, 15 Jun 2021 02:53:03 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"O8CpSD+u\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1623718383;\n\tbh=XruNNptZU/46H55ablh/a3xkpZ24bVcwjk4BSR4ojqs=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=O8CpSD+uFaq9ghsWRApuwLM1FqgNL159pjWo336e0dAyQgdytU0wsJetTHwfIBgVa\n\tSXFEnUSd/Bxv9v4akd1EnGA0vaHZsExMxomc0laevHAXqFiYazFhOqg7qNboFg8SjH\n\tttbcRaPvOwmpZl8mndZzjnnwpLu/fV09keBpxxU4=","Date":"Tue, 15 Jun 2021 03:52:43 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Andrey Konovalov <andrey.konovalov@linaro.org>","Message-ID":"<YMf527pipG8L46lA@pendragon.ideasonboard.com>","References":"<20210611162726.824789-1-andrey.konovalov@linaro.org>\n\t<20210611162726.824789-5-andrey.konovalov@linaro.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210611162726.824789-5-andrey.konovalov@linaro.org>","Subject":"Re: [libcamera-devel] [PATCH v3 4/4] qcam: viewfinder_gl: Add\n\tsupport for RAW8 Bayer formats","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>","Cc":"morgan@casual-effects.com, libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]