显卡适配器(Adapter)
// Windows 系统可能有问题建议不填, low-power
const options = { 'powerPerference': 'high-performance' };
const adapter = await navigator.gpu.requestAdapter(options);
这段代码可以获取一个 “显卡适配器”
。他是对图形计算器的抽象,对应到具体的集成显卡、独显、甚至软件模拟的驱动。总之你可以用 WebGPU
提供的一套API干活儿了。
Chrome中在浏览器地址栏访问并开启可以看到更详细的 Adapter信息 chrome://flags/#enable-webgpu-developer-features
显卡设备(Device)
- 通过它操作具体的资源分配和使用、创建命令编码器、计算过程、渲染过程等。
const device = await adapter.requestDevice();
限制描述
maxBindGroups: 4
maxBindGroupsPlusVertexBuffers: 24
maxBindingsPerBindGroup: 1000
maxBufferSize: 4294967296
maxColorAttachmentBytesPerSample: 128
maxColorAttachments: 8
maxComputeInvocationsPerWorkgroup: 1024
maxComputeWorkgroupSizeX: 1024
maxComputeWorkgroupSizeY: 1024
maxComputeWorkgroupSizeZ: 64
maxComputeWorkgroupStorageSize: 32768
maxComputeWorkgroupsPerDimension: 65535
maxDynamicStorageBuffersPerPipelineLayout: 8
maxDynamicUniformBuffersPerPipelineLayout: 10
maxInterStageShaderVariables: 28
maxSampledTexturesPerShaderStage: 16
maxSamplersPerShaderStage: 16
maxStorageBufferBindingSize: 4294967292
maxStorageBuffersPerShaderStage: 10
maxStorageTexturesPerShaderStage: 8
maxTextureArrayLayers: 2048
maxTextureDimension1D: 16384
maxTextureDimension2D: 16384
maxTextureDimension3D: 2048
maxUniformBufferBindingSize: 65536
maxUniformBuffersPerShaderStage: 12
maxVertexAttributes: 30
maxVertexBufferArrayStride: 2048
maxVertexBuffers: 8
minStorageBufferOffsetAlignment: 256
minUniformBufferOffsetAlignment: 256
字段含义和代码片段
🔶 1. maxBindGroups: 4
表示一次渲染/计算管线中最多可绑定 4 个 bind group
。
const pipeline = device.createRenderPipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [bgLayout0, bgLayout1, bgLayout2, bgLayout3] // 最多 4 个
}),
});
🔶 2. maxBindGroupsPlusVertexBuffers: 24
bind groups
+ 顶点缓冲区总数 ≤ 24。
bind groups
+ 最多 20 顶点缓冲区(因为 maxVertexBuffers = 8
)
🔶 3. maxBindingsPerBindGroup: 1000
单个 bind group
内最多绑定 1000 个资源(buffer、texture、sampler)
// bind group layout 最多可声明 1000 个 binding
entries: new Array(1000).fill(...).map((_, i) => ({ binding: i, ... }))
🔶 4. maxBufferSize: 4GB
任意单个 GPU buffer 最大为 4GB(4294967296 bytes)。
const bigBuffer = device.createBuffer({
size: 4 * 1024 * 1024 * 1024, // 4GB
usage: GPUBufferUsage.STORAGE
});
🔶 5. maxColorAttachmentBytesPerSample: 128
指每个像素每个采样点最多可用 128 字节(如多个渲染目标叠加时)
🔶 6. maxColorAttachments: 8
render pass
最多同时绑定 8 个颜色附件(MRT:multi render target)
const renderPass = commandEncoder.beginRenderPass({
colorAttachments: new Array(8).fill(...).map(...)
});
🔶 7–11. maxComputeInvocationsPerWorkgroup / maxComputeWorkgroupSizeX/Y/Z
@compute @workgroup_size(256, 2, 2) // 256×2×2 = 1024(不能超过 1024)
fn main(...) { ... }
你可以每个 workgroup 中最多执行 1024 次,受 X/Y/Z 限制:
- X ≤ 1024
- Y ≤ 1024
- Z ≤ 64
🔶 12. maxComputeWorkgroupStorageSize: 32768
每个 compute shader
的 var<workgroup>
内存最多 32KB。
var<workgroup> sharedData: array<u32, 8192>;
// 8192 * 4 = 32KB
🔶 13. maxComputeWorkgroupsPerDimension: 65535
dispatch 的最大维度:
commandEncoder.dispatchWorkgroups(65535, 1, 1);
// 最大 X 可达 65535
🔶 14–15. maxDynamic{Storage,Uniform}BuffersPerPipelineLayout
动态 buffer offset 的最大数量(如 bind group 中用 dynamic offset)
entries: [
{ binding: 0, resource: { buffer, offset: 0 } }, // dynamic
...
]
🔶 16. maxInterStageShaderVariables: 28
顶点到片元之间最多传递 28 个变量(不能传太多 @location(i)
)
🔶 17–18. maxSampledTextures / maxSamplers per shader stage: 16
WGSL 中:
@group(0) @binding(0) var tex0: texture_2d<f32>;
...
@group(0) @binding(15) var tex15: texture_2d<f32>; // 最多 16
🔶 19. maxStorageBufferBindingSize: 4GB-4
存储缓冲区最大绑定大小 ≈ 4GB
🔶 20–21. maxStorageBuffers / maxStorageTextures per shader stage
每个 shader stage(vertex/fragment/compute)中:
- 最多绑定 10 个 storage buffer;
- 最多绑定 8 个 storage texture。
🔶 22–25. maxTextureDimension1D/2D/3D / maxTextureArrayLayers
表示最大纹理尺寸:
device.createTexture({
size: [16384, 16384, 1], // 2D 最大 16384×16384
format: 'rgba8unorm',
usage: GPUTextureUsage.TEXTURE_BINDING
});
🔶 26. maxUniformBufferBindingSize: 65536
单个 uniform buffer
最大为 64KB(统一限制)
device.createBuffer({ size: 64 * 1024, usage: GPUBufferUsage.UNIFORM })
🔶 27. maxUniformBuffersPerShaderStage: 12
每个 shader 阶段最多可用 12 个 uniform buffer
。
🔶 28–30. maxVertexAttributes / maxVertexBufferArrayStride / maxVertexBuffers
• 最多 30 个 @location(n) 顶点输入;
• 单个顶点缓冲 stride 最大 2048 字节;
• 最多 8 个顶点缓冲区:
attributes: [
{ shaderLocation: 0, offset: 0, format: 'float32' }, // ...
]
🔶 31–32. minStorageBufferOffsetAlignment / minUniformBufferOffsetAlignment: 256
动态 offset 必须是 256 的整数倍:
device.queue.writeBuffer(buffer, 0, data);
若动态绑定:
device.setBindGroup(0, bindGroup, [0, 256, 512]);
// 必须 256 对齐