Skip to content

Commit 8ae9035

Browse files
Josemawcandillon
andauthored
fix(🍏): turn off metal_disable_sampler_compare on the iOS Simulator (#624)
* fix(🍏): turn off metal_disable_sampler_compare on the iOS Simulator * Clarify simulator toggle handling in GPUAdapter.cpp Updated comments to clarify the purpose of disabling toggles for the iOS Simulator in Dawn. * test(🍏): sample a depth texture with a compare sampler Fails on the iOS Simulator without metal_disable_sampler_compare turned off: the device is lost with "Compare functions are disabled with the Metal backend". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Josema <1819888+josema@users.noreply.github.com> Co-authored-by: William Candillon <wcandillon@gmail.com>
1 parent 5df30c9 commit 8ae9035

2 files changed

Lines changed: 129 additions & 9 deletions

File tree

β€Žpackages/webgpu/cpp/rnwgpu/api/GPUAdapter.cppβ€Ž

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,28 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
217217
// (https://issues.chromium.org/issues/42241591)
218218
#if defined(TARGET_OS_SIMULATOR) && TARGET_OS_SIMULATOR
219219
// The iOS Simulator only advertises MTLFeatureSet_iOS_GPUFamily2, so
220-
// Dawn defaults disable_base_instance/disable_base_vertex on and then
221-
// rejects every draw with a non-zero firstInstance or baseVertex,
222-
// both core WebGPU. The simulator forwards Metal calls to the host
223-
// GPU, which does support base vertex/instance drawing, so force the
224-
// toggles off. Device builds are unaffected: WebGPU-capable iPhones
225-
// and iPads are all GPUFamily3+. These are device-stage toggles, so
226-
// they must be chained here rather than on the instance descriptor
227-
// (instance-stage toggle parsing silently drops them).
220+
// Dawn turns on toggles that reject core WebGPU features. The
221+
// simulator forwards Metal calls to the host GPU, which supports
222+
// them, so force these toggles off unless the caller enables them
223+
// through dawnToggles. Device builds are unaffected: WebGPU-capable
224+
// iPhones and iPads are all GPUFamily3+. These are device-stage
225+
// toggles, so they must be chained here rather than on the instance
226+
// descriptor (instance-stage toggle parsing silently drops them).
227+
//
228+
// - disable_base_instance, disable_base_vertex: Dawn rejects every
229+
// draw with a non-zero firstInstance or baseVertex.
230+
// TODO: newer Dawn no longer needs these two
231+
// (https://issues.chromium.org/issues/42241591).
232+
// - metal_disable_sampler_compare: Dawn rejects any sampler with a
233+
// compare function, and the error loses the device. Upstream has
234+
// no fix for this one, so keep it when updating Dawn. Apple does
235+
// not document compare functions on the Simulator: this relies on
236+
// the host GPU and was checked on an iPhone 11 Simulator (iOS 17,
237+
// Xcode 26.6). If shadows look wrong on the Simulator only, look
238+
// here first.
228239
static const char *const kSimulatorDisabledToggles[] = {
229-
"disable_base_instance", "disable_base_vertex"};
240+
"disable_base_instance", "disable_base_vertex",
241+
"metal_disable_sampler_compare"};
230242
for (const char *name : kSimulatorDisabledToggles) {
231243
const bool explicitlyEnabled = std::any_of(
232244
enabledToggles.begin(), enabledToggles.end(),
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
Β (0)