From patchwork Sun Aug 14 16:07:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kunal Agarwal X-Patchwork-Id: 17117 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 39AE4C3272 for ; Sun, 14 Aug 2022 16:08:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 050F161FBA; Sun, 14 Aug 2022 18:08:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1660493332; bh=cn5cxbSO9TKRTz95mqMJUItKo7xk7Pgzu8KIow2cAJU=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=FSOZVADeoMrCTz1uaKOg0VCZJ16UEueZ/WTjT4Z2git4PH2wLFStb8LidGYnct48n AhuKzNLG1H2d9hS0ViV2WTgzCQtY95SHCaPz976y6nmXtmykd+IN1iBaO9FODoq+0U vt3hQomMQhNLEGQvbJffMB1kVHx7xkYAn6Sv6oGtQ/FHjgYxJ5PZ3liGKDo/2ubB5U Hy/rxYPZtWIR3K07PwjWVxkM/jBdd4+upbmg4neOAGotogMir9c38D0pd5ta0soGVg 3oU13K/M5HM99Map3QNohHkHLdSqVlKhinNhY+WuOncn4tP55dWYX9neM/JdNrb5W6 NOSd8f9xROuMw== Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BA28761FBA for ; Sun, 14 Aug 2022 18:08:50 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="R1BMq26o"; dkim-atps=neutral Received: by mail-pl1-x62d.google.com with SMTP id p18so4583541plr.8 for ; Sun, 14 Aug 2022 09:08:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=vK8jRGiVpF4kQwMqlfQbjrWhJfA8FqWvwjpfSDafvp4=; b=R1BMq26okBNtAWsSyv04RqlGeD5hCdhgZ6YAe/gHuEHPDPQRpejWQQUqhp0tqbq53M RpZKJpr+yOMaYYmSD2FLoCnVy9rPy/nwkK5QT8Ca2n3w8B9DQZc23iR0HAJ+Zsof6aP1 6345GjXsNpUipU7lcUPopeJdzhvKutUx1rslHe1jS19cr7d1KNYQnfHriVOVQnGcKNeg s2IYIF5B26NPmg3J5Sgt+tKgJ90TMRjq3mNSB/9MXsl5uYIR+0KhclUCoKR8r8SMaiCB tgKofmb3cav58I70yuNMkf1sf3LHcpa4V9yojqFgpA903XXboTntwrONNcuts5qJNjKI qbBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=vK8jRGiVpF4kQwMqlfQbjrWhJfA8FqWvwjpfSDafvp4=; b=XOrxL/YRnIhBQtcDtDKshMSqOY15uJ+wDB4ddhQSpjgQVgot4qu0VJQMf/65MbB3hl rEkicJrrvdzwbRxEOJrxVPAYSTJLhaNDSMSqHD3t8vt83fSsniE4mVlc+56SfuNDbzdS XhaWN+tYFxFSDu1KyIK8Ua1MYEG+lFUqFg02oxwNbmPy57UGmRE+vXVvB5GMBlfNMARh 5ABMln47Ri30c9rNpkog4RpSzv6qGMsmdWVYN/9DTjN70FIhlr8MObm+9mnD7+1geoLo NsdabqhO4dSihUSF1SIMo8+b9Tj7dA4NO7u5pjGRwxFAOzogstS/2lGxmWavGIcP0e9w taNg== X-Gm-Message-State: ACgBeo3i8h6S/EITWnYm7bMnkE66bvR6o6GLhy9kAqVR3LjjxEeQVMUF Bb5lNM2muqtARfj3W1rkB93G4LM68A8= X-Google-Smtp-Source: AA6agR7mIHt8BYXc+FBs7NETfUeH9thK3i3dIcfoVXpGFnho0ganRfwcM8bFnYi6GVNuiATR83Jdkg== X-Received: by 2002:a17:90a:e2cc:b0:1f3:1151:e0ff with SMTP id fr12-20020a17090ae2cc00b001f31151e0ffmr14582275pjb.50.1660493329069; Sun, 14 Aug 2022 09:08:49 -0700 (PDT) Received: from pop-os.localdomain ([115.96.217.20]) by smtp.googlemail.com with ESMTPSA id j6-20020a170902da8600b001709aea1516sm5576653plx.276.2022.08.14.09.08.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Aug 2022 09:08:48 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Sun, 14 Aug 2022 21:37:43 +0530 Message-Id: <20220814160747.52093-2-kunalagarwal1072002@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220814160747.52093-1-kunalagarwal1072002@gmail.com> References: <20220814160747.52093-1-kunalagarwal1072002@gmail.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 2/6] pipeline: simple: Implements Shader Handling 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: , X-Patchwork-Original-From: Kunal Agarwal via libcamera-devel From: Kunal Agarwal Reply-To: Kunal Agarwal Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Shader Class which includes functions for building, activating, deleting and compiling shaders Signed-off-by: Kunal Agarwal --- src/libcamera/pipeline/simple/shader.cpp | 110 +++++++++++++++++++++++ src/libcamera/pipeline/simple/shader.h | 34 +++++++ 2 files changed, 144 insertions(+) create mode 100644 src/libcamera/pipeline/simple/shader.cpp create mode 100644 src/libcamera/pipeline/simple/shader.h diff --git a/src/libcamera/pipeline/simple/shader.cpp b/src/libcamera/pipeline/simple/shader.cpp new file mode 100644 index 00000000..f0079618 --- /dev/null +++ b/src/libcamera/pipeline/simple/shader.cpp @@ -0,0 +1,110 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2022, Kunal Agarwal + * + * shader.cpp - Shader Handling + */ + +#include "shader.h" + +#include +#include + +#include + +namespace libcamera { + +LOG_DECLARE_CATEGORY(SimplePipeline) + +/* Reads a text file and outputs a string with everything in the text file */ +static std::string get_file_contents(const char *filename) +{ + std::string fullname = std::string("/home/pi/Desktop/compile/libcamera/src/libcamera/pipeline/simple/shader/") + filename; + + File file(fullname); + if (!file.open(File::OpenModeFlag::ReadOnly)) + return ""; + + Span data = file.map(); + return std::string(reinterpret_cast(data.data()), data.size()); +} + +/* Constructor that build the Shader Program from 2 different shaders */ +void ShaderProgram::callShader(const char *vertexFile, const char *fragmentFile) +{ + /* Read vertexFile and fragmentFile and store the strings */ + std::string vertexCode = get_file_contents(vertexFile); + std::string fragmentCode = get_file_contents(fragmentFile); + const char *vertexSource = vertexCode.c_str(); + const char *fragmentSource = fragmentCode.c_str(); + + /* Create the vertex shader, set its source code and compile it. */ + GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertexShader, 1, &vertexSource, NULL); + glCompileShader(vertexShader); + compileErrors(vertexShader, "VERTEX"); + + /* Create the fragment shader, set its source code and compile it. */ + GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragmentShader, 1, &fragmentSource, NULL); + glCompileShader(fragmentShader); + compileErrors(fragmentShader, "FRAGMENT"); + + /* Create Shader Program Object and get its reference */ + id_ = glCreateProgram(); + + /* Attach and wrap-up/link the Vertex and Fragment Shaders to the Shader Program */ + glAttachShader(id_, vertexShader); + glAttachShader(id_, fragmentShader); + glLinkProgram(id_); + + /* Checks if Shaders linked succesfully */ + compileErrors(id_, "PROGRAM"); + + /* Delete the Vertex and Fragment Shader objects. Here, they are flagged for deletion + and will not be deleted until they are detached from the program object. This frees + up the memory used to store the shader source. */ + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); +} + +/* Activates the Shader Program */ +void ShaderProgram::activate() +{ + glUseProgram(id_); +} + +/* Deletes the Shader Program */ +void ShaderProgram::deleteProgram() +{ + glDeleteProgram(id_); +} + +/* Checks if the different Shaders have compiled properly */ +void ShaderProgram::compileErrors(unsigned int shader, const char *type) +{ + /* Stores status of compilation */ + GLint hasCompiled; + GLint logLength; + /* Character array to store error message in */ + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength); + char *infoLog = new char[logLength]; + if (strcmp(type, "PROGRAM") != 0) { + glGetShaderiv(shader, GL_COMPILE_STATUS, &hasCompiled); + if (hasCompiled == GL_FALSE) { + glGetShaderInfoLog(shader, logLength, NULL, infoLog); + LOG(SimplePipeline, Error) << "SHADER_COMPILATION_ERROR for:" + << type << "\t" + << infoLog; + } + } else { + glGetProgramiv(shader, GL_LINK_STATUS, &hasCompiled); + if (hasCompiled == GL_FALSE) { + glGetProgramInfoLog(shader, logLength, NULL, infoLog); + LOG(SimplePipeline, Error) << "SHADER_LINKING_ERROR for:" + << type << "\t" + << infoLog; + } + } +} +} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/simple/shader.h b/src/libcamera/pipeline/simple/shader.h new file mode 100644 index 00000000..921e4040 --- /dev/null +++ b/src/libcamera/pipeline/simple/shader.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2022, Kunal Agarwal + * + * shader.h - Shader Handling + */ + +#pragma once + +#include +#include + +#include + +namespace libcamera { + +class ShaderProgram +{ +public: + void callShader(const char *vertexFile, const char *fragmentFile); + + void activate(); + + void deleteProgram(); + + int id() const { return id_; }; + +private: + /* Reference ID of the Shader Program */ + GLuint id_; + void compileErrors(unsigned int shader, const char *type); +}; + +} /* namespace libcamera */