geojs/scripts/preprocess_glsl.py
David Manthey a64268b721 Change the point shaders so that common code is in shared files.
The shader-loader allows importing chunks into other files.  This allows
common code to be placed in a shared file and imported by other files.
There are some restrictions: the common chunks must have filenames
ending in `glsl`, and the glslangValidator doesn't handle these
inclusions on its own.  However, sharing code is of substantial benefit
for reducing maintenance issues, so it is worth this change.

Note that there are other webpack shader loaders (e.g.,
webpack-glsl-loader) that provide similar include functionality but with
a different syntax, as glsl doesn't have its own include statements.  As
such, if we ever switch the shader loader to another one, the import
statements would have to change.
2019-10-29 10:10:46 -04:00

26 lines
760 B
Python
Executable File

#!/usr/bin/env python3
import argparse
import os
import re
import sys
def readSource(source):
data = open(source).read()
parts = re.split('(\\$[-.\\w]+)', data)
for idx, chunk in enumerate(parts):
if chunk.startswith('$') and len(chunk) > 1:
parts[idx] = readSource(os.path.join(os.path.dirname(source), chunk[1:] + '.glsl'))
return ''.join(parts)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Preprocess glsl files to handle includes in the same way '
'as shader-loader. The output of this can sent to glslangValidator.')
parser.add_argument('source', help='Source file')
args = parser.parse_args()
data = readSource(args.source)
sys.stdout.write(data)