Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-gpu

Source of release 0.5.1

0.5.1 / tools / embed_shaders.py

#!/usr/bin/env python3
"""Embed GLSL shaders for both GPU backends into a Luce Base module.

Each fragment shader becomes SPIR-V words (Vulkan) and Metal Shading Language
source cross-compiled from them, with its entry point named `luce_fragment`;
a vertex shader becomes SPIR-V words only. Builds use the checked-in module,
so glslangValidator and spirv-cross are needed only when a shader changes.

    embed_shaders.py OUTPUT.lucb [--vertex FILE.vert] [-I DIR] FILE.frag...

`#include "name.glsl"` resolves beside the shader, then in each `-I` directory,
so packages can share one include (luce-canvas's srgb.glsl) without copies.
"""
import argparse
from pathlib import Path
import shutil
import struct
import subprocess
import sys
import tempfile

parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('output', type=Path)
parser.add_argument('fragments', type=Path, nargs='*')
parser.add_argument('--vertex', type=Path, action='append', default=[])
parser.add_argument('-I', '--include', type=Path, action='append', default=[], help='a directory `#include` searches')
parser.add_argument('--glslang', default='glslangValidator')
parser.add_argument('--spirv-cross', default='spirv-cross')
parser.add_argument('--public', action='store_true', help='declare the embedded shaders `pub`')
parser.add_argument('--shared', action='store_true', help='the output joins a module that already imports c')
parser.add_argument('--vertex-metal', action='store_true', help='also translate vertex stages to MSL for retained geometry')
args = parser.parse_args()
visibility = 'pub ' if args.public else ''
lines = ['#' + '=' * 94, '#', f'#   {args.output.stem} - Embedded GPU shaders', '#', '#   DESCRIPTION:',
         '#       Generated by luce-gpu/tools/embed_shaders.py from ' + ', '.join(p.name for p in [*args.vertex, *args.fragments]) + '; do not edit.',
         '#', '#' + '=' * 94, ''] + ([] if args.shared else ['import c', ''])


def words_of(path, stage, temporary):
    output = Path(temporary) / (path.stem + '.spv')
    includes = [f'-I{directory}' for directory in args.include]
    subprocess.run([args.glslang, '-V', '--target-env', 'vulkan1.0', *includes, '-S', stage, str(path), '-o', str(output)], check=True)
    data = output.read_bytes()
    words = struct.unpack(f'<{len(data) // 4}I', data)
    assert words[0] == 0x07230203
    return output, words


def check_msl(path, msl, temporary):
    """Compile the Metal source where Metal is (macOS): spirv-cross passes GLSL
    names through that Metal reserves (a helper called `level`), and the device
    would only refuse the shader at run time."""
    if sys.platform != 'darwin' or shutil.which('xcrun') is None:
        return
    source = Path(temporary) / (path.stem + '.metal')
    source.write_text(msl, encoding='utf-8')
    compiled = subprocess.run(['xcrun', '-sdk', 'macosx', 'metal', '-c', str(source), '-o', str(source.with_suffix('.air'))],
                              capture_output=True, text=True)
    if compiled.returncode != 0:
        sys.exit(f'{path.name}: the Metal translation does not compile:\n{compiled.stderr}')


def emit_mark(title):
    head = f'# mark: {title} '
    lines.append(head + '=' * (95 - len(head)))
    lines.append('')


def emit_words(name, words):
    global lines
    lines.append(f'{visibility}let {name}: u32[{len(words)}] = [')
    for offset in range(0, len(words), 8):
        lines.append('    ' + ', '.join(f'0x{word:08x}' for word in words[offset:offset + 8]) + ',')
    lines = lines + [']', '']


with tempfile.TemporaryDirectory(prefix='luce-shader-') as temporary:
    for path in args.vertex:
        emit_mark(f'{path.stem}: vertex stage')
        spv, words = words_of(path, 'vert', temporary)
        emit_words(path.stem + '_vert_words', words)
        if args.vertex_metal:
            msl = subprocess.run([args.spirv_cross, '--msl', '--msl-version', '20100', '--msl-decoration-binding', '--rename-entry-point', 'main', 'luce_geometry', 'vert', str(spv)], check=True, capture_output=True, text=True).stdout
            check_msl(path, msl, temporary)
            escaped = msl.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
            lines.append(f'{visibility}let {path.stem}_vert_msl: c.str = "{escaped}"')
    for path in args.fragments:
        emit_mark(f'{path.stem}: fragment stage')
        spv, words = words_of(path, 'frag', temporary)
        emit_words(path.stem + '_frag_words', words)
        msl = subprocess.run([args.spirv_cross, '--msl', '--msl-version', '20100', '--msl-decoration-binding',
                              '--rename-entry-point', 'main', 'luce_fragment', 'frag', str(spv)],
                             check=True, capture_output=True, text=True).stdout
        check_msl(path, msl, temporary)
        escaped = msl.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
        lines.append(f'{visibility}let {path.stem}_frag_msl: c.str = "{escaped}"')
        lines.append('')
args.output.write_text('\n'.join(lines).rstrip('\n') + '\n', encoding='utf-8')
print(f'wrote {args.output}')