|
| 1 | +export const description = ` |
| 2 | +Validation tests for the 'texture-component-swizzle' feature. |
| 3 | +
|
| 4 | +Test that: |
| 5 | +* when the feature is off, swizzling is not allowed, even the identity swizzle. |
| 6 | +* swizzling is not allowed on textures with usage STORAGE_BINDING nor RENDER_ATTACHMENT |
| 7 | + except the identity swizzle. |
| 8 | +`; |
| 9 | + |
| 10 | +import { makeTestGroup } from '../../../../../common/framework/test_group.js'; |
| 11 | +import { UniqueFeaturesOrLimitsGPUTest } from '../../../../gpu_test.js'; |
| 12 | + |
| 13 | +import { isIdentitySwizzle, kSwizzleTests } from './texture_component_swizzle_utils.js'; |
| 14 | + |
| 15 | +export const g = makeTestGroup(UniqueFeaturesOrLimitsGPUTest); |
| 16 | + |
| 17 | +g.test('invalid_swizzle') |
| 18 | + .desc( |
| 19 | + ` |
| 20 | + Test that setting an invalid swizzle value on a texture view throws an exception. |
| 21 | + ` |
| 22 | + ) |
| 23 | + .params(u => |
| 24 | + u.beginSubcases().combine('invalidSwizzle', [ |
| 25 | + 'rgbA', // swizzles are case-sensitive |
| 26 | + 'RGBA', // swizzles are case-sensitive |
| 27 | + 'rgb', // must have 4 components |
| 28 | + 'rgba01', |
| 29 | + 'ɲgba', // r with 0x200 added to each code point to make sure values are not truncated. |
| 30 | + 'ɲɧɢɡ', // rgba with 0x200 added to each code point to make sure values are not truncated. |
| 31 | + '𝐫𝐠𝐛𝐚', // various unicode values that normalize to rgba |
| 32 | + '𝑟𝑔𝑏𝑎', |
| 33 | + '𝗋𝗀𝖻𝖺', |
| 34 | + '𝓇ℊ𝒷𝒶', |
| 35 | + 'ⓡⓖⓑⓐ', |
| 36 | + 'rgba', |
| 37 | + 'ʳᵍᵇᵃ', |
| 38 | + '000', |
| 39 | + '00000', |
| 40 | + '111', |
| 41 | + '11111', |
| 42 | + 0, |
| 43 | + 1, |
| 44 | + 1111, // passes because toString is '1111' |
| 45 | + 1234, |
| 46 | + 1111.1, |
| 47 | + 0x72676261, // big endian rgba |
| 48 | + 0x61626772, // little endian rgba |
| 49 | + 0x30303030, // 0000 |
| 50 | + 0x31313131, // 1111 |
| 51 | + true, |
| 52 | + false, |
| 53 | + null, |
| 54 | + ]) |
| 55 | + ) |
| 56 | + .beforeAllSubcases(t => { |
| 57 | + // MAINTENANCE_TODO: Remove this cast once texture-component-swizzle is added to @webgpu/types |
| 58 | + t.selectDeviceOrSkipTestCase('texture-component-swizzle' as GPUFeatureName); |
| 59 | + }) |
| 60 | + .fn(t => { |
| 61 | + const { invalidSwizzle } = t.params; |
| 62 | + const texture = t.createTextureTracked({ |
| 63 | + format: 'rgba8unorm', |
| 64 | + size: [1], |
| 65 | + usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING, |
| 66 | + }); |
| 67 | + |
| 68 | + const failure = typeof invalidSwizzle !== 'number' || invalidSwizzle !== 1111; |
| 69 | + t.shouldThrow(failure ? 'TypeError' : false, () => { |
| 70 | + texture.createView({ swizzle: invalidSwizzle as GPUTextureComponentSwizzle }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | +g.test('only_identity_swizzle') |
| 75 | + .desc( |
| 76 | + ` |
| 77 | + Test that if texture-component-swizzle is not enabled, having a non-default swizzle property generates a validation error. |
| 78 | + ` |
| 79 | + ) |
| 80 | + .params(u => u.beginSubcases().combine('swizzle', kSwizzleTests)) |
| 81 | + .fn(t => { |
| 82 | + // MAINTENANCE_TODO: Remove this check if the spec is updated to say that all implementations must validate this. |
| 83 | + t.skipIf( |
| 84 | + !t.adapter.features.has('texture-component-swizzle'), |
| 85 | + 'skip on browsers that have not implemented texture-component-swizzle' |
| 86 | + ); |
| 87 | + |
| 88 | + const { swizzle } = t.params; |
| 89 | + const texture = t.createTextureTracked({ |
| 90 | + format: 'rgba8unorm', |
| 91 | + size: [1], |
| 92 | + usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING, |
| 93 | + }); |
| 94 | + const shouldError = !isIdentitySwizzle(swizzle); |
| 95 | + t.expectValidationError(() => { |
| 96 | + texture.createView({ swizzle }); |
| 97 | + }, shouldError); |
| 98 | + }); |
| 99 | + |
| 100 | +g.test('no_render_no_resolve_no_storage') |
| 101 | + .desc( |
| 102 | + ` |
| 103 | + Test that setting a non-identity swizzle gets an error if used as a render attachment, |
| 104 | + a resolve target, or a storage binding. |
| 105 | + ` |
| 106 | + ) |
| 107 | + .params(u => |
| 108 | + u |
| 109 | + .combine('useCase', [ |
| 110 | + 'texture-binding', |
| 111 | + 'color-attachment', |
| 112 | + 'depth-attachment', |
| 113 | + 'stencil-attachment', |
| 114 | + 'resolve-target', |
| 115 | + 'storage-binding', |
| 116 | + ] as const) |
| 117 | + .beginSubcases() |
| 118 | + .combine('swizzle', kSwizzleTests) |
| 119 | + ) |
| 120 | + .beforeAllSubcases(t => { |
| 121 | + // MAINTENANCE_TODO: Remove this cast once texture-component-swizzle is added to @webgpu/types |
| 122 | + t.selectDeviceOrSkipTestCase('texture-component-swizzle' as GPUFeatureName); |
| 123 | + }) |
| 124 | + .fn(t => { |
| 125 | + const { swizzle, useCase } = t.params; |
| 126 | + const texture = t.createTextureTracked({ |
| 127 | + format: |
| 128 | + useCase === 'depth-attachment' |
| 129 | + ? 'depth16unorm' |
| 130 | + : useCase === 'stencil-attachment' |
| 131 | + ? 'stencil8' |
| 132 | + : 'rgba8unorm', |
| 133 | + size: [1], |
| 134 | + usage: |
| 135 | + GPUTextureUsage.COPY_SRC | |
| 136 | + GPUTextureUsage.COPY_DST | |
| 137 | + GPUTextureUsage.TEXTURE_BINDING | |
| 138 | + GPUTextureUsage.RENDER_ATTACHMENT | |
| 139 | + (useCase === 'storage-binding' ? GPUTextureUsage.STORAGE_BINDING : 0), |
| 140 | + }); |
| 141 | + const view = texture.createView({ swizzle }); |
| 142 | + const shouldError = useCase !== 'texture-binding' && !isIdentitySwizzle(swizzle); |
| 143 | + switch (useCase) { |
| 144 | + case 'texture-binding': { |
| 145 | + const bindGroupLayout = t.device.createBindGroupLayout({ |
| 146 | + entries: [ |
| 147 | + { |
| 148 | + binding: 0, |
| 149 | + visibility: GPUShaderStage.FRAGMENT, |
| 150 | + texture: {}, |
| 151 | + }, |
| 152 | + ], |
| 153 | + }); |
| 154 | + t.expectValidationError(() => { |
| 155 | + t.device.createBindGroup({ |
| 156 | + layout: bindGroupLayout, |
| 157 | + entries: [{ binding: 0, resource: view }], |
| 158 | + }); |
| 159 | + }, shouldError); |
| 160 | + break; |
| 161 | + } |
| 162 | + case 'color-attachment': { |
| 163 | + t.expectValidationError(() => { |
| 164 | + const encoder = t.device.createCommandEncoder(); |
| 165 | + const pass = encoder.beginRenderPass({ |
| 166 | + colorAttachments: [ |
| 167 | + { |
| 168 | + view, |
| 169 | + loadOp: 'clear', |
| 170 | + storeOp: 'store', |
| 171 | + }, |
| 172 | + ], |
| 173 | + }); |
| 174 | + pass.end(); |
| 175 | + encoder.finish(); |
| 176 | + }, shouldError); |
| 177 | + break; |
| 178 | + } |
| 179 | + case 'depth-attachment': { |
| 180 | + t.expectValidationError(() => { |
| 181 | + const encoder = t.device.createCommandEncoder(); |
| 182 | + const pass = encoder.beginRenderPass({ |
| 183 | + colorAttachments: [], |
| 184 | + depthStencilAttachment: { |
| 185 | + view, |
| 186 | + depthClearValue: 1, |
| 187 | + depthLoadOp: 'clear', |
| 188 | + depthStoreOp: 'store', |
| 189 | + }, |
| 190 | + }); |
| 191 | + pass.end(); |
| 192 | + encoder.finish(); |
| 193 | + }, shouldError); |
| 194 | + break; |
| 195 | + } |
| 196 | + case 'stencil-attachment': { |
| 197 | + t.expectValidationError(() => { |
| 198 | + const encoder = t.device.createCommandEncoder(); |
| 199 | + const pass = encoder.beginRenderPass({ |
| 200 | + colorAttachments: [], |
| 201 | + depthStencilAttachment: { |
| 202 | + view, |
| 203 | + stencilClearValue: 0, |
| 204 | + stencilLoadOp: 'clear', |
| 205 | + stencilStoreOp: 'store', |
| 206 | + }, |
| 207 | + }); |
| 208 | + pass.end(); |
| 209 | + encoder.finish(); |
| 210 | + }, shouldError); |
| 211 | + break; |
| 212 | + } |
| 213 | + case 'resolve-target': { |
| 214 | + t.expectValidationError(() => { |
| 215 | + const encoder = t.device.createCommandEncoder(); |
| 216 | + const pass = encoder.beginRenderPass({ |
| 217 | + colorAttachments: [ |
| 218 | + { |
| 219 | + view: t.createTextureTracked({ |
| 220 | + format: 'rgba8unorm', |
| 221 | + size: [1], |
| 222 | + usage: GPUTextureUsage.RENDER_ATTACHMENT, |
| 223 | + sampleCount: 4, |
| 224 | + }), |
| 225 | + resolveTarget: view, |
| 226 | + loadOp: 'clear', |
| 227 | + storeOp: 'store', |
| 228 | + }, |
| 229 | + ], |
| 230 | + }); |
| 231 | + pass.end(); |
| 232 | + encoder.finish(); |
| 233 | + }, shouldError); |
| 234 | + break; |
| 235 | + } |
| 236 | + case 'storage-binding': { |
| 237 | + const bindGroupLayout = t.device.createBindGroupLayout({ |
| 238 | + entries: [ |
| 239 | + { |
| 240 | + binding: 0, |
| 241 | + visibility: GPUShaderStage.COMPUTE, |
| 242 | + storageTexture: { |
| 243 | + access: 'read-only', |
| 244 | + format: 'rgba8unorm', |
| 245 | + }, |
| 246 | + }, |
| 247 | + ], |
| 248 | + }); |
| 249 | + t.expectValidationError(() => { |
| 250 | + t.device.createBindGroup({ |
| 251 | + layout: bindGroupLayout, |
| 252 | + entries: [{ binding: 0, resource: view }], |
| 253 | + }); |
| 254 | + }, shouldError); |
| 255 | + break; |
| 256 | + } |
| 257 | + } |
| 258 | + }); |
| 259 | + |
| 260 | +g.test('compatibility_mode') |
| 261 | + .desc( |
| 262 | + ` |
| 263 | + Test that in compatibility mode, swizzles must be equivalent. |
| 264 | + ` |
| 265 | + ) |
| 266 | + .beforeAllSubcases(t => { |
| 267 | + // MAINTENANCE_TODO: Remove this cast once texture-component-swizzle is added to @webgpu/types |
| 268 | + t.selectDeviceOrSkipTestCase('texture-component-swizzle' as GPUFeatureName); |
| 269 | + }) |
| 270 | + .params(u => |
| 271 | + u |
| 272 | + .beginSubcases() |
| 273 | + .combine('swizzle', kSwizzleTests) |
| 274 | + .combine('otherSwizzle', kSwizzleTests) |
| 275 | + .combine('pipelineType', ['render', 'compute'] as const) |
| 276 | + ) |
| 277 | + .fn(t => { |
| 278 | + const { swizzle, otherSwizzle, pipelineType } = t.params; |
| 279 | + |
| 280 | + const module = t.device.createShaderModule({ |
| 281 | + code: ` |
| 282 | + @group(0) @binding(0) var tex0: texture_2d<f32>; |
| 283 | + @group(1) @binding(0) var tex1: texture_2d<f32>; |
| 284 | +
|
| 285 | + @compute @workgroup_size(1) fn cs() { |
| 286 | + _ = tex0; |
| 287 | + _ = tex1; |
| 288 | + } |
| 289 | +
|
| 290 | + @vertex fn vs() -> @builtin(position) vec4f { |
| 291 | + return vec4f(0); |
| 292 | + } |
| 293 | +
|
| 294 | + @fragment fn fs() -> @location(0) vec4f { |
| 295 | + _ = tex0; |
| 296 | + _ = tex1; |
| 297 | + return vec4f(0); |
| 298 | + } |
| 299 | + `, |
| 300 | + }); |
| 301 | + |
| 302 | + const pipeline = |
| 303 | + pipelineType === 'compute' |
| 304 | + ? t.device.createComputePipeline({ |
| 305 | + layout: 'auto', |
| 306 | + compute: { module }, |
| 307 | + }) |
| 308 | + : t.device.createRenderPipeline({ |
| 309 | + layout: 'auto', |
| 310 | + vertex: { module }, |
| 311 | + fragment: { module, targets: [{ format: 'rgba8unorm' }] }, |
| 312 | + }); |
| 313 | + |
| 314 | + const texture = t.createTextureTracked({ |
| 315 | + size: [1], |
| 316 | + format: 'rgba8unorm', |
| 317 | + usage: GPUTextureUsage.TEXTURE_BINDING, |
| 318 | + }); |
| 319 | + |
| 320 | + const bindGroup0 = t.device.createBindGroup({ |
| 321 | + layout: pipeline.getBindGroupLayout(0), |
| 322 | + entries: [ |
| 323 | + { |
| 324 | + binding: 0, |
| 325 | + resource: texture.createView({ swizzle }), |
| 326 | + }, |
| 327 | + ], |
| 328 | + }); |
| 329 | + |
| 330 | + const bindGroup1 = t.device.createBindGroup({ |
| 331 | + layout: pipeline.getBindGroupLayout(0), |
| 332 | + entries: [ |
| 333 | + { |
| 334 | + binding: 0, |
| 335 | + resource: texture.createView({ swizzle: otherSwizzle }), |
| 336 | + }, |
| 337 | + ], |
| 338 | + }); |
| 339 | + |
| 340 | + const encoder = t.device.createCommandEncoder(); |
| 341 | + switch (pipelineType) { |
| 342 | + case 'compute': { |
| 343 | + const pass = encoder.beginComputePass(); |
| 344 | + pass.setPipeline(pipeline as GPUComputePipeline); |
| 345 | + pass.setBindGroup(0, bindGroup0); |
| 346 | + pass.setBindGroup(1, bindGroup1); |
| 347 | + pass.dispatchWorkgroups(1); |
| 348 | + pass.end(); |
| 349 | + break; |
| 350 | + } |
| 351 | + case 'render': { |
| 352 | + const view = t.createTextureTracked({ |
| 353 | + size: [1], |
| 354 | + format: 'rgba8unorm', |
| 355 | + usage: GPUTextureUsage.RENDER_ATTACHMENT, |
| 356 | + }); |
| 357 | + const pass = encoder.beginRenderPass({ |
| 358 | + colorAttachments: [{ view, loadOp: 'clear', storeOp: 'store' }], |
| 359 | + }); |
| 360 | + pass.setPipeline(pipeline as GPURenderPipeline); |
| 361 | + pass.setBindGroup(0, bindGroup0); |
| 362 | + pass.setBindGroup(1, bindGroup1); |
| 363 | + pass.draw(3); |
| 364 | + pass.end(); |
| 365 | + } |
| 366 | + } |
| 367 | + |
| 368 | + const shouldError = t.isCompatibility && swizzle !== otherSwizzle; |
| 369 | + |
| 370 | + t.expectValidationError(() => { |
| 371 | + encoder.finish(); |
| 372 | + }, shouldError); |
| 373 | + }); |
0 commit comments