#!/bin/bash
# Compile GLSL compute shaders to SPIR-V
# Requires glslc (from Vulkan SDK) or glslangValidator

SHADER_DIR="$(dirname "$0")"
OUTPUT_DIR="${1:-$SHADER_DIR}"

# Find compiler
if command -v glslc &> /dev/null; then
    COMPILER="glslc"
    compile() { $COMPILER -o "$2" "$1"; }
elif command -v glslangValidator &> /dev/null; then
    COMPILER="glslangValidator"
    compile() { $COMPILER -V -o "$2" "$1"; }
else
    echo "ERROR: No GLSL compiler found!"
    echo "Install Vulkan SDK: https://vulkan.lunarg.com/sdk/home"
    echo "  Ubuntu/Debian: sudo apt install vulkan-tools glslang-tools"
    echo "  Arch:          sudo pacman -S vulkan-tools glslang"
    echo "  Fedora:        sudo dnf install vulkan-tools glslang"
    exit 1
fi

echo "Using compiler: $COMPILER"
echo "Output directory: $OUTPUT_DIR"
mkdir -p "$OUTPUT_DIR"

for shader in "$SHADER_DIR"/*.comp; do
    if [ -f "$shader" ]; then
        name=$(basename "$shader")
        output="$OUTPUT_DIR/${name}.spv"
        echo "  Compiling $name -> ${name}.spv"
        compile "$shader" "$output"
        if [ $? -ne 0 ]; then
            echo "  FAILED: $name"
            exit 1
        fi
    fi
done

echo "All shaders compiled successfully!"
