#!/usr/bin/env bash
# The "Qualify public cases on native Vulkan (Linux)" step of .github/workflows/ci.yml, shared
# with scripts/linux-gate-ci so the two cannot drift.
#
#   scripts/qualify-public-cases <bin-dir>     # <bin-dir> holds the built validation binaries
#
# Executes the owned public cases with the engine (the only engine the validation binaries run
# since cutover X2b) on the CPU Vulkan ICD (lavapipe), against their committed
# Metal observations in the reference list's primary environment
# (crates/metal2vulkan-validation/cutover/c3-references.json), then rebuilds the disposable index and
# requires each case's engine slot to be current. Run it from the repository root.
#
# Rows written here are ephemeral CI evidence under the operator environment `ci-lavapipe`, which
# no C3 venue names: a runner's lavapipe is not a venue's declared device, so a
# venue's environment would refuse it (`venue_mismatch`), and these rows are no venue's evidence.
#
# A public checkout has no private store, so `corpus-index` indexes only the rows of public
# sources and reports the committed rows of private AIR it left out (`scope=public-only ...`);
# any failure of it fails the step.
set -euo pipefail

[ "$#" -eq 1 ] || { echo "usage: scripts/qualify-public-cases <bin-dir>" >&2; exit 64; }
bin=$1
export LP_NUM_THREADS=1

environment=ci-lavapipe
metal=$(python3 -c 'import json, sys; print(json.load(open(sys.argv[1]))["primary"]["environment_id"])' \
    crates/metal2vulkan-validation/cutover/c3-references.json)
cases=(
    d9ce86618e0924966fbb1e7de63761ef471bb6e85b592a70193a26375ec0423f
    4659fedddd1695fba79c3faa797ec3be647e3fd1a87ecec23bde55ccbfbcf656
    1e54315eb0ffcac633d4992c577f3e6ac384ed1bdf2321887c1f37242c00c887
    # A kernel that asks two bound visible-function tables whether they are null and their sizes,
    # and one that calls through a table of four entries, one of them null.
    3a8f75e182b71ba42a94dd39d178cf6789010fbe1355b56944adf0a1c75ba807
    af884d65cb584518fcb8b7319dfe9deff15b20604cfc68f6d72dd97ca3113536
    # An AIR 1.8 fragment (engine/air_1_8_sampling) that samples plainly and by gradient through
    # an argument sampler: the forms an AIR library declares before AIR 2.0.
    884ad7d75a221a11480581ae8d55ef5773defdd021c4fc56c6bde51445e87d37
    # A float (engine/fc_float_gate) and a half (engine/fc_half_gate) function constant that gate
    # a kernel's argument and a fragment's sample mask: Metal keeps what they gate where the
    # value's bits are not all zero (1.5, -0.0 and a quiet NaN), and drops it at +0.0 and unsupplied.
    3a843c7911108e011ac425010de8918ea2ffaabc680efe846d1fa60427bb89da
    ad28780327b4d7bfdfbeba9985f9a065b96766455d39f2187a45aa667acbcb9d
    c7440f65e3dc4b2a20e5a80e1303bd4733973ef4a9d7e0da4f8b2d852b82ead8
    8e22d09b34c6911bf89e9a3819fe8935c877298faf69b235cba74f83cc9f0d1e
    4db5409a6e7d85d95d16cf6a0c2c6a386ee0a2ca281834c5ebf7d54481a115e5
    0ec889df18ff07ab70791b6116fe2dd30331251d9715e62f6644c561ff411426
    03cbdd46a5c0dbe4edcd3aaee7205110805835347288f0f8269131e743362d4d
    8235cf2527e58712c73b1baacb72b2cd576cf45786fe3024b3776d88c969b71f
    579eb7c4c5c2eb7cd30871124eac021cbb131e7931e514cd6ee03234bcc68983
    16bb9c40891adce3e478f143c4d0cf1abd093e6b6c8ba1b5793bcff70f50487a
    deeca6b1a5f4a5daa333c7cb757bd330eccc2f50870a4b73afe7b3704328c853
    0d652c7e36e231111b77232853ae8237603e2eaf0205a30a08c9e517e2b27dfe
    4534a2f6acfe169228698c79410b9f69a2a27193be26d24df48f04bfa212f134
    18c8aab797757953abb8725f36053c4607a1829a8b4f1dcb8c5b877d697fdf1b
    b9bfa9f2737e76d6052e4f705891209af9e7306206b5999acae1a9c30d7a13aa
)

for case_id in "${cases[@]}"; do
    "$bin/corpus-vulkan" \
        --case-id "$case_id" \
        --metal-environment-id "$metal" \
        --environment-id "$environment" \
        --device-type cpu
done

"$bin/corpus-index"
"$bin/corpus-index" --check
named=()
for case_id in "${cases[@]}"; do
    named+=(--case-id "$case_id")
done
status=$("$bin/corpus-status" "${named[@]}")
echo "$status"
incomplete=0
tab=$'\t'
slot="$environment@engine"
for case_id in "${cases[@]}"; do
    if ! grep -Fqx "slot${tab}${case_id}${tab}vulkan${tab}${slot}${tab}current" <<<"$status"; then
        echo "native Vulkan qualification of $case_id in $slot is not current" >&2
        incomplete=1
    fi
done
if [ "$incomplete" -ne 0 ]; then
    echo "native Vulkan qualification is incomplete" >&2
    exit 1
fi
