diff --git a/guest/bundle/tests/gl_tests.mm b/guest/bundle/tests/gl_tests.mm index c5cb4c700..7f2c78327 100644 --- a/guest/bundle/tests/gl_tests.mm +++ b/guest/bundle/tests/gl_tests.mm @@ -1392,6 +1392,60 @@ uint32_t pciVendorOf(id metal) return vendor; } +// The components of an unsigned 8-bit normalized format (UCharNormalized to UChar4Normalized, and +// UChar4Normalized_BGRA); 0 for any other format. +unsigned unsignedByteNormalizedComponents(MTLAttributeFormat format) +{ + switch (format) { + case MTLAttributeFormatUCharNormalized: + return 1; + case MTLAttributeFormatUChar2Normalized: + return 2; + case MTLAttributeFormatUChar3Normalized: + return 3; + case MTLAttributeFormatUChar4Normalized: + case MTLAttributeFormatUChar4Normalized_BGRA: + return 4; + default: + return 0; + } +} + +// The element of the attribute's buffer that thread `t` of case `c` fetches, as its layout steps: +// by the thread's x or y over the step rate, by the index at its x, or the first for a constant. +size_t stageInElement(const StageInCase &c, const std::vector &indices, unsigned t) +{ + const unsigned x = t % c.width, y = t / c.width; + switch (c.step) { + case MTLStepFunctionThreadPositionInGridX: + return x / c.rate; + case MTLStepFunctionThreadPositionInGridY: + return y / c.rate; + case MTLStepFunctionThreadPositionInGridXIndexed: + if (c.indexType == MTLIndexTypeUInt32) { + uint32_t k; + std::memcpy(&k, &indices[4 * x], 4); + return k; + } else { + uint16_t k; + std::memcpy(&k, &indices[2 * x], 2); + return k; + } + default: + return 0; + } +} + +// The ULPs between the floats of `a` and `b`, of either sign. +uint64_t ulpsBetween(uint32_t a, uint32_t b) +{ + auto ordered = [](uint32_t v) { + return (v & 0x80000000u) ? -static_cast(v & 0x7fffffffu) : static_cast(v); + }; + const int64_t d = ordered(a) - ordered(b); + return static_cast(d < 0 ? -d : d); +} + // A GL kernel's control point input, specialized under a stage input descriptor // (RgfxGLSpecializeStageIn), fetches what Metal's own [[stage_in]] kernel fetches under the same // descriptor on the GPU `metal`, bit for bit: every MTLAttributeFormat into a field of four @@ -1406,12 +1460,21 @@ uint32_t pciVendorOf(id metal) // pciVendorOf) every case still makes both pipelines and runs, but the lanes are not compared, and // the test says so on a SKIP line: there our specialized kernel's lanes 2 and 3 read 0 (measured on // a Radeon Pro 5300M under macOS 26.7.1), where its IR stores every lane and the M3 matches Metal; -// the cause lies past our IR, and its row is parked. +// the cause lies past our IR, and its row is parked. On a Mac's own Intel GPU (PCI vendor 0x8086), +// measured on a UHD 630 under macOS 26.7.1, Metal's fetch of an unsigned 8-bit normalized format +// multiplies the code by f32(1/255) where the M3's answers the quotient: a component lane of ours +// that differs from Metal's there passes within 1 ULP of the correctly rounded quotient, computed +// here, the M3's answer, and the case says so on a line with the count of such lanes. Three formats +// are not compared there, each on a SKIP line with its reason: Int1010102Normalized and +// UInt1010102Normalized, whose lanes of ours read wrong on that GPU while the M3 and our device are +// exact, and FloatRG11B10, for which that GPU's Metal answers 2^31 for a NaN code. Every other lane +// stays bit for bit. void stageInMatchesMetalOn(id metal, const char *path) { const bool guest = isOurDevice(metal); const uint32_t vendor = guest ? 0 : pciVendorOf(metal); const bool compare = vendor != 0x1002; + const bool intel = vendor == 0x8086; if (!compare) fprintf(stderr, "SKIP test_stage_in_matches_metal's comparison on %s (PCI vendor 0x%04x): the 5300M zeroes lanes " @@ -1550,16 +1613,43 @@ void stageInMatchesMetalOn(id metal, const char *path) runStageInCase(metal, queue, ourPipeline, c, data, indices, got); if (!compare) continue; + const char *skip = nullptr; + if (intel && c.format == MTLAttributeFormatInt1010102Normalized) + skip = "on this GPU our lanes read as the unmasked word; the M3 and our device are exact"; + else if (intel && c.format == MTLAttributeFormatUInt1010102Normalized) + skip = "far off on this GPU, cause not found; the M3 and our device are exact"; + else if (intel && c.format == MTLAttributeFormatFloatRG11B10) + skip = "this GPU's Metal answers 2^31 for a NaN code; the M3's quiet NaN is the reference"; + if (skip != nullptr) { + fprintf(stderr, "SKIP stage input oracle on %s: format %lu field %s: %s\n", metal.name.UTF8String, + static_cast(c.format), c.typeName, skip); + continue; + } compared++; const bool anyNaN = guest && nanEncodingIsTheDevices(c.format); - unsigned wrong = 0; + const unsigned byteNormalized = intel ? unsignedByteNormalizedComponents(c.format) : 0; + unsigned wrong = 0, allowed = 0; for (unsigned t = 0; t < c.width * c.height; t++) { uint32_t e[4] = {}, g[4] = {}; std::memcpy(e, &expected[16 * t], 4 * c.lanes); std::memcpy(g, &got[16 * t], 4 * c.lanes); + const uint8_t *element = &data[stageInElement(c, indices, t) * c.stride + c.offset]; bool same = true; - for (unsigned k = 0; k < c.lanes; k++) - same = same && (e[k] == g[k] || (anyNaN && isNaNBits(e[k]) && isNaNBits(g[k]))); + for (unsigned k = 0; k < c.lanes; k++) { + if (e[k] == g[k] || (anyNaN && isNaNBits(e[k]) && isNaNBits(g[k]))) + continue; + if (k < byteNormalized) { + const bool bgra = c.format == MTLAttributeFormatUChar4Normalized_BGRA; + const float quotient = static_cast(element[bgra && k != 1 && k != 3 ? 2 - k : k]) / 255.0f; + uint32_t q; + std::memcpy(&q, "ient, 4); + if (ulpsBetween(g[k], q) <= 1) { + allowed++; + continue; + } + } + same = false; + } if (!same && wrong++ < 3) fprintf(stderr, "stage input oracle: format %lu field %s step %lu: thread %u: Metal %08x %08x %08x %08x, " @@ -1568,6 +1658,11 @@ void stageInMatchesMetalOn(id metal, const char *path) e[0], e[1], e[2], e[3], g[0], g[1], g[2], g[3]); } CHECK(wrong == 0); + if (allowed != 0) + fprintf(stderr, + "stage input oracle on %s: format %lu field %s: %u lanes passed within 1 ULP of the correctly " + "rounded quotient, not equal to this GPU's Metal, which multiplies by f32(1/255)\n", + metal.name.UTF8String, static_cast(c.format), c.typeName, allowed); } fprintf(stderr, "stage input oracle: %s (PCI vendor 0x%04x): %u of %zu cases compared\n", metal.name.UTF8String, vendor, compared, cases.size());