textureSample
Function
Filtered sampling looks up a texture at normalized coordinates. A sampler controls interpolation and addressing; nearby fragments determine the mip level.
Related names in other languages
These related operations can differ in arguments, support, and semantics. The foreign names are not accepted TypeShade aliases. Check the target forms and restrictions.
- GLSL:
texture - GLSL:
texture2D
Compare examples
Compare a texel centre with the centre of the grid. Linear filtering blends the surrounding texels.
Model assumptionsfilter = linear; address = clamp-to-edge; level = 0
Example 1uv = vec2(0.25, 0.25)
0.75100.25 - Result
0
Example 2uv = vec2(0.5, 0.5)
0.75100.25 - Result
0.5
Example 3uv = vec2(0.75, 0.75)
0.75100.25 - Result
1
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 put an image on a surface while reducing aliasing as the image gets smaller.
Inputs and limits
This model uses a scalar channel, linear filtering, clamp-to-edge addressing, and the base mip. Real implicit sampling chooses detail from fragment derivatives.
For shader input rules and precision, consult WGSL and GLSL ES 3.00.
Related functions
- WGSL
textureSample(a, b, c)- GLSL ES 3.00
texture(a, c)
The signature, the sentence and any message are the compiler's own text at commit af54ddad5.
Samples a float texture through the sampler smp with the implicit level of detail, in the fragment stage only: at uv on a texture_2d, by a vec3 direction on a texture_cube, by a vec3 coordinate on a texture_3d, by one f32 on a texture_1d, and on an array texture the argument after the coordinate picks the layer.
Syntax
declare function textureSample(tex: texture_2d<f32>, smp: sampler, uv: vec2): vec4declare function textureSample( tex: texture_2d_array<f32>, smp: sampler, uv: vec2, layer: number,): vec4declare function textureSample(tex: texture_cube<f32>, smp: sampler, dir: vec3): vec4declare function textureSample(tex: texture_3d<f32>, smp: sampler, coord: vec3): vec4declare function textureSample(tex: texture_1d<f32>, smp: sampler, coord: number): vec4declare function textureSample( tex: texture_cube_array<f32>, 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.
Parameters
-
textexture_2d<f32>texture_2d_array<f32>texture_cube<f32>texture_3d<f32>texture_1d<f32>texture_cube_array<f32> -
smpsampler -
uvoptionalvec2 -
diroptionalvec3 -
coordoptionalvec3number -
layeroptionalnumber
Return value
Description
Compiles to textureSample on WGSL and texture on GLSL, where the layer is folded into a vec3 coordinate; the GLSL lowering of a texture_1d or a texture_cube_array is not supported yet (Rule 10.5), so a module using one emits WGSL alone and is not supported yet on the WebGL2 tier.
Targets
- WGSL
-
textureSample(a, b, c) - GLSL ES 3.00
-
texture(a, c)