"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 },
+ "uint": { "Base": "uint", "Dim": 1, "Vec": "uvec" },
+ "uvec2": { "Base": "uint", "Dim": 2 },
+ "uvec3": { "Base": "uint", "Dim": 3 },
+ "uvec4": { "Base": "uint", "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
+floatvectypes = float32vectypes
floattypes = float32types
-int32types = ("int", "ivec2", "ivec3", "ivec4")
-signedtypes = floattypes+int32types
-arithmetictypes = signedtypes
+sint32vectypes = ("ivec2", "ivec3", "ivec4")
+sint32types = ("int",)+sint32vectypes
+signedtypes = floattypes+sint32types
+uint32vectypes = ("uvec2", "uvec3", "uvec4")
+uint32types = ("uint",)+uint32vectypes
+int32types = sint32types+uint32types
+unsignedtypes = uint32types
+arithmetictypes = signedtypes+unsignedtypes
+arithmeticvectypes = float32vectypes+sint32vectypes+uint32vectypes
+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),
("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 mod(T x, T::Base y)", "mod(x, T(y))", floatvectypes),
("T min(T x, T y)", arithmetictypes),
- ("T min(T x, T::Base y)", arithmetictypes),
+ ("T min(T x, T::Base y)", "min(x, T(y))", arithmeticvectypes),
("T max(T x, T y)", arithmetictypes),
- ("T max(T x, T::Base y)", arithmetictypes),
+ ("T max(T x, T::Base y)", "max(x, T(y))", arithmeticvectypes),
("T clamp(T x, T minVal, T maxVal)", arithmetictypes),
- ("T clamp(T x, T::Base minVal, T::Base maxVal)", arithmetictypes),
+ ("T clamp(T x, T::Base minVal, T::Base maxVal)", "clamp(x, T(minVal), T(maxVal))", arithmeticvectypes),
("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, T::Base a)", "mix(x, y, T(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 step(T::Base edge, T x)", "step(T(edge), x)", floatvectypes),
("T smoothstep(T edge0, T edge1, T x)", floattypes),
- ("T smoothstep(T::Base edge0, T::Base edge1, T x)", floattypes),
+ ("T smoothstep(T::Base edge0, T::Base edge1, T x)", "smoothstep(T(edge0), T(edge1), x)", floatvectypes),
+ ("bool[T::Dim] isnan(T x)", floattypes),
+ ("bool[T::Dim] isinf(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)",
+ "vec3 cross(vec3 x, vec3 y)",
("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),
+ ("T reflect(T I, T N)", floattypes),
+ ("T refract(T I, T N, float eta)", floattypes),
# 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
+ ("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),
("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 textureLod(T sampler, float[T::CDim] P, float lod)", colorsamplertypes),
("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 = ""
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)
result += " "
special = not t[0].isalpha()
- if t==",":
- result += ", "
- else:
- result += t
+ result += t
+ if t[-1]==",":
+ result += " "
i += advance
return result
-def generate_functions():
+def generate_functions(funcs):
out_lines = []
generated = set()
for f in funcs:
if type(f)==tuple:
- for t in f[1]:
+ for t in f[-1]:
decl = expand_template(f[0], t)
+ if len(f)>=3:
+ decl += " {{ return {}; }}".format(expand_template(f[1], t))
+ else:
+ decl += ";"
if not decl in generated:
- out_lines.append(decl+";\n")
+ out_lines.append(decl+"\n")
generated.add(decl)
else:
out_lines.append(f+";\n")
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