[{"id":37136,"web_url":"https://patchwork.libcamera.org/comment/37136/","msgid":"<1be03cef-d99d-4ca4-8d6c-999974ff85fb@ideasonboard.com>","date":"2025-12-01T12:15:21","subject":"Re: [PATCH v2 02/22] utils: gen-shader-headers: Add a utility to\n\tgenerate headers from shaders","submitter":{"id":216,"url":"https://patchwork.libcamera.org/api/people/216/","name":"Barnabás Pőcze","email":"barnabas.pocze@ideasonboard.com"},"content":"Hi\n\n2025. 11. 27. 3:22 keltezéssel, Bryan O'Donoghue írta:\n> Two simple script to generate a header that contains GLSL shaders translated\n> to C arrays.\n> \n> Acked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>\n> ---\n>   utils/gen-shader-header.py  | 37 +++++++++++++++++++++++++\n>   utils/gen-shader-headers.sh | 55 +++++++++++++++++++++++++++++++++++++\n>   2 files changed, 92 insertions(+)\n>   create mode 100755 utils/gen-shader-header.py\n>   create mode 100755 utils/gen-shader-headers.sh\n> \n> diff --git a/utils/gen-shader-header.py b/utils/gen-shader-header.py\n> new file mode 100755\n> index 000000000..731a63b41\n> --- /dev/null\n> +++ b/utils/gen-shader-header.py\n> @@ -0,0 +1,37 @@\n> +#!/usr/bin/env python3\n> +# SPDX-License-Identifier: GPL-2.0-or-later\n> +# Copyright (C) 2025, Bryan O'Donoghue.\n> +#\n> +# Author: Bryan O'Donoghue <bryan.odonoghue@linaro.org>\n> +#\n> +# A Python script which takes a list of shader files and converts each of them\n> +# into a C header.\n> +#\n> +import sys\n> +\n> +try:\n> +    with open(sys.argv[2]) as file:\n\n\"rb\" as mode?\n\n\n> +        data = file.read()\n> +        data_len = len(data)\n> +\n> +        name = sys.argv[1].replace(\".\", \"_\")\n> +        name_len = name + \"_len\"\n> +\n> +        j = 0\n> +        print(\"unsigned char\", name, \"[] = {\")\n\nI think the array itself should also be marked `const`.\n\n\nRegards,\nBarnabás Pőcze\n\n> +        for ch in data:\n> +            print(f\"0x{ord(ch):02x}, \", end=\"\")\n> +            j = (j + 1) % 16\n> +            if j == 0:\n> +                print()\n> +        if j != 0:\n> +            print()\n> +        print(\"};\")\n> +\n> +        print()\n> +        print(f\"const unsigned int {name_len}={data_len};\")\n> +\n> +except FileNotFoundError:\n> +    print(f\"File {sys.argv[2]} not found\", file=sys.stderr)\n> +except IOError:\n> +    print(f\"Unable to read {sys.argv[2]}\", file=sys.stderr)\n> diff --git a/utils/gen-shader-headers.sh b/utils/gen-shader-headers.sh\n> new file mode 100755\n> index 000000000..81bf1584c\n> --- /dev/null\n> +++ b/utils/gen-shader-headers.sh\n> @@ -0,0 +1,55 @@\n> +#!/bin/sh\n> +\n> +set -e\n> +\n> +usage() {\n> +\techo \"Usage: $0 <src_dir> <build_dir> <output_header_name> <shader_file1> [shader_file2 ...]\"\n> +\techo\n> +\techo \"Generates a C header file containing hex-encoded shader data.\"\n> +\techo\n> +\techo \"Arguments:\"\n> +\techo \"  src_dir             Path to the base of the source directory\"\n> +\techo \"  build_dir           Directory where shader files are located and header will be written\"\n> +\techo \"  output_header_name  Name of the generated header file (relative to build_dir)\"\n> +\techo \"  shader_file(s)      One or more shader files to embed in the header\"\n> +\texit 1\n> +}\n> +\n> +if [ $# -lt 4 ]; then\n> +\techo \"Error: Invalid argument count.\"\n> +\tusage\n> +fi\n> +\n> +src_dir=\"$1\"; shift\n> +build_dir=\"$1\"; shift\n> +build_path=$build_dir/\"$1\"; shift\n> +\n> +cat <<EOF > \"$build_path\"\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/* This file is auto-generated, do not edit! */\n> +\n> +#pragma once\n> +\n> +EOF\n> +\n> +cat <<EOF >> \"$build_path\"\n> +/*\n> + * List the names of the shaders at the top of\n> + * header for readability's sake\n> + *\n> +EOF\n> +\n> +for file in \"$@\"; do\n> +\tname=$(basename \"$build_dir/$file\" | tr '.' '_')\n> +\techo \"[SHADER-GEN] $name\"\n> +\techo \" * unsigned char $name;\" >> \"$build_path\"\n> +done\n> +\n> +echo \"*/\" >> \"$build_path\"\n> +\n> +echo \"/* Hex encoded shader data */\" >> \"$build_path\"\n> +for file in \"$@\"; do\n> +\tname=$(basename \"$build_dir/$file\")\n> +\t\"$src_dir/utils/gen-shader-header.py\" \"$name\" \"$build_dir/$file\" >> \"$build_path\"\n> +\techo >> \"$build_path\"\n> +done","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 38E43BD80A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  1 Dec 2025 12:15:31 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 35B5A60A7B;\n\tMon,  1 Dec 2025 13:15:30 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C6EC460A7B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  1 Dec 2025 13:15:28 +0100 (CET)","from [192.168.33.24] (185.182.214.104.nat.pool.zt.hu\n\t[185.182.214.104])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 058AF6DF;\n\tMon,  1 Dec 2025 13:13:13 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"pos/BSuJ\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1764591194;\n\tbh=05x9JCU/Zg5t9cohL8f4oB8j3Vzmq0PMSZAlM5LlcKc=;\n\th=Date:Subject:To:Cc:References:From:In-Reply-To:From;\n\tb=pos/BSuJUzD/9gV9T29b8p63G1bYRaGCuIHJ74jlUW3X+k7dFXRZSMmlTODs6JCwu\n\tumKU38dD7Vhy6NorVvyQz38TdAAgCWwb96Xob26HTEqtEGJURdfHkmPNJkL8nFQtVb\n\tkxqH/vs1gycrnwHdkKMc2E17k+coIc6YeyBOpKFU=","Message-ID":"<1be03cef-d99d-4ca4-8d6c-999974ff85fb@ideasonboard.com>","Date":"Mon, 1 Dec 2025 13:15:21 +0100","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH v2 02/22] utils: gen-shader-headers: Add a utility to\n\tgenerate headers from shaders","To":"Bryan O'Donoghue <bryan.odonoghue@linaro.org>,\n\tlibcamera-devel@lists.libcamera.org","Cc":"pavel@ucw.cz, Kieran Bingham <kieran.bingham@ideasonboard.com>","References":"<20251127022256.178929-1-bryan.odonoghue@linaro.org>\n\t<20251127022256.178929-3-bryan.odonoghue@linaro.org>","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","Content-Language":"en-US, hu-HU","In-Reply-To":"<20251127022256.178929-3-bryan.odonoghue@linaro.org>","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"8bit","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]