1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
import { assert, unreachable } from '../../../common/util/util.js';
import { kTextureFormatInfo } from '../../format_info.js';
import { align } from '../../util/math.js';
import { reifyExtent3D } from '../../util/unions.js';
/**
* Compute the maximum mip level count allowed for a given texture size and texture dimension.
*/
export function maxMipLevelCount({
size,
dimension = '2d',
}: {
readonly size: Readonly<GPUExtent3DDict> | readonly number[];
readonly dimension?: GPUTextureDimension;
}): number {
const sizeDict = reifyExtent3D(size);
let maxMippedDimension = 0;
switch (dimension) {
case '1d':
maxMippedDimension = 1; // No mipmaps allowed.
break;
case '2d':
maxMippedDimension = Math.max(sizeDict.width, sizeDict.height);
break;
case '3d':
maxMippedDimension = Math.max(sizeDict.width, sizeDict.height, sizeDict.depthOrArrayLayers);
break;
}
return Math.floor(Math.log2(maxMippedDimension)) + 1;
}
/**
* Compute the "physical size" of a mip level: the size of the level, rounded up to a
* multiple of the texel block size.
*/
export function physicalMipSize(
baseSize: Required<GPUExtent3DDict>,
format: GPUTextureFormat,
dimension: GPUTextureDimension,
level: number
): Required<GPUExtent3DDict> {
switch (dimension) {
case '1d':
assert(level === 0, '1d textures cannot be mipmapped');
assert(baseSize.height === 1 && baseSize.depthOrArrayLayers === 1, '1d texture not Wx1x1');
return { width: baseSize.width, height: 1, depthOrArrayLayers: 1 };
case '2d': {
assert(
Math.max(baseSize.width, baseSize.height) >> level > 0,
() => `level (${level}) too large for base size (${baseSize.width}x${baseSize.height})`
);
const virtualWidthAtLevel = Math.max(baseSize.width >> level, 1);
const virtualHeightAtLevel = Math.max(baseSize.height >> level, 1);
const physicalWidthAtLevel = align(
virtualWidthAtLevel,
kTextureFormatInfo[format].blockWidth
);
const physicalHeightAtLevel = align(
virtualHeightAtLevel,
kTextureFormatInfo[format].blockHeight
);
return {
width: physicalWidthAtLevel,
height: physicalHeightAtLevel,
depthOrArrayLayers: baseSize.depthOrArrayLayers,
};
}
case '3d': {
assert(
Math.max(baseSize.width, baseSize.height, baseSize.depthOrArrayLayers) >> level > 0,
() =>
`level (${level}) too large for base size (${baseSize.width}x${baseSize.height}x${baseSize.depthOrArrayLayers})`
);
assert(
kTextureFormatInfo[format].blockWidth === 1 && kTextureFormatInfo[format].blockHeight === 1,
'not implemented for 3d block formats'
);
return {
width: Math.max(baseSize.width >> level, 1),
height: Math.max(baseSize.height >> level, 1),
depthOrArrayLayers: Math.max(baseSize.depthOrArrayLayers >> level, 1),
};
}
}
}
/**
* Compute the "physical size" of a mip level: the size of the level, rounded up to a
* multiple of the texel block size.
*/
export function physicalMipSizeFromTexture(
texture: GPUTexture,
mipLevel: number
): [number, number, number] {
const size = physicalMipSize(texture, texture.format, texture.dimension, mipLevel);
return [size.width, size.height, size.depthOrArrayLayers];
}
/**
* Compute the "virtual size" of a mip level of a texture (not accounting for texel block rounding).
*
* MAINTENANCE_TODO: Change input/output to Required<GPUExtent3DDict> for consistency.
*/
export function virtualMipSize(
dimension: GPUTextureDimension,
size: readonly [number, number, number],
mipLevel: number
): [number, number, number] {
const shiftMinOne = (n: number) => Math.max(1, n >> mipLevel);
switch (dimension) {
case '1d':
assert(size[2] === 1);
return [shiftMinOne(size[0]), size[1], size[2]];
case '2d':
return [shiftMinOne(size[0]), shiftMinOne(size[1]), size[2]];
case '3d':
return [shiftMinOne(size[0]), shiftMinOne(size[1]), shiftMinOne(size[2])];
default:
unreachable();
}
}
/**
* Get texture dimension from view dimension in order to create an compatible texture for a given
* view dimension.
*/
export function getTextureDimensionFromView(viewDimension: GPUTextureViewDimension) {
switch (viewDimension) {
case '1d':
return '1d';
case '2d':
case '2d-array':
case 'cube':
case 'cube-array':
return '2d';
case '3d':
return '3d';
default:
unreachable();
}
}
/** Returns the possible valid view dimensions for a given texture dimension. */
export function viewDimensionsForTextureDimension(textureDimension: GPUTextureDimension) {
switch (textureDimension) {
case '1d':
return ['1d'] as const;
case '2d':
return ['2d', '2d-array', 'cube', 'cube-array'] as const;
case '3d':
return ['3d'] as const;
}
}
/** Returns the effective view dimension for a given texture dimension and depthOrArrayLayers */
export function effectiveViewDimensionForDimension(
viewDimension: GPUTextureViewDimension | undefined,
dimension: GPUTextureDimension | undefined,
depthOrArrayLayers: number
) {
if (viewDimension) {
return viewDimension;
}
switch (dimension || '2d') {
case '1d':
return '1d';
case '2d':
case undefined:
return depthOrArrayLayers > 1 ? '2d-array' : '2d';
break;
case '3d':
return '3d';
default:
unreachable();
}
}
/** Returns the effective view dimension for a given texture */
export function effectiveViewDimensionForTexture(
texture: GPUTexture,
viewDimension: GPUTextureViewDimension | undefined
) {
return effectiveViewDimensionForDimension(
viewDimension,
texture.dimension,
texture.depthOrArrayLayers
);
}
/** Returns the default view dimension for a given texture descriptor. */
export function defaultViewDimensionsForTexture(textureDescriptor: Readonly<GPUTextureDescriptor>) {
const sizeDict = reifyExtent3D(textureDescriptor.size);
return effectiveViewDimensionForDimension(
undefined,
textureDescriptor.dimension,
sizeDict.depthOrArrayLayers
);
}
/** Reifies the optional fields of `GPUTextureDescriptor`.
* MAINTENANCE_TODO: viewFormats should not be omitted here, but it seems likely that the
* @webgpu/types definition will have to change before we can include it again.
*/
export function reifyTextureDescriptor(
desc: Readonly<GPUTextureDescriptor>
): Required<Omit<GPUTextureDescriptor, 'label' | 'viewFormats'>> {
return { dimension: '2d' as const, mipLevelCount: 1, sampleCount: 1, ...desc };
}
/** Reifies the optional fields of `GPUTextureViewDescriptor` (given a `GPUTextureDescriptor`). */
export function reifyTextureViewDescriptor(
textureDescriptor: Readonly<GPUTextureDescriptor>,
view: Readonly<GPUTextureViewDescriptor>
): Required<Omit<GPUTextureViewDescriptor, 'label'>> {
const texture = reifyTextureDescriptor(textureDescriptor);
// IDL defaulting
const baseMipLevel = view.baseMipLevel ?? 0;
const baseArrayLayer = view.baseArrayLayer ?? 0;
const aspect = view.aspect ?? 'all';
// Spec defaulting
const format = view.format ?? texture.format;
const mipLevelCount = view.mipLevelCount ?? texture.mipLevelCount - baseMipLevel;
const dimension = view.dimension ?? defaultViewDimensionsForTexture(texture);
let arrayLayerCount = view.arrayLayerCount;
if (arrayLayerCount === undefined) {
if (dimension === '2d-array' || dimension === 'cube-array') {
arrayLayerCount = reifyExtent3D(texture.size).depthOrArrayLayers - baseArrayLayer;
} else if (dimension === 'cube') {
arrayLayerCount = 6;
} else {
arrayLayerCount = 1;
}
}
return {
format,
dimension,
aspect,
baseMipLevel,
mipLevelCount,
baseArrayLayer,
arrayLayerCount,
};
}
/**
* Get generator of all the coordinates in a subrect.
* @param subrectOrigin - Subrect origin
* @param subrectSize - Subrect size
*/
export function* fullSubrectCoordinates(
subrectOrigin: Required<GPUOrigin3DDict>,
subrectSize: Required<GPUExtent3DDict>
): Generator<Required<GPUOrigin3DDict>> {
for (let z = subrectOrigin.z; z < subrectOrigin.z + subrectSize.depthOrArrayLayers; ++z) {
for (let y = subrectOrigin.y; y < subrectOrigin.y + subrectSize.height; ++y) {
for (let x = subrectOrigin.x; x < subrectOrigin.x + subrectSize.width; ++x) {
yield { x, y, z };
}
}
}
}
|