Debian Codegraph @

src/panfrost/lib/pan_fb.h

raw
1 /*
2 * Copyright (C) 2026 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef __PAN_FB_H
7 #define __PAN_FB_H
8
9 #include "genxml/gen_macros.h"
10 #include "util/format/u_formats.h"
11 #include "compiler/shader_enums.h"
12
13 struct nir_shader;
14 struct nir_shader_compiler_options;
15 struct pan_fb_info;
16 struct pan_image_view;
17
18 #define PAN_MAX_RTS 8
19
20 /** Bounding box used by the framebuffer abstraction */
21 struct pan_fb_bbox {
22 /** Minimum x/y value */
23 uint16_t min_x, min_y;
24
25 /** Maximum x/y value
26 *
27 * Like the hardware, the maximums here are inclusive. A pixel is
28 * in-bounds if min_x <= x <= max_x and min_y <= y <= max_y.
29 */
30 uint16_t max_x, max_y;
31 };
32
33 static inline bool
34 pan_fb_bbox_is_valid(struct pan_fb_bbox b)
35 {
36 return b.min_x <= b.max_x && b.min_y <= b.max_y;
37 }
38
39 static inline bool
40 pan_fb_bbox_contains_bbox(struct pan_fb_bbox a, struct pan_fb_bbox b)
41 {
42 return a.min_x <= b.min_x && b.max_x <= a.max_x &&
43 a.min_y <= b.min_y && b.max_y <= a.max_y;
44 }
45
46 static inline bool
47 pan_fb_bbox_equal(struct pan_fb_bbox a, struct pan_fb_bbox b)
48 {
49 return a.min_x == b.min_x && b.max_x == a.max_x &&
50 a.min_y == b.min_y && b.max_y == a.max_y;
51 }
52
53 static inline struct pan_fb_bbox
54 pan_fb_bbox_clamp(struct pan_fb_bbox a, struct pan_fb_bbox b)
55 {
56 assert(pan_fb_bbox_is_valid(a) && pan_fb_bbox_is_valid(a));
57 return (struct pan_fb_bbox) {
58 .min_x = CLAMP(a.min_x, b.min_x, b.max_x),
59 .min_y = CLAMP(a.min_y, b.min_y, b.max_y),
60 .max_x = CLAMP(a.max_x, b.min_x, b.max_x),
61 .max_y = CLAMP(a.max_y, b.min_y, b.max_y),
62 };
63 }
64
65 static inline struct pan_fb_bbox
66 pan_fb_bbox_align(struct pan_fb_bbox b, uint32_t align_x, uint32_t align_y)
67 {
68 assert(util_is_power_of_two_nonzero(align_x));
69 assert(util_is_power_of_two_nonzero(align_y));
70 assert(align_x <= UINT16_MAX && align_y <= UINT16_MAX);
71
72 b.min_x = ROUND_DOWN_TO(b.min_x, align_x);
73 b.min_y = ROUND_DOWN_TO(b.min_y, align_y);
74 b.max_x = align((uint32_t)b.max_x + 1, align_x) - 1;
75 b.max_y = align((uint32_t)b.max_y + 1, align_y) - 1;
76
77 return b;
78 }
79
80 static inline struct pan_fb_bbox
81 pan_fb_bbox_from_xywh(uint32_t x, uint32_t y, uint32_t w, uint32_t h)
82 {
83 assert(x + w > x && x + w - 1 <= UINT16_MAX);
84 assert(y + h > y && y + h - 1 <= UINT16_MAX);
85 return (struct pan_fb_bbox) {
86 .min_x = x,
87 .min_y = y,
88 .max_x = x + w - 1,
89 .max_y = y + h - 1,
90 };
91 }
92
93 /** Framebuffer layout
94 *
95 * This describes the layout of framebuffer in tile memory as well as various
96 * properties that are properties of the framebuffer itself, nit any of the
97 * image views attached to it.
98 */
99 struct pan_fb_layout {
100 /** Dimensions of the framebuffer itself, in pixels */
101 uint32_t width_px, height_px;
102
103 /** The render area
104 *
105 * This is the API render area (as defined by Vulkan) and may be smaller
106 * than the actual bounding box of the framebuffer. The in_bounds_load
107 * and border_load members of pan_fb_load_target are relative to the
108 * render area.
109 */
110 struct pan_fb_bbox render_area_px;
111
112 /** The tiling area
113 *
114 * This is based on the render area but may be larger to accomodate tiling
115 * restrictions.
116 */
117 struct pan_fb_bbox tiling_area_px;
118
119 /** Sample count for this framebuffer */
120 uint8_t sample_count;
121
122 /** Color render target formats
123 *
124 * If a render target is unused, set it to PIPE_FORMAT_NONE.
125 */
126 uint8_t rt_count;
127 enum pipe_format rt_formats[PAN_MAX_RTS];
128
129 /** Depth format for this framebuffer, or PIPE_FORMAT_NONE */
130 enum pipe_format z_format;
131
132 /** Stencil format for this framebuffer, or PIPE_FORMAT_NONE */
133 enum pipe_format s_format;
134
135 /** Amount of space reserved for PLS, in bytes.
136 *
137 * This may overlap the color render targets in tile memory.
138 */
139 uint16_t pls_size_B;
140
141 uint32_t tile_rt_budget_B;
142 uint32_t tile_z_budget_B;
143
144 /** Optimal tile buffer size, in pixels
145 *
146 * This must be a power of two.
147 */
148 unsigned tile_size_px;
149
150 /** Amount of tile memory allocated for color render targets */
151 unsigned tile_rt_alloc_B;
152 };
153
154 static inline bool
155 pan_fb_has_zs(const struct pan_fb_layout *fb)
156 {
157 /* An intentional choice is made here to base has_zs on the formats in
158 * the pan_fb_layout and not the stores being done in the end. PanVK in
159 * particular relies on the size of the final framebuffer descriptor being
160 * the same for all the different variants with incremental rendering.
161 */
162 return fb->z_format != PIPE_FORMAT_NONE ||
163 fb->s_format != PIPE_FORMAT_NONE;
164 }
165
166 static inline bool
167 pan_fb_is_fully_covered(const struct pan_fb_layout *fb)
168 {
169 const struct pan_fb_bbox fb_area_px =
170 pan_fb_bbox_from_xywh(0, 0, fb->width_px, fb->height_px);
171 return pan_fb_bbox_equal(fb->render_area_px, fb_area_px);
172 }
173
174 static inline bool
175 pan_fb_has_partial_tiles(const struct pan_fb_layout *fb)
176 {
177 assert(pan_fb_bbox_contains_bbox(fb->tiling_area_px, fb->render_area_px));
178 return !pan_fb_bbox_equal(fb->tiling_area_px, fb->render_area_px);
179 }
180
181 #ifdef PAN_ARCH
182 void GENX(pan_select_fb_tile_size)(struct pan_fb_layout *fb);
183 #endif
184
185 enum ENUM_PACKED pan_fb_load_op {
186 PAN_FB_LOAD_NONE = 0,
187 PAN_FB_LOAD_CLEAR,
188 PAN_FB_LOAD_IMAGE,
189 PAN_FB_LOAD_OP_COUNT,
190 };
191
192 /** Describes how MSAA is handled when copying
193 *
194 * The source or destination may or may not be multisampled independently and
195 * this controls how that is handled in the copy.
196 */
197 enum ENUM_PACKED pan_fb_msaa_copy_op {
198 /** Copies all samples of the source to the destination
199 *
200 * This requires that the source and destination sample counts be the same.
201 * If the source and destination are both single-sampled, this is equivalent
202 * as PAN_FB_MSAA_COPY_SINGLE or PAN_FB_MSAA_COPY_SAMPLE_0.
203 */
204 PAN_FB_MSAA_COPY_ALL = 0,
205
206 /** Copies the single sample from the source to the destination
207 *
208 * This requires that the source be single-sampled. Semantically, this is
209 * the same as PAN_FB_MSAA_COPY_SAMPLE_0, but it's useful to make this
210 * distinction in shader keys so single and multi-sampled sources can be
211 * differentiated.
212 */
213 PAN_FB_MSAA_COPY_SINGLE,
214
215 /** Copies assuming all source samples are identical
216 *
217 * The actual copy can use any resolve mode because we know a priori that
218 * all samples are identical. All resolve modes yield the same result.
219 */
220 PAN_FB_MSAA_COPY_IDENTICAL,
221
222 /** Copies sample 0 from the source to all samples in the destination */
223 PAN_FB_MSAA_COPY_SAMPLE_0,
224
225 /** Copies the average of the samples in the source to the destination */
226 PAN_FB_MSAA_COPY_AVERAGE,
227
228 /** Copies the minimum of the samples in the source to the destination */
229 PAN_FB_MSAA_COPY_MIN,
230
231 /** Copies the maximum of the samples in the source to the destination */
232 PAN_FB_MSAA_COPY_MAX,
233
234 PAN_FB_MSAA_COPY_OP_COUNT,
235 };
236
237 struct pan_fb_load_target {
238 /** Load op for pixels inside the bounding box */
239 enum pan_fb_load_op in_bounds_load;
240
241 /** Load op for border (out of bounds) pixels
242 *
243 * This should almost always be LOAD_OP_LOAD, unless you have a very good
244 * reason to make a different choice.
245 */
246 enum pan_fb_load_op border_load;
247
248 /** Always load, even if no primitives intersect the tile
249 *
250 * This should usually be set for clears.
251 */
252 bool always;
253
254 /** MSAA mode used when load op is IMAGE */
255 enum pan_fb_msaa_copy_op msaa;
256
257 /* Image view to load from, or NULL if neither load op is IMAGE */
258 const struct pan_image_view *iview;
259
260 /** Clear value, if either load op is CLEAR */
261 union {
262 union pipe_color_union color;
263 float depth;
264 uint8_t stencil;
265 } clear;
266 };
267
268 static inline struct pan_fb_load_target
269 pan_fb_load_iview(const struct pan_image_view *iview)
270 {
271 return (struct pan_fb_load_target) {
272 .in_bounds_load = PAN_FB_LOAD_IMAGE,
273 .border_load = PAN_FB_LOAD_IMAGE,
274 .msaa = PAN_FB_MSAA_COPY_ALL,
275 .iview = iview,
276 };
277 }
278
279 struct pan_fb_load {
280 /** Color render target loads */
281 struct pan_fb_load_target rts[PAN_MAX_RTS];
282
283 /** Depth target load */
284 struct pan_fb_load_target z;
285
286 /** Stencil target load */
287 struct pan_fb_load_target s;
288 };
289
290 static inline bool
291 pan_fb_has_image_load(const struct pan_fb_load *load, bool include_border)
292 {
293 if (load->z.in_bounds_load == PAN_FB_LOAD_IMAGE ||
294 load->s.in_bounds_load == PAN_FB_LOAD_IMAGE)
295 return true;
296
297 if (include_border &&
298 (load->z.border_load == PAN_FB_LOAD_IMAGE ||
299 load->s.border_load == PAN_FB_LOAD_IMAGE))
300 return true;
301
302 for (unsigned rt = 0; rt < PAN_MAX_RTS; rt++) {
303 if (load->rts[rt].in_bounds_load == PAN_FB_LOAD_IMAGE)
304 return true;
305 if (include_border && load->rts[rt].border_load == PAN_FB_LOAD_IMAGE)
306 return true;
307 }
308
309 return false;
310 }
311
312 /** Describes a resolve operation
313 *
314 * This describes the source data for the resolve operation. The way in which
315 * MSAA is handled in the source data is defined by the pan_fb_msaa_copy_op.
316 */
317 enum ENUM_PACKED pan_fb_resolve_op {
318 /** Do nothing
319 *
320 * In this mode, the framebuffer is left unaffected by the resolve. If
321 * necessary (sometimes required for Z/S), the old value will be read and
322 * written back to ensure the final value is the same.
323 */
324 PAN_FB_RESOLVE_NONE = 0,
325
326 /** Load from the specified image view */
327 PAN_FB_RESOLVE_IMAGE,
328
329 /** Load from color render target 0 */
330 PAN_FB_RESOLVE_RT_0,
331 PAN_FB_RESOLVE_RT_1,
332 PAN_FB_RESOLVE_RT_2,
333 PAN_FB_RESOLVE_RT_3,
334 PAN_FB_RESOLVE_RT_4,
335 PAN_FB_RESOLVE_RT_5,
336 PAN_FB_RESOLVE_RT_6,
337 PAN_FB_RESOLVE_RT_7,
338
339 /** Load from the depth target
340 *
341 * From the perspective of resolve ops, Z/S are always separate
342 */
343 PAN_FB_RESOLVE_Z,
344
345 /** Load from the stencil target
346 *
347 * From the perspective of resolve ops, Z/S are always separate
348 */
349 PAN_FB_RESOLVE_S,
350 PAN_FB_RESOLVE_OP_COUNT,
351 };
352
353 static_assert(PAN_FB_RESOLVE_Z == PAN_FB_RESOLVE_RT_0 + PAN_MAX_RTS,
354 "There are PAN_MAX_RTS many RTs");
355
356 #define PAN_FB_RESOLVE_RT(rt) \
357 (assert(0 <= (rt) && (rt) < PAN_MAX_RTS), (PAN_FB_RESOLVE_RT_0 + (rt)))
358
359 /** Describes a resovle operation on a given target
360 *
361 * For each side of the render area (in-bounds or border), this defines both
362 * a source to copy from (the resolve op) and a MSAA copy op to apply as part
363 * of the copy. In the common case, a resolve op will read from itself but
364 * that is not strictly a requirement. Any resolve op can read from any
365 * render target.
366 */
367 struct pan_fb_resolve_target {
368 struct pan_fb_resolve_op_msaa {
369 enum pan_fb_resolve_op resolve;
370 enum pan_fb_msaa_copy_op msaa;
371 } in_bounds, border;
372
373 /** For PAN_FB_RESOLVE_IMAGE the image view to load from */
374 const struct pan_image_view *iview;
375 };
376
377 /** Describes a resolve operation
378 *
379 * A resolve operation is implemented as a post-frame shader (see
380 * pan_fb_resolve_shader_key_fill) and does a copy from render targets to
381 * render targets, applying MSAA copy ops along the way.
382 */
383 struct pan_fb_resolve {
384 struct pan_fb_resolve_target rts[PAN_MAX_RTS];
385 struct pan_fb_resolve_target z, s;
386 };
387
388 struct pan_fb_store_target {
389 /** Whether or not to do a store */
390 bool store;
391
392 /** Always store, even if no primitives intersect the tile
393 *
394 * clean_tile_write_enable will be set if either store.always is set or
395 * load.always is set for the corresponding load[s].
396 */
397 bool always;
398
399 /** MSAA mode to use when copying to the destination image
400 *
401 * This can only be set to AVERAGE for blendable color formats.
402 */
403 enum pan_fb_msaa_copy_op msaa;
404
405 /** Image view to store to */
406 const struct pan_image_view *iview;
407 };
408
409 static inline struct pan_fb_store_target
410 pan_fb_store_iview(const struct pan_image_view *iview)
411 {
412 return (struct pan_fb_store_target) {
413 .store = true,
414 .msaa = PAN_FB_MSAA_COPY_ALL,
415 .iview = iview,
416 };
417 }
418
419 static inline struct pan_fb_store_target
420 pan_fb_always_store_iview_s0(const struct pan_image_view *iview)
421 {
422 return (struct pan_fb_store_target) {
423 .store = true,
424 .always = true,
425 .msaa = PAN_FB_MSAA_COPY_SAMPLE_0,
426 .iview = iview,
427 };
428 }
429
430 struct pan_fb_store {
431 /* Color render target stores */
432 struct pan_fb_store_target rts[PAN_MAX_RTS];
433
434 /* Depth/stencil target store
435 *
436 * If the attached image view has a combined depth/stencil format, both
437 * depth and stencil will be written.
438 */
439 struct pan_fb_store_target zs;
440
441 /* Stencil target store */
442 struct pan_fb_store_target s;
443 };
444
445 #ifdef PAN_ARCH
446 void GENX(pan_align_fb_tiling_area)(struct pan_fb_layout *fb,
447 const struct pan_fb_store *store);
448
449 void GENX(pan_fb_fold_resolve_into_store)(const struct pan_fb_layout *fb,
450 struct pan_fb_resolve *resolve,
451 struct pan_fb_store *store);
452 #endif
453
454 struct pan_fb_frame_shaders {
455 uint64_t dcd_pointer;
456 uint8_t modes[3];
457 };
458
459 struct pan_fb_desc_info {
460 const struct pan_fb_layout *fb;
461 const struct pan_fb_load *load;
462 const struct pan_fb_store *store;
463
464 struct pan_fb_frame_shaders frame_shaders;
465
466 uint64_t sample_pos_array_pointer;
467
468 /* Only used on Valhal */
469 bool sprite_coord_origin_max_y;
470 bool provoking_vertex_first;
471 bool allow_hsr_prepass;
472
473 uint16_t layer;
474
475 const struct pan_tls_info *tls;
476 const struct pan_tiler_context *tiler_ctx;
477 };
478
479 #ifdef PAN_ARCH
480 void GENX(pan_fill_fb_info)(const struct pan_fb_desc_info *info,
481 struct pan_fb_info *fbinfo);
482
483 uint32_t GENX(pan_emit_fb_desc)(const struct pan_fb_desc_info *info,
484 void *out);
485 #endif
486
487 enum ENUM_PACKED pan_fb_shader_op {
488 /** Preserves the current fragment contents
489 *
490 * The MSAA mode is ignored and, if a load does need to occur to preserve
491 * the current fragment contents, PAN_FB_MSAA_COPY_ALL is implied.
492 *
493 * This is only allowed in resolve shaders so this can always be replaced
494 * with a discard.
495 */
496 PAN_FB_SHADER_PRESERVE = 0,
497
498 /** Do whatever is the fastest (lowest bandwidth)
499 *
500 * This could be a CLEAR or PRESERVE or just write zeros. It will never
501 * write actual random data but the exact behavior may depend on other bits
502 * in the shader key.
503 */
504 PAN_FB_SHADER_DONT_CARE,
505
506 /** Loads the clear color
507 *
508 * This is only allowed in preload shaders and the clear will also be done
509 * in hardware so it can always be replaced with a discard.
510 */
511 PAN_FB_SHADER_LOAD_CLEAR,
512
513 /** Loads from the image bound at the corresponding gl_frag_result */
514 PAN_FB_SHADER_LOAD_IMAGE,
515
516 PAN_FB_SHADER_COPY_RT_0,
517 PAN_FB_SHADER_COPY_RT_1,
518 PAN_FB_SHADER_COPY_RT_2,
519 PAN_FB_SHADER_COPY_RT_3,
520 PAN_FB_SHADER_COPY_RT_4,
521 PAN_FB_SHADER_COPY_RT_5,
522 PAN_FB_SHADER_COPY_RT_6,
523 PAN_FB_SHADER_COPY_RT_7,
524 PAN_FB_SHADER_COPY_Z,
525 PAN_FB_SHADER_COPY_S,
526
527 PAN_FB_SHADER_OP_COUNT,
528 };
529
530 static_assert(PAN_FB_SHADER_COPY_Z == PAN_FB_SHADER_COPY_RT_0 + PAN_MAX_RTS,
531 "We should have one PAN_FB_SHADER_COPY_RT_N per RT");
532
533 #define PAN_FB_SHADER_COPY_RT(rt) \
534 (assert(0 <= (rt) && (rt) < PAN_MAX_RTS), (PAN_FB_SHADER_COPY_RT_0 + (rt)))
535
536 static inline bool
537 pan_fb_shader_op_can_discard(enum pan_fb_shader_op op)
538 {
539 return op == PAN_FB_SHADER_PRESERVE ||
540 op == PAN_FB_SHADER_DONT_CARE ||
541 op == PAN_FB_SHADER_LOAD_CLEAR;
542 }
543
544 enum ENUM_PACKED pan_fb_shader_data_type {
545 PAN_FB_SHADER_DATA_TYPE_F32 = 0,
546 PAN_FB_SHADER_DATA_TYPE_I32,
547 PAN_FB_SHADER_DATA_TYPE_U32,
548 PAN_FB_SHADER_DATA_TYPE_COUNT,
549 };
550
551 static_assert(PAN_FB_SHADER_OP_COUNT <= (1 << 4),
552 "pan_fb_shader_op fits in 4 bits");
553 static_assert(PAN_FB_MSAA_COPY_OP_COUNT <= (1 << 3),
554 "pan_fb_msaa_copy_op fits in 3 bits");
555 static_assert(PAN_FB_SHADER_DATA_TYPE_COUNT <= (1 << 2),
556 "pan_fb_shader_data_type fits in 2 bits");
557
558 /* Asserts for glsl_sampler_dim glsl_base_type have to be runtime because
559 * there is no MAX value we can use.
560 */
561
562 PRAGMA_DIAGNOSTIC_PUSH
563 PRAGMA_DIAGNOSTIC_ERROR(-Wpadded)
564 struct pan_fb_shader_key_target {
565 uint32_t in_bounds_op : 4;
566 uint32_t border_op : 4;
567 uint32_t in_bounds_msaa : 3;
568 uint32_t border_msaa : 3;
569 uint32_t sample0_only : 1;
570 uint32_t image_dim : 2;
571 uint32_t image_is_array : 1;
572 uint32_t image_samples_log2 : 3;
573 uint32_t data_type : 2;
574 uint32_t pad : 9;
575 };
576 PRAGMA_DIAGNOSTIC_POP
577 static_assert(sizeof(struct pan_fb_shader_key_target) == 4,
578 "This struct has no holes");
579
580 /** Whether or not the given target is written by the FB shader
581 *
582 * GENX(pan_get_fb_shader)() guarantees that if both in_bounds_op and
583 * border_op can be discarded, that it does nothing with that target. This
584 * way the full outputs_written can be known from just the shader key.
585 */
586 static inline bool
587 pan_fb_shader_key_target_written(const struct pan_fb_shader_key_target *target)
588 {
589 return !pan_fb_shader_op_can_discard(target->in_bounds_op) ||
590 !pan_fb_shader_op_can_discard(target->border_op);
591 }
592
593 PRAGMA_DIAGNOSTIC_PUSH
594 PRAGMA_DIAGNOSTIC_ERROR(-Wpadded)
595 struct pan_fb_shader_key {
596 struct pan_fb_shader_key_target rts[PAN_MAX_RTS];
597 struct pan_fb_shader_key_target z, s;
598 uint16_t z_format;
599 uint8_t fb_sample_count;
600 uint8_t _pad;
601 };
602 PRAGMA_DIAGNOSTIC_POP
603 static_assert(sizeof(struct pan_fb_shader_key) == 4 * (PAN_MAX_RTS + 3),
604 "This struct has no holes");
605
606 #ifdef PAN_ARCH
607 bool GENX(pan_fb_load_shader_key_fill)(struct pan_fb_shader_key *key,
608 const struct pan_fb_layout *fb,
609 const struct pan_fb_load *load,
610 bool zs_prepass);
611
612 #if PAN_ARCH >= 6
613 bool GENX(pan_fb_resolve_shader_key_fill)(struct pan_fb_shader_key *key,
614 const struct pan_fb_layout *fb,
615 const struct pan_fb_resolve *resolve);
616 #endif
617
618 struct nir_shader *
619 GENX(pan_get_fb_shader)(const struct pan_fb_shader_key *key,
620 const struct nir_shader_compiler_options *nir_options);
621 #endif
622
623 #endif /* __PAN_FB_H */