Debian Codegraph @

src/panfrost/lib/pan_fb_nir.c

raw
1 /*
2 * Copyright (C) 2026 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "pan_blend.h"
7 #include "pan_desc.h"
8 #include "pan_fb.h"
9
10 #include "compiler/nir/nir.h"
11 #include "compiler/nir/nir_builder.h"
12 #include "compiler/pan_nir.h"
13
14 static enum glsl_sampler_dim
15 mali_to_glsl_dim(enum mali_texture_dimension dim)
16 {
17 switch (dim) {
18 case MALI_TEXTURE_DIMENSION_1D: return GLSL_SAMPLER_DIM_1D;
19 case MALI_TEXTURE_DIMENSION_CUBE:
20 case MALI_TEXTURE_DIMENSION_2D: return GLSL_SAMPLER_DIM_2D;
21 case MALI_TEXTURE_DIMENSION_3D: return GLSL_SAMPLER_DIM_3D;
22 default:
23 UNREACHABLE("Unknown mali_texture_dimension");
24 }
25 }
26
27 static enum pan_fb_shader_data_type
28 data_type_for_format(enum pipe_format format)
29 {
30 if (util_format_is_pure_uint(format))
31 return PAN_FB_SHADER_DATA_TYPE_U32;
32 else if (util_format_is_pure_sint(format))
33 return PAN_FB_SHADER_DATA_TYPE_I32;
34 else
35 return PAN_FB_SHADER_DATA_TYPE_F32;
36 }
37
38 static nir_alu_type
39 nir_alu_type_for_data_type(enum pan_fb_shader_data_type data_type)
40 {
41 switch (data_type) {
42 case PAN_FB_SHADER_DATA_TYPE_F32: return nir_type_float32;
43 case PAN_FB_SHADER_DATA_TYPE_I32: return nir_type_int32;
44 case PAN_FB_SHADER_DATA_TYPE_U32: return nir_type_uint32;
45 default:
46 UNREACHABLE("Invalid pan_fb_shader_data_type");
47 }
48 }
49
50 static enum pan_fb_shader_op
51 get_shader_op_for_load(enum pan_fb_load_op op)
52 {
53 switch (op) {
54 case PAN_FB_LOAD_NONE: return PAN_FB_SHADER_DONT_CARE;
55 case PAN_FB_LOAD_CLEAR: return PAN_FB_SHADER_LOAD_CLEAR;
56 case PAN_FB_LOAD_IMAGE: return PAN_FB_SHADER_LOAD_IMAGE;
57 default:
58 UNREACHABLE("Invalid load op");
59 }
60 }
61
62 static inline enum pan_fb_shader_op
63 get_shader_op_for_resolve(enum pan_fb_resolve_op op)
64 {
65 switch (op) {
66 case PAN_FB_RESOLVE_NONE: return PAN_FB_SHADER_PRESERVE;
67 case PAN_FB_RESOLVE_IMAGE: return PAN_FB_SHADER_LOAD_IMAGE;
68 case PAN_FB_RESOLVE_RT_0: return PAN_FB_SHADER_COPY_RT_0;
69 case PAN_FB_RESOLVE_RT_1: return PAN_FB_SHADER_COPY_RT_1;
70 case PAN_FB_RESOLVE_RT_2: return PAN_FB_SHADER_COPY_RT_2;
71 case PAN_FB_RESOLVE_RT_3: return PAN_FB_SHADER_COPY_RT_3;
72 case PAN_FB_RESOLVE_RT_4: return PAN_FB_SHADER_COPY_RT_4;
73 case PAN_FB_RESOLVE_RT_5: return PAN_FB_SHADER_COPY_RT_5;
74 case PAN_FB_RESOLVE_RT_6: return PAN_FB_SHADER_COPY_RT_6;
75 case PAN_FB_RESOLVE_RT_7: return PAN_FB_SHADER_COPY_RT_7;
76 case PAN_FB_RESOLVE_Z: return PAN_FB_SHADER_COPY_Z;
77 case PAN_FB_RESOLVE_S: return PAN_FB_SHADER_COPY_S;
78 default:
79 UNREACHABLE("Invalid resolve op");
80 }
81 }
82
83 static enum pan_fb_msaa_copy_op
84 reduce_msaa_op(enum pan_fb_msaa_copy_op msaa, enum pan_fb_shader_op op,
85 uint8_t fb_sample_count, uint8_t image_sample_count)
86 {
87 if (pan_fb_shader_op_can_discard(op))
88 return PAN_FB_MSAA_COPY_ALL;
89
90 const uint8_t src_sample_count = op == PAN_FB_SHADER_LOAD_IMAGE
91 ? image_sample_count : fb_sample_count;
92 if (msaa == PAN_FB_MSAA_COPY_ALL)
93 assert(src_sample_count == fb_sample_count);
94
95 if (src_sample_count <= 1)
96 return PAN_FB_MSAA_COPY_SINGLE;
97
98 return msaa;
99 }
100
101 static bool
102 op_needs_sample_count(enum pan_fb_shader_op op,
103 enum pan_fb_msaa_copy_op msaa)
104 {
105 return msaa == PAN_FB_MSAA_COPY_AVERAGE ||
106 msaa == PAN_FB_MSAA_COPY_MIN ||
107 msaa == PAN_FB_MSAA_COPY_MAX;
108 }
109
110 static const struct pan_fb_shader_key_target key_target_dont_care = {
111 .in_bounds_op = PAN_FB_SHADER_DONT_CARE,
112 .border_op = PAN_FB_SHADER_DONT_CARE,
113 };
114
115 static const struct pan_fb_shader_key_target key_target_preserve = {
116 .in_bounds_op = PAN_FB_SHADER_PRESERVE,
117 .border_op = PAN_FB_SHADER_PRESERVE,
118 };
119
120 static struct pan_fb_shader_key_target
121 get_key_target(enum pipe_format format,
122 uint8_t fb_sample_count,
123 bool has_border,
124 enum pan_fb_shader_op in_bounds_op,
125 enum pan_fb_shader_op border_op,
126 enum pan_fb_msaa_copy_op in_bounds_msaa,
127 enum pan_fb_msaa_copy_op border_msaa,
128 bool sample0_only,
129 const struct pan_image_view *iview)
130 {
131 if (format == PIPE_FORMAT_NONE)
132 return key_target_dont_care;
133
134 /* If we have a full framebuffer, there is no border. Set the boarder load
135 * equal to the in-bounds load so things in the shader fold nicely.
136 */
137 if (!has_border) {
138 border_op = in_bounds_op;
139 border_msaa = in_bounds_msaa;
140 }
141
142 if (in_bounds_op == PAN_FB_SHADER_DONT_CARE &&
143 border_op == PAN_FB_SHADER_DONT_CARE)
144 return key_target_dont_care;
145
146 /* If both load ops can be implemented as a discard, set the target to
147 * always PRESERVE and we just won't emit any code to do a load.
148 *
149 * Importantly, this is true for all load ops except PAN_FB_LOAD_OP_IMAGE.
150 */
151 if (pan_fb_shader_op_can_discard(in_bounds_op) &&
152 pan_fb_shader_op_can_discard(border_op))
153 return key_target_preserve;
154
155 /* We don't have a bound on this enum so we need a live assert */
156 enum mali_texture_dimension dim = MALI_TEXTURE_DIMENSION_2D;
157 bool is_array = false;
158 uint8_t image_sample_count = 0;
159 if (in_bounds_op == PAN_FB_SHADER_LOAD_IMAGE ||
160 border_op == PAN_FB_SHADER_LOAD_IMAGE) {
161 if (util_format_has_depth(util_format_description(format)))
162 assert(util_format_get_depth_bits(format) ==
163 util_format_get_depth_bits(iview->format));
164 else if (util_format_has_stencil(util_format_description(format)))
165 assert(util_format_has_stencil(util_format_description(iview->format)));
166 else
167 assert(format == iview->format);
168
169 dim = iview->dim;
170 is_array = iview->first_layer != iview->last_layer;
171 image_sample_count = pan_image_view_get_nr_samples(iview);
172 }
173
174 in_bounds_msaa = reduce_msaa_op(in_bounds_msaa, in_bounds_op,
175 fb_sample_count, image_sample_count);
176 border_msaa = reduce_msaa_op(border_msaa, border_op,
177 fb_sample_count, image_sample_count);
178
179 const bool needs_sample_count =
180 op_needs_sample_count(in_bounds_op, in_bounds_msaa) ||
181 op_needs_sample_count(border_op, border_msaa);
182
183 assert(((unsigned)dim) < (1 << 2));
184
185 return (struct pan_fb_shader_key_target) {
186 .in_bounds_op = in_bounds_op,
187 .border_op = border_op,
188 .in_bounds_msaa = in_bounds_msaa,
189 .border_msaa = border_msaa,
190 .sample0_only = sample0_only,
191 .image_dim = dim,
192 .image_is_array = is_array,
193 .image_samples_log2 = needs_sample_count
194 ? util_logbase2(image_sample_count) : 0,
195 .data_type = data_type_for_format(format),
196 };
197 }
198
199 static struct pan_fb_shader_key_target
200 get_load_key_target(enum pipe_format format,
201 uint8_t fb_sample_count,
202 bool has_border,
203 const struct pan_fb_load_target *load)
204 {
205 return get_key_target(format, fb_sample_count, has_border,
206 get_shader_op_for_load(load->in_bounds_load),
207 get_shader_op_for_load(load->border_load),
208 load->msaa, load->msaa, false, load->iview);
209 }
210
211 bool
212 GENX(pan_fb_load_shader_key_fill)(struct pan_fb_shader_key *key,
213 const struct pan_fb_layout *fb,
214 const struct pan_fb_load *load,
215 bool zs_prepass)
216 {
217 *key = (struct pan_fb_shader_key) { };
218
219 const bool has_border = pan_fb_has_partial_tiles(fb);
220
221 /* Z/S can only be written from the prepass and color can only be written
222 * from the non-prepass. Setting everything to zero will cause the shader
223 * to just not write anything in that case.
224 */
225 if (zs_prepass) {
226 key->z = get_load_key_target(fb->z_format, fb->sample_count,
227 has_border, &load->z);
228 key->s = get_load_key_target(fb->s_format, fb->sample_count,
229 has_border, &load->s);
230 return pan_fb_shader_key_target_written(&key->z) ||
231 pan_fb_shader_key_target_written(&key->s);
232 } else {
233 bool needs_shader = false;
234 for (unsigned rt = 0; rt < fb->rt_count; rt++) {
235 key->rts[rt] = get_load_key_target(fb->rt_formats[rt],
236 fb->sample_count,
237 has_border, &load->rts[rt]);
238 if (pan_fb_shader_key_target_written(&key->rts[rt]))
239 needs_shader = true;
240 }
241 return needs_shader;
242 }
243 }
244
245 #if PAN_ARCH >= 6
246 static struct pan_fb_shader_key_target
247 get_resolve_key_target(enum pipe_format format,
248 uint8_t fb_sample_count,
249 bool has_border,
250 const struct pan_fb_resolve_target *load)
251 {
252 const enum pan_fb_shader_op in_bounds_op =
253 get_shader_op_for_resolve(load->in_bounds.resolve);
254 const enum pan_fb_shader_op border_op =
255 get_shader_op_for_resolve(load->border.resolve);
256 return get_key_target(format, fb_sample_count, has_border,
257 in_bounds_op, border_op,
258 load->in_bounds.msaa, load->border.msaa,
259 true, load->iview);
260 }
261
262 bool
263 GENX(pan_fb_resolve_shader_key_fill)(struct pan_fb_shader_key *key,
264 const struct pan_fb_layout *fb,
265 const struct pan_fb_resolve *resolve)
266 {
267 const bool has_border = pan_fb_has_partial_tiles(fb);
268
269 *key = (struct pan_fb_shader_key) {
270 .z = get_resolve_key_target(fb->z_format, fb->sample_count,
271 has_border, &resolve->z),
272 .s = get_resolve_key_target(fb->s_format, fb->sample_count,
273 has_border, &resolve->s),
274 .z_format = fb->z_format,
275 .fb_sample_count = fb->sample_count,
276 };
277 bool needs_shader = pan_fb_shader_key_target_written(&key->z) ||
278 pan_fb_shader_key_target_written(&key->s);
279
280 for (unsigned rt = 0; rt < fb->rt_count; rt++) {
281 key->rts[rt] = get_resolve_key_target(fb->rt_formats[rt],
282 fb->sample_count,
283 has_border,
284 &resolve->rts[rt]);
285 if (pan_fb_shader_key_target_written(&key->rts[rt]))
286 needs_shader = true;
287 }
288
289 return needs_shader;
290 }
291 #endif
292
293 static nir_def *
294 combine_samples_no_div(nir_builder *b, nir_def **samples, uint8_t sample_count,
295 const nir_alu_type nir_type,
296 enum pan_fb_msaa_copy_op msaa)
297 {
298 assert(util_is_power_of_two_nonzero(sample_count));
299 if (sample_count == 1)
300 return samples[0];
301
302 nir_def *lo = combine_samples_no_div(b, samples,
303 sample_count / 2, nir_type, msaa);
304 nir_def *hi = combine_samples_no_div(b, samples + (sample_count / 2),
305 sample_count / 2, nir_type, msaa);
306
307 /* We assume that first half always comes before the second so setting the
308 * cursor after the second half combine instruction will give us the least
309 * common ancestor.
310 */
311 b->cursor = nir_after_instr(nir_def_instr(hi));
312
313 switch (msaa) {
314 case PAN_FB_MSAA_COPY_AVERAGE:
315 assert(nir_alu_type_get_base_type(nir_type) == nir_type_float);
316 return nir_fadd(b, lo, hi);
317
318 case PAN_FB_MSAA_COPY_MIN:
319 switch (nir_alu_type_get_base_type(nir_type)) {
320 case nir_type_float:
321 return nir_fmin(b, lo, hi);
322 case nir_type_uint:
323 return nir_umin(b, lo, hi);
324 case nir_type_int:
325 return nir_imin(b, lo, hi);
326 default:
327 UNREACHABLE("Unsupported NIR type");
328 }
329
330 case PAN_FB_MSAA_COPY_MAX:
331 switch (nir_alu_type_get_base_type(nir_type)) {
332 case nir_type_float:
333 return nir_fmax(b, lo, hi);
334 case nir_type_uint:
335 return nir_umax(b, lo, hi);
336 case nir_type_int:
337 return nir_imax(b, lo, hi);
338 default:
339 UNREACHABLE("Unsupported NIR type");
340 }
341
342 default:
343 UNREACHABLE("Invalid MSAA op");
344 }
345 }
346
347 static nir_def *
348 combine_samples(nir_builder *b, nir_def **samples, uint8_t sample_count,
349 const nir_alu_type nir_type, enum pan_fb_msaa_copy_op msaa)
350 {
351 if (msaa == PAN_FB_MSAA_COPY_SAMPLE_0)
352 return samples[0];
353
354 nir_def *val = combine_samples_no_div(b, samples, sample_count,
355 nir_type, msaa);
356 if (msaa == PAN_FB_MSAA_COPY_AVERAGE)
357 val = nir_fdiv_imm(b, val, sample_count);
358
359 return val;
360 }
361
362 static nir_def *
363 build_sample_id(nir_builder *b)
364 {
365 b->shader->info.fs.uses_sample_shading = true;
366 return nir_load_sample_id(b);
367 }
368
369 static nir_def *
370 build_image_load(nir_builder *b, const nir_alu_type nir_type,
371 nir_def *pos, gl_frag_result location,
372 enum pan_fb_msaa_copy_op msaa, uint8_t sample_count,
373 enum mali_texture_dimension dim, bool is_array)
374 {
375 assert(pos->num_components == 3);
376 switch (dim) {
377 case MALI_TEXTURE_DIMENSION_1D:
378 if (is_array)
379 pos = nir_channels(b, pos, 0b101);
380 else
381 pos = nir_channel(b, pos, 0);
382 break;
383
384 case MALI_TEXTURE_DIMENSION_CUBE:
385 assert(is_array);
386 break;
387
388 case MALI_TEXTURE_DIMENSION_2D:
389 if (!is_array)
390 pos = nir_channels(b, pos, 0b011);
391 break;
392
393 case MALI_TEXTURE_DIMENSION_3D:
394 break;
395
396 default:
397 UNREACHABLE("Unsupported dim");
398 }
399
400 nir_def *val;
401 switch (msaa) {
402 case PAN_FB_MSAA_COPY_SINGLE:
403 val = nir_txf(b, pos,
404 .texture_index = location,
405 .dim = mali_to_glsl_dim(dim),
406 .dest_type = nir_type,
407 .is_array = is_array);
408 break;
409
410 case PAN_FB_MSAA_COPY_ALL:
411 case PAN_FB_MSAA_COPY_IDENTICAL:
412 case PAN_FB_MSAA_COPY_SAMPLE_0: {
413 assert(dim == MALI_TEXTURE_DIMENSION_2D);
414
415 nir_def *sample_id = msaa == PAN_FB_MSAA_COPY_ALL
416 ? build_sample_id(b) : nir_imm_int(b, 0);
417
418 val = nir_txf_ms(b, pos, sample_id,
419 .texture_index = location,
420 .dim = GLSL_SAMPLER_DIM_MS,
421 .dest_type = nir_type,
422 .is_array = is_array);
423 break;
424 }
425
426 case PAN_FB_MSAA_COPY_AVERAGE:
427 case PAN_FB_MSAA_COPY_MIN:
428 case PAN_FB_MSAA_COPY_MAX: {
429 assert(dim == MALI_TEXTURE_DIMENSION_2D);
430 assert(sample_count > 0);
431
432 nir_def *samples[16];
433 assert(sample_count <= ARRAY_SIZE(samples));
434 for (uint32_t s = 0; s < sample_count; s++) {
435 samples[s] = nir_txf_ms(b, pos, nir_imm_int(b, s),
436 .texture_index = location,
437 .dim = GLSL_SAMPLER_DIM_MS,
438 .dest_type = nir_type,
439 .is_array = is_array);
440 }
441 val = combine_samples(b, samples, sample_count, nir_type, msaa);
442 break;
443 }
444
445 default:
446 UNREACHABLE("Invalid copy op");
447 }
448
449 if (location == FRAG_RESULT_DEPTH || location == FRAG_RESULT_STENCIL)
450 val = nir_channel(b, val, 0);
451
452 return val;
453 }
454
455 #if PAN_ARCH >= 6
456 static uint32_t
457 get_fb_conversion(nir_alu_type nir_type, enum pipe_format format)
458 {
459 #if PAN_ARCH >= 9
460 return 0;
461 #else
462 struct mali_internal_conversion_packed conv;
463 pan_pack(&conv, INTERNAL_CONVERSION, cfg) {
464 cfg.register_format = pan_blend_type_from_nir(nir_type);
465 cfg.memory_format = GENX(pan_dithered_format_from_pipe_format)(
466 format, false);
467 }
468 return conv.opaque[0];
469 #endif
470 }
471
472 static nir_def *
473 build_single_fb_load(nir_builder *b, const nir_alu_type nir_type,
474 gl_frag_result location, nir_def *sample,
475 enum pipe_format z_format)
476 {
477 const bool is_zs = location == FRAG_RESULT_DEPTH ||
478 location == FRAG_RESULT_STENCIL;
479 unsigned num_components = is_zs ? 1 : 4;
480
481 nir_def *conversion;
482 if (location == FRAG_RESULT_DEPTH) {
483 conversion = nir_imm_int(b, get_fb_conversion(nir_type, z_format));
484 } else if (location == FRAG_RESULT_STENCIL) {
485 conversion = nir_imm_int(b,
486 get_fb_conversion(nir_type, PIPE_FORMAT_S8_UINT));
487 } else {
488 assert(location >= FRAG_RESULT_DATA0);
489 uint8_t rt = location - FRAG_RESULT_DATA0;
490 assert(rt < PAN_MAX_RTS);
491
492 conversion = nir_unpack_64_2x32_split_y(b,
493 nir_load_blend_descriptor_pan(b, .base = rt));
494 }
495
496 nir_def *val = nir_load_tile_pan(b,
497 num_components, 32,
498 pan_nir_tile_location_sample(b, location, sample),
499 pan_nir_tile_default_coverage(b),
500 conversion,
501 .dest_type = nir_type,
502 .io_semantics.location = location,
503 .io_semantics.num_slots = 1);
504
505 /* If we loaded the stencil value, the upper 24 bits might contain garbage,
506 * so we need to mask that off before we do any math on the result.
507 */
508 if (location == FRAG_RESULT_STENCIL)
509 val = nir_iand_imm(b, val, 0xff);
510
511 return val;
512 }
513
514 static nir_def *
515 build_fb_load(nir_builder *b, const nir_alu_type nir_type,
516 gl_frag_result location, enum pipe_format z_format,
517 enum pan_fb_msaa_copy_op msaa, uint8_t fb_sample_count)
518 {
519 switch (msaa) {
520 case PAN_FB_MSAA_COPY_SINGLE:
521 case PAN_FB_MSAA_COPY_ALL:
522 case PAN_FB_MSAA_COPY_IDENTICAL:
523 case PAN_FB_MSAA_COPY_SAMPLE_0: {
524 nir_def *sample_id = msaa == PAN_FB_MSAA_COPY_ALL
525 ? build_sample_id(b) : nir_imm_int(b, 0);
526
527 return build_single_fb_load(b, nir_type, location, sample_id, z_format);
528 }
529
530 case PAN_FB_MSAA_COPY_AVERAGE:
531 case PAN_FB_MSAA_COPY_MIN:
532 case PAN_FB_MSAA_COPY_MAX: {
533 nir_def *samples[16];
534 assert(fb_sample_count > 0 && fb_sample_count <= ARRAY_SIZE(samples));
535 for (uint32_t s = 0; s < fb_sample_count; s++) {
536 samples[s] = build_single_fb_load(b, nir_type, location,
537 nir_imm_int(b, s), z_format);
538 }
539 return combine_samples(b, samples, fb_sample_count, nir_type, msaa);
540 }
541
542 default:
543 UNREACHABLE("Invalid copy op");
544 }
545 }
546 #endif
547
548 static nir_def *
549 build_load(nir_builder *b, nir_def *pos,
550 enum pan_fb_shader_op op, enum pan_fb_msaa_copy_op msaa,
551 gl_frag_result location,
552 const struct pan_fb_shader_key_target *target,
553 const struct pan_fb_shader_key *key)
554 {
555 const nir_alu_type nir_type = nir_alu_type_for_data_type(target->data_type);
556 const bool is_zs = location == FRAG_RESULT_DEPTH ||
557 location == FRAG_RESULT_STENCIL;
558
559 switch (op) {
560 #if PAN_ARCH >= 6
561 case PAN_FB_SHADER_PRESERVE:
562 return build_fb_load(b, nir_type, location, key->z_format,
563 PAN_FB_MSAA_COPY_ALL, key->fb_sample_count);
564 #endif
565
566 case PAN_FB_SHADER_DONT_CARE:
567 return nir_imm_zero(b, is_zs ? 1 : 4, 32);
568
569 case PAN_FB_SHADER_LOAD_CLEAR:
570 return nir_load_clear_value_pan(b, is_zs ? 1 : 4, 32,
571 .io_semantics.location = location,
572 .dest_type = nir_type);
573
574 case PAN_FB_SHADER_LOAD_IMAGE:
575 return build_image_load(b, nir_type, pos, location, msaa,
576 1 << target->image_samples_log2,
577 target->image_dim, target->image_is_array);
578
579 #if PAN_ARCH >= 6
580 default: { /* PAN_FB_SHADER_COPY_RT_N */
581 uint8_t rt = op - PAN_FB_SHADER_COPY_RT_0;
582 assert(rt < PAN_MAX_RTS);
583 return build_fb_load(b, nir_type, FRAG_RESULT_DATA0 + rt,
584 PIPE_FORMAT_NONE, msaa, key->fb_sample_count);
585 }
586
587 case PAN_FB_SHADER_COPY_Z:
588 return build_fb_load(b, nir_type, FRAG_RESULT_DEPTH,
589 key->z_format, msaa, key->fb_sample_count);
590
591 case PAN_FB_SHADER_COPY_S:
592 return build_fb_load(b, nir_type, FRAG_RESULT_STENCIL,
593 PIPE_FORMAT_S8_UINT, msaa, key->fb_sample_count);
594 #else
595 default:
596 UNREACHABLE("Unsupported shader op");
597 #endif
598 }
599 }
600
601 static void
602 build_op(nir_builder *b, nir_def *pos, nir_def *in_bounds,
603 gl_frag_result location,
604 const struct pan_fb_shader_key_target *target,
605 const struct pan_fb_shader_key *key)
606 {
607 nir_if *nif_s0 = NULL;
608 if (target->sample0_only)
609 nif_s0 = nir_push_if(b, nir_ieq_imm(b, build_sample_id(b), 0));
610
611 nir_def *val;
612 if (target->in_bounds_op == target->border_op &&
613 target->in_bounds_msaa == target->border_msaa) {
614 val = build_load(b, pos, target->in_bounds_op, target->in_bounds_msaa,
615 location, target, key);
616 } else {
617 nir_def *in_bounds_val, *border_val;
618 nir_if *nif = nir_push_if(b, in_bounds);
619 {
620 in_bounds_val = build_load(b, pos, target->in_bounds_op,
621 target->in_bounds_msaa,
622 location, target, key);
623 }
624 nir_push_else(b, nif);
625 {
626 border_val = build_load(b, pos, target->border_op,
627 target->border_msaa,
628 location, target, key);
629 }
630 nir_pop_if(b, nif);
631 val = nir_if_phi(b, in_bounds_val, border_val);
632 }
633
634 nir_store_output(b, val, nir_imm_int(b, 0),
635 .base = location, .range = 1,
636 .write_mask = nir_component_mask(val->num_components),
637 .src_type = nir_alu_type_for_data_type(target->data_type),
638 .io_semantics.location = location,
639 .io_semantics.num_slots = 1);
640
641 if (nif_s0)
642 nir_pop_if(b, nif_s0);
643 }
644
645 struct pan_fb_shader_info {
646 bool discard_in_bounds;
647 bool discard_border;
648 bool sample0_only;
649 };
650
651 static void
652 gather_target_info(struct pan_fb_shader_info *info,
653 const struct pan_fb_shader_key_target *target)
654 {
655 /* Ignore any targets we don't write */
656 if (!pan_fb_shader_key_target_written(target))
657 return;
658
659 if (!pan_fb_shader_op_can_discard(target->in_bounds_op))
660 info->discard_in_bounds = false;
661
662 if (!pan_fb_shader_op_can_discard(target->border_op))
663 info->discard_border = false;
664
665 if (!target->sample0_only)
666 info->sample0_only = false;
667 }
668
669 static struct pan_fb_shader_info
670 gather_shader_info(const struct pan_fb_shader_key *key)
671 {
672 struct pan_fb_shader_info info = {
673 .discard_in_bounds = true,
674 .discard_border = true,
675 .sample0_only = true,
676 };
677
678 for (unsigned rt = 0; rt < PAN_MAX_RTS; rt++)
679 gather_target_info(&info, &key->rts[rt]);
680
681 gather_target_info(&info, &key->z);
682 gather_target_info(&info, &key->s);
683
684 return info;
685 }
686
687 static bool
688 opt_sample0_only_intr(nir_builder *b, nir_intrinsic_instr *intr, void *data)
689 {
690 switch (intr->intrinsic) {
691 case nir_intrinsic_load_sample_id:
692 b->cursor = nir_before_instr(&intr->instr);
693 nir_def_replace(&intr->def, nir_imm_int(b, 0));
694 return true;
695
696 case nir_intrinsic_load_cumulative_coverage_pan:
697 b->cursor = nir_before_instr(&intr->instr);
698 nir_def_replace(&intr->def, nir_imm_int(b, 1));
699 return true;
700
701 default:
702 return false;
703 }
704 }
705
706 nir_shader *
707 GENX(pan_get_fb_shader)(const struct pan_fb_shader_key *key,
708 const struct nir_shader_compiler_options *nir_options)
709 {
710 nir_builder builder = nir_builder_init_simple_shader(
711 MESA_SHADER_FRAGMENT, nir_options, "pan-fb-load");
712 nir_builder *b = &builder;
713
714 const struct pan_fb_shader_info info = gather_shader_info(key);
715
716 /* We shouldn't even be building a shader at all in this case but, on the
717 * off chance that we do, build one that just discards.
718 */
719 if (info.discard_in_bounds && info.discard_border) {
720 nir_terminate(b);
721 return builder.shader;
722 }
723
724 nir_def *pos_xy = nir_load_pixel_coord(b);
725 nir_def *pos_x = nir_u2u32(b, nir_channel(b, pos_xy, 0));
726 nir_def *pos_y = nir_u2u32(b, nir_channel(b, pos_xy, 1));
727 nir_def *pos = nir_vec3(b, pos_x, pos_y, nir_load_layer_id(b));
728
729 nir_def *ra = nir_load_fb_render_area_pan(b);
730 nir_def *ra_min_xy = nir_channels(b, ra, 0b0011);
731 nir_def *ra_max_xy = nir_channels(b, ra, 0b1100);
732
733 nir_def *in_bounds =
734 nir_ball(b, nir_iand(b, nir_uge(b, pos_xy, ra_min_xy),
735 nir_uge(b, ra_max_xy, pos_xy)));
736
737 if (PAN_ARCH >= 6 && !pan_fb_shader_key_target_written(&key->z) &&
738 !pan_fb_shader_key_target_written(&key->s)) {
739 /* We assume that the framebuffer will clear if either load op is set to
740 * clear. For color-only on bifrost, we can do a bit better by emitting
741 * BLEND instructions directly only writing the tile buffer if we're
742 * actually loading the image.
743 */
744 for (unsigned rt = 0; rt < PAN_MAX_RTS; rt++) {
745 const struct pan_fb_shader_key_target *target = &key->rts[rt];
746 const gl_frag_result location = FRAG_RESULT_DATA0 + rt;
747
748 if (!pan_fb_shader_key_target_written(target))
749 continue;
750
751 nir_def *coverage = nir_load_cumulative_coverage_pan(b);
752 nir_def *z1 = nir_imm_int(b, 0);
753 nir_def *u4 = nir_undef(b, 4, 32);
754
755 nir_if *nif_s0 = NULL;
756 if (target->sample0_only)
757 nif_s0 = nir_push_if(b, nir_ieq_imm(b, build_sample_id(b), 0));
758
759 nir_def *color;
760 if (target->in_bounds_op == target->border_op &&
761 target->in_bounds_msaa == target->border_msaa) {
762 color = build_load(b, pos, target->in_bounds_op,
763 target->in_bounds_msaa,
764 location, target, key);
765 } else {
766 nir_def *ib_color, *bd_color, *ib_cov, *bd_cov;
767 nir_if *nif = nir_push_if(b, in_bounds);
768 {
769 if (pan_fb_shader_op_can_discard(target->in_bounds_op)) {
770 ib_color = u4;
771 ib_cov = z1;
772 } else {
773 ib_color = build_load(b, pos, target->in_bounds_op,
774 target->in_bounds_msaa,
775 location, target, key);
776 ib_cov = coverage;
777 }
778 }
779 nir_push_else(b, nif);
780 {
781 if (pan_fb_shader_op_can_discard(target->border_op)) {
782 bd_color = u4;
783 bd_cov = z1;
784 } else {
785 bd_color = build_load(b, pos, target->border_op,
786 target->border_msaa,
787 location, target, key);
788 bd_cov = coverage;
789 }
790 }
791 nir_pop_if(b, nif);
792 color = nir_if_phi(b, ib_color, bd_color);
793 coverage = nir_if_phi(b, ib_cov, bd_cov);
794 }
795
796 if (nif_s0) {
797 nir_pop_if(b, nif_s0);
798 color = nir_if_phi(b, color, u4);
799 coverage = nir_if_phi(b, coverage, z1);
800 }
801
802 const nir_alu_type nir_type =
803 nir_alu_type_for_data_type(target->data_type);
804 nir_def *blend = nir_load_blend_descriptor_pan(b, .base = rt);
805
806 nir_blend_pan(b, coverage, blend, color,
807 .src_type = nir_type,
808 .io_semantics.location = location,
809 .io_semantics.num_slots = 1);
810 }
811 } else {
812 if (info.discard_in_bounds) {
813 nir_terminate_if(b, in_bounds);
814 in_bounds = nir_imm_false(b);
815 } else if (info.discard_border) {
816 nir_terminate_if(b, nir_inot(b, in_bounds));
817 in_bounds = nir_imm_true(b);
818 }
819
820 if (info.sample0_only) {
821 /* The little optimization we do at the end will take care of most
822 * of what we need for sample0-only but we also need to write
823 * SAMPLE_MASK so pan_nir_lower_fs_outputs will do the right thing.
824 */
825 nir_store_output(b, nir_imm_int(b, 1), nir_imm_int(b, 0),
826 .base = FRAG_RESULT_SAMPLE_MASK, .range = 1,
827 .write_mask = 1,
828 .src_type = nir_type_uint32,
829 .io_semantics.location = FRAG_RESULT_SAMPLE_MASK,
830 .io_semantics.num_slots = 1);
831 }
832
833 if (pan_fb_shader_key_target_written(&key->z)) {
834 assert(key->z.sample0_only == info.sample0_only);
835 build_op(b, pos, in_bounds, FRAG_RESULT_DEPTH, &key->z, key);
836 }
837
838 if (pan_fb_shader_key_target_written(&key->s)) {
839 assert(key->s.sample0_only == info.sample0_only);
840 build_op(b, pos, in_bounds, FRAG_RESULT_STENCIL, &key->s, key);
841 }
842
843 for (unsigned rt = 0; rt < PAN_MAX_RTS; rt++) {
844 if (pan_fb_shader_key_target_written(&key->rts[rt])) {
845 assert(key->rts[rt].sample0_only == info.sample0_only);
846 build_op(b, pos, in_bounds, FRAG_RESULT_DATA0 + rt,
847 &key->rts[rt], key);
848 }
849 }
850 }
851
852 if (info.sample0_only) {
853 NIR_PASS(_, b->shader, nir_shader_intrinsics_pass,
854 opt_sample0_only_intr, nir_metadata_control_flow, NULL);
855 b->shader->info.fs.uses_sample_shading = false;
856 }
857
858 return builder.shader;
859 }