1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// SPDX-FileCopyrightText: Copyright 2026 Au-Zone Technologies
// SPDX-License-Identifier: Apache-2.0
//! Layout-only C ABI contract for the EdgeFirst tensor surface.
//!
//! Plain `#[repr(C)]` values and the integer vocabularies, declared exactly
//! once. Every `-capi` crate imports these instead of carrying an "identical
//! copy" — r2's duplication was safe but unverified-by-construction; a single
//! declaration makes divergence impossible instead of merely absent.
//!
//! This crate has **no dependencies** and must never gain one. The vocabulary
//! values are asserted against `edgefirst-tensor`'s authority from the crates
//! that depend on both (see `edgefirst-tensor-capi/src/codes.rs`) — this
//! crate is emitted surface, never a second authority.
/// One plane's location, mirroring `TensorPlane` on the wire.
///
/// The route by which one library consumes a tensor another minted: the Rust
/// types are not shared across `.so` boundaries, so the planes are the
/// interface. Emitted into `edgefirst/tensor.h` as `ef_tensor_plane` by
/// cbindgen.
/// Element type of a tensor's addressing grid.
/// Backing store for a tensor.
///
/// Every kind is declared on every platform. Whether one can be *materialised*
/// here is a runtime question — an IOSurface is a meaningful thing to name on
/// Linux even though nothing will allocate one — and platform-gating the
/// vocabulary would make the same integer mean different things per target.
/// CPU access direction for a map window.
///
/// Mirrors `edgefirst_tensor::CpuAccess`'s semantics; the values are the wire
/// codes. `None` names the no-CPU-access declaration and is never a valid map
/// direction.
/// Vendor tile-compression scheme actually in force for a tensor's
/// allocation.
///
/// A *result*, not a request: [`EfImageDescView::compression`] carries what
/// an allocation asked for ("any scheme" / "a specific one"), whereas this
/// names the scheme the allocator actually resolved to. `None` is a real
/// enumerator rather than a presence flag because "linear" is the answer for
/// almost every allocation on almost every platform -- there is no absent
/// case to distinguish from it, unlike `EfViewOrigin`/`EfQuantizationInfo`
/// where every field value is legitimate.
///
/// Mirrors `edgefirst_tensor::CompressionScheme`'s variants, plus the
/// `None` the Rust side spells as `Option::None`.
/// Which *kind* of failure the calling thread's last failing
/// `ef_tensor_*` call was.
///
/// The companion to `ef_tensor_last_error_message`, and the reason it
/// exists: an entry point that reports failure by returning `NULL` has no
/// errno to carry a class, so a Rust consumer rebuilding a typed
/// `edgefirst_tensor::Error` from it had nothing to go on but the advisory
/// message -- which the message's own contract says must never be parsed.
/// `ef_tensor_batch` really did match on a fragment of a `Display` string
/// for exactly this reason.
///
/// A **class**, not a one-to-one mirror of `edgefirst_tensor::Error`. Its
/// variants are the distinctions a caller acts on differently; several Rust
/// variants deliberately collapse into one here, and adding a Rust variant
/// does not oblige adding one here. `Unspecified` is the honest answer when
/// a failure path recorded only a message, and is what every such path
/// resets this to -- a stale class from an earlier failure would be read as
/// this call's, which is the confident-falsehood shape this whole mechanism
/// exists to remove.
/// Flattened, `#[repr(C)]` view of an image-request descriptor's fields.
///
/// `ef_tensor_image_desc` is opaque (handles are opaque both ways: a
/// receiving library never dereferences, sizes, or copies one). This is the
/// scalar block `ef_tensor_image_desc_get` fills instead -- the same shape as
/// `ef_tensor_plane`, which lets one library read a tensor it did not mint
/// without touching the other's private layout.
///
/// `memory` and `compression` are each a value plus an explicit presence
/// flag rather than a sentinel: every code in `ef_storage_kind` (0..=5) is a
/// real value, so there is no unused number to repurpose as "no request"
/// without colliding with `ef_storage_kind`'s `MEM == 0`. `compression` is 1
/// for "any scheme" and 2 for "a specific vendor scheme", the latter not
/// further decodable through this view -- no `ef_tensor_image_desc_set_*`
/// entry point can request one, so this view has never needed to carry more
/// detail than "present, and it's a specific one."
/// Parent-region snapshot for a tensor that is a `view`/`batch` sub-region.
///
/// The scalar block `ef_tensor_view_origin` fills, the same shape as
/// `ef_tensor_plane` and `ef_tensor_image_desc_view` -- one library reading a
/// tensor it did not mint. `has_origin` is a presence flag rather than a
/// sentinel value because every field is a legitimate 0 for a view pinned at
/// the parent's top-left corner; the other fields are meaningful only when
/// it is non-zero. Mirrors `edgefirst_tensor::ViewOrigin`.
/// The D3D11 texture behind a Windows texture tensor.
///
/// The scalar block `ef_tensor_d3d11_layout` fills, the same shape as
/// `ef_tensor_plane` and `ef_tensor_view_origin` -- one library reading a
/// tensor it did not mint. Mirrors `edgefirst_tensor::d3d11_layout::
/// D3d11ImageLayout`.
///
/// The dimensions are the texture's, in texels and rows, not the image's in
/// pixels: a semi-planar image is one texture whose row count covers both
/// planes, and a YUYV image is one texel per two pixels, so
/// `texture_width`/`texture_height` do not match `ef_tensor_shape` for
/// either. Read the image's own dimensions from the shape.
///
/// For a semi-planar format (`nv12`, `nv16`, `nv24`) `texture_width` is the
/// driver's row pitch -- at least `even(width)`, and on a discrete adapter
/// commonly more (128 bytes on NVIDIA, so a 64-wide NV12 image is a 128-wide
/// texture). It is the pitch the combined plane's rows are spaced by and the
/// width a sampler must address the texture at. Never derive it from the
/// image width: read this field, or `ef_tensor_row_stride`, which carries the
/// same number.
///
/// By-value and frozen forever: a consumer bakes this size and these offsets
/// into its call sites, so the struct evolves by a suffixed successor, never
/// by an in-place edit. `d3d11_layout_is_pinned` and the C golden in
/// `tensor-capi/tests/c/test_layout_goldens.c` hold both sides to it.
/// Presence/shape summary of a tensor's quantization metadata.
///
/// The first half of the two-call idiom `ef_tensor_quantization_info` /
/// `ef_tensor_quantization_get` use: this scalar block tells the caller
/// *whether* quantization is attached and *how big* a buffer the second call
/// needs, without allocating on either side of the boundary --
/// `Quantization` itself is variable-length (an axis plus per-axis `scales`
/// and `zero_points`), so unlike `ef_tensor_plane`/`ef_tensor_view_origin`
/// this view cannot carry the payload itself.
///
/// `has_quantization` is a presence flag, not a sentinel, for the same
/// reason `EfViewOrigin::has_origin` is: axis `0` and a scale of `0.0` are
/// both legitimate values, so there is no unused bit pattern to repurpose as
/// "absent" without colliding with a real one.
/// A mapped CPU window over a tensor's bytes.
///
/// By-value with no version field and no reserved tail, so its size
/// is baked into consumer call sites, so it evolves by a suffixed successor
/// (`ef_tensor_view2` + new entry points), never in place. The pointer is
/// valid from `ef_tensor_map` until the matching `ef_tensor_unmap`; writing
/// through it is allowed only when the map was taken with a writable access.
/// A retain or release call on a client-owned callback context.
///
/// Must be safe to call from any thread and must not unwind.
pub type EfClientRefFn = unsafe extern "C" fn;
/// Client-owned state a library entry point calls back into — **frozen by
/// value, forever**, and reused verbatim by every domain that needs one
/// (PBO today; CUDA-GL next).
///
/// # What `retain`/`release` govern — and what they do not
///
/// They extend the life of the **callback channel only**. They never
/// transfer ownership of the GL buffer, the CUDA resource, or anything else
/// the context happens to address. Getting this backwards double-frees a GL
/// buffer:
///
/// * The producing side's own destructor stays the sole caller of the real
/// `delete_buffer`.
/// * A library that reconstructed operations from this struct treats
/// `delete_buffer` as a no-op — it borrows the resource, it does not own
/// it.
///
/// # Ownership at an entry point
///
/// An entry point that stores this struct calls `retain(ctx)` itself and
/// calls `release(ctx)` exactly once when the object holding it is
/// destroyed. The caller keeps its own count and may drop it at any time
/// after the call returns.
///
/// # Freezing
///
/// This vocabulary declines the `struct_size` handshake (see the root
/// `ARCHITECTURE.md`, "Why there is no `struct_size` handshake"), so a
/// by-value struct grows only by a major bump or a suffixed successor.
/// Freezing **one** tiny universal struct — rather than one per domain, or
/// one that grows per domain — is what keeps that cost paid once. Its size
/// and offsets are pinned by `tests/c/test_layout_goldens.c` in
/// `edgefirst-tensor-capi` and by `client_state_layout_is_pinned` below.
///
/// `ctx` is opaque to the library: only the producing copy of the code ever
/// dereferences it.
/// Map a PBO for CPU access. `0` on success; `-1` when the GL context is
/// gone; any other non-zero value a generic failure.
pub type EfPboMapFn = unsafe extern "C" fn ;
/// Unmap a PBO previously mapped by an [`EfPboMapFn`]. Same return codes.
pub type EfPboUnmapFn =
unsafe extern "C" fn ;
// This doc is what a C reader sees for `ef_pbo_map_fn`, so it says what the
// callback does and nothing about Rust. The mechanics, for Rust readers:
//
// Two Rust aliases, one C type. In C there is only ever a function pointer
// that may or may not be NULL, so this one carries the `ef_pbo_map_fn` name
// in the header and the bare `EfPboMapFn` is not emitted at all. The bare
// alias is the Rust-side vocabulary for a pointer already checked -- what
// `PboOpsVtable` stores and what `client_state_pbo_ops` takes -- and it must
// stay bare, or every holder would carry an `Option` it has already proved
// is `Some`.
//
// This one is spelled as `Option<` over a *bare* `fn`, not over
// `EfPboMapFn`: cbindgen resolves `Option<T>` to a nullable C function
// pointer only when `T` is syntactically a bare `fn`. Given the alias it
// emits an opaque `struct Option_EfPboMapFn` and passes it by value -- an
// incomplete type no C caller can name. Given this, it emits
// `typedef int (*ef_pbo_map_fn)(...)` and uses that name at the call site,
// which is why `ef_tensor_wrap_pbo`'s header signature does not move.
/// Map a PBO for CPU access. `0` on success; `-1` when the GL context is
/// gone; any other non-zero value a generic failure.
///
/// May be NULL where an entry point takes one; such an entry point refuses
/// a NULL rather than calling through it.
pub type EfPboMapFnNullable = ;
// See `EfPboMapFnNullable` for why there are two aliases and why this one
// spells the `fn` out rather than naming `EfPboUnmapFn`.
/// Unmap a PBO previously mapped by an `ef_pbo_map_fn`. Same return codes.
///
/// May be NULL where an entry point takes one; such an entry point refuses
/// a NULL rather than calling through it.
pub type EfPboUnmapFnNullable =
;
/// Map a CUDA graphics resource to a device pointer. `0` on success.
pub type EfCudaMapFn = unsafe extern "C" fn ;
/// Unmap a resource previously mapped by an [`EfCudaMapFn`].
pub type EfCudaUnmapFn =
unsafe extern "C" fn;
/// Unregister a CUDA graphics resource.
pub type EfCudaUnregisterFn =
unsafe extern "C" fn;
/// Map a CUDA graphics resource to a device pointer. `0` on success.
///
/// May be NULL where an entry point takes one; such an entry point refuses
/// a NULL rather than calling through it.
pub type EfCudaMapFnNullable = ;
/// Unmap a resource previously mapped by an `ef_cuda_map_fn`.
///
/// May be NULL where an entry point takes one; such an entry point refuses
/// a NULL rather than calling through it.
pub type EfCudaUnmapFnNullable =
;
/// Unregister a CUDA graphics resource.
///
/// May be NULL where an entry point takes one; such an entry point refuses
/// a NULL rather than calling through it.
pub type EfCudaUnregisterFnNullable =
;