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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
//! The per-renderer pool the engine draws its off-screen intermediates from.
//!
//! A layer, a filter input, a scratch copy — every target that is not the
//! frame's own surface is a short-lived texture that a later pass in the same
//! frame samples and nothing outlives the frame. Allocating one per frame is
//! the cost `frust_gpu::TexturePool` exists to remove, so the engine holds one
//! pool per renderer (one per surface, since a renderer is per surface) and
//! takes its intermediates from there.
//!
//! Two engine-level decisions sit on top of the substrate pool.
//!
//! - **A ceiling of [`MAX_INTERMEDIATE_DIMENSION`].** The adapter's own
//! `max_texture_dimension_2d` can be far larger than anything worth
//! allocating as a transient: a single 16384-square `Rgba8Unorm` intermediate
//! is a gigabyte. [`max_texture_size`] therefore takes the smaller of the
//! adapter's ceiling and 8192, which is the largest intermediate the engine
//! will ask a driver for.
//! - **An over-ceiling request is a value, not an error.** A layer larger than
//! that ceiling is answered with [`IntermediateTexture::TooLarge`] carrying
//! the extent that was refused, so a caller sees exactly what it asked for
//! and skips the layer rather than taking a device error mid-frame. A caller
//! that instead splits the layer into
//! [bands](crate::schedule::pages::page_bands) never reaches this arm: every
//! band is sized inside the same ceiling, and all of a layer's bands share
//! one extent, so a banded layer draws one texture out of this pool and
//! reuses it band after band.
//!
//! Like the substrate pool this type is generic over the texture and view
//! types, so its keying, reuse and aging are exercised against a counting fake
//! with no GPU in the loop. The engine always uses the default
//! `IntermediateTargets<wgpu::Texture, wgpu::TextureView>`.
//!
//! ## The one off-screen target this module does not hand out
//!
//! [`atlas_layer_config`] describes a pass whose colour attachment is one
//! layer of the glyph/image atlas array — a target [`crate::gpu::atlas`] owns
//! outright and deliberately keeps out of the pool (its module doc gives the
//! reason: residency across frames is the atlas's whole purpose). The
//! *allocation* therefore belongs there; the *viewport* decision belongs here,
//! beside [`IntermediateTargets::descriptor`], because it is the same decision
//! this module already makes for every other target that is not the frame's own
//! surface — what extent the vertex stage maps its NDC against, and which
//! resource-texture widths the fragment stage reconstructs by shift.
//!
//! ## The filter pass's two extras
//!
//! A [filter](crate::filters) round renders between two pooled pages this
//! module hands out, and needs two things beyond them: the *filter-data*
//! texture holding the frame's parameter blocks
//! ([`filter_data_texture_descriptor`], sized by
//! [`filter_data_texture_height`]) and the bilinear [`filter_sampler`] its
//! kernels read the source page through — the engine's first and only sampler.
//! Both are described here, beside the pages they are used with;
//! [`crate::renderer::FilterResources`] owns the live resources.
use ;
use crateGpuFilterData;
use GpuConfig;
use INTERMEDIATE_FORMAT;
/// The largest intermediate the engine allocates on any adapter, whatever its
/// own `max_texture_dimension_2d` reports.
///
/// 8192 square is 256 MiB at [`INTERMEDIATE_FORMAT`] — already far past any
/// real layer — and a transient that big is a memory decision rather than a
/// capability one, which is why it is pinned here instead of taken from the
/// adapter.
pub const MAX_INTERMEDIATE_DIMENSION: u32 = 8192;
/// How many of the most recent frames' parked entries a surface resize keeps
/// (see [`IntermediateTargets::drop_parked`]).
///
/// Two: the frame that just ran and the one before it. One is not enough — a
/// resize arrives *between* frames, so the page the last frame released is
/// already a frame behind by the time the resize is handled, and a window
/// being dragged would evict it on every step. Three or more starts holding
/// pages across frames that stopped asking for them, which is what the pool's
/// own aging is for.
pub const RESIZE_KEEP_ALIVE_FRAMES: u64 = 2;
/// The usage every intermediate is created with: drawn into by a strip pass,
/// then sampled by the pass that composites it.
pub const INTERMEDIATE_USAGE: TextureUsages = RENDER_ATTACHMENT
.union
.union;
/// The largest intermediate extent the engine will request on `caps`' adapter.
/// The viewport uniform a strip pass whose colour attachment is one atlas
/// array layer draws with.
///
/// `page` is the layer's own extent in texels — the atlas page size, not the
/// frame's — because the vertex stage maps a strip's pixel coordinates into NDC
/// against the *attachment* it writes, and an atlas layer is neither the
/// surface's extent nor a pooled page's. Getting this wrong does not fail
/// validation: it silently scales every glyph by the ratio of the two extents.
///
/// `alphas_tex_width` is the coverage texture the replayed strips sample;
/// `encoded_paints_tex_width` is the encoded-paint texture, which an atlas pass
/// only ever binds as a stand-in — a replayed glyph outline paints solid, and a
/// solid instance carries its colour in its own payload rather than indexing a
/// record. Both must be powers of two, for
/// [`tex_width_bits`](super::config::tex_width_bits)' reason.
///
/// No strip offset and no NDC negation: an atlas page holds its glyphs at the
/// slot coordinates the allocator handed out, in the same y-down space every
/// other engine target uses.
/// Whether an atlas page of `page` texels can be a render attachment on
/// `caps`' adapter.
///
/// The atlas is not pooled, so it never passes through
/// [`IntermediateTargets::acquire`]'s own ceiling check — but a page is still a
/// texture a driver has to accept, and the budgets
/// [`crate::cache::images::AtlasBudget`] hands out are chosen without the
/// adapter in view. This is the check a caller makes once, at the point it
/// decides a page size, rather than discovering the refusal as a device error
/// on the first frame that misses a glyph.
///
/// The adapter's own `max_texture_dimension_2d` is the bound, not
/// [`MAX_INTERMEDIATE_DIMENSION`]: that ceiling is a transient-memory decision
/// about targets allocated per frame, and an atlas page is allocated once and
/// lives for the renderer.
/// Texels per row of the filter-data texture.
///
/// Sixteen [`super::RESOURCE_TEXTURE_FORMAT`] texels is exactly 256 bytes,
/// which is `wgpu::COPY_BYTES_PER_ROW_ALIGNMENT` — the narrowest row an upload
/// may legally have, and so the cheapest whole-texture write a frame with one
/// filter can issue. A filter's parameter block is
/// [`GpuFilterData::SIZE_TEXELS`] wide and blocks are packed back to back, so a
/// block may straddle a row boundary; the fragment stage addresses one by a
/// flat texel index and reconstructs the coordinate by division, so it never
/// notices (see `load_filter_texel` in `shaders/filter.wgsl`).
pub const FILTER_DATA_TEXTURE_WIDTH: u32 = 16;
const _: = assert!;
/// The filter-data texture's descriptor at `height` rows.
///
/// A resource texture like the alpha and encoded-paint ones: `Rgba32Uint`,
/// sampled by the fragment stage with `textureLoad` and written by a queue
/// upload, never a render attachment. Unlike those two it is
/// [`FILTER_DATA_TEXTURE_WIDTH`] texels wide rather than the adapter's resource
/// dimension — a frame's filters are counted in ones, not in thousands, and a
/// row of the adapter's width would make the smallest possible upload 64 KiB.
/// The filter-data texture height that holds `blocks` parameter blocks, or
/// `None` for a block count past [`MAX_INTERMEDIATE_DIMENSION`] rows.
///
/// The refusal is a value rather than an error for the reason every ceiling in
/// this module is: the caller skips what does not fit rather than taking a
/// device error mid-frame. It is also unreachable in practice —
/// [`MAX_INTERMEDIATE_DIMENSION`] rows hold over forty thousand filters, and a
/// frame records filters in ones.
/// The engine's one sampler, which the blur kernels read their source page
/// through.
///
/// Bilinear, and that is the whole reason it exists: every other engine
/// pipeline reads its textures with `textureLoad` at integer coordinates, while
/// the blur kernels sample at *fractional* offsets so a decimation costs four
/// samples instead of sixteen and a convolution tap one instead of two (see
/// [`crate::filters::blur`]'s bilinear kernel).
///
/// Clamped rather than bordered on every axis: `wgpu`'s transparent-black
/// border address mode needs an adapter feature this tier never requests. The
/// address mode is not what makes a tap past the region transparent, and could
/// not be — a filter layer's region sits at its page's own *origin*, so on the
/// near side a clamp replicates the region's own edge texel instead of leaving
/// the texture at all. Every kernel therefore bounds its own taps against the
/// source region (`sample_region_bilinear` in `shaders/filters_blur.wgsl`,
/// `drop_shadow_load_checked` in `shaders/filters_drop_shadow.wgsl`), which is
/// what makes this sampler's behaviour outside that region unobservable rather
/// than merely harmless. No mip chain, because a pooled page has exactly one
/// level.
/// The result of asking for an intermediate: a pooled texture, or the extent
/// that was refused.
/// The engine's pool of off-screen intermediates, with its own frame clock.
///
/// The clock is what the substrate pool ages entries against; it advances once
/// per [`Self::end_frame`], including frames that acquired nothing — those are
/// the frames a parked entry ages on.