From 9e44b553e200a9377901bd8e4c9de2b1465ec86a Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 10 Mar 2021 15:35:42 +0200 Subject: [PATCH] Expand the selection of available builtin GLSL functions --- builtin_data/_builtin.glsl | 188 +++++++++++++++++++++++++++++++++++++ scripts/builtin_funcs.py | 111 ++++++++++++++++++---- 2 files changed, 281 insertions(+), 18 deletions(-) diff --git a/builtin_data/_builtin.glsl b/builtin_data/_builtin.glsl index cfd67272..b7737356 100644 --- a/builtin_data/_builtin.glsl +++ b/builtin_data/_builtin.glsl @@ -18,6 +18,10 @@ typedef vector(2) int ivec2; typedef vector(3) int ivec3; typedef vector(4) int ivec4; +typedef vector(2) bool bvec2; +typedef vector(3) bool bvec3; +typedef vector(4) bool bvec4; + typedef image(dimensions=1, sampled) float sampler1D; typedef image(dimensions=2, sampled) float sampler2D; typedef image(dimensions=3, sampled) float sampler3D; @@ -33,6 +37,14 @@ typedef image(dimensions=cube, shadow, sampled) float samplerCubeShadow; typedef image(dimensions=cube[], shadow, sampled) float samplerCubeArrayShadow; // BEGIN BUILTIN FUNCTIONS +float radians(float degrees); +vec2 radians(vec2 degrees); +vec3 radians(vec3 degrees); +vec4 radians(vec4 degrees); +float degrees(float radians); +vec2 degrees(vec2 radians); +vec3 degrees(vec3 radians); +vec4 degrees(vec4 radians); float sin(float angle); vec2 sin(vec2 angle); vec3 sin(vec3 angle); @@ -57,6 +69,10 @@ 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 atan(float y_over_x); +vec2 atan(vec2 y_over_x); +vec3 atan(vec3 y_over_x); +vec4 atan(vec4 y_over_x); float sinh(float angle); vec2 sinh(vec2 angle); vec3 sinh(vec3 angle); @@ -209,6 +225,14 @@ 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 mix(float x, float y, bool a); +vec2 mix(vec2 x, vec2 y, bvec2 a); +vec3 mix(vec3 x, vec3 y, bvec3 a); +vec4 mix(vec4 x, vec4 y, bvec4 a); +int mix(int x, int y, bool a); +ivec2 mix(ivec2 x, ivec2 y, bvec2 a); +ivec3 mix(ivec3 x, ivec3 y, bvec3 a); +ivec4 mix(ivec4 x, ivec4 y, bvec4 a); float step(float edge, float x); vec2 step(vec2 edge, vec2 x); vec3 step(vec3 edge, vec3 x); @@ -223,10 +247,34 @@ 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); +bool isnan(float x); +bvec2 isnan(vec2 x); +bvec3 isnan(vec3 x); +bvec4 isnan(vec4 x); +bool isinf(float x); +bvec2 isinf(vec2 x); +bvec3 isinf(vec3 x); +bvec4 isinf(vec4 x); +int floatBitsToInt(float value); +ivec2 floatBitsToInt(vec2 value); +ivec3 floatBitsToInt(vec3 value); +ivec4 floatBitsToInt(vec4 value); +float intBitsToFloat(int value); +vec2 intBitsToFloat(ivec2 value); +vec3 intBitsToFloat(ivec3 value); +vec4 intBitsToFloat(ivec4 value); 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 frexp(float x, out int exp); +vec2 frexp(vec2 x, out ivec2 exp); +vec3 frexp(vec3 x, out ivec3 exp); +vec4 frexp(vec4 x, out ivec4 exp); +float ldexp(float x, int exp); +vec2 ldexp(vec2 x, ivec2 exp); +vec3 ldexp(vec3 x, ivec3 exp); +vec4 ldexp(vec4 x, ivec4 exp); float length(float x); float length(vec2 x); float length(vec3 x); @@ -265,15 +313,103 @@ 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 outerProduct(vec2 c, vec2 r); +mat3 outerProduct(vec3 c, vec3 r); +mat4 outerProduct(vec4 c, vec4 r); +mat2x3 outerProduct(vec3 c, vec2 r); +mat3x2 outerProduct(vec2 c, vec3 r); +mat2x4 outerProduct(vec4 c, vec2 r); +mat4x2 outerProduct(vec2 c, vec4 r); +mat3x4 outerProduct(vec4 c, vec3 r); +mat4x3 outerProduct(vec3 c, vec4 r); mat2 transpose(mat2 m); mat3 transpose(mat3 m); mat4 transpose(mat4 m); +mat3x2 transpose(mat2x3 m); +mat2x3 transpose(mat3x2 m); +mat4x2 transpose(mat2x4 m); +mat2x4 transpose(mat4x2 m); +mat4x3 transpose(mat3x4 m); +mat3x4 transpose(mat4x3 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); +bvec2 lessThan(vec2 x, vec2 y); +bvec3 lessThan(vec3 x, vec3 y); +bvec4 lessThan(vec4 x, vec4 y); +bvec2 lessThan(ivec2 x, ivec2 y); +bvec3 lessThan(ivec3 x, ivec3 y); +bvec4 lessThan(ivec4 x, ivec4 y); +bvec2 lessThanEqual(vec2 x, vec2 y); +bvec3 lessThanEqual(vec3 x, vec3 y); +bvec4 lessThanEqual(vec4 x, vec4 y); +bvec2 lessThanEqual(ivec2 x, ivec2 y); +bvec3 lessThanEqual(ivec3 x, ivec3 y); +bvec4 lessThanEqual(ivec4 x, ivec4 y); +bvec2 greaterThan(vec2 x, vec2 y); +bvec3 greaterThan(vec3 x, vec3 y); +bvec4 greaterThan(vec4 x, vec4 y); +bvec2 greaterThan(ivec2 x, ivec2 y); +bvec3 greaterThan(ivec3 x, ivec3 y); +bvec4 greaterThan(ivec4 x, ivec4 y); +bvec2 greaterThanEqual(vec2 x, vec2 y); +bvec3 greaterThanEqual(vec3 x, vec3 y); +bvec4 greaterThanEqual(vec4 x, vec4 y); +bvec2 greaterThanEqual(ivec2 x, ivec2 y); +bvec3 greaterThanEqual(ivec3 x, ivec3 y); +bvec4 greaterThanEqual(ivec4 x, ivec4 y); +bvec2 equal(vec2 x, vec2 y); +bvec3 equal(vec3 x, vec3 y); +bvec4 equal(vec4 x, vec4 y); +bvec2 equal(ivec2 x, ivec2 y); +bvec3 equal(ivec3 x, ivec3 y); +bvec4 equal(ivec4 x, ivec4 y); +bvec2 notEqual(vec2 x, vec2 y); +bvec3 notEqual(vec3 x, vec3 y); +bvec4 notEqual(vec4 x, vec4 y); +bvec2 notEqual(ivec2 x, ivec2 y); +bvec3 notEqual(ivec3 x, ivec3 y); +bvec4 notEqual(ivec4 x, ivec4 y); +bool any(bvec2 x); +bool any(bvec3 x); +bool any(bvec4 x); +bool all(bvec2 x); +bool all(bvec3 x); +bool all(bvec4 x); +bool not(bvec2 x); +bool not(bvec3 x); +bool not(bvec4 x); +void imulExtended(int x, int y, out int msb, out int lsb); +void imulExtended(ivec2 x, ivec2 y, out ivec2 msb, out ivec2 lsb); +void imulExtended(ivec3 x, ivec3 y, out ivec3 msb, out ivec3 lsb); +void imulExtended(ivec4 x, ivec4 y, out ivec4 msb, out ivec4 lsb); +int bitfieldExtract(int value, int offset, int bits); +ivec2 bitfieldExtract(ivec2 value, int offset, int bits); +ivec3 bitfieldExtract(ivec3 value, int offset, int bits); +ivec4 bitfieldExtract(ivec4 value, int offset, int bits); +int bitfieldInsert(int value, int insert, int offset, int bits); +ivec2 bitfieldInsert(ivec2 value, ivec2 insert, int offset, int bits); +ivec3 bitfieldInsert(ivec3 value, ivec3 insert, int offset, int bits); +ivec4 bitfieldInsert(ivec4 value, ivec4 insert, int offset, int bits); +int bitfieldReverse(int value); +ivec2 bitfieldReverse(ivec2 value); +ivec3 bitfieldReverse(ivec3 value); +ivec4 bitfieldReverse(ivec4 value); +int bitCount(int value); +ivec2 bitCount(ivec2 value); +ivec3 bitCount(ivec3 value); +ivec4 bitCount(ivec4 value); +int findLSB(int value); +ivec2 findLSB(ivec2 value); +ivec3 findLSB(ivec3 value); +ivec4 findLSB(ivec4 value); +int findMSB(int value); +ivec2 findMSB(ivec2 value); +ivec3 findMSB(ivec3 value); +ivec4 findMSB(ivec4 value); int textureSize(sampler1D sampler, int lod); ivec2 textureSize(sampler2D sampler, int lod); ivec3 textureSize(sampler3D sampler, int lod); @@ -335,3 +471,55 @@ out gl_PerVertex void EmitVertex(); void EndPrimitive(); + +#pragma MSP stage(fragment) +// BEGIN BUILTIN FRAGMENT FUNCTIONS +float dFdx(float p); +vec2 dFdx(vec2 p); +vec3 dFdx(vec3 p); +vec4 dFdx(vec4 p); +float dFdy(float p); +vec2 dFdy(vec2 p); +vec3 dFdy(vec3 p); +vec4 dFdy(vec4 p); +float dFdxFine(float p); +vec2 dFdxFine(vec2 p); +vec3 dFdxFine(vec3 p); +vec4 dFdxFine(vec4 p); +float dFdyFine(float p); +vec2 dFdyFine(vec2 p); +vec3 dFdyFine(vec3 p); +vec4 dFdyFine(vec4 p); +float dFdxCoarse(float p); +vec2 dFdxCoarse(vec2 p); +vec3 dFdxCoarse(vec3 p); +vec4 dFdxCoarse(vec4 p); +float dFdyCoarse(float p); +vec2 dFdyCoarse(vec2 p); +vec3 dFdyCoarse(vec3 p); +vec4 dFdyCoarse(vec4 p); +float fwidth(float p); +vec2 fwidth(vec2 p); +vec3 fwidth(vec3 p); +vec4 fwidth(vec4 p); +float fwidthFine(float p); +vec2 fwidthFine(vec2 p); +vec3 fwidthFine(vec3 p); +vec4 fwidthFine(vec4 p); +float fwidthCoarse(float p); +vec2 fwidthCoarse(vec2 p); +vec3 fwidthCoarse(vec3 p); +vec4 fwidthCoarse(vec4 p); +float interpolateAtCentroid(float interpolant); +vec2 interpolateAtCentroid(vec2 interpolant); +vec3 interpolateAtCentroid(vec3 interpolant); +vec4 interpolateAtCentroid(vec4 interpolant); +float interpolateAtSample(float interpolant, int sample); +vec2 interpolateAtSample(vec2 interpolant, int sample); +vec3 interpolateAtSample(vec3 interpolant, int sample); +vec4 interpolateAtSample(vec4 interpolant, int sample); +float interpolateAtOffset(float interpolant, vec2 offset); +vec2 interpolateAtOffset(vec2 interpolant, vec2 offset); +vec3 interpolateAtOffset(vec3 interpolant, vec2 offset); +vec4 interpolateAtOffset(vec4 interpolant, vec2 offset); +// END BUILTIN FRAGMENT FUNCTIONS diff --git a/scripts/builtin_funcs.py b/scripts/builtin_funcs.py index 1c5b8320..0022f40c 100755 --- a/scripts/builtin_funcs.py +++ b/scripts/builtin_funcs.py @@ -16,35 +16,55 @@ traits = { "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" } + "float": { "Base": "float", "Dim": 1, "Vec": "vec", "Mat": "mat" }, + "vec2": { "Base": "float", "Dim": 2 }, + "vec3": { "Base": "float", "Dim": 3 }, + "vec4": { "Base": "float", "Dim": 4 }, + "int": { "Base": "int", "Dim": 1, "Vec": "ivec" }, + "ivec2": { "Base": "int", "Dim": 2 }, + "ivec3": { "Base": "int", "Dim": 3 }, + "ivec4": { "Base": "int", "Dim": 4 }, + "bool": { "Base": "bool", "Dim": 1, "Vec": "bvec" }, + "bvec2": { "Base": "bool", "Dim": 2 }, + "bvec3": { "Base": "bool", "Dim": 3 }, + "bvec4": { "Base": "bool", "Dim": 4 }, + "mat2": { "Cols": 2, "Rows": 2 }, + "mat3": { "Cols": 3, "Rows": 3 }, + "mat4": { "Cols": 4, "Rows": 4 }, + "mat2x3": { "Cols": 2, "Rows": 3 }, + "mat3x2": { "Cols": 3, "Rows": 2 }, + "mat2x4": { "Cols": 2, "Rows": 4 }, + "mat4x2": { "Cols": 4, "Rows": 2 }, + "mat3x4": { "Cols": 3, "Rows": 4 }, + "mat4x3": { "Cols": 4, "Rows": 3 } } -float32types = ("float", "vec2", "vec3", "vec4") +float32vectypes = ("vec2", "vec3", "vec4") +float32types = ("float",)+float32vectypes floattypes = float32types -int32types = ("int", "ivec2", "ivec3", "ivec4") +int32vectypes = ("ivec2", "ivec3", "ivec4") +int32types = ("int",)+int32vectypes signedtypes = floattypes+int32types arithmetictypes = signedtypes +arithmeticvectypes = float32vectypes+int32vectypes +boolvectypes = ("bvec2", "bvec3", "bvec4") 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 = [ +shared_funcs = [ # Trigonometric + ("T radians(T degrees)", float32types), + ("T degrees(T radians)", float32types), ("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 atan(T y_over_x)", float32types), ("T sinh(T angle)", float32types), ("T cosh(T angle)", float32types), ("T tanh(T angle)", float32types), @@ -81,11 +101,18 @@ funcs = [ ("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 mix(T x, T y, bool[T::Dim] a)", arithmetictypes), ("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), + ("bool[T::Dim] isnan(T x)", floattypes), + ("bool[T::Dim] isinf(T x)", floattypes), + ("int[T::Dim] floatBitsToInt(T value)", float32types), + ("T intBitsToFloat(int[T::Dim] value)", float32types), ("T fma(T a, T b, T c)", floattypes), + ("T frexp(T x, out int[T::Dim] exp)", floattypes), + ("T ldexp(T x, int[T::Dim] exp)", floattypes), # Geometric ("T::Base length(T x)", floattypes), @@ -99,10 +126,31 @@ funcs = [ # Matrix ("T matrixCompMult(T x, T y)", matrixtypes), - ("T transpose(T m)", squarematrixtypes), + ("T outerProduct(float[T::Rows] c, float[T::Cols] r)", matrixtypes), + ("float[T::Rows, T::Cols] transpose(T m)", matrixtypes), ("T determinant(T m)", squarematrixtypes), ("T inverse(T m)", squarematrixtypes), + # Vector relational + ("bool[T::Dim] lessThan(T x, T y)", arithmeticvectypes), + ("bool[T::Dim] lessThanEqual(T x, T y)", arithmeticvectypes), + ("bool[T::Dim] greaterThan(T x, T y)", arithmeticvectypes), + ("bool[T::Dim] greaterThanEqual(T x, T y)", arithmeticvectypes), + ("bool[T::Dim] equal(T x, T y)", arithmeticvectypes), + ("bool[T::Dim] notEqual(T x, T y)", arithmeticvectypes), + ("bool any(T x)", boolvectypes), + ("bool all(T x)", boolvectypes), + ("bool not(T x)", boolvectypes), + + # Integer + ("void imulExtended(T x, T y, out T msb, out T lsb)", int32types), + ("T bitfieldExtract(T value, int offset, int bits)", int32types), + ("T bitfieldInsert(T value, T insert, int offset, int bits)", int32types), + ("T bitfieldReverse(T value)", int32types), + ("T bitCount(T value)", int32types), + ("int[T::Dim] findLSB(T value)", int32types), + ("int[T::Dim] findMSB(T value)", int32types), + # Texture ("int[T::IDim] textureSize(T sampler, int lod)", samplertypes), ("vec4 texture(T sampler, float[T::CDim] P)", colorsamplertypes), @@ -112,6 +160,24 @@ funcs = [ ("vec4 texelFetch(T sampler, int[T::CDim] P, int lod)", flatsamplertypes) ] +fragment_funcs = [ + # Derivative + ("T dFdx(T p)", float32types), + ("T dFdy(T p)", float32types), + ("T dFdxFine(T p)", float32types), + ("T dFdyFine(T p)", float32types), + ("T dFdxCoarse(T p)", float32types), + ("T dFdyCoarse(T p)", float32types), + ("T fwidth(T p)", float32types), + ("T fwidthFine(T p)", float32types), + ("T fwidthCoarse(T p)", float32types), + + # Interpolation + ("T interpolateAtCentroid(T interpolant)", float32types), + ("T interpolateAtSample(T interpolant, int sample)", float32types), + ("T interpolateAtOffset(T interpolant, vec2 offset)", float32types) +] + def tokenize(code): out_tokens = [] token = "" @@ -145,9 +211,15 @@ def expand_tokens(tokens, i, gentype): 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) + if tokens[i+2+advance]==",": + sub2, adv2 = expand_tokens(tokens, i+3+advance, gentype) + advance += 1+adv2 + if sub2==sub: + t = traits[t]["Mat"]+str(sub) + else: + t = traits[t]["Mat"]+"{}x{}".format(sub, sub2) + elif sub>1: + t = traits[t]["Vec"]+str(sub) return (t, 3+advance) return (t, 1) @@ -172,7 +244,7 @@ def expand_template(template, gentype): return result -def generate_functions(): +def generate_functions(funcs): out_lines = [] generated = set() for f in funcs: @@ -195,8 +267,11 @@ def generate_file(fn): out_lines.append(line) if "BEGIN BUILTIN FUNCTIONS" in line: skip = True - out_lines += generate_functions() - elif "END BUILTIN FUNCTIONS" in line: + out_lines += generate_functions(shared_funcs) + elif "BEGIN BUILTIN FRAGMENT FUNCTIONS" in line: + skip = True + out_lines += generate_functions(fragment_funcs) + elif "END BUILTIN" in line: out_lines.append(line) skip = False -- 2.43.0