DS2Revival/mods/widescreen/radar/build_shaders.py
DS2Revival d7107c0866
All checks were successful
build / build (push) Successful in 1m14s
CI: include the radar shaders
2026-10-27 22:04:47 -04:00

48 lines
2.1 KiB
Python

"""Compile radar_vs.hlsl with fxc (Windows SDK) into radar_shaders.h (vs_2_0 bytecode arrays, four vertex-format variants).
usage: python -I mods/widescreen/radar/build_shaders.py [path\\to\\fxc.exe]
The generated header is committed, so normal builds (MSVC or the clang CI) do not need fxc; rerun this only
after changing the .hlsl.
"""
import glob, os, subprocess, sys, tempfile
HERE = os.path.dirname(os.path.abspath(__file__))
# variant index = (HAS_DIFFUSE ? 1 : 0) | (HAS_TEX ? 2 : 0)
VARIANTS = [[], ['HAS_DIFFUSE'], ['HAS_TEX'], ['HAS_DIFFUSE', 'HAS_TEX']]
def find_fxc():
if len(sys.argv) > 1:
return sys.argv[1]
hits = sorted(glob.glob(r'C:\Program Files (x86)\Windows Kits\10\bin\10.*\x64\fxc.exe'))
if not hits:
sys.exit('fxc.exe not found; pass its path')
return hits[-1]
def main():
fxc = find_fxc()
out = ['// Generated by build_shaders.py from radar_vs.hlsl (our own code). Do not edit.',
'#pragma once', '']
with tempfile.TemporaryDirectory() as tmp:
for idx, defs in enumerate(VARIANTS):
obj = os.path.join(tmp, 'v%d.cso' % idx)
cmd = [fxc, '/nologo', '/T', 'vs_2_0', '/E', 'RadarVS', '/O3', '/Fo', obj]
for d in defs:
cmd += ['/D', d]
r = subprocess.run(cmd + [os.path.join(HERE, 'radar_vs.hlsl')], capture_output=True, text=True, cwd=HERE)
if r.returncode:
sys.exit('variant %d failed:\n%s%s' % (idx, r.stdout, r.stderr))
data = open(obj, 'rb').read()
words = [int.from_bytes(data[i:i + 4], 'little') for i in range(0, len(data), 4)]
out.append('static const unsigned long g_radarVS%d[] = {' % idx)
for i in range(0, len(words), 8):
out.append(' ' + ', '.join('0x%08x' % w for w in words[i:i + 8]) + ',')
out.append('};')
print('variant %d (%s) %d bytes' % (idx, '+'.join(defs) or 'position only', len(data)))
open(os.path.join(HERE, 'radar_shaders.h'), 'w', newline='\n').write('\n'.join(out) + '\n')
print('wrote radar_shaders.h')
if __name__ == '__main__':
main()