65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
|
import os
|
||
|
import glob
|
||
|
import shutil
|
||
|
from pathlib import Path
|
||
|
|
||
|
# Simple Script to generate/update Shaders
|
||
|
# WHY? Cause having this stupid .v.pica files in
|
||
|
# sourcecode is pain with some buildsystems
|
||
|
|
||
|
def file2array(path, custom_incluse_path):
|
||
|
print(path)
|
||
|
cip = len(custom_incluse_path)
|
||
|
sip = ''
|
||
|
if cip > 0:
|
||
|
sip = custom_incluse_path
|
||
|
name = Path(path).stem
|
||
|
filei = open(path, 'rb')
|
||
|
buf = filei.read()
|
||
|
filei.close()
|
||
|
fs = open(name + '.cpp', 'w')
|
||
|
fs.write("// THIS FILE WAS GENERATED BY build_shaders.py!!!\n\n")
|
||
|
fs.write('#include <'+ sip + name + '.hpp>\n\n')
|
||
|
fs.write('// clang-format off\n')
|
||
|
fs.write('unsigned char ' + name + '[] = {\n')
|
||
|
for byte in buf:
|
||
|
fs.write(hex(byte) + ', ')
|
||
|
fs.write('\n};\n')
|
||
|
fs.write('// clang-format on\n')
|
||
|
fs.write('size_t ' + name + '_size = ' + hex(len(buf)) + ';')
|
||
|
fs.close()
|
||
|
fh = open(name + '.hpp', 'w')
|
||
|
fh.write("// THIS FILE WAS GENERATED BY build_shaders.py!!!\n\n")
|
||
|
fh.write('#pragma once\n\n')
|
||
|
fh.write('#include <cstddef>\n\n')
|
||
|
fh.write('extern unsigned char ' + name + '[];\n')
|
||
|
fh.write('extern size_t ' + name + '_size;')
|
||
|
fh.close()
|
||
|
|
||
|
def build_shader(path):
|
||
|
p = os.path.dirname(path)
|
||
|
n = Path(Path(path).stem).stem
|
||
|
os.system('picasso -o ' + p + '/' + n + '.shbin ' + path)
|
||
|
|
||
|
def cleanup():
|
||
|
t3x = glob.glob('shaders/*.shbin')
|
||
|
for f in t3x:
|
||
|
os.remove(f)
|
||
|
|
||
|
def install_code(what, where):
|
||
|
if Path(what).is_dir:
|
||
|
os.error('Must be a file!!!')
|
||
|
shutil.move(what, where + Path(what).name)
|
||
|
|
||
|
print("Generating...")
|
||
|
shaders = glob.glob('shaders/*v.pica')
|
||
|
for object in shaders:
|
||
|
name = Path(Path(object).stem).stem
|
||
|
bp = os.path.dirname(object)
|
||
|
build_shader(object)
|
||
|
file2array(bp + '/' + name + '.shbin', 'pd/')
|
||
|
install_code(name + '.cpp', 'source/')
|
||
|
install_code(name + '.hpp', 'include/pd/')
|
||
|
|
||
|
cleanup()
|
||
|
print("Done")
|