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
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrVkGpu.h"
#include "GrBackendSemaphore.h"
#include "GrBackendSurface.h"
#include "GrContextOptions.h"
#include "GrGeometryProcessor.h"
#include "GrGpuResourceCacheAccess.h"
#include "GrMesh.h"
#include "GrPipeline.h"
#include "GrRenderTargetPriv.h"
#include "GrTexturePriv.h"
#include "GrVkCommandBuffer.h"
#include "GrVkGpuCommandBuffer.h"
#include "GrVkImage.h"
#include "GrVkIndexBuffer.h"
#include "GrVkMemory.h"
#include "GrVkPipeline.h"
#include "GrVkPipelineState.h"
#include "GrVkRenderPass.h"
#include "GrVkResourceProvider.h"
#include "GrVkSemaphore.h"
#include "GrVkTexelBuffer.h"
#include "GrVkTexture.h"
#include "GrVkTextureRenderTarget.h"
#include "GrVkTransferBuffer.h"
#include "GrVkVertexBuffer.h"
#include "SkConvertPixels.h"
#include "SkMipMap.h"
#include "vk/GrVkInterface.h"
#include "vk/GrVkTypes.h"
#include "SkSLCompiler.h"
#if !defined(SK_BUILD_FOR_WIN)
#include <unistd.h>
#endif // !defined(SK_BUILD_FOR_WIN)
#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
#ifdef SK_ENABLE_VK_LAYERS
VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(
VkDebugReportFlagsEXT flags,
VkDebugReportObjectTypeEXT objectType,
uint64_t object,
size_t location,
int32_t messageCode,
const char* pLayerPrefix,
const char* pMessage,
void* pUserData) {
if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
SkDebugf("Vulkan error [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
return VK_TRUE; // skip further layers
} else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
SkDebugf("Vulkan warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
} else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
SkDebugf("Vulkan perf warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
} else {
SkDebugf("Vulkan info/debug [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
}
return VK_FALSE;
}
#endif
sk_sp<GrGpu> GrVkGpu::Make(GrBackendContext backendContext, const GrContextOptions& options,
GrContext* context) {
const auto* backend = reinterpret_cast<const GrVkBackendContext*>(backendContext);
return Make(sk_ref_sp(backend), options, context);
}
sk_sp<GrGpu> GrVkGpu::Make(sk_sp<const GrVkBackendContext> backendContext,
const GrContextOptions& options, GrContext* context) {
if (!backendContext) {
return nullptr;
}
if (!backendContext->fInterface->validate(backendContext->fExtensions)) {
return nullptr;
}
return sk_sp<GrGpu>(new GrVkGpu(context, options, std::move(backendContext)));
}
////////////////////////////////////////////////////////////////////////////////
GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
sk_sp<const GrVkBackendContext> backendCtx)
: INHERITED(context)
, fBackendContext(std::move(backendCtx))
, fDevice(fBackendContext->fDevice)
, fQueue(fBackendContext->fQueue)
, fResourceProvider(this)
, fDisconnected(false) {
#ifdef SK_ENABLE_VK_LAYERS
fCallback = VK_NULL_HANDLE;
if (fBackendContext->fExtensions & kEXT_debug_report_GrVkExtensionFlag) {
// Setup callback creation information
VkDebugReportCallbackCreateInfoEXT callbackCreateInfo;
callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
callbackCreateInfo.pNext = nullptr;
callbackCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT |
VK_DEBUG_REPORT_WARNING_BIT_EXT |
//VK_DEBUG_REPORT_INFORMATION_BIT_EXT |
//VK_DEBUG_REPORT_DEBUG_BIT_EXT |
VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
callbackCreateInfo.pfnCallback = &DebugReportCallback;
callbackCreateInfo.pUserData = nullptr;
// Register the callback
GR_VK_CALL_ERRCHECK(this->vkInterface(),
CreateDebugReportCallbackEXT(fBackendContext->fInstance,
&callbackCreateInfo, nullptr, &fCallback));
}
#endif
fCompiler = new SkSL::Compiler();
fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), fBackendContext->fPhysicalDevice,
fBackendContext->fFeatures, fBackendContext->fExtensions));
fCaps.reset(SkRef(fVkCaps.get()));
VK_CALL(GetPhysicalDeviceMemoryProperties(fBackendContext->fPhysicalDevice, &fPhysDevMemProps));
const VkCommandPoolCreateInfo cmdPoolInfo = {
VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType
nullptr, // pNext
VK_COMMAND_POOL_CREATE_TRANSIENT_BIT |
VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, // CmdPoolCreateFlags
fBackendContext->fGraphicsQueueIndex, // queueFamilyIndex
};
GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr,
&fCmdPool));
// must call this after creating the CommandPool
fResourceProvider.init();
fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
SkASSERT(fCurrentCmdBuffer);
fCurrentCmdBuffer->begin(this);
// set up our heaps
fHeaps[kLinearImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024));
fHeaps[kOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 64*1024*1024));
fHeaps[kSmallOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 2*1024*1024));
fHeaps[kVertexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
fHeaps[kIndexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
fHeaps[kUniformBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 256*1024));
fHeaps[kTexelBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
fHeaps[kCopyReadBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
fHeaps[kCopyWriteBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024));
}
void GrVkGpu::destroyResources() {
if (fCurrentCmdBuffer) {
fCurrentCmdBuffer->end(this);
fCurrentCmdBuffer->unref(this);
}
// wait for all commands to finish
fResourceProvider.checkCommandBuffers();
VkResult res = VK_CALL(QueueWaitIdle(fQueue));
// On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
// on the command buffers even though they have completed. This causes an assert to fire when
// destroying the command buffers. Currently this ony seems to happen on windows, so we add a
// sleep to make sure the fence signals.
#ifdef SK_DEBUG
if (this->vkCaps().mustSleepOnTearDown()) {
#if defined(SK_BUILD_FOR_WIN)
Sleep(10); // In milliseconds
#else
sleep(1); // In seconds
#endif
}
#endif
#ifdef SK_DEBUG
SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
#endif
for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
fSemaphoresToWaitOn[i]->unref(this);
}
fSemaphoresToWaitOn.reset();
for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
fSemaphoresToSignal[i]->unref(this);
}
fSemaphoresToSignal.reset();
fCopyManager.destroyResources(this);
// must call this just before we destroy the command pool and VkDevice
fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
if (fCmdPool != VK_NULL_HANDLE) {
VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr));
}
#ifdef SK_ENABLE_VK_LAYERS
if (fCallback) {
VK_CALL(DestroyDebugReportCallbackEXT(fBackendContext->fInstance, fCallback, nullptr));
}
#endif
}
GrVkGpu::~GrVkGpu() {
if (!fDisconnected) {
this->destroyResources();
}
delete fCompiler;
}
void GrVkGpu::disconnect(DisconnectType type) {
INHERITED::disconnect(type);
if (!fDisconnected) {
if (DisconnectType::kCleanup == type) {
this->destroyResources();
} else {
fCurrentCmdBuffer->unrefAndAbandon();
for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
fSemaphoresToWaitOn[i]->unrefAndAbandon();
}
for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
fSemaphoresToSignal[i]->unrefAndAbandon();
}
fCopyManager.abandonResources();
// must call this just before we destroy the command pool and VkDevice
fResourceProvider.abandonResources();
}
fSemaphoresToWaitOn.reset();
fSemaphoresToSignal.reset();
#ifdef SK_ENABLE_VK_LAYERS
fCallback = VK_NULL_HANDLE;
#endif
fCurrentCmdBuffer = nullptr;
fCmdPool = VK_NULL_HANDLE;
fDisconnected = true;
}
}
///////////////////////////////////////////////////////////////////////////////
GrGpuRTCommandBuffer* GrVkGpu::createCommandBuffer(
GrRenderTarget* rt, GrSurfaceOrigin origin,
const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
return new GrVkGpuRTCommandBuffer(this, rt, origin, colorInfo, stencilInfo);
}
GrGpuTextureCommandBuffer* GrVkGpu::createCommandBuffer(GrTexture* texture,
GrSurfaceOrigin origin) {
return new GrVkGpuTextureCommandBuffer(this, texture, origin);
}
void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
SkASSERT(fCurrentCmdBuffer);
fCurrentCmdBuffer->end(this);
fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
fSemaphoresToWaitOn[i]->unref(this);
}
fSemaphoresToWaitOn.reset();
for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
fSemaphoresToSignal[i]->unref(this);
}
fSemaphoresToSignal.reset();
fResourceProvider.checkCommandBuffers();
// Release old command buffer and create a new one
fCurrentCmdBuffer->unref(this);
fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
SkASSERT(fCurrentCmdBuffer);
fCurrentCmdBuffer->begin(this);
}
///////////////////////////////////////////////////////////////////////////////
GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern,
const void* data) {
GrBuffer* buff;
switch (type) {
case kVertex_GrBufferType:
SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
kStatic_GrAccessPattern == accessPattern);
buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
break;
case kIndex_GrBufferType:
SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
kStatic_GrAccessPattern == accessPattern);
buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
break;
case kXferCpuToGpu_GrBufferType:
SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
kStream_GrAccessPattern == accessPattern);
buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type);
break;
case kXferGpuToCpu_GrBufferType:
SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
kStream_GrAccessPattern == accessPattern);
buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type);
break;
case kTexel_GrBufferType:
SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
kStatic_GrAccessPattern == accessPattern);
buff = GrVkTexelBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
break;
case kDrawIndirect_GrBufferType:
SK_ABORT("DrawIndirect Buffers not supported in vulkan backend.");
return nullptr;
default:
SK_ABORT("Unknown buffer type.");
return nullptr;
}
if (data && buff) {
buff->updateData(data, size);
}
return buff;
}
////////////////////////////////////////////////////////////////////////////////
bool GrVkGpu::onGetWritePixelsInfo(GrSurface* dstSurface, GrSurfaceOrigin dstOrigin,
int width, int height,
GrPixelConfig srcConfig, DrawPreference* drawPreference,
WritePixelTempDrawInfo* tempDrawInfo) {
GrRenderTarget* renderTarget = dstSurface->asRenderTarget();
// Start off assuming no swizzling
tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
tempDrawInfo->fWriteConfig = srcConfig;
// These settings we will always want if a temp draw is performed. Initially set the config
// to srcConfig, though that may be modified if we decide to do a R/B swap
tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
tempDrawInfo->fTempSurfaceDesc.fWidth = width;
tempDrawInfo->fTempSurfaceDesc.fHeight = height;
tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 1;
tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
if (dstSurface->config() == srcConfig) {
// We only support writing pixels to textures. Forcing a draw lets us write to pure RTs.
if (!dstSurface->asTexture()) {
ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
}
// If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
if (renderTarget && renderTarget->numColorSamples() > 1) {
ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
}
return true;
}
// Any config change requires a draw
ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
if (!this->vkCaps().isConfigTexturable(srcConfig) && configsAreRBSwaps) {
tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
tempDrawInfo->fWriteConfig = dstSurface->config();
}
return true;
}
bool GrVkGpu::onWritePixels(GrSurface* surface, GrSurfaceOrigin origin,
int left, int top, int width, int height,
GrPixelConfig config,
const GrMipLevel texels[], int mipLevelCount) {
GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
if (!vkTex) {
return false;
}
// Make sure we have at least the base level
if (!mipLevelCount || !texels[0].fPixels) {
return false;
}
// We assume Vulkan doesn't do sRGB <-> linear conversions when reading and writing pixels.
if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
return false;
}
bool success = false;
bool linearTiling = vkTex->isLinearTiled();
if (linearTiling) {
if (mipLevelCount > 1) {
SkDebugf("Can't upload mipmap data to linear tiled texture");
return false;
}
if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
// Need to change the layout to general in order to perform a host write
vkTex->setImageLayout(this,
VK_IMAGE_LAYOUT_GENERAL,
VK_ACCESS_HOST_WRITE_BIT,
VK_PIPELINE_STAGE_HOST_BIT,
false);
this->submitCommandBuffer(kForce_SyncQueue);
}
success = this->uploadTexDataLinear(vkTex, origin, left, top, width, height, config,
texels[0].fPixels, texels[0].fRowBytes);
} else {
int currentMipLevels = vkTex->texturePriv().maxMipMapLevel() + 1;
if (mipLevelCount > currentMipLevels) {
if (!vkTex->reallocForMipmap(this, mipLevelCount)) {
return false;
}
}
success = this->uploadTexDataOptimal(vkTex, origin, left, top, width, height, config,
texels, mipLevelCount);
}
return success;
}
bool GrVkGpu::onTransferPixels(GrTexture* texture,
int left, int top, int width, int height,
GrPixelConfig config, GrBuffer* transferBuffer,
size_t bufferOffset, size_t rowBytes) {
// Vulkan only supports 4-byte aligned offsets
if (SkToBool(bufferOffset & 0x2)) {
return false;
}
GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
if (!vkTex) {
return false;
}
GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
if (!vkBuffer) {
return false;
}
// We assume Vulkan doesn't do sRGB <-> linear conversions when reading and writing pixels.
if (GrPixelConfigIsSRGB(texture->config()) != GrPixelConfigIsSRGB(config)) {
return false;
}
SkDEBUGCODE(
SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
SkASSERT(bounds.contains(subRect));
)
size_t bpp = GrBytesPerPixel(config);
if (rowBytes == 0) {
rowBytes = bpp*width;
}
// Set up copy region
VkBufferImageCopy region;
memset(®ion, 0, sizeof(VkBufferImageCopy));
region.bufferOffset = bufferOffset;
region.bufferRowLength = (uint32_t)(rowBytes/bpp);
region.bufferImageHeight = 0;
region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
region.imageOffset = { left, top, 0 };
region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
// Change layout of our target so it can be copied to
vkTex->setImageLayout(this,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
false);
// Copy the buffer to the image
fCurrentCmdBuffer->copyBufferToImage(this,
vkBuffer,
vkTex,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1,
®ion);
vkTex->texturePriv().markMipMapsDirty();
return true;
}
void GrVkGpu::resolveImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
GrVkRenderTarget* src, GrSurfaceOrigin srcOrigin,
const SkIRect& srcRect, const SkIPoint& dstPoint) {
SkASSERT(dst);
SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
}
// Flip rect if necessary
SkIRect srcVkRect = srcRect;
int32_t dstY = dstPoint.fY;
if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
srcVkRect.fTop = src->height() - srcRect.fBottom;
srcVkRect.fBottom = src->height() - srcRect.fTop;
dstY = dst->height() - dstPoint.fY - srcVkRect.height();
}
VkImageResolve resolveInfo;
resolveInfo.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
resolveInfo.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
resolveInfo.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
resolveInfo.dstOffset = { dstPoint.fX, dstY, 0 };
resolveInfo.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
GrVkImage* dstImage;
GrRenderTarget* dstRT = dst->asRenderTarget();
if (dstRT) {
GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
dstImage = vkRT;
} else {
SkASSERT(dst->asTexture());
dstImage = static_cast<GrVkTexture*>(dst->asTexture());
}
dstImage->setImageLayout(this,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
false);
src->msaaImage()->setImageLayout(this,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_ACCESS_TRANSFER_READ_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
false);
fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
}
void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, GrSurfaceOrigin origin,
bool requiresSubmit) {
if (target->needsResolve()) {
SkASSERT(target->numColorSamples() > 1);
GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
SkASSERT(rt->msaaImage());
const SkIRect& srcRect = rt->getResolveRect();
this->resolveImage(target, origin, rt, origin, srcRect,
SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
rt->flagAsResolved();
if (requiresSubmit) {
this->submitCommandBuffer(kSkip_SyncQueue);
}
}
}
bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, GrSurfaceOrigin texOrigin,
int left, int top, int width, int height,
GrPixelConfig dataConfig,
const void* data,
size_t rowBytes) {
SkASSERT(data);
SkASSERT(tex->isLinearTiled());
SkDEBUGCODE(
SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
SkASSERT(bounds.contains(subRect));
)
size_t bpp = GrBytesPerPixel(dataConfig);
size_t trimRowBytes = width * bpp;
if (!rowBytes) {
rowBytes = trimRowBytes;
}
SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
const VkImageSubresource subres = {
VK_IMAGE_ASPECT_COLOR_BIT,
0, // mipLevel
0, // arraySlice
};
VkSubresourceLayout layout;
VkResult err;
const GrVkInterface* interface = this->vkInterface();
GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
tex->image(),
&subres,
&layout));
int texTop = kBottomLeft_GrSurfaceOrigin == texOrigin ? tex->height() - top - height : top;
const GrVkAlloc& alloc = tex->alloc();
VkDeviceSize offset = alloc.fOffset + texTop*layout.rowPitch + left*bpp;
VkDeviceSize size = height*layout.rowPitch;
void* mapPtr;
err = GR_VK_CALL(interface, MapMemory(fDevice, alloc.fMemory, offset, size, 0, &mapPtr));
if (err) {
return false;
}
if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
// copy into buffer by rows
const char* srcRow = reinterpret_cast<const char*>(data);
char* dstRow = reinterpret_cast<char*>(mapPtr)+(height - 1)*layout.rowPitch;
for (int y = 0; y < height; y++) {
memcpy(dstRow, srcRow, trimRowBytes);
srcRow += rowBytes;
dstRow -= layout.rowPitch;
}
} else {
SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
height);
}
GrVkMemory::FlushMappedAlloc(this, alloc);
GR_VK_CALL(interface, UnmapMemory(fDevice, alloc.fMemory));
return true;
}
bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, GrSurfaceOrigin texOrigin,
int left, int top, int width, int height,
GrPixelConfig dataConfig,
const GrMipLevel texels[], int mipLevelCount) {
SkASSERT(!tex->isLinearTiled());
// The assumption is either that we have no mipmaps, or that our rect is the entire texture
SkASSERT(1 == mipLevelCount ||
(0 == left && 0 == top && width == tex->width() && height == tex->height()));
// We assume that if the texture has mip levels, we either upload to all the levels or just the
// first.
SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
if (width == 0 || height == 0) {
return false;
}
SkASSERT(this->caps()->isConfigTexturable(tex->config()));
size_t bpp = GrBytesPerPixel(dataConfig);
// texels is const.
// But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
// Because of this we need to make a non-const shallow copy of texels.
SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
if (mipLevelCount) {
texelsShallowCopy.reset(mipLevelCount);
memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
}
// Determine whether we need to flip when we copy into the buffer
bool flipY = (kBottomLeft_GrSurfaceOrigin == texOrigin && mipLevelCount);
SkTArray<size_t> individualMipOffsets(mipLevelCount);
individualMipOffsets.push_back(0);
size_t combinedBufferSize = width * bpp * height;
int currentWidth = width;
int currentHeight = height;
if (mipLevelCount > 0 && !texelsShallowCopy[0].fPixels) {
combinedBufferSize = 0;
}
// The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
// config. This works with the assumption that the bytes in pixel config is always a power of 2.
SkASSERT((bpp & (bpp - 1)) == 0);
const size_t alignmentMask = 0x3 | (bpp - 1);
for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
currentWidth = SkTMax(1, currentWidth/2);
currentHeight = SkTMax(1, currentHeight/2);
if (texelsShallowCopy[currentMipLevel].fPixels) {
const size_t trimmedSize = currentWidth * bpp * currentHeight;
const size_t alignmentDiff = combinedBufferSize & alignmentMask;
if (alignmentDiff != 0) {
combinedBufferSize += alignmentMask - alignmentDiff + 1;
}
individualMipOffsets.push_back(combinedBufferSize);
combinedBufferSize += trimmedSize;
} else {
individualMipOffsets.push_back(0);
}
}
if (0 == combinedBufferSize) {
// We don't actually have any data to upload so just return success
return true;
}
// allocate buffer to hold our mip data
GrVkTransferBuffer* transferBuffer =
GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
if(!transferBuffer) {
return false;
}
char* buffer = (char*) transferBuffer->map();
SkTArray<VkBufferImageCopy> regions(mipLevelCount);
currentWidth = width;
currentHeight = height;
int layerHeight = tex->height();
for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
if (texelsShallowCopy[currentMipLevel].fPixels) {
SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
const size_t trimRowBytes = currentWidth * bpp;
const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
? texelsShallowCopy[currentMipLevel].fRowBytes
: trimRowBytes;
// copy data into the buffer, skipping the trailing bytes
char* dst = buffer + individualMipOffsets[currentMipLevel];
const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
if (flipY) {
src += (currentHeight - 1) * rowBytes;
for (int y = 0; y < currentHeight; y++) {
memcpy(dst, src, trimRowBytes);
src -= rowBytes;
dst += trimRowBytes;
}
} else {
SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
}
VkBufferImageCopy& region = regions.push_back();
memset(®ion, 0, sizeof(VkBufferImageCopy));
region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
region.bufferRowLength = currentWidth;
region.bufferImageHeight = currentHeight;
region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
region.imageOffset = { left, flipY ? layerHeight - top - currentHeight : top, 0 };
region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
}
currentWidth = SkTMax(1, currentWidth/2);
currentHeight = SkTMax(1, currentHeight/2);
layerHeight = currentHeight;
}
// no need to flush non-coherent memory, unmap will do that for us
transferBuffer->unmap();
// Change layout of our target so it can be copied to
tex->setImageLayout(this,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
false);
// Copy the buffer to the image
fCurrentCmdBuffer->copyBufferToImage(this,
transferBuffer,
tex,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
regions.count(),
regions.begin());
transferBuffer->unref();
if (1 == mipLevelCount) {
tex->texturePriv().markMipMapsDirty();
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
const GrMipLevel texels[], int mipLevelCount) {
bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
VkFormat pixelFormat;
SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
if (renderTarget) {
usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
}
// For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
// VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
// will be using this texture in some copy or not. Also this assumes, as is the current case,
// that all render targets in vulkan are also textures. If we change this practice of setting
// both bits, we must make sure to set the destination bit if we are uploading srcData to the
// texture.
usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
// This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
// requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
// to 1.
int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
GrVkImage::ImageDesc imageDesc;
imageDesc.fImageType = VK_IMAGE_TYPE_2D;
imageDesc.fFormat = pixelFormat;
imageDesc.fWidth = desc.fWidth;
imageDesc.fHeight = desc.fHeight;
imageDesc.fLevels = mipLevels;
imageDesc.fSamples = 1;
imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
imageDesc.fUsageFlags = usageFlags;
imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
if (mipLevels > 1) {
mipMapsStatus = GrMipMapsStatus::kValid;
for (int i = 0; i < mipLevels; ++i) {
if (!texels[i].fPixels) {
mipMapsStatus = GrMipMapsStatus::kDirty;
break;
}
}
}
sk_sp<GrVkTexture> tex;
if (renderTarget) {
tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, desc,
imageDesc,
mipMapsStatus);
} else {
tex = GrVkTexture::CreateNewTexture(this, budgeted, desc, imageDesc,
mipMapsStatus);
}
if (!tex) {
return nullptr;
}
if (mipLevelCount) {
if (!this->uploadTexDataOptimal(tex.get(), desc.fOrigin, 0, 0, desc.fWidth, desc.fHeight,
desc.fConfig, texels, mipLevelCount)) {
tex->unref();
return nullptr;
}
}
if (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) {
VkClearColorValue zeroClearColor;
memset(&zeroClearColor, 0, sizeof(zeroClearColor));
VkImageSubresourceRange range;
range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
range.baseArrayLayer = 0;
range.baseMipLevel = 0;
range.layerCount = 1;
range.levelCount = 1;
tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
}
return tex;
}
////////////////////////////////////////////////////////////////////////////////
void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
VkDeviceSize dstOffset, VkDeviceSize size) {
VkBufferCopy copyRegion;
copyRegion.srcOffset = srcOffset;
copyRegion.dstOffset = dstOffset;
copyRegion.size = size;
fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, ©Region);
}
bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
VkDeviceSize offset, VkDeviceSize size) {
// Update the buffer
fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
return true;
}
////////////////////////////////////////////////////////////////////////////////
static bool check_backend_texture(const GrBackendTexture& backendTex,
GrPixelConfig config) {
const GrVkImageInfo* info = backendTex.getVkImageInfo();
if (!info) {
return false;
}
if (VK_NULL_HANDLE == info->fImage || VK_NULL_HANDLE == info->fAlloc.fMemory) {
return false;
}
SkASSERT(GrVkFormatPixelConfigPairIsValid(info->fFormat, config));
return true;
}
sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
GrWrapOwnership ownership) {
if (!check_backend_texture(backendTex, backendTex.config())) {
return nullptr;
}
GrSurfaceDesc surfDesc;
surfDesc.fFlags = kNone_GrSurfaceFlags;
surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // Not actually used in the following
surfDesc.fWidth = backendTex.width();
surfDesc.fHeight = backendTex.height();
surfDesc.fConfig = backendTex.config();
surfDesc.fSampleCnt = 1;
return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, backendTex.getVkImageInfo());
}
sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
int sampleCnt,
GrWrapOwnership ownership) {
if (!check_backend_texture(backendTex, backendTex.config())) {
return nullptr;
}
GrSurfaceDesc surfDesc;
surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; // Not actually used in the following
surfDesc.fWidth = backendTex.width();
surfDesc.fHeight = backendTex.height();
surfDesc.fConfig = backendTex.config();
surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(this, surfDesc, ownership,
backendTex.getVkImageInfo());
}
sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
// Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
// general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
// you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
// creating and owning the MSAA images.
if (backendRT.sampleCnt() > 1) {
return nullptr;
}
const GrVkImageInfo* info = backendRT.getVkImageInfo();
if (!info) {
return nullptr;
}
if (VK_NULL_HANDLE == info->fImage) {
return nullptr;
}
GrSurfaceDesc desc;
desc.fFlags = kRenderTarget_GrSurfaceFlag;
desc.fOrigin = kBottomLeft_GrSurfaceOrigin; // Not actually used in the following
desc.fWidth = backendRT.width();
desc.fHeight = backendRT.height();
desc.fConfig = backendRT.config();
desc.fSampleCnt = 1;
sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info);
if (tgt && backendRT.stencilBits()) {
if (!createStencilAttachmentForRenderTarget(tgt.get(), desc.fWidth, desc.fHeight)) {
return nullptr;
}
}
return tgt;
}
sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
int sampleCnt) {
const GrVkImageInfo* info = tex.getVkImageInfo();
if (!info) {
return nullptr;
}
if (VK_NULL_HANDLE == info->fImage) {
return nullptr;
}
GrSurfaceDesc desc;
desc.fFlags = kRenderTarget_GrSurfaceFlag;
desc.fOrigin = kBottomLeft_GrSurfaceOrigin; // Not actually used in the following
desc.fWidth = tex.width();
desc.fHeight = tex.height();
desc.fConfig = tex.config();
desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
if (!desc.fSampleCnt) {
return nullptr;
}
sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info);
return tgt;
}
void GrVkGpu::generateMipmap(GrVkTexture* tex, GrSurfaceOrigin texOrigin) {
// don't do anything for linearly tiled textures (can't have mipmaps)
if (tex->isLinearTiled()) {
SkDebugf("Trying to create mipmap for linear tiled texture");
return;
}
// determine if we can blit to and from this format
const GrVkCaps& caps = this->vkCaps();
if (!caps.configCanBeDstofBlit(tex->config(), false) ||
!caps.configCanBeSrcofBlit(tex->config(), false) ||
!caps.mipMapSupport()) {
return;
}
if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
this->submitCommandBuffer(kSkip_SyncQueue);
}
// We may need to resolve the texture first if it is also a render target
GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(tex->asRenderTarget());
if (texRT) {
this->internalResolveRenderTarget(texRT, texOrigin, false);
}
int width = tex->width();
int height = tex->height();
VkImageBlit blitRegion;
memset(&blitRegion, 0, sizeof(VkImageBlit));
// SkMipMap doesn't include the base level in the level count so we have to add 1
uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
if (levelCount != tex->mipLevels()) {
const GrVkResource* oldResource = tex->resource();
oldResource->ref();
// grab handle to the original image resource
VkImage oldImage = tex->image();
// change the original image's layout so we can copy from it
tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
if (!tex->reallocForMipmap(this, levelCount)) {
oldResource->unref(this);
return;
}
// change the new image's layout so we can blit to it
tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL,
VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
// Blit original image to top level of new image
blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
blitRegion.srcOffsets[0] = { 0, 0, 0 };
blitRegion.srcOffsets[1] = { width, height, 1 };
blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
blitRegion.dstOffsets[0] = { 0, 0, 0 };
blitRegion.dstOffsets[1] = { width, height, 1 };
fCurrentCmdBuffer->blitImage(this,
oldResource,
oldImage,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
tex->resource(),
tex->image(),
VK_IMAGE_LAYOUT_GENERAL,
1,
&blitRegion,
VK_FILTER_LINEAR);
oldResource->unref(this);
} else {
// change layout of the layers so we can write to them.
tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL,
VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
}
// setup memory barrier
SkASSERT(GrVkFormatIsSupported(tex->imageFormat()));
VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
VkImageMemoryBarrier imageMemoryBarrier = {
VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
nullptr, // pNext
VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
VK_IMAGE_LAYOUT_GENERAL, // oldLayout
VK_IMAGE_LAYOUT_GENERAL, // newLayout
VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
tex->image(), // image
{ aspectFlags, 0, 1, 0, 1 } // subresourceRange
};
// Blit the miplevels
uint32_t mipLevel = 1;
while (mipLevel < levelCount) {
int prevWidth = width;
int prevHeight = height;
width = SkTMax(1, width / 2);
height = SkTMax(1, height / 2);
imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
false, &imageMemoryBarrier);
blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
blitRegion.srcOffsets[0] = { 0, 0, 0 };
blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
blitRegion.dstOffsets[0] = { 0, 0, 0 };
blitRegion.dstOffsets[1] = { width, height, 1 };
fCurrentCmdBuffer->blitImage(this,
*tex,
*tex,
1,
&blitRegion,
VK_FILTER_LINEAR);
++mipLevel;
}
}
////////////////////////////////////////////////////////////////////////////////
GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
int width,
int height) {
SkASSERT(width >= rt->width());
SkASSERT(height >= rt->height());
int samples = rt->numStencilSamples();
const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat();
GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
width,
height,
samples,
sFmt));
fStats.incStencilAttachmentCreates();
return stencil;
}
////////////////////////////////////////////////////////////////////////////////
bool copy_testing_data(GrVkGpu* gpu, void* srcData, const GrVkAlloc& alloc, size_t bufferOffset,
size_t srcRowBytes, size_t dstRowBytes, int h) {
void* mapPtr;
VkResult err = GR_VK_CALL(gpu->vkInterface(), MapMemory(gpu->device(),
alloc.fMemory,
alloc.fOffset + bufferOffset,
dstRowBytes * h,
0,
&mapPtr));
if (err) {
return false;
}
if (srcData) {
// If there is no padding on dst we can do a single memcopy.
// This assumes the srcData comes in with no padding.
SkRectMemcpy(mapPtr, static_cast<size_t>(dstRowBytes),
srcData, srcRowBytes, srcRowBytes, h);
} else {
// If there is no srcdata we always copy 0's into the textures so that it is initialized
// with some data.
if (srcRowBytes == static_cast<size_t>(dstRowBytes)) {
memset(mapPtr, 0, srcRowBytes * h);
} else {
for (int i = 0; i < h; ++i) {
memset(mapPtr, 0, srcRowBytes);
mapPtr = SkTAddOffset<void>(mapPtr, static_cast<size_t>(dstRowBytes));
}
}
}
GrVkMemory::FlushMappedAlloc(gpu, alloc);
GR_VK_CALL(gpu->vkInterface(), UnmapMemory(gpu->device(), alloc.fMemory));
return true;
}
GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(void* srcData, int w, int h,
GrPixelConfig config,
bool isRenderTarget,
GrMipMapped mipMapped) {
VkFormat pixelFormat;
if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
return GrBackendTexture(); // invalid
}
bool linearTiling = false;
if (!fVkCaps->isConfigTexturable(config)) {
return GrBackendTexture(); // invalid
}
if (isRenderTarget && !fVkCaps->isConfigRenderable(config)) {
return GrBackendTexture(); // invalid
}
// Currently we don't support uploading pixel data when mipped.
if (srcData && GrMipMapped::kYes == mipMapped) {
return GrBackendTexture(); // invalid
}
if (fVkCaps->isConfigTexturableLinearly(config) &&
(!isRenderTarget || fVkCaps->isConfigRenderableLinearly(config, false)) &&
GrMipMapped::kNo == mipMapped) {
linearTiling = true;
}
VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
if (isRenderTarget) {
usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
}
VkImage image = VK_NULL_HANDLE;
GrVkAlloc alloc = { VK_NULL_HANDLE, 0, 0, 0 };
VkImageTiling imageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
VkImageLayout initialLayout = (VK_IMAGE_TILING_LINEAR == imageTiling)
? VK_IMAGE_LAYOUT_PREINITIALIZED
: VK_IMAGE_LAYOUT_UNDEFINED;
// Create Image
VkSampleCountFlagBits vkSamples;
if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
return GrBackendTexture(); // invalid
}
// Figure out the number of mip levels.
uint32_t mipLevels = 1;
if (GrMipMapped::kYes == mipMapped) {
mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
}
const VkImageCreateInfo imageCreateInfo = {
VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
nullptr, // pNext
0, // VkImageCreateFlags
VK_IMAGE_TYPE_2D, // VkImageType
pixelFormat, // VkFormat
{ (uint32_t) w, (uint32_t) h, 1 }, // VkExtent3D
mipLevels, // mipLevels
1, // arrayLayers
vkSamples, // samples
imageTiling, // VkImageTiling
usageFlags, // VkImageUsageFlags
VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
0, // queueFamilyCount
0, // pQueueFamilyIndices
initialLayout // initialLayout
};
GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
if (!GrVkMemory::AllocAndBindImageMemory(this, image, linearTiling, &alloc)) {
VK_CALL(DestroyImage(this->device(), image, nullptr));
return GrBackendTexture(); // invalid
}
// We need to declare these early so that we can delete them at the end outside of the if block.
GrVkAlloc bufferAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
VkBuffer buffer = VK_NULL_HANDLE;
VkResult err;
const VkCommandBufferAllocateInfo cmdInfo = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
nullptr, // pNext
fCmdPool, // commandPool
VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1 // bufferCount
};
VkCommandBuffer cmdBuffer;
err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
if (err) {
GrVkMemory::FreeImageMemory(this, false, alloc);
VK_CALL(DestroyImage(fDevice, image, nullptr));
return GrBackendTexture(); // invalid
}
VkCommandBufferBeginInfo cmdBufferBeginInfo;
memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
cmdBufferBeginInfo.pNext = nullptr;
cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
cmdBufferBeginInfo.pInheritanceInfo = nullptr;
err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
SkASSERT(!err);
size_t bpp = GrBytesPerPixel(config);
size_t rowCopyBytes = bpp * w;
if (linearTiling) {
const VkImageSubresource subres = {
VK_IMAGE_ASPECT_COLOR_BIT,
0, // mipLevel
0, // arraySlice
};
VkSubresourceLayout layout;
VK_CALL(GetImageSubresourceLayout(fDevice, image, &subres, &layout));
if (!copy_testing_data(this, srcData, alloc, 0, rowCopyBytes,
static_cast<size_t>(layout.rowPitch), h)) {
GrVkMemory::FreeImageMemory(this, true, alloc);
VK_CALL(DestroyImage(fDevice, image, nullptr));
VK_CALL(EndCommandBuffer(cmdBuffer));
VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
return GrBackendTexture(); // invalid
}
} else {
SkASSERT(w && h);
SkTArray<size_t> individualMipOffsets(mipLevels);
individualMipOffsets.push_back(0);
size_t combinedBufferSize = w * bpp * h;
int currentWidth = w;
int currentHeight = h;
// The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
// config. This works with the assumption that the bytes in pixel config is always a power
// of 2.
SkASSERT((bpp & (bpp - 1)) == 0);
const size_t alignmentMask = 0x3 | (bpp - 1);
for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
currentWidth = SkTMax(1, currentWidth/2);
currentHeight = SkTMax(1, currentHeight/2);
const size_t trimmedSize = currentWidth * bpp * currentHeight;
const size_t alignmentDiff = combinedBufferSize & alignmentMask;
if (alignmentDiff != 0) {
combinedBufferSize += alignmentMask - alignmentDiff + 1;
}
individualMipOffsets.push_back(combinedBufferSize);
combinedBufferSize += trimmedSize;
}
VkBufferCreateInfo bufInfo;
memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufInfo.flags = 0;
bufInfo.size = combinedBufferSize;
bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
bufInfo.queueFamilyIndexCount = 0;
bufInfo.pQueueFamilyIndices = nullptr;
err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
if (err) {
GrVkMemory::FreeImageMemory(this, false, alloc);
VK_CALL(DestroyImage(fDevice, image, nullptr));
VK_CALL(EndCommandBuffer(cmdBuffer));
VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
return GrBackendTexture(); // invalid
}
if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type,
true, &bufferAlloc)) {
GrVkMemory::FreeImageMemory(this, false, alloc);
VK_CALL(DestroyImage(fDevice, image, nullptr));
VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
VK_CALL(EndCommandBuffer(cmdBuffer));
VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
return GrBackendTexture(); // invalid
}
currentWidth = w;
currentHeight = h;
for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
SkASSERT(0 == currentMipLevel || !srcData);
size_t currentRowBytes = bpp * currentWidth;
size_t bufferOffset = individualMipOffsets[currentMipLevel];
if (!copy_testing_data(this, srcData, bufferAlloc, bufferOffset,
currentRowBytes, currentRowBytes, currentHeight)) {
GrVkMemory::FreeImageMemory(this, false, alloc);
VK_CALL(DestroyImage(fDevice, image, nullptr));
GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
VK_CALL(EndCommandBuffer(cmdBuffer));
VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
return GrBackendTexture(); // invalid
}
currentWidth = SkTMax(1, currentWidth/2);
currentHeight = SkTMax(1, currentHeight/2);
}
// Set image layout and add barrier
VkImageMemoryBarrier barrier;
memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.pNext = nullptr;
barrier.srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(initialLayout);
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
barrier.oldLayout = initialLayout;
barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = image;
barrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0 , 1};
VK_CALL(CmdPipelineBarrier(cmdBuffer,
GrVkMemory::LayoutToPipelineStageFlags(initialLayout),
VK_PIPELINE_STAGE_TRANSFER_BIT,
0,
0, nullptr,
0, nullptr,
1, &barrier));
initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
SkTArray<VkBufferImageCopy> regions(mipLevels);
currentWidth = w;
currentHeight = h;
for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
// Submit copy command
VkBufferImageCopy& region = regions.push_back();
memset(®ion, 0, sizeof(VkBufferImageCopy));
region.bufferOffset = individualMipOffsets[currentMipLevel];
region.bufferRowLength = currentWidth;
region.bufferImageHeight = currentHeight;
region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
region.imageOffset = { 0, 0, 0 };
region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
currentWidth = SkTMax(1, currentWidth/2);
currentHeight = SkTMax(1, currentHeight/2);
}
VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
regions.begin()));
}
// Change Image layout to shader read since if we use this texture as a borrowed textures within
// Ganesh we require that its layout be set to that
VkImageMemoryBarrier barrier;
memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.pNext = nullptr;
barrier.srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(initialLayout);
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
barrier.oldLayout = initialLayout;
barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = image;
barrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0 , 1};
VK_CALL(CmdPipelineBarrier(cmdBuffer,
GrVkMemory::LayoutToPipelineStageFlags(initialLayout),
VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
0,
0, nullptr,
0, nullptr,
1, &barrier));
// End CommandBuffer
err = VK_CALL(EndCommandBuffer(cmdBuffer));
SkASSERT(!err);
// Create Fence for queue
VkFence fence;
VkFenceCreateInfo fenceInfo;
memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
SkASSERT(!err);
VkSubmitInfo submitInfo;
memset(&submitInfo, 0, sizeof(VkSubmitInfo));
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.pNext = nullptr;
submitInfo.waitSemaphoreCount = 0;
submitInfo.pWaitSemaphores = nullptr;
submitInfo.pWaitDstStageMask = 0;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &cmdBuffer;
submitInfo.signalSemaphoreCount = 0;
submitInfo.pSignalSemaphores = nullptr;
err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
SkASSERT(!err);
err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
if (VK_TIMEOUT == err) {
GrVkMemory::FreeImageMemory(this, false, alloc);
VK_CALL(DestroyImage(fDevice, image, nullptr));
GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
VK_CALL(DestroyFence(fDevice, fence, nullptr));
SkDebugf("Fence failed to signal: %d\n", err);
SK_ABORT("failing");
}
SkASSERT(!err);
// Clean up transfer resources
if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
}
VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
VK_CALL(DestroyFence(fDevice, fence, nullptr));
GrVkImageInfo info;
info.fImage = image;
info.fAlloc = alloc;
info.fImageTiling = imageTiling;
info.fImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
info.fFormat = pixelFormat;
info.fLevelCount = mipLevels;
return GrBackendTexture(w, h, info);
}
bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
SkASSERT(kVulkan_GrBackend == tex.fBackend);
const GrVkImageInfo* backend = tex.getVkImageInfo();
if (backend && backend->fImage && backend->fAlloc.fMemory) {
VkMemoryRequirements req;
memset(&req, 0, sizeof(req));
GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
backend->fImage,
&req));
// TODO: find a better check
// This will probably fail with a different driver
return (req.size > 0) && (req.size <= 8192 * 8192);
}
return false;
}
void GrVkGpu::deleteTestingOnlyBackendTexture(GrBackendTexture* tex, bool abandon) {
SkASSERT(kVulkan_GrBackend == tex->fBackend);
const GrVkImageInfo* info = tex->getVkImageInfo();
if (info && !abandon) {
// something in the command buffer may still be using this, so force submit
this->submitCommandBuffer(kForce_SyncQueue);
GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(info));
}
}
////////////////////////////////////////////////////////////////////////////////
void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask,
bool byRegion,
VkMemoryBarrier* barrier) const {
SkASSERT(fCurrentCmdBuffer);
fCurrentCmdBuffer->pipelineBarrier(this,
srcStageMask,
dstStageMask,
byRegion,
GrVkCommandBuffer::kMemory_BarrierType,
barrier);
}
void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask,
bool byRegion,
VkBufferMemoryBarrier* barrier) const {
SkASSERT(fCurrentCmdBuffer);
fCurrentCmdBuffer->pipelineBarrier(this,
srcStageMask,
dstStageMask,
byRegion,
GrVkCommandBuffer::kBufferMemory_BarrierType,
barrier);
}
void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask,
bool byRegion,
VkImageMemoryBarrier* barrier) const {
SkASSERT(fCurrentCmdBuffer);
fCurrentCmdBuffer->pipelineBarrier(this,
srcStageMask,
dstStageMask,
byRegion,
GrVkCommandBuffer::kImageMemory_BarrierType,
barrier);
}
void GrVkGpu::onFinishFlush(bool insertedSemaphore) {
// Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
// not effect what we do here.
this->submitCommandBuffer(kSkip_SyncQueue);
}
void GrVkGpu::clearStencil(GrRenderTarget* target, int clearValue) {
if (!target) {
return;
}
GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment();
GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
VkClearDepthStencilValue vkStencilColor;
vkStencilColor.depth = 0.0f;
vkStencilColor.stencil = clearValue;
vkStencil->setImageLayout(this,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
false);
VkImageSubresourceRange subRange;
memset(&subRange, 0, sizeof(VkImageSubresourceRange));
subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
subRange.baseMipLevel = 0;
subRange.levelCount = 1;
subRange.baseArrayLayer = 0;
subRange.layerCount = 1;
// TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a
// draw. Thus we should look into using the load op functions on the render pass to clear out
// the stencil there.
fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange);
}
inline bool can_copy_image(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
const GrSurface* src, GrSurfaceOrigin srcOrigin,
const GrVkGpu* gpu) {
const GrRenderTarget* dstRT = dst->asRenderTarget();
const GrRenderTarget* srcRT = src->asRenderTarget();
if (dstRT && srcRT) {
if (srcRT->numColorSamples() != dstRT->numColorSamples()) {
return false;
}
} else if (dstRT) {
if (dstRT->numColorSamples() > 1) {
return false;
}
} else if (srcRT) {
if (srcRT->numColorSamples() > 1) {
return false;
}
}
// We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
// as image usage flags.
if (srcOrigin == dstOrigin &&
GrBytesPerPixel(src->config()) == GrBytesPerPixel(dst->config())) {
return true;
}
return false;
}
void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
GrSurface* src, GrSurfaceOrigin srcOrigin,
GrVkImage* dstImage,
GrVkImage* srcImage,
const SkIRect& srcRect,
const SkIPoint& dstPoint) {
SkASSERT(can_copy_image(dst, dstOrigin, src, srcOrigin, this));
// These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
// the cache is flushed since it is only being written to.
dstImage->setImageLayout(this,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
false);
srcImage->setImageLayout(this,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_ACCESS_TRANSFER_READ_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
false);
// Flip rect if necessary
SkIRect srcVkRect = srcRect;
int32_t dstY = dstPoint.fY;
if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
srcVkRect.fTop = src->height() - srcRect.fBottom;
srcVkRect.fBottom = src->height() - srcRect.fTop;
dstY = dst->height() - dstPoint.fY - srcVkRect.height();
}
VkImageCopy copyRegion;
memset(©Region, 0, sizeof(VkImageCopy));
copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
fCurrentCmdBuffer->copyImage(this,
srcImage,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
dstImage,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1,
©Region);
SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
srcRect.width(), srcRect.height());
this->didWriteToSurface(dst, &dstRect);
}
inline bool can_copy_as_blit(const GrSurface* dst,
const GrSurface* src,
const GrVkImage* dstImage,
const GrVkImage* srcImage,
const GrVkGpu* gpu) {
// We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
// as image usage flags.
const GrVkCaps& caps = gpu->vkCaps();
if (!caps.configCanBeDstofBlit(dst->config(), dstImage->isLinearTiled()) ||
!caps.configCanBeSrcofBlit(src->config(), srcImage->isLinearTiled())) {
return false;
}
// We cannot blit images that are multisampled. Will need to figure out if we can blit the
// resolved msaa though.
if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) ||
(src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) {
return false;
}
return true;
}
void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
GrSurface* src, GrSurfaceOrigin srcOrigin,
GrVkImage* dstImage,
GrVkImage* srcImage,
const SkIRect& srcRect,
const SkIPoint& dstPoint) {
SkASSERT(can_copy_as_blit(dst, src, dstImage, srcImage, this));
dstImage->setImageLayout(this,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
false);
srcImage->setImageLayout(this,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_ACCESS_TRANSFER_READ_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
false);
// Flip rect if necessary
SkIRect srcVkRect;
srcVkRect.fLeft = srcRect.fLeft;
srcVkRect.fRight = srcRect.fRight;
SkIRect dstRect;
dstRect.fLeft = dstPoint.fX;
dstRect.fRight = dstPoint.fX + srcRect.width();
if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
srcVkRect.fTop = src->height() - srcRect.fBottom;
srcVkRect.fBottom = src->height() - srcRect.fTop;
} else {
srcVkRect.fTop = srcRect.fTop;
srcVkRect.fBottom = srcRect.fBottom;
}
if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
} else {
dstRect.fTop = dstPoint.fY;
}
dstRect.fBottom = dstRect.fTop + srcVkRect.height();
// If we have different origins, we need to flip the top and bottom of the dst rect so that we
// get the correct origintation of the copied data.
if (srcOrigin != dstOrigin) {
SkTSwap(dstRect.fTop, dstRect.fBottom);
}
VkImageBlit blitRegion;
memset(&blitRegion, 0, sizeof(VkImageBlit));
blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
fCurrentCmdBuffer->blitImage(this,
*srcImage,
*dstImage,
1,
&blitRegion,
VK_FILTER_NEAREST); // We never scale so any filter works here
this->didWriteToSurface(dst, &dstRect);
}
inline bool can_copy_as_resolve(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
const GrSurface* src, GrSurfaceOrigin srcOrigin,
const GrVkGpu* gpu) {
// Our src must be a multisampled render target
if (!src->asRenderTarget() || 1 == src->asRenderTarget()->numColorSamples()) {
return false;
}
// The dst must not be a multisampled render target, expect in the case where the dst is the
// resolve texture connected to the msaa src. We check for this in case we are copying a part of
// a surface to a different region in the same surface.
if (dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1 && dst != src) {
return false;
}
// Surfaces must have the same origin.
if (srcOrigin != dstOrigin) {
return false;
}
return true;
}
void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin,
GrSurface* src, GrSurfaceOrigin srcOrigin,
const SkIRect& srcRect,
const SkIPoint& dstPoint) {
GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
this->resolveImage(dst, dstOrigin, srcRT, srcOrigin, srcRect, dstPoint);
}
bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
GrSurface* src, GrSurfaceOrigin srcOrigin,
const SkIRect& srcRect,
const SkIPoint& dstPoint) {
if (can_copy_as_resolve(dst, dstOrigin, src, srcOrigin, this)) {
this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
return true;
}
if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
}
if (fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
return true;
}
GrVkImage* dstImage;
GrVkImage* srcImage;
GrRenderTarget* dstRT = dst->asRenderTarget();
if (dstRT) {
GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
} else {
SkASSERT(dst->asTexture());
dstImage = static_cast<GrVkTexture*>(dst->asTexture());
}
GrRenderTarget* srcRT = src->asRenderTarget();
if (srcRT) {
GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
} else {
SkASSERT(src->asTexture());
srcImage = static_cast<GrVkTexture*>(src->asTexture());
}
// For borrowed textures, we *only* want to copy using draws (to avoid layout changes)
if (srcImage->isBorrowed()) {
return false;
}
if (can_copy_image(dst, dstOrigin, src, srcOrigin, this)) {
this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
srcRect, dstPoint);
return true;
}
if (can_copy_as_blit(dst, src, dstImage, srcImage, this)) {
this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
srcRect, dstPoint);
return true;
}
return false;
}
void GrVkGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, GrSurfaceOrigin, const GrStencilSettings&,
int* effectiveSampleCnt, SamplePattern*) {
// TODO: stub.
SkASSERT(!this->caps()->sampleLocationsSupport());
*effectiveSampleCnt = rt->numStencilSamples();
}
bool GrVkGpu::onGetReadPixelsInfo(GrSurface* srcSurface, GrSurfaceOrigin srcOrigin,
int width, int height, size_t rowBytes,
GrPixelConfig readConfig, DrawPreference* drawPreference,
ReadPixelTempDrawInfo* tempDrawInfo) {
// These settings we will always want if a temp draw is performed.
tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
tempDrawInfo->fTempSurfaceDesc.fWidth = width;
tempDrawInfo->fTempSurfaceDesc.fHeight = height;
tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 1;
tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
tempDrawInfo->fTempSurfaceFit = SkBackingFit::kApprox;
// For now assume no swizzling, we may change that below.
tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
// Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
// from will be srcConfig and we will read readConfig pixels from it.
// Note that if we require a draw and return a non-renderable format for the temp surface the
// base class will fail for us.
tempDrawInfo->fTempSurfaceDesc.fConfig = srcSurface->config();
tempDrawInfo->fReadConfig = readConfig;
if (srcSurface->config() == readConfig) {
return true;
}
// Any config change requires a draw
ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
tempDrawInfo->fReadConfig = readConfig;
return true;
}
bool GrVkGpu::onReadPixels(GrSurface* surface, GrSurfaceOrigin origin,
int left, int top, int width, int height,
GrPixelConfig config,
void* buffer,
size_t rowBytes) {
VkFormat pixelFormat;
if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
return false;
}
GrVkImage* image = nullptr;
GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
if (rt) {
// resolve the render target if necessary
switch (rt->getResolveType()) {
case GrVkRenderTarget::kCantResolve_ResolveType:
return false;
case GrVkRenderTarget::kAutoResolves_ResolveType:
break;
case GrVkRenderTarget::kCanResolve_ResolveType:
this->internalResolveRenderTarget(rt, origin, false);
break;
default:
SK_ABORT("Unknown resolve type");
}
image = rt;
} else {
image = static_cast<GrVkTexture*>(surface->asTexture());
}
if (!image) {
return false;
}
// Change layout of our target so it can be used as copy
image->setImageLayout(this,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_ACCESS_TRANSFER_READ_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
false);
size_t bpp = GrBytesPerPixel(config);
size_t tightRowBytes = bpp * width;
bool flipY = kBottomLeft_GrSurfaceOrigin == origin;
VkBufferImageCopy region;
memset(®ion, 0, sizeof(VkBufferImageCopy));
bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
if (copyFromOrigin) {
region.imageOffset = { 0, 0, 0 };
region.imageExtent = { (uint32_t)(left + width),
(uint32_t)(flipY ? surface->height() - top : top + height),
1
};
} else {
VkOffset3D offset = {
left,
flipY ? surface->height() - top - height : top,
0
};
region.imageOffset = offset;
region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
}
size_t transBufferRowBytes = bpp * region.imageExtent.width;
GrVkTransferBuffer* transferBuffer =
static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * height,
kXferGpuToCpu_GrBufferType,
kStream_GrAccessPattern));
// Copy the image to a buffer so we can map it to cpu memory
region.bufferOffset = transferBuffer->offset();
region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
fCurrentCmdBuffer->copyImageToBuffer(this,
image,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
transferBuffer,
1,
®ion);
// make sure the copy to buffer has finished
transferBuffer->addMemoryBarrier(this,
VK_ACCESS_TRANSFER_WRITE_BIT,
VK_ACCESS_HOST_READ_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_HOST_BIT,
false);
// We need to submit the current command buffer to the Queue and make sure it finishes before
// we can copy the data out of the buffer.
this->submitCommandBuffer(kForce_SyncQueue);
GrVkMemory::InvalidateMappedAlloc(this, transferBuffer->alloc());
void* mappedMemory = transferBuffer->map();
if (copyFromOrigin) {
uint32_t skipRows = region.imageExtent.height - height;
mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
}
if (flipY) {
const char* srcRow = reinterpret_cast<const char*>(mappedMemory);
char* dstRow = reinterpret_cast<char*>(buffer)+(height - 1) * rowBytes;
for (int y = 0; y < height; y++) {
memcpy(dstRow, srcRow, tightRowBytes);
srcRow += transBufferRowBytes;
dstRow -= rowBytes;
}
} else {
SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
}
transferBuffer->unmap();
transferBuffer->unref();
return true;
}
// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
// the the entire attachment. Similar requirements for the y and height components.
void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
const VkExtent2D& granularity, int maxWidth, int maxHeight) {
// Adjust Width
if ((0 != granularity.width && 1 != granularity.width)) {
// Start with the right side of rect so we know if we end up going pass the maxWidth.
int rightAdj = srcBounds.fRight % granularity.width;
if (rightAdj != 0) {
rightAdj = granularity.width - rightAdj;
}
dstBounds->fRight = srcBounds.fRight + rightAdj;
if (dstBounds->fRight > maxWidth) {
dstBounds->fRight = maxWidth;
dstBounds->fLeft = 0;
} else {
dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
}
} else {
dstBounds->fLeft = srcBounds.fLeft;
dstBounds->fRight = srcBounds.fRight;
}
// Adjust height
if ((0 != granularity.height && 1 != granularity.height)) {
// Start with the bottom side of rect so we know if we end up going pass the maxHeight.
int bottomAdj = srcBounds.fBottom % granularity.height;
if (bottomAdj != 0) {
bottomAdj = granularity.height - bottomAdj;
}
dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
if (dstBounds->fBottom > maxHeight) {
dstBounds->fBottom = maxHeight;
dstBounds->fTop = 0;
} else {
dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
}
} else {
dstBounds->fTop = srcBounds.fTop;
dstBounds->fBottom = srcBounds.fBottom;
}
}
void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
const GrVkRenderPass* renderPass,
const VkClearValue* colorClear,
GrVkRenderTarget* target, GrSurfaceOrigin origin,
const SkIRect& bounds) {
const SkIRect* pBounds = &bounds;
SkIRect flippedBounds;
if (kBottomLeft_GrSurfaceOrigin == origin) {
flippedBounds = bounds;
flippedBounds.fTop = target->height() - bounds.fBottom;
flippedBounds.fBottom = target->height() - bounds.fTop;
pBounds = &flippedBounds;
}
// The bounds we use for the render pass should be of the granularity supported
// by the device.
const VkExtent2D& granularity = renderPass->granularity();
SkIRect adjustedBounds;
if ((0 != granularity.width && 1 != granularity.width) ||
(0 != granularity.height && 1 != granularity.height)) {
adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
target->width(), target->height());
pBounds = &adjustedBounds;
}
#ifdef SK_DEBUG
uint32_t index;
bool result = renderPass->colorAttachmentIndex(&index);
SkASSERT(result && 0 == index);
result = renderPass->stencilAttachmentIndex(&index);
if (result) {
SkASSERT(1 == index);
}
#endif
VkClearValue clears[2];
clears[0].color = colorClear->color;
clears[1].depthStencil.depth = 0.0f;
clears[1].depthStencil.stencil = 0;
fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
for (int i = 0; i < buffers.count(); ++i) {
fCurrentCmdBuffer->executeCommands(this, buffers[i]);
}
fCurrentCmdBuffer->endRenderPass(this);
this->didWriteToSurface(target, &bounds);
}
GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
VkFenceCreateInfo createInfo;
memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
createInfo.pNext = nullptr;
createInfo.flags = 0;
VkFence fence = VK_NULL_HANDLE;
VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
return (GrFence)fence;
}
bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
return (VK_SUCCESS == result);
}
void GrVkGpu::deleteFence(GrFence fence) const {
VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
}
sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
return GrVkSemaphore::Make(this, isOwned);
}
sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
GrResourceProvider::SemaphoreWrapType wrapType,
GrWrapOwnership ownership) {
return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
}
void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
GrVkSemaphore::Resource* resource = vkSem->getResource();
if (resource->shouldSignal()) {
resource->ref();
fSemaphoresToSignal.push_back(resource);
}
if (flush) {
this->submitCommandBuffer(kSkip_SyncQueue);
}
}
void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
GrVkSemaphore::Resource* resource = vkSem->getResource();
if (resource->shouldWait()) {
resource->ref();
fSemaphoresToWaitOn.push_back(resource);
}
}
sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
SkASSERT(texture);
GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
vkTexture->setImageLayout(this,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
VK_ACCESS_SHADER_READ_BIT,
VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
false);
this->submitCommandBuffer(kSkip_SyncQueue);
// The image layout change serves as a barrier, so no semaphore is needed
return nullptr;
}
|