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 |
瀏覽器相容性
載入中…