Skip to content

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: 275 modules translate becomes 313, and all 313 compile for the Adreno

vcvkk287a95cc5e4b
diff --git a/docs/air-report.md b/docs/air-report.md
index 7fe96fe..3be1ef9 100644
--- a/docs/air-report.md
+++ b/docs/air-report.md
@@ -11,15 +11,15 @@ correct**: correctness needs the CPU reference and a run on the GPU.
| --- | --- | --- |
| container | 456 | 100% |
| reader | 456 | 100% |
-| translate | 275 | 60% |
-| ir3 | 275 | 60% |
+| translate | 313 | 69% |
+| ir3 | 313 | 69% |
## By stage of the shader
| what the module is | modules | translate | ir3 |
| --- | --- | --- | --- |
-| fragment | 271 | 256 | 256 |
-| kernel | 47 | 16 | 16 |
+| fragment | 271 | 270 | 270 |
+| kernel | 47 | 40 | 40 |
| vertex | 3 | 3 | 3 |
| visible | 135 | 0 | 0 |
@@ -29,10 +29,10 @@ The container's TYPE tag agrees with the module's own metadata on 456 of 456; 0
| library | modules | translate | ir3 |
| --- | --- | --- | --- |
-| AppleMetalGLRenderer.bundle/default.metallib | 274 | 259 | 259 |
-| PortraitFilters.cifilter/default.metallib | 36 | 16 | 16 |
+| AppleMetalGLRenderer.bundle/default.metallib | 274 | 273 | 273 |
+| PortraitFilters.cifilter/default.metallib | 36 | 34 | 34 |
| PortraitFilters.cifilter/portrait_filters.metallib | 135 | 0 | 0 |
-| VideoDecoders/AV1SW.metallib | 11 | 0 | 0 |
+| VideoDecoders/AV1SW.metallib | 11 | 6 | 6 |
## Modules that call no `air.*` at all
@@ -43,16 +43,11 @@ The container's TYPE tag agrees with the module's own metadata on 456 of 456; 0
| modules | where it stops |
| --- | --- |
| 135 | stage: air.visible, a library function (not yet) |
-| 13 | intrinsic: air.get_width_texture_2d |
-| 10 | intrinsic: air.sample_texture_3d |
-| 7 | intrinsic: air.read_texture_2d |
-| 5 | intrinsic: air.sample_texture_2d |
-| 4 | intrinsic: air.get_num_samples_texture_2d_ms |
-| 3 | intrinsic: air.write_texture_2d |
-| 1 | intrinsic: air.sample_depth_2d |
-| 1 | intrinsic: air.read_depth_2d_ms |
-| 1 | intrinsic: air.read_texture_2d_ms |
-| 1 | intrinsic: air.gather_texture_2d |
+| 2 | instruction: a shuffle mask that is not constant |
+| 2 | intrinsic: a call of _ZL13FilmGrainLumaN5metal9texture2dItLNS_6accessE1EvEES2_NS0_IsLS1_1EvEERU11MTLconstantK15FilmGrainParamsPU11MTLconstantKhDv2_j, a function in the module |
+| 2 | intrinsic: a call of _ZL15FilmGrainChromaN5metal9texture2dItLNS_6accessE1EvEES2_NS0_IsLS1_1EvEES2_S2_RU11MTLconstantK15FilmGrainParamsPU11MTLconstantKhS8_Dv2_j, a function in the module |
+| 1 | intrinsic: a call of ___metal_fract_v2float, a function in the module |
+| 1 | intrinsic: a call of ___metal_fract_v4float, a function in the module |
## The `air.*` functions the corpus calls
diff --git a/docs/air.md b/docs/air.md
index c5cdc97..7171472 100644
--- a/docs/air.md
+++ b/docs/air.md
@@ -387,12 +387,28 @@ an unhandled metadata key), so "features not yet handled" is a ranked list, not
- **M2** — the 283 modules with no `air.*` call: a stated fraction reach *ir3*, a growing subset
*softpipe*/*adreno*.
**262 of the 283 reach ir3**, 255 of them agree with LLVM numerically, and those same 255 draw
- a matching pixel. The other 21 are
+ a matching pixel. (With textures done the whole corpus is at 313 of 456; this milestone's own
+ count is the intrinsic-free subset.) The other 21 are
`air.visible` library functions, which are not entry points at all (below). Counting every
entry 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.
- Not started, and it is what nearly everything left is waiting for: 46 modules stop at a texture
- call, including all 11 of AV1SW's.
+ **Translated and compiled, not yet rendered.** The family took the corpus from 275 modules to
+ 313, 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,
+ 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, not
+ guessed at: compare sampling (`sample_compare`, `gather_compare`), texture buffers, and a depth
+ texture whose aspect selector is not 1.
+
+ The sampler state is the gap that matters for correctness. Metal's `constexpr sampler` arrives
+ as a global holding one 64-bit word, and **that word's layout is not decoded**: the frontend
+ reports it to the runtime verbatim (`air_resource::sampler_state`) and assigns the sampler an
+ index after the API's. Until it is decoded, nothing knows whether a sample is meant to be
+ nearest or linear, clamped or repeated — which is exactly the sort of thing that renders
+ plausibly and wrongly. `AGXCompilerCore` has a `sampler_words` symbol, so the layout is
+ discoverable the same way the vocabulary was.
- **M4** — atomics, barriers, threadgroup memory, compute against the CPU reference.
Atomics, the barrier and threadgroup memory are written (`air_intrinsics.c`) and 16 kernels
compile through ir3 with them; none has been *run*, so M4's acceptance test is untouched.
diff --git a/tools/mesa/air/air_intrinsics.c b/tools/mesa/air/air_intrinsics.c
index b6d680c..94cb8db 100644
--- a/tools/mesa/air/air_intrinsics.c
+++ b/tools/mesa/air/air_intrinsics.c
@@ -72,7 +72,8 @@ math_op(struct air_ctx *c, const struct air_inst *in, const struct air_name *n,
* a texture handle used as a number
*/
if (strstr(op, "texture") || strstr(op, "_depth_") || !strncmp(op, "sample", 6) ||
- !strncmp(op, "gather", 6) || !strncmp(op, "read_", 5) || !strncmp(op, "write_", 6)) {
+ !strncmp(op, "gather", 6) || !strncmp(op, "read_", 5) || !strncmp(op, "write_", 6) ||
+ strstr(op, "_lod_")) {
*handled = false;
return true;
}
@@ -421,6 +422,508 @@ llvm_op(struct air_ctx *c, const struct air_inst *in, const char *name)
return air_error(c, "intrinsic: %s", name);
}
+/* -- textures ------------------------------------------------------------
+ *
+ * The largest family and the one with the long tail. Its vocabulary is
+ * Apple's, read off AGXCompilerCore the same way the arithmetic was
+ * (docs/air.md section 3.2): the names there end in an underscore --
+ * air.sample_texture_, air.gather_compare_depth_, air.get_num_mip_levels_texture_
+ * -- because the dimensionality is appended to them. That is why the earlier
+ * survey counted "about 117 get_*_texture_* variants": it is some twenty
+ * operations crossed with nine dimensionalities, not a hundred separate
+ * things.
+ *
+ * The signatures are measured from the corpus, not guessed:
+ *
+ * sample_<kind>_<dim> (tex, samp, [plane,] coord, i1 has_offset, offset,
+ * i1 lod_is_level, float lod_or_bias, float min_lod,
+ * i32 flags) -> { <4 x T>, i8 }
+ * gather_<kind>_<dim> (tex, samp, coord, i1 has_offset, offset,
+ * i32 component, i32 flags) -> { <4 x T>, i8 }
+ * read_<kind>_<dim> (tex, [plane,] coord, i32 lod_or_sample, i32 flags)
+ * write_<kind>_<dim> (tex, coord, value, i32 lod, i32 flags)
+ * get_<what>_<kind>_<dim> (tex[, i32 lod]) -> i32
+ *
+ * The i1 before the level is read as "the float is a level, not a bias", so
+ * (false, 0.0) is a plain sample -- which is what every one of the corpus's
+ * sample calls passes. The level and bias forms are written here but no
+ * module exercises them.
+ *
+ * The second field of the returned struct is Metal's sparse-residency byte.
+ * No module in the corpus reads it (every extractvalue of a texture result
+ * takes field 0), so it is produced as zero rather than invented.
+ */
+
+struct air_tex_name {
+ const char *op; /* "sample", "gather", "read", "write", "get", "lod" */
+ const char *query; /* for "get": width, height, depth, array_size, ... */
+ bool compare; /* sample_compare / gather_compare */
+ bool depth; /* a depth texture rather than a colour one */
+ enum glsl_sampler_dim dim;
+ bool is_array, is_ms, is_buffer;
+};
+
+static bool
+eat(const char **s, const char *prefix)
+{
+ size_t n = strlen(prefix);
+ if (strncmp(*s, prefix, n))
+ return false;
+ *s += n;
+ return true;
+}
+
+/* air.<op>_<kind>_<dim> -> the pieces, or false when it is not a texture name */
+static bool
+parse_tex_name(const char *name, struct air_tex_name *t)
+{
+ memset(t, 0, sizeof(*t));
+ const char *s = name;
+
+ if (eat(&s, "sample_compare_")) {
+ t->op = "sample";
+ t->compare = true;
+ } else if (eat(&s, "sample_")) {
+ t->op = "sample";
+ } else if (eat(&s, "gather_compare_")) {
+ t->op = "gather";
+ t->compare = true;
+ } else if (eat(&s, "gather_")) {
+ t->op = "gather";
+ } else if (eat(&s, "read_")) {
+ t->op = "read";
+ } else if (eat(&s, "write_")) {
+ t->op = "write";
+ } else if (eat(&s, "calculate_clamped_lod_") || eat(&s, "calculate_unclamped_lod_")) {
+ t->op = "lod";
+ } else if (eat(&s, "get_")) {
+ t->op = "get";
+ static const char *queries[] = {
+ "num_mip_levels_", "num_samples_", "array_size_", "width_", "height_", "depth_",
+ };
+ static const char *names[] = {
+ "num_mip_levels", "num_samples", "array_size", "width", "height", "depth",
+ };
+ for (unsigned i = 0; i < ARRAY_SIZE(queries); i++) {
+ if (eat(&s, queries[i])) {
+ t->query = names[i];
+ break;
+ }
+ }
+ if (!t->query)
+ return false;
+ } else {
+ return false;
+ }
+
+ if (eat(&s, "texture_buffer_1d")) {
+ t->dim = GLSL_SAMPLER_DIM_BUF;
+ t->is_buffer = true;
+ return *s == 0;
+ }
+ if (eat(&s, "depth_"))
+ t->depth = true;
+ else if (!eat(&s, "texture_"))
+ return false;
+
+ /* the dimensionality, longest first so that 2d_ms_array beats 2d */
+ static const struct {
+ const char *suffix;
+ enum glsl_sampler_dim dim;
+ bool array, ms;
+ } dims[] = {
+ { "cube_array", GLSL_SAMPLER_DIM_CUBE, true, false },
+ { "2d_ms_array", GLSL_SAMPLER_DIM_MS, true, true },
+ { "2d_array", GLSL_SAMPLER_DIM_2D, true, false },
+ { "1d_array", GLSL_SAMPLER_DIM_1D, true, false },
+ { "2d_ms", GLSL_SAMPLER_DIM_MS, false, true },
+ { "cube", GLSL_SAMPLER_DIM_CUBE, false, false },
+ { "1d", GLSL_SAMPLER_DIM_1D, false, false },
+ { "2d", GLSL_SAMPLER_DIM_2D, false, false },
+ { "3d", GLSL_SAMPLER_DIM_3D, false, false },
+ };
+ for (unsigned i = 0; i < ARRAY_SIZE(dims); i++) {
+ if (!strcmp(s, dims[i].suffix)) {
+ t->dim = dims[i].dim;
+ t->is_array = dims[i].array;
+ t->is_ms = dims[i].ms;
+ return true;
+ }
+ }
+ return false;
+}
+
+static unsigned
+coord_components_of(const struct air_tex_name *t)
+{
+ unsigned n = t->dim == GLSL_SAMPLER_DIM_1D ? 1 :
+ t->dim == GLSL_SAMPLER_DIM_3D ? 3 :
+ t->dim == GLSL_SAMPLER_DIM_CUBE ? 3 :
+ t->dim == GLSL_SAMPLER_DIM_BUF ? 1 : 2;
+ return n + (t->is_array ? 1 : 0);
+}
+
+/* the result type of a texture operation, from the LLVM type it returns */
+static nir_alu_type
+tex_dest_type(struct air_ctx *c, const struct air_type *ret, const struct air_name *n)
+{
+ const struct air_type *t = ret;
+ if (t->kind == AIR_TYPE_STRUCT && t->num_members)
+ t = t->members[0];
+ const struct air_type *scalar = air_type_scalar(t);
+ unsigned bits = air_type_bit_size(scalar);
+ if (air_type_is_float(scalar))
+ return nir_type_float | bits;
+ /* the signedness is in the name, not in the type: .s or .u */
+ for (unsigned i = 1; i < n->num; i++) {
+ if (!strcmp(n->part[i], "s"))
+ return nir_type_int | bits;
+ if (!strcmp(n->part[i], "u"))
+ return nir_type_uint | bits;
+ }
+ return nir_type_uint | bits;
+}
+
+/* The pointer a texture or sampler argument carries. */
+static bool
+tex_handle(struct air_ctx *c, const struct air_inst *in, unsigned operand,
+ enum air_ptr_class want, struct air_ptr *out)
+{
+ struct air_val *v = air_get(c, in->operands[operand]);
+ if (!v)
+ return false;
+ if (v->kind != AIR_VAL_PTR || v->ptr.class != want)
+ return air_error(c, "intrinsic: argument %u is not a %s", operand - 1,
+ want == AIR_PTR_TEXTURE ? "texture" : "sampler");
+ *out = v->ptr;
+ return true;
+}
+
+/* a constant i1/i32 argument, which these intrinsics use as flags */
+static bool
+tex_flag(struct air_ctx *c, const struct air_inst *in, unsigned operand, uint64_t *out)
+{
+ if (!air_const_uint(in->operands[operand], out))
+ return air_error(c, "intrinsic: argument %u is not a constant", operand - 1);
+ return true;
+}
+
+/* the { value, residency } struct these return */
+static void
+set_tex_result(struct air_ctx *c, const struct air_inst *in, nir_def *value)
+{
+ const struct air_type *ret = in->base.type;
+ if (ret->kind != AIR_TYPE_STRUCT) {
+ air_set(c, &in->base, air_val_ssa(c, value));
+ return;
+ }
+ struct air_val *agg = rzalloc(c->mem_ctx, struct air_val);
+ agg->kind = AIR_VAL_AGG;
+ agg->num_elems = ret->num_members;
+ agg->elems = rzalloc_array(c->mem_ctx, struct air_val *, MAX2(ret->num_members, 1));
+ agg->elems[0] = air_val_ssa(c, value);
+ for (unsigned i = 1; i < ret->num_members; i++) {
+ /* the sparse-residency byte: nothing in the corpus reads it */
+ unsigned bits = air_nir_bit_size(c, ret->members[i]);
+ agg->elems[i] = air_val_ssa(c, nir_imm_intN_t(&c->b, 0, bits));
+ }
+ air_set(c, &in->base, agg);
+}
+
+
+/* A query: a size, a level count or a sample count, of a texture or an image. */
+static nir_def *
+query_texture(struct air_ctx *c, const struct air_ptr *tex, const struct air_tex_name *t,
+ nir_texop op, nir_def *lod, unsigned comps)
+{
+ if (tex->is_image) {
+ nir_def *index = nir_imm_int(&c->b, tex->binding);
+ switch (op) {
+ case nir_texop_txs: {
+ /* an image size has as many components as the image has dimensions;
+ * the caller passes 0 for "as many as there are"
+ */
+ unsigned n = comps ? comps :
+ (t->dim == GLSL_SAMPLER_DIM_1D || t->dim == GLSL_SAMPLER_DIM_BUF ? 1 :
+ t->dim == GLSL_SAMPLER_DIM_3D ? 3 : 2) + (t->is_array ? 1 : 0);
+ return nir_image_size(&c->b, n, 32, index, lod ? lod : nir_imm_int(&c->b, 0),
+ .image_dim = t->dim, .image_array = t->is_array);
+ }
+ case nir_texop_query_levels:
+ return nir_image_levels(&c->b, 32, index, .image_dim = t->dim,
+ .image_array = t->is_array);
+ default:
+ return nir_image_samples(&c->b, 32, index, .image_dim = t->dim,
+ .image_array = t->is_array);
+ }
+ }
+
+ nir_tex_instr *q = nir_tex_instr_create(c->nir, lod ? 1 : 0);
+ q->op = op;
+ q->sampler_dim = t->dim;
+ q->is_array = t->is_array;
+ q->dest_type = nir_type_int32;
+ q->texture_index = tex->binding;
+ q->sampler_index = 0;
+ if (lod)
+ q->src[0] = (nir_tex_src){ .src_type = nir_tex_src_lod, .src = nir_src_for_ssa(lod) };
+ /* NIR fixes the size of a query's result itself -- txs is two components
+ * for a 2D texture whatever the caller asks one of them for -- so it is
+ * asked here and the component picked afterwards.
+ */
+ if (comps == 0)
+ comps = nir_tex_instr_dest_size(q);
+ nir_def_init(&q->instr, &q->def, comps, 32);
+ nir_builder_instr_insert(&c->b, &q->instr);
+ return &q->def;
+}
+
+/* is every component of this value a constant zero? */
+static bool
+all_zero(nir_def *def)
+{
+ if (!nir_src_is_const(nir_src_for_ssa(def)))
+ return false;
+ for (unsigned i = 0; i < def->num_components; i++)
+ if (nir_src_comp_as_int(nir_src_for_ssa(def), i) != 0)
+ return false;
+ return true;
+}
+
+static bool
+texture_op(struct air_ctx *c, const struct air_inst *in, const struct air_name *n)
+{
+ struct air_tex_name t;
+ if (!parse_tex_name(n->part[0], &t))
+ return air_error(c, "intrinsic: air.%s (a texture name this cannot take apart)",
+ n->part[0]);
+ if (t.is_buffer)
+ return air_error(c, "intrinsic: air.%s (texture buffers, not yet)", n->part[0]);
+ if (t.compare)
+ return air_error(c, "intrinsic: air.%s (compare sampling, not yet)", n->part[0]);
+
+ struct air_ptr tex;
+ if (!tex_handle(c, in, 1, AIR_PTR_TEXTURE, &tex))
+ return false;
+
+ unsigned arg = 2; /* operands[0] is the callee, [1] the texture */
+ struct air_ptr samp = { 0 };
+ bool have_sampler = !strcmp(t.op, "sample") || !strcmp(t.op, "gather") ||
+ !strcmp(t.op, "lod");
+ if (have_sampler) {
+ if (!tex_handle(c, in, arg, AIR_PTR_SAMPLER, &samp))
+ return false;
+ arg++;
+ }
+
+ /* A depth texture takes a plane or aspect selector before the coordinate,
+ * and every call in the corpus passes 1. What else it can be is not known
+ * from the data, so anything else is refused rather than assumed.
+ */
+ if (t.depth) {
+ uint64_t plane;
+ if (!tex_flag(c, in, arg, &plane))
+ return false;
+ if (plane != 1)
+ return air_error(c, "intrinsic: air.%s with aspect %u (only 1 is understood)",
+ n->part[0], (unsigned)plane);
+ arg++;
+ }
+
+ unsigned ncoord = coord_components_of(&t);
+ nir_alu_type dest_type = tex_dest_type(c, in->base.type, n);
+ unsigned dest_bits = nir_alu_type_get_type_size(dest_type);
+
+ if (!strcmp(t.op, "get")) {
+ /* the queries: a size, a level count or a sample count */
+ nir_def *res;
+ if (!strcmp(t.query, "num_samples")) {
+ res = query_texture(c, &tex, &t, nir_texop_texture_samples, NULL, 1);
+ } else if (!strcmp(t.query, "num_mip_levels")) {
+ res = query_texture(c, &tex, &t, nir_texop_query_levels, NULL, 1);
+ } else {
+ nir_def *lod = nir_imm_int(&c->b, 0);
+ if (in->num_operands > arg) {
+ nir_def *given = air_get_ssa(c, in->operands[arg]);
+ if (!given)
+ return false;
+ lod = nir_u2u32(&c->b, given);
+ }
+ nir_def *size = query_texture(c, &tex, &t, nir_texop_txs, lod, 0);
+ unsigned comp = !strcmp(t.query, "width") ? 0 :
+ !strcmp(t.query, "height") ? 1 :
+ !strcmp(t.query, "depth") ? 2 : size->num_components - 1;
+ if (!strcmp(t.query, "array_size") && !t.is_array)
+ return air_error(c, "intrinsic: air.%s on a texture that is not an array",
+ n->part[0]);
+ if (comp >= size->num_components)
+ return air_error(c, "intrinsic: air.%s asks for component %u of %u",
+ n->part[0], comp, size->num_components);
+ res = nir_channel(&c->b, size, comp);
+ }
+ air_set(c, &in->base, air_val_ssa(c, nir_u2uN(&c->b, res, dest_bits)));
+ return true;
+ }
+
+ if (!strcmp(t.op, "write")) {
+ nir_def *coord = air_get_ssa(c, in->operands[arg++]);
+ nir_def *value = air_get_ssa(c, in->operands[arg++]);
+ if (!coord || !value)
+ return false;
+ uint64_t lod;
+ if (!tex_flag(c, in, arg, &lod))
+ return false;
+ if (lod != 0)
+ return air_error(c, "intrinsic: air.%s to mip level %u (only 0 is handled)",
+ n->part[0], (unsigned)lod);
+ if (!tex.is_image)
+ return air_error(c, "intrinsic: air.%s to a texture the interface says is not "
+ "written", n->part[0]);
+ nir_image_store(&c->b, nir_imm_int(&c->b, tex.binding),
+ nir_pad_vector(&c->b, nir_u2u32(&c->b, coord), 4),
+ nir_imm_int(&c->b, 0), nir_pad_vector(&c->b, value, 4),
+ nir_imm_int(&c->b, 0),
+ .image_dim = t.dim, .image_array = t.is_array,
+ .src_type = dest_type);
+ return true;
+ }
+
+ /* sample, gather, read: a texture instruction, or an image load */
+ nir_def *coord = air_get_ssa(c, in->operands[arg++]);
+ if (!coord)
+ return false;
+ if (coord->num_components != ncoord)
+ return air_error(c, "intrinsic: air.%s with a %u-component coordinate, wanting %u",
+ n->part[0], coord->num_components, ncoord);
+
+ if (!strcmp(t.op, "read") && tex.is_image) {
+ uint64_t extra;
+ if (!tex_flag(c, in, arg, &extra))
+ return false;
+ nir_def *res = nir_image_load(&c->b, 4, dest_bits, nir_imm_int(&c->b, tex.binding),
+ nir_pad_vector(&c->b, nir_u2u32(&c->b, coord), 4),
+ nir_imm_int(&c->b, t.is_ms ? extra : 0),
+ nir_imm_int(&c->b, t.is_ms ? 0 : extra),
+ .image_dim = t.dim, .image_array = t.is_array,
+ .dest_type = dest_type);
+ set_tex_result(c, in, res);
+ return true;
+ }
+
+ nir_tex_src srcs[6];
+ unsigned nsrc = 0;
+ srcs[nsrc++] = (nir_tex_src){ .src_type = nir_tex_src_coord,
+ .src = nir_src_for_ssa(
+ air_type_is_float(air_type_scalar(
+ in->operands[arg - 1]->type))
+ ? coord : nir_u2u32(&c->b, coord)) };
+
+ nir_texop op;
+ if (!strcmp(t.op, "sample")) {
+ uint64_t has_offset, is_level;
+ if (!tex_flag(c, in, arg, &has_offset))
+ return false;
+ arg++;
+ nir_def *offset = air_get_ssa(c, in->operands[arg++]);
+ if (!offset)
+ return false;
+ if (has_offset && !all_zero(offset))
+ srcs[nsrc++] = (nir_tex_src){ .src_type = nir_tex_src_offset,
+ .src = nir_src_for_ssa(nir_u2u32(&c->b, offset)) };
+ if (!tex_flag(c, in, arg, &is_level))
+ return false;
+ arg++;
+ nir_def *level = air_get_ssa(c, in->operands[arg++]);
+ nir_def *min_lod = air_get_ssa(c, in->operands[arg++]);
+ if (!level || !min_lod)
+ return false;
+ bool level_is_zero = nir_src_is_const(nir_src_for_ssa(level)) &&
+ nir_src_as_float(nir_src_for_ssa(level)) == 0.0;
+ if (is_level) {
+ op = nir_texop_txl;
+ srcs[nsrc++] = (nir_tex_src){ .src_type = nir_tex_src_lod,
+ .src = nir_src_for_ssa(level) };
+ } else if (!level_is_zero) {
+ op = nir_texop_txb;
+ srcs[nsrc++] = (nir_tex_src){ .src_type = nir_tex_src_bias,
+ .src = nir_src_for_ssa(level) };
+ } else {
+ op = nir_texop_tex;
+ }
+ if (!(nir_src_is_const(nir_src_for_ssa(min_lod)) &&
+ nir_src_as_float(nir_src_for_ssa(min_lod)) == 0.0))
+ srcs[nsrc++] = (nir_tex_src){ .src_type = nir_tex_src_min_lod,
+ .src = nir_src_for_ssa(min_lod) };
+ } else if (!strcmp(t.op, "gather")) {
+ uint64_t has_offset;
+ if (!tex_flag(c, in, arg, &has_offset))
+ return false;
+ arg++;
+ nir_def *offset = air_get_ssa(c, in->operands[arg++]);
+ if (!offset)
+ return false;
+ if (has_offset && !all_zero(offset))
+ srcs[nsrc++] = (nir_tex_src){ .src_type = nir_tex_src_offset,
+ .src = nir_src_for_ssa(nir_u2u32(&c->b, offset)) };
+ op = nir_texop_tg4;
+ } else {
+ /* read: a fetch by integer coordinate, with a level or a sample. Neither
+ * has to be a constant -- read_texture_2d_ms is called with a computed
+ * sample index throughout AppleMetalGLRenderer.
+ */
+ nir_def *which = air_get_ssa(c, in->operands[arg++]);
+ if (!which)
+ return false;
+ which = nir_u2u32(&c->b, which);
+ op = t.is_ms ? nir_texop_txf_ms : nir_texop_txf;
+ srcs[nsrc++] = (nir_tex_src){
+ .src_type = t.is_ms ? nir_tex_src_ms_index : nir_tex_src_lod,
+ .src = nir_src_for_ssa(which),
+ };
+ }
+
+ nir_tex_instr *tex_instr = nir_tex_instr_create(c->nir, nsrc);
+ tex_instr->op = op;
+ tex_instr->sampler_dim = t.dim;
+ tex_instr->is_array = t.is_array;
+ tex_instr->is_shadow = false;
+ tex_instr->dest_type = dest_type;
+ tex_instr->coord_components = ncoord;
+ tex_instr->texture_index = tex.binding;
+ tex_instr->sampler_index = have_sampler ? samp.binding : 0;
+ if (op == nir_texop_tg4) {
+ uint64_t component;
+ if (!tex_flag(c, in, arg, &component))
+ return false;
+ tex_instr->component = component;
+ }
+ for (unsigned i = 0; i < nsrc; i++)
+ tex_instr->src[i] = srcs[i];
+
+ const struct air_type *value_type = in->base.type;
+ if (value_type->kind == AIR_TYPE_STRUCT && value_type->num_members)
+ value_type = value_type->members[0];
+
+ /* The width of the result is NIR's to decide: a sample is four components
+ * whatever the shader does with them. Metal's depth2d.sample() returns one
+ * float, which is channel 0 of those four -- the same thing GLSL gets from
+ * a depth texture read without a comparison.
+ */
+ unsigned nir_comps = nir_tex_instr_dest_size(tex_instr);
+ nir_def_init(&tex_instr->instr, &tex_instr->def, nir_comps, dest_bits);
+ nir_builder_instr_insert(&c->b, &tex_instr->instr);
+
+ nir_def *res = &tex_instr->def;
+ unsigned want = air_nir_components(value_type);
+ if (want < nir_comps)
+ res = nir_trim_vector(&c->b, res, want);
+ else if (want > nir_comps)
+ return air_error(c, "intrinsic: air.%s gives %u components, the shader wants %u",
+ n->part[0], nir_comps, want);
+ set_tex_result(c, in, res);
+ return true;
+}
+
/* -- the dispatcher ----------------------------------------------------- */
bool
@@ -452,13 +955,11 @@ air_translate_call(struct air_ctx *c, const struct air_inst *in, const char *nam
if (handled)
return true;
- /* textures: the largest family, and M3's whole subject. Named here so the
- * corpus report counts them as one thing rather than as unknown calls.
- */
const char *op = n.part[0];
if (strstr(op, "texture") || strstr(op, "_depth_") || !strncmp(op, "sample", 6) ||
- !strncmp(op, "gather", 6) || !strncmp(op, "read_", 5) || !strncmp(op, "write_", 6))
- return air_error(c, "intrinsic: %s (textures, not yet)", name);
+ !strncmp(op, "gather", 6) || !strncmp(op, "read_", 5) || !strncmp(op, "write_", 6) ||
+ strstr(op, "_lod_"))
+ return texture_op(c, in, &n);
return air_error(c, "intrinsic: %s", name);
}
diff --git a/tools/mesa/air/air_private.h b/tools/mesa/air/air_private.h
index a1bacbb..12be065 100644
--- a/tools/mesa/air/air_private.h
+++ b/tools/mesa/air/air_private.h
@@ -34,6 +34,7 @@ enum air_ptr_class {
struct air_ptr {
enum air_ptr_class class;
unsigned binding;
+ bool is_image; /* a texture the shader writes: an image, not a view */
nir_def *offset; /* bytes, 32-bit; NULL means zero */
const struct air_type *pointee; /* for sizes, and for loads without a type */
};
diff --git a/tools/mesa/air/air_run.c b/tools/mesa/air/air_run.c
index c962324..bbb7596 100644
--- a/tools/mesa/air/air_run.c
+++ b/tools/mesa/air/air_run.c
@@ -41,9 +41,71 @@
#include "util/u_inlines.h"
#include "util/u_memory.h"
+#ifdef __APPLE__
+#include <mach-o/dyld.h>
+#include <signal.h>
+#include <ucontext.h>
+#endif
+
#define WIDTH 4
#define HEIGHT 4
+#ifdef __APPLE__
+/*
+ * A program that dies on this port leaves nothing behind: the kernel says
+ * "Failed to send exception EXC_CORPSE_NOTIFY" and that is the end of it. So
+ * print the fault before dying. These two functions are userland/src/inject.c's
+ * say_the_fault()/watch_for_faults() (branch sileo2, 6683521), with the slide
+ * taken from this program's own image rather than from the shared cache, since
+ * the pc of interest is in air_run or in libOSMesa and not in iOS.
+ */
+static void
+say_the_fault(int signal_number, siginfo_t *info, void *context_in)
+{
+ ucontext_t *context = context_in;
+ uintptr_t slide = (uintptr_t)_dyld_get_image_vmaddr_slide(0);
+ uintptr_t pc = 0, lr = 0, fp = 0;
+
+ if (context != NULL && context->uc_mcontext != NULL) {
+ pc = (uintptr_t)context->uc_mcontext->__ss.__pc;
+ lr = (uintptr_t)context->uc_mcontext->__ss.__lr;
+ fp = (uintptr_t)context->uc_mcontext->__ss.__fp;
+ }
+ /* who called, by the frame pointers: on arm64 each frame holds the previous
+ * at [fp] and its return address at [fp + 8]
+ */
+ char frames[8 * 20 + 1] = "";
+ size_t used = 0;
+ for (unsigned i = 0; i < 8 && fp != 0 && (fp & 7) == 0; i++) {
+ uintptr_t next = ((uintptr_t *)fp)[0], ret = ((uintptr_t *)fp)[1];
+ if (ret == 0)
+ break;
+ used += (size_t)snprintf(frames + used, sizeof(frames) - used, " %#lx", ret - slide);
+ fp = next > fp ? next : 0; /* the chain only ever goes up the stack */
+ }
+ fprintf(stderr, "air_run: signal %d at %p, pc %#lx, lr %#lx, called from%s (unslid)\n",
+ signal_number, info != NULL ? info->si_addr : NULL, pc - slide, lr - slide,
+ frames);
+ fflush(stderr);
+ signal(signal_number, SIG_DFL);
+ raise(signal_number);
+}
+
+static void
+watch_for_faults(void)
+{
+ struct sigaction action;
+
+ memset(&action, 0, sizeof(action));
+ action.sa_sigaction = say_the_fault;
+ action.sa_flags = SA_SIGINFO | SA_RESETHAND;
+ sigaction(SIGSEGV, &action, NULL);
+ sigaction(SIGBUS, &action, NULL);
+}
+#else
+static void watch_for_faults(void) {}
+#endif
+
/* the pattern cpuref.py and air2nir --eval fill their buffers with, so that
* all three agree on what the shader was given
*/
@@ -104,6 +166,7 @@ main(int argc, char **argv)
return 2;
}
+ watch_for_faults();
glsl_type_singleton_init_or_ref();
void *ctx = ralloc_context(NULL);
diff --git a/tools/mesa/air/air_to_nir.c b/tools/mesa/air/air_to_nir.c
index 9af2156..c46bd1f 100644
--- a/tools/mesa/air/air_to_nir.c
+++ b/tools/mesa/air/air_to_nir.c
@@ -672,9 +672,13 @@ translate_input(struct air_ctx *c, const struct air_md *node, unsigned *next_var
bool sample = md_key(node, "air.sample") >= 0;
bool read = md_key(node, "air.read") >= 0;
bool write = md_key(node, "air.write") >= 0 || md_key(node, "air.read_write") >= 0;
+ /* A texture the shader writes is an image to Gallium, with its own
+ * binding space; one it only samples or reads is a sampler view.
+ */
struct air_ptr p = {
- .class = (sample || (read && !write)) ? AIR_PTR_TEXTURE : AIR_PTR_TEXTURE,
+ .class = AIR_PTR_TEXTURE,
.binding = index,
+ .is_image = write,
};
add_resource(c, write ? AIR_RESOURCE_IMAGE : AIR_RESOURCE_TEXTURE, index, 0,
sample || read, write, param, node);
@@ -1368,9 +1372,16 @@ translate_body(struct air_ctx *c)
}
}
- /* where the phi stores of this block's successors go */
+ /* Where the phi stores of this block's successors go. A nop marks the
+ * spot: without it, a block whose only instruction is its terminator
+ * leaves the cursor at the end of the block, and a store put there
+ * lands after the jump -- which NIR asserts on. vtn marks its blocks
+ * the same way (vtn_cfg.c's end_nop).
+ */
+ nir_instr *end = &nir_nop(&c->b)->instr;
+ nir_cursor at_end = nir_after_instr(end);
_mesa_hash_table_insert(c->phi_cursors, blk,
- ralloc_memdup(c->mem_ctx, &c->b.cursor, sizeof(c->b.cursor)));
+ ralloc_memdup(c->mem_ctx, &at_end, sizeof(at_end)));
const struct air_inst *term = blk->insts[blk->num_insts - 1];
switch (term->op) {
@@ -1478,9 +1489,32 @@ translate_globals(struct air_ctx *c)
p.class = AIR_PTR_FUNCTION_CONSTANT;
p.binding = idx;
} else if (!strncmp(name, "__air_sampler_state", 19)) {
- /* an inline sampler; its state word is g->init (docs/air.md 7.7) */
+ /* A sampler the shader declares itself. It needs a real index, not a
+ * sentinel: the Adreno's texture instruction holds the sampler in a
+ * four-bit field, and ~0u there asserts in ir3's encoder. The index
+ * goes after the API's samplers, and the runtime creates the sampler
+ * from the state word (docs/air.md section 7.7).
+ */
+ unsigned next = 0;
+ for (unsigned k = 0; k < c->info->num_resources; k++)
+ if (c->info->resources[k].kind == AIR_RESOURCE_SAMPLER)
+ next = MAX2(next, c->info->resources[k].index + 1);
p.class = AIR_PTR_SAMPLER;
- p.binding = ~0u;
+ p.binding = next;
+
+ uint64_t word = 0;
+ if (g->init && !air_const_uint(g->init, &word)) {
+ air_error(c, "interface: an inline sampler whose state is not a constant");
+ return;
+ }
+ struct air_shader_info *info = c->info;
+ info->resources = reralloc(c->mem_ctx, info->resources, struct air_resource,
+ info->num_resources + 1);
+ info->resources[info->num_resources++] = (struct air_resource) {
+ .kind = AIR_RESOURCE_SAMPLER, .index = next, .read = true,
+ .param = ~0u, .name = name,
+ .inline_sampler = true, .sampler_state = word,
+ };
} else if (g->addrspace == 3) {
/* a static threadgroup array */
unsigned align = MAX2(g->align, air_type_abi_align(c->m, g->value_type));
diff --git a/tools/mesa/air/nir_air.h b/tools/mesa/air/nir_air.h
index c42a8b5..2a69ab5 100644
--- a/tools/mesa/air/nir_air.h
+++ b/tools/mesa/air/nir_air.h
@@ -60,9 +60,17 @@ struct air_resource {
unsigned index; /* Metal's [[buffer(n)]], [[texture(n)]], [[sampler(n)]] */
unsigned size; /* air.buffer_size when given, else 0 */
bool read, write;
- unsigned param; /* the entry point's parameter */
+ unsigned param; /* the entry point's parameter; ~0u for an inline sampler */
const char *name; /* air.arg_name */
const char *type_name;
+
+ /* A sampler the shader declares itself (Metal's constexpr sampler) rather
+ * than one the API binds: the runtime has to create it from this state
+ * word and bind it at .index, which the frontend assigns after the API's
+ * samplers. The word's layout is not decoded yet -- see docs/air.md.
+ */
+ bool inline_sampler;
+ uint64_t sampler_state;
};
struct air_varying {