Skip to content

Commit

Permalink
fix view resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Jun 12, 2024
1 parent 3a0cea5 commit 574ef03
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 12 deletions.
46 changes: 46 additions & 0 deletions src/format-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
51 changes: 39 additions & 12 deletions src/texture.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {
getDepthStencilFormatResolvedAspect,
} from './format-info.js';
import {
wrapFunctionBefore,
wrapFunctionAfter,
Expand All @@ -15,20 +18,44 @@ export type TextureViewDescriptor = {
export const s_textureViewToTexture = new WeakMap<GPUTextureView, GPUTexture>();
export const s_textureViewToDesc = new WeakMap<GPUTextureView, TextureViewDescriptor>();

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
)
),
};
}

Expand Down

0 comments on commit 574ef03

Please sign in to comment.