forked from notaz/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblorp_blit.c
3153 lines (2791 loc) · 118 KB
/
blorp_blit.c
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2012 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "blorp_nir_builder.h"
#include "compiler/nir/nir_format_convert.h"
#include "blorp_priv.h"
#include "dev/intel_debug.h"
#include "util/format_rgb9e5.h"
/* header-only include needed for _mesa_unorm_to_float and friends. */
#include "mesa/main/format_utils.h"
#include "util/u_math.h"
#define FILE_DEBUG_FLAG DEBUG_BLORP
static const bool split_blorp_blit_debug = false;
struct brw_blorp_blit_vars {
/* Input values from brw_blorp_wm_inputs */
nir_variable *v_bounds_rect;
nir_variable *v_rect_grid;
nir_variable *v_coord_transform;
nir_variable *v_src_z;
nir_variable *v_src_offset;
nir_variable *v_dst_offset;
nir_variable *v_src_inv_size;
};
static void
brw_blorp_blit_vars_init(nir_builder *b, struct brw_blorp_blit_vars *v,
const struct brw_blorp_blit_prog_key *key)
{
#define LOAD_INPUT(name, type)\
v->v_##name = BLORP_CREATE_NIR_INPUT(b->shader, name, type);
LOAD_INPUT(bounds_rect, glsl_vec4_type())
LOAD_INPUT(rect_grid, glsl_vec4_type())
LOAD_INPUT(coord_transform, glsl_vec4_type())
LOAD_INPUT(src_z, glsl_float_type())
LOAD_INPUT(src_offset, glsl_vector_type(GLSL_TYPE_UINT, 2))
LOAD_INPUT(dst_offset, glsl_vector_type(GLSL_TYPE_UINT, 2))
LOAD_INPUT(src_inv_size, glsl_vector_type(GLSL_TYPE_FLOAT, 2))
#undef LOAD_INPUT
}
static nir_ssa_def *
blorp_blit_get_frag_coords(nir_builder *b,
const struct brw_blorp_blit_prog_key *key,
struct brw_blorp_blit_vars *v)
{
nir_ssa_def *coord = nir_f2i32(b, nir_load_frag_coord(b));
/* Account for destination surface intratile offset
*
* Transformation parameters giving translation from destination to source
* coordinates don't take into account possible intra-tile destination
* offset. Therefore it has to be first subtracted from the incoming
* coordinates. Vertices are set up based on coordinates containing the
* intra-tile offset.
*/
if (key->need_dst_offset)
coord = nir_isub(b, coord, nir_load_var(b, v->v_dst_offset));
if (key->persample_msaa_dispatch) {
b->shader->info.fs.uses_sample_shading = true;
return nir_vec3(b, nir_channel(b, coord, 0), nir_channel(b, coord, 1),
nir_load_sample_id(b));
} else {
return nir_vec2(b, nir_channel(b, coord, 0), nir_channel(b, coord, 1));
}
}
static nir_ssa_def *
blorp_blit_get_cs_dst_coords(nir_builder *b,
const struct brw_blorp_blit_prog_key *key,
struct brw_blorp_blit_vars *v)
{
nir_ssa_def *coord = nir_load_global_invocation_id(b, 32);
/* Account for destination surface intratile offset
*
* Transformation parameters giving translation from destination to source
* coordinates don't take into account possible intra-tile destination
* offset. Therefore it has to be first subtracted from the incoming
* coordinates. Vertices are set up based on coordinates containing the
* intra-tile offset.
*/
if (key->need_dst_offset)
coord = nir_isub(b, coord, nir_load_var(b, v->v_dst_offset));
assert(!key->persample_msaa_dispatch);
return nir_channels(b, coord, 0x3);
}
/**
* Emit code to translate from destination (X, Y) coordinates to source (X, Y)
* coordinates.
*/
static nir_ssa_def *
blorp_blit_apply_transform(nir_builder *b, nir_ssa_def *src_pos,
struct brw_blorp_blit_vars *v)
{
nir_ssa_def *coord_transform = nir_load_var(b, v->v_coord_transform);
nir_ssa_def *offset = nir_vec2(b, nir_channel(b, coord_transform, 1),
nir_channel(b, coord_transform, 3));
nir_ssa_def *mul = nir_vec2(b, nir_channel(b, coord_transform, 0),
nir_channel(b, coord_transform, 2));
return nir_fadd(b, nir_fmul(b, src_pos, mul), offset);
}
static nir_tex_instr *
blorp_create_nir_tex_instr(nir_builder *b, struct brw_blorp_blit_vars *v,
nir_texop op, nir_ssa_def *pos, unsigned num_srcs,
nir_alu_type dst_type)
{
nir_tex_instr *tex = nir_tex_instr_create(b->shader, num_srcs);
tex->op = op;
tex->dest_type = dst_type | 32;
tex->is_array = false;
tex->is_shadow = false;
tex->texture_index = BLORP_TEXTURE_BT_INDEX;
tex->sampler_index = BLORP_SAMPLER_INDEX;
/* To properly handle 3-D and 2-D array textures, we pull the Z component
* from an input. TODO: This is a bit magic; we should probably make this
* more explicit in the future.
*/
assert(pos->num_components >= 2);
if (op == nir_texop_txf || op == nir_texop_txf_ms ||
op == nir_texop_txf_ms_mcs_intel) {
pos = nir_vec3(b, nir_channel(b, pos, 0), nir_channel(b, pos, 1),
nir_f2i32(b, nir_load_var(b, v->v_src_z)));
} else {
pos = nir_vec3(b, nir_channel(b, pos, 0), nir_channel(b, pos, 1),
nir_load_var(b, v->v_src_z));
}
tex->src[0].src_type = nir_tex_src_coord;
tex->src[0].src = nir_src_for_ssa(pos);
tex->coord_components = 3;
nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
return tex;
}
static nir_ssa_def *
blorp_nir_tex(nir_builder *b, struct brw_blorp_blit_vars *v,
const struct brw_blorp_blit_prog_key *key, nir_ssa_def *pos)
{
if (key->need_src_offset)
pos = nir_fadd(b, pos, nir_i2f32(b, nir_load_var(b, v->v_src_offset)));
/* If the sampler requires normalized coordinates, we need to compensate. */
if (key->src_coords_normalized)
pos = nir_fmul(b, pos, nir_load_var(b, v->v_src_inv_size));
nir_tex_instr *tex =
blorp_create_nir_tex_instr(b, v, nir_texop_txl, pos, 2,
key->texture_data_type);
assert(pos->num_components == 2);
tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
tex->src[1].src_type = nir_tex_src_lod;
tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
nir_builder_instr_insert(b, &tex->instr);
return &tex->dest.ssa;
}
static nir_ssa_def *
blorp_nir_txf(nir_builder *b, struct brw_blorp_blit_vars *v,
nir_ssa_def *pos, nir_alu_type dst_type)
{
nir_tex_instr *tex =
blorp_create_nir_tex_instr(b, v, nir_texop_txf, pos, 2, dst_type);
tex->sampler_dim = GLSL_SAMPLER_DIM_3D;
tex->src[1].src_type = nir_tex_src_lod;
tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
nir_builder_instr_insert(b, &tex->instr);
return &tex->dest.ssa;
}
static nir_ssa_def *
blorp_nir_txf_ms(nir_builder *b, struct brw_blorp_blit_vars *v,
nir_ssa_def *pos, nir_ssa_def *mcs, nir_alu_type dst_type)
{
nir_tex_instr *tex =
blorp_create_nir_tex_instr(b, v, nir_texop_txf_ms, pos,
mcs != NULL ? 3 : 2, dst_type);
tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
tex->src[1].src_type = nir_tex_src_ms_index;
if (pos->num_components == 2) {
tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
} else {
assert(pos->num_components == 3);
tex->src[1].src = nir_src_for_ssa(nir_channel(b, pos, 2));
}
if (mcs) {
tex->src[2].src_type = nir_tex_src_ms_mcs_intel;
tex->src[2].src = nir_src_for_ssa(mcs);
}
nir_builder_instr_insert(b, &tex->instr);
return &tex->dest.ssa;
}
static nir_ssa_def *
blorp_blit_txf_ms_mcs(nir_builder *b, struct brw_blorp_blit_vars *v,
nir_ssa_def *pos)
{
nir_tex_instr *tex =
blorp_create_nir_tex_instr(b, v, nir_texop_txf_ms_mcs_intel,
pos, 1, nir_type_int);
tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
nir_builder_instr_insert(b, &tex->instr);
return &tex->dest.ssa;
}
/**
* Emit code to compensate for the difference between Y and W tiling.
*
* This code modifies the X and Y coordinates according to the formula:
*
* (X', Y', S') = detile(W-MAJOR, tile(Y-MAJOR, X, Y, S))
*
* (See brw_blorp_build_nir_shader).
*/
static inline nir_ssa_def *
blorp_nir_retile_y_to_w(nir_builder *b, nir_ssa_def *pos)
{
assert(pos->num_components == 2);
nir_ssa_def *x_Y = nir_channel(b, pos, 0);
nir_ssa_def *y_Y = nir_channel(b, pos, 1);
/* Given X and Y coordinates that describe an address using Y tiling,
* translate to the X and Y coordinates that describe the same address
* using W tiling.
*
* If we break down the low order bits of X and Y, using a
* single letter to represent each low-order bit:
*
* X = A << 7 | 0bBCDEFGH
* Y = J << 5 | 0bKLMNP (1)
*
* Then we can apply the Y tiling formula to see the memory offset being
* addressed:
*
* offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
*
* If we apply the W detiling formula to this memory location, that the
* corresponding X' and Y' coordinates are:
*
* X' = A << 6 | 0bBCDPFH (3)
* Y' = J << 6 | 0bKLMNEG
*
* Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
* we need to make the following computation:
*
* X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
* Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
*/
nir_ssa_def *x_W = nir_imm_int(b, 0);
x_W = nir_mask_shift_or(b, x_W, x_Y, 0xfffffff4, -1);
x_W = nir_mask_shift_or(b, x_W, y_Y, 0x1, 2);
x_W = nir_mask_shift_or(b, x_W, x_Y, 0x1, 0);
nir_ssa_def *y_W = nir_imm_int(b, 0);
y_W = nir_mask_shift_or(b, y_W, y_Y, 0xfffffffe, 1);
y_W = nir_mask_shift_or(b, y_W, x_Y, 0x8, -2);
y_W = nir_mask_shift_or(b, y_W, x_Y, 0x2, -1);
return nir_vec2(b, x_W, y_W);
}
/**
* Emit code to compensate for the difference between Y and W tiling.
*
* This code modifies the X and Y coordinates according to the formula:
*
* (X', Y', S') = detile(Y-MAJOR, tile(W-MAJOR, X, Y, S))
*
* (See brw_blorp_build_nir_shader).
*/
static inline nir_ssa_def *
blorp_nir_retile_w_to_y(nir_builder *b, nir_ssa_def *pos)
{
assert(pos->num_components == 2);
nir_ssa_def *x_W = nir_channel(b, pos, 0);
nir_ssa_def *y_W = nir_channel(b, pos, 1);
/* Applying the same logic as above, but in reverse, we obtain the
* formulas:
*
* X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
* Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
*/
nir_ssa_def *x_Y = nir_imm_int(b, 0);
x_Y = nir_mask_shift_or(b, x_Y, x_W, 0xfffffffa, 1);
x_Y = nir_mask_shift_or(b, x_Y, y_W, 0x2, 2);
x_Y = nir_mask_shift_or(b, x_Y, y_W, 0x1, 1);
x_Y = nir_mask_shift_or(b, x_Y, x_W, 0x1, 0);
nir_ssa_def *y_Y = nir_imm_int(b, 0);
y_Y = nir_mask_shift_or(b, y_Y, y_W, 0xfffffffc, -1);
y_Y = nir_mask_shift_or(b, y_Y, x_W, 0x4, -2);
return nir_vec2(b, x_Y, y_Y);
}
/**
* Emit code to compensate for the difference between MSAA and non-MSAA
* surfaces.
*
* This code modifies the X and Y coordinates according to the formula:
*
* (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
*
* (See brw_blorp_blit_program).
*/
static inline nir_ssa_def *
blorp_nir_encode_msaa(nir_builder *b, nir_ssa_def *pos,
unsigned num_samples, enum isl_msaa_layout layout)
{
assert(pos->num_components == 2 || pos->num_components == 3);
switch (layout) {
case ISL_MSAA_LAYOUT_NONE:
assert(pos->num_components == 2);
return pos;
case ISL_MSAA_LAYOUT_ARRAY:
/* No translation needed */
return pos;
case ISL_MSAA_LAYOUT_INTERLEAVED: {
nir_ssa_def *x_in = nir_channel(b, pos, 0);
nir_ssa_def *y_in = nir_channel(b, pos, 1);
nir_ssa_def *s_in = pos->num_components == 2 ? nir_imm_int(b, 0) :
nir_channel(b, pos, 2);
nir_ssa_def *x_out = nir_imm_int(b, 0);
nir_ssa_def *y_out = nir_imm_int(b, 0);
switch (num_samples) {
case 2:
case 4:
/* encode_msaa(2, IMS, X, Y, S) = (X', Y', 0)
* where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
* Y' = Y
*
* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
* where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
* Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
*/
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 1);
x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
if (num_samples == 2) {
y_out = y_in;
} else {
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 1);
y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
}
break;
case 8:
/* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
* where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
* | (X & 0b1)
* Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
*/
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 2);
x_out = nir_mask_shift_or(b, x_out, s_in, 0x4, 0);
x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 1);
y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
break;
case 16:
/* encode_msaa(16, IMS, X, Y, S) = (X', Y', 0)
* where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
* | (X & 0b1)
* Y' = (Y & ~0b1) << 2 | (S & 0b1000) >> 1 (S & 0b10)
* | (Y & 0b1)
*/
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 2);
x_out = nir_mask_shift_or(b, x_out, s_in, 0x4, 0);
x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 2);
y_out = nir_mask_shift_or(b, y_out, s_in, 0x8, -1);
y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
break;
default:
unreachable("Invalid number of samples for IMS layout");
}
return nir_vec2(b, x_out, y_out);
}
default:
unreachable("Invalid MSAA layout");
}
}
/**
* Emit code to compensate for the difference between MSAA and non-MSAA
* surfaces.
*
* This code modifies the X and Y coordinates according to the formula:
*
* (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
*
* (See brw_blorp_blit_program).
*/
static inline nir_ssa_def *
blorp_nir_decode_msaa(nir_builder *b, nir_ssa_def *pos,
unsigned num_samples, enum isl_msaa_layout layout)
{
assert(pos->num_components == 2 || pos->num_components == 3);
switch (layout) {
case ISL_MSAA_LAYOUT_NONE:
/* No translation necessary, and S should already be zero. */
assert(pos->num_components == 2);
return pos;
case ISL_MSAA_LAYOUT_ARRAY:
/* No translation necessary. */
return pos;
case ISL_MSAA_LAYOUT_INTERLEAVED: {
assert(pos->num_components == 2);
nir_ssa_def *x_in = nir_channel(b, pos, 0);
nir_ssa_def *y_in = nir_channel(b, pos, 1);
nir_ssa_def *x_out = nir_imm_int(b, 0);
nir_ssa_def *y_out = nir_imm_int(b, 0);
nir_ssa_def *s_out = nir_imm_int(b, 0);
switch (num_samples) {
case 2:
case 4:
/* decode_msaa(2, IMS, X, Y, 0) = (X', Y', S)
* where X' = (X & ~0b11) >> 1 | (X & 0b1)
* S = (X & 0b10) >> 1
*
* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
* where X' = (X & ~0b11) >> 1 | (X & 0b1)
* Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
* S = (Y & 0b10) | (X & 0b10) >> 1
*/
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffc, -1);
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
if (num_samples == 2) {
y_out = y_in;
s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
} else {
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffc, -1);
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
}
break;
case 8:
/* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
* where X' = (X & ~0b111) >> 2 | (X & 0b1)
* Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
* S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
*/
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffff8, -2);
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffc, -1);
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
s_out = nir_mask_shift_or(b, s_out, x_in, 0x4, 0);
s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
break;
case 16:
/* decode_msaa(16, IMS, X, Y, 0) = (X', Y', S)
* where X' = (X & ~0b111) >> 2 | (X & 0b1)
* Y' = (Y & ~0b111) >> 2 | (Y & 0b1)
* S = (Y & 0b100) << 1 | (X & 0b100) |
* (Y & 0b10) | (X & 0b10) >> 1
*/
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffff8, -2);
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffff8, -2);
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
s_out = nir_mask_shift_or(b, s_out, y_in, 0x4, 1);
s_out = nir_mask_shift_or(b, s_out, x_in, 0x4, 0);
s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
break;
default:
unreachable("Invalid number of samples for IMS layout");
}
return nir_vec3(b, x_out, y_out, s_out);
}
default:
unreachable("Invalid MSAA layout");
}
}
/**
* Count the number of trailing 1 bits in the given value. For example:
*
* count_trailing_one_bits(0) == 0
* count_trailing_one_bits(7) == 3
* count_trailing_one_bits(11) == 2
*/
static inline int count_trailing_one_bits(unsigned value)
{
#ifdef HAVE___BUILTIN_CTZ
return __builtin_ctz(~value);
#else
return util_bitcount(value & ~(value + 1));
#endif
}
static nir_ssa_def *
blorp_nir_combine_samples(nir_builder *b, struct brw_blorp_blit_vars *v,
nir_ssa_def *pos, unsigned tex_samples,
enum isl_aux_usage tex_aux_usage,
nir_alu_type dst_type,
enum blorp_filter filter)
{
nir_variable *color =
nir_local_variable_create(b->impl, glsl_vec4_type(), "color");
nir_ssa_def *mcs = NULL;
if (isl_aux_usage_has_mcs(tex_aux_usage))
mcs = blorp_blit_txf_ms_mcs(b, v, pos);
nir_op combine_op;
switch (filter) {
case BLORP_FILTER_AVERAGE:
assert(dst_type == nir_type_float);
combine_op = nir_op_fadd;
break;
case BLORP_FILTER_MIN_SAMPLE:
switch (dst_type) {
case nir_type_int: combine_op = nir_op_imin; break;
case nir_type_uint: combine_op = nir_op_umin; break;
case nir_type_float: combine_op = nir_op_fmin; break;
default: unreachable("Invalid dst_type");
}
break;
case BLORP_FILTER_MAX_SAMPLE:
switch (dst_type) {
case nir_type_int: combine_op = nir_op_imax; break;
case nir_type_uint: combine_op = nir_op_umax; break;
case nir_type_float: combine_op = nir_op_fmax; break;
default: unreachable("Invalid dst_type");
}
break;
default:
unreachable("Invalid filter");
}
/* If true, we inserted an if statement that we need to pop at at the end.
*/
bool inserted_if = false;
/* We add together samples using a binary tree structure, e.g. for 4x MSAA:
*
* result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
*
* This ensures that when all samples have the same value, no numerical
* precision is lost, since each addition operation always adds two equal
* values, and summing two equal floating point values does not lose
* precision.
*
* We perform this computation by treating the texture_data array as a
* stack and performing the following operations:
*
* - push sample 0 onto stack
* - push sample 1 onto stack
* - add top two stack entries
* - push sample 2 onto stack
* - push sample 3 onto stack
* - add top two stack entries
* - add top two stack entries
* - divide top stack entry by 4
*
* Note that after pushing sample i onto the stack, the number of add
* operations we do is equal to the number of trailing 1 bits in i. This
* works provided the total number of samples is a power of two, which it
* always is for i965.
*
* For integer formats, we replace the add operations with average
* operations and skip the final division.
*/
nir_ssa_def *texture_data[5];
texture_data[0] = NULL; /* Avoid maybe-uninitialized warning with GCC 10 */
unsigned stack_depth = 0;
for (unsigned i = 0; i < tex_samples; ++i) {
assert(stack_depth == util_bitcount(i)); /* Loop invariant */
/* Push sample i onto the stack */
assert(stack_depth < ARRAY_SIZE(texture_data));
nir_ssa_def *ms_pos = nir_vec3(b, nir_channel(b, pos, 0),
nir_channel(b, pos, 1),
nir_imm_int(b, i));
texture_data[stack_depth++] = blorp_nir_txf_ms(b, v, ms_pos, mcs, dst_type);
if (i == 0 && isl_aux_usage_has_mcs(tex_aux_usage)) {
/* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
* suggests an optimization:
*
* "A simple optimization with probable large return in
* performance is to compare the MCS value to zero (indicating
* all samples are on sample slice 0), and sample only from
* sample slice 0 using ld2dss if MCS is zero."
*
* Note that in the case where the MCS value is zero, sampling from
* sample slice 0 using ld2dss and sampling from sample 0 using
* ld2dms are equivalent (since all samples are on sample slice 0).
* Since we have already sampled from sample 0, all we need to do is
* skip the remaining fetches and averaging if MCS is zero.
*
* It's also trivial to detect when the MCS has the magic clear color
* value. In this case, the txf we did on sample 0 will return the
* clear color and we can skip the remaining fetches just like we do
* when MCS == 0.
*/
nir_ssa_def *mcs_zero = nir_ieq_imm(b, nir_channel(b, mcs, 0), 0);
if (tex_samples == 16) {
mcs_zero = nir_iand(b, mcs_zero,
nir_ieq_imm(b, nir_channel(b, mcs, 1), 0));
}
nir_ssa_def *mcs_clear =
blorp_nir_mcs_is_clear_color(b, mcs, tex_samples);
nir_push_if(b, nir_ior(b, mcs_zero, mcs_clear));
nir_store_var(b, color, texture_data[0], 0xf);
nir_push_else(b, NULL);
inserted_if = true;
}
for (int j = 0; j < count_trailing_one_bits(i); j++) {
assert(stack_depth >= 2);
--stack_depth;
texture_data[stack_depth - 1] =
nir_build_alu(b, combine_op,
texture_data[stack_depth - 1],
texture_data[stack_depth],
NULL, NULL);
}
}
/* We should have just 1 sample on the stack now. */
assert(stack_depth == 1);
if (filter == BLORP_FILTER_AVERAGE) {
assert(dst_type == nir_type_float);
texture_data[0] = nir_fmul(b, texture_data[0],
nir_imm_float(b, 1.0 / tex_samples));
}
nir_store_var(b, color, texture_data[0], 0xf);
if (inserted_if)
nir_pop_if(b, NULL);
return nir_load_var(b, color);
}
static nir_ssa_def *
blorp_nir_manual_blend_bilinear(nir_builder *b, nir_ssa_def *pos,
unsigned tex_samples,
const struct brw_blorp_blit_prog_key *key,
struct brw_blorp_blit_vars *v)
{
nir_ssa_def *pos_xy = nir_channels(b, pos, 0x3);
nir_ssa_def *rect_grid = nir_load_var(b, v->v_rect_grid);
nir_ssa_def *scale = nir_imm_vec2(b, key->x_scale, key->y_scale);
/* Translate coordinates to lay out the samples in a rectangular grid
* roughly corresponding to sample locations.
*/
pos_xy = nir_fmul(b, pos_xy, scale);
/* Adjust coordinates so that integers represent pixel centers rather
* than pixel edges.
*/
pos_xy = nir_fadd(b, pos_xy, nir_imm_float(b, -0.5));
/* Clamp the X, Y texture coordinates to properly handle the sampling of
* texels on texture edges.
*/
pos_xy = nir_fmin(b, nir_fmax(b, pos_xy, nir_imm_float(b, 0.0)),
nir_vec2(b, nir_channel(b, rect_grid, 0),
nir_channel(b, rect_grid, 1)));
/* Store the fractional parts to be used as bilinear interpolation
* coefficients.
*/
nir_ssa_def *frac_xy = nir_ffract(b, pos_xy);
/* Round the float coordinates down to nearest integer */
pos_xy = nir_fdiv(b, nir_ftrunc(b, pos_xy), scale);
nir_ssa_def *tex_data[4];
for (unsigned i = 0; i < 4; ++i) {
float sample_off_x = (float)(i & 0x1) / key->x_scale;
float sample_off_y = (float)((i >> 1) & 0x1) / key->y_scale;
nir_ssa_def *sample_off = nir_imm_vec2(b, sample_off_x, sample_off_y);
nir_ssa_def *sample_coords = nir_fadd(b, pos_xy, sample_off);
nir_ssa_def *sample_coords_int = nir_f2i32(b, sample_coords);
/* The MCS value we fetch has to match up with the pixel that we're
* sampling from. Since we sample from different pixels in each
* iteration of this "for" loop, the call to mcs_fetch() should be
* here inside the loop after computing the pixel coordinates.
*/
nir_ssa_def *mcs = NULL;
if (isl_aux_usage_has_mcs(key->tex_aux_usage))
mcs = blorp_blit_txf_ms_mcs(b, v, sample_coords_int);
/* Compute sample index and map the sample index to a sample number.
* Sample index layout shows the numbering of slots in a rectangular
* grid of samples with in a pixel. Sample number layout shows the
* rectangular grid of samples roughly corresponding to the real sample
* locations with in a pixel.
*
* In the case of 2x MSAA, the layout of sample indices is reversed from
* the layout of sample numbers:
*
* sample index layout : --------- sample number layout : ---------
* | 0 | 1 | | 1 | 0 |
* --------- ---------
*
* In case of 4x MSAA, layout of sample indices matches the layout of
* sample numbers:
* ---------
* | 0 | 1 |
* ---------
* | 2 | 3 |
* ---------
*
* In case of 8x MSAA the two layouts don't match.
* sample index layout : --------- sample number layout : ---------
* | 0 | 1 | | 3 | 7 |
* --------- ---------
* | 2 | 3 | | 5 | 0 |
* --------- ---------
* | 4 | 5 | | 1 | 2 |
* --------- ---------
* | 6 | 7 | | 4 | 6 |
* --------- ---------
*
* Fortunately, this can be done fairly easily as:
* S' = (0x17306425 >> (S * 4)) & 0xf
*
* In the case of 16x MSAA the two layouts don't match.
* Sample index layout: Sample number layout:
* --------------------- ---------------------
* | 0 | 1 | 2 | 3 | | 15 | 10 | 9 | 7 |
* --------------------- ---------------------
* | 4 | 5 | 6 | 7 | | 4 | 1 | 3 | 13 |
* --------------------- ---------------------
* | 8 | 9 | 10 | 11 | | 12 | 2 | 0 | 6 |
* --------------------- ---------------------
* | 12 | 13 | 14 | 15 | | 11 | 8 | 5 | 14 |
* --------------------- ---------------------
*
* This is equivalent to
* S' = (0xe58b602cd31479af >> (S * 4)) & 0xf
*/
nir_ssa_def *frac = nir_ffract(b, sample_coords);
nir_ssa_def *sample =
nir_fdot2(b, frac, nir_imm_vec2(b, key->x_scale,
key->x_scale * key->y_scale));
sample = nir_f2i32(b, sample);
if (tex_samples == 2) {
sample = nir_isub(b, nir_imm_int(b, 1), sample);
} else if (tex_samples == 8) {
sample = nir_iand(b, nir_ishr(b, nir_imm_int(b, 0x64210573),
nir_ishl(b, sample, nir_imm_int(b, 2))),
nir_imm_int(b, 0xf));
} else if (tex_samples == 16) {
nir_ssa_def *sample_low =
nir_iand(b, nir_ishr(b, nir_imm_int(b, 0xd31479af),
nir_ishl(b, sample, nir_imm_int(b, 2))),
nir_imm_int(b, 0xf));
nir_ssa_def *sample_high =
nir_iand(b, nir_ishr(b, nir_imm_int(b, 0xe58b602c),
nir_ishl(b, nir_iadd(b, sample,
nir_imm_int(b, -8)),
nir_imm_int(b, 2))),
nir_imm_int(b, 0xf));
sample = nir_bcsel(b, nir_ilt(b, sample, nir_imm_int(b, 8)),
sample_low, sample_high);
}
nir_ssa_def *pos_ms = nir_vec3(b, nir_channel(b, sample_coords_int, 0),
nir_channel(b, sample_coords_int, 1),
sample);
tex_data[i] = blorp_nir_txf_ms(b, v, pos_ms, mcs, key->texture_data_type);
}
nir_ssa_def *frac_x = nir_channel(b, frac_xy, 0);
nir_ssa_def *frac_y = nir_channel(b, frac_xy, 1);
return nir_flrp(b, nir_flrp(b, tex_data[0], tex_data[1], frac_x),
nir_flrp(b, tex_data[2], tex_data[3], frac_x),
frac_y);
}
/** Perform a color bit-cast operation
*
* For copy operations involving CCS, we may need to use different formats for
* the source and destination surfaces. The two formats must both be UINT
* formats and must have the same size but may have different bit layouts.
* For instance, we may be copying from R8G8B8A8_UINT to R32_UINT or R32_UINT
* to R16G16_UINT. This function generates code to shuffle bits around to get
* us from one to the other.
*/
static nir_ssa_def *
bit_cast_color(struct nir_builder *b, nir_ssa_def *color,
const struct brw_blorp_blit_prog_key *key)
{
if (key->src_format == key->dst_format)
return color;
const struct isl_format_layout *src_fmtl =
isl_format_get_layout(key->src_format);
const struct isl_format_layout *dst_fmtl =
isl_format_get_layout(key->dst_format);
/* They must be formats with the same bit size */
assert(src_fmtl->bpb == dst_fmtl->bpb);
if (src_fmtl->bpb <= 32) {
assert(src_fmtl->channels.r.type == ISL_UINT ||
src_fmtl->channels.r.type == ISL_UNORM);
assert(dst_fmtl->channels.r.type == ISL_UINT ||
dst_fmtl->channels.r.type == ISL_UNORM);
nir_ssa_def *packed = nir_imm_int(b, 0);
for (unsigned c = 0; c < 4; c++) {
if (src_fmtl->channels_array[c].bits == 0)
continue;
const unsigned chan_start_bit = src_fmtl->channels_array[c].start_bit;
const unsigned chan_bits = src_fmtl->channels_array[c].bits;
nir_ssa_def *chan = nir_channel(b, color, c);
if (src_fmtl->channels_array[c].type == ISL_UNORM)
chan = nir_format_float_to_unorm(b, chan, &chan_bits);
packed = nir_ior(b, packed, nir_shift_imm(b, chan, chan_start_bit));
}
nir_ssa_def *chans[4] = { };
for (unsigned c = 0; c < 4; c++) {
if (dst_fmtl->channels_array[c].bits == 0) {
chans[c] = nir_imm_int(b, 0);
continue;
}
const unsigned chan_start_bit = dst_fmtl->channels_array[c].start_bit;
const unsigned chan_bits = dst_fmtl->channels_array[c].bits;
chans[c] = nir_iand(b, nir_shift_imm(b, packed, -(int)chan_start_bit),
nir_imm_int(b, BITFIELD_MASK(chan_bits)));
if (dst_fmtl->channels_array[c].type == ISL_UNORM)
chans[c] = nir_format_unorm_to_float(b, chans[c], &chan_bits);
}
color = nir_vec(b, chans, 4);
} else {
/* This path only supports UINT formats */
assert(src_fmtl->channels.r.type == ISL_UINT);
assert(dst_fmtl->channels.r.type == ISL_UINT);
const unsigned src_bpc = src_fmtl->channels.r.bits;
const unsigned dst_bpc = dst_fmtl->channels.r.bits;
assert(src_fmtl->channels.g.bits == 0 ||
src_fmtl->channels.g.bits == src_fmtl->channels.r.bits);
assert(src_fmtl->channels.b.bits == 0 ||
src_fmtl->channels.b.bits == src_fmtl->channels.r.bits);
assert(src_fmtl->channels.a.bits == 0 ||
src_fmtl->channels.a.bits == src_fmtl->channels.r.bits);
assert(dst_fmtl->channels.g.bits == 0 ||
dst_fmtl->channels.g.bits == dst_fmtl->channels.r.bits);
assert(dst_fmtl->channels.b.bits == 0 ||
dst_fmtl->channels.b.bits == dst_fmtl->channels.r.bits);
assert(dst_fmtl->channels.a.bits == 0 ||
dst_fmtl->channels.a.bits == dst_fmtl->channels.r.bits);
/* Restrict to only the channels we actually have */
const unsigned src_channels =
isl_format_get_num_channels(key->src_format);
color = nir_trim_vector(b, color, src_channels);
color = nir_format_bitcast_uvec_unmasked(b, color, src_bpc, dst_bpc);
}
/* Blorp likes to assume that colors are vec4s */
nir_ssa_def *u = nir_ssa_undef(b, 1, 32);
nir_ssa_def *chans[4] = { u, u, u, u };
for (unsigned i = 0; i < color->num_components; i++)
chans[i] = nir_channel(b, color, i);
return nir_vec4(b, chans[0], chans[1], chans[2], chans[3]);
}
static nir_ssa_def *
select_color_channel(struct nir_builder *b, nir_ssa_def *color,
nir_alu_type data_type,
enum isl_channel_select chan)
{
if (chan == ISL_CHANNEL_SELECT_ZERO) {
return nir_imm_int(b, 0);
} else if (chan == ISL_CHANNEL_SELECT_ONE) {
switch (data_type) {
case nir_type_int:
case nir_type_uint:
return nir_imm_int(b, 1);
case nir_type_float:
return nir_imm_float(b, 1);
default:
unreachable("Invalid data type");
}
} else {
assert((unsigned)(chan - ISL_CHANNEL_SELECT_RED) < 4);
return nir_channel(b, color, chan - ISL_CHANNEL_SELECT_RED);
}
}
static nir_ssa_def *
swizzle_color(struct nir_builder *b, nir_ssa_def *color,
struct isl_swizzle swizzle, nir_alu_type data_type)
{
return nir_vec4(b,
select_color_channel(b, color, data_type, swizzle.r),
select_color_channel(b, color, data_type, swizzle.g),
select_color_channel(b, color, data_type, swizzle.b),
select_color_channel(b, color, data_type, swizzle.a));
}
static nir_ssa_def *
convert_color(struct nir_builder *b, nir_ssa_def *color,
const struct brw_blorp_blit_prog_key *key)
{
/* All of our color conversions end up generating a single-channel color
* value that we need to write out.
*/
nir_ssa_def *value;
if (key->dst_format == ISL_FORMAT_R24_UNORM_X8_TYPELESS) {