SuperAPI AI 工具集

webgpu-wgsl-fragment-shaders

智能体与模型

查看项目

更新于 10-15 22:18

技能介绍

用途:编写 WGSL @fragment 入口点,使颜色、深度与覆盖输出与渲染管线匹配,避免目标不匹配或丢失 early-Z。WGSL 是 WebGPU 1.0-stable 的着色语言(Chrome 113+、Safari 26+、Firefox 141+)。片元着色器每个光栅化片元运行一次,标记为 @fragment,不能被其他 WGSL 代码调用。

主要能力:片元输入包括 @builtin(position)(帧缓冲空间像素坐标,非裁剪空间)、@builtin(front_facing)、@builtin(sample_index)、@builtin(sample_mask) 与 @location(n) 插值变量;输出包括每个管线 fragment.targets 条目对应的 @location(n) 颜色、@builtin(frag_depth) 深度和 @builtin(sample_mask)。@builtin(sample_mask) 是唯一既可输入又可输出的内建。

使用方式:入口点形如 @fragment fn fs(in : VSOut) -> @location(0) vec4<f32>。片元输入结构体与顶点阶段返回的结构体相同。每个 @location 颜色输出的类型与分量数必须匹配对应 GPUColorTargetState 的格式。多渲染目标需返回含 @location(0)、@location(1) 等成员的结构体。需要覆盖深度时在输出结构体加 @builtin(frag_depth) : f32;需要丢弃片元时调用 discard;需要逐采样着色时读取 @builtin(sample_index) 或声明 @interpolate(perspective, sample)。

适用场景:单颜色输出、G-buffer/延迟着色 MRT、光线步进/软粒子等深度覆盖、alpha 裁剪、MSAA 边缘抗锯齿。

限制:@location 输出数量、顺序或格式与管线 targets 不一致会导致管线创建失败;写 frag_depth 会为整个绘制禁用 early-Z;discard 只能用于 @fragment;front_facing 取决于管线 primitive.frontFace 而非仅绕序;片元阶段 @builtin(position) 不是裁剪空间;textureSample、dpdx、dpdy、fwidth 需在一致控制流中调用。参考文件与相关技能见资料,具体内容暂未提供。