From 837e33395ed7fe02a162217d79eb1cfbb286347b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 30 Jun 2026 22:33:29 +0530 Subject: [PATCH] Output uniform variable names mapping for GLSL shaders --- kitty/shaders/slang.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/kitty/shaders/slang.py b/kitty/shaders/slang.py index 2dba04b17..f1d703c04 100644 --- a/kitty/shaders/slang.py +++ b/kitty/shaders/slang.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # License: GPLv3 Copyright: 2026, Kovid Goyal +import json import os import re import runpy @@ -274,7 +275,7 @@ def commands_to_compile_to_spirv(sources: dict[str, SlangFile], build_dir: str, dest = f'{base_dest}.{x.name}.spv' if x.name else f'{base_dest}.spv' if x.name: cmd.insert(-1, f'{base_dest}{x.filename_insert}.slang-module') - cmd += base_cmd + ['-o', dest] + cmd += base_cmd + ['-o', dest, '-reflection-json', dest + '.json'] output_mtime = safe_mtime(dest) module_mtime = os.path.getmtime(slang_module) needs_build = output_mtime < module_mtime @@ -283,7 +284,6 @@ def commands_to_compile_to_spirv(sources: dict[str, SlangFile], build_dir: str, yield Command(needs_build, f'Linking |{os.path.basename(dest)}| ...', cmd) - def commands_to_compile_to_glsl(sources: dict[str, SlangFile], build_dir: str, dest_dir: str, built_glsl_files: list[str]) -> Iterator[Command]: for base_dest, slang_module, cmd, sfile in iter_entry_point_shaders(sources, build_dir, dest_dir): module_mtime = os.path.getmtime(slang_module) @@ -303,7 +303,7 @@ def commands_to_compile_to_glsl(sources: dict[str, SlangFile], build_dir: str, d yield Command(needs_build, f'Linking |{os.path.basename(slang_module)}| to GLSL {ep.stage.value} shader ...', c) -def fixup_opengl_code(glsl_code: str) -> str: +def fixup_opengl_code(glsl_code: str) -> tuple[str, dict[str, str]]: lines = [] in_uniform_block = False in_uniform_block_contents = False @@ -322,7 +322,10 @@ def fixup_opengl_code(glsl_code: str) -> str: line = line.strip() name = line.split()[-1].rstrip(';') current_uniform_names.append(name) - uniform_names[name] = name.rpartition('_')[0] + uniform_name = name.rpartition('_')[0] + if uniform_name in uniform_names: + raise KeyError(f'The uniform name {uniform_name} is used with multiple suffixes') + uniform_names[uniform_name] = name line = 'uniform ' + line elif line.startswith('{'): # }} line = '// ' + line @@ -354,7 +357,7 @@ def fixup_opengl_code(glsl_code: str) -> str: ans = ans.replace(f'{block_name}.{u}', u) ans = ans.replace('gl_VertexIndex', 'gl_VertexID') ans = ans.replace('gl_BaseVertex', '0') - return ans + return ans, uniform_names def fixup_opengl_files(*paths: str) -> None: @@ -364,7 +367,10 @@ def fixup_opengl_files(*paths: str) -> None: glsl_code = f.read() f.seek(0) f.truncate() - f.write(fixup_opengl_code(glsl_code)) + fixed, uniform_names = fixup_opengl_code(glsl_code) + f.write(fixed) + with open(path + '.json', 'w') as f: + f.write(json.dumps(uniform_names)) ParallelRun = Callable[[Iterable[tuple[bool, str, list[str]]]], None]