From patchwork Mon Sep 6 23:04:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 13702 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 3E671C3242 for ; Mon, 6 Sep 2021 23:05:06 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 96D8669182; Tue, 7 Sep 2021 01:05:03 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="qfJdZHCV"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B84B169167 for ; Tue, 7 Sep 2021 01:04:59 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5A2FE993 for ; Tue, 7 Sep 2021 01:04:59 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1630969499; bh=qA/QDTXbRNCgEiMTWj43du7RQ3xu+VWaAzJ3TzR7K+c=; h=From:To:Subject:Date:In-Reply-To:References:From; b=qfJdZHCV/UYmk0NW29ns9XP+4ZBC70CYW11DaeLhwHqJGW3YdAzMIyC7Pq0sI1QoQ KX/Nw9cIbj20+3p4zGCMmDXh6VTrydaC0IBR9UEvL9D++r2bPsj+7ffnJBO0LJh/Sb epfe9XYHGsx1zdT707Xwti3XaN1t6aa30YMkoq7I= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 7 Sep 2021 02:04:36 +0300 Message-Id: <20210906230436.17106-6-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210906230436.17106-1-laurent.pinchart@ideasonboard.com> References: <20210906230436.17106-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v1 5/5] qcam: viewfinder_gl: Support configurable stride in shaders 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" The RGB and YUV conversion doesn't take the stride into account, neither when creating the textures, nor when sampling them. Fix it by using the stride as the texture width, and multiplying the x coordinate in the vertex shaders by a factor to only sample the active portion of the image. Signed-off-by: Laurent Pinchart --- src/qcam/assets/shader/identity.vert | 4 ++- src/qcam/viewfinder_gl.cpp | 54 ++++++++++++++++++++++------ src/qcam/viewfinder_gl.h | 1 + 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/qcam/assets/shader/identity.vert b/src/qcam/assets/shader/identity.vert index 6d6f7551017e..12c41377cfe7 100644 --- a/src/qcam/assets/shader/identity.vert +++ b/src/qcam/assets/shader/identity.vert @@ -9,8 +9,10 @@ attribute vec4 vertexIn; attribute vec2 textureIn; varying vec2 textureOut; +uniform float stride_factor; + void main(void) { gl_Position = vertexIn; - textureOut = textureIn; + textureOut = vec2(textureIn.x * stride_factor, textureIn.y); } diff --git a/src/qcam/viewfinder_gl.cpp b/src/qcam/viewfinder_gl.cpp index aeb1ea02d2d5..3ae8b03accb5 100644 --- a/src/qcam/viewfinder_gl.cpp +++ b/src/qcam/viewfinder_gl.cpp @@ -395,6 +395,7 @@ bool ViewFinderGL::createFragmentShader() textureUniformV_ = shaderProgram_.uniformLocation("tex_v"); textureUniformStep_ = shaderProgram_.uniformLocation("tex_step"); textureUniformSize_ = shaderProgram_.uniformLocation("tex_size"); + textureUniformStrideFactor_ = shaderProgram_.uniformLocation("stride_factor"); textureUniformBayerFirstRed_ = shaderProgram_.uniformLocation("tex_bayer_first_red"); /* Create the textures. */ @@ -464,6 +465,9 @@ void ViewFinderGL::initializeGL() void ViewFinderGL::doRender() { + /* Stride of the first plane, in pixels. */ + unsigned int stridePixels; + switch (format_) { case libcamera::formats::NV12: case libcamera::formats::NV21: @@ -477,7 +481,7 @@ void ViewFinderGL::doRender() glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, - size_.width(), + stride_, size_.height(), 0, GL_LUMINANCE, @@ -491,13 +495,15 @@ void ViewFinderGL::doRender() glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, - size_.width() / horzSubSample_, + stride_ / horzSubSample_, size_.height() / vertSubSample_, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, image_->data(1).data()); shaderProgram_.setUniformValue(textureUniformU_, 1); + + stridePixels = stride_; break; case libcamera::formats::YUV420: @@ -507,7 +513,7 @@ void ViewFinderGL::doRender() glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, - size_.width(), + stride_, size_.height(), 0, GL_LUMINANCE, @@ -521,7 +527,7 @@ void ViewFinderGL::doRender() glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, - size_.width() / horzSubSample_, + stride_ / horzSubSample_, size_.height() / vertSubSample_, 0, GL_LUMINANCE, @@ -535,13 +541,15 @@ void ViewFinderGL::doRender() glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, - size_.width() / horzSubSample_, + stride_ / horzSubSample_, size_.height() / vertSubSample_, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, image_->data(2).data()); shaderProgram_.setUniformValue(textureUniformV_, 2); + + stridePixels = stride_; break; case libcamera::formats::YVU420: @@ -551,7 +559,7 @@ void ViewFinderGL::doRender() glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, - size_.width(), + stride_, size_.height(), 0, GL_LUMINANCE, @@ -565,7 +573,7 @@ void ViewFinderGL::doRender() glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, - size_.width() / horzSubSample_, + stride_ / horzSubSample_, size_.height() / vertSubSample_, 0, GL_LUMINANCE, @@ -579,13 +587,15 @@ void ViewFinderGL::doRender() glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, - size_.width() / horzSubSample_, + stride_ / horzSubSample_, size_.height() / vertSubSample_, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, image_->data(2).data()); shaderProgram_.setUniformValue(textureUniformU_, 1); + + stridePixels = stride_; break; case libcamera::formats::UYVY: @@ -602,7 +612,7 @@ void ViewFinderGL::doRender() glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, - size_.width() / 2, + stride_ / 4, size_.height(), 0, GL_RGBA, @@ -619,6 +629,8 @@ void ViewFinderGL::doRender() shaderProgram_.setUniformValue(textureUniformStep_, 1.0f / (size_.width() / 2 - 1), 1.0f /* not used */); + + stridePixels = stride_ / 2; break; case libcamera::formats::ABGR8888: @@ -630,13 +642,15 @@ void ViewFinderGL::doRender() glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, - size_.width(), + stride_ / 4, size_.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image_->data(0).data()); shaderProgram_.setUniformValue(textureUniformY_, 0); + + stridePixels = stride_ / 4; break; case libcamera::formats::BGR888: @@ -646,13 +660,15 @@ void ViewFinderGL::doRender() glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, - size_.width(), + stride_ / 3, size_.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, image_->data(0).data()); shaderProgram_.setUniformValue(textureUniformY_, 0); + + stridePixels = stride_ / 3; break; case libcamera::formats::SBGGR8: @@ -692,11 +708,27 @@ void ViewFinderGL::doRender() shaderProgram_.setUniformValue(textureUniformStep_, 1.0f / (stride_ - 1), 1.0f / (size_.height() - 1)); + + /* + * The stride is already taken into account in the shaders, set + * the generic stride factor to 1.0. + */ + stridePixels = size_.width(); break; default: + stridePixels = size_.width(); break; }; + + /* + * Compute the stride factor for the vertex shader, to map the + * horizontal texture coordinate range [0.0, 1.0] to the active portion + * of the image. + */ + shaderProgram_.setUniformValue(textureUniformStrideFactor_, + static_cast(size_.width() - 1) / + (stridePixels - 1)); } void ViewFinderGL::paintGL() diff --git a/src/qcam/viewfinder_gl.h b/src/qcam/viewfinder_gl.h index 2b2b1e86035a..37b1ddd04b1d 100644 --- a/src/qcam/viewfinder_gl.h +++ b/src/qcam/viewfinder_gl.h @@ -97,6 +97,7 @@ private: /* Raw Bayer texture parameters */ GLuint textureUniformSize_; + GLuint textureUniformStrideFactor_; GLuint textureUniformBayerFirstRed_; QPointF firstRed_;