textureSample
이 페이지에서

textureSample()

함수, 내장 함수 분류

Sample a 2D texture, returning vec4<f32>.

import { textureSample } from 'typeshade'

시그니처, 설명, 예제는 커밋 c66579bf의 컴파일러 소스에서 그대로 가져온 영어 원문입니다.

구문

function textureSample(
tex: ReadonlyNode<'texture_2d<f32>'>,
smp: ReadonlyNode<'sampler'>,
uv: ReadonlyNode<'vec2<f32>'>,
): Node<'vec4<f32>'>;
function textureSample(
tex: ReadonlyNode<'texture_2d_array<f32>'>,
smp: ReadonlyNode<'sampler'>,
uv: ReadonlyNode<'vec2<f32>'>,
layer: ReadonlyNode<'i32' | 'u32'> | number,
): Node<'vec4<f32>'>;

매개변수

tex ReadonlyNode<'texture_2d<f32>'>

the sampled texture binding, resource(name, texture2dfT, at).node.

smp ReadonlyNode<'sampler'>

the sampler binding to filter with.

uv ReadonlyNode<'vec2<f32>'>

normalised texture coordinates.

layer

which layer to read, required for an array texture and rejected otherwise.

반환값

Node<'vec4<f32>'>

the filtered texel.

예외

SD0109

That makes this call fragment-only, and the fragment-only-builtin lint rule, a core rule that fires at every emit, reports SD0109 when it appears in a vertex or compute stage. textureSampleLevel takes the level as an argument and is legal in every stage, so it is the form a vertex or compute shader reaches for.

설명

The level of detail is implicit: it comes from screen-space derivatives, which exist only in a fragment invocation. That makes this call fragment-only, and the fragment-only-builtin lint rule, a core rule that fires at every emit, reports SD0109 when it appears in a vertex or compute stage. textureSampleLevel takes the level as an argument and is legal in every stage, so it is the form a vertex or compute shader reaches for.

A 2D array texture uses the same name, and the first argument’s key picks the overload: a texture_2d_array<f32> requires the layer argument, and omitting it is a tsc error. A number layer lifts to an i32 literal. The targets spell the layer differently and the DSL absorbs that: WGSL takes it as its own argument, textureSample(t, s, uv, layer), while GLSL ES 3.00 folds it into the coordinate, texture(t, vec3(uv, float(layer))). Both spellings are core, so an array texture needs no capability on either target.

Integer texture keys (texture_2d<u32>, texture_2d<i32> and their array twins) are rejected at tsc, deliberately. Filtering is a weighted average, and interpolating integer texels has no meaning, so WGSL has no textureSample for them at all. GLSL’s texture(usampler2D, …) would compile, and accepting it would mint a construct that runs on WebGL2 and cannot be expressed on WebGPU. The surface both targets share for an integer texture is textureLoad, textureDimensions and textureNumLayers.

The CPU evaluation (compileModule) has no way to read a texture and returns a placeholder under its gpuStubs option.

예제

예제

import { fn, resource, textureSample, texture2dfT, samplerT, vec2fT, vec4fT } from 'typeshade'
const tex = resource('tex', texture2dfT, { group: 0, binding: 1 })
const smp = resource('tex_sampler', samplerT, { group: 0, binding: 2 })
const fs = fn('fs_main', { uv: vec2fT }, vec4fT, ({ uv }) => textureSample(tex.node, smp.node, uv), {
stage: 'fragment',
})

대상별 지원

대상지원비고
WGSL (WebGPU) 지원

textureSample(tex, smp, uv).

GLSL ES 3.00 (WebGL2) 지원

texture(tex, uv).

CPU 오라클 스텁

The oracle has no texture memory and no neighbouring fragments. The call throws unless the module was compiled with { gpuStubs: true }, which returns a placeholder.

관련 가이드

함께 보기

소스

src/core/ir/node.ts, 2049행, 커밋 c66579bf 기준

이 페이지 편집 문제 보고