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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
|
/*
* Copyright © 2016 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 "anv_private.h"
static bool
lookup_blorp_shader(struct blorp_context *blorp,
const void *key, uint32_t key_size,
uint32_t *kernel_out, void *prog_data_out)
{
struct anv_device *device = blorp->driver_ctx;
/* The blorp cache must be a real cache */
assert(device->blorp_shader_cache.cache);
struct anv_shader_bin *bin =
anv_pipeline_cache_search(&device->blorp_shader_cache, key, key_size);
if (!bin)
return false;
/* The cache already has a reference and it's not going anywhere so there
* is no need to hold a second reference.
*/
anv_shader_bin_unref(device, bin);
*kernel_out = bin->kernel.offset;
*(const struct brw_stage_prog_data **)prog_data_out = bin->prog_data;
return true;
}
static void
upload_blorp_shader(struct blorp_context *blorp,
const void *key, uint32_t key_size,
const void *kernel, uint32_t kernel_size,
const struct brw_stage_prog_data *prog_data,
uint32_t prog_data_size,
uint32_t *kernel_out, void *prog_data_out)
{
struct anv_device *device = blorp->driver_ctx;
/* The blorp cache must be a real cache */
assert(device->blorp_shader_cache.cache);
struct anv_pipeline_bind_map bind_map = {
.surface_count = 0,
.sampler_count = 0,
};
struct anv_shader_bin *bin =
anv_pipeline_cache_upload_kernel(&device->blorp_shader_cache,
key, key_size, kernel, kernel_size,
prog_data, prog_data_size, &bind_map);
/* The cache already has a reference and it's not going anywhere so there
* is no need to hold a second reference.
*/
anv_shader_bin_unref(device, bin);
*kernel_out = bin->kernel.offset;
*(const struct brw_stage_prog_data **)prog_data_out = bin->prog_data;
}
void
anv_device_init_blorp(struct anv_device *device)
{
anv_pipeline_cache_init(&device->blorp_shader_cache, device, true);
blorp_init(&device->blorp, device, &device->isl_dev);
device->blorp.compiler = device->instance->physicalDevice.compiler;
device->blorp.mocs.tex = device->default_mocs;
device->blorp.mocs.rb = device->default_mocs;
device->blorp.mocs.vb = device->default_mocs;
device->blorp.lookup_shader = lookup_blorp_shader;
device->blorp.upload_shader = upload_blorp_shader;
switch (device->info.gen) {
case 7:
if (device->info.is_haswell) {
device->blorp.exec = gen75_blorp_exec;
} else {
device->blorp.exec = gen7_blorp_exec;
}
break;
case 8:
device->blorp.exec = gen8_blorp_exec;
break;
case 9:
device->blorp.exec = gen9_blorp_exec;
break;
default:
unreachable("Unknown hardware generation");
}
}
void
anv_device_finish_blorp(struct anv_device *device)
{
blorp_finish(&device->blorp);
anv_pipeline_cache_finish(&device->blorp_shader_cache);
}
static void
get_blorp_surf_for_anv_buffer(struct anv_device *device,
struct anv_buffer *buffer, uint64_t offset,
uint32_t width, uint32_t height,
uint32_t row_pitch, enum isl_format format,
struct blorp_surf *blorp_surf,
struct isl_surf *isl_surf)
{
const struct isl_format_layout *fmtl =
isl_format_get_layout(format);
/* ASTC is the only format which doesn't support linear layouts.
* Create an equivalently sized surface with ISL to get around this.
*/
if (fmtl->txc == ISL_TXC_ASTC) {
/* Use an equivalently sized format */
format = ISL_FORMAT_R32G32B32A32_UINT;
assert(fmtl->bpb == isl_format_get_layout(format)->bpb);
/* Shrink the dimensions for the new format */
width = DIV_ROUND_UP(width, fmtl->bw);
height = DIV_ROUND_UP(height, fmtl->bh);
}
*blorp_surf = (struct blorp_surf) {
.surf = isl_surf,
.addr = {
.buffer = buffer->bo,
.offset = buffer->offset + offset,
},
};
isl_surf_init(&device->isl_dev, isl_surf,
.dim = ISL_SURF_DIM_2D,
.format = format,
.width = width,
.height = height,
.depth = 1,
.levels = 1,
.array_len = 1,
.samples = 1,
.min_pitch = row_pitch,
.usage = ISL_SURF_USAGE_TEXTURE_BIT |
ISL_SURF_USAGE_RENDER_TARGET_BIT,
.tiling_flags = ISL_TILING_LINEAR_BIT);
assert(isl_surf->row_pitch == row_pitch);
}
static void
get_blorp_surf_for_anv_image(const struct anv_image *image,
VkImageAspectFlags aspect,
enum isl_aux_usage aux_usage,
struct blorp_surf *blorp_surf)
{
if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT ||
aux_usage == ISL_AUX_USAGE_HIZ)
aux_usage = ISL_AUX_USAGE_NONE;
const struct anv_surface *surface =
anv_image_get_surface_for_aspect_mask(image, aspect);
*blorp_surf = (struct blorp_surf) {
.surf = &surface->isl,
.addr = {
.buffer = image->bo,
.offset = image->offset + surface->offset,
},
};
if (aux_usage != ISL_AUX_USAGE_NONE) {
blorp_surf->aux_surf = &image->aux_surface.isl,
blorp_surf->aux_addr = (struct blorp_address) {
.buffer = image->bo,
.offset = image->offset + image->aux_surface.offset,
};
blorp_surf->aux_usage = aux_usage;
}
}
void anv_CmdCopyImage(
VkCommandBuffer commandBuffer,
VkImage srcImage,
VkImageLayout srcImageLayout,
VkImage dstImage,
VkImageLayout dstImageLayout,
uint32_t regionCount,
const VkImageCopy* pRegions)
{
ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
ANV_FROM_HANDLE(anv_image, src_image, srcImage);
ANV_FROM_HANDLE(anv_image, dst_image, dstImage);
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer, 0);
for (unsigned r = 0; r < regionCount; r++) {
VkOffset3D srcOffset =
anv_sanitize_image_offset(src_image->type, pRegions[r].srcOffset);
VkOffset3D dstOffset =
anv_sanitize_image_offset(dst_image->type, pRegions[r].dstOffset);
VkExtent3D extent =
anv_sanitize_image_extent(src_image->type, pRegions[r].extent);
unsigned dst_base_layer, layer_count;
if (dst_image->type == VK_IMAGE_TYPE_3D) {
dst_base_layer = pRegions[r].dstOffset.z;
layer_count = pRegions[r].extent.depth;
} else {
dst_base_layer = pRegions[r].dstSubresource.baseArrayLayer;
layer_count = pRegions[r].dstSubresource.layerCount;
}
unsigned src_base_layer;
if (src_image->type == VK_IMAGE_TYPE_3D) {
src_base_layer = pRegions[r].srcOffset.z;
} else {
src_base_layer = pRegions[r].srcSubresource.baseArrayLayer;
assert(pRegions[r].srcSubresource.layerCount == layer_count);
}
assert(pRegions[r].srcSubresource.aspectMask ==
pRegions[r].dstSubresource.aspectMask);
uint32_t a;
for_each_bit(a, pRegions[r].dstSubresource.aspectMask) {
VkImageAspectFlagBits aspect = (1 << a);
struct blorp_surf src_surf, dst_surf;
get_blorp_surf_for_anv_image(src_image, aspect, src_image->aux_usage,
&src_surf);
get_blorp_surf_for_anv_image(dst_image, aspect, dst_image->aux_usage,
&dst_surf);
for (unsigned i = 0; i < layer_count; i++) {
blorp_copy(&batch, &src_surf, pRegions[r].srcSubresource.mipLevel,
src_base_layer + i,
&dst_surf, pRegions[r].dstSubresource.mipLevel,
dst_base_layer + i,
srcOffset.x, srcOffset.y,
dstOffset.x, dstOffset.y,
extent.width, extent.height);
}
}
}
blorp_batch_finish(&batch);
}
static void
copy_buffer_to_image(struct anv_cmd_buffer *cmd_buffer,
struct anv_buffer *anv_buffer,
struct anv_image *anv_image,
uint32_t regionCount,
const VkBufferImageCopy* pRegions,
bool buffer_to_image)
{
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer, 0);
struct {
struct blorp_surf surf;
uint32_t level;
VkOffset3D offset;
} image, buffer, *src, *dst;
buffer.level = 0;
buffer.offset = (VkOffset3D) { 0, 0, 0 };
if (buffer_to_image) {
src = &buffer;
dst = ℑ
} else {
src = ℑ
dst = &buffer;
}
for (unsigned r = 0; r < regionCount; r++) {
const VkImageAspectFlags aspect = pRegions[r].imageSubresource.aspectMask;
get_blorp_surf_for_anv_image(anv_image, aspect, anv_image->aux_usage,
&image.surf);
image.offset =
anv_sanitize_image_offset(anv_image->type, pRegions[r].imageOffset);
image.level = pRegions[r].imageSubresource.mipLevel;
VkExtent3D extent =
anv_sanitize_image_extent(anv_image->type, pRegions[r].imageExtent);
if (anv_image->type != VK_IMAGE_TYPE_3D) {
image.offset.z = pRegions[r].imageSubresource.baseArrayLayer;
extent.depth = pRegions[r].imageSubresource.layerCount;
}
const enum isl_format buffer_format =
anv_get_isl_format(&cmd_buffer->device->info, anv_image->vk_format,
aspect, VK_IMAGE_TILING_LINEAR);
const VkExtent3D bufferImageExtent = {
.width = pRegions[r].bufferRowLength ?
pRegions[r].bufferRowLength : extent.width,
.height = pRegions[r].bufferImageHeight ?
pRegions[r].bufferImageHeight : extent.height,
};
const struct isl_format_layout *buffer_fmtl =
isl_format_get_layout(buffer_format);
const uint32_t buffer_row_pitch =
DIV_ROUND_UP(bufferImageExtent.width, buffer_fmtl->bw) *
(buffer_fmtl->bpb / 8);
const uint32_t buffer_layer_stride =
DIV_ROUND_UP(bufferImageExtent.height, buffer_fmtl->bh) *
buffer_row_pitch;
struct isl_surf buffer_isl_surf;
get_blorp_surf_for_anv_buffer(cmd_buffer->device,
anv_buffer, pRegions[r].bufferOffset,
extent.width, extent.height,
buffer_row_pitch, buffer_format,
&buffer.surf, &buffer_isl_surf);
for (unsigned z = 0; z < extent.depth; z++) {
blorp_copy(&batch, &src->surf, src->level, src->offset.z,
&dst->surf, dst->level, dst->offset.z,
src->offset.x, src->offset.y, dst->offset.x, dst->offset.y,
extent.width, extent.height);
image.offset.z++;
buffer.surf.addr.offset += buffer_layer_stride;
}
}
blorp_batch_finish(&batch);
}
void anv_CmdCopyBufferToImage(
VkCommandBuffer commandBuffer,
VkBuffer srcBuffer,
VkImage dstImage,
VkImageLayout dstImageLayout,
uint32_t regionCount,
const VkBufferImageCopy* pRegions)
{
ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
ANV_FROM_HANDLE(anv_buffer, src_buffer, srcBuffer);
ANV_FROM_HANDLE(anv_image, dst_image, dstImage);
copy_buffer_to_image(cmd_buffer, src_buffer, dst_image,
regionCount, pRegions, true);
}
void anv_CmdCopyImageToBuffer(
VkCommandBuffer commandBuffer,
VkImage srcImage,
VkImageLayout srcImageLayout,
VkBuffer dstBuffer,
uint32_t regionCount,
const VkBufferImageCopy* pRegions)
{
ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
ANV_FROM_HANDLE(anv_image, src_image, srcImage);
ANV_FROM_HANDLE(anv_buffer, dst_buffer, dstBuffer);
copy_buffer_to_image(cmd_buffer, dst_buffer, src_image,
regionCount, pRegions, false);
}
static bool
flip_coords(unsigned *src0, unsigned *src1, unsigned *dst0, unsigned *dst1)
{
bool flip = false;
if (*src0 > *src1) {
unsigned tmp = *src0;
*src0 = *src1;
*src1 = tmp;
flip = !flip;
}
if (*dst0 > *dst1) {
unsigned tmp = *dst0;
*dst0 = *dst1;
*dst1 = tmp;
flip = !flip;
}
return flip;
}
void anv_CmdBlitImage(
VkCommandBuffer commandBuffer,
VkImage srcImage,
VkImageLayout srcImageLayout,
VkImage dstImage,
VkImageLayout dstImageLayout,
uint32_t regionCount,
const VkImageBlit* pRegions,
VkFilter filter)
{
ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
ANV_FROM_HANDLE(anv_image, src_image, srcImage);
ANV_FROM_HANDLE(anv_image, dst_image, dstImage);
struct blorp_surf src, dst;
uint32_t gl_filter;
switch (filter) {
case VK_FILTER_NEAREST:
gl_filter = 0x2600; /* GL_NEAREST */
break;
case VK_FILTER_LINEAR:
gl_filter = 0x2601; /* GL_LINEAR */
break;
default:
unreachable("Invalid filter");
}
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer, 0);
for (unsigned r = 0; r < regionCount; r++) {
const VkImageSubresourceLayers *src_res = &pRegions[r].srcSubresource;
const VkImageSubresourceLayers *dst_res = &pRegions[r].dstSubresource;
get_blorp_surf_for_anv_image(src_image, src_res->aspectMask,
src_image->aux_usage, &src);
get_blorp_surf_for_anv_image(dst_image, dst_res->aspectMask,
dst_image->aux_usage, &dst);
struct anv_format src_format =
anv_get_format(&cmd_buffer->device->info, src_image->vk_format,
src_res->aspectMask, src_image->tiling);
struct anv_format dst_format =
anv_get_format(&cmd_buffer->device->info, dst_image->vk_format,
dst_res->aspectMask, dst_image->tiling);
unsigned dst_start, dst_end;
if (dst_image->type == VK_IMAGE_TYPE_3D) {
assert(dst_res->baseArrayLayer == 0);
dst_start = pRegions[r].dstOffsets[0].z;
dst_end = pRegions[r].dstOffsets[1].z;
} else {
dst_start = dst_res->baseArrayLayer;
dst_end = dst_start + dst_res->layerCount;
}
unsigned src_start, src_end;
if (src_image->type == VK_IMAGE_TYPE_3D) {
assert(src_res->baseArrayLayer == 0);
src_start = pRegions[r].srcOffsets[0].z;
src_end = pRegions[r].srcOffsets[1].z;
} else {
src_start = src_res->baseArrayLayer;
src_end = src_start + src_res->layerCount;
}
bool flip_z = flip_coords(&src_start, &src_end, &dst_start, &dst_end);
float src_z_step = (float)(src_end + 1 - src_start) /
(float)(dst_end + 1 - dst_start);
if (flip_z) {
src_start = src_end;
src_z_step *= -1;
}
unsigned src_x0 = pRegions[r].srcOffsets[0].x;
unsigned src_x1 = pRegions[r].srcOffsets[1].x;
unsigned dst_x0 = pRegions[r].dstOffsets[0].x;
unsigned dst_x1 = pRegions[r].dstOffsets[1].x;
bool flip_x = flip_coords(&src_x0, &src_x1, &dst_x0, &dst_x1);
unsigned src_y0 = pRegions[r].srcOffsets[0].y;
unsigned src_y1 = pRegions[r].srcOffsets[1].y;
unsigned dst_y0 = pRegions[r].dstOffsets[0].y;
unsigned dst_y1 = pRegions[r].dstOffsets[1].y;
bool flip_y = flip_coords(&src_y0, &src_y1, &dst_y0, &dst_y1);
const unsigned num_layers = dst_end - dst_start;
for (unsigned i = 0; i < num_layers; i++) {
unsigned dst_z = dst_start + i;
unsigned src_z = src_start + i * src_z_step;
blorp_blit(&batch, &src, src_res->mipLevel, src_z,
src_format.isl_format, src_format.swizzle,
&dst, dst_res->mipLevel, dst_z,
dst_format.isl_format, dst_format.swizzle,
src_x0, src_y0, src_x1, src_y1,
dst_x0, dst_y0, dst_x1, dst_y1,
gl_filter, flip_x, flip_y);
}
}
blorp_batch_finish(&batch);
}
static enum isl_format
isl_format_for_size(unsigned size_B)
{
switch (size_B) {
case 1: return ISL_FORMAT_R8_UINT;
case 2: return ISL_FORMAT_R8G8_UINT;
case 4: return ISL_FORMAT_R8G8B8A8_UINT;
case 8: return ISL_FORMAT_R16G16B16A16_UINT;
case 16: return ISL_FORMAT_R32G32B32A32_UINT;
default:
unreachable("Not a power-of-two format size");
}
}
static void
do_buffer_copy(struct blorp_batch *batch,
struct anv_bo *src, uint64_t src_offset,
struct anv_bo *dst, uint64_t dst_offset,
int width, int height, int block_size)
{
struct anv_device *device = batch->blorp->driver_ctx;
/* The actual format we pick doesn't matter as blorp will throw it away.
* The only thing that actually matters is the size.
*/
enum isl_format format = isl_format_for_size(block_size);
struct isl_surf surf;
isl_surf_init(&device->isl_dev, &surf,
.dim = ISL_SURF_DIM_2D,
.format = format,
.width = width,
.height = height,
.depth = 1,
.levels = 1,
.array_len = 1,
.samples = 1,
.usage = ISL_SURF_USAGE_TEXTURE_BIT |
ISL_SURF_USAGE_RENDER_TARGET_BIT,
.tiling_flags = ISL_TILING_LINEAR_BIT);
assert(surf.row_pitch == width * block_size);
struct blorp_surf src_blorp_surf = {
.surf = &surf,
.addr = {
.buffer = src,
.offset = src_offset,
},
};
struct blorp_surf dst_blorp_surf = {
.surf = &surf,
.addr = {
.buffer = dst,
.offset = dst_offset,
},
};
blorp_copy(batch, &src_blorp_surf, 0, 0, &dst_blorp_surf, 0, 0,
0, 0, 0, 0, width, height);
}
/**
* Returns the greatest common divisor of a and b that is a power of two.
*/
static inline uint64_t
gcd_pow2_u64(uint64_t a, uint64_t b)
{
assert(a > 0 || b > 0);
unsigned a_log2 = ffsll(a) - 1;
unsigned b_log2 = ffsll(b) - 1;
/* If either a or b is 0, then a_log2 or b_log2 till be UINT_MAX in which
* case, the MIN2() will take the other one. If both are 0 then we will
* hit the assert above.
*/
return 1 << MIN2(a_log2, b_log2);
}
/* This is maximum possible width/height our HW can handle */
#define MAX_SURFACE_DIM (1ull << 14)
void anv_CmdCopyBuffer(
VkCommandBuffer commandBuffer,
VkBuffer srcBuffer,
VkBuffer dstBuffer,
uint32_t regionCount,
const VkBufferCopy* pRegions)
{
ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
ANV_FROM_HANDLE(anv_buffer, src_buffer, srcBuffer);
ANV_FROM_HANDLE(anv_buffer, dst_buffer, dstBuffer);
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer, 0);
for (unsigned r = 0; r < regionCount; r++) {
uint64_t src_offset = src_buffer->offset + pRegions[r].srcOffset;
uint64_t dst_offset = dst_buffer->offset + pRegions[r].dstOffset;
uint64_t copy_size = pRegions[r].size;
/* First, we compute the biggest format that can be used with the
* given offsets and size.
*/
int bs = 16;
bs = gcd_pow2_u64(bs, src_offset);
bs = gcd_pow2_u64(bs, dst_offset);
bs = gcd_pow2_u64(bs, pRegions[r].size);
/* First, we make a bunch of max-sized copies */
uint64_t max_copy_size = MAX_SURFACE_DIM * MAX_SURFACE_DIM * bs;
while (copy_size >= max_copy_size) {
do_buffer_copy(&batch, src_buffer->bo, src_offset,
dst_buffer->bo, dst_offset,
MAX_SURFACE_DIM, MAX_SURFACE_DIM, bs);
copy_size -= max_copy_size;
src_offset += max_copy_size;
dst_offset += max_copy_size;
}
/* Now make a max-width copy */
uint64_t height = copy_size / (MAX_SURFACE_DIM * bs);
assert(height < MAX_SURFACE_DIM);
if (height != 0) {
uint64_t rect_copy_size = height * MAX_SURFACE_DIM * bs;
do_buffer_copy(&batch, src_buffer->bo, src_offset,
dst_buffer->bo, dst_offset,
MAX_SURFACE_DIM, height, bs);
copy_size -= rect_copy_size;
src_offset += rect_copy_size;
dst_offset += rect_copy_size;
}
/* Finally, make a small copy to finish it off */
if (copy_size != 0) {
do_buffer_copy(&batch, src_buffer->bo, src_offset,
dst_buffer->bo, dst_offset,
copy_size / bs, 1, bs);
}
}
blorp_batch_finish(&batch);
}
void anv_CmdUpdateBuffer(
VkCommandBuffer commandBuffer,
VkBuffer dstBuffer,
VkDeviceSize dstOffset,
VkDeviceSize dataSize,
const void* pData)
{
ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
ANV_FROM_HANDLE(anv_buffer, dst_buffer, dstBuffer);
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer, 0);
/* We can't quite grab a full block because the state stream needs a
* little data at the top to build its linked list.
*/
const uint32_t max_update_size =
cmd_buffer->device->dynamic_state_block_pool.block_size - 64;
assert(max_update_size < MAX_SURFACE_DIM * 4);
while (dataSize) {
const uint32_t copy_size = MIN2(dataSize, max_update_size);
struct anv_state tmp_data =
anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, copy_size, 64);
memcpy(tmp_data.map, pData, copy_size);
int bs = 16;
bs = gcd_pow2_u64(bs, dstOffset);
bs = gcd_pow2_u64(bs, copy_size);
do_buffer_copy(&batch,
&cmd_buffer->device->dynamic_state_block_pool.bo,
tmp_data.offset,
dst_buffer->bo, dst_buffer->offset + dstOffset,
copy_size / bs, 1, bs);
dataSize -= copy_size;
dstOffset += copy_size;
pData = (void *)pData + copy_size;
}
blorp_batch_finish(&batch);
}
void anv_CmdFillBuffer(
VkCommandBuffer commandBuffer,
VkBuffer dstBuffer,
VkDeviceSize dstOffset,
VkDeviceSize fillSize,
uint32_t data)
{
ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
ANV_FROM_HANDLE(anv_buffer, dst_buffer, dstBuffer);
struct blorp_surf surf;
struct isl_surf isl_surf;
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer, 0);
if (fillSize == VK_WHOLE_SIZE) {
fillSize = dst_buffer->size - dstOffset;
/* Make sure fillSize is a multiple of 4 */
fillSize &= ~3ull;
}
/* First, we compute the biggest format that can be used with the
* given offsets and size.
*/
int bs = 16;
bs = gcd_pow2_u64(bs, dstOffset);
bs = gcd_pow2_u64(bs, fillSize);
enum isl_format isl_format = isl_format_for_size(bs);
union isl_color_value color = {
.u32 = { data, data, data, data },
};
const uint64_t max_fill_size = MAX_SURFACE_DIM * MAX_SURFACE_DIM * bs;
while (fillSize >= max_fill_size) {
get_blorp_surf_for_anv_buffer(cmd_buffer->device,
dst_buffer, dstOffset,
MAX_SURFACE_DIM, MAX_SURFACE_DIM,
MAX_SURFACE_DIM * bs, isl_format,
&surf, &isl_surf);
blorp_clear(&batch, &surf, isl_format, ISL_SWIZZLE_IDENTITY,
0, 0, 1, 0, 0, MAX_SURFACE_DIM, MAX_SURFACE_DIM,
color, NULL);
fillSize -= max_fill_size;
dstOffset += max_fill_size;
}
uint64_t height = fillSize / (MAX_SURFACE_DIM * bs);
assert(height < MAX_SURFACE_DIM);
if (height != 0) {
const uint64_t rect_fill_size = height * MAX_SURFACE_DIM * bs;
get_blorp_surf_for_anv_buffer(cmd_buffer->device,
dst_buffer, dstOffset,
MAX_SURFACE_DIM, height,
MAX_SURFACE_DIM * bs, isl_format,
&surf, &isl_surf);
blorp_clear(&batch, &surf, isl_format, ISL_SWIZZLE_IDENTITY,
0, 0, 1, 0, 0, MAX_SURFACE_DIM, height,
color, NULL);
fillSize -= rect_fill_size;
dstOffset += rect_fill_size;
}
if (fillSize != 0) {
const uint32_t width = fillSize / bs;
get_blorp_surf_for_anv_buffer(cmd_buffer->device,
dst_buffer, dstOffset,
width, 1,
width * bs, isl_format,
&surf, &isl_surf);
blorp_clear(&batch, &surf, isl_format, ISL_SWIZZLE_IDENTITY,
0, 0, 1, 0, 0, width, 1,
color, NULL);
}
blorp_batch_finish(&batch);
}
void anv_CmdClearColorImage(
VkCommandBuffer commandBuffer,
VkImage _image,
VkImageLayout imageLayout,
const VkClearColorValue* pColor,
uint32_t rangeCount,
const VkImageSubresourceRange* pRanges)
{
ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
ANV_FROM_HANDLE(anv_image, image, _image);
static const bool color_write_disable[4] = { false, false, false, false };
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer, 0);
struct blorp_surf surf;
get_blorp_surf_for_anv_image(image, VK_IMAGE_ASPECT_COLOR_BIT,
image->aux_usage, &surf);
for (unsigned r = 0; r < rangeCount; r++) {
if (pRanges[r].aspectMask == 0)
continue;
assert(pRanges[r].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
struct anv_format src_format =
anv_get_format(&cmd_buffer->device->info, image->vk_format,
VK_IMAGE_ASPECT_COLOR_BIT, image->tiling);
unsigned base_layer = pRanges[r].baseArrayLayer;
unsigned layer_count = anv_get_layerCount(image, &pRanges[r]);
for (unsigned i = 0; i < anv_get_levelCount(image, &pRanges[r]); i++) {
const unsigned level = pRanges[r].baseMipLevel + i;
const unsigned level_width = anv_minify(image->extent.width, level);
const unsigned level_height = anv_minify(image->extent.height, level);
if (image->type == VK_IMAGE_TYPE_3D) {
base_layer = 0;
layer_count = anv_minify(image->extent.depth, level);
}
blorp_clear(&batch, &surf,
src_format.isl_format, src_format.swizzle,
level, base_layer, layer_count,
0, 0, level_width, level_height,
vk_to_isl_color(*pColor), color_write_disable);
}
}
blorp_batch_finish(&batch);
}
void anv_CmdClearDepthStencilImage(
VkCommandBuffer commandBuffer,
VkImage image_h,
VkImageLayout imageLayout,
const VkClearDepthStencilValue* pDepthStencil,
uint32_t rangeCount,
const VkImageSubresourceRange* pRanges)
{
ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
ANV_FROM_HANDLE(anv_image, image, image_h);
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer, 0);
struct blorp_surf depth, stencil;
if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
get_blorp_surf_for_anv_image(image, VK_IMAGE_ASPECT_DEPTH_BIT,
ISL_AUX_USAGE_NONE, &depth);
} else {
memset(&depth, 0, sizeof(depth));
}
if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
get_blorp_surf_for_anv_image(image, VK_IMAGE_ASPECT_STENCIL_BIT,
ISL_AUX_USAGE_NONE, &stencil);
} else {
memset(&stencil, 0, sizeof(stencil));
}
for (unsigned r = 0; r < rangeCount; r++) {
if (pRanges[r].aspectMask == 0)
continue;
bool clear_depth = pRanges[r].aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT;
bool clear_stencil = pRanges[r].aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT;
unsigned base_layer = pRanges[r].baseArrayLayer;
unsigned layer_count = anv_get_layerCount(image, &pRanges[r]);
for (unsigned i = 0; i < anv_get_levelCount(image, &pRanges[r]); i++) {
const unsigned level = pRanges[r].baseMipLevel + i;
const unsigned level_width = anv_minify(image->extent.width, level);
const unsigned level_height = anv_minify(image->extent.height, level);
if (image->type == VK_IMAGE_TYPE_3D)
layer_count = anv_minify(image->extent.depth, level);
blorp_clear_depth_stencil(&batch, &depth, &stencil,
level, base_layer, layer_count,
0, 0, level_width, level_height,
clear_depth, pDepthStencil->depth,
clear_stencil ? 0xff : 0,
pDepthStencil->stencil);
}
}
blorp_batch_finish(&batch);
}
struct anv_state
anv_cmd_buffer_alloc_blorp_binding_table(struct anv_cmd_buffer *cmd_buffer,
uint32_t num_entries,
uint32_t *state_offset)
{
struct anv_state bt_state =
anv_cmd_buffer_alloc_binding_table(cmd_buffer, num_entries,
state_offset);
if (bt_state.map == NULL) {
/* We ran out of space. Grab a new binding table block. */
MAYBE_UNUSED VkResult result =
anv_cmd_buffer_new_binding_table_block(cmd_buffer);
assert(result == VK_SUCCESS);
/* Re-emit state base addresses so we get the new surface state base
* address before we start emitting binding tables etc.
*/
anv_cmd_buffer_emit_state_base_address(cmd_buffer);
bt_state = anv_cmd_buffer_alloc_binding_table(cmd_buffer, num_entries,
state_offset);
assert(bt_state.map != NULL);
}
return bt_state;
}
static uint32_t
binding_table_for_surface_state(struct anv_cmd_buffer *cmd_buffer,
struct anv_state surface_state)
{
uint32_t state_offset;
struct anv_state bt_state =
anv_cmd_buffer_alloc_blorp_binding_table(cmd_buffer, 1, &state_offset);
uint32_t *bt_map = bt_state.map;
bt_map[0] = surface_state.offset + state_offset;
return bt_state.offset;
}
static void
clear_color_attachment(struct anv_cmd_buffer *cmd_buffer,
struct blorp_batch *batch,
const VkClearAttachment *attachment,
uint32_t rectCount, const VkClearRect *pRects)
{
const struct anv_subpass *subpass = cmd_buffer->state.subpass;
const uint32_t color_att = attachment->colorAttachment;
const uint32_t att_idx = subpass->color_attachments[color_att];
if (att_idx == VK_ATTACHMENT_UNUSED)
return;
struct anv_render_pass_attachment *pass_att =
&cmd_buffer->state.pass->attachments[att_idx];
struct anv_attachment_state *att_state =
&cmd_buffer->state.attachments[att_idx];
uint32_t binding_table =
binding_table_for_surface_state(cmd_buffer, att_state->color_rt_state);
union isl_color_value clear_color =
vk_to_isl_color(attachment->clearValue.color);
for (uint32_t r = 0; r < rectCount; ++r) {
const VkOffset2D offset = pRects[r].rect.offset;
const VkExtent2D extent = pRects[r].rect.extent;
blorp_clear_attachments(batch, binding_table,
ISL_FORMAT_UNSUPPORTED, pass_att->samples,
pRects[r].baseArrayLayer,
pRects[r].layerCount,
offset.x, offset.y,
offset.x + extent.width, offset.y + extent.height,
true, clear_color, false, 0.0f, 0, 0);
}
}
static void
clear_depth_stencil_attachment(struct anv_cmd_buffer *cmd_buffer,
struct blorp_batch *batch,
const VkClearAttachment *attachment,
uint32_t rectCount, const VkClearRect *pRects)
{
static const union isl_color_value color_value = { .u32 = { 0, } };
const struct anv_subpass *subpass = cmd_buffer->state.subpass;
const uint32_t att_idx = subpass->depth_stencil_attachment;
if (att_idx == VK_ATTACHMENT_UNUSED)
return;
struct anv_render_pass_attachment *pass_att =
&cmd_buffer->state.pass->attachments[att_idx];
bool clear_depth = attachment->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT;
bool clear_stencil = attachment->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT;
enum isl_format depth_format = ISL_FORMAT_UNSUPPORTED;
if (clear_depth) {
depth_format = anv_get_isl_format(&cmd_buffer->device->info,
pass_att->format,
VK_IMAGE_ASPECT_DEPTH_BIT,
VK_IMAGE_TILING_OPTIMAL);
}
uint32_t binding_table =
binding_table_for_surface_state(cmd_buffer,
cmd_buffer->state.null_surface_state);
for (uint32_t r = 0; r < rectCount; ++r) {
const VkOffset2D offset = pRects[r].rect.offset;
const VkExtent2D extent = pRects[r].rect.extent;
VkClearDepthStencilValue value = attachment->clearValue.depthStencil;
blorp_clear_attachments(batch, binding_table,
depth_format, pass_att->samples,
pRects[r].baseArrayLayer,
pRects[r].layerCount,
offset.x, offset.y,
offset.x + extent.width, offset.y + extent.height,
false, color_value,
clear_depth, value.depth,
clear_stencil ? 0xff : 0, value.stencil);
}
}
void anv_CmdClearAttachments(
VkCommandBuffer commandBuffer,
uint32_t attachmentCount,
const VkClearAttachment* pAttachments,
uint32_t rectCount,
const VkClearRect* pRects)
{
ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
/* Because this gets called within a render pass, we tell blorp not to
* trash our depth and stencil buffers.
*/
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer,
BLORP_BATCH_NO_EMIT_DEPTH_STENCIL);
for (uint32_t a = 0; a < attachmentCount; ++a) {
if (pAttachments[a].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
clear_color_attachment(cmd_buffer, &batch,
&pAttachments[a],
rectCount, pRects);
} else {
clear_depth_stencil_attachment(cmd_buffer, &batch,
&pAttachments[a],
rectCount, pRects);
}
}
blorp_batch_finish(&batch);
}
enum subpass_stage {
SUBPASS_STAGE_LOAD,
SUBPASS_STAGE_DRAW,
SUBPASS_STAGE_RESOLVE,
};
static bool
attachment_needs_flush(struct anv_cmd_buffer *cmd_buffer,
struct anv_render_pass_attachment *att,
enum subpass_stage stage)
{
struct anv_render_pass *pass = cmd_buffer->state.pass;
struct anv_subpass *subpass = cmd_buffer->state.subpass;
unsigned subpass_idx = subpass - pass->subpasses;
assert(subpass_idx < pass->subpass_count);
/* We handle this subpass specially based on the current stage */
enum anv_subpass_usage usage = att->subpass_usage[subpass_idx];
switch (stage) {
case SUBPASS_STAGE_LOAD:
if (usage & (ANV_SUBPASS_USAGE_INPUT | ANV_SUBPASS_USAGE_RESOLVE_SRC))
return true;
break;
case SUBPASS_STAGE_DRAW:
if (usage & ANV_SUBPASS_USAGE_RESOLVE_SRC)
return true;
break;
default:
break;
}
for (uint32_t s = subpass_idx + 1; s < pass->subpass_count; s++) {
usage = att->subpass_usage[s];
/* If this attachment is going to be used as an input in this or any
* future subpass, then we need to flush its cache and invalidate the
* texture cache.
*/
if (att->subpass_usage[s] & ANV_SUBPASS_USAGE_INPUT)
return true;
if (usage & (ANV_SUBPASS_USAGE_DRAW | ANV_SUBPASS_USAGE_RESOLVE_DST)) {
/* We found another subpass that draws to this attachment. We'll
* wait to resolve until then.
*/
return false;
}
}
return false;
}
static void
anv_cmd_buffer_flush_attachments(struct anv_cmd_buffer *cmd_buffer,
enum subpass_stage stage)
{
struct anv_subpass *subpass = cmd_buffer->state.subpass;
struct anv_render_pass *pass = cmd_buffer->state.pass;
for (uint32_t i = 0; i < subpass->color_count; ++i) {
uint32_t att = subpass->color_attachments[i];
assert(att < pass->attachment_count);
if (attachment_needs_flush(cmd_buffer, &pass->attachments[att], stage)) {
cmd_buffer->state.pending_pipe_bits |=
ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT |
ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
}
}
if (subpass->depth_stencil_attachment != VK_ATTACHMENT_UNUSED) {
uint32_t att = subpass->depth_stencil_attachment;
assert(att < pass->attachment_count);
if (attachment_needs_flush(cmd_buffer, &pass->attachments[att], stage)) {
cmd_buffer->state.pending_pipe_bits |=
ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT |
ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
}
}
}
static bool
subpass_needs_clear(const struct anv_cmd_buffer *cmd_buffer)
{
const struct anv_cmd_state *cmd_state = &cmd_buffer->state;
uint32_t ds = cmd_state->subpass->depth_stencil_attachment;
for (uint32_t i = 0; i < cmd_state->subpass->color_count; ++i) {
uint32_t a = cmd_state->subpass->color_attachments[i];
if (cmd_state->attachments[a].pending_clear_aspects) {
return true;
}
}
if (ds != VK_ATTACHMENT_UNUSED &&
cmd_state->attachments[ds].pending_clear_aspects) {
return true;
}
return false;
}
void
anv_cmd_buffer_clear_subpass(struct anv_cmd_buffer *cmd_buffer)
{
const struct anv_cmd_state *cmd_state = &cmd_buffer->state;
const VkRect2D render_area = cmd_buffer->state.render_area;
if (!subpass_needs_clear(cmd_buffer))
return;
/* Because this gets called within a render pass, we tell blorp not to
* trash our depth and stencil buffers.
*/
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer,
BLORP_BATCH_NO_EMIT_DEPTH_STENCIL);
VkClearRect clear_rect = {
.rect = cmd_buffer->state.render_area,
.baseArrayLayer = 0,
.layerCount = cmd_buffer->state.framebuffer->layers,
};
struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
for (uint32_t i = 0; i < cmd_state->subpass->color_count; ++i) {
const uint32_t a = cmd_state->subpass->color_attachments[i];
struct anv_attachment_state *att_state = &cmd_state->attachments[a];
if (!att_state->pending_clear_aspects)
continue;
assert(att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
struct anv_image_view *iview = fb->attachments[a];
const struct anv_image *image = iview->image;
struct blorp_surf surf;
get_blorp_surf_for_anv_image(image, VK_IMAGE_ASPECT_COLOR_BIT,
att_state->aux_usage, &surf);
if (att_state->fast_clear) {
surf.clear_color = vk_to_isl_color(att_state->clear_value.color);
/* From the Sky Lake PRM Vol. 7, "Render Target Fast Clear":
*
* "After Render target fast clear, pipe-control with color cache
* write-flush must be issued before sending any DRAW commands on
* that render target."
*
* This comment is a bit cryptic and doesn't really tell you what's
* going or what's really needed. It appears that fast clear ops are
* not properly synchronized with other drawing. This means that we
* cannot have a fast clear operation in the pipe at the same time as
* other regular drawing operations. We need to use a PIPE_CONTROL
* to ensure that the contents of the previous draw hit the render
* target before we resolve and then use a second PIPE_CONTROL after
* the resolve to ensure that it is completed before any additional
* drawing occurs.
*/
cmd_buffer->state.pending_pipe_bits |=
ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_CS_STALL_BIT;
blorp_fast_clear(&batch, &surf, iview->isl.format,
iview->isl.base_level,
iview->isl.base_array_layer, fb->layers,
render_area.offset.x, render_area.offset.y,
render_area.offset.x + render_area.extent.width,
render_area.offset.y + render_area.extent.height);
cmd_buffer->state.pending_pipe_bits |=
ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_CS_STALL_BIT;
} else {
blorp_clear(&batch, &surf, iview->isl.format, iview->isl.swizzle,
iview->isl.base_level,
iview->isl.base_array_layer, fb->layers,
render_area.offset.x, render_area.offset.y,
render_area.offset.x + render_area.extent.width,
render_area.offset.y + render_area.extent.height,
vk_to_isl_color(att_state->clear_value.color), NULL);
}
att_state->pending_clear_aspects = 0;
}
const uint32_t ds = cmd_state->subpass->depth_stencil_attachment;
if (ds != VK_ATTACHMENT_UNUSED &&
cmd_state->attachments[ds].pending_clear_aspects) {
VkClearAttachment clear_att = {
.aspectMask = cmd_state->attachments[ds].pending_clear_aspects,
.clearValue = cmd_state->attachments[ds].clear_value,
};
const uint8_t gen = cmd_buffer->device->info.gen;
bool clear_with_hiz = gen >= 8 && cmd_state->attachments[ds].aux_usage ==
ISL_AUX_USAGE_HIZ;
const struct anv_image_view *iview = fb->attachments[ds];
if (clear_with_hiz) {
const bool clear_depth = clear_att.aspectMask &
VK_IMAGE_ASPECT_DEPTH_BIT;
const bool clear_stencil = clear_att.aspectMask &
VK_IMAGE_ASPECT_STENCIL_BIT;
/* Check against restrictions for depth buffer clearing. A great GPU
* performance benefit isn't expected when using the HZ sequence for
* stencil-only clears. Therefore, we don't emit a HZ op sequence for
* a stencil clear in addition to using the BLORP-fallback for depth.
*/
if (clear_depth) {
if (!blorp_can_hiz_clear_depth(gen, iview->isl.format,
iview->image->samples,
render_area.offset.x,
render_area.offset.y,
render_area.offset.x +
render_area.extent.width,
render_area.offset.y +
render_area.extent.height)) {
clear_with_hiz = false;
} else if (clear_att.clearValue.depthStencil.depth !=
ANV_HZ_FC_VAL) {
/* Don't enable fast depth clears for any color not equal to
* ANV_HZ_FC_VAL.
*/
clear_with_hiz = false;
} else if (gen == 8 &&
anv_can_sample_with_hiz(cmd_buffer->device->info.gen,
iview->image->samples)) {
/* Only gen9+ supports returning ANV_HZ_FC_VAL when sampling a
* fast-cleared portion of a HiZ buffer. Testing has revealed
* that Gen8 only supports returning 0.0f. Gens prior to gen8 do
* not support this feature at all.
*/
clear_with_hiz = false;
}
}
if (clear_with_hiz) {
blorp_gen8_hiz_clear_attachments(&batch, iview->image->samples,
render_area.offset.x,
render_area.offset.y,
render_area.offset.x +
render_area.extent.width,
render_area.offset.y +
render_area.extent.height,
clear_depth, clear_stencil,
clear_att.clearValue.
depthStencil.stencil);
}
}
if (!clear_with_hiz) {
clear_depth_stencil_attachment(cmd_buffer, &batch,
&clear_att, 1, &clear_rect);
}
cmd_state->attachments[ds].pending_clear_aspects = 0;
}
blorp_batch_finish(&batch);
anv_cmd_buffer_flush_attachments(cmd_buffer, SUBPASS_STAGE_LOAD);
}
static void
resolve_image(struct blorp_batch *batch,
const struct anv_image *src_image,
uint32_t src_level, uint32_t src_layer,
const struct anv_image *dst_image,
uint32_t dst_level, uint32_t dst_layer,
VkImageAspectFlags aspect_mask,
uint32_t src_x, uint32_t src_y, uint32_t dst_x, uint32_t dst_y,
uint32_t width, uint32_t height)
{
assert(src_image->type == VK_IMAGE_TYPE_2D);
assert(src_image->samples > 1);
assert(dst_image->type == VK_IMAGE_TYPE_2D);
assert(dst_image->samples == 1);
uint32_t a;
for_each_bit(a, aspect_mask) {
VkImageAspectFlagBits aspect = 1 << a;
struct blorp_surf src_surf, dst_surf;
get_blorp_surf_for_anv_image(src_image, aspect,
src_image->aux_usage, &src_surf);
get_blorp_surf_for_anv_image(dst_image, aspect,
dst_image->aux_usage, &dst_surf);
blorp_blit(batch,
&src_surf, src_level, src_layer,
ISL_FORMAT_UNSUPPORTED, ISL_SWIZZLE_IDENTITY,
&dst_surf, dst_level, dst_layer,
ISL_FORMAT_UNSUPPORTED, ISL_SWIZZLE_IDENTITY,
src_x, src_y, src_x + width, src_y + height,
dst_x, dst_y, dst_x + width, dst_y + height,
0x2600 /* GL_NEAREST */, false, false);
}
}
void anv_CmdResolveImage(
VkCommandBuffer commandBuffer,
VkImage srcImage,
VkImageLayout srcImageLayout,
VkImage dstImage,
VkImageLayout dstImageLayout,
uint32_t regionCount,
const VkImageResolve* pRegions)
{
ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
ANV_FROM_HANDLE(anv_image, src_image, srcImage);
ANV_FROM_HANDLE(anv_image, dst_image, dstImage);
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer, 0);
for (uint32_t r = 0; r < regionCount; r++) {
assert(pRegions[r].srcSubresource.aspectMask ==
pRegions[r].dstSubresource.aspectMask);
assert(pRegions[r].srcSubresource.layerCount ==
pRegions[r].dstSubresource.layerCount);
const uint32_t layer_count = pRegions[r].dstSubresource.layerCount;
for (uint32_t layer = 0; layer < layer_count; layer++) {
resolve_image(&batch,
src_image, pRegions[r].srcSubresource.mipLevel,
pRegions[r].srcSubresource.baseArrayLayer + layer,
dst_image, pRegions[r].dstSubresource.mipLevel,
pRegions[r].dstSubresource.baseArrayLayer + layer,
pRegions[r].dstSubresource.aspectMask,
pRegions[r].srcOffset.x, pRegions[r].srcOffset.y,
pRegions[r].dstOffset.x, pRegions[r].dstOffset.y,
pRegions[r].extent.width, pRegions[r].extent.height);
}
}
blorp_batch_finish(&batch);
}
static void
ccs_resolve_attachment(struct anv_cmd_buffer *cmd_buffer,
struct blorp_batch *batch,
uint32_t att)
{
struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
struct anv_attachment_state *att_state =
&cmd_buffer->state.attachments[att];
if (att_state->aux_usage == ISL_AUX_USAGE_NONE)
return; /* Nothing to resolve */
assert(att_state->aux_usage == ISL_AUX_USAGE_CCS_E ||
att_state->aux_usage == ISL_AUX_USAGE_CCS_D);
struct anv_render_pass *pass = cmd_buffer->state.pass;
struct anv_subpass *subpass = cmd_buffer->state.subpass;
unsigned subpass_idx = subpass - pass->subpasses;
assert(subpass_idx < pass->subpass_count);
/* Scan forward to see what all ways this attachment will be used.
* Ideally, we would like to resolve in the same subpass as the last write
* of a particular attachment. That way we only resolve once but it's
* still hot in the cache.
*/
bool found_draw = false;
enum anv_subpass_usage usage = 0;
for (uint32_t s = subpass_idx + 1; s < pass->subpass_count; s++) {
usage |= pass->attachments[att].subpass_usage[s];
if (usage & (ANV_SUBPASS_USAGE_DRAW | ANV_SUBPASS_USAGE_RESOLVE_DST)) {
/* We found another subpass that draws to this attachment. We'll
* wait to resolve until then.
*/
found_draw = true;
break;
}
}
struct anv_image_view *iview = fb->attachments[att];
const struct anv_image *image = iview->image;
assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
enum blorp_fast_clear_op resolve_op = BLORP_FAST_CLEAR_OP_NONE;
if (!found_draw) {
/* This is the last subpass that writes to this attachment so we need to
* resolve here. Ideally, we would like to only resolve if the storeOp
* is set to VK_ATTACHMENT_STORE_OP_STORE. However, we need to ensure
* that the CCS bits are set to "resolved" because there may be copy or
* blit operations (which may ignore CCS) between now and the next time
* we render and we need to ensure that anything they write will be
* respected in the next render. Unfortunately, the hardware does not
* provide us with any sort of "invalidate" pass that sets the CCS to
* "resolved" without writing to the render target.
*/
if (iview->image->aux_usage != ISL_AUX_USAGE_CCS_E) {
/* The image destination surface doesn't support compression outside
* the render pass. We need a full resolve.
*/
resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
} else if (att_state->fast_clear) {
/* We don't know what to do with clear colors outside the render
* pass. We need a partial resolve. Only transparent black is
* built into the surface state object and thus no resolve is
* required for this case.
*/
if (att_state->clear_value.color.uint32[0] ||
att_state->clear_value.color.uint32[1] ||
att_state->clear_value.color.uint32[2] ||
att_state->clear_value.color.uint32[3])
resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL;
} else {
/* The image "natively" supports all the compression we care about
* and we don't need to resolve at all. If this is the case, we also
* don't need to resolve for any of the input attachment cases below.
*/
}
} else if (usage & ANV_SUBPASS_USAGE_INPUT) {
/* Input attachments are clear-color aware so, at least on Sky Lake, we
* can frequently sample from them with no resolves at all.
*/
if (att_state->aux_usage != att_state->input_aux_usage) {
assert(att_state->input_aux_usage == ISL_AUX_USAGE_NONE);
resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
} else if (!att_state->clear_color_is_zero_one) {
/* Sky Lake PRM, Vol. 2d, RENDER_SURFACE_STATE::Red Clear Color:
*
* "If Number of Multisamples is MULTISAMPLECOUNT_1 AND if this RT
* is fast cleared with non-0/1 clear value, this RT must be
* partially resolved (refer to Partial Resolve operation) before
* binding this surface to Sampler."
*/
resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL;
}
}
if (resolve_op == BLORP_FAST_CLEAR_OP_NONE)
return;
struct blorp_surf surf;
get_blorp_surf_for_anv_image(image, VK_IMAGE_ASPECT_COLOR_BIT,
att_state->aux_usage, &surf);
if (att_state->fast_clear)
surf.clear_color = vk_to_isl_color(att_state->clear_value.color);
/* From the Sky Lake PRM Vol. 7, "Render Target Resolve":
*
* "When performing a render target resolve, PIPE_CONTROL with end of
* pipe sync must be delivered."
*
* This comment is a bit cryptic and doesn't really tell you what's going
* or what's really needed. It appears that fast clear ops are not
* properly synchronized with other drawing. We need to use a PIPE_CONTROL
* to ensure that the contents of the previous draw hit the render target
* before we resolve and then use a second PIPE_CONTROL after the resolve
* to ensure that it is completed before any additional drawing occurs.
*/
cmd_buffer->state.pending_pipe_bits |=
ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_CS_STALL_BIT;
for (uint32_t layer = 0; layer < fb->layers; layer++) {
blorp_ccs_resolve(batch, &surf,
iview->isl.base_level,
iview->isl.base_array_layer + layer,
iview->isl.format, resolve_op);
}
cmd_buffer->state.pending_pipe_bits |=
ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_CS_STALL_BIT;
/* Once we've done any sort of resolve, we're no longer fast-cleared */
att_state->fast_clear = false;
if (att_state->aux_usage == ISL_AUX_USAGE_CCS_D)
att_state->aux_usage = ISL_AUX_USAGE_NONE;
}
void
anv_cmd_buffer_resolve_subpass(struct anv_cmd_buffer *cmd_buffer)
{
struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
struct anv_subpass *subpass = cmd_buffer->state.subpass;
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer, 0);
for (uint32_t i = 0; i < subpass->color_count; ++i) {
ccs_resolve_attachment(cmd_buffer, &batch,
subpass->color_attachments[i]);
}
anv_cmd_buffer_flush_attachments(cmd_buffer, SUBPASS_STAGE_DRAW);
if (subpass->has_resolve) {
for (uint32_t i = 0; i < subpass->color_count; ++i) {
uint32_t src_att = subpass->color_attachments[i];
uint32_t dst_att = subpass->resolve_attachments[i];
if (dst_att == VK_ATTACHMENT_UNUSED)
continue;
if (cmd_buffer->state.attachments[dst_att].pending_clear_aspects) {
/* From the Vulkan 1.0 spec:
*
* If the first use of an attachment in a render pass is as a
* resolve attachment, then the loadOp is effectively ignored
* as the resolve is guaranteed to overwrite all pixels in the
* render area.
*/
cmd_buffer->state.attachments[dst_att].pending_clear_aspects = 0;
}
struct anv_image_view *src_iview = fb->attachments[src_att];
struct anv_image_view *dst_iview = fb->attachments[dst_att];
const VkRect2D render_area = cmd_buffer->state.render_area;
assert(src_iview->aspect_mask == dst_iview->aspect_mask);
resolve_image(&batch, src_iview->image,
src_iview->isl.base_level,
src_iview->isl.base_array_layer,
dst_iview->image,
dst_iview->isl.base_level,
dst_iview->isl.base_array_layer,
src_iview->aspect_mask,
render_area.offset.x, render_area.offset.y,
render_area.offset.x, render_area.offset.y,
render_area.extent.width, render_area.extent.height);
ccs_resolve_attachment(cmd_buffer, &batch, dst_att);
}
anv_cmd_buffer_flush_attachments(cmd_buffer, SUBPASS_STAGE_RESOLVE);
}
blorp_batch_finish(&batch);
}
void
anv_gen8_hiz_op_resolve(struct anv_cmd_buffer *cmd_buffer,
const struct anv_image *image,
enum blorp_hiz_op op)
{
assert(image);
/* Don't resolve depth buffers without an auxiliary HiZ buffer and
* don't perform such a resolve on gens that don't support it.
*/
if (cmd_buffer->device->info.gen < 8 ||
image->aux_usage != ISL_AUX_USAGE_HIZ)
return;
assert(op == BLORP_HIZ_OP_HIZ_RESOLVE ||
op == BLORP_HIZ_OP_DEPTH_RESOLVE);
struct blorp_batch batch;
blorp_batch_init(&cmd_buffer->device->blorp, &batch, cmd_buffer, 0);
struct blorp_surf surf;
get_blorp_surf_for_anv_image(image, VK_IMAGE_ASPECT_DEPTH_BIT,
ISL_AUX_USAGE_NONE, &surf);
/* Manually add the aux HiZ surf */
surf.aux_surf = &image->aux_surface.isl,
surf.aux_addr = (struct blorp_address) {
.buffer = image->bo,
.offset = image->offset + image->aux_surface.offset,
};
surf.aux_usage = ISL_AUX_USAGE_HIZ;
surf.clear_color.u32[0] = (uint32_t) ANV_HZ_FC_VAL;
blorp_gen6_hiz_op(&batch, &surf, 0, 0, op);
blorp_batch_finish(&batch);
}
|