From cae521f02bda5988ed2f1a9ae5c09734f6fa2352 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 9 Mar 2021 19:16:42 +0200 Subject: [PATCH] Add declarations for most of the builtin functions in GLSL --- builtin_data/_builtin.glsl | 285 +++++++++++++++++++++++++++++++++++++ scripts/builtin_funcs.py | 206 +++++++++++++++++++++++++++ 2 files changed, 491 insertions(+) create mode 100755 scripts/builtin_funcs.py diff --git a/builtin_data/_builtin.glsl b/builtin_data/_builtin.glsl index df39bcc0..cfd67272 100644 --- a/builtin_data/_builtin.glsl +++ b/builtin_data/_builtin.glsl @@ -32,6 +32,288 @@ typedef image(dimensions=2[], shadow, sampled) float sampler2DArrayShadow; typedef image(dimensions=cube, shadow, sampled) float samplerCubeShadow; typedef image(dimensions=cube[], shadow, sampled) float samplerCubeArrayShadow; +// BEGIN BUILTIN FUNCTIONS +float sin(float angle); +vec2 sin(vec2 angle); +vec3 sin(vec3 angle); +vec4 sin(vec4 angle); +float cos(float angle); +vec2 cos(vec2 angle); +vec3 cos(vec3 angle); +vec4 cos(vec4 angle); +float tan(float angle); +vec2 tan(vec2 angle); +vec3 tan(vec3 angle); +vec4 tan(vec4 angle); +float asin(float x); +vec2 asin(vec2 x); +vec3 asin(vec3 x); +vec4 asin(vec4 x); +float acos(float x); +vec2 acos(vec2 x); +vec3 acos(vec3 x); +vec4 acos(vec4 x); +float atan(float y, float x); +vec2 atan(vec2 y, vec2 x); +vec3 atan(vec3 y, vec3 x); +vec4 atan(vec4 y, vec4 x); +float sinh(float angle); +vec2 sinh(vec2 angle); +vec3 sinh(vec3 angle); +vec4 sinh(vec4 angle); +float cosh(float angle); +vec2 cosh(vec2 angle); +vec3 cosh(vec3 angle); +vec4 cosh(vec4 angle); +float tanh(float angle); +vec2 tanh(vec2 angle); +vec3 tanh(vec3 angle); +vec4 tanh(vec4 angle); +float asinh(float x); +vec2 asinh(vec2 x); +vec3 asinh(vec3 x); +vec4 asinh(vec4 x); +float acosh(float x); +vec2 acosh(vec2 x); +vec3 acosh(vec3 x); +vec4 acosh(vec4 x); +float atanh(float x); +vec2 atanh(vec2 x); +vec3 atanh(vec3 x); +vec4 atanh(vec4 x); +float pow(float x, float y); +vec2 pow(vec2 x, vec2 y); +vec3 pow(vec3 x, vec3 y); +vec4 pow(vec4 x, vec4 y); +float exp(float x); +vec2 exp(vec2 x); +vec3 exp(vec3 x); +vec4 exp(vec4 x); +float log(float x); +vec2 log(vec2 x); +vec3 log(vec3 x); +vec4 log(vec4 x); +float exp2(float x); +vec2 exp2(vec2 x); +vec3 exp2(vec3 x); +vec4 exp2(vec4 x); +float log2(float x); +vec2 log2(vec2 x); +vec3 log2(vec3 x); +vec4 log2(vec4 x); +float sqrt(float x); +vec2 sqrt(vec2 x); +vec3 sqrt(vec3 x); +vec4 sqrt(vec4 x); +float inversesqrt(float x); +vec2 inversesqrt(vec2 x); +vec3 inversesqrt(vec3 x); +vec4 inversesqrt(vec4 x); +float abs(float x); +vec2 abs(vec2 x); +vec3 abs(vec3 x); +vec4 abs(vec4 x); +int abs(int x); +ivec2 abs(ivec2 x); +ivec3 abs(ivec3 x); +ivec4 abs(ivec4 x); +float sign(float x); +vec2 sign(vec2 x); +vec3 sign(vec3 x); +vec4 sign(vec4 x); +int sign(int x); +ivec2 sign(ivec2 x); +ivec3 sign(ivec3 x); +ivec4 sign(ivec4 x); +float floor(float x); +vec2 floor(vec2 x); +vec3 floor(vec3 x); +vec4 floor(vec4 x); +float trunc(float x); +vec2 trunc(vec2 x); +vec3 trunc(vec3 x); +vec4 trunc(vec4 x); +float round(float x); +vec2 round(vec2 x); +vec3 round(vec3 x); +vec4 round(vec4 x); +float roundEven(float x); +vec2 roundEven(vec2 x); +vec3 roundEven(vec3 x); +vec4 roundEven(vec4 x); +float ceil(float x); +vec2 ceil(vec2 x); +vec3 ceil(vec3 x); +vec4 ceil(vec4 x); +float fract(float x); +vec2 fract(vec2 x); +vec3 fract(vec3 x); +vec4 fract(vec4 x); +float mod(float x, float y); +vec2 mod(vec2 x, vec2 y); +vec3 mod(vec3 x, vec3 y); +vec4 mod(vec4 x, vec4 y); +vec2 mod(vec2 x, float y); +vec3 mod(vec3 x, float y); +vec4 mod(vec4 x, float y); +float modf(float x, out float y); +vec2 modf(vec2 x, out vec2 y); +vec3 modf(vec3 x, out vec3 y); +vec4 modf(vec4 x, out vec4 y); +float min(float x, float y); +vec2 min(vec2 x, vec2 y); +vec3 min(vec3 x, vec3 y); +vec4 min(vec4 x, vec4 y); +int min(int x, int y); +ivec2 min(ivec2 x, ivec2 y); +ivec3 min(ivec3 x, ivec3 y); +ivec4 min(ivec4 x, ivec4 y); +vec2 min(vec2 x, float y); +vec3 min(vec3 x, float y); +vec4 min(vec4 x, float y); +ivec2 min(ivec2 x, int y); +ivec3 min(ivec3 x, int y); +ivec4 min(ivec4 x, int y); +float max(float x, float y); +vec2 max(vec2 x, vec2 y); +vec3 max(vec3 x, vec3 y); +vec4 max(vec4 x, vec4 y); +int max(int x, int y); +ivec2 max(ivec2 x, ivec2 y); +ivec3 max(ivec3 x, ivec3 y); +ivec4 max(ivec4 x, ivec4 y); +vec2 max(vec2 x, float y); +vec3 max(vec3 x, float y); +vec4 max(vec4 x, float y); +ivec2 max(ivec2 x, int y); +ivec3 max(ivec3 x, int y); +ivec4 max(ivec4 x, int y); +float clamp(float x, float minVal, float maxVal); +vec2 clamp(vec2 x, vec2 minVal, vec2 maxVal); +vec3 clamp(vec3 x, vec3 minVal, vec3 maxVal); +vec4 clamp(vec4 x, vec4 minVal, vec4 maxVal); +int clamp(int x, int minVal, int maxVal); +ivec2 clamp(ivec2 x, ivec2 minVal, ivec2 maxVal); +ivec3 clamp(ivec3 x, ivec3 minVal, ivec3 maxVal); +ivec4 clamp(ivec4 x, ivec4 minVal, ivec4 maxVal); +vec2 clamp(vec2 x, float minVal, float maxVal); +vec3 clamp(vec3 x, float minVal, float maxVal); +vec4 clamp(vec4 x, float minVal, float maxVal); +ivec2 clamp(ivec2 x, int minVal, int maxVal); +ivec3 clamp(ivec3 x, int minVal, int maxVal); +ivec4 clamp(ivec4 x, int minVal, int maxVal); +float mix(float x, float y, float a); +vec2 mix(vec2 x, vec2 y, vec2 a); +vec3 mix(vec3 x, vec3 y, vec3 a); +vec4 mix(vec4 x, vec4 y, vec4 a); +vec2 mix(vec2 x, vec2 y, float a); +vec3 mix(vec3 x, vec3 y, float a); +vec4 mix(vec4 x, vec4 y, float a); +float step(float edge, float x); +vec2 step(vec2 edge, vec2 x); +vec3 step(vec3 edge, vec3 x); +vec4 step(vec4 edge, vec4 x); +vec2 step(float edge, vec2 x); +vec3 step(float edge, vec3 x); +vec4 step(float edge, vec4 x); +float smoothstep(float edge0, float edge1, float x); +vec2 smoothstep(vec2 edge0, vec2 edge1, vec2 x); +vec3 smoothstep(vec3 edge0, vec3 edge1, vec3 x); +vec4 smoothstep(vec4 edge0, vec4 edge1, vec4 x); +vec2 smoothstep(float edge0, float edge1, vec2 x); +vec3 smoothstep(float edge0, float edge1, vec3 x); +vec4 smoothstep(float edge0, float edge1, vec4 x); +float fma(float a, float b, float c); +vec2 fma(vec2 a, vec2 b, vec2 c); +vec3 fma(vec3 a, vec3 b, vec3 c); +vec4 fma(vec4 a, vec4 b, vec4 c); +float length(float x); +float length(vec2 x); +float length(vec3 x); +float length(vec4 x); +float distance(float p0, float p1); +float distance(vec2 p0, vec2 p1); +float distance(vec3 p0, vec3 p1); +float distance(vec4 p0, vec4 p1); +float dot(float x, float y); +float dot(vec2 x, vec2 y); +float dot(vec3 x, vec3 y); +float dot(vec4 x, vec4 y); +vec3 cross(vec3 x); +float normalize(float x); +vec2 normalize(vec2 x); +vec3 normalize(vec3 x); +vec4 normalize(vec4 x); +float faceforward(float N, float I, float Nref); +vec2 faceforward(vec2 N, vec2 I, vec2 Nref); +vec3 faceforward(vec3 N, vec3 I, vec3 Nref); +vec4 faceforward(vec4 N, vec4 I, vec4 Nref); +float reflect(float N, float I); +vec2 reflect(vec2 N, vec2 I); +vec3 reflect(vec3 N, vec3 I); +vec4 reflect(vec4 N, vec4 I); +float refract(float N, float I, float eta); +vec2 refract(vec2 N, vec2 I, float eta); +vec3 refract(vec3 N, vec3 I, float eta); +vec4 refract(vec4 N, vec4 I, float eta); +mat2 matrixCompMult(mat2 x, mat2 y); +mat3 matrixCompMult(mat3 x, mat3 y); +mat4 matrixCompMult(mat4 x, mat4 y); +mat2x3 matrixCompMult(mat2x3 x, mat2x3 y); +mat3x2 matrixCompMult(mat3x2 x, mat3x2 y); +mat2x4 matrixCompMult(mat2x4 x, mat2x4 y); +mat4x2 matrixCompMult(mat4x2 x, mat4x2 y); +mat3x4 matrixCompMult(mat3x4 x, mat3x4 y); +mat4x3 matrixCompMult(mat4x3 x, mat4x3 y); +mat2 transpose(mat2 m); +mat3 transpose(mat3 m); +mat4 transpose(mat4 m); +mat2 determinant(mat2 m); +mat3 determinant(mat3 m); +mat4 determinant(mat4 m); +mat2 inverse(mat2 m); +mat3 inverse(mat3 m); +mat4 inverse(mat4 m); +int textureSize(sampler1D sampler, int lod); +ivec2 textureSize(sampler2D sampler, int lod); +ivec3 textureSize(sampler3D sampler, int lod); +ivec2 textureSize(sampler1DArray sampler, int lod); +ivec3 textureSize(sampler2DArray sampler, int lod); +ivec2 textureSize(samplerCube sampler, int lod); +ivec3 textureSize(samplerCubeArray sampler, int lod); +int textureSize(sampler1DShadow sampler, int lod); +ivec2 textureSize(sampler2DShadow sampler, int lod); +ivec2 textureSize(samplerCubeShadow sampler, int lod); +ivec2 textureSize(sampler1DArrayShadow sampler, int lod); +ivec3 textureSize(sampler2DArrayShadow sampler, int lod); +ivec3 textureSize(samplerCubeArrayShadow sampler, int lod); +vec4 texture(sampler1D sampler, float P); +vec4 texture(sampler2D sampler, vec2 P); +vec4 texture(sampler3D sampler, vec3 P); +vec4 texture(sampler1DArray sampler, vec2 P); +vec4 texture(sampler2DArray sampler, vec3 P); +vec4 texture(samplerCube sampler, vec3 P); +vec4 texture(samplerCubeArray sampler, vec4 P); +float texture(sampler1DShadow sampler, vec3 P); +float texture(sampler2DShadow sampler, vec3 P); +float texture(samplerCubeShadow sampler, vec4 P); +float texture(sampler1DArrayShadow sampler, vec3 P); +float texture(sampler2DArrayShadow sampler, vec4 P); +float texture(samplerCubeArrayShadow sampler, vec4 P, float compare); +vec4 textureLod(sampler1D sampler, float P); +vec4 textureLod(sampler2D sampler, vec2 P); +vec4 textureLod(sampler3D sampler, vec3 P); +vec4 textureLod(sampler1DArray sampler, vec2 P); +vec4 textureLod(sampler2DArray sampler, vec3 P); +vec4 textureLod(samplerCube sampler, vec3 P); +vec4 textureLod(samplerCubeArray sampler, vec4 P); +vec4 texelFetch(sampler1D sampler, int P, int lod); +vec4 texelFetch(sampler2D sampler, ivec2 P, int lod); +vec4 texelFetch(sampler3D sampler, ivec3 P, int lod); +vec4 texelFetch(sampler1DArray sampler, ivec2 P, int lod); +vec4 texelFetch(sampler2DArray sampler, ivec3 P, int lod); +// END BUILTIN FUNCTIONS + #pragma MSP stage(vertex) out gl_PerVertex { @@ -50,3 +332,6 @@ out gl_PerVertex vec4 gl_Position; float gl_ClipDistance[]; }; + +void EmitVertex(); +void EndPrimitive(); 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