High Level Shading Language
High Level Shading Language (HLSL) is a shader programming language for GPU. Pixel Composer has HLSL supports through the HLSL node. This page will only cover basics of HLSL. For more information, check out the further reading section.
HLSL Node
You can find the HLSL node under the Misc. category. HLSL node takes HLSL fragment code and custom arguments. The code will compile automatically, however you can also compile it manually by clicking the Compile button in the inspector.
Syntax
HLSL runs over every pixel. Process and returns the output pixel color. Let's take a look at the default
HLSL fragment code.
float4 surfaceColor = gm_BaseTextureObject.Sample(gm_BaseTexture, input.uv);
output.color = surfaceColor;
This code does 2 things.
The first line
float4 surfaceColor = gm_BaseTextureObject.Sample(gm_BaseTexture, input.uv);
Get the color of the current pixel from the input surface and stores it in the
surfaceColor variable.
The seconds line output.color = surfaceColor; apply the color to the output.
Variable
Variable in HLSL are strongly typed (type need to be defined when create). Variable types you can use are:
Type | Description |
---|---|
float | Floating point number |
int | Integer |
bool | Boolean |
float2 | 2D vector |
float3 | 3D vector |
float4 | 4D vector |
float3x3 | 3x3 matrix |
float4x4 | 4x4 matrix |
Operators
Operators in HLSL are similar to other programming languages. Here are some of the operators you can use:
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
++ | Increment |
-- | Decrement |
== | Equal |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
! | Logical NOT |
&& | Logical AND |
|| | Logical OR |
Swizzling
Swizzling is a feature in HLSL that allows you to access the components of a vector. For example, you can access the x and y components of a float3 vector by using .xy suffixes. Here are some examples:
.x | Get a single value from vector. |
.xz | Create new float2 consisting of the first and third elements. |
.xxx | Create new float3 with all the values equals to the first element. |
.rgb | Create new float3 with the first three elements. |
Functions Reference
abs(x)
abs
acos(x)
acos
asfloat(x)
asfloat
asin(x)
asin
asint(x)
asint
atan(x)
atan
atan2(y, x)
atan2
ceil(x)
ceil
clamp(x, min, max)
clamp
clip(x)
clip
cos(x)
cos
cosh(x)
cosh
cross(x, y)
cross
ddx(x)
ddx
ddy(x)
ddy
degrees(x)
degrees
determinant(x)
determinant
distance(x, y)
distance
dot(x, y)
dot
exp(x)
exp
exp2(x)
exp2
faceforward(n, i, ng)
faceforward
floor(x)
floor
fma(a, b, c)
fma
fmod(x, y)
fmod
frac(x)
frac
frexp(x, exp)
frexp
fwidth(x)
fwidth
isfinite(x)
isfinite
isinf(x)
isinf
isnan(x)
isnan
ldexp(x, exp)
ldexp
length(x)
length
lerp(x, y, s)
lerp
lit(n_dot_l, n_dot_h, m)
lit
log(x)
log
log10(x)
log10
log2(x)
log2
max(x, y)
max
min(x, y)
min
modf(x, out ip)
modf
mul(x, y)
mul
noise(x)
noise
normalize(x)
normalize
pow(x, y)
pow
radians(x)
radians
rcp(x)
rcp
reflect(i, n)
reflect
refract(i, n, ?)
refract
round(x)
round
rsqrt(x)
rsqrt
saturate(x)
saturate
sign(x)
sign
sin(x)
sin
sincos(x, out s, out c)
sincos
sinh(x)
sinh
smoothstep(min, max, x)
smoothstep
sqrt(x)
sqrt
step(y, x)
step
tan(x)
tan
tanh(x)
tanh
transpose(x)
transpose
trunc(x)
trunc