Types & Expressions
Type System
AXSLC does not perform type checking. All types are passed through to GLSL, where the driver validates them.
Built-in Types
All GLSL types are supported:
Scalars: bool, int, uint, float, double
Vectors:
- Float:
vec2,vec3,vec4 - Int:
ivec2,ivec3,ivec4 - Unsigned:
uvec2,uvec3,uvec4 - Boolean:
bvec2,bvec3,bvec4
Matrices:
- Square:
mat2,mat3,mat4 - Rectangular:
mat2x3,mat2x4,mat3x2,mat3x4,mat4x2,mat4x3
Samplers:
sampler2D,sampler3D,samplerCubesampler2DShadow,samplerCubeShadowisampler2D,isampler3D,isamplerCubeusampler2D,usampler3D,usamplerCube
Images:
image2D,image3D,imageCubeiimage2D,iimage3D,iimageCubeuimage2D,uimage3D,uimageCube
User-Defined Types
Any identifier can be used as a type:
struct Material {
vec3 albedo;
float roughness;
};
Material create_default_material() {
Material m;
m.albedo = vec3(0.5);
m.roughness = 0.5;
return m;
}Expressions
Literals
Integer:
42 // decimal
0xFF // hexadecimal
0x1A2B // hexadecimalFloat:
3.14159
0.5
1.0
0. // abbreviation for 0.0
1. // abbreviation for 1.0Boolean:
true
falseIdentifiers
position
u_time
gl_PositionType Constructors
Vector constructors:
vec2(1.0, 0.0)
vec3(1.0) // all components = 1.0
vec4(vec3(1.0, 0.0, 0.0), 1.0) // from vec3 + scalarMatrix constructors:
mat4(1.0) // identity matrix
mat3(rotation_matrix) // extract 3x3 from 4x4Struct constructors:
Material(vec3(1.0), 0.5)Binary Operators
Arithmetic:
a + b
a - b
a * b
a / b
a % bComparison:
a == b
a != b
a < b
a > b
a <= b
a >= bLogical:
a && b
a || bBitwise:
a & b
a | b
a ^ b
a << b
a >> bUnary Operators
Prefix:
!a // logical not
~a // bitwise not
-a // negation
+a // unary plus
++a // pre-increment
--a // pre-decrementPostfix:
a++ // post-increment
a-- // post-decrementAssignment Operators
Simple assignment:
a = bCompound assignment:
a += b // a = a + b
a -= b // a = a - b
a *= b // a = a * b
a /= b // a = a / b
a %= b // a = a % b
a &= b // a = a & b
a |= b // a = a | b
a ^= b // a = a ^ b
a <<= b // a = a << b
a >>= b // a = a >> bTernary Operator
condition ? then_value : else_value
// Example:
float value = x > 0.0 ? 1.0 : -1.0;Field Access
Vector swizzling:
position.x
position.xyz
color.rgb
color.bgr
position.xyzwStruct field access:
material.albedo
light.positionBlock instance access:
camera.view
scene.timeArray Subscript
array[0]
array[index]
matrix[row][col]Function Calls
sin(angle)
normalize(vec)
texture(sampler, uv)
pow(base, exp)Built-in functions:
AXSLC recognizes 70+ GLSL built-in functions:
Trigonometric:sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh
Exponential:pow, exp, log, exp2, log2, sqrt, inversesqrt
Common:abs, sign, floor, trunc, round, roundEven, ceil, fract, mod, modf, min, max, clamp, mix, step, smoothstep, isnan, isinf
Geometric:length, distance, dot, cross, normalize, reflect, refract, faceforward
Matrix:matrixCompMult, outerProduct, transpose, determinant, inverse
Vector Relational:lessThan, lessThanEqual, greaterThan, greaterThanEqual, equal, notEqual, any, all, not
Texture:texture, texture2D, texture3D, textureCube, textureLod, texture2DLod, textureSize, texelFetch, texelFetchOffset, textureOffset, textureGrad, textureGather
Image:imageLoad, imageStore, imageSize
Atomic:atomicAdd, atomicMin, atomicMax, atomicAnd, atomicOr, atomicXor, atomicExchange, atomicCompSwap
Derivative:dFdx, dFdy, fwidth
Geometry:emit, endPrimitive
Compute:barrier, memoryBarrier, groupMemoryBarrier
Packing:packUnorm2x16, packSnorm2x16, unpackUnorm2x16, unpackSnorm2x16, packHalf2x16, unpackHalf2x16
Operator Precedence
From highest to lowest precedence:
| Level | Operators | Associativity |
|---|---|---|
| 15 | () [] . ++ -- (postfix) | Left |
| 14 | ++ -- (prefix) + - (unary) ! ~ | Right |
| 12 | * / % | Left |
| 11 | + - | Left |
| 10 | << >> | Left |
| 9 | < > <= >= | Left |
| 8 | == != | Left |
| 7 | & | Left |
| 6 | ^ | Left |
| 5 | ` | ` |
| 4 | && | Left |
| 3 | ` | |
| 2 | ?: | Right |
| 1 | = += -= *= /= etc. | Right |
Type Conversions
AXSLC does not validate conversions. The GLSL compiler handles implicit and explicit conversions:
float f = 1.0;
int i = int(f); // explicit cast
vec3 v = vec3(1.0); // constructor
vec4 v4 = vec4(v, 1.0); // mixed constructorExamples
Complex Expression
vec3 result = normalize(cross(a, b)) * dot(c, d) + mix(e, f, t);Chained Field Access
vec3 camera_pos = scene.camera.position;
float red = material.albedo.r;Matrix Multiplication
vec4 clip_pos = projection * view * model * vec4(local_pos, 1.0);Ternary with Function Calls
vec3 color = is_lit ? compute_lighting(normal) : ambient_color;Array and Swizzle
vec2 uv = vertices[index].position.xy;