--- /dev/null
+#!/usr/bin/python3
+
+import sys
+
+traits = {
+ "sampler1D": { "CDim": 1, "IDim": 1 },
+ "sampler2D": { "CDim": 2, "IDim": 2 },
+ "sampler3D": { "CDim": 3, "IDim": 3 },
+ "sampler1DArray": { "CDim": 2, "IDim": 2 },
+ "sampler2DArray": { "CDim": 3, "IDim": 3 },
+ "samplerCube": { "CDim": 3, "IDim": 2 },
+ "samplerCubeArray": { "CDim": 4, "IDim": 3 },
+ "sampler1DShadow": { "CDim": 3, "IDim": 1 },
+ "sampler2DShadow": { "CDim": 3, "IDim": 2 },
+ "samplerCubeShadow": { "CDim": 4, "IDim": 2 },
+ "sampler1DArrayShadow": { "CDim": 3, "IDim": 2 },
+ "sampler2DArrayShadow": { "CDim": 4, "IDim": 3 },
+ "samplerCubeArrayShadow": { "IDim": 3 },
+ "float": { "Base": "float" },
+ "vec2": { "Base": "float" },
+ "vec3": { "Base": "float" },
+ "vec4": { "Base": "float" },
+ "int": { "Base": "int" },
+ "ivec2": { "Base": "int" },
+ "ivec3": { "Base": "int" },
+ "ivec4": { "Base": "int" }
+}
+
+float32types = ("float", "vec2", "vec3", "vec4")
+floattypes = float32types
+int32types = ("int", "ivec2", "ivec3", "ivec4")
+signedtypes = floattypes+int32types
+arithmetictypes = signedtypes
+squarematrixtypes = ("mat2", "mat3", "mat4")
+matrixtypes = squarematrixtypes+("mat2x3", "mat3x2", "mat2x4", "mat4x2", "mat3x4", "mat4x3")
+flatsamplertypes = ("sampler1D", "sampler2D", "sampler3D", "sampler1DArray", "sampler2DArray")
+colorsamplertypes = flatsamplertypes+("samplerCube", "samplerCubeArray")
+shadowsamplertypes = ("sampler1DShadow", "sampler2DShadow", "samplerCubeShadow", "sampler1DArrayShadow", "sampler2DArrayShadow", "samplerCubeArrayShadow")
+samplertypes = colorsamplertypes+shadowsamplertypes
+funcs = [
+ # Trigonometric
+ ("T sin(T angle)", float32types),
+ ("T cos(T angle)", float32types),
+ ("T tan(T angle)", float32types),
+ ("T asin(T x)", float32types),
+ ("T acos(T x)", float32types),
+ ("T atan(T y, T x)", float32types),
+ ("T sinh(T angle)", float32types),
+ ("T cosh(T angle)", float32types),
+ ("T tanh(T angle)", float32types),
+ ("T asinh(T x)", float32types),
+ ("T acosh(T x)", float32types),
+ ("T atanh(T x)", float32types),
+
+ # Exponential
+ ("T pow(T x, T y)", float32types),
+ ("T exp(T x)", float32types),
+ ("T log(T x)", float32types),
+ ("T exp2(T x)", float32types),
+ ("T log2(T x)", float32types),
+ ("T sqrt(T x)", floattypes),
+ ("T inversesqrt(T x)", floattypes),
+
+ # Common
+ ("T abs(T x)", signedtypes),
+ ("T sign(T x)", signedtypes),
+ ("T floor(T x)", floattypes),
+ ("T trunc(T x)", floattypes),
+ ("T round(T x)", floattypes),
+ ("T roundEven(T x)", floattypes),
+ ("T ceil(T x)", floattypes),
+ ("T fract(T x)", floattypes),
+ ("T mod(T x, T y)", floattypes),
+ ("T mod(T x, T::Base y)", floattypes),
+ ("T modf(T x, out T y)", floattypes),
+ ("T min(T x, T y)", arithmetictypes),
+ ("T min(T x, T::Base y)", arithmetictypes),
+ ("T max(T x, T y)", arithmetictypes),
+ ("T max(T x, T::Base y)", arithmetictypes),
+ ("T clamp(T x, T minVal, T maxVal)", arithmetictypes),
+ ("T clamp(T x, T::Base minVal, T::Base maxVal)", arithmetictypes),
+ ("T mix(T x, T y, T a)", floattypes),
+ ("T mix(T x, T y, T::Base a)", floattypes),
+ ("T step(T edge, T x)", floattypes),
+ ("T step(T::Base edge, T x)", floattypes),
+ ("T smoothstep(T edge0, T edge1, T x)", floattypes),
+ ("T smoothstep(T::Base edge0, T::Base edge1, T x)", floattypes),
+ ("T fma(T a, T b, T c)", floattypes),
+
+ # Geometric
+ ("T::Base length(T x)", floattypes),
+ ("T::Base distance(T p0, T p1)", floattypes),
+ ("T::Base dot(T x, T y)", floattypes),
+ "vec3 cross(vec3 x)",
+ ("T normalize(T x)", floattypes),
+ ("T faceforward(T N, T I, T Nref)", floattypes),
+ ("T reflect(T N, T I)", floattypes),
+ ("T refract(T N, T I, float eta)", floattypes),
+
+ # Matrix
+ ("T matrixCompMult(T x, T y)", matrixtypes),
+ ("T transpose(T m)", squarematrixtypes),
+ ("T determinant(T m)", squarematrixtypes),
+ ("T inverse(T m)", squarematrixtypes),
+
+ # Texture
+ ("int[T::IDim] textureSize(T sampler, int lod)", samplertypes),
+ ("vec4 texture(T sampler, float[T::CDim] P)", colorsamplertypes),
+ ("float texture(T sampler, float[T::CDim] P)", tuple(s for s in shadowsamplertypes if "CubeArray" not in s)),
+ "float texture(samplerCubeArrayShadow sampler, vec4 P, float compare)",
+ ("vec4 textureLod(T sampler, float[T::CDim] P)", colorsamplertypes),
+ ("vec4 texelFetch(T sampler, int[T::CDim] P, int lod)", flatsamplertypes)
+]
+
+def tokenize(code):
+ out_tokens = []
+ token = ""
+ for i, c in enumerate(code):
+ if c.isspace():
+ continue
+
+ token += c
+ n = code[i+1] if i+1<len(code) else " "
+
+ end = False
+ if token[0].isalpha():
+ if not n.isalnum():
+ end = True
+ else:
+ if n.isalnum() or n.isspace():
+ end = True
+
+ if end:
+ out_tokens.append(token)
+ token = ""
+
+ return out_tokens
+
+def expand_tokens(tokens, i, gentype):
+ t = tokens[i]
+ if t=="T":
+ t = gentype
+ if i+1<len(tokens):
+ if tokens[i+1]=="::":
+ return (traits[t][tokens[i+2]], 3)
+ elif tokens[i+1]=="[":
+ sub, advance = expand_tokens(tokens, i+2, gentype)
+ if sub>1:
+ t = "ivec" if t=="int" else "vec"
+ t += str(sub)
+ return (t, 3+advance)
+ return (t, 1)
+
+def expand_template(template, gentype):
+ result = ""
+ special = True
+ tokens = tokenize(template)
+ i = 0
+ while i<len(tokens):
+ t, advance = expand_tokens(tokens, i, gentype)
+
+ if not special and t[0].isalpha():
+ result += " "
+ special = not t[0].isalpha()
+
+ if t==",":
+ result += ", "
+ else:
+ result += t
+
+ i += advance
+
+ return result
+
+def generate_functions():
+ out_lines = []
+ generated = set()
+ for f in funcs:
+ if type(f)==tuple:
+ for t in f[1]:
+ decl = expand_template(f[0], t)
+ if not decl in generated:
+ out_lines.append(decl+";\n")
+ generated.add(decl)
+ else:
+ out_lines.append(f+";\n")
+
+ return out_lines
+
+def generate_file(fn):
+ out_lines = []
+ skip = False
+ for line in open(fn):
+ if not skip:
+ out_lines.append(line)
+ if "BEGIN BUILTIN FUNCTIONS" in line:
+ skip = True
+ out_lines += generate_functions()
+ elif "END BUILTIN FUNCTIONS" in line:
+ out_lines.append(line)
+ skip = False
+
+ open(fn, "w").writelines(out_lines)
+
+if __name__=="__main__":
+ generate_file(sys.argv[1])