From 574ef0345e9fc3134ee92fc248907282393b9c5e Mon Sep 17 00:00:00 2001 From: Gregg Tavares Date: Tue, 11 Jun 2024 19:10:49 -0700 Subject: [PATCH] fix view resolve --- src/format-info.ts | 46 +++++++++++++++++++++++++++++++++++++++++ src/texture.ts | 51 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 85 insertions(+), 12 deletions(-) diff --git a/src/format-info.ts b/src/format-info.ts index 89f5d92..9ceb198 100644 --- a/src/format-info.ts +++ b/src/format-info.ts @@ -41,10 +41,56 @@ export type FormatInfo = { feature?: string, }; + + // exported from the WebGPU CTS by adding the following line to src/webgpu/format_info.ts // // console.log(JSON.stringify(kAllTextureFormatInfo, null, 2)); +/** `kDepthStencilFormatResolvedAspect[format][aspect]` returns the aspect-specific format for a + * depth-stencil format, or `undefined` if the format doesn't have the aspect. + */ +const kDepthStencilFormatResolvedAspect = { + // kUnsizedDepthStencilFormats + depth24plus: { + all: 'depth24plus', + 'depth-only': 'depth24plus', + 'stencil-only': undefined, + }, + 'depth24plus-stencil8': { + all: 'depth24plus-stencil8', + 'depth-only': 'depth24plus', + 'stencil-only': 'stencil8', + }, + + // kSizedDepthStencilFormats + depth16unorm: { + all: 'depth16unorm', + 'depth-only': 'depth16unorm', + 'stencil-only': undefined, + }, + depth32float: { + all: 'depth32float', + 'depth-only': 'depth32float', + 'stencil-only': undefined, + }, + 'depth32float-stencil8': { + all: 'depth32float-stencil8', + 'depth-only': 'depth32float', + 'stencil-only': 'stencil8', + }, + stencil8: { + all: 'stencil8', + 'depth-only': undefined, + 'stencil-only': 'stencil8', + }, +}; + +export function getDepthStencilFormatResolvedAspect(format: GPUTextureFormat, aspect: GPUTextureAspect): GPUTextureFormat | undefined { + const info = kDepthStencilFormatResolvedAspect[format as keyof typeof kDepthStencilFormatResolvedAspect]; + return info ? info[aspect] as GPUTextureFormat : undefined; +} + export const kAllTextureFormatInfo: {[key: string]: FormatInfo} = { "r8unorm": { "blockWidth": 1, diff --git a/src/texture.ts b/src/texture.ts index c3704a8..c760ede 100644 --- a/src/texture.ts +++ b/src/texture.ts @@ -1,3 +1,6 @@ +import { + getDepthStencilFormatResolvedAspect, +} from './format-info.js'; import { wrapFunctionBefore, wrapFunctionAfter, @@ -15,20 +18,44 @@ export type TextureViewDescriptor = { export const s_textureViewToTexture = new WeakMap(); export const s_textureViewToDesc = new WeakMap(); +function resolveTextureAspect(format: GPUTextureFormat, aspect: GPUTextureAspect) { + switch (aspect) { + case 'all': + return format; + case 'depth-only': + case 'stencil-only': + return getDepthStencilFormatResolvedAspect(format, aspect); + } + return undefined; +} + function reifyTextureViewDescriptor(texture: GPUTexture, desc: GPUTextureViewDescriptor | undefined): TextureViewDescriptor { - const { - format = texture.format, - dimension = texture.dimension === '2d' - ? (texture.depthOrArrayLayers === 1 ? '2d' : '2d-array') - : texture.dimension, - aspect = 'all', - baseMipLevel = 0, - mipLevelCount = texture.mipLevelCount, - baseArrayLayer = 0, - arrayLayerCount = texture.depthOrArrayLayers, - } = desc || {}; + const dimension = desc?.dimension ?? ( + texture.dimension === '2d' + ? (texture.depthOrArrayLayers === 1 ? '2d' : '2d-array') + : texture.dimension + ); + const aspect = desc?.aspect ?? 'all'; + let format = desc?.format; + if (!format) { + const f = resolveTextureAspect(texture.format, aspect); + format = f ?? texture.format; + } return { - format, dimension, aspect, baseMipLevel, mipLevelCount, baseArrayLayer, arrayLayerCount, + format, + dimension, + aspect, + baseMipLevel: desc?.baseMipLevel ?? 0, + mipLevelCount: desc?.mipLevelCount ?? (texture.mipLevelCount - (desc?.baseMipLevel ?? 0)), + baseArrayLayer: desc?.baseArrayLayer ?? 0, + arrayLayerCount: desc?.arrayLayerCount ?? ( + dimension === 'cube' + ? 6 + : (dimension === '2d-array' || dimension === 'cube-array' + ? texture.depthOrArrayLayers - (desc?.baseArrayLayer ?? 0) + : 1 + ) + ), }; }