#!/usr/bin/env bash

# Ensure directories exist
mkdir -p spv
mkdir -p msl
mkdir -p dxil
mkdir -p reflection

# Vertex shaders
for filename in *.vert.hlsl; do
  if [ -f "$filename" ]; then
    outSpv="spv/${filename/.hlsl/.spv}"
    outDxil="dxil/${filename/.hlsl/.dxil}"
    outMsl="msl/${filename/.hlsl/.msl}"
    outReflect="reflection/${filename/.hlsl/.json}"
      # Produce SPIR-V
      dxc -spirv -T vs_6_0 -Fo "$outSpv" "$filename"
      # Produce DXIL with embedded debug info (example)
      shadercross "$filename" -o "$outDxil"
#      dxc -Zi -Qembed_debug -T vs_6_0 -Fo "$outDxil" "$filename"
      # Convert SPIR-V to Metal Shader Language
      spirv-cross "$outSpv" --msl > "$outMsl"
      # Generate reflection
      spirv-cross "$outSpv" --reflect > "$outReflect"
  fi
done

# Fragment shaders
for filename in *.frag.hlsl; do
  if [ -f "$filename" ]; then
    outSpv="spv/${filename/.hlsl/.spv}"
    outDxil="dxil/${filename/.hlsl/.dxil}"
    outMsl="msl/${filename/.hlsl/.msl}"
    outReflect="reflection/${filename/.hlsl/.json}"
      dxc -spirv -T ps_6_0 -Fo "$outSpv" "$filename"
      shadercross "$filename" -o "$outDxil"
#      dxc -Zi -Qembed_debug -T ps_6_0 -Fo "$outDxil" "$filename"
      spirv-cross "$outSpv" --msl > "$outMsl"
      spirv-cross "$outSpv" --reflect > "$outReflect"
  fi
done

# Compute shaders
for filename in *.comp.hlsl; do
  if [ -f "$filename" ]; then
    outSpv="spv/${filename/.hlsl/.spv}"
    outDxil="dxil/${filename/.hlsl/.dxil}"
    outMsl="msl/${filename/.hlsl/.msl}"
    outReflect="reflection/${filename/.hlsl/.json}"
      dxc -spirv -T cs_6_0 -Fo "$outSpv" "$filename"
      dxc -Zi -Qembed_debug -T cs_6_0 -Fo "$outDxil" "$filename"
      spirv-cross "$outSpv" --msl > "$outMsl"
      spirv-cross "$outSpv" --reflect > "$outReflect"
  fi
done
