From patchwork Tue Jun 30 08:30:29 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 27137 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 CEB82C3264 for ; Tue, 30 Jun 2026 08:30:38 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 98A3765F4E; Tue, 30 Jun 2026 10:30:38 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="q/y/wdyW"; 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 03ECC65F3F for ; Tue, 30 Jun 2026 10:30:36 +0200 (CEST) Received: from killaraus.ideasonboard.com (2001-14ba-70f3-e800--a06.rev.dnainternet.fi [IPv6:2001:14ba:70f3:e800::a06]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 322E8D52; Tue, 30 Jun 2026 10:29:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1782808192; bh=aW5t+7LnOeEWb2hlVGf4cRvYqMTO1Vef42mzNX33KgY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q/y/wdyWRxnRUh3kNwU1GW6HcW16cpwamI5a584AG2t/R0E3s7ax7xQqZIXGQe/Fo fyLBLWwon7UOHLqYHKCp0yjrFSp0MVsEh/x2VRZyepHofoutra4DO94J+Ota03dgLf abFYYUlqWCGlO6wfelmPPVsVLM6bG1uBsiJdoJvM= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Cc: Bryan O'Donoghue Subject: [PATCH 2/4] utils: Merge shader header generation scripts Date: Tue, 30 Jun 2026 11:30:29 +0300 Message-ID: <20260630083031.3197714-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260630083031.3197714-1-laurent.pinchart@ideasonboard.com> References: <20260630083031.3197714-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 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 shader header is generated by a combination of a shell script and a Python script. Simplify this by merging everything into a single script. The output of the old and new script only differ in white spaces. Signed-off-by: Laurent Pinchart Reviewed-by: Milan Zamazal Reviewed-by: Barnabás Pőcze Reviewed-by: Bryan O'Donoghue --- src/libcamera/shaders/meson.build | 2 +- utils/gen-shader-header.py | 37 ---------------- utils/gen-shader-headers.py | 71 +++++++++++++++++++++++++++++++ utils/gen-shader-headers.sh | 54 ----------------------- utils/meson.build | 2 +- 5 files changed, 73 insertions(+), 93 deletions(-) delete mode 100755 utils/gen-shader-header.py create mode 100755 utils/gen-shader-headers.py delete mode 100755 utils/gen-shader-headers.sh diff --git a/src/libcamera/shaders/meson.build b/src/libcamera/shaders/meson.build index adac77327a3b..4f4e8da607c7 100644 --- a/src/libcamera/shaders/meson.build +++ b/src/libcamera/shaders/meson.build @@ -14,7 +14,7 @@ libcamera_shader_headers = custom_target( 'gen-shader-headers', input : [shader_files], output : 'glsl_shaders.h', - command : [gen_shader_headers, meson.project_source_root(), '@OUTPUT@', '@INPUT@'], + command : [gen_shader_headers, '-o', '@OUTPUT@', '@INPUT@'], ) libcamera_internal_headers += libcamera_shader_headers diff --git a/utils/gen-shader-header.py b/utils/gen-shader-header.py deleted file mode 100755 index 745852b4c03f..000000000000 --- a/utils/gen-shader-header.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# Copyright (C) 2025, Bryan O'Donoghue. -# -# Author: Bryan O'Donoghue -# -# A Python script which takes a list of shader files and converts each of them -# into a C header. -# -import sys - -try: - with open(sys.argv[2], "rb") as file: - data = file.read() - data_len = len(data) - - name = sys.argv[1].replace(".", "_") - name_len = name + "_len" - - j = 0 - print("unsigned char const", name, "[] = {") - for ch in data: - print(f"0x{ch:02x}, ", end="") - j = (j + 1) % 16 - if j == 0: - print() - if j != 0: - print() - print("};") - - print() - print(f"const unsigned int {name_len}={data_len};") - -except FileNotFoundError: - print(f"File {sys.argv[2]} not found", file=sys.stderr) -except IOError: - print(f"Unable to read {sys.argv[2]}", file=sys.stderr) diff --git a/utils/gen-shader-headers.py b/utils/gen-shader-headers.py new file mode 100755 index 000000000000..908217a2bd2e --- /dev/null +++ b/utils/gen-shader-headers.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright (C) 2025, Bryan O'Donoghue +# Copyright (C) 2026, Ideas on Board Oy +# +# Generate a C header file containing hex-encoded shader sources + +import argparse +import binascii +import math +import os +import sys + + +def process_file(name, out): + data = open(name, 'rb').read() + + hex_data = [f'0x{c:02x}' for c in data] + var_name = os.path.basename(name).replace('.', '_') + + out.write(f'unsigned char const {var_name}[] = {{\n') + + for i in range(math.ceil(len(data) / 16)): + out.write('\t') + out.write(', '.join(hex_data[16 * i:16 * (i + 1)])) + out.write(',\n') + + out.write('};\n\n') + out.write(f'const unsigned int {var_name}_len = {len(data)};\n') + + +def main(argv): + parser = argparse.ArgumentParser( + description='Generate a C header file containing hex-encoded shader sources') + parser.add_argument('--output', '-o', metavar='file', default=sys.stdout, + type=argparse.FileType('w', encoding='utf-8'), + help='Output file name. Defaults to standard output if not specified.') + parser.add_argument('input', nargs='+', type=str, + help='Input file names.') + args = parser.parse_args(argv[1:]) + + args.output.write('''/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* This file is auto-generated, do not edit! */ + +#pragma once + +/* + * List the names of the shaders at the top of header for readability's sake. + * +''') + + for name in args.input: + name = os.path.basename(name).replace('.', '_') + args.output.write(f' * unsigned char {name};\n') + + args.output.write(''' */ + +/* Hex encoded shader data */ +''') + + for name in args.input: + process_file(name, args.output) + args.output.write('\n') + + return 0 + + +if __name__ == '__main__': + sys.exit(main(sys.argv)) + diff --git a/utils/gen-shader-headers.sh b/utils/gen-shader-headers.sh deleted file mode 100755 index 0d6649521a7a..000000000000 --- a/utils/gen-shader-headers.sh +++ /dev/null @@ -1,54 +0,0 @@ -#!/bin/sh -# SPDX-License-Identifier: GPL-2.0-or-later - -set -e - -usage() { - echo "Usage: $0 [shader_file2 ...]" - echo - echo "Generates a C header file containing hex-encoded shader data." - echo - echo "Arguments:" - echo " src_dir Path to the base of the source directory" - echo " output_header Path to the generated header file" - echo " shader_file(s) One or more shader files to embed in the header" - exit 1 -} - -if [ $# -lt 4 ]; then - echo "Error: Invalid argument count." - usage -fi - -src_dir="$1"; shift -build_path="$1"; shift - -cat < "$build_path" -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* This file is auto-generated, do not edit! */ - -#pragma once - -EOF - -cat <> "$build_path" -/* - * List the names of the shaders at the top of - * header for readability's sake - * -EOF - -for file in "$@"; do - name=$(basename "$file" | tr '.' '_') - echo "[SHADER-GEN] $name" - echo " * unsigned char $name;" >> "$build_path" -done - -echo "*/" >> "$build_path" - -echo "/* Hex encoded shader data */" >> "$build_path" -for file in "$@"; do - name=$(basename "$file") - "$src_dir/utils/gen-shader-header.py" "$name" "$file" >> "$build_path" - echo >> "$build_path" -done diff --git a/utils/meson.build b/utils/meson.build index 9c598793035c..17a7aa7c3f5e 100644 --- a/utils/meson.build +++ b/utils/meson.build @@ -3,7 +3,7 @@ subdir('codegen') subdir('ipu3') -gen_shader_headers = files('gen-shader-headers.sh') +gen_shader_headers = files('gen-shader-headers.py') ## Module signing gen_ipa_priv_key = files('gen-ipa-priv-key.sh')