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.

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.