Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Texture & Sampler Functions

wgsl_rs::std provides the WGSL texture and sampler builtins plus the sampler! and texture! binding macros used to declare them.

Sampler types

TypeWGSLDescription
SamplersamplerFiltering sampler.
SamplerComparisonsampler_comparisonComparison sampler for shadow/PCF sampling.

See Binding Macros: texture! & sampler! for declaration syntax.

Sampling functions

Each function has multiple overloads (2D, 2DArray, 3D, Cube, CubeArray, etc.), implemented as separate Rust functions that map to the same WGSL builtin. The transpiler picks the right overload from argument types.

FunctionWGSL EquivalentDescription
texture_sample(tex, sampler, coords)textureSampleFiltered sample. Fragment stage only.
texture_sample(tex, sampler, coords, offset)textureSampleWith integer texel offset.
texture_sample_level(tex, sampler, coords, level)textureSampleLevelExplicit mip level. Any stage.
texture_sample_level(tex, sampler, coords, level, offset)textureSampleLevelWith offset.
texture_sample_bias(tex, sampler, coords, bias)textureSampleBiasAdds mip bias. Fragment stage only.
texture_sample_bias(tex, sampler, coords, bias, offset)textureSampleBiasWith offset.
texture_sample_grad(tex, sampler, coords, ddx, ddy)textureSampleGradExplicit gradients. Any stage.
texture_sample_grad(tex, sampler, coords, ddx, ddy, offset)textureSampleGradWith offset.
texture_sample_compare(tex, sampler, coords, ref)textureSampleCompareDepth comparison. Fragment stage only.
texture_sample_compare_level(tex, sampler, coords, ref)textureSampleCompareLevelDepth comparison, uniform level. Any stage.
texture_sample_base_clamp_to_edge(tex, sampler, coords)textureSampleBaseClampToEdgeSample with coords clamped to [0,1]. Any stage.

Functions suffixed ..._level are usable from any stage; plain texture_sample and texture_sample_bias/texture_sample_compare are restricted to the fragment stage in WGSL.

The level parameter of texture_sample_level must be f32, per the WGSL spec. wgsl-rs does not expose a level parameter for depth textures (texture_sample_compare_level always samples at mip 0).

Gather functions

textureGather reads one channel of the 4 texels that would be used in bilinear filtering, returning them as a vec4<ST> in the order (u_min,v_max), (u_max,v_max), (u_max,v_min), (u_min,v_min).

FunctionWGSL EquivalentTexture kinds
texture_gather(component, tex, sampler, coords)textureGather2D, cube
texture_gather_offset(component, tex, sampler, coords, offset)textureGather2D
texture_gather_array(component, tex, sampler, coords, array_index)textureGather2D array, cube array
texture_gather_array_offset(component, tex, sampler, coords, array_index, offset)textureGather2D array
texture_gather_depth(tex, sampler, coords)textureGatherdepth 2D, depth cube
texture_gather_depth(tex, sampler, coords, offset)textureGatherdepth 2D
texture_gather_depth_array(tex, sampler, coords, array_index)textureGatherdepth 2D array, depth cube array
texture_gather_depth_array(tex, sampler, coords, array_index, offset)textureGatherdepth 2D array
texture_gather_compare(tex, cmp_sampler, coords, ref)textureGatherComparedepth 2D, depth cube
texture_gather_compare(tex, cmp_sampler, coords, ref, offset)textureGatherComparedepth 2D
texture_gather_compare_array(tex, cmp_sampler, coords, array_index, ref)textureGatherComparedepth 2D array, depth cube array
texture_gather_compare_array(tex, cmp_sampler, coords, array_index, ref, offset)textureGatherComparedepth 2D array

2D, 2D-array, and cube gathers support f32, i32, and u32 sampled types; all array gather forms accept u32 or i32 array indices, matching the WGSL spec.

Load / store / query

FunctionWGSL EquivalentDescription
texture_load(tex, coords)textureLoadLoad texel at integer coords.
texture_load(tex, coords, level)textureLoadWith mip level (2D/3D/array).
texture_load(tex, coords, sample)textureLoadMultisample load.
texture_load_storage(tex, coords)textureLoadLoad from a storage texture (Read/ReadWrite).
texture_store(tex, coords, value)textureStoreWrite texel to a storage texture (Write/ReadWrite).
texture_dimensions(tex)textureDimensionsDimensions at mip 0.
texture_dimensions(tex, level)textureDimensionsDimensions at given mip level.
texture_num_layers(tex)textureNumLayersArray layer count.
texture_num_levels(tex)textureNumLevelsMip level count.
texture_num_samples(tex)textureNumSamplesSample count (multisample).

Storage texture access

texture_load_storage and texture_store are gated by the ReadableStorageAccess and WritableStorageAccess traits respectively, so the access mode of the storage texture is enforced at compile time:

FunctionTrait boundAccess modes allowed
texture_load_storage(tex, coords)ReadableStorageAccessRead, ReadWrite
texture_store(tex, coords, value)WritableStorageAccessWrite, ReadWrite

A separate texture_load_storage function (rather than overloading texture_load) is used because the WGSL storage overload of textureLoad takes no level parameter, unlike the sampled texture overload.

Example

#![allow(unused)]
fn main() {
#[wgsl]
pub mod texture_example {
    use wgsl_rs::std::*;

    texture!(group(0), binding(0), COLOR_TEX: Texture2D<f32>);
    sampler!(group(0), binding(1), COLOR_SMP: Sampler);

    pub struct FragmentInput {
        #[location(0)]
        pub uv: Vec2f,
    }

    #[fragment]
    pub fn fs_main(input: FragmentInput) -> Vec4f {
        let uv = input.uv;
        let color = texture_sample(COLOR_TEX, COLOR_SMP, uv);
        let dim = texture_dimensions(COLOR_TEX);
        color
    }
}
}

Overload resolution

Because Rust has no WGSL-style overload sets, each texture builtin is implemented as a distinct Rust function per texture kind, e.g. there is one texture_sample for Texture2D, another for Texture2DArray, another for Texture3D, another for TextureCube, and so on. The transpiler emits the WGSL textureSample builtin regardless of which Rust overload you called — the overload exists only to type-check on the CPU side.