Intrinsics8 min read

Intrinsics

Built-in functions available in BWSL for common shader operations.

Reading Time
8 min
Word Count
1,354
Sections
15
Try It Live

Turn the guide into code

Take the key idea from this page into the playground and validate it in a real shader instead of leaving it as theory.

Open Playground

BWSL provides a broad intrinsic surface for math, texturing, derivatives, compute synchronization, atomics, and wave operations.

Math Functions

FunctionSignatureDescription
absabs :: (T x) -> TAbsolute value
ceilceil :: (T x) -> TRound up to nearest integer
clampclamp :: (T x, T min, T max) -> TClamp value between min and max
floorfloor :: (T x) -> TRound down to nearest integer
fmafma :: (T a, T b, T c) -> TFused multiply-add
fractfract :: (T x) -> TFractional part of value
lerplerp :: (T a, T b, S t) -> TLinear interpolation
maxmax :: (T a, T b, ...) -> TMaximum of values
minmin :: (T a, T b, ...) -> TMinimum of values
modmod :: (T x, T y) -> TFloating-point modulo
modfmodf :: (float x) -> BwslModfResultSplit fraction and whole parts
roundround :: (T x) -> TRound to nearest integer
saturatesaturate :: (T x) -> TClamp to [0, 1]
signsign :: (T x) -> TSign of value (-1, 0, or 1)
smoothstepsmoothstep :: (S edge0, S edge1, T x) -> THermite interpolation
stepstep :: (T edge, T x) -> TStep function
trunctrunc :: (T x) -> TRound toward zero

Exponential & Power Functions

FunctionSignatureDescription
expexp :: (T x) -> TNatural exponential
exp2exp2 :: (T x) -> TBase-2 exponential
frexpfrexp :: (float x) -> BwslFrexpResultSplit mantissa and exponent
ldexpldexp :: (float x, int e) -> floatScale by a power of two
loglog :: (T x) -> TNatural logarithm
log10log10 :: (T x) -> TBase-10 logarithm
log2log2 :: (T x) -> TBase-2 logarithm
powpow :: (T x, T y) -> TPower function
rcprcp :: (T x) -> TReciprocal
rsqrtrsqrt :: (T x) -> TInverse square root
sqrtsqrt :: (T x) -> TSquare root

Trigonometric Functions

FunctionSignatureDescription
acosacos :: (T x) -> TArc cosine
asinasin :: (T x) -> TArc sine
atanatan :: (T x) -> TArc tangent
atan2atan2 :: (T y, T x) -> TTwo-argument arc tangent
coscos :: (T x) -> TCosine
coshcosh :: (T x) -> THyperbolic cosine
degreesdegrees :: (T x) -> TRadians to degrees
radiansradians :: (T x) -> TDegrees to radians
sinsin :: (T x) -> TSine
sincossincos :: (...) -> voidCombined sine/cosine intrinsic; prefer sin and cos in user code for now
sinhsinh :: (T x) -> THyperbolic sine
tantan :: (T x) -> TTangent
tanhtanh :: (T x) -> THyperbolic tangent

Vector Functions

FunctionSignatureDescription
crosscross :: (float3 a, float3 b) -> float3Cross product
distancedistance :: (vecN a, vecN b) -> floatDistance between points
dotdot :: (vecN a, vecN b) -> floatDot product
faceforwardfaceforward :: (vecN n, vecN i, vecN nref) -> vecNFlip normal to face viewer
lengthlength :: (vecN v) -> floatVector magnitude
normalizenormalize :: (vecN v) -> vecNUnit vector
reflectreflect :: (vecN i, vecN n) -> vecNReflection vector
refractrefract :: (vecN i, vecN n, float eta) -> vecNRefraction vector

Matrix Functions

FunctionSignatureDescription
determinantdeterminant :: (matNxN m) -> floatMatrix determinant
inverseinverse :: (matNxN m) -> matNxNMatrix inverse
transposetranspose :: (matNxM m) -> matMxNMatrix transpose

Derivative Functions

FunctionSignatureDescription
ddxddx :: (T x) -> TPartial derivative in x
ddx_coarseddx_coarse :: (T x) -> TCoarse partial derivative in x
ddx_fineddx_fine :: (T x) -> TFine partial derivative in x
ddyddy :: (T x) -> TPartial derivative in y
ddy_coarseddy_coarse :: (T x) -> TCoarse partial derivative in y
ddy_fineddy_fine :: (T x) -> TFine partial derivative in y
fwidthfwidth :: (T x) -> TSum of absolute derivatives
fwidth_coarsefwidth_coarse :: (T x) -> TCoarse filter width
fwidth_finefwidth_fine :: (T x) -> TFine filter width

Texture Functions

FunctionSignatureDescription
gathergather :: (texture2D tex, sampler samp, float2 uv, int component) -> float4Gather 4 texels
gather_offsetgather_offset :: (texture2D tex, sampler samp, float2 uv, int component, int2 offset) -> float4Gather 4 texels with offset
loadload :: (texture2D tex, int2 coord, int mip) -> float4Load texel without filtering
load_offsetload_offset :: (texture2D tex, int2 coord, int mip, int2 offset) -> float4Load texel with offset
samplesample :: (texture2D tex, sampler samp, float2 uv) -> float4Sample texture
sample_offsetsample_offset :: (texture2D tex, sampler samp, float2 uv, int2 offset) -> float4Sample texture with offset
sample_biassample_bias :: (texture2D tex, sampler samp, float2 uv, float bias) -> float4Sample with LOD bias
sample_bias_offsetsample_bias_offset :: (texture2D tex, sampler samp, float2 uv, float bias, int2 offset) -> float4Sample with bias and offset
sample_cmpsample_cmp :: (texture2D tex, sampler samp, float2 uv, float cmp) -> floatSample with comparison
sample_gradsample_grad :: (texture2D tex, sampler samp, float2 uv, float2 ddx, float2 ddy) -> float4Sample with explicit gradients
sample_lodsample_lod :: (texture2D tex, sampler samp, float2 uv, float lod) -> float4Sample at explicit LOD
sample_lod_offsetsample_lod_offset :: (texture2D tex, sampler samp, float2 uv, float lod, int2 offset) -> float4Sample explicit LOD with offset
storestore :: (RWTexture2D tex, int2 coord, float4 value)Write to storage texture
texture_levelstexture_levels :: (texture tex) -> intQuery texture mip count
texture_sizetexture_size :: (texture tex, int mip) -> intNQuery texture dimensions

Synchronization Functions

FunctionSignatureDescription
barrierbarrier :: () -> voidWorkgroup barrier
memoryBarriermemoryBarrier :: () -> voidMake memory writes visible
storageBarrierstorageBarrier :: () -> voidMake storage writes visible

Wave/SIMD Functions

FunctionSignatureDescription
wave_allwave_all :: (bool x) -> boolTrue if all lanes are true
wave_anywave_any :: (bool x) -> boolTrue if any lane is true
wave_broadcastwave_broadcast :: (T x, int lane) -> TBroadcast from lane
wave_maxwave_max :: (T x) -> TMaximum across wave
wave_minwave_min :: (T x) -> TMinimum across wave
wave_productwave_product :: (T x) -> TProduct across wave
wave_read_firstwave_read_first :: (T x) -> TRead from first active lane
wave_sumwave_sum :: (T x) -> TSum across wave

Atomic Functions

FunctionSignatureDescription
atomic_addatomic_add :: (integer lvalue dest, integer value) -> integerAtomic addition
atomic_andatomic_and :: (integer lvalue dest, integer value) -> integerAtomic bitwise AND
atomic_cmp_exchangeatomic_cmp_exchange :: (integer lvalue dest, integer compare, integer value) -> integerAtomic compare-exchange
atomic_exchangeatomic_exchange :: (integer lvalue dest, integer value) -> integerAtomic exchange
atomic_maxatomic_max :: (integer lvalue dest, integer value) -> integerAtomic maximum
atomic_minatomic_min :: (integer lvalue dest, integer value) -> integerAtomic minimum
atomic_oratomic_or :: (integer lvalue dest, integer value) -> integerAtomic bitwise OR
atomic_xoratomic_xor :: (integer lvalue dest, integer value) -> integerAtomic bitwise XOR

Bit Manipulation Functions

FunctionSignatureDescription
bitfield_extractbitfield_extract :: (T value, int offset, int bits) -> TExtract a bit field
bitfield_insertbitfield_insert :: (T base, T insert, int offset, int bits) -> TInsert a bit field
count_bitscount_bits :: (int x) -> intCount set bits
first_bit_highfirst_bit_high :: (int x) -> intFind highest set bit
first_bit_lowfirst_bit_low :: (int x) -> intFind lowest set bit
reverse_bitsreverse_bits :: (int x) -> intReverse bit order

Data Conversion & Packing

FunctionSignatureDescription
asfloatasfloat :: (intNuintN x) -> floatNReinterpret bits as floats
asintasint :: (floatNuintN x) -> intNReinterpret bits as signed integers
asuintasuint :: (floatNintN x) -> uintNReinterpret bits as unsigned integers
f16tof32f16tof32 :: (uint x) -> floatDecode half-float bits
f32tof16f32tof16 :: (float x) -> uintEncode half-float bits
pack_half2x16pack_half2x16 :: (float2 value) -> uintPack two half floats
unpack_half2x16unpack_half2x16 :: (uint packed) -> float2Unpack two half floats
pack_snorm2x16pack_snorm2x16 :: (float2 value) -> uintPack two signed normalized channels
unpack_snorm2x16unpack_snorm2x16 :: (uint packed) -> float2Unpack two signed normalized channels
pack_snorm4x8pack_snorm4x8 :: (float4 value) -> uintPack signed normalized channels
unpack_snorm4x8unpack_snorm4x8 :: (uint packed) -> float4Unpack signed normalized channels
pack_unorm2x16pack_unorm2x16 :: (float2 value) -> uintPack two unsigned normalized channels
unpack_unorm2x16unpack_unorm2x16 :: (uint packed) -> float2Unpack two unsigned normalized channels
pack_unorm4x8pack_unorm4x8 :: (float4 value) -> uintPack unsigned normalized channels
unpack_unorm4x8unpack_unorm4x8 :: (uint packed) -> float4Unpack unsigned normalized channels

Control Flow Functions

FunctionSignatureDescription
discarddiscard :: ()Discard current fragment
selectselect :: (T a, T b, bool cond) -> TConditional select

Boolean Reductions

FunctionSignatureDescription
allall :: (bvecN x) -> boolTrue if every component is true
anyany :: (bvecN x) -> boolTrue if at least one component is true

Float Classification

FunctionSignatureDescription
isfiniteisfinite :: (floatN x) -> boolNComponentwise finite-value test
isinfisinf :: (floatN x) -> boolNComponentwise +/-infinity test
isnanisnan :: (floatN x) -> boolNComponentwise NaN test
isnormalisnormal :: (floatN x) -> boolNComponentwise normal-value test

T indicates the function works with scalar and vector types. S indicates a scalar type. The atomic rows above describe the current call shape rather than a formal type-system syntax.