从 WebGL 到 WebGPU

原文标题:WebGPU from WebGL
原作者:webgpufundamentals Contributors

这篇写给已经熟悉 WebGL、想开始用 WebGPU 的开发者。

如果你从 WebGL 转到 WebGPU,会发现很多概念是通的。两边都能在 GPU 上跑小函数。WebGL 有顶点着色器(vertex shader)和片元着色器(fragment shader);WebGPU 多了个计算着色器(compute shader)。WebGL 用 GLSL 当着色语言,WebGPU 用 WGSL。语言不一样,核心概念大体相同。

两个 API 都有属性(attribute)——从缓冲区(buffer)里取数据,喂给顶点着色器的每次迭代;都有统一变量(uniform)——着色器函数所有迭代共享的值;都有可变变量(varying)——从顶点着色器传到片元着色器,光栅化时插值;都有纹理(texture)和采样器(sampler)——放 2D/3D 数据并采样(把多个像素过滤成一个值);都能渲染到纹理;还有一大堆像素混合(blending)、深度缓冲(depth buffer)、模板缓冲(stencil buffer)的设置。

最大的区别在:WebGL 是有状态的 API,WebGPU 不是。WebGL 里到处都是全局状态:当前绑定的纹理、缓冲区、程序,还有混合、深度、模板设置。调 gl.bindBuffer、gl.enable、gl.blendFunc 这些 API 来设,设完就一直全局生效,直到被改。

WebGPU 几乎没有全局状态。换来的是渲染管线(render pipeline)和渲染通道(render pass),WebGL 里原本全局的那堆状态,基本都收进这两个东西里了。纹理、属性、缓冲区以及别的设置都在里面。没显式设的选项走默认值。管线创建完就改不了——是不可变(immutable)对象,想换设置只能新建一条管线。渲染通道也有状态,但只在这个通道内部有效。

第二个大区别是 WebGPU 更底层。WebGL 里很多东西靠名字连起来。比如在 GLSL 里声明一个 uniform,再查它的位置:

loc = gl.getUniformLocation(program, "nameOfUniform");

再比如 varying:顶点着色器里写 varying vec2 v_texcoord 或者 out vec2 v_texcoord,片元着色器里声明对应的 varying,名字也叫 v_texcoord。好处是拼错了会报错。

而 WebGPU 完全靠索引或字节偏移来连接。你不会像 WebGL 那样建单独的 uniform,而是声明 uniform 块(uniform block,也就是声明 uniform 的结构体)。然后传给着色器的数据怎么组织才能对上这个结构,得你自己管。注意:WebGL2 也有类似概念(Uniform Blocks),但 WebGL2 还支持按名字访问 uniform;就算需要字节偏移,WebGL2 也能查偏移量和块位置。WebGPU 则是一切都靠字节偏移或索引(常说的“位置 location”),而且没有查询 API,全靠手动同步。

用 JavaScript 打个比方:

function likeWebGL(inputs) {
  const {position, texcoords, normal, color} = inputs;
  ...
}

function likeWebGPU(inputs) {
  const [position, texcoords, normal, color] = inputs;
  ...
}

likeWebGL 按名字连,参数顺序无所谓,缺参数也能跳;likeWebGPU 按数组索引传,每个参数的索引位置你必须知道。WebGPU 里,WGSL 内部和 JavaScript/WASM 外部的位置同步,全靠你自己。

其他显著差异

  • 画布(Canvas)
    WebGL 帮你管画布,建上下文时选抗锯齿、preserveDrawingBuffer、模板、深度、alpha,之后只管设 canvas.width 和 canvas.height。
    WebGPU 得自己干大部分活:深度缓冲自己建(可以带模板缓冲),抗锯齿自己建多重采样纹理再解析到画布纹理。但换来的好处是,一个 WebGPU 设备能渲染到多个画布。

  • WebGPU 不生成 mipmap
    WebGL 里调 gl.generateMipmap 就能自动生成所有 mip 层级;WebGPU 没这个函数,得自己生成(参考本文)。

  • WebGPU 强制要求采样器(sampler)
    WebGL1 里没有采样器概念(内部处理),WebGL2 可选,WebGPU 必须有。

  • 缓冲区和纹理不可调整大小
    WebGL 能在运行时重新分配缓冲区或纹理;WebGPU 里纹理和缓冲区的大小、用途、格式都不可变,只能改内容。要改尺寸,就得销毁旧的、建新的。

着色器对比

下面这个着色器画一个带纹理、受光照的三角形,GLSL 和 WGSL 对照着看:

// GLSL
const vSrc = `
uniform mat4 u_worldViewProjection;
uniform mat4 u_worldInverseTranspose;

attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec2 a_texcoord;

varying vec2 v_texCoord;
varying vec3 v_normal;

void main() {
  gl_Position = u_worldViewProjection * a_position;
  v_texCoord = a_texcoord;
  v_normal = (u_worldInverseTranspose * vec4(a_normal, 0)).xyz;
}
`;

const fSrc = `
precision highp float;

varying vec2 v_texCoord;
varying vec3 v_normal;

uniform sampler2D u_diffuse;
uniform vec3 u_lightDirection;

void main() {
  vec4 diffuseColor = texture2D(u_diffuse, v_texCoord);
  vec3 a_normal = normalize(v_normal);
  float l = dot(a_normal, u_lightDirection) * 0.5 + 0.5;
  gl_FragColor = vec4(diffuseColor.rgb * l, diffuseColor.a);
}
`;
// WGSL
const shaderSrc = `
struct VSUniforms {
  worldViewProjection: mat4x4f,
  worldInverseTranspose: mat4x4f,
};
@group(0) binding(0) `var<uniform>` vsUniforms: VSUniforms;

struct MyVSInput {
    @location(0) position: vec4f,
    @location(1) normal: vec3f,
    @location(2) texcoord: vec2f,
};

struct MyVSOutput {
  @builtin(position) position: vec4f,
  @location(0) normal: vec3f,
  @location(1) texcoord: vec2f,
};

@vertex
fn myVSMain(v: MyVSInput) -> MyVSOutput {
  var vsOut: MyVSOutput;
  vsOut.position = vsUniforms.worldViewProjection * v.position;
  vsOut.normal = (vsUniforms.worldInverseTranspose * vec4f(v.normal, 0.0)).xyz;
  vsOut.texcoord = v.texcoord;
  return vsOut;
}

struct FSUniforms {
  lightDirection: vec3f,
};

@group(0) binding(1) `var<uniform>` fsUniforms: FSUniforms;
@group(0) binding(2) var diffuseSampler: sampler;
@group(0) binding(3) var diffuseTexture: `texture_2d<f32>`;

@fragment
fn myFSMain(v: MyVSOutput) -> @location(0) vec4f {
  var diffuseColor = textureSample(diffuseTexture, diffuseSampler, v.texcoord);
  var a_normal = normalize(v.normal);
  var l = dot(a_normal, fsUniforms.lightDirection) * 0.5 + 0.5;
  return vec4f(diffuseColor.rgb * l, diffuseColor.a);
}
`;

两者核心很像,vec4 变 vec4f,mat4 变 mat4x4f,别的像 int→i32,uint→u32,ivec2→vec2i,uvec3→vec3u。GLSL 风格像 C/C++,WGSL 像 Rust:类型在 GLSL 里放左边,在 WGSL 里放右边。WGSL 支持类型推断,GLSL 必须显式写类型。

最大的区别是 @??? 注解,它们把数据来源写明。uniform 的 @group(?) @binding(?) 得保证不冲突。属性用 @location 指定,必须手动给。varying 在 WGSL 里靠结构体定义,每个字段指定位置。片元着色器的输入结构不用跟顶点输出结构同名,位置对上就行。gl_Position 在 WGSL 里对应 @builtin(position),片元输出用 @location(0) 标。

获取 API

// WebGL
function main() {
  const gl = document.querySelector("canvas").getContext("webgl");
  if (!gl) {
    fail("need webgl");
    return;
  }
}
main();
// WebGPU
async function main() {
  const adapter = await navigator.gpu?.requestAdapter();
  const device = await adapter?.requestDevice();
  if (!device) {
    fail("need a browser that supports WebGPU");
    return;
  }
}
main();

adapter 代表 GPU 本身,device 代表这个 GPU 上的 API 实例。最大的区别是 WebGPU 拿 API 是异步的。

创建缓冲区

// WebGL
function createBuffer(gl, data, type = gl.ARRAY_BUFFER) {
  const buf = gl.createBuffer();
  gl.bindBuffer(type, buf);
  gl.bufferData(type, data, gl.STATIC_DRAW);
  return buf;
}
const positionBuffer = createBuffer(gl, positions);
const normalBuffer = createBuffer(gl, normals);
const texcoordBuffer = createBuffer(gl, texcoords);
const indicesBuffer = createBuffer(gl, indices, gl.ELEMENT_ARRAY_BUFFER);
// WebGPU
function createBuffer(device, data, usage) {
  const buffer = device.createBuffer({
    size: data.byteLength,
    usage,
    mappedAtCreation: true,
  });
  const dst = new data.constructor(buffer.getMappedRange());
  dst.set(data);
  buffer.unmap();
  return buffer;
}
const positionBuffer = createBuffer(device, positions, GPUBufferUsage.VERTEX);
const normalBuffer = createBuffer(device, normals, GPUBufferUsage.VERTEX);
const texcoordBuffer = createBuffer(device, texcoords, GPUBufferUsage.VERTEX);
const indicesBuffer = createBuffer(device, indices, GPUBufferUsage.INDEX);

两边差别不大,主要是调的函数不一样。

创建纹理与采样器

// WebGL
const tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 2, 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([...]));
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
// WebGPU
const tex = device.createTexture({
  size: [2, 2],
  format: 'rgba8unorm',
  usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
});
device.queue.writeTexture(
  { texture: tex },
  new Uint8Array([...]),
  { bytesPerRow: 8, rowsPerImage: 2 },
  { width: 2, height: 2 },
);
const sampler = device.createSampler({
  magFilter: 'nearest',
  minFilter: 'nearest',
});

WebGPU 得设纹理用途标志,而且必须显式创建采样器。

编译着色器

// WebGL
const vs = createShader(gl, gl.VERTEX_SHADER, vSrc);
const fs = createShader(gl, gl.FRAGMENT_SHADER, fSrc);
// WebGPU
const shaderModule = device.createShaderModule({ code: shaderSrc });

WebGL 得手动查编译状态、取错误日志;WebGPU 大多数实现会把错误打到 JavaScript 控制台,省事多了。

链接程序 / 设置管线

WebGL 里着色器链接、顶点属性设置、图元类型、深度缓冲使用,散在不同调用里;WebGPU 把它们合并到渲染管线的创建过程。

// WebGL 简化
const program = createProgram(gl, vs, fs);
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.vertexAttribPointer(positionLoc, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(positionLoc);
// ... 其他属性
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.CULL_FACE);
// WebGPU
const pipeline = device.createRenderPipeline({
  layout: "auto",
  vertex: {
    module: shaderModule,
    buffers: [
      {
        arrayStride: 12,
        attributes: [{ shaderLocation: 0, offset: 0, format: "float32x3" }],
      },
      {
        arrayStride: 12,
        attributes: [{ shaderLocation: 1, offset: 0, format: "float32x3" }],
      },
      {
        arrayStride: 8,
        attributes: [{ shaderLocation: 2, offset: 0, format: "float32x2" }],
      },
    ],
  },
  fragment: {
    module: shaderModule,
    targets: [{ format: presentationFormat }],
  },
  primitive: { topology: "triangle-list", cullMode: "back" },
  depthStencil: {
    depthWriteEnabled: true,
    depthCompare: "less",
    format: "depth24plus",
  },
  ...(canvasInfo.sampleCount > 1 && {
    multisample: { count: canvasInfo.sampleCount },
  }),
});

注意 createRenderPipeline 是个比较慢的调用,内部可能还会调整着色器。顶点缓冲区布局在这儿声明,实际缓冲区等绘制时才绑。想画不同图元(三角形或线)、或者不同顶点布局,就得建多条管线。

准备 uniform

// WebGL
const u_lightDirectionLoc = gl.getUniformLocation(program, "u_lightDirection");
const u_diffuseLoc = gl.getUniformLocation(program, "u_diffuse");
const u_worldInverseTransposeLoc = gl.getUniformLocation(
  program,
  "u_worldInverseTranspose",
);
const u_worldViewProjectionLoc = gl.getUniformLocation(
  program,
  "u_worldViewProjection",
);
// WebGPU
const vUniformBufferSize = 2 * 16 * 4; // 2 mat4s * 16 floats * 4 bytes
const fUniformBufferSize = 3 * 4; // 1 vec3 * 3 floats * 4 bytes

const vsUniformBuffer = device.createBuffer({
  size: vUniformBufferSize,
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const fsUniformBuffer = device.createBuffer({
  size: fUniformBufferSize,
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const vsUniformValues = new Float32Array(2 * 16);
const worldViewProjection = vsUniformValues.subarray(0, 16);
const worldInverseTranspose = vsUniformValues.subarray(16, 32);
const fsUniformValues = new Float32Array(3);
const lightDirection = fsUniformValues.subarray(0, 3);

WebGPU 没有查询 uniform 位置的 API,字节偏移和大小都得自己算。

准备绘制:绑定组与渲染通道描述符

// WebGPU
const bindGroup = device.createBindGroup({
  layout: pipeline.getBindGroupLayout(0),
  entries: [
    { binding: 0, resource: vsUniformBuffer },
    { binding: 1, resource: fsUniformBuffer },
    { binding: 2, resource: sampler },
    { binding: 3, resource: tex },
  ],
});

绑定组里的 binding 必须跟着色器里的 @binding 对上。

渲染通道描述符:

// WebGPU
const renderPassDescriptor = {
  colorAttachments: [
    {
      clearValue: [0.5, 0.5, 0.5, 1],
      loadOp: "clear",
      storeOp: "store",
    },
  ],
  depthStencilAttachment: {
    depthClearValue: 1,
    depthLoadOp: "clear",
    depthStoreOp: "store",
  },
};

WebGL 里渲染到画布,这些设置 API 自动处理;WebGPU 里就算渲染到画布,也得自己描述。

设置 uniform 数据

// WebGL
gl.uniform3fv(u_lightDirectionLoc, v3.normalize([1, 8, -10]));
gl.uniform1i(u_diffuseLoc, 0);
gl.uniformMatrix4fv(
  u_worldInverseTransposeLoc,
  false,
  m4.transpose(m4.inverse(world)),
);
gl.uniformMatrix4fv(
  u_worldViewProjectionLoc,
  false,
  m4.multiply(viewProjection, world),
);
// WebGPU
m4.transpose(m4.inverse(world), worldInverseTranspose);
m4.multiply(viewProjection, world, worldViewProjection);
v3.normalize([1, 8, -10], lightDirection);

device.queue.writeBuffer(vsUniformBuffer, 0, vsUniformValues);
device.queue.writeBuffer(fsUniformBuffer, 0, fsUniformValues);

调整绘制缓冲区大小

WebGL 只要设 canvas.width 和 canvas.height;WebGPU 得手动管纹理和深度缓冲的销毁和重建。

// WebGL
function resizeCanvasToDisplaySize(canvas) {
  const width = canvas.clientWidth;
  const height = canvas.clientHeight;
  const needResize = width !== canvas.width || height !== canvas.height;
  if (needResize) {
    canvas.width = width;
    canvas.height = height;
  }
  return needResize;
}
// WebGPU 简化
function resizeToDisplaySize(device, canvasInfo) {
  const {
    canvas,
    context,
    renderTarget,
    presentationFormat,
    depthTexture,
    sampleCount,
  } = canvasInfo;
  const width = Math.max(
    1,
    Math.min(device.limits.maxTextureDimension2D, canvas.clientWidth),
  );
  const height = Math.max(
    1,
    Math.min(device.limits.maxTextureDimension2D, canvas.clientHeight),
  );
  const needResize =
    !canvasInfo.renderTarget ||
    width !== canvas.width ||
    height !== canvas.height;
  if (needResize) {
    if (renderTarget) renderTarget.destroy();
    if (depthTexture) depthTexture.destroy();
    canvas.width = width;
    canvas.height = height;
    if (sampleCount > 1) {
      const newRenderTarget = device.createTexture({
        size: [canvas.width, canvas.height],
        format: presentationFormat,
        sampleCount,
        usage: GPUTextureUsage.RENDER_ATTACHMENT,
      });
      canvasInfo.renderTarget = newRenderTarget;
      canvasInfo.renderTargetView = newRenderTarget.createView();
    }
    const newDepthTexture = device.createTexture({
      size: [canvas.width, canvas.height],
      format: "depth24plus",
      sampleCount,
      usage: GPUTextureUsage.RENDER_ATTACHMENT,
    });
    canvasInfo.depthTexture = newDepthTexture;
    canvasInfo.depthTextureView = newDepthTexture.createView();
  }
  return needResize;
}

WebGPU 还得查硬件限制(比如最大纹理尺寸),内存不够的情况也得自己处理(可以用 pushErrorScope('out-of-memory') 异步抓)。

绘制

// WebGL 简化
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// 绑定纹理、缓冲区、设置 uniform 等
gl.drawElements(gl.TRIANGLES, 6 * 6, gl.UNSIGNED_SHORT, 0);
// WebGPU
if (canvasInfo.sampleCount === 1) {
  const colorTexture = context.getCurrentTexture();
  renderPassDescriptor.colorAttachments[0].view = colorTexture.createView();
} else {
  renderPassDescriptor.colorAttachments[0].view = canvasInfo.renderTargetView;
  renderPassDescriptor.colorAttachments[0].resolveTarget = context
    .getCurrentTexture()
    .createView();
}
renderPassDescriptor.depthStencilAttachment.view = canvasInfo.depthTextureView;

const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(pipeline);
passEncoder.setBindGroup(0, bindGroup);
passEncoder.setVertexBuffer(0, positionBuffer);
passEncoder.setVertexBuffer(1, normalBuffer);
passEncoder.setVertexBuffer(2, texcoordBuffer);
passEncoder.setIndexBuffer(indicesBuffer, "uint16");
passEncoder.drawIndexed(indices.length);
passEncoder.end();
device.queue.submit([commandEncoder.finish()]);

WebGPU 里得更新渲染通道描述符的纹理视图,建命令编码器(command encoder),开渲染通道,设管线、绑定组、顶点缓冲区、索引缓冲区,最后提交命令队列。这里要留神命令队列(device.queue):所有上传和提交都走队列,同一个命令编码器里两次绘制之间不能改缓冲区。

绘制多个物体

WebGPU 里画多个物体,一般每个物体都得建独立的 uniform 缓冲区(还有对应的绑定组),因为 uniform 缓冲区不能在命令编码器中间更新。

// WebGPU 多物体初始化简化
const numObjects = 100;
const objectInfos = [];
for (let i = 0; i < numObjects; ++i) {
  const vsUniformBuffer = device.createBuffer({
    size: Math.max(16, vUniformBufferSize),
    usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
  });
  const vsUniformValues = new Float32Array(2 * 16);
  const worldViewProjection = vsUniformValues.subarray(0, 16);
  const worldInverseTranspose = vsUniformValues.subarray(16, 32);
  const bindGroup = device.createBindGroup({
    layout: pipeline.getBindGroupLayout(0),
    entries: [
      { binding: 0, resource: vsUniformBuffer },
      { binding: 1, resource: fsUniformBuffer },
      { binding: 2, resource: sampler },
      { binding: 3, resource: tex },
    ],
  });
  // 计算位置并保存引用
  objectInfos.push({
    vsUniformBuffer,
    vsUniformValues,
    worldViewProjection,
    worldInverseTranspose,
    bindGroup,
    translation: [x, y, 0],
  });
}

渲染循环里,给每个物体更新它自己的 uniform 缓冲区,再编码绘制命令:

// WebGPU 渲染循环简化
passEncoder.setPipeline(pipeline);
passEncoder.setVertexBuffer(0, positionBuffer);
passEncoder.setVertexBuffer(1, normalBuffer);
passEncoder.setVertexBuffer(2, texcoordBuffer);
passEncoder.setIndexBuffer(indicesBuffer, 'uint16');

const projection = mat4.perspective(...);
const view = mat4.lookAt(...);
const viewProjection = mat4.multiply(projection, view);
vec3.normalize([1, 8, -10], lightDirection);
device.queue.writeBuffer(fsUniformBuffer, 0, fsUniformValues);

objectInfos.forEach(({ vsUniformBuffer, vsUniformValues, worldViewProjection, worldInverseTranspose, bindGroup, translation }, ndx) => {
  passEncoder.setBindGroup(0, bindGroup);
  const world = mat4.translation(translation);
  mat4.rotateX(world, time * 0.9 + ndx, world);
  mat4.rotateY(world, time + ndx, world);
  mat4.transpose(mat4.inverse(world), worldInverseTranspose);
  mat4.multiply(viewProjection, world, worldViewProjection);
  device.queue.writeBuffer(vsUniformBuffer, 0, vsUniformValues);
  passEncoder.drawIndexed(indices.length);
});
passEncoder.end();
device.queue.submit([commandEncoder.finish()]);

核心点是:跟 WebGL 不一样,WebGPU 里每个物体特有的 uniform 都要独立的 uniform 缓冲区,所以可能也需要独立的绑定组。

其他细节差异

  • Z 裁剪空间:WebGL 是 -1 到 +1;WebGPU 是 0 到 1。
  • 帧缓冲/视口坐标系 Y 轴向下:跟 WebGL 相反,虽然裁剪空间里 Y 轴向上(这点相同)。
  • WGSL 内建变量:@builtin(position) 对应 gl_Position;@builtin(vertex_index) 对应 gl_VertexID;@builtin(instance_index) 对应 gl_InstanceID;@builtin(position) 也用在片元着色器里对应 gl_FragCoord,但方向跟 WebGL 相反(左上为原点)。
  • 点与线的宽度:WebGPU 只支持 1 像素宽的点与线。其实大多数 WebGL 实现也不支持更宽的线,但 WebGL 一般支持更大的点(上限 64 像素)。WebGPU 卡死这个限制,对写可移植方案是好事。
  • WebGPU 优化方式不同于 WebGL:直接把 WebGL 应用转成 WebGPU,可能跑得更慢。想拿到 WebGPU 的性能优势,得重新组织数据、优化绘制方式(看优化文章)。

关键的三点

从 WebGL 迁到 WebGPU,这三个方向特别关键:

利用管线不可变性,预创建并复用管线。WebGPU 渲染管线(pipeline)创建成本高,还改不了。不同顶点布局、图元类型、混合/深度设置都得独立管线。最好在初始化阶段就把需要的管线变体全建好,别在渲染循环里动态创建。

显式管理资源生命周期与内存布局,避免运行时重新分配。缓冲区、纹理的大小和格式不可变,mipmap 得自己生成,uniform 数据必须按字节偏移手动排。所以最好预先分配够大的缓冲区,用池化或者提前销毁重建的方式管资源,同时保持 WGSL 和 JavaScript 之间的位置/偏移同步,减少运行时开销。

重新组织绘制数据,减少绑定组与命令提交开销。WebGPU 里每个物体特有的 uniform 都要独立缓冲区,对应独立绑定组。尽量合并共享数据(比如光照方向、投影矩阵),也可以考虑用一大块 uniform buffer 配动态偏移,或者用存储缓冲区(storage buffer)批量处理,把绑定组切换和命令队列提交的次数压下来。


术语对照

翻译时涉及的核心术语都按行业标准处理:

英文术语中文翻译备注
vertex shader顶点着色器
fragment shader片元着色器亦译“片段着色器”
compute shader计算着色器
uniform统一变量 / 统一值常见音译“uniform”
attribute顶点属性
varying可变变量 / 插值变量
texture纹理
sampler采样器
buffer缓冲区
render pipeline渲染管线
render pass渲染通道
bind group绑定组
depth buffer深度缓冲
stencil buffer模板缓冲
blending混合
mipmap多级渐远纹理 / mipmap保留英文更常见
framebuffer帧缓冲
viewport视口
draw call绘制调用
command encoder命令编码器
uniform block统一变量块
location位置(索引)
byte offset字节偏移
topology图元拓扑
cull mode剔除模式
multisample多重采样
resolve解析

代码里的英文注释和标识符都保持原样,符合技术文章惯例。