什么是纹素(texel) ?
纹素(texle)是一个标量或向量,用作纹理中最小的独立可访问单元, texel是纹理元素的缩写。
纹理是一组支持渲染所需的特殊操作的纹素(texle)集合。在WGSL中,这些操作是通过纹理内置函数调用的。
纹理的概念
WGSL 中的纹理关联到 WebGPU GPUTexture;
纹理既可以是数组形式,又可以是非数组形式:
非数组的纹理是纹素网格,每个纹素有唯一的网格坐标;
数组型的纹理是同质的纹素网格数组,在数组纹理中,每个纹素都通过其阵列索引和网格坐标的唯一组合来识别。
纹理有以下特性:
纹素格式;
维度;
大小;
细节层级数;
数组型的;
数组大小;
寻址模式;
滤波模式;
LOD 截取;
比较;
最大各向异性值;
采样器相关的概念
采样器是一个不透明的手柄,控制如何从被采样的纹理中访问纹素(texle);
WGSL中的采样器关联到 WebGPU中的 GPUSampler 对象;
纹素的访问由采样器的参数进行控制:
寻址模式;
滤波模式;
LOD 截取;
比较;
最大各向异性;
采样器只能由 WebGPU API 进行创建;
在shader 中不能够修改 采样器句柄!
纹素格式
纹素格式:
在WGSL中,某些纹理类型通过纹素(texel)格式进行参数化的, 纹素格式的特点:
通道;
通道格式;
每个纹素格式 与 WebGPU 中的 GPUTextureFormat 有一样的名字;
Channel Formats
Channel format | Number of stored bits | Interpretation of stored bits | Shader type | Shader value (Channel Transfer Function) |
8unorm | 8 | unsigned integer v ∈ {0,...,255} | f32 | v ÷ 255 |
8snorm | 8 | signed integer v ∈ {-128,...,127} | f32 | max(-1, v ÷ 127) |
8uint | 8 | unsigned integer v ∈ {0,...,255} | u32 | v |
8sint | 8 | signed integer v ∈ {-128,...,127} | i32 | v |
16uint | 16 | unsigned integer v ∈ {0,...,65535} | u32 | v |
16sint | 16 | signed integer v ∈ {-32768,...,32767} | i32 | v |
16float | 16 | IEEE-754 binary16 16-bit floating point value v, with 1 sign bit, 5 exponent bits, 10 mantissa bits | f32 | v |
32uint | 32 | 32-bit unsigned integer value v | u32 | v |
32sint | 32 | 32-bit signed integer value v | i32 | v |
32float | 32 | IEEE-754 binary32 32-bit floating point value v | f32 | v |
Texel Formats for Storage Textures
Texel format | Channel format | Channels in memory order | Corresponding shader value |
rgba8unorm | 8unorm | r, g, b, a | vec4<f32>(CTF(r), CTF(g), CTF(b), CTF(a)) |
rgba8snorm | 8snorm | r, g, b, a | vec4<f32>(CTF(r), CTF(g), CTF(b), CTF(a)) |
rgba8uint | 8uint | r, g, b, a | vec4<u32>(CTF(r), CTF(g), CTF(b), CTF(a)) |
rgba8sint | 8sint | r, g, b, a | vec4<i32>(CTF(r), CTF(g), CTF(b), CTF(a)) |
rgba16uint | 16uint | r, g, b, a | vec4<u32>(CTF(r), CTF(g), CTF(b), CTF(a)) |
rgba16sint | 16sint | r, g, b, a | vec4<i32>(CTF(r), CTF(g), CTF(b), CTF(a)) |
rgba16float | 16float | r, g, b, a | vec4<f32>(CTF(r), CTF(g), CTF(b), CTF(a)) |
r32uint | 32uint | r | vec4<u32>(CTF(r), 0u, 0u, 1u) |
r32sint | 32sint | r | vec4<i32>(CTF(r), 0, 0, 1) |
r32float | 32float | r | vec4<f32>(CTF(r), 0.0, 0.0, 1.0) |
rg32uint | 32uint | r, g | vec4<u32>(CTF(r), CTF(g), 0.0, 1.0) |
rg32sint | 32sint | r, g | vec4<i32>(CTF(r), CTF(g), 0.0, 1.0) |
rg32float | 32float | r, g | vec4<f32>(CTF(r), CTF(g), 0.0, 1.0) |
rgba32uint | 32uint | r, g, b, a | vec4<u32>(CTF(r), CTF(g), CTF(b), CTF(a)) |
rgba32sint | 32sint | r, g, b, a | vec4<i32>(CTF(r), CTF(g), CTF(b), CTF(a)) |
rgba32float | 32float | r, g, b, a | vec4<f32>(CTF(r), CTF(g), CTF(b), CTF(a)) |
bgra8unorm | 8unorm | b, g, r, a | vec4<f32>(CTF(r), CTF(g), CTF(b), CTF(a)) |
采样纹理类型
普通纹理类型
texture_1d<type>
texture_2d<type>
texture_2d_array<type>
texture_3d<type>
texture_cube<type>
texture_cube_array<type>
(1) type : f32, i32, u32;
(2)图像的参数化类型是采样转换后的类型。例如,你可以有一个具有8位uniform组件的texel的图像,但当你对它们进行采样时,你会得到一个32位浮点结果(或f32向量)。
多重采样纹理类型
texture_multisampled_2d<type>
type: f32, i32, u32;
用在反走样上的采样类型。
外部采样纹理类型
texture_external
外部采样纹理是一个不透明的二维浮点采样纹理类型,类似于texture_2d<f32>,但可能有不同的表示。它可以使用textureLoad或textureSampleBaseClampToEdge内置函数来读取。
存储纹理类型
texture_storage_1d<texel_format, access>
texture_storage_2d<texel_format, access>
texture_storage_2d_array<texel_foramt ,access>
texture_storage-3d<texel_format, access>
存储纹理支持不使用采样器就可以访问单个纹素(texel)的操作。
只写属性的存储纹理支持写入单个texel,并自动将着色器值转换为存储的texel值。
存储纹理必须由一种存储纹理的texel格式参数化,texel格式决定了转换功能。
对于只写存储的纹理,转换函数的逆函数用于将着色器值转换为存储的texel。
texel_format是一个上下文相关的名称,必须是storage-texel-formats中指定的texel类型之一。
access是上下文相关的名称,必须是write。
深度纹理类型:
texture_depth_2d;
texture_depth_2d_array;
texture_depth_cube;
texture_depth_cube_array;
texture_depth_multisampled_2d;
采样器类型
采样器通过执行以下操作来调节对采样纹理或深度纹理的访问:
坐标变换;
可选地修改mip级选择;
对于采样纹理,可选地过滤检索到的texel值;
对于深度纹理,确定应用于检索texel的比较函数;
只有两种类型:
sampler;
sampler_comparision;
采样器在WebGPU API中创建时是参数化的。它们不能被WGSL程序修改。
纹理和采样类型总结:
sampler_type :
sampled_texture_type :
multisampled_texture_type :
storage_texture_type :
depth_texture_type :