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
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
|
/*
* Copyright © 2014 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.
*
* Authors:
* Connor Abbott (cwabbott0@gmail.com)
*
*/
#include "glsl_to_nir.h"
#include "ir_visitor.h"
#include "ir_hierarchical_visitor.h"
#include "ir.h"
#include "compiler/nir/nir_control_flow.h"
#include "compiler/nir/nir_builder.h"
#include "main/imports.h"
/*
* pass to lower GLSL IR to NIR
*
* This will lower variable dereferences to loads/stores of corresponding
* variables in NIR - the variables will be converted to registers in a later
* pass.
*/
namespace {
class nir_visitor : public ir_visitor
{
public:
nir_visitor(nir_shader *shader);
~nir_visitor();
virtual void visit(ir_variable *);
virtual void visit(ir_function *);
virtual void visit(ir_function_signature *);
virtual void visit(ir_loop *);
virtual void visit(ir_if *);
virtual void visit(ir_discard *);
virtual void visit(ir_loop_jump *);
virtual void visit(ir_return *);
virtual void visit(ir_call *);
virtual void visit(ir_assignment *);
virtual void visit(ir_emit_vertex *);
virtual void visit(ir_end_primitive *);
virtual void visit(ir_expression *);
virtual void visit(ir_swizzle *);
virtual void visit(ir_texture *);
virtual void visit(ir_constant *);
virtual void visit(ir_dereference_variable *);
virtual void visit(ir_dereference_record *);
virtual void visit(ir_dereference_array *);
virtual void visit(ir_barrier *);
void create_function(ir_function_signature *ir);
private:
void add_instr(nir_instr *instr, unsigned num_components, unsigned bit_size);
nir_ssa_def *evaluate_rvalue(ir_rvalue *ir);
nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def **srcs);
nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1);
nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1,
nir_ssa_def *src2);
nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1,
nir_ssa_def *src2, nir_ssa_def *src3);
bool supports_ints;
nir_shader *shader;
nir_function_impl *impl;
nir_builder b;
nir_ssa_def *result; /* result of the expression tree last visited */
nir_deref_var *evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir);
/* the head of the dereference chain we're creating */
nir_deref_var *deref_head;
/* the tail of the dereference chain we're creating */
nir_deref *deref_tail;
nir_variable *var; /* variable created by ir_variable visitor */
/* whether the IR we're operating on is per-function or global */
bool is_global;
/* map of ir_variable -> nir_variable */
struct hash_table *var_table;
/* map of ir_function_signature -> nir_function_overload */
struct hash_table *overload_table;
};
/*
* This visitor runs before the main visitor, calling create_function() for
* each function so that the main visitor can resolve forward references in
* calls.
*/
class nir_function_visitor : public ir_hierarchical_visitor
{
public:
nir_function_visitor(nir_visitor *v) : visitor(v)
{
}
virtual ir_visitor_status visit_enter(ir_function *);
private:
nir_visitor *visitor;
};
} /* end of anonymous namespace */
static void
nir_remap_attributes(nir_shader *shader)
{
nir_foreach_variable(var, &shader->inputs) {
var->data.location += _mesa_bitcount_64(shader->info.double_inputs_read &
BITFIELD64_MASK(var->data.location));
}
/* Once the remap is done, reset double_inputs_read, so later it will have
* which location/slots are doubles */
shader->info.double_inputs_read = 0;
}
nir_shader *
glsl_to_nir(const struct gl_shader_program *shader_prog,
gl_shader_stage stage,
const nir_shader_compiler_options *options)
{
struct gl_linked_shader *sh = shader_prog->_LinkedShaders[stage];
nir_shader *shader = nir_shader_create(NULL, stage, options,
&sh->Program->info);
nir_visitor v1(shader);
nir_function_visitor v2(&v1);
v2.run(sh->ir);
visit_exec_list(sh->ir, &v1);
nir_lower_constant_initializers(shader, (nir_variable_mode)~0);
/* Remap the locations to slots so those requiring two slots will occupy
* two locations. For instance, if we have in the IR code a dvec3 attr0 in
* location 0 and vec4 attr1 in location 1, in NIR attr0 will use
* locations/slots 0 and 1, and attr1 will use location/slot 2 */
if (shader->stage == MESA_SHADER_VERTEX)
nir_remap_attributes(shader);
shader->info.name = ralloc_asprintf(shader, "GLSL%d", shader_prog->Name);
if (shader_prog->Label)
shader->info.label = ralloc_strdup(shader, shader_prog->Label);
/* Check for transform feedback varyings specified via the API */
shader->info.has_transform_feedback_varyings =
shader_prog->TransformFeedback.NumVarying > 0;
/* Check for transform feedback varyings specified in the Shader */
if (shader_prog->last_vert_prog)
shader->info.has_transform_feedback_varyings |=
shader_prog->last_vert_prog->sh.LinkedTransformFeedback->NumVarying > 0;
return shader;
}
nir_visitor::nir_visitor(nir_shader *shader)
{
this->supports_ints = shader->options->native_integers;
this->shader = shader;
this->is_global = true;
this->var_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
_mesa_key_pointer_equal);
this->overload_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
_mesa_key_pointer_equal);
this->result = NULL;
this->impl = NULL;
this->var = NULL;
this->deref_head = NULL;
this->deref_tail = NULL;
memset(&this->b, 0, sizeof(this->b));
}
nir_visitor::~nir_visitor()
{
_mesa_hash_table_destroy(this->var_table, NULL);
_mesa_hash_table_destroy(this->overload_table, NULL);
}
nir_deref_var *
nir_visitor::evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir)
{
ir->accept(this);
ralloc_steal(mem_ctx, this->deref_head);
return this->deref_head;
}
static nir_constant *
constant_copy(ir_constant *ir, void *mem_ctx)
{
if (ir == NULL)
return NULL;
nir_constant *ret = ralloc(mem_ctx, nir_constant);
const unsigned rows = ir->type->vector_elements;
const unsigned cols = ir->type->matrix_columns;
unsigned i;
ret->num_elements = 0;
switch (ir->type->base_type) {
case GLSL_TYPE_UINT:
/* Only float base types can be matrices. */
assert(cols == 1);
for (unsigned r = 0; r < rows; r++)
ret->values[0].u32[r] = ir->value.u[r];
break;
case GLSL_TYPE_INT:
/* Only float base types can be matrices. */
assert(cols == 1);
for (unsigned r = 0; r < rows; r++)
ret->values[0].i32[r] = ir->value.i[r];
break;
case GLSL_TYPE_FLOAT:
for (unsigned c = 0; c < cols; c++) {
for (unsigned r = 0; r < rows; r++)
ret->values[c].f32[r] = ir->value.f[c * rows + r];
}
break;
case GLSL_TYPE_DOUBLE:
for (unsigned c = 0; c < cols; c++) {
for (unsigned r = 0; r < rows; r++)
ret->values[c].f64[r] = ir->value.d[c * rows + r];
}
break;
case GLSL_TYPE_UINT64:
/* Only float base types can be matrices. */
assert(cols == 1);
for (unsigned r = 0; r < rows; r++)
ret->values[0].u64[r] = ir->value.u64[r];
break;
case GLSL_TYPE_INT64:
/* Only float base types can be matrices. */
assert(cols == 1);
for (unsigned r = 0; r < rows; r++)
ret->values[0].i64[r] = ir->value.i64[r];
break;
case GLSL_TYPE_BOOL:
/* Only float base types can be matrices. */
assert(cols == 1);
for (unsigned r = 0; r < rows; r++)
ret->values[0].u32[r] = ir->value.b[r] ? NIR_TRUE : NIR_FALSE;
break;
case GLSL_TYPE_STRUCT:
ret->elements = ralloc_array(mem_ctx, nir_constant *,
ir->type->length);
ret->num_elements = ir->type->length;
i = 0;
foreach_in_list(ir_constant, field, &ir->components) {
ret->elements[i] = constant_copy(field, mem_ctx);
i++;
}
break;
case GLSL_TYPE_ARRAY:
ret->elements = ralloc_array(mem_ctx, nir_constant *,
ir->type->length);
ret->num_elements = ir->type->length;
for (i = 0; i < ir->type->length; i++)
ret->elements[i] = constant_copy(ir->array_elements[i], mem_ctx);
break;
default:
unreachable("not reached");
}
return ret;
}
void
nir_visitor::visit(ir_variable *ir)
{
/* TODO: In future we should switch to using the NIR lowering pass but for
* now just ignore these variables as GLSL IR should have lowered them.
* Anything remaining are just dead vars that weren't cleaned up.
*/
if (ir->data.mode == ir_var_shader_shared)
return;
nir_variable *var = ralloc(shader, nir_variable);
var->type = ir->type;
var->name = ralloc_strdup(var, ir->name);
var->data.read_only = ir->data.read_only;
var->data.centroid = ir->data.centroid;
var->data.sample = ir->data.sample;
var->data.patch = ir->data.patch;
var->data.invariant = ir->data.invariant;
var->data.location = ir->data.location;
var->data.compact = false;
switch(ir->data.mode) {
case ir_var_auto:
case ir_var_temporary:
if (is_global)
var->data.mode = nir_var_global;
else
var->data.mode = nir_var_local;
break;
case ir_var_function_in:
case ir_var_function_out:
case ir_var_function_inout:
case ir_var_const_in:
var->data.mode = nir_var_local;
break;
case ir_var_shader_in:
if (shader->stage == MESA_SHADER_FRAGMENT &&
ir->data.location == VARYING_SLOT_FACE) {
/* For whatever reason, GLSL IR makes gl_FrontFacing an input */
var->data.location = SYSTEM_VALUE_FRONT_FACE;
var->data.mode = nir_var_system_value;
} else if (shader->stage == MESA_SHADER_GEOMETRY &&
ir->data.location == VARYING_SLOT_PRIMITIVE_ID) {
/* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
var->data.location = SYSTEM_VALUE_PRIMITIVE_ID;
var->data.mode = nir_var_system_value;
} else {
var->data.mode = nir_var_shader_in;
if (shader->stage == MESA_SHADER_TESS_EVAL &&
(ir->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
ir->data.location == VARYING_SLOT_TESS_LEVEL_OUTER)) {
var->data.compact = ir->type->without_array()->is_scalar();
}
}
/* Mark all the locations that require two slots */
if (glsl_type_is_dual_slot(glsl_without_array(var->type))) {
for (uint i = 0; i < glsl_count_attribute_slots(var->type, true); i++) {
uint64_t bitfield = BITFIELD64_BIT(var->data.location + i);
shader->info.double_inputs_read |= bitfield;
}
}
break;
case ir_var_shader_out:
var->data.mode = nir_var_shader_out;
if (shader->stage == MESA_SHADER_TESS_CTRL &&
(ir->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
ir->data.location == VARYING_SLOT_TESS_LEVEL_OUTER)) {
var->data.compact = ir->type->without_array()->is_scalar();
}
break;
case ir_var_uniform:
var->data.mode = nir_var_uniform;
break;
case ir_var_shader_storage:
var->data.mode = nir_var_shader_storage;
break;
case ir_var_system_value:
var->data.mode = nir_var_system_value;
break;
default:
unreachable("not reached");
}
var->data.interpolation = ir->data.interpolation;
var->data.origin_upper_left = ir->data.origin_upper_left;
var->data.pixel_center_integer = ir->data.pixel_center_integer;
var->data.location_frac = ir->data.location_frac;
switch (ir->data.depth_layout) {
case ir_depth_layout_none:
var->data.depth_layout = nir_depth_layout_none;
break;
case ir_depth_layout_any:
var->data.depth_layout = nir_depth_layout_any;
break;
case ir_depth_layout_greater:
var->data.depth_layout = nir_depth_layout_greater;
break;
case ir_depth_layout_less:
var->data.depth_layout = nir_depth_layout_less;
break;
case ir_depth_layout_unchanged:
var->data.depth_layout = nir_depth_layout_unchanged;
break;
default:
unreachable("not reached");
}
var->data.index = ir->data.index;
var->data.descriptor_set = 0;
var->data.binding = ir->data.binding;
var->data.offset = ir->data.offset;
var->data.image.read_only = ir->data.memory_read_only;
var->data.image.write_only = ir->data.memory_write_only;
var->data.image.coherent = ir->data.memory_coherent;
var->data.image._volatile = ir->data.memory_volatile;
var->data.image.restrict_flag = ir->data.memory_restrict;
var->data.image.format = ir->data.image_format;
var->data.fb_fetch_output = ir->data.fb_fetch_output;
var->num_state_slots = ir->get_num_state_slots();
if (var->num_state_slots > 0) {
var->state_slots = ralloc_array(var, nir_state_slot,
var->num_state_slots);
ir_state_slot *state_slots = ir->get_state_slots();
for (unsigned i = 0; i < var->num_state_slots; i++) {
for (unsigned j = 0; j < 5; j++)
var->state_slots[i].tokens[j] = state_slots[i].tokens[j];
var->state_slots[i].swizzle = state_slots[i].swizzle;
}
} else {
var->state_slots = NULL;
}
var->constant_initializer = constant_copy(ir->constant_initializer, var);
var->interface_type = ir->get_interface_type();
if (var->data.mode == nir_var_local)
nir_function_impl_add_variable(impl, var);
else
nir_shader_add_variable(shader, var);
_mesa_hash_table_insert(var_table, ir, var);
this->var = var;
}
ir_visitor_status
nir_function_visitor::visit_enter(ir_function *ir)
{
foreach_in_list(ir_function_signature, sig, &ir->signatures) {
visitor->create_function(sig);
}
return visit_continue_with_parent;
}
void
nir_visitor::create_function(ir_function_signature *ir)
{
if (ir->is_intrinsic())
return;
nir_function *func = nir_function_create(shader, ir->function_name());
assert(ir->parameters.is_empty());
assert(ir->return_type == glsl_type::void_type);
_mesa_hash_table_insert(this->overload_table, ir, func);
}
void
nir_visitor::visit(ir_function *ir)
{
foreach_in_list(ir_function_signature, sig, &ir->signatures)
sig->accept(this);
}
void
nir_visitor::visit(ir_function_signature *ir)
{
if (ir->is_intrinsic())
return;
struct hash_entry *entry =
_mesa_hash_table_search(this->overload_table, ir);
assert(entry);
nir_function *func = (nir_function *) entry->data;
if (ir->is_defined) {
nir_function_impl *impl = nir_function_impl_create(func);
this->impl = impl;
assert(strcmp(func->name, "main") == 0);
assert(ir->parameters.is_empty());
assert(func->return_type == glsl_type::void_type);
this->is_global = false;
nir_builder_init(&b, impl);
b.cursor = nir_after_cf_list(&impl->body);
visit_exec_list(&ir->body, this);
this->is_global = true;
} else {
func->impl = NULL;
}
}
void
nir_visitor::visit(ir_loop *ir)
{
nir_push_loop(&b);
visit_exec_list(&ir->body_instructions, this);
nir_pop_loop(&b, NULL);
}
void
nir_visitor::visit(ir_if *ir)
{
nir_push_if(&b, evaluate_rvalue(ir->condition));
visit_exec_list(&ir->then_instructions, this);
nir_push_else(&b, NULL);
visit_exec_list(&ir->else_instructions, this);
nir_pop_if(&b, NULL);
}
void
nir_visitor::visit(ir_discard *ir)
{
/*
* discards aren't treated as control flow, because before we lower them
* they can appear anywhere in the shader and the stuff after them may still
* be executed (yay, crazy GLSL rules!). However, after lowering, all the
* discards will be immediately followed by a return.
*/
nir_intrinsic_instr *discard;
if (ir->condition) {
discard = nir_intrinsic_instr_create(this->shader,
nir_intrinsic_discard_if);
discard->src[0] =
nir_src_for_ssa(evaluate_rvalue(ir->condition));
} else {
discard = nir_intrinsic_instr_create(this->shader, nir_intrinsic_discard);
}
nir_builder_instr_insert(&b, &discard->instr);
}
void
nir_visitor::visit(ir_emit_vertex *ir)
{
nir_intrinsic_instr *instr =
nir_intrinsic_instr_create(this->shader, nir_intrinsic_emit_vertex);
nir_intrinsic_set_stream_id(instr, ir->stream_id());
nir_builder_instr_insert(&b, &instr->instr);
}
void
nir_visitor::visit(ir_end_primitive *ir)
{
nir_intrinsic_instr *instr =
nir_intrinsic_instr_create(this->shader, nir_intrinsic_end_primitive);
nir_intrinsic_set_stream_id(instr, ir->stream_id());
nir_builder_instr_insert(&b, &instr->instr);
}
void
nir_visitor::visit(ir_loop_jump *ir)
{
nir_jump_type type;
switch (ir->mode) {
case ir_loop_jump::jump_break:
type = nir_jump_break;
break;
case ir_loop_jump::jump_continue:
type = nir_jump_continue;
break;
default:
unreachable("not reached");
}
nir_jump_instr *instr = nir_jump_instr_create(this->shader, type);
nir_builder_instr_insert(&b, &instr->instr);
}
void
nir_visitor::visit(ir_return *ir)
{
if (ir->value != NULL) {
nir_intrinsic_instr *copy =
nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
copy->variables[0] = nir_deref_var_create(copy, this->impl->return_var);
copy->variables[1] = evaluate_deref(©->instr, ir->value);
}
nir_jump_instr *instr = nir_jump_instr_create(this->shader, nir_jump_return);
nir_builder_instr_insert(&b, &instr->instr);
}
void
nir_visitor::visit(ir_call *ir)
{
if (ir->callee->is_intrinsic()) {
nir_intrinsic_op op;
switch (ir->callee->intrinsic_id) {
case ir_intrinsic_atomic_counter_read:
op = nir_intrinsic_atomic_counter_read_var;
break;
case ir_intrinsic_atomic_counter_increment:
op = nir_intrinsic_atomic_counter_inc_var;
break;
case ir_intrinsic_atomic_counter_predecrement:
op = nir_intrinsic_atomic_counter_dec_var;
break;
case ir_intrinsic_atomic_counter_add:
op = nir_intrinsic_atomic_counter_add_var;
break;
case ir_intrinsic_atomic_counter_and:
op = nir_intrinsic_atomic_counter_and_var;
break;
case ir_intrinsic_atomic_counter_or:
op = nir_intrinsic_atomic_counter_or_var;
break;
case ir_intrinsic_atomic_counter_xor:
op = nir_intrinsic_atomic_counter_xor_var;
break;
case ir_intrinsic_atomic_counter_min:
op = nir_intrinsic_atomic_counter_min_var;
break;
case ir_intrinsic_atomic_counter_max:
op = nir_intrinsic_atomic_counter_max_var;
break;
case ir_intrinsic_atomic_counter_exchange:
op = nir_intrinsic_atomic_counter_exchange_var;
break;
case ir_intrinsic_atomic_counter_comp_swap:
op = nir_intrinsic_atomic_counter_comp_swap_var;
break;
case ir_intrinsic_image_load:
op = nir_intrinsic_image_load;
break;
case ir_intrinsic_image_store:
op = nir_intrinsic_image_store;
break;
case ir_intrinsic_image_atomic_add:
op = nir_intrinsic_image_atomic_add;
break;
case ir_intrinsic_image_atomic_min:
op = nir_intrinsic_image_atomic_min;
break;
case ir_intrinsic_image_atomic_max:
op = nir_intrinsic_image_atomic_max;
break;
case ir_intrinsic_image_atomic_and:
op = nir_intrinsic_image_atomic_and;
break;
case ir_intrinsic_image_atomic_or:
op = nir_intrinsic_image_atomic_or;
break;
case ir_intrinsic_image_atomic_xor:
op = nir_intrinsic_image_atomic_xor;
break;
case ir_intrinsic_image_atomic_exchange:
op = nir_intrinsic_image_atomic_exchange;
break;
case ir_intrinsic_image_atomic_comp_swap:
op = nir_intrinsic_image_atomic_comp_swap;
break;
case ir_intrinsic_memory_barrier:
op = nir_intrinsic_memory_barrier;
break;
case ir_intrinsic_image_size:
op = nir_intrinsic_image_size;
break;
case ir_intrinsic_image_samples:
op = nir_intrinsic_image_samples;
break;
case ir_intrinsic_ssbo_store:
op = nir_intrinsic_store_ssbo;
break;
case ir_intrinsic_ssbo_load:
op = nir_intrinsic_load_ssbo;
break;
case ir_intrinsic_ssbo_atomic_add:
op = nir_intrinsic_ssbo_atomic_add;
break;
case ir_intrinsic_ssbo_atomic_and:
op = nir_intrinsic_ssbo_atomic_and;
break;
case ir_intrinsic_ssbo_atomic_or:
op = nir_intrinsic_ssbo_atomic_or;
break;
case ir_intrinsic_ssbo_atomic_xor:
op = nir_intrinsic_ssbo_atomic_xor;
break;
case ir_intrinsic_ssbo_atomic_min:
assert(ir->return_deref);
if (ir->return_deref->type == glsl_type::int_type)
op = nir_intrinsic_ssbo_atomic_imin;
else if (ir->return_deref->type == glsl_type::uint_type)
op = nir_intrinsic_ssbo_atomic_umin;
else
unreachable("Invalid type");
break;
case ir_intrinsic_ssbo_atomic_max:
assert(ir->return_deref);
if (ir->return_deref->type == glsl_type::int_type)
op = nir_intrinsic_ssbo_atomic_imax;
else if (ir->return_deref->type == glsl_type::uint_type)
op = nir_intrinsic_ssbo_atomic_umax;
else
unreachable("Invalid type");
break;
case ir_intrinsic_ssbo_atomic_exchange:
op = nir_intrinsic_ssbo_atomic_exchange;
break;
case ir_intrinsic_ssbo_atomic_comp_swap:
op = nir_intrinsic_ssbo_atomic_comp_swap;
break;
case ir_intrinsic_shader_clock:
op = nir_intrinsic_shader_clock;
break;
case ir_intrinsic_group_memory_barrier:
op = nir_intrinsic_group_memory_barrier;
break;
case ir_intrinsic_memory_barrier_atomic_counter:
op = nir_intrinsic_memory_barrier_atomic_counter;
break;
case ir_intrinsic_memory_barrier_buffer:
op = nir_intrinsic_memory_barrier_buffer;
break;
case ir_intrinsic_memory_barrier_image:
op = nir_intrinsic_memory_barrier_image;
break;
case ir_intrinsic_memory_barrier_shared:
op = nir_intrinsic_memory_barrier_shared;
break;
case ir_intrinsic_shared_load:
op = nir_intrinsic_load_shared;
break;
case ir_intrinsic_shared_store:
op = nir_intrinsic_store_shared;
break;
case ir_intrinsic_shared_atomic_add:
op = nir_intrinsic_shared_atomic_add;
break;
case ir_intrinsic_shared_atomic_and:
op = nir_intrinsic_shared_atomic_and;
break;
case ir_intrinsic_shared_atomic_or:
op = nir_intrinsic_shared_atomic_or;
break;
case ir_intrinsic_shared_atomic_xor:
op = nir_intrinsic_shared_atomic_xor;
break;
case ir_intrinsic_shared_atomic_min:
assert(ir->return_deref);
if (ir->return_deref->type == glsl_type::int_type)
op = nir_intrinsic_shared_atomic_imin;
else if (ir->return_deref->type == glsl_type::uint_type)
op = nir_intrinsic_shared_atomic_umin;
else
unreachable("Invalid type");
break;
case ir_intrinsic_shared_atomic_max:
assert(ir->return_deref);
if (ir->return_deref->type == glsl_type::int_type)
op = nir_intrinsic_shared_atomic_imax;
else if (ir->return_deref->type == glsl_type::uint_type)
op = nir_intrinsic_shared_atomic_umax;
else
unreachable("Invalid type");
break;
case ir_intrinsic_shared_atomic_exchange:
op = nir_intrinsic_shared_atomic_exchange;
break;
case ir_intrinsic_shared_atomic_comp_swap:
op = nir_intrinsic_shared_atomic_comp_swap;
break;
case ir_intrinsic_vote_any:
op = nir_intrinsic_vote_any;
break;
case ir_intrinsic_vote_all:
op = nir_intrinsic_vote_all;
break;
case ir_intrinsic_vote_eq:
op = nir_intrinsic_vote_eq;
break;
case ir_intrinsic_ballot:
op = nir_intrinsic_ballot;
break;
case ir_intrinsic_read_invocation:
op = nir_intrinsic_read_invocation;
break;
case ir_intrinsic_read_first_invocation:
op = nir_intrinsic_read_first_invocation;
break;
default:
unreachable("not reached");
}
nir_intrinsic_instr *instr = nir_intrinsic_instr_create(shader, op);
nir_dest *dest = &instr->dest;
switch (op) {
case nir_intrinsic_atomic_counter_read_var:
case nir_intrinsic_atomic_counter_inc_var:
case nir_intrinsic_atomic_counter_dec_var:
case nir_intrinsic_atomic_counter_add_var:
case nir_intrinsic_atomic_counter_min_var:
case nir_intrinsic_atomic_counter_max_var:
case nir_intrinsic_atomic_counter_and_var:
case nir_intrinsic_atomic_counter_or_var:
case nir_intrinsic_atomic_counter_xor_var:
case nir_intrinsic_atomic_counter_exchange_var:
case nir_intrinsic_atomic_counter_comp_swap_var: {
/* Set the counter variable dereference. */
exec_node *param = ir->actual_parameters.get_head();
ir_dereference *counter = (ir_dereference *)param;
instr->variables[0] = evaluate_deref(&instr->instr, counter);
param = param->get_next();
/* Set the intrinsic destination. */
if (ir->return_deref) {
nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
}
/* Set the intrinsic parameters. */
if (!param->is_tail_sentinel()) {
instr->src[0] =
nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
param = param->get_next();
}
if (!param->is_tail_sentinel()) {
instr->src[1] =
nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
param = param->get_next();
}
nir_builder_instr_insert(&b, &instr->instr);
break;
}
case nir_intrinsic_image_load:
case nir_intrinsic_image_store:
case nir_intrinsic_image_atomic_add:
case nir_intrinsic_image_atomic_min:
case nir_intrinsic_image_atomic_max:
case nir_intrinsic_image_atomic_and:
case nir_intrinsic_image_atomic_or:
case nir_intrinsic_image_atomic_xor:
case nir_intrinsic_image_atomic_exchange:
case nir_intrinsic_image_atomic_comp_swap:
case nir_intrinsic_image_samples:
case nir_intrinsic_image_size: {
nir_ssa_undef_instr *instr_undef =
nir_ssa_undef_instr_create(shader, 1, 32);
nir_builder_instr_insert(&b, &instr_undef->instr);
/* Set the image variable dereference. */
exec_node *param = ir->actual_parameters.get_head();
ir_dereference *image = (ir_dereference *)param;
const glsl_type *type =
image->variable_referenced()->type->without_array();
instr->variables[0] = evaluate_deref(&instr->instr, image);
param = param->get_next();
/* Set the intrinsic destination. */
if (ir->return_deref) {
unsigned num_components = ir->return_deref->type->vector_elements;
if (instr->intrinsic == nir_intrinsic_image_size)
instr->num_components = num_components;
nir_ssa_dest_init(&instr->instr, &instr->dest,
num_components, 32, NULL);
}
if (op == nir_intrinsic_image_size ||
op == nir_intrinsic_image_samples) {
nir_builder_instr_insert(&b, &instr->instr);
break;
}
/* Set the address argument, extending the coordinate vector to four
* components.
*/
nir_ssa_def *src_addr =
evaluate_rvalue((ir_dereference *)param);
nir_ssa_def *srcs[4];
for (int i = 0; i < 4; i++) {
if (i < type->coordinate_components())
srcs[i] = nir_channel(&b, src_addr, i);
else
srcs[i] = &instr_undef->def;
}
instr->src[0] = nir_src_for_ssa(nir_vec(&b, srcs, 4));
param = param->get_next();
/* Set the sample argument, which is undefined for single-sample
* images.
*/
if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
instr->src[1] =
nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
param = param->get_next();
} else {
instr->src[1] = nir_src_for_ssa(&instr_undef->def);
}
/* Set the intrinsic parameters. */
if (!param->is_tail_sentinel()) {
instr->src[2] =
nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
param = param->get_next();
}
if (!param->is_tail_sentinel()) {
instr->src[3] =
nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
param = param->get_next();
}
nir_builder_instr_insert(&b, &instr->instr);
break;
}
case nir_intrinsic_memory_barrier:
case nir_intrinsic_group_memory_barrier:
case nir_intrinsic_memory_barrier_atomic_counter:
case nir_intrinsic_memory_barrier_buffer:
case nir_intrinsic_memory_barrier_image:
case nir_intrinsic_memory_barrier_shared:
nir_builder_instr_insert(&b, &instr->instr);
break;
case nir_intrinsic_shader_clock:
nir_ssa_dest_init(&instr->instr, &instr->dest, 2, 32, NULL);
instr->num_components = 2;
nir_builder_instr_insert(&b, &instr->instr);
break;
case nir_intrinsic_store_ssbo: {
exec_node *param = ir->actual_parameters.get_head();
ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
param = param->get_next();
ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
param = param->get_next();
ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
param = param->get_next();
ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
assert(write_mask);
instr->src[0] = nir_src_for_ssa(evaluate_rvalue(val));
instr->src[1] = nir_src_for_ssa(evaluate_rvalue(block));
instr->src[2] = nir_src_for_ssa(evaluate_rvalue(offset));
nir_intrinsic_set_write_mask(instr, write_mask->value.u[0]);
instr->num_components = val->type->vector_elements;
nir_builder_instr_insert(&b, &instr->instr);
break;
}
case nir_intrinsic_load_ssbo: {
exec_node *param = ir->actual_parameters.get_head();
ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
param = param->get_next();
ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
instr->src[0] = nir_src_for_ssa(evaluate_rvalue(block));
instr->src[1] = nir_src_for_ssa(evaluate_rvalue(offset));
const glsl_type *type = ir->return_deref->var->type;
instr->num_components = type->vector_elements;
/* Setup destination register */
unsigned bit_size = glsl_get_bit_size(type);
nir_ssa_dest_init(&instr->instr, &instr->dest,
type->vector_elements, bit_size, NULL);
/* Insert the created nir instruction now since in the case of boolean
* result we will need to emit another instruction after it
*/
nir_builder_instr_insert(&b, &instr->instr);
/*
* In SSBO/UBO's, a true boolean value is any non-zero value, but we
* consider a true boolean to be ~0. Fix this up with a != 0
* comparison.
*/
if (type->is_boolean()) {
nir_alu_instr *load_ssbo_compare =
nir_alu_instr_create(shader, nir_op_ine);
load_ssbo_compare->src[0].src.is_ssa = true;
load_ssbo_compare->src[0].src.ssa = &instr->dest.ssa;
load_ssbo_compare->src[1].src =
nir_src_for_ssa(nir_imm_int(&b, 0));
for (unsigned i = 0; i < type->vector_elements; i++)
load_ssbo_compare->src[1].swizzle[i] = 0;
nir_ssa_dest_init(&load_ssbo_compare->instr,
&load_ssbo_compare->dest.dest,
type->vector_elements, bit_size, NULL);
load_ssbo_compare->dest.write_mask = (1 << type->vector_elements) - 1;
nir_builder_instr_insert(&b, &load_ssbo_compare->instr);
dest = &load_ssbo_compare->dest.dest;
}
break;
}
case nir_intrinsic_ssbo_atomic_add:
case nir_intrinsic_ssbo_atomic_imin:
case nir_intrinsic_ssbo_atomic_umin:
case nir_intrinsic_ssbo_atomic_imax:
case nir_intrinsic_ssbo_atomic_umax:
case nir_intrinsic_ssbo_atomic_and:
case nir_intrinsic_ssbo_atomic_or:
case nir_intrinsic_ssbo_atomic_xor:
case nir_intrinsic_ssbo_atomic_exchange:
case nir_intrinsic_ssbo_atomic_comp_swap: {
int param_count = ir->actual_parameters.length();
assert(param_count == 3 || param_count == 4);
/* Block index */
exec_node *param = ir->actual_parameters.get_head();
ir_instruction *inst = (ir_instruction *) param;
instr->src[0] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
/* Offset */
param = param->get_next();
inst = (ir_instruction *) param;
instr->src[1] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
/* data1 parameter (this is always present) */
param = param->get_next();
inst = (ir_instruction *) param;
instr->src[2] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
/* data2 parameter (only with atomic_comp_swap) */
if (param_count == 4) {
assert(op == nir_intrinsic_ssbo_atomic_comp_swap);
param = param->get_next();
inst = (ir_instruction *) param;
instr->src[3] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
}
/* Atomic result */
assert(ir->return_deref);
nir_ssa_dest_init(&instr->instr, &instr->dest,
ir->return_deref->type->vector_elements, 32, NULL);
nir_builder_instr_insert(&b, &instr->instr);
break;
}
case nir_intrinsic_load_shared: {
exec_node *param = ir->actual_parameters.get_head();
ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
nir_intrinsic_set_base(instr, 0);
instr->src[0] = nir_src_for_ssa(evaluate_rvalue(offset));
const glsl_type *type = ir->return_deref->var->type;
instr->num_components = type->vector_elements;
/* Setup destination register */
unsigned bit_size = glsl_get_bit_size(type);
nir_ssa_dest_init(&instr->instr, &instr->dest,
type->vector_elements, bit_size, NULL);
nir_builder_instr_insert(&b, &instr->instr);
break;
}
case nir_intrinsic_store_shared: {
exec_node *param = ir->actual_parameters.get_head();
ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
param = param->get_next();
ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
param = param->get_next();
ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
assert(write_mask);
nir_intrinsic_set_base(instr, 0);
instr->src[1] = nir_src_for_ssa(evaluate_rvalue(offset));
nir_intrinsic_set_write_mask(instr, write_mask->value.u[0]);
instr->src[0] = nir_src_for_ssa(evaluate_rvalue(val));
instr->num_components = val->type->vector_elements;
nir_builder_instr_insert(&b, &instr->instr);
break;
}
case nir_intrinsic_shared_atomic_add:
case nir_intrinsic_shared_atomic_imin:
case nir_intrinsic_shared_atomic_umin:
case nir_intrinsic_shared_atomic_imax:
case nir_intrinsic_shared_atomic_umax:
case nir_intrinsic_shared_atomic_and:
case nir_intrinsic_shared_atomic_or:
case nir_intrinsic_shared_atomic_xor:
case nir_intrinsic_shared_atomic_exchange:
case nir_intrinsic_shared_atomic_comp_swap: {
int param_count = ir->actual_parameters.length();
assert(param_count == 2 || param_count == 3);
/* Offset */
exec_node *param = ir->actual_parameters.get_head();
ir_instruction *inst = (ir_instruction *) param;
instr->src[0] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
/* data1 parameter (this is always present) */
param = param->get_next();
inst = (ir_instruction *) param;
instr->src[1] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
/* data2 parameter (only with atomic_comp_swap) */
if (param_count == 3) {
assert(op == nir_intrinsic_shared_atomic_comp_swap);
param = param->get_next();
inst = (ir_instruction *) param;
instr->src[2] =
nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
}
/* Atomic result */
assert(ir->return_deref);
unsigned bit_size = glsl_get_bit_size(ir->return_deref->type);
nir_ssa_dest_init(&instr->instr, &instr->dest,
ir->return_deref->type->vector_elements,
bit_size, NULL);
nir_builder_instr_insert(&b, &instr->instr);
break;
}
case nir_intrinsic_vote_any:
case nir_intrinsic_vote_all:
case nir_intrinsic_vote_eq: {
nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
instr->variables[0] = evaluate_deref(&instr->instr, ir->return_deref);
ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
nir_builder_instr_insert(&b, &instr->instr);
break;
}
case nir_intrinsic_ballot: {
nir_ssa_dest_init(&instr->instr, &instr->dest,
ir->return_deref->type->vector_elements, 64, NULL);
ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
nir_builder_instr_insert(&b, &instr->instr);
break;
}
case nir_intrinsic_read_invocation: {
nir_ssa_dest_init(&instr->instr, &instr->dest,
ir->return_deref->type->vector_elements, 32, NULL);
instr->num_components = ir->return_deref->type->vector_elements;
ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
ir_rvalue *invocation = (ir_rvalue *) ir->actual_parameters.get_head()->next;
instr->src[1] = nir_src_for_ssa(evaluate_rvalue(invocation));
nir_builder_instr_insert(&b, &instr->instr);
break;
}
case nir_intrinsic_read_first_invocation: {
nir_ssa_dest_init(&instr->instr, &instr->dest,
ir->return_deref->type->vector_elements, 32, NULL);
instr->num_components = ir->return_deref->type->vector_elements;
ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
nir_builder_instr_insert(&b, &instr->instr);
break;
}
default:
unreachable("not reached");
}
if (ir->return_deref) {
nir_intrinsic_instr *store_instr =
nir_intrinsic_instr_create(shader, nir_intrinsic_store_var);
store_instr->num_components = ir->return_deref->type->vector_elements;
nir_intrinsic_set_write_mask(store_instr,
(1 << store_instr->num_components) - 1);
store_instr->variables[0] =
evaluate_deref(&store_instr->instr, ir->return_deref);
store_instr->src[0] = nir_src_for_ssa(&dest->ssa);
nir_builder_instr_insert(&b, &store_instr->instr);
}
return;
}
struct hash_entry *entry =
_mesa_hash_table_search(this->overload_table, ir->callee);
assert(entry);
nir_function *callee = (nir_function *) entry->data;
nir_call_instr *instr = nir_call_instr_create(this->shader, callee);
unsigned i = 0;
foreach_in_list(ir_dereference, param, &ir->actual_parameters) {
instr->params[i] = evaluate_deref(&instr->instr, param);
i++;
}
instr->return_deref = evaluate_deref(&instr->instr, ir->return_deref);
nir_builder_instr_insert(&b, &instr->instr);
}
void
nir_visitor::visit(ir_assignment *ir)
{
unsigned num_components = ir->lhs->type->vector_elements;
b.exact = ir->lhs->variable_referenced()->data.invariant ||
ir->lhs->variable_referenced()->data.precise;
if ((ir->rhs->as_dereference() || ir->rhs->as_constant()) &&
(ir->write_mask == (1 << num_components) - 1 || ir->write_mask == 0)) {
/* We're doing a plain-as-can-be copy, so emit a copy_var */
nir_intrinsic_instr *copy =
nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
copy->variables[0] = evaluate_deref(©->instr, ir->lhs);
copy->variables[1] = evaluate_deref(©->instr, ir->rhs);
if (ir->condition) {
nir_push_if(&b, evaluate_rvalue(ir->condition));
nir_builder_instr_insert(&b, ©->instr);
nir_pop_if(&b, NULL);
} else {
nir_builder_instr_insert(&b, ©->instr);
}
return;
}
assert(ir->rhs->type->is_scalar() || ir->rhs->type->is_vector());
ir->lhs->accept(this);
nir_deref_var *lhs_deref = this->deref_head;
nir_ssa_def *src = evaluate_rvalue(ir->rhs);
if (ir->write_mask != (1 << num_components) - 1 && ir->write_mask != 0) {
/* GLSL IR will give us the input to the write-masked assignment in a
* single packed vector. So, for example, if the writemask is xzw, then
* we have to swizzle x -> x, y -> z, and z -> w and get the y component
* from the load.
*/
unsigned swiz[4];
unsigned component = 0;
for (unsigned i = 0; i < 4; i++) {
swiz[i] = ir->write_mask & (1 << i) ? component++ : 0;
}
src = nir_swizzle(&b, src, swiz, num_components, !supports_ints);
}
nir_intrinsic_instr *store =
nir_intrinsic_instr_create(this->shader, nir_intrinsic_store_var);
store->num_components = ir->lhs->type->vector_elements;
nir_intrinsic_set_write_mask(store, ir->write_mask);
store->variables[0] = nir_deref_var_clone(lhs_deref, store);
store->src[0] = nir_src_for_ssa(src);
if (ir->condition) {
nir_push_if(&b, evaluate_rvalue(ir->condition));
nir_builder_instr_insert(&b, &store->instr);
nir_pop_if(&b, NULL);
} else {
nir_builder_instr_insert(&b, &store->instr);
}
}
/*
* Given an instruction, returns a pointer to its destination or NULL if there
* is no destination.
*
* Note that this only handles instructions we generate at this level.
*/
static nir_dest *
get_instr_dest(nir_instr *instr)
{
nir_alu_instr *alu_instr;
nir_intrinsic_instr *intrinsic_instr;
nir_tex_instr *tex_instr;
switch (instr->type) {
case nir_instr_type_alu:
alu_instr = nir_instr_as_alu(instr);
return &alu_instr->dest.dest;
case nir_instr_type_intrinsic:
intrinsic_instr = nir_instr_as_intrinsic(instr);
if (nir_intrinsic_infos[intrinsic_instr->intrinsic].has_dest)
return &intrinsic_instr->dest;
else
return NULL;
case nir_instr_type_tex:
tex_instr = nir_instr_as_tex(instr);
return &tex_instr->dest;
default:
unreachable("not reached");
}
return NULL;
}
void
nir_visitor::add_instr(nir_instr *instr, unsigned num_components,
unsigned bit_size)
{
nir_dest *dest = get_instr_dest(instr);
if (dest)
nir_ssa_dest_init(instr, dest, num_components, bit_size, NULL);
nir_builder_instr_insert(&b, instr);
if (dest) {
assert(dest->is_ssa);
this->result = &dest->ssa;
}
}
nir_ssa_def *
nir_visitor::evaluate_rvalue(ir_rvalue* ir)
{
ir->accept(this);
if (ir->as_dereference() || ir->as_constant()) {
/*
* A dereference is being used on the right hand side, which means we
* must emit a variable load.
*/
nir_intrinsic_instr *load_instr =
nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
load_instr->num_components = ir->type->vector_elements;
load_instr->variables[0] = this->deref_head;
ralloc_steal(load_instr, load_instr->variables[0]);
unsigned bit_size = glsl_get_bit_size(ir->type);
add_instr(&load_instr->instr, ir->type->vector_elements, bit_size);
}
return this->result;
}
static bool
type_is_float(glsl_base_type type)
{
return type == GLSL_TYPE_FLOAT || type == GLSL_TYPE_DOUBLE;
}
static bool
type_is_signed(glsl_base_type type)
{
return type == GLSL_TYPE_INT || type == GLSL_TYPE_INT64;
}
void
nir_visitor::visit(ir_expression *ir)
{
/* Some special cases */
switch (ir->operation) {
case ir_binop_ubo_load: {
nir_intrinsic_instr *load =
nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_ubo);
unsigned bit_size = glsl_get_bit_size(ir->type);
load->num_components = ir->type->vector_elements;
load->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[0]));
load->src[1] = nir_src_for_ssa(evaluate_rvalue(ir->operands[1]));
add_instr(&load->instr, ir->type->vector_elements, bit_size);
/*
* In UBO's, a true boolean value is any non-zero value, but we consider
* a true boolean to be ~0. Fix this up with a != 0 comparison.
*/
if (ir->type->is_boolean())
this->result = nir_ine(&b, &load->dest.ssa, nir_imm_int(&b, 0));
return;
}
case ir_unop_interpolate_at_centroid:
case ir_binop_interpolate_at_offset:
case ir_binop_interpolate_at_sample: {
ir_dereference *deref = ir->operands[0]->as_dereference();
ir_swizzle *swizzle = NULL;
if (!deref) {
/* the api does not allow a swizzle here, but the varying packing code
* may have pushed one into here.
*/
swizzle = ir->operands[0]->as_swizzle();
assert(swizzle);
deref = swizzle->val->as_dereference();
assert(deref);
}
deref->accept(this);
nir_intrinsic_op op;
if (this->deref_head->var->data.mode == nir_var_shader_in) {
switch (ir->operation) {
case ir_unop_interpolate_at_centroid:
op = nir_intrinsic_interp_var_at_centroid;
break;
case ir_binop_interpolate_at_offset:
op = nir_intrinsic_interp_var_at_offset;
break;
case ir_binop_interpolate_at_sample:
op = nir_intrinsic_interp_var_at_sample;
break;
default:
unreachable("Invalid interpolation intrinsic");
}
} else {
/* This case can happen if the vertex shader does not write the
* given varying. In this case, the linker will lower it to a
* global variable. Since interpolating a variable makes no
* sense, we'll just turn it into a load which will probably
* eventually end up as an SSA definition.
*/
assert(this->deref_head->var->data.mode == nir_var_global);
op = nir_intrinsic_load_var;
}
nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(shader, op);
intrin->num_components = deref->type->vector_elements;
intrin->variables[0] = this->deref_head;
ralloc_steal(intrin, intrin->variables[0]);
if (intrin->intrinsic == nir_intrinsic_interp_var_at_offset ||
intrin->intrinsic == nir_intrinsic_interp_var_at_sample)
intrin->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[1]));
unsigned bit_size = glsl_get_bit_size(deref->type);
add_instr(&intrin->instr, deref->type->vector_elements, bit_size);
if (swizzle) {
unsigned swiz[4] = {
swizzle->mask.x, swizzle->mask.y, swizzle->mask.z, swizzle->mask.w
};
result = nir_swizzle(&b, result, swiz,
swizzle->type->vector_elements, false);
}
return;
}
default:
break;
}
nir_ssa_def *srcs[4];
for (unsigned i = 0; i < ir->num_operands; i++)
srcs[i] = evaluate_rvalue(ir->operands[i]);
glsl_base_type types[4];
for (unsigned i = 0; i < ir->num_operands; i++)
if (supports_ints)
types[i] = ir->operands[i]->type->base_type;
else
types[i] = GLSL_TYPE_FLOAT;
glsl_base_type out_type;
if (supports_ints)
out_type = ir->type->base_type;
else
out_type = GLSL_TYPE_FLOAT;
switch (ir->operation) {
case ir_unop_bit_not: result = nir_inot(&b, srcs[0]); break;
case ir_unop_logic_not:
result = supports_ints ? nir_inot(&b, srcs[0]) : nir_fnot(&b, srcs[0]);
break;
case ir_unop_neg:
result = type_is_float(types[0]) ? nir_fneg(&b, srcs[0])
: nir_ineg(&b, srcs[0]);
break;
case ir_unop_abs:
result = type_is_float(types[0]) ? nir_fabs(&b, srcs[0])
: nir_iabs(&b, srcs[0]);
break;
case ir_unop_saturate:
assert(type_is_float(types[0]));
result = nir_fsat(&b, srcs[0]);
break;
case ir_unop_sign:
result = type_is_float(types[0]) ? nir_fsign(&b, srcs[0])
: nir_isign(&b, srcs[0]);
break;
case ir_unop_rcp: result = nir_frcp(&b, srcs[0]); break;
case ir_unop_rsq: result = nir_frsq(&b, srcs[0]); break;
case ir_unop_sqrt: result = nir_fsqrt(&b, srcs[0]); break;
case ir_unop_exp: unreachable("ir_unop_exp should have been lowered");
case ir_unop_log: unreachable("ir_unop_log should have been lowered");
case ir_unop_exp2: result = nir_fexp2(&b, srcs[0]); break;
case ir_unop_log2: result = nir_flog2(&b, srcs[0]); break;
case ir_unop_i2f:
result = supports_ints ? nir_i2f32(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
break;
case ir_unop_u2f:
result = supports_ints ? nir_u2f32(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
break;
case ir_unop_b2f:
result = supports_ints ? nir_b2f(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
break;
case ir_unop_f2i:
case ir_unop_f2u:
case ir_unop_f2b:
case ir_unop_i2b:
case ir_unop_b2i:
case ir_unop_b2i64:
case ir_unop_d2f:
case ir_unop_f2d:
case ir_unop_d2i:
case ir_unop_d2u:
case ir_unop_d2b:
case ir_unop_i2d:
case ir_unop_u2d:
case ir_unop_i642i:
case ir_unop_i642u:
case ir_unop_i642f:
case ir_unop_i642b:
case ir_unop_i642d:
case ir_unop_u642i:
case ir_unop_u642u:
case ir_unop_u642f:
case ir_unop_u642d:
case ir_unop_i2i64:
case ir_unop_u2i64:
case ir_unop_f2i64:
case ir_unop_d2i64:
case ir_unop_i2u64:
case ir_unop_u2u64:
case ir_unop_f2u64:
case ir_unop_d2u64:
case ir_unop_i2u:
case ir_unop_u2i:
case ir_unop_i642u64:
case ir_unop_u642i64: {
nir_alu_type src_type = nir_get_nir_type_for_glsl_base_type(types[0]);
nir_alu_type dst_type = nir_get_nir_type_for_glsl_base_type(out_type);
result = nir_build_alu(&b, nir_type_conversion_op(src_type, dst_type),
srcs[0], NULL, NULL, NULL);
/* b2i and b2f don't have fixed bit-size versions so the builder will
* just assume 32 and we have to fix it up here.
*/
result->bit_size = nir_alu_type_get_type_size(dst_type);
break;
}
case ir_unop_bitcast_i2f:
case ir_unop_bitcast_f2i:
case ir_unop_bitcast_u2f:
case ir_unop_bitcast_f2u:
case ir_unop_bitcast_i642d:
case ir_unop_bitcast_d2i64:
case ir_unop_bitcast_u642d:
case ir_unop_bitcast_d2u64:
case ir_unop_subroutine_to_int:
/* no-op */
result = nir_imov(&b, srcs[0]);
break;
case ir_unop_trunc: result = nir_ftrunc(&b, srcs[0]); break;
case ir_unop_ceil: result = nir_fceil(&b, srcs[0]); break;
case ir_unop_floor: result = nir_ffloor(&b, srcs[0]); break;
case ir_unop_fract: result = nir_ffract(&b, srcs[0]); break;
case ir_unop_round_even: result = nir_fround_even(&b, srcs[0]); break;
case ir_unop_sin: result = nir_fsin(&b, srcs[0]); break;
case ir_unop_cos: result = nir_fcos(&b, srcs[0]); break;
case ir_unop_dFdx: result = nir_fddx(&b, srcs[0]); break;
case ir_unop_dFdy: result = nir_fddy(&b, srcs[0]); break;
case ir_unop_dFdx_fine: result = nir_fddx_fine(&b, srcs[0]); break;
case ir_unop_dFdy_fine: result = nir_fddy_fine(&b, srcs[0]); break;
case ir_unop_dFdx_coarse: result = nir_fddx_coarse(&b, srcs[0]); break;
case ir_unop_dFdy_coarse: result = nir_fddy_coarse(&b, srcs[0]); break;
case ir_unop_pack_snorm_2x16:
result = nir_pack_snorm_2x16(&b, srcs[0]);
break;
case ir_unop_pack_snorm_4x8:
result = nir_pack_snorm_4x8(&b, srcs[0]);
break;
case ir_unop_pack_unorm_2x16:
result = nir_pack_unorm_2x16(&b, srcs[0]);
break;
case ir_unop_pack_unorm_4x8:
result = nir_pack_unorm_4x8(&b, srcs[0]);
break;
case ir_unop_pack_half_2x16:
result = nir_pack_half_2x16(&b, srcs[0]);
break;
case ir_unop_unpack_snorm_2x16:
result = nir_unpack_snorm_2x16(&b, srcs[0]);
break;
case ir_unop_unpack_snorm_4x8:
result = nir_unpack_snorm_4x8(&b, srcs[0]);
break;
case ir_unop_unpack_unorm_2x16:
result = nir_unpack_unorm_2x16(&b, srcs[0]);
break;
case ir_unop_unpack_unorm_4x8:
result = nir_unpack_unorm_4x8(&b, srcs[0]);
break;
case ir_unop_unpack_half_2x16:
result = nir_unpack_half_2x16(&b, srcs[0]);
break;
case ir_unop_pack_double_2x32:
case ir_unop_pack_int_2x32:
case ir_unop_pack_uint_2x32:
result = nir_pack_64_2x32(&b, srcs[0]);
break;
case ir_unop_unpack_double_2x32:
case ir_unop_unpack_int_2x32:
case ir_unop_unpack_uint_2x32:
result = nir_unpack_64_2x32(&b, srcs[0]);
break;
case ir_unop_bitfield_reverse:
result = nir_bitfield_reverse(&b, srcs[0]);
break;
case ir_unop_bit_count:
result = nir_bit_count(&b, srcs[0]);
break;
case ir_unop_find_msb:
switch (types[0]) {
case GLSL_TYPE_UINT:
result = nir_ufind_msb(&b, srcs[0]);
break;
case GLSL_TYPE_INT:
result = nir_ifind_msb(&b, srcs[0]);
break;
default:
unreachable("Invalid type for findMSB()");
}
break;
case ir_unop_find_lsb:
result = nir_find_lsb(&b, srcs[0]);
break;
case ir_unop_noise:
switch (ir->type->vector_elements) {
case 1:
switch (ir->operands[0]->type->vector_elements) {
case 1: result = nir_fnoise1_1(&b, srcs[0]); break;
case 2: result = nir_fnoise1_2(&b, srcs[0]); break;
case 3: result = nir_fnoise1_3(&b, srcs[0]); break;
case 4: result = nir_fnoise1_4(&b, srcs[0]); break;
default: unreachable("not reached");
}
break;
case 2:
switch (ir->operands[0]->type->vector_elements) {
case 1: result = nir_fnoise2_1(&b, srcs[0]); break;
case 2: result = nir_fnoise2_2(&b, srcs[0]); break;
case 3: result = nir_fnoise2_3(&b, srcs[0]); break;
case 4: result = nir_fnoise2_4(&b, srcs[0]); break;
default: unreachable("not reached");
}
break;
case 3:
switch (ir->operands[0]->type->vector_elements) {
case 1: result = nir_fnoise3_1(&b, srcs[0]); break;
case 2: result = nir_fnoise3_2(&b, srcs[0]); break;
case 3: result = nir_fnoise3_3(&b, srcs[0]); break;
case 4: result = nir_fnoise3_4(&b, srcs[0]); break;
default: unreachable("not reached");
}
break;
case 4:
switch (ir->operands[0]->type->vector_elements) {
case 1: result = nir_fnoise4_1(&b, srcs[0]); break;
case 2: result = nir_fnoise4_2(&b, srcs[0]); break;
case 3: result = nir_fnoise4_3(&b, srcs[0]); break;
case 4: result = nir_fnoise4_4(&b, srcs[0]); break;
default: unreachable("not reached");
}
break;
default:
unreachable("not reached");
}
break;
case ir_unop_get_buffer_size: {
nir_intrinsic_instr *load = nir_intrinsic_instr_create(
this->shader,
nir_intrinsic_get_buffer_size);
load->num_components = ir->type->vector_elements;
load->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[0]));
unsigned bit_size = glsl_get_bit_size(ir->type);
add_instr(&load->instr, ir->type->vector_elements, bit_size);
return;
}
case ir_binop_add:
result = type_is_float(out_type) ? nir_fadd(&b, srcs[0], srcs[1])
: nir_iadd(&b, srcs[0], srcs[1]);
break;
case ir_binop_sub:
result = type_is_float(out_type) ? nir_fsub(&b, srcs[0], srcs[1])
: nir_isub(&b, srcs[0], srcs[1]);
break;
case ir_binop_mul:
result = type_is_float(out_type) ? nir_fmul(&b, srcs[0], srcs[1])
: nir_imul(&b, srcs[0], srcs[1]);
break;
case ir_binop_div:
if (type_is_float(out_type))
result = nir_fdiv(&b, srcs[0], srcs[1]);
else if (type_is_signed(out_type))
result = nir_idiv(&b, srcs[0], srcs[1]);
else
result = nir_udiv(&b, srcs[0], srcs[1]);
break;
case ir_binop_mod:
result = type_is_float(out_type) ? nir_fmod(&b, srcs[0], srcs[1])
: nir_umod(&b, srcs[0], srcs[1]);
break;
case ir_binop_min:
if (type_is_float(out_type))
result = nir_fmin(&b, srcs[0], srcs[1]);
else if (type_is_signed(out_type))
result = nir_imin(&b, srcs[0], srcs[1]);
else
result = nir_umin(&b, srcs[0], srcs[1]);
break;
case ir_binop_max:
if (type_is_float(out_type))
result = nir_fmax(&b, srcs[0], srcs[1]);
else if (type_is_signed(out_type))
result = nir_imax(&b, srcs[0], srcs[1]);
else
result = nir_umax(&b, srcs[0], srcs[1]);
break;
case ir_binop_pow: result = nir_fpow(&b, srcs[0], srcs[1]); break;
case ir_binop_bit_and: result = nir_iand(&b, srcs[0], srcs[1]); break;
case ir_binop_bit_or: result = nir_ior(&b, srcs[0], srcs[1]); break;
case ir_binop_bit_xor: result = nir_ixor(&b, srcs[0], srcs[1]); break;
case ir_binop_logic_and:
result = supports_ints ? nir_iand(&b, srcs[0], srcs[1])
: nir_fand(&b, srcs[0], srcs[1]);
break;
case ir_binop_logic_or:
result = supports_ints ? nir_ior(&b, srcs[0], srcs[1])
: nir_for(&b, srcs[0], srcs[1]);
break;
case ir_binop_logic_xor:
result = supports_ints ? nir_ixor(&b, srcs[0], srcs[1])
: nir_fxor(&b, srcs[0], srcs[1]);
break;
case ir_binop_lshift: result = nir_ishl(&b, srcs[0], srcs[1]); break;
case ir_binop_rshift:
result = (type_is_signed(out_type)) ? nir_ishr(&b, srcs[0], srcs[1])
: nir_ushr(&b, srcs[0], srcs[1]);
break;
case ir_binop_imul_high:
result = (out_type == GLSL_TYPE_INT) ? nir_imul_high(&b, srcs[0], srcs[1])
: nir_umul_high(&b, srcs[0], srcs[1]);
break;
case ir_binop_carry: result = nir_uadd_carry(&b, srcs[0], srcs[1]); break;
case ir_binop_borrow: result = nir_usub_borrow(&b, srcs[0], srcs[1]); break;
case ir_binop_less:
if (supports_ints) {
if (type_is_float(types[0]))
result = nir_flt(&b, srcs[0], srcs[1]);
else if (type_is_signed(types[0]))
result = nir_ilt(&b, srcs[0], srcs[1]);
else
result = nir_ult(&b, srcs[0], srcs[1]);
} else {
result = nir_slt(&b, srcs[0], srcs[1]);
}
break;
case ir_binop_greater:
if (supports_ints) {
if (type_is_float(types[0]))
result = nir_flt(&b, srcs[1], srcs[0]);
else if (type_is_signed(types[0]))
result = nir_ilt(&b, srcs[1], srcs[0]);
else
result = nir_ult(&b, srcs[1], srcs[0]);
} else {
result = nir_slt(&b, srcs[1], srcs[0]);
}
break;
case ir_binop_lequal:
if (supports_ints) {
if (type_is_float(types[0]))
result = nir_fge(&b, srcs[1], srcs[0]);
else if (type_is_signed(types[0]))
result = nir_ige(&b, srcs[1], srcs[0]);
else
result = nir_uge(&b, srcs[1], srcs[0]);
} else {
result = nir_slt(&b, srcs[1], srcs[0]);
}
break;
case ir_binop_gequal:
if (supports_ints) {
if (type_is_float(types[0]))
result = nir_fge(&b, srcs[0], srcs[1]);
else if (type_is_signed(types[0]))
result = nir_ige(&b, srcs[0], srcs[1]);
else
result = nir_uge(&b, srcs[0], srcs[1]);
} else {
result = nir_slt(&b, srcs[0], srcs[1]);
}
break;
case ir_binop_equal:
if (supports_ints) {
if (type_is_float(types[0]))
result = nir_feq(&b, srcs[0], srcs[1]);
else
result = nir_ieq(&b, srcs[0], srcs[1]);
} else {
result = nir_seq(&b, srcs[0], srcs[1]);
}
break;
case ir_binop_nequal:
if (supports_ints) {
if (type_is_float(types[0]))
result = nir_fne(&b, srcs[0], srcs[1]);
else
result = nir_ine(&b, srcs[0], srcs[1]);
} else {
result = nir_sne(&b, srcs[0], srcs[1]);
}
break;
case ir_binop_all_equal:
if (supports_ints) {
if (type_is_float(types[0])) {
switch (ir->operands[0]->type->vector_elements) {
case 1: result = nir_feq(&b, srcs[0], srcs[1]); break;
case 2: result = nir_ball_fequal2(&b, srcs[0], srcs[1]); break;
case 3: result = nir_ball_fequal3(&b, srcs[0], srcs[1]); break;
case 4: result = nir_ball_fequal4(&b, srcs[0], srcs[1]); break;
default:
unreachable("not reached");
}
} else {
switch (ir->operands[0]->type->vector_elements) {
case 1: result = nir_ieq(&b, srcs[0], srcs[1]); break;
case 2: result = nir_ball_iequal2(&b, srcs[0], srcs[1]); break;
case 3: result = nir_ball_iequal3(&b, srcs[0], srcs[1]); break;
case 4: result = nir_ball_iequal4(&b, srcs[0], srcs[1]); break;
default:
unreachable("not reached");
}
}
} else {
switch (ir->operands[0]->type->vector_elements) {
case 1: result = nir_seq(&b, srcs[0], srcs[1]); break;
case 2: result = nir_fall_equal2(&b, srcs[0], srcs[1]); break;
case 3: result = nir_fall_equal3(&b, srcs[0], srcs[1]); break;
case 4: result = nir_fall_equal4(&b, srcs[0], srcs[1]); break;
default:
unreachable("not reached");
}
}
break;
case ir_binop_any_nequal:
if (supports_ints) {
if (type_is_float(types[0])) {
switch (ir->operands[0]->type->vector_elements) {
case 1: result = nir_fne(&b, srcs[0], srcs[1]); break;
case 2: result = nir_bany_fnequal2(&b, srcs[0], srcs[1]); break;
case 3: result = nir_bany_fnequal3(&b, srcs[0], srcs[1]); break;
case 4: result = nir_bany_fnequal4(&b, srcs[0], srcs[1]); break;
default:
unreachable("not reached");
}
} else {
switch (ir->operands[0]->type->vector_elements) {
case 1: result = nir_ine(&b, srcs[0], srcs[1]); break;
case 2: result = nir_bany_inequal2(&b, srcs[0], srcs[1]); break;
case 3: result = nir_bany_inequal3(&b, srcs[0], srcs[1]); break;
case 4: result = nir_bany_inequal4(&b, srcs[0], srcs[1]); break;
default:
unreachable("not reached");
}
}
} else {
switch (ir->operands[0]->type->vector_elements) {
case 1: result = nir_sne(&b, srcs[0], srcs[1]); break;
case 2: result = nir_fany_nequal2(&b, srcs[0], srcs[1]); break;
case 3: result = nir_fany_nequal3(&b, srcs[0], srcs[1]); break;
case 4: result = nir_fany_nequal4(&b, srcs[0], srcs[1]); break;
default:
unreachable("not reached");
}
}
break;
case ir_binop_dot:
switch (ir->operands[0]->type->vector_elements) {
case 2: result = nir_fdot2(&b, srcs[0], srcs[1]); break;
case 3: result = nir_fdot3(&b, srcs[0], srcs[1]); break;
case 4: result = nir_fdot4(&b, srcs[0], srcs[1]); break;
default:
unreachable("not reached");
}
break;
case ir_binop_ldexp: result = nir_ldexp(&b, srcs[0], srcs[1]); break;
case ir_triop_fma:
result = nir_ffma(&b, srcs[0], srcs[1], srcs[2]);
break;
case ir_triop_lrp:
result = nir_flrp(&b, srcs[0], srcs[1], srcs[2]);
break;
case ir_triop_csel:
if (supports_ints)
result = nir_bcsel(&b, srcs[0], srcs[1], srcs[2]);
else
result = nir_fcsel(&b, srcs[0], srcs[1], srcs[2]);
break;
case ir_triop_bitfield_extract:
result = (out_type == GLSL_TYPE_INT) ?
nir_ibitfield_extract(&b, srcs[0], srcs[1], srcs[2]) :
nir_ubitfield_extract(&b, srcs[0], srcs[1], srcs[2]);
break;
case ir_quadop_bitfield_insert:
result = nir_bitfield_insert(&b, srcs[0], srcs[1], srcs[2], srcs[3]);
break;
case ir_quadop_vector:
result = nir_vec(&b, srcs, ir->type->vector_elements);
break;
default:
unreachable("not reached");
}
}
void
nir_visitor::visit(ir_swizzle *ir)
{
unsigned swizzle[4] = { ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w };
result = nir_swizzle(&b, evaluate_rvalue(ir->val), swizzle,
ir->type->vector_elements, !supports_ints);
}
void
nir_visitor::visit(ir_texture *ir)
{
unsigned num_srcs;
nir_texop op;
switch (ir->op) {
case ir_tex:
op = nir_texop_tex;
num_srcs = 1; /* coordinate */
break;
case ir_txb:
case ir_txl:
op = (ir->op == ir_txb) ? nir_texop_txb : nir_texop_txl;
num_srcs = 2; /* coordinate, bias/lod */
break;
case ir_txd:
op = nir_texop_txd; /* coordinate, dPdx, dPdy */
num_srcs = 3;
break;
case ir_txf:
op = nir_texop_txf;
if (ir->lod_info.lod != NULL)
num_srcs = 2; /* coordinate, lod */
else
num_srcs = 1; /* coordinate */
break;
case ir_txf_ms:
op = nir_texop_txf_ms;
num_srcs = 2; /* coordinate, sample_index */
break;
case ir_txs:
op = nir_texop_txs;
if (ir->lod_info.lod != NULL)
num_srcs = 1; /* lod */
else
num_srcs = 0;
break;
case ir_lod:
op = nir_texop_lod;
num_srcs = 1; /* coordinate */
break;
case ir_tg4:
op = nir_texop_tg4;
num_srcs = 1; /* coordinate */
break;
case ir_query_levels:
op = nir_texop_query_levels;
num_srcs = 0;
break;
case ir_texture_samples:
op = nir_texop_texture_samples;
num_srcs = 0;
break;
case ir_samples_identical:
op = nir_texop_samples_identical;
num_srcs = 1; /* coordinate */
break;
default:
unreachable("not reached");
}
if (ir->projector != NULL)
num_srcs++;
if (ir->shadow_comparator != NULL)
num_srcs++;
if (ir->offset != NULL)
num_srcs++;
nir_tex_instr *instr = nir_tex_instr_create(this->shader, num_srcs);
instr->op = op;
instr->sampler_dim =
(glsl_sampler_dim) ir->sampler->type->sampler_dimensionality;
instr->is_array = ir->sampler->type->sampler_array;
instr->is_shadow = ir->sampler->type->sampler_shadow;
if (instr->is_shadow)
instr->is_new_style_shadow = (ir->type->vector_elements == 1);
switch (ir->type->base_type) {
case GLSL_TYPE_FLOAT:
instr->dest_type = nir_type_float;
break;
case GLSL_TYPE_INT:
instr->dest_type = nir_type_int;
break;
case GLSL_TYPE_BOOL:
case GLSL_TYPE_UINT:
instr->dest_type = nir_type_uint;
break;
default:
unreachable("not reached");
}
instr->texture = evaluate_deref(&instr->instr, ir->sampler);
unsigned src_number = 0;
if (ir->coordinate != NULL) {
instr->coord_components = ir->coordinate->type->vector_elements;
instr->src[src_number].src =
nir_src_for_ssa(evaluate_rvalue(ir->coordinate));
instr->src[src_number].src_type = nir_tex_src_coord;
src_number++;
}
if (ir->projector != NULL) {
instr->src[src_number].src =
nir_src_for_ssa(evaluate_rvalue(ir->projector));
instr->src[src_number].src_type = nir_tex_src_projector;
src_number++;
}
if (ir->shadow_comparator != NULL) {
instr->src[src_number].src =
nir_src_for_ssa(evaluate_rvalue(ir->shadow_comparator));
instr->src[src_number].src_type = nir_tex_src_comparator;
src_number++;
}
if (ir->offset != NULL) {
/* we don't support multiple offsets yet */
assert(ir->offset->type->is_vector() || ir->offset->type->is_scalar());
instr->src[src_number].src =
nir_src_for_ssa(evaluate_rvalue(ir->offset));
instr->src[src_number].src_type = nir_tex_src_offset;
src_number++;
}
switch (ir->op) {
case ir_txb:
instr->src[src_number].src =
nir_src_for_ssa(evaluate_rvalue(ir->lod_info.bias));
instr->src[src_number].src_type = nir_tex_src_bias;
src_number++;
break;
case ir_txl:
case ir_txf:
case ir_txs:
if (ir->lod_info.lod != NULL) {
instr->src[src_number].src =
nir_src_for_ssa(evaluate_rvalue(ir->lod_info.lod));
instr->src[src_number].src_type = nir_tex_src_lod;
src_number++;
}
break;
case ir_txd:
instr->src[src_number].src =
nir_src_for_ssa(evaluate_rvalue(ir->lod_info.grad.dPdx));
instr->src[src_number].src_type = nir_tex_src_ddx;
src_number++;
instr->src[src_number].src =
nir_src_for_ssa(evaluate_rvalue(ir->lod_info.grad.dPdy));
instr->src[src_number].src_type = nir_tex_src_ddy;
src_number++;
break;
case ir_txf_ms:
instr->src[src_number].src =
nir_src_for_ssa(evaluate_rvalue(ir->lod_info.sample_index));
instr->src[src_number].src_type = nir_tex_src_ms_index;
src_number++;
break;
case ir_tg4:
instr->component = ir->lod_info.component->as_constant()->value.u[0];
break;
default:
break;
}
assert(src_number == num_srcs);
unsigned bit_size = glsl_get_bit_size(ir->type);
add_instr(&instr->instr, nir_tex_instr_dest_size(instr), bit_size);
}
void
nir_visitor::visit(ir_constant *ir)
{
/*
* We don't know if this variable is an array or struct that gets
* dereferenced, so do the safe thing an make it a variable with a
* constant initializer and return a dereference.
*/
nir_variable *var =
nir_local_variable_create(this->impl, ir->type, "const_temp");
var->data.read_only = true;
var->constant_initializer = constant_copy(ir, var);
this->deref_head = nir_deref_var_create(this->shader, var);
this->deref_tail = &this->deref_head->deref;
}
void
nir_visitor::visit(ir_dereference_variable *ir)
{
struct hash_entry *entry =
_mesa_hash_table_search(this->var_table, ir->var);
assert(entry);
nir_variable *var = (nir_variable *) entry->data;
nir_deref_var *deref = nir_deref_var_create(this->shader, var);
this->deref_head = deref;
this->deref_tail = &deref->deref;
}
void
nir_visitor::visit(ir_dereference_record *ir)
{
ir->record->accept(this);
int field_index = ir->field_idx;
assert(field_index >= 0);
nir_deref_struct *deref = nir_deref_struct_create(this->deref_tail, field_index);
deref->deref.type = ir->type;
this->deref_tail->child = &deref->deref;
this->deref_tail = &deref->deref;
}
void
nir_visitor::visit(ir_dereference_array *ir)
{
nir_deref_array *deref = nir_deref_array_create(this->shader);
deref->deref.type = ir->type;
ir_constant *const_index = ir->array_index->as_constant();
if (const_index != NULL) {
deref->deref_array_type = nir_deref_array_type_direct;
deref->base_offset = const_index->value.u[0];
} else {
deref->deref_array_type = nir_deref_array_type_indirect;
deref->indirect =
nir_src_for_ssa(evaluate_rvalue(ir->array_index));
}
ir->array->accept(this);
this->deref_tail->child = &deref->deref;
ralloc_steal(this->deref_tail, deref);
this->deref_tail = &deref->deref;
}
void
nir_visitor::visit(ir_barrier *)
{
nir_intrinsic_instr *instr =
nir_intrinsic_instr_create(this->shader, nir_intrinsic_barrier);
nir_builder_instr_insert(&b, &instr->instr);
}
|