Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
Textures run: six return the exact texel, and none returns a wrong number
vcvkke25d4a9ff54d
diff --git a/docs/air.md b/docs/air.mdindex 8e623aa..a8baf3e 100644--- a/docs/air.md+++ b/docs/air.md@@ -393,16 +393,38 @@ an unhandled metadata key), so "features not yet handled" is a ranked list, not`air.visible` library functions, which are not entry points at all (below). Counting everyentry point rather than every module: 275 of 321, 86%. Nothing has run on the Adreno.- **M3** — textures and samplers, each form with a test that renders a known image.- **Translated and compiled, not yet rendered.** The family took the corpus from 275 modules to+ **Translated, compiled, and six of them rendered and checked.** The family took the corpus from 275 modules to313, and all 313 compile for the Adreno. What is *not* done is M3's acceptance test: not one- texture operation has been run, because `air_run` binds no textures yet and the CPU reference- cannot judge a shader whose coordinate comes from a varying. The forms written are 2D, 3D, 1D,+ texture operation had been run until `air_run` learned to bind one. The forms written are 2D, 3D, 1D,cube and their array and multisample variants, for sampling (plain, explicit level, bias,minimum-level clamp), gathering, fetching by integer coordinate, writing, and the queries(width, height, depth, array size, level count, sample count). Refused with a named error, notguessed at: compare sampling (`sample_compare`, `gather_compare`), texture buffers, and a depthtexture whose aspect selector is not 1.+ **Six texture shaders have now been run and return the exact texel**+ (`tools/air/texcheck.sh`). `air_run` binds a texture whose texel is+ `(100+x, 200+y, 300+x+y+z, 400)` — 4×4 for a 2D sampler, 4×4×4 for a 3D one — a nearest+ sampler with clamped addressing, and a coordinate at the centre of texel (1, 1). A single+ sample or fetch must then return that texel *exactly*: nearest filtering at a texel centre has+ no weights to round and clamping has no arithmetic. 2D and 3D, in float, int and uint, all six+ return it; **none returns anything else**.++ Binding the matching target mattered more than it looks: with a 2D texture bound for a 3D+ sampler the three 3D shaders also "passed", by accident. With a real 4×4×4 texture they return+ `101 201 303 400` — the 3D texel — and only then are they evidence.++ Of 55 texture shaders, 49 are not run, and the reasons are the honest part: 24 are compute+ kernels (`air_run` draws, it does not dispatch); 11 are `air.visible` library functions; 4+ sample a multisample texture the harness does not bind; 1 writes only depth; 1 hits an+ assertion in softpipe's own NIR-to-TGSI; 8 are other translation refusals. **Not one returns a+ wrong number** — the failures are all a refusal to run, which is the shape of failure to want.++ `texturedFragment3DDepth` is worth naming because it cost a wrong-looking result: it returns+ `{undef, depth}` and stores *nothing* to its colour attachment, so reading the colour gave the+ clear value — a number that means nothing and looks like an answer. `air_run` now checks that+ the attachment it reads is one the shader actually stores to, and refuses when it is not.+The sampler state is the gap that matters for correctness. Metal's `constexpr sampler` arrivesas a global holding one 64-bit word, and **that word's layout is not decoded**: the frontendreports it to the runtime verbatim (`air_resource::sampler_state`) and assigns the sampler andiff --git a/tools/air/texcheck.sh b/tools/air/texcheck.shnew file mode 100755index 0000000..23883b7--- /dev/null+++ b/tools/air/texcheck.sh@@ -0,0 +1,80 @@+#!/bin/bash+# Run every texture shader of the corpus and check the texel it returns.+#+# air_run binds a texture whose texel (x, y, z) is (100+x, 200+y, 300+x+y+z, 400) --+# 4x4 for a 2D sampler, 4x4x4 for a 3D one, because a 3D sample out of a 2D+# texture can come back right by accident and that is not evidence --+# a nearest sampler with clamped addressing, and a coordinate at the centre of+# texel (1, 1). A shader that samples or fetches once must therefore return+# exactly 101 201 302 400 -- an equality, not a tolerance: nearest filtering at+# a texel centre has no weights to round and clamping has no arithmetic.+#+# What this does NOT check is anything that depends on Metal's own sampler+# state: the two modules of the corpus that declare a `constexpr sampler`+# carry it as a 64-bit word that is not decoded (docs/air.md). Here the API+# chooses the sampler, so what is verified is the texture operation and not+# Apple's filtering or addressing.+#+# A shader that combines several samples, or scales the coordinate, will not+# return the bare texel; those are listed as "other" with what they did return,+# which is a thing to read rather than a failure.+#+# usage: texcheck.sh [FILE.metallib ...]+set -u+ROOT=$(cd "$(dirname "$(readlink -f "$0")")/../.." && pwd)+AIR_RUN=${AIR_RUN:-$ROOT/build/mesa-host/build/src/gallium/drivers/freedreno/air_run}+AIR2NIR=${AIR2NIR:-$ROOT/build/mesa-host/build/src/compiler/air/air2nir}+[ -x "$AIR_RUN" ] || { echo "texcheck.sh: run tools/air/mk-host.sh first"; exit 1; }++LIBS=("$@")+if [ ${#LIBS[@]} = 0 ]; then+ LIBS=(+ "$ROOT/ios165/rootfs/System/Library/VideoDecoders/AV1SW.metallib"+ "$ROOT/ios165/rootfs/System/Library/Extensions/AppleMetalGLRenderer.bundle/default.metallib"+ "$ROOT/ios165/rootfs/System/Library/CoreImage/PortraitFilters.cifilter/default.metallib"+ "$ROOT/ios165/rootfs/System/Library/CoreImage/PortraitFilters.cifilter/portrait_filters.metallib"+ )+fi++exact=0 other=0 refused=0 total=0+for lib in "${LIBS[@]}"; do+ [ -f "$lib" ] || continue+ # the functions that touch a texture at all+ "$AIR2NIR" --report "$lib" 2>/dev/null | while IFS= read -r line; do+ case "$line" in *$'\t'calls=*texture*) ;; *) continue ;; esac+ printf '%s\n' "${line%%$'\t'*}" | sed 's/^module=//'+ done > /tmp/texcheck.names+ while IFS= read -r fn; do+ [ -n "$fn" ] || continue+ total=$((total + 1))+ out=$("$AIR_RUN" "$lib" "$fn" 2>&1)+ got=$(printf '%s\n' "$out" | sed -n '/came back as/,$p' | sed -n '2,5p' |+ tr -d ' ' | paste -sd' ')+ # air_run knows what it bound, so it says what to expect; the 3D case+ # wants a different texel from the 2D one+ WANT=$(printf '%s\n' "$out" | sed -n 's/^air_run: expected \(.*\) (the centre.*/\1/p')+ if [ -z "$got" ]; then+ # the reason, not the first line: air_run says why on stderr, and+ # "<name> on softpipe" is only which screen it got+ why=$(printf '%s\n' "$out" |+ grep -E 'this harness|is a .* shader|Assertion|stage: |error|no gallium|does not bind|stores nothing' |+ head -1)+ [ -n "$why" ] || why=$(printf '%s\n' "$out" | tail -1)+ printf 'refused %-44s %s\n' "$fn" "${why:0:90}"+ refused=$((refused + 1))+ elif [ -n "$WANT" ] && [ "$got" = "$(echo $WANT | tr ' ' '\n' | paste -sd' ')" ]; then+ printf 'EXACT %-44s %s\n' "$fn" "$got"+ exact=$((exact + 1))+ else+ printf 'other %-44s %s\n' "$fn" "$got"+ other=$((other + 1))+ fi+ done < /tmp/texcheck.names+done+rm -f /tmp/texcheck.names++echo+echo "texcheck: $total texture shaders run: $exact returned the expected texel exactly,"+echo "texcheck: $other returned something else, $refused could not be run"+echo "texcheck: the sampler here is the API's (nearest, clamped); Metal's own sampler word"+echo "texcheck: is not decoded, so nothing here verifies Apple's filtering or addressing"diff --git a/tools/mesa/air/air_run.c b/tools/mesa/air/air_run.cindex 64b122e..71302af 100644--- a/tools/mesa/air/air_run.c+++ b/tools/mesa/air/air_run.c@@ -45,6 +45,7 @@#endif#include "util/format/u_format.h"#include "util/u_inlines.h"+#include "util/u_sampler.h"#include "util/u_debug.h"#include "util/u_memory.h"@@ -61,6 +62,32 @@#define WIDTH 4#define HEIGHT 4+/* The texture bound for every sampled texture the shader declares: four by+ * four, 32-bit floats, each texel a number that says which texel it is. The+ * sampler is nearest with no mip filter and clamped addressing, and the+ * coordinate the vertex shader hands over is the centre of texel (1, 1), so+ * a correct sample is an exact equality and not a tolerance -- no filtering+ * weights, no wrap arithmetic, nothing to round.+ *+ * This verifies the texture operations whose sampler the API chooses (nine+ * modules of the corpus) and those that take no sampler at all (twenty-nine,+ * which fetch by integer coordinate). It cannot verify the two whose sampler+ * the shader declares itself: that needs Metal's 64-bit sampler word decoded,+ * which it is not.+ */+#define TEX_SIZE 4+#define TEX_X 1+#define TEX_Y 1++static void+texel_value(unsigned x, unsigned y, unsigned z, float out[4])+{+ out[0] = 100.0f + x;+ out[1] = 200.0f + y;+ out[2] = 300.0f + x + y + z;+ out[3] = 400.0f;+}+#ifdef __APPLE__/** A program that dies on this port leaves nothing behind: the kernel says@@ -132,7 +159,7 @@ pattern_byte(unsigned i)* its own that is a separate run.*/static nir_shader *-quad_vertex_shader(const nir_shader_compiler_options *options)+quad_vertex_shader(const nir_shader_compiler_options *options, unsigned num_varyings){nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, options, "quad");nir_variable *in = nir_variable_create(b.shader, nir_var_shader_in,@@ -144,8 +171,23 @@ quad_vertex_shader(const nir_shader_compiler_options *options)out->data.location = VARYING_SLOT_POS;out->data.driver_location = 0;nir_store_var(&b, out, nir_load_var(&b, in), 0xf);++ /* One constant coordinate per varying the fragment shader reads: the+ * centre of texel (TEX_X, TEX_Y), so every fragment samples the same+ * texel and the expected value is a single known one.+ */+ float c = (TEX_X + 0.5f) / TEX_SIZE;+ float d = (TEX_Y + 0.5f) / TEX_SIZE;+ for (unsigned i = 0; i < num_varyings; i++) {+ nir_variable *v = nir_variable_create(b.shader, nir_var_shader_out,+ glsl_vec4_type(), "varying");+ v->data.location = VARYING_SLOT_VAR0 + i;+ v->data.driver_location = 1 + i;+ v->data.interpolation = INTERP_MODE_SMOOTH;+ nir_store_var(&b, v, nir_imm_vec4(&b, c, d, c, 1.0f), 0xf);+ }b.shader->num_inputs = 1;- b.shader->num_outputs = 1;+ b.shader->num_outputs = 1 + num_varyings;return b.shader;}@@ -349,6 +391,34 @@ main(int argc, char **argv)return 1;}+ /* A shader may declare a render target and never store to it:+ * texturedFragment3DDepth returns {undef, depth} and its only real output+ * is FRAG_RESULT_DEPTH. Reading the colour then gives the clear value,+ * which is a number that means nothing -- worse than no number, because it+ * looks like a result. This harness binds no depth attachment, so say so.+ */+ bool stores_colour = false;+ nir_foreach_function_impl(impl, fs) {+ nir_foreach_block(block, impl) {+ nir_foreach_instr(instr, block) {+ if (instr->type != nir_instr_type_intrinsic)+ continue;+ nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);+ if (intr->intrinsic != nir_intrinsic_store_deref)+ continue;+ nir_deref_instr *d = nir_src_as_deref(intr->src[0]);+ if (d && d->deref_type == nir_deref_type_var &&+ d->var->data.location == (int)(FRAG_RESULT_DATA0 + read_rt))+ stores_colour = true;+ }+ }+ }+ if (!stores_colour) {+ fprintf(stderr, "air_run: %s stores nothing to a colour attachment -- its output is "+ "depth or stencil, which this harness does not read back\n", name);+ return 1;+ }+finalize_for_gallium(fs);struct pipe_context *pipe = screen->context_create(screen, NULL, 0);@@ -424,6 +494,118 @@ main(int argc, char **argv)free(bits);}+ /* every sampled texture the shader declares, and a sampler for it */+ unsigned num_tex = 0;+ for (unsigned i = 0; i < info.num_resources; i++)+ if (info.resources[i].kind == AIR_RESOURCE_TEXTURE)+ num_tex = MAX2(num_tex, info.resources[i].index + 1);+ if (num_tex) {+ /* The texture's format follows what the shader samples into: a Metal+ * texture2d<uint> read out of a float texture gives numbers that are+ * neither right nor obviously wrong.+ */+ enum pipe_format tex_format = PIPE_FORMAT_R32G32B32A32_FLOAT;+ bool tex_is_float = true;+ /* and the target, which has to match too: a 3D sample from a 2D+ * texture can come back right by accident, which is not evidence+ */+ enum pipe_texture_target target = PIPE_TEXTURE_2D;+ enum glsl_sampler_dim dim = GLSL_SAMPLER_DIM_2D;+ nir_foreach_function_impl(impl, fs) {+ nir_foreach_block(block, impl) {+ nir_foreach_instr(instr, block) {+ if (instr->type != nir_instr_type_tex)+ continue;+ nir_tex_instr *t = nir_instr_as_tex(instr);+ nir_alu_type base = nir_alu_type_get_base_type(t->dest_type);+ if (base == nir_type_int) {+ tex_format = PIPE_FORMAT_R32G32B32A32_SINT;+ tex_is_float = false;+ } else if (base == nir_type_uint) {+ tex_format = PIPE_FORMAT_R32G32B32A32_UINT;+ tex_is_float = false;+ }+ dim = t->sampler_dim;+ if (dim == GLSL_SAMPLER_DIM_3D)+ target = PIPE_TEXTURE_3D;+ else if (dim == GLSL_SAMPLER_DIM_MS)+ target = PIPE_TEXTURE_2D; /* refused below */+ goto have_format;+ }+ }+ }+ have_format:;+ if (dim == GLSL_SAMPLER_DIM_MS) {+ fprintf(stderr, "air_run: %s samples a multisample texture; this harness binds "+ "none, so it is not run rather than run against the wrong thing\n",+ name);+ return 1;+ }+ if (dim != GLSL_SAMPLER_DIM_2D && dim != GLSL_SAMPLER_DIM_3D) {+ fprintf(stderr, "air_run: %s samples a texture of a kind this harness does not "+ "bind (dim %u)\n", name, dim);+ return 1;+ }+ unsigned depth = target == PIPE_TEXTURE_3D ? TEX_SIZE : 1;+ unsigned tex_z = target == PIPE_TEXTURE_3D ? TEX_Y : 0;++ float texels[TEX_SIZE * TEX_SIZE * TEX_SIZE * 4];+ int32_t itexels[TEX_SIZE * TEX_SIZE * TEX_SIZE * 4];+ for (unsigned z = 0; z < depth; z++) {+ for (unsigned y = 0; y < TEX_SIZE; y++) {+ for (unsigned x = 0; x < TEX_SIZE; x++) {+ unsigned i = ((z * TEX_SIZE + y) * TEX_SIZE + x) * 4;+ texel_value(x, y, z, &texels[i]);+ for (unsigned c = 0; c < 4; c++)+ itexels[i + c] = (int32_t)texels[i + c];+ }+ }+ }++ struct pipe_resource ttempl = {+ .target = target,+ .format = tex_format,+ .width0 = TEX_SIZE, .height0 = TEX_SIZE, .depth0 = depth, .array_size = 1,+ .bind = PIPE_BIND_SAMPLER_VIEW,+ };+ struct pipe_resource *tex = screen->resource_create(screen, &ttempl);+ struct pipe_box box = { .width = TEX_SIZE, .height = TEX_SIZE, .depth = depth };+ pipe->texture_subdata(pipe, tex, 0, PIPE_MAP_WRITE, &box,+ tex_is_float ? (const void *)texels : (const void *)itexels,+ TEX_SIZE * 4 * sizeof(float),+ TEX_SIZE * TEX_SIZE * 4 * sizeof(float));++ void *sampler = pipe->create_sampler_state(pipe, &(struct pipe_sampler_state){+ .wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE,+ .wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE,+ .wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE,+ .min_img_filter = PIPE_TEX_FILTER_NEAREST,+ .mag_img_filter = PIPE_TEX_FILTER_NEAREST,+ .min_mip_filter = PIPE_TEX_MIPFILTER_NONE,+ });++ struct pipe_sampler_view *views[PIPE_MAX_SHADER_SAMPLER_VIEWS] = { 0 };+ void *samplers[PIPE_MAX_SAMPLERS] = { 0 };+ for (unsigned i = 0; i < num_tex && i < PIPE_MAX_SAMPLERS; i++) {+ struct pipe_sampler_view vtempl = { 0 };+ u_sampler_view_default_template(&vtempl, tex, tex->format);+ views[i] = pipe->create_sampler_view(pipe, tex, &vtempl);+ samplers[i] = sampler;+ }+ pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, num_tex, 0, false, views);+ pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, num_tex, samplers);++ float want[4];+ texel_value(TEX_X, TEX_Y, tex_z, want);+ printf("air_run: %u texture%s bound: %ux%ux%u %s, texel (x,y,z) = "+ "(100+x, 200+y, 300+x+y+z, 400), nearest, clamped\n",+ num_tex, num_tex == 1 ? "" : "s", TEX_SIZE, TEX_SIZE, depth,+ util_format_short_name(tex_format));+ printf("air_run: expected %.9g %.9g %.9g %.9g (the centre of texel %u,%u,%u sampled "+ "once)\n", (double)want[0], (double)want[1], (double)want[2], (double)want[3],+ TEX_X, TEX_Y, tex_z);+ }+/* the quad */static const float verts[] = {-1, -1, 0, 1, 1, -1, 0, 1, -1, 1, 0, 1, 1, 1, 0, 1,@@ -441,7 +623,7 @@ main(int argc, char **argv)});pipe->bind_vertex_elements_state(pipe, velem);- nir_shader *vs = quad_vertex_shader(options);+ nir_shader *vs = quad_vertex_shader(options, info.num_varyings);finalize_for_gallium(vs);void *vs_cso = pipe->create_vs_state(pipe, &(struct pipe_shader_state){.type = PIPE_SHADER_IR_NIR, .ir.nir = vs,