GPUTexture: createView() 方法
注意:此功能在 Web Workers 中可用。
GPUTexture 接口的 createView() 方法创建一个 GPUTextureView 对象,该对象表示 GPUTexture 的特定视图。
语法
createView()
createView(descriptor)
参数
descriptor可选-
包含以下属性的对象:
arrayLayerCount可选-
一个数字,定义视图可访问的数组层数,从
baseArrayLayer值开始。如果省略
arrayLayerCount,则会按如下方式给定值:- 如果
dimension是"1d"、"2d"或"3d",则arrayLayerCount为 1。 - 如果
dimension是"cube",则arrayLayerCount为 6。 - 如果
dimension是"2d-array"或"cube-array",则arrayLayerCount为GPUTexture.depthOrArrayLayers-baseArrayLayer。
- 如果
aspect可选-
一个枚举值,指定纹理的哪个(哪些)方面对纹理视图是可访问的。可能的值为:
"all"-
纹理格式的所有可用方面都将对视图可访问,这可能意味着颜色、深度和模板,具体取决于您处理的是哪种格式。
"depth-only"-
只有 深度或模板格式 的深度方面对视图可访问。
"stencil-only"-
只有深度或模板格式的模板方面对视图可访问。
如果省略,
aspect的值为"all"。 baseArrayLayer可选-
一个数字,定义视图可访问的第一个数组层的索引。如果省略,
baseArrayLayer的值为 0。 baseMipLevel可选-
一个数字,表示视图可访问的第一个(最详细的)mipmap 级别。如果省略,
baseMipLevel的值为 0。 dimension可选-
一个枚举值,指定将纹理视为哪种格式。可能的值为:
"1d":将纹理视为一维图像。"2d":将纹理视为单个二维图像。"2d-array":将纹理视为二维图像数组。"cube":将纹理视为立方体贴图。视图有 6 个数组层,对应于立方体的[+X, -X, +Y, -Y, +Z, -Z]面。采样在立方体贴图的面之间无缝进行。"cube-array":将纹理视为 N 个立方体贴图的打包数组,每个立方体贴图有 6 个数组层,对应于立方体的[+X, -X, +Y, -Y, +Z, -Z]面。采样在立方体贴图的面之间无缝进行。"3d":将纹理视为三维图像。
如果省略
dimension,则会按如下方式给定值:- 如果
GPUTexture.dimension是"1d",则dimension为"1d"。 - 如果
GPUTexture.dimension是"2d"且GPUTexture.depthOrArrayLayers为 1,则dimension为"2d"。 - 如果
GPUTexture.dimension是"2d"且GPUTexture.depthOrArrayLayers大于 1,则dimension为"2d-array"。 - 如果
GPUTexture.dimension是"3d",则dimension为"3d"。
format可选-
一个枚举值,指定纹理视图的格式。有关所有可能值的详细信息,请参阅规范中的 纹理格式 部分。
如果省略
format,则会按如下方式给定值:- 如果
aspect是"depth-only"或"stencil-only",并且GPUTexture.format是 深度或模板格式,则format将设置为与适当的 方面特定格式 相同。 - 否则,它将设置为与
GPUTexture.format相同。
- 如果
label可选-
一个字符串,提供可用于识别对象的标签,例如在
GPUError消息或控制台警告中。 mipLevelCount可选-
一个数字,定义视图可访问的 mipmap 级别数,从
baseMipLevel值开始。如果省略
mipLevelCount,则其值将设置为GPUTexture.mipLevelCount-baseMipLevel。 usage可选-
一组 按位标志,表示源纹理的使用标志(可在
GPUTexture.usage属性中找到)的一个子集,这些标志与所选视图格式兼容。在视图格式与某些用法不兼容的情况下,可以使用此选项来限制允许的视图用法。可用的使用标志列在GPUTexture.usage值表 中。默认值为
0,表示源纹理的完整使用标志集。如果视图的format不支持纹理的所有用法,则默认值将失败,并且必须显式指定视图的用法。
返回值
一个 GPUTextureView 对象实例。
验证
调用 createView() 时必须满足以下条件,否则会生成 GPUValidationError 并返回一个无效的 GPUTextureView 对象:
- 如果
aspect是"all",则format等于GPUTexture.format,或者等于原始GPUDevice.createTexture()调用描述符对象中指定的viewFormats之一。 - 如果
aspect是"depth-only"或"stencil-only",则format等于 深度或模板格式 的适当 方面特定格式。 mipLevelCount大于 0。mipLevelCount+baseMipLevel小于或等于GPUTexture.mipLevelCount。arrayLayerCount大于 0。- 如果
GPUTexture.dimension是"2d",则arrayLayerCount+baseArrayLayer小于或等于GPUTexture.depthOrArrayLayers,或者如果GPUTexture.dimension是"1d"或"3d",则小于或等于 1。 - 如果
sampleCount大于 1,则dimension为"2d"。 - 如果
dimension是"1d"GPUTexture.dimension是"1d"arrayLayerCount是 1
"2d"GPUTexture.dimension是"2d"arrayLayerCount是 1
"2d-array"GPUTexture.dimension是"2d"
"cube"GPUTexture.dimension是"2d"arrayLayerCount是 6GPUTexture.width等于GPUTexture.height
"cube-array"GPUTexture.dimension是"2d"arrayLayerCount是 6 的倍数GPUTexture.width等于GPUTexture.height
"3d"GPUTexture.dimension是"3d"arrayLayerCount是 1
- 视图的
format支持usage属性中指定的所有用法。
示例
典型的 createView() 用法
在 WebGPU Samples 的 立方体贴图演示 中,您将看到 createView() 的多种用法示例,既用于为 GPUDevice.createBindGroup() 调用创建视图 resource,也用于在 GPUCommandEncoder.beginRenderPass() 描述符的 depthStencilAttachment 对象中提供 view。
const uniformBindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [
{
binding: 0,
resource: {
buffer: uniformBuffer,
offset: 0,
size: uniformBufferSize,
},
},
{
binding: 1,
resource: sampler,
},
{
binding: 2,
resource: cubemapTexture.createView({
dimension: "cube",
}),
},
],
});
const renderPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
view: undefined, // Assigned later
loadOp: "clear",
storeOp: "store",
},
],
depthStencilAttachment: {
view: depthTexture.createView(),
depthClearValue: 1.0,
depthLoadOp: "clear",
depthStoreOp: "store",
},
};
// …
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
// …
带有用法限制的 createView()
在此代码片段中,我们创建了一个纹理,然后创建了一个通过 usage 属性限制了其用法的视图。
const texture = myDevice.createTexture({
size: [4, 4],
format: "rgba8unorm",
usage:
GPUTextureUsage.RENDER_ATTACHMENT |
GPUTextureUsage.TEXTURE_BINDING |
GPUTextureUsage.STORAGE_BINDING,
viewFormats: ["rgba8unorm-srgb"],
});
const view = texture.createView({
format: "rgba8unorm-srgb",
usage: GPUTextureUsage.RENDER_ATTACHMENT, // Restrict allowed usage
});
规范
| 规范 |
|---|
| WebGPU # dom-gputexture-createview |
浏览器兼容性
加载中…