#!/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
    # A row of floats written four at a time from a float4 (engine/split_store_tail), the first in
    # one block and the rest where the row holds them, at rows of 5, 6 and 7 floats, and the four
    # in one block at rows of 7: every residue of 16 for the first word's offset.
    1c1ad4164a77a6c10927742ee54ce9b5e6757df96f8eaa3a4e5dcf70eb7c3fe3
    440c5d434c2c77b1147d0ccf9cd482c4f48424f80f1e72042c0e025e3dcd030c
    5cc3b69ff9552e7dc62f5b2f3e89d0c09666f48f9886406506a55842aaf0ead7
    861ff4ddf6f7458effd523d90fdb1d816e4d5d1ba39759ef8755bff0b2243817
    # mul24 and mad24, unsigned and signed, as a scalar and at 2, 3 and 4 lanes
    # (engine/int24_products), with operands in their 24-bit range and outside it.
    5f654fa8f41f1ea09229fc65c9a66261ad4a4ff3fc89c4b3c66851e7daf39194
    36ffb2b4990596138bbf7e50788d8878d0d32ddc4ac7717558ec3ec668768832
    # A sampler or a texture taken from a branch merged at a join (kernel_handle_word_joins): as
    # integer words in a phi and in a select, and as pointers in a phi.
    b0fe67d9b927e646cb76549d72ac73b9c446526a7635d07032d6bdff28362ea5
    2172332f380f4dd6a7ef3166e1148863511532c4aea0677805fdc20f0a60a3a1
    a8649cab0d387804f325ea8fa65e10d7064d06fa076a9d4cfba1a15fe2ca41b1
    d7b7b490f2dfc6a3cbf669b4e3c6ad9d30c93623dca57acc28755923ff3f9427
    # calculate_clamped_lod and calculate_unclamped_lod in a kernel (engine/rfs_kernel_lod) over
    # 2D, 2D-array, 3D and cube textures and in a vertex function, through a runtime sampler without
    # a clamp and with clamps under each mip filter, the clamped level read from the sampler's word.
    ecf328dab422c8a91d4c4ca895bbe95af2e8dfae989a43ee978dc0963d918848
    8f5ff12827c6e096106e8415c6a031818cab0c3b982159fa31f2e8218fcede86
    f46b514ad4907484a4552a7b8fcc9061478a8de4078fce6085fa474ff209a74c
    268ec7e1f6e8e2e00b7e60af234ea2f5ca6ff176a4bcf7301dce5f68aae16d9b
    36313efb0d5bc19b9817d5574c4126c8a0cb242d616825b4898d2da1221ce184
    7ab89b0e1656651a2a07a7e88a2e2361c0e9982c8a3de0ea864246fbfba73798
    47c4985389ccdfe32383df0f0413313bbd59fbff43d1d7d3002ddd87de6b2685
    77869a88dc284419e30cb87528985f6a031e911fbb2a2074116650e1fae0f7d7
    fc4a9ce88fc47734cd9ebd8cc4f38f454b5fb17af7890341a2bbbeb572aff7b4
    fc0539a14aabbb9c8c2ee400bcaabc745d76093eef51858716fc88d95d8e253e
    c2710071e11451fe902a368186bbdfa2899c097edc7881f165f50a807cdbcb98
    805d404243e91650837d93f6787268d8f0833b9aaeaa1ef289ee3f1950d31555
    34bffa47d2d5331f2f6e72db8e0b18f173c04573fb6904a27bda7be45545e292
    436e2dee4c0ad2590acf0b702d0538176293e90443045120cd47ca1376800eae
    # The same queries through a sampler bound with LOD clamps of the bind's own
    # (setSamplerState:lodMinClamp:lodMaxClamp:atIndex: and its vertex form), which the query reads.
    3ed7967d1502fdd8a8ffcf861a14a850f81794b0787d78bdd2584829aceca072
    5f2bb17238a054de6944763aee705892434e4f28d75fe317dc25a2ca188f42eb
    92b04960b3e4d5d296b5720b96ac59a2a5fd640d7a0cef2439c7e9121245e989
    9b9369bbfa1f905eb3e908740ae8d10f632ca0a73f020eb995fcce6666a60972
    ea3e144e1f7a0ea5a58a5951092b095d0b12f727411fce501e3ec8c950400d6b
    c93359f4c8d94d91be2e2f7335a9a235cf1a322dadb12344bd1b2ee0581dd396
    # A texture of memory_coherence_device handed between two threadgroups through a release
    # fence, a device-scope flag and an acquire fence (engine/coherent_texture_handoff): no read
    # that saw every writer's flag reads the texel's earlier value.
    32697a0895a8dcf85651da45ed7d6139ae86bc80f18f986eb28a72f06eb3b061
)

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
