X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=scripts%2Fbuiltin_funcs.py;fp=scripts%2Fbuiltin_funcs.py;h=1c5b832095409c11ecac19dca24c1fb92fa0273b;hb=cae521f02bda5988ed2f1a9ae5c09734f6fa2352;hp=0000000000000000000000000000000000000000;hpb=7d66c70e15b84cbaf6b1973db07629f5bd3e5cdf;p=libs%2Fgl.git diff --git a/scripts/builtin_funcs.py b/scripts/builtin_funcs.py new file mode 100755 index 00000000..1c5b8320 --- /dev/null +++ b/scripts/builtin_funcs.py @@ -0,0 +1,206 @@ +#!/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+11: + 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