|
| 1 | +import { client } from "./setup"; |
| 2 | + |
| 3 | +// Regression test for #624: on the iOS Simulator, Dawn turns on |
| 4 | +// metal_disable_sampler_compare (the Simulator only advertises GPUFamily2), so |
| 5 | +// creating a sampler with a compare function is a validation error that also |
| 6 | +// loses the device. Compare samplers are core WebGPU (shadow maps), so this |
| 7 | +// must work on every backend, the Simulator included. |
| 8 | +describe("Sampler compare", () => { |
| 9 | + it("samples a depth texture with a compare sampler", async () => { |
| 10 | + const result = await client.eval(({ gpu }) => { |
| 11 | + // A fresh device: on failure it is lost, and the shared test device must |
| 12 | + // stay usable for the other specs. |
| 13 | + return gpu |
| 14 | + .requestAdapter() |
| 15 | + .then((adapter) => adapter!.requestDevice()) |
| 16 | + .then((device) => { |
| 17 | + let lost: string | null = null; |
| 18 | + device.lost.then((info) => { |
| 19 | + lost = info.message; |
| 20 | + }); |
| 21 | + |
| 22 | + // Two depth texels: 0.25 and 0.75. depth16unorm is a valid |
| 23 | + // writeTexture destination (depth32float is not). |
| 24 | + const depth = device.createTexture({ |
| 25 | + size: [2, 1], |
| 26 | + format: "depth16unorm", |
| 27 | + usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST, |
| 28 | + }); |
| 29 | + device.queue.writeTexture( |
| 30 | + { texture: depth }, |
| 31 | + new Uint16Array([16384, 49152]), |
| 32 | + { bytesPerRow: 4 }, |
| 33 | + [2, 1], |
| 34 | + ); |
| 35 | + |
| 36 | + device.pushErrorScope("validation"); |
| 37 | + const sampler = device.createSampler({ compare: "less" }); |
| 38 | + const out = device.createBuffer({ |
| 39 | + size: 8, |
| 40 | + usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC, |
| 41 | + }); |
| 42 | + const readback = device.createBuffer({ |
| 43 | + size: 8, |
| 44 | + usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST, |
| 45 | + }); |
| 46 | + const pipeline = device.createComputePipeline({ |
| 47 | + layout: "auto", |
| 48 | + compute: { |
| 49 | + module: device.createShaderModule({ |
| 50 | + code: ` |
| 51 | +@group(0) @binding(0) var depthTex: texture_depth_2d; |
| 52 | +@group(0) @binding(1) var cmp: sampler_comparison; |
| 53 | +@group(0) @binding(2) var<storage, read_write> result: array<f32, 2>; |
| 54 | +
|
| 55 | +@compute @workgroup_size(1) |
| 56 | +fn main() { |
| 57 | + // "less": 1.0 when the reference (0.5) is less than the stored depth. |
| 58 | + result[0] = textureSampleCompareLevel(depthTex, cmp, vec2f(0.25, 0.5), 0.5); |
| 59 | + result[1] = textureSampleCompareLevel(depthTex, cmp, vec2f(0.75, 0.5), 0.5); |
| 60 | +}`, |
| 61 | + }), |
| 62 | + entryPoint: "main", |
| 63 | + }, |
| 64 | + }); |
| 65 | + const bindGroup = device.createBindGroup({ |
| 66 | + layout: pipeline.getBindGroupLayout(0), |
| 67 | + entries: [ |
| 68 | + { binding: 0, resource: depth.createView() }, |
| 69 | + { binding: 1, resource: sampler }, |
| 70 | + { binding: 2, resource: { buffer: out } }, |
| 71 | + ], |
| 72 | + }); |
| 73 | + const encoder = device.createCommandEncoder(); |
| 74 | + const pass = encoder.beginComputePass(); |
| 75 | + pass.setPipeline(pipeline); |
| 76 | + pass.setBindGroup(0, bindGroup); |
| 77 | + pass.dispatchWorkgroups(1); |
| 78 | + pass.end(); |
| 79 | + encoder.copyBufferToBuffer(out, 0, readback, 0, 8); |
| 80 | + device.queue.submit([encoder.finish()]); |
| 81 | + |
| 82 | + return device.popErrorScope().then((error) => { |
| 83 | + const errorMessage = error ? error.message : null; |
| 84 | + return readback.mapAsync(GPUMapMode.READ).then( |
| 85 | + () => { |
| 86 | + const values = Array.from( |
| 87 | + new Float32Array(readback.getMappedRange()), |
| 88 | + ); |
| 89 | + readback.unmap(); |
| 90 | + device.destroy(); |
| 91 | + return { errorMessage, lost, values }; |
| 92 | + }, |
| 93 | + (e: Error) => ({ |
| 94 | + errorMessage: errorMessage || e.message, |
| 95 | + lost, |
| 96 | + values: [] as number[], |
| 97 | + }), |
| 98 | + ); |
| 99 | + }); |
| 100 | + }); |
| 101 | + }); |
| 102 | + // On failure the device is lost first, so the lost message carries the |
| 103 | + // actual cause (the error scope and mapAsync only see the aftermath). |
| 104 | + expect(result.lost).toBeNull(); |
| 105 | + expect(result.errorMessage).toBeNull(); |
| 106 | + expect(result.values).toEqual([0, 1]); |
| 107 | + }); |
| 108 | +}); |
0 commit comments