textureGather
Function
Gather returns one channel from the four texels around a sampling coordinate without blending them into one value.
Compare examples
Follow the output order around the grid: upper left, upper right, lower right, lower left.
Model assumptionschannel = 0; uv = vec2(0.5, 0.5); level = 0
Example 1textureGather(0, tex, smp, uv)
0.75100.25 - Result
vec4(0.75, 1, 0.25, 0)
Example 2textureGather(0, tex, smp, uv)
1001 - Result
vec4(1, 0, 1, 0)
Example 3textureGather(0, tex, smp, uv)
0.50.50.50.5 - Result
vec4(0.5, 0.5, 0.5, 0.5)
The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.
When to use it
Use it to implement a custom image filter that needs individual neighbouring samples.
Inputs and limits
This model gathers an interior base-mip neighbourhood. Colour textures require a literal channel selector. Gather is WebGPU-only in this compiler target pair.
For shader input rules and precision, consult WGSL and GLSL ES 3.00.
Related functions
The signature, the sentence and any message are the compiler's own text at commit af54ddad5.
Reads one channel from the four texels a linear filter would blend at the coordinate on mip level 0, through the sampler smp, and returns them as a vec4 in the order (umin,vmax), (umax,vmax), (umax,vmin), (umin,vmin), in any stage.
Syntax
declare function textureGather<E extends TextureElem = f32>( component: number, tex: texture_2d<E>, smp: sampler, uv: vec2,): Vec4OfElem<E>declare function textureGather<E extends TextureElem = f32>( component: number, tex: texture_2d_array<E>, smp: sampler, uv: vec2, layer: number,): Vec4OfElem<E>declare function textureGather<E extends TextureElem = f32>( component: number, tex: texture_cube<E>, smp: sampler, dir: vec3,): Vec4OfElem<E>declare function textureGather<E extends TextureElem = f32>( component: number, tex: texture_cube_array<E>, smp: sampler, dir: vec3, layer: number,): Vec4OfElem<E>declare function textureGather(tex: texture_depth_2d, smp: sampler, uv: vec2): vec4declare function textureGather(tex: texture_depth_2d_array, smp: sampler, uv: vec2, layer: number): vec4declare function textureGather(tex: texture_depth_cube, smp: sampler, dir: vec3): vec4declare function textureGather(tex: texture_depth_cube_array, smp: sampler, dir: vec3, layer: number): vec4A number in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.
type f32 = number & { readonly [f32Tag]?: true }
A number you write as a type is inferred from its uses, and f32, i32 and u32 fix a width; the TypeScript constructs page carries that row with the compiler's emit.
A signature may name a type the ambient file declares for its own use. These are those declarations.
TextureElemtype TextureElem = f32 | i32 | u32Vec4OfElemtype Vec4OfElem<E> = typeof u32Tag extends keyof E ? vec4u : typeof i32Tag extends keyof E ? vec4i : vec4
Parameters
-
componentoptionalnumber -
textexture_2d<E>texture_2d_array<E>texture_cube<E>texture_cube_array<E>texture_depth_2dtexture_depth_2d_arraytexture_depth_cubetexture_depth_cube_array -
smpsampler -
uvoptionalvec2 -
diroptionalvec3 -
layeroptionalnumber
Return value
Vec4OfElem<E>vec4
Description
On a colour texture the first argument is the channel, a whole number from 0 to 3 written in the call; a depth texture has one channel and takes none; an array texture takes the layer after the coordinate. Compiles to textureGather on WGSL; its GLSL lowering is not supported yet (Rule 10.5), so a module using it emits WGSL alone.
Targets
- WGSL
-
textureGather(a, b, c, d) - GLSL ES 3.00
-
textureGatheris not supported yet on the WebGL2 tier (Rule 10.5)