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
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
|
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2008,2009,2010 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
*
*
* Authors:
* Robert Bragg <robert@linux.intel.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cogl.h"
#include "cogl-debug.h"
#include "cogl-internal.h"
#include "cogl-context-private.h"
#include "cogl-object.h"
#include "cogl-pipeline-private.h"
#include "cogl-pipeline-opengl-private.h"
#include "cogl-pipeline-state-private.h"
#include "cogl-texture-private.h"
#include "cogl-blend-string.h"
#include "cogl-journal-private.h"
#include "cogl-color-private.h"
#include "cogl-util.h"
#include "cogl-profile.h"
#include "cogl-depth-state-private.h"
#include <glib.h>
#include <glib/gprintf.h>
#include <string.h>
#ifndef GL_FUNC_ADD
#define GL_FUNC_ADD 0x8006
#endif
static CoglPipelineLayer *_cogl_pipeline_layer_copy (CoglPipelineLayer *layer);
static void _cogl_pipeline_free (CoglPipeline *tex);
static void _cogl_pipeline_layer_free (CoglPipelineLayer *layer);
static void _cogl_pipeline_add_layer_difference (CoglPipeline *pipeline,
CoglPipelineLayer *layer,
gboolean inc_n_layers);
static void recursively_free_layer_caches (CoglPipeline *pipeline);
static gboolean _cogl_pipeline_is_weak (CoglPipeline *pipeline);
const CoglPipelineFragend *_cogl_pipeline_fragends[COGL_PIPELINE_N_FRAGENDS];
const CoglPipelineVertend *_cogl_pipeline_vertends[COGL_PIPELINE_N_VERTENDS];
/* The 'MAX' here is so that we don't define an empty array when there
are no progends */
const CoglPipelineProgend *
_cogl_pipeline_progends[MAX (COGL_PIPELINE_N_PROGENDS, 1)];
#ifdef COGL_PIPELINE_FRAGEND_GLSL
#include "cogl-pipeline-fragend-glsl-private.h"
#endif
#ifdef COGL_PIPELINE_FRAGEND_ARBFP
#include "cogl-pipeline-fragend-arbfp-private.h"
#endif
#ifdef COGL_PIPELINE_FRAGEND_FIXED
#include "cogl-pipeline-fragend-fixed-private.h"
#endif
#ifdef COGL_PIPELINE_PROGEND_GLSL
#include "cogl-pipeline-progend-glsl-private.h"
#endif
#ifdef COGL_PIPELINE_VERTEND_GLSL
#include "cogl-pipeline-vertend-glsl-private.h"
#endif
#ifdef COGL_PIPELINE_VERTEND_FIXED
#include "cogl-pipeline-vertend-fixed-private.h"
#endif
COGL_OBJECT_DEFINE (Pipeline, pipeline);
/* This type was made deprecated before the cogl_is_pipeline_layer
function was ever exposed in the public headers so there's no need
to make the cogl_is_pipeline_layer function public. We use INTERNAL
so that the cogl_is_* function won't get defined */
COGL_OBJECT_INTERNAL_DEFINE (PipelineLayer, pipeline_layer);
GQuark
_cogl_pipeline_error_quark (void)
{
return g_quark_from_static_string ("cogl-pipeline-error-quark");
}
static void
_cogl_pipeline_node_init (CoglPipelineNode *node)
{
node->parent = NULL;
COGL_LIST_INIT (&node->children);
}
static void
_cogl_pipeline_node_set_parent_real (CoglPipelineNode *node,
CoglPipelineNode *parent,
CoglPipelineNodeUnparentVFunc unparent,
gboolean take_strong_reference)
{
/* NB: the old parent may indirectly be keeping the new parent alive
* so we have to ref the new parent before unrefing the old.
*
* Note: we take a reference here regardless of
* take_strong_reference because weak children may need special
* handling when the parent disposes itself which relies on a
* consistent link to all weak nodes. Once the node is linked to its
* parent then we remove the reference at the end if
* take_strong_reference == FALSE. */
cogl_object_ref (parent);
if (node->parent)
unparent (node);
COGL_LIST_INSERT_HEAD (&parent->children, node, list_node);
node->parent = parent;
node->has_parent_reference = take_strong_reference;
/* Now that there is a consistent parent->child link we can remove
* the parent reference if no reference was requested. If it turns
* out that the new parent was only being kept alive by the old
* parent then it will be disposed of here. */
if (!take_strong_reference)
cogl_object_unref (parent);
}
static void
_cogl_pipeline_node_unparent_real (CoglPipelineNode *node)
{
CoglPipelineNode *parent = node->parent;
if (parent == NULL)
return;
g_return_if_fail (!COGL_LIST_EMPTY (&parent->children));
COGL_LIST_REMOVE (node, list_node);
if (node->has_parent_reference)
cogl_object_unref (parent);
node->parent = NULL;
}
void
_cogl_pipeline_node_foreach_child (CoglPipelineNode *node,
CoglPipelineNodeChildCallback callback,
void *user_data)
{
CoglPipelineNode *child, *next;
COGL_LIST_FOREACH_SAFE (child, &node->children, list_node, next)
callback (child, user_data);
}
/*
* This initializes the first pipeline owned by the Cogl context. All
* subsequently instantiated pipelines created via the cogl_pipeline_new()
* API will initially be a copy of this pipeline.
*
* The default pipeline is the topmost ancester for all pipelines.
*/
void
_cogl_pipeline_init_default_pipeline (void)
{
/* Create new - blank - pipeline */
CoglPipeline *pipeline = g_slice_new0 (CoglPipeline);
/* XXX: NB: It's important that we zero this to avoid polluting
* pipeline hash values with un-initialized data */
CoglPipelineBigState *big_state = g_slice_new0 (CoglPipelineBigState);
CoglPipelineLightingState *lighting_state = &big_state->lighting_state;
CoglPipelineAlphaFuncState *alpha_state = &big_state->alpha_state;
CoglPipelineBlendState *blend_state = &big_state->blend_state;
CoglDepthState *depth_state = &big_state->depth_state;
CoglPipelineLogicOpsState *logic_ops_state = &big_state->logic_ops_state;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
/* Take this opportunity to setup the backends... */
#ifdef COGL_PIPELINE_FRAGEND_GLSL
_cogl_pipeline_fragends[COGL_PIPELINE_FRAGEND_GLSL] =
&_cogl_pipeline_glsl_fragend;
#endif
#ifdef COGL_PIPELINE_FRAGEND_ARBFP
_cogl_pipeline_fragends[COGL_PIPELINE_FRAGEND_ARBFP] =
&_cogl_pipeline_arbfp_fragend;
#endif
#ifdef COGL_PIPELINE_FRAGEND_FIXED
_cogl_pipeline_fragends[COGL_PIPELINE_FRAGEND_FIXED] =
&_cogl_pipeline_fixed_fragend;
#endif
#ifdef COGL_PIPELINE_PROGEND_GLSL
_cogl_pipeline_progends[COGL_PIPELINE_PROGEND_GLSL] =
&_cogl_pipeline_glsl_progend;
#endif
#ifdef COGL_PIPELINE_VERTEND_GLSL
_cogl_pipeline_vertends[COGL_PIPELINE_VERTEND_GLSL] =
&_cogl_pipeline_glsl_vertend;
#endif
#ifdef COGL_PIPELINE_VERTEND_FIXED
_cogl_pipeline_vertends[COGL_PIPELINE_VERTEND_FIXED] =
&_cogl_pipeline_fixed_vertend;
#endif
_cogl_pipeline_node_init (COGL_PIPELINE_NODE (pipeline));
pipeline->is_weak = FALSE;
pipeline->journal_ref_count = 0;
pipeline->fragend = COGL_PIPELINE_FRAGEND_UNDEFINED;
pipeline->vertend = COGL_PIPELINE_VERTEND_UNDEFINED;
pipeline->differences = COGL_PIPELINE_STATE_ALL_SPARSE;
pipeline->real_blend_enable = FALSE;
pipeline->blend_enable = COGL_PIPELINE_BLEND_ENABLE_AUTOMATIC;
pipeline->layer_differences = NULL;
pipeline->n_layers = 0;
pipeline->big_state = big_state;
pipeline->has_big_state = TRUE;
pipeline->static_breadcrumb = "default pipeline";
pipeline->has_static_breadcrumb = TRUE;
pipeline->age = 0;
/* Use the same defaults as the GL spec... */
cogl_color_init_from_4ub (&pipeline->color, 0xff, 0xff, 0xff, 0xff);
/* Use the same defaults as the GL spec... */
lighting_state->ambient[0] = 0.2;
lighting_state->ambient[1] = 0.2;
lighting_state->ambient[2] = 0.2;
lighting_state->ambient[3] = 1.0;
lighting_state->diffuse[0] = 0.8;
lighting_state->diffuse[1] = 0.8;
lighting_state->diffuse[2] = 0.8;
lighting_state->diffuse[3] = 1.0;
lighting_state->specular[0] = 0;
lighting_state->specular[1] = 0;
lighting_state->specular[2] = 0;
lighting_state->specular[3] = 1.0;
lighting_state->emission[0] = 0;
lighting_state->emission[1] = 0;
lighting_state->emission[2] = 0;
lighting_state->emission[3] = 1.0;
lighting_state->shininess = 0.0f;
/* Use the same defaults as the GL spec... */
alpha_state->alpha_func = COGL_PIPELINE_ALPHA_FUNC_ALWAYS;
alpha_state->alpha_func_reference = 0.0;
/* Not the same as the GL default, but seems saner... */
#if defined(HAVE_COGL_GLES2) || defined(HAVE_COGL_GL)
blend_state->blend_equation_rgb = GL_FUNC_ADD;
blend_state->blend_equation_alpha = GL_FUNC_ADD;
blend_state->blend_src_factor_alpha = GL_ONE;
blend_state->blend_dst_factor_alpha = GL_ONE_MINUS_SRC_ALPHA;
cogl_color_init_from_4ub (&blend_state->blend_constant,
0x00, 0x00, 0x00, 0x00);
#endif
blend_state->blend_src_factor_rgb = GL_ONE;
blend_state->blend_dst_factor_rgb = GL_ONE_MINUS_SRC_ALPHA;
big_state->user_program = COGL_INVALID_HANDLE;
/* The same as the GL defaults */
depth_state->test_enabled = FALSE;
depth_state->test_function = COGL_DEPTH_TEST_FUNCTION_LESS;
depth_state->write_enabled = TRUE;
depth_state->range_near = 0;
depth_state->range_far = 1;
big_state->point_size = 1.0f;
logic_ops_state->color_mask = COGL_COLOR_MASK_ALL;
ctx->default_pipeline = _cogl_pipeline_object_new (pipeline);
}
static void
_cogl_pipeline_unparent (CoglPipelineNode *pipeline)
{
/* Chain up */
_cogl_pipeline_node_unparent_real (pipeline);
}
static gboolean
recursively_free_layer_caches_cb (CoglPipelineNode *node,
void *user_data)
{
recursively_free_layer_caches (COGL_PIPELINE (node));
return TRUE;
}
/* This recursively frees the layers_cache of a pipeline and all of
* its descendants.
*
* For instance if we change a pipelines ->layer_differences list
* then that pipeline and all of its descendants may now have
* incorrect layer caches. */
static void
recursively_free_layer_caches (CoglPipeline *pipeline)
{
/* Note: we maintain the invariable that if a pipeline already has a
* dirty layers_cache then so do all of its descendants. */
if (pipeline->layers_cache_dirty)
return;
if (G_UNLIKELY (pipeline->layers_cache != pipeline->short_layers_cache))
g_slice_free1 (sizeof (CoglPipelineLayer *) * pipeline->n_layers,
pipeline->layers_cache);
pipeline->layers_cache_dirty = TRUE;
_cogl_pipeline_node_foreach_child (COGL_PIPELINE_NODE (pipeline),
recursively_free_layer_caches_cb,
NULL);
}
static void
_cogl_pipeline_set_parent (CoglPipeline *pipeline,
CoglPipeline *parent,
gboolean take_strong_reference)
{
/* Chain up */
_cogl_pipeline_node_set_parent_real (COGL_PIPELINE_NODE (pipeline),
COGL_PIPELINE_NODE (parent),
_cogl_pipeline_unparent,
take_strong_reference);
/* Since we just changed the ancestry of the pipeline its cache of
* layers could now be invalid so free it... */
if (pipeline->differences & COGL_PIPELINE_STATE_LAYERS)
recursively_free_layer_caches (pipeline);
/* If the backends are also caching state along with the pipeline
* that depends on the pipeline's ancestry then it may be notified
* here...
*/
if (pipeline->fragend != COGL_PIPELINE_FRAGEND_UNDEFINED &&
_cogl_pipeline_fragends[pipeline->fragend]->pipeline_set_parent_notify)
{
const CoglPipelineFragend *fragend =
_cogl_pipeline_fragends[pipeline->fragend];
fragend->pipeline_set_parent_notify (pipeline);
}
}
static void
_cogl_pipeline_promote_weak_ancestors (CoglPipeline *strong)
{
CoglPipelineNode *n;
g_return_if_fail (!strong->is_weak);
/* If the parent of strong is weak, then we want to promote it by
taking a reference on strong's grandparent. We don't need to take
a reference on strong's direct parent */
if (COGL_PIPELINE_NODE (strong)->parent == NULL)
return;
for (n = COGL_PIPELINE_NODE (strong)->parent;
/* We can assume that all weak pipelines have a parent */
COGL_PIPELINE (n)->is_weak;
n = n->parent)
/* 'n' is weak so we take a reference on its parent */
cogl_object_ref (n->parent);
}
static void
_cogl_pipeline_revert_weak_ancestors (CoglPipeline *strong)
{
CoglPipelineNode *n;
g_return_if_fail (!strong->is_weak);
/* This reverts the effect of calling
_cogl_pipeline_promote_weak_ancestors */
if (COGL_PIPELINE_NODE (strong)->parent == NULL)
return;
for (n = COGL_PIPELINE_NODE (strong)->parent;
/* We can assume that all weak pipelines have a parent */
COGL_PIPELINE (n)->is_weak;
n = n->parent)
/* 'n' is weak so we unref its parent */
cogl_object_unref (n->parent);
}
/* XXX: Always have an eye out for opportunities to lower the cost of
* cogl_pipeline_copy. */
static CoglPipeline *
_cogl_pipeline_copy (CoglPipeline *src, gboolean is_weak)
{
CoglPipeline *pipeline = g_slice_new (CoglPipeline);
_cogl_pipeline_node_init (COGL_PIPELINE_NODE (pipeline));
pipeline->is_weak = is_weak;
pipeline->journal_ref_count = 0;
pipeline->differences = 0;
pipeline->has_big_state = FALSE;
/* NB: real_blend_enable isn't a sparse property, it's valid for
* every pipeline node so we have fast access to it. */
pipeline->real_blend_enable = src->real_blend_enable;
/* XXX:
* consider generalizing the idea of "cached" properties. These
* would still have an authority like other sparse properties but
* you wouldn't have to walk up the ancestry to find the authority
* because the value would be cached directly in each pipeline.
*/
pipeline->layers_cache_dirty = TRUE;
pipeline->deprecated_get_layers_list = NULL;
pipeline->deprecated_get_layers_list_dirty = TRUE;
pipeline->fragend = src->fragend;
pipeline->vertend = src->vertend;
pipeline->has_static_breadcrumb = FALSE;
pipeline->age = 0;
_cogl_pipeline_set_parent (pipeline, src, !is_weak);
/* The semantics for copying a weak pipeline are that we promote all
* weak ancestors to temporarily become strong pipelines until the
* copy is freed. */
if (!is_weak)
_cogl_pipeline_promote_weak_ancestors (pipeline);
return _cogl_pipeline_object_new (pipeline);
}
CoglPipeline *
cogl_pipeline_copy (CoglPipeline *src)
{
return _cogl_pipeline_copy (src, FALSE);
}
CoglPipeline *
_cogl_pipeline_weak_copy (CoglPipeline *pipeline,
CoglPipelineDestroyCallback callback,
void *user_data)
{
CoglPipeline *copy;
CoglPipeline *copy_pipeline;
copy = _cogl_pipeline_copy (pipeline, TRUE);
copy_pipeline = COGL_PIPELINE (copy);
copy_pipeline->destroy_callback = callback;
copy_pipeline->destroy_data = user_data;
return copy;
}
CoglPipeline *
cogl_pipeline_new (void)
{
CoglPipeline *new;
_COGL_GET_CONTEXT (ctx, NULL);
new = cogl_pipeline_copy (ctx->default_pipeline);
_cogl_pipeline_set_static_breadcrumb (new, "new");
return new;
}
static gboolean
destroy_weak_children_cb (CoglPipelineNode *node,
void *user_data)
{
CoglPipeline *pipeline = COGL_PIPELINE (node);
if (_cogl_pipeline_is_weak (pipeline))
{
_cogl_pipeline_node_foreach_child (COGL_PIPELINE_NODE (pipeline),
destroy_weak_children_cb,
NULL);
pipeline->destroy_callback (pipeline, pipeline->destroy_data);
_cogl_pipeline_unparent (COGL_PIPELINE_NODE (pipeline));
}
return TRUE;
}
static void
_cogl_pipeline_free (CoglPipeline *pipeline)
{
if (!pipeline->is_weak)
_cogl_pipeline_revert_weak_ancestors (pipeline);
/* Weak pipelines don't take a reference on their parent */
_cogl_pipeline_node_foreach_child (COGL_PIPELINE_NODE (pipeline),
destroy_weak_children_cb,
NULL);
g_assert (COGL_LIST_EMPTY (&COGL_PIPELINE_NODE (pipeline)->children));
_cogl_pipeline_unparent (COGL_PIPELINE_NODE (pipeline));
if (pipeline->differences & COGL_PIPELINE_STATE_USER_SHADER &&
pipeline->big_state->user_program)
cogl_handle_unref (pipeline->big_state->user_program);
if (pipeline->differences & COGL_PIPELINE_STATE_NEEDS_BIG_STATE)
g_slice_free (CoglPipelineBigState, pipeline->big_state);
if (pipeline->differences & COGL_PIPELINE_STATE_LAYERS)
{
g_list_foreach (pipeline->layer_differences,
(GFunc)cogl_object_unref, NULL);
g_list_free (pipeline->layer_differences);
}
g_list_free (pipeline->deprecated_get_layers_list);
g_slice_free (CoglPipeline, pipeline);
}
gboolean
_cogl_pipeline_get_real_blend_enabled (CoglPipeline *pipeline)
{
g_return_val_if_fail (cogl_is_pipeline (pipeline), FALSE);
return pipeline->real_blend_enable;
}
/* XXX: Think twice before making this non static since it is used
* heavily and we expect the compiler to inline it...
*/
static CoglPipelineLayer *
_cogl_pipeline_layer_get_parent (CoglPipelineLayer *layer)
{
CoglPipelineNode *parent_node = COGL_PIPELINE_NODE (layer)->parent;
return COGL_PIPELINE_LAYER (parent_node);
}
CoglPipelineLayer *
_cogl_pipeline_layer_get_authority (CoglPipelineLayer *layer,
unsigned long difference)
{
CoglPipelineLayer *authority = layer;
while (!(authority->differences & difference))
authority = _cogl_pipeline_layer_get_parent (authority);
return authority;
}
int
_cogl_pipeline_layer_get_unit_index (CoglPipelineLayer *layer)
{
CoglPipelineLayer *authority =
_cogl_pipeline_layer_get_authority (layer, COGL_PIPELINE_LAYER_STATE_UNIT);
return authority->unit_index;
}
static void
_cogl_pipeline_update_layers_cache (CoglPipeline *pipeline)
{
/* Note: we assume this pipeline is a _LAYERS authority */
int n_layers;
CoglPipeline *current;
int layers_found;
if (G_LIKELY (!pipeline->layers_cache_dirty) ||
pipeline->n_layers == 0)
return;
pipeline->layers_cache_dirty = FALSE;
n_layers = pipeline->n_layers;
if (G_LIKELY (n_layers < G_N_ELEMENTS (pipeline->short_layers_cache)))
{
pipeline->layers_cache = pipeline->short_layers_cache;
memset (pipeline->layers_cache, 0,
sizeof (CoglPipelineLayer *) *
G_N_ELEMENTS (pipeline->short_layers_cache));
}
else
{
pipeline->layers_cache =
g_slice_alloc0 (sizeof (CoglPipelineLayer *) * n_layers);
}
/* Notes:
*
* Each pipeline doesn't have to contain a complete list of the layers
* it depends on, some of them are indirectly referenced through the
* pipeline's ancestors.
*
* pipeline->layer_differences only contains a list of layers that
* have changed in relation to its parent.
*
* pipeline->layer_differences is not maintained sorted, but it
* won't contain multiple layers corresponding to a particular
* ->unit_index.
*
* Some of the ancestor pipelines may reference layers with
* ->unit_index values >= n_layers so we ignore them.
*
* As we ascend through the ancestors we are searching for any
* CoglPipelineLayers corresponding to the texture ->unit_index
* values in the range [0,n_layers-1]. As soon as a pointer is found
* we ignore layers of further ancestors with the same ->unit_index
* values.
*/
layers_found = 0;
for (current = pipeline;
_cogl_pipeline_get_parent (current);
current = _cogl_pipeline_get_parent (current))
{
GList *l;
if (!(current->differences & COGL_PIPELINE_STATE_LAYERS))
continue;
for (l = current->layer_differences; l; l = l->next)
{
CoglPipelineLayer *layer = l->data;
int unit_index = _cogl_pipeline_layer_get_unit_index (layer);
if (unit_index < n_layers && !pipeline->layers_cache[unit_index])
{
pipeline->layers_cache[unit_index] = layer;
layers_found++;
if (layers_found == n_layers)
return;
}
}
}
g_warn_if_reached ();
}
/* XXX: Be carefull when using this API that the callback given doesn't result
* in the layer cache being invalidated during the iteration! */
void
_cogl_pipeline_foreach_layer_internal (CoglPipeline *pipeline,
CoglPipelineInternalLayerCallback callback,
void *user_data)
{
CoglPipeline *authority =
_cogl_pipeline_get_authority (pipeline, COGL_PIPELINE_STATE_LAYERS);
int n_layers;
int i;
gboolean cont;
n_layers = authority->n_layers;
if (n_layers == 0)
return;
_cogl_pipeline_update_layers_cache (authority);
for (i = 0, cont = TRUE; i < n_layers && cont == TRUE; i++)
{
g_return_if_fail (authority->layers_cache_dirty == FALSE);
cont = callback (authority->layers_cache[i], user_data);
}
}
typedef struct
{
int i;
int *indices;
} AppendLayerIndexState;
static gboolean
append_layer_index_cb (CoglPipelineLayer *layer,
void *user_data)
{
AppendLayerIndexState *state = user_data;
state->indices[state->i++] = layer->index;
return TRUE;
}
void
cogl_pipeline_foreach_layer (CoglPipeline *pipeline,
CoglPipelineLayerCallback callback,
void *user_data)
{
CoglPipeline *authority =
_cogl_pipeline_get_authority (pipeline, COGL_PIPELINE_STATE_LAYERS);
AppendLayerIndexState state;
gboolean cont;
int i;
/* XXX: We don't know what the user is going to want to do to the layers
* but any modification of layers can result in the layer graph changing
* which could confuse _cogl_pipeline_foreach_layer_internal(). We first
* get a list of layer indices which will remain valid so long as the
* user doesn't remove layers. */
state.i = 0;
state.indices = g_alloca (authority->n_layers * sizeof (int));
_cogl_pipeline_foreach_layer_internal (pipeline,
append_layer_index_cb,
&state);
for (i = 0, cont = TRUE; i < authority->n_layers && cont; i++)
cont = callback (pipeline, state.indices[i], user_data);
}
static gboolean
layer_has_alpha_cb (CoglPipelineLayer *layer, void *data)
{
CoglPipelineLayer *combine_authority =
_cogl_pipeline_layer_get_authority (layer,
COGL_PIPELINE_LAYER_STATE_COMBINE);
CoglPipelineLayerBigState *big_state = combine_authority->big_state;
CoglPipelineLayer *tex_authority;
gboolean *has_alpha = data;
/* has_alpha maintains the alpha status for the GL_PREVIOUS layer */
/* For anything but the default texture combine we currently just
* assume it may result in an alpha value < 1
*
* FIXME: we could do better than this. */
if (big_state->texture_combine_alpha_func !=
COGL_PIPELINE_COMBINE_FUNC_MODULATE ||
big_state->texture_combine_alpha_src[0] !=
COGL_PIPELINE_COMBINE_SOURCE_PREVIOUS ||
big_state->texture_combine_alpha_op[0] !=
COGL_PIPELINE_COMBINE_OP_SRC_ALPHA ||
big_state->texture_combine_alpha_src[1] !=
COGL_PIPELINE_COMBINE_SOURCE_TEXTURE ||
big_state->texture_combine_alpha_op[1] !=
COGL_PIPELINE_COMBINE_OP_SRC_ALPHA)
{
*has_alpha = TRUE;
/* return FALSE to stop iterating layers... */
return FALSE;
}
/* NB: A layer may have a combine mode set on it but not yet
* have an associated texture which would mean we'd fallback
* to the default texture which doesn't have an alpha component
*/
tex_authority =
_cogl_pipeline_layer_get_authority (layer,
COGL_PIPELINE_LAYER_STATE_TEXTURE_DATA);
if (tex_authority->texture &&
cogl_texture_get_format (tex_authority->texture) & COGL_A_BIT)
{
*has_alpha = TRUE;
/* return FALSE to stop iterating layers... */
return FALSE;
}
*has_alpha = FALSE;
/* return FALSE to continue iterating layers... */
return TRUE;
}
static gboolean
_cogl_pipeline_needs_blending_enabled (CoglPipeline *pipeline,
unsigned long changes,
const CoglColor *override_color)
{
CoglPipeline *enable_authority;
CoglPipeline *blend_authority;
CoglPipelineBlendState *blend_state;
CoglPipelineBlendEnable enabled;
unsigned long other_state;
_COGL_GET_CONTEXT (ctx, FALSE);
if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_DISABLE_BLENDING)))
return FALSE;
enable_authority =
_cogl_pipeline_get_authority (pipeline, COGL_PIPELINE_STATE_BLEND_ENABLE);
enabled = enable_authority->blend_enable;
if (enabled != COGL_PIPELINE_BLEND_ENABLE_AUTOMATIC)
return enabled == COGL_PIPELINE_BLEND_ENABLE_ENABLED ? TRUE : FALSE;
blend_authority =
_cogl_pipeline_get_authority (pipeline, COGL_PIPELINE_STATE_BLEND);
blend_state = &blend_authority->big_state->blend_state;
/* We are trying to identify awkward cases that are equivalent to
* blending being disable, where the output is simply GL_SRC_COLOR.
*
* Note: we assume that all OpenGL drivers will identify the simple
* case of ADD (ONE, ZERO) as equivalent to blending being disabled.
*
* We should update this when we add support for more blend
* functions...
*/
#if defined (HAVE_COGL_GLES2) || defined (HAVE_COGL_GL)
if (ctx->driver != COGL_DRIVER_GLES1)
{
/* GLES 1 can't change the function or have separate alpha factors */
if (blend_state->blend_equation_rgb != GL_FUNC_ADD ||
blend_state->blend_equation_alpha != GL_FUNC_ADD)
return TRUE;
if (blend_state->blend_src_factor_alpha != GL_ONE ||
blend_state->blend_dst_factor_alpha != GL_ONE_MINUS_SRC_ALPHA)
return TRUE;
}
#endif
if (blend_state->blend_src_factor_rgb != GL_ONE ||
blend_state->blend_dst_factor_rgb != GL_ONE_MINUS_SRC_ALPHA)
return TRUE;
/* Given the above constraints, it's now a case of finding any
* SRC_ALPHA that != 1 */
/* In the case of a layer state change we need to check everything
* else first since they contribute to the has_alpha status of the
* GL_PREVIOUS layer. */
if (changes & COGL_PIPELINE_STATE_LAYERS)
changes = COGL_PIPELINE_STATE_AFFECTS_BLENDING;
if ((override_color && cogl_color_get_alpha_byte (override_color) != 0xff))
return TRUE;
if (changes & COGL_PIPELINE_STATE_COLOR)
{
CoglColor tmp;
cogl_pipeline_get_color (pipeline, &tmp);
if (cogl_color_get_alpha_byte (&tmp) != 0xff)
return TRUE;
}
if (changes & COGL_PIPELINE_STATE_USER_SHADER)
{
/* We can't make any assumptions about the alpha channel if the user
* is using an unknown fragment shader.
*
* TODO: check that it isn't just a vertex shader!
*/
if (_cogl_pipeline_get_user_program (pipeline) != COGL_INVALID_HANDLE)
return TRUE;
}
/* XXX: we should only need to look at these if lighting is enabled
*/
if (changes & COGL_PIPELINE_STATE_LIGHTING)
{
/* XXX: This stuff is showing up in sysprof reports which is
* silly because lighting isn't currently actually supported
* by Cogl except for these token properties. When we actually
* expose lighting support we can avoid these checks when
* lighting is disabled. */
#if 0
CoglColor tmp;
cogl_pipeline_get_ambient (pipeline, &tmp);
if (cogl_color_get_alpha_byte (&tmp) != 0xff)
return TRUE;
cogl_pipeline_get_diffuse (pipeline, &tmp);
if (cogl_color_get_alpha_byte (&tmp) != 0xff)
return TRUE;
cogl_pipeline_get_specular (pipeline, &tmp);
if (cogl_color_get_alpha_byte (&tmp) != 0xff)
return TRUE;
cogl_pipeline_get_emission (pipeline, &tmp);
if (cogl_color_get_alpha_byte (&tmp) != 0xff)
return TRUE;
#endif
}
if (changes & COGL_PIPELINE_STATE_LAYERS)
{
/* has_alpha tracks the alpha status of the GL_PREVIOUS layer.
* To start with that's defined by the pipeline color which
* must be fully opaque if we got this far. */
gboolean has_alpha = FALSE;
_cogl_pipeline_foreach_layer_internal (pipeline,
layer_has_alpha_cb,
&has_alpha);
if (has_alpha)
return TRUE;
}
/* At this point, considering just the state that has changed it
* looks like blending isn't needed. If blending was previously
* enabled though it could be that some other state still requires
* that we have blending enabled. In this case we still need to
* go and check the other state...
*
* FIXME: We should explicitly keep track of the mask of state
* groups that are currently causing blending to be enabled so that
* we never have to resort to checking *all* the state and can
* instead always limit the check to those in the mask.
*/
if (pipeline->real_blend_enable)
{
other_state = COGL_PIPELINE_STATE_AFFECTS_BLENDING & ~changes;
if (other_state &&
_cogl_pipeline_needs_blending_enabled (pipeline,
other_state,
NULL))
return TRUE;
}
return FALSE;
}
void
_cogl_pipeline_set_fragend (CoglPipeline *pipeline, int fragend)
{
pipeline->fragend = fragend;
}
void
_cogl_pipeline_set_vertend (CoglPipeline *pipeline, int vertend)
{
pipeline->vertend = vertend;
}
static void
_cogl_pipeline_copy_differences (CoglPipeline *dest,
CoglPipeline *src,
unsigned long differences)
{
CoglPipelineBigState *big_state;
if (differences & COGL_PIPELINE_STATE_COLOR)
dest->color = src->color;
if (differences & COGL_PIPELINE_STATE_BLEND_ENABLE)
dest->blend_enable = src->blend_enable;
if (differences & COGL_PIPELINE_STATE_LAYERS)
{
GList *l;
if (dest->differences & COGL_PIPELINE_STATE_LAYERS &&
dest->layer_differences)
{
g_list_foreach (dest->layer_differences,
(GFunc)cogl_object_unref,
NULL);
g_list_free (dest->layer_differences);
}
for (l = src->layer_differences; l; l = l->next)
{
/* NB: a layer can't have more than one ->owner so we can't
* simply take a references on each of the original
* layer_differences, we have to derive new layers from the
* originals instead. */
CoglPipelineLayer *copy = _cogl_pipeline_layer_copy (l->data);
_cogl_pipeline_add_layer_difference (dest, copy, FALSE);
cogl_object_unref (copy);
}
/* Note: we initialize n_layers after adding the layer differences
* since the act of adding the layers will initialize n_layers to 0
* because dest isn't initially a STATE_LAYERS authority. */
dest->n_layers = src->n_layers;
}
if (differences & COGL_PIPELINE_STATE_NEEDS_BIG_STATE)
{
if (!dest->has_big_state)
{
dest->big_state = g_slice_new (CoglPipelineBigState);
dest->has_big_state = TRUE;
}
big_state = dest->big_state;
}
else
goto check_for_blending_change;
if (differences & COGL_PIPELINE_STATE_LIGHTING)
{
memcpy (&big_state->lighting_state,
&src->big_state->lighting_state,
sizeof (CoglPipelineLightingState));
}
if (differences & COGL_PIPELINE_STATE_ALPHA_FUNC)
big_state->alpha_state.alpha_func =
src->big_state->alpha_state.alpha_func;
if (differences & COGL_PIPELINE_STATE_ALPHA_FUNC_REFERENCE)
big_state->alpha_state.alpha_func_reference =
src->big_state->alpha_state.alpha_func_reference;
if (differences & COGL_PIPELINE_STATE_BLEND)
{
memcpy (&big_state->blend_state,
&src->big_state->blend_state,
sizeof (CoglPipelineBlendState));
}
if (differences & COGL_PIPELINE_STATE_USER_SHADER)
{
if (src->big_state->user_program)
big_state->user_program =
cogl_handle_ref (src->big_state->user_program);
else
big_state->user_program = COGL_INVALID_HANDLE;
}
if (differences & COGL_PIPELINE_STATE_DEPTH)
{
memcpy (&big_state->depth_state,
&src->big_state->depth_state,
sizeof (CoglDepthState));
}
if (differences & COGL_PIPELINE_STATE_FOG)
{
memcpy (&big_state->fog_state,
&src->big_state->fog_state,
sizeof (CoglPipelineFogState));
}
if (differences & COGL_PIPELINE_STATE_POINT_SIZE)
big_state->point_size = src->big_state->point_size;
if (differences & COGL_PIPELINE_STATE_LOGIC_OPS)
{
memcpy (&big_state->logic_ops_state,
&src->big_state->logic_ops_state,
sizeof (CoglPipelineLogicOpsState));
}
/* XXX: we shouldn't bother doing this in most cases since
* _copy_differences is typically used to initialize pipeline state
* by copying it from the current authority, so it's not actually
* *changing* anything.
*/
check_for_blending_change:
if (differences & COGL_PIPELINE_STATE_AFFECTS_BLENDING)
_cogl_pipeline_update_blend_enable (dest, differences);
dest->differences |= differences;
}
static void
_cogl_pipeline_init_multi_property_sparse_state (CoglPipeline *pipeline,
CoglPipelineState change)
{
CoglPipeline *authority;
g_return_if_fail (change & COGL_PIPELINE_STATE_ALL_SPARSE);
if (!(change & COGL_PIPELINE_STATE_MULTI_PROPERTY))
return;
authority = _cogl_pipeline_get_authority (pipeline, change);
switch (change)
{
/* XXX: avoid using a default: label so we get a warning if we
* don't explicitly handle a newly defined state-group here. */
case COGL_PIPELINE_STATE_COLOR:
case COGL_PIPELINE_STATE_BLEND_ENABLE:
case COGL_PIPELINE_STATE_ALPHA_FUNC:
case COGL_PIPELINE_STATE_ALPHA_FUNC_REFERENCE:
case COGL_PIPELINE_STATE_POINT_SIZE:
case COGL_PIPELINE_STATE_USER_SHADER:
case COGL_PIPELINE_STATE_REAL_BLEND_ENABLE:
g_return_if_reached ();
case COGL_PIPELINE_STATE_LAYERS:
pipeline->n_layers = authority->n_layers;
pipeline->layer_differences = NULL;
break;
case COGL_PIPELINE_STATE_LIGHTING:
{
memcpy (&pipeline->big_state->lighting_state,
&authority->big_state->lighting_state,
sizeof (CoglPipelineLightingState));
break;
}
case COGL_PIPELINE_STATE_BLEND:
{
memcpy (&pipeline->big_state->blend_state,
&authority->big_state->blend_state,
sizeof (CoglPipelineBlendState));
break;
}
case COGL_PIPELINE_STATE_DEPTH:
{
memcpy (&pipeline->big_state->depth_state,
&authority->big_state->depth_state,
sizeof (CoglDepthState));
break;
}
case COGL_PIPELINE_STATE_FOG:
{
memcpy (&pipeline->big_state->fog_state,
&authority->big_state->fog_state,
sizeof (CoglPipelineFogState));
break;
}
case COGL_PIPELINE_STATE_LOGIC_OPS:
{
memcpy (&pipeline->big_state->logic_ops_state,
&authority->big_state->logic_ops_state,
sizeof (CoglPipelineLogicOpsState));
break;
}
}
}
static gboolean
check_if_strong_cb (CoglPipelineNode *node, void *user_data)
{
CoglPipeline *pipeline = COGL_PIPELINE (node);
gboolean *has_strong_child = user_data;
if (!_cogl_pipeline_is_weak (pipeline))
{
*has_strong_child = TRUE;
return FALSE;
}
return TRUE;
}
static gboolean
has_strong_children (CoglPipeline *pipeline)
{
gboolean has_strong_child = FALSE;
_cogl_pipeline_node_foreach_child (COGL_PIPELINE_NODE (pipeline),
check_if_strong_cb,
&has_strong_child);
return has_strong_child;
}
static gboolean
_cogl_pipeline_is_weak (CoglPipeline *pipeline)
{
if (pipeline->is_weak && !has_strong_children (pipeline))
return TRUE;
else
return FALSE;
}
static gboolean
reparent_children_cb (CoglPipelineNode *node,
void *user_data)
{
CoglPipeline *pipeline = COGL_PIPELINE (node);
CoglPipeline *parent = user_data;
_cogl_pipeline_set_parent (pipeline, parent, TRUE);
return TRUE;
}
void
_cogl_pipeline_pre_change_notify (CoglPipeline *pipeline,
CoglPipelineState change,
const CoglColor *new_color,
gboolean from_layer_change)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
/* If primitives have been logged in the journal referencing the
* current state of this pipeline we need to flush the journal
* before we can modify it... */
if (pipeline->journal_ref_count)
{
gboolean skip_journal_flush = FALSE;
/* XXX: We don't usually need to flush the journal just due to
* color changes since pipeline colors are logged in the
* journal's vertex buffer. The exception is when the change in
* color enables or disables the need for blending. */
if (change == COGL_PIPELINE_STATE_COLOR)
{
gboolean will_need_blending =
_cogl_pipeline_needs_blending_enabled (pipeline,
change,
new_color);
gboolean blend_enable = pipeline->real_blend_enable ? TRUE : FALSE;
if (will_need_blending == blend_enable)
skip_journal_flush = TRUE;
}
if (!skip_journal_flush)
{
/* XXX: note we use cogl_flush() not _cogl_flush_journal() so
* we will flush *all* known journals that might reference the
* current pipeline. */
cogl_flush ();
}
}
/* The fixed function backend has no private state and can't
* do anything special to handle small pipeline changes so we may as
* well try to find a better backend whenever the pipeline changes.
*
* The programmable backends may be able to cache a lot of the code
* they generate and only need to update a small section of that
* code in response to a pipeline change therefore we don't want to
* try searching for another backend when the pipeline changes.
*/
#ifdef COGL_PIPELINE_FRAGEND_FIXED
if (pipeline->fragend == COGL_PIPELINE_FRAGEND_FIXED)
_cogl_pipeline_set_fragend (pipeline, COGL_PIPELINE_FRAGEND_UNDEFINED);
#endif
#ifdef COGL_PIPELINE_VERTEND_FIXED
if (pipeline->vertend == COGL_PIPELINE_VERTEND_FIXED)
_cogl_pipeline_set_vertend (pipeline, COGL_PIPELINE_VERTEND_UNDEFINED);
#endif
/* XXX:
* To simplify things for the vertex, fragment and program backends
* we are careful about how we report STATE_LAYERS changes.
*
* All STATE_LAYERS change notification with the exception of
* ->n_layers will also result in layer_pre_change_notifications.
* For backends that perform code generation for fragment
* processing they typically need to understand the details of how
* layers get changed to determine if they need to repeat codegen.
* It doesn't help them to
* report a pipeline STATE_LAYERS change for all layer changes since
* it's so broad, they really need to wait for the specific layer
* change to be notified. What does help though is to report a
* STATE_LAYERS change for a change in
* ->n_layers because they typically do need to repeat codegen in
* that case.
*
* Here we ensure that change notifications against a pipeline or
* against a layer are mutually exclusive as far as fragment, vertex
* and program backends are concerned.
*/
if (!from_layer_change)
{
int i;
if (pipeline->fragend != COGL_PIPELINE_FRAGEND_UNDEFINED &&
_cogl_pipeline_fragends[pipeline->fragend]->pipeline_pre_change_notify)
{
const CoglPipelineFragend *fragend =
_cogl_pipeline_fragends[pipeline->fragend];
fragend->pipeline_pre_change_notify (pipeline, change, new_color);
}
if (pipeline->vertend != COGL_PIPELINE_VERTEND_UNDEFINED &&
_cogl_pipeline_vertends[pipeline->vertend]->pipeline_pre_change_notify)
{
const CoglPipelineVertend *vertend =
_cogl_pipeline_vertends[pipeline->vertend];
vertend->pipeline_pre_change_notify (pipeline, change, new_color);
}
for (i = 0; i < COGL_PIPELINE_N_PROGENDS; i++)
if (_cogl_pipeline_progends[i]->pipeline_pre_change_notify)
_cogl_pipeline_progends[i]->pipeline_pre_change_notify (pipeline,
change,
new_color);
}
/* There may be an arbitrary tree of descendants of this pipeline;
* any of which may indirectly depend on this pipeline as the
* authority for some set of properties. (Meaning for example that
* one of its descendants derives its color or blending state from
* this pipeline.)
*
* We can't modify any property that this pipeline is the authority
* for unless we create another pipeline to take its place first and
* make sure descendants reference this new pipeline instead.
*/
/* The simplest descendants to handle are weak pipelines; we simply
* destroy them if we are modifying a pipeline they depend on. This
* means weak pipelines never cause us to do a copy-on-write. */
_cogl_pipeline_node_foreach_child (COGL_PIPELINE_NODE (pipeline),
destroy_weak_children_cb,
NULL);
/* If there are still children remaining though we'll need to
* perform a copy-on-write and reparent the dependants as children
* of the copy. */
if (!COGL_LIST_EMPTY (&COGL_PIPELINE_NODE (pipeline)->children))
{
CoglPipeline *new_authority;
COGL_STATIC_COUNTER (pipeline_copy_on_write_counter,
"pipeline copy on write counter",
"Increments each time a pipeline "
"must be copied to allow modification",
0 /* no application private data */);
COGL_COUNTER_INC (_cogl_uprof_context, pipeline_copy_on_write_counter);
new_authority =
cogl_pipeline_copy (_cogl_pipeline_get_parent (pipeline));
_cogl_pipeline_set_static_breadcrumb (new_authority,
"pre_change_notify:copy-on-write");
/* We could explicitly walk the descendants, OR together the set
* of differences that we determine this pipeline is the
* authority on and only copy those differences copied across.
*
* Or, if we don't explicitly walk the descendants we at least
* know that pipeline->differences represents the largest set of
* differences that this pipeline could possibly be an authority
* on.
*
* We do the later just because it's simplest, but we might need
* to come back to this later...
*/
_cogl_pipeline_copy_differences (new_authority, pipeline,
pipeline->differences);
/* Reparent the dependants of pipeline to be children of
* new_authority instead... */
_cogl_pipeline_node_foreach_child (COGL_PIPELINE_NODE (pipeline),
reparent_children_cb,
new_authority);
/* The children will keep the new authority alive so drop the
* reference we got when copying... */
cogl_object_unref (new_authority);
}
/* At this point we know we have a pipeline with no strong
* dependants (though we may have some weak children) so we are now
* free to modify the pipeline. */
pipeline->age++;
if (change & COGL_PIPELINE_STATE_NEEDS_BIG_STATE &&
!pipeline->has_big_state)
{
pipeline->big_state = g_slice_new (CoglPipelineBigState);
pipeline->has_big_state = TRUE;
}
/* Note: conceptually we have just been notified that a single
* property value is about to change, but since some state-groups
* contain multiple properties and 'pipeline' is about to take over
* being the authority for the property's corresponding state-group
* we need to maintain the integrity of the other property values
* too.
*
* To ensure this we handle multi-property state-groups by copying
* all the values from the old-authority to the new...
*
* We don't have to worry about non-sparse property groups since
* we never take over being an authority for such properties so
* they automatically maintain integrity.
*/
if (change & COGL_PIPELINE_STATE_ALL_SPARSE &&
!(pipeline->differences & change))
{
_cogl_pipeline_init_multi_property_sparse_state (pipeline, change);
pipeline->differences |= change;
}
/* Each pipeline has a sorted cache of the layers it depends on
* which will need updating via _cogl_pipeline_update_layers_cache
* if a pipeline's layers are changed. */
if (change == COGL_PIPELINE_STATE_LAYERS)
recursively_free_layer_caches (pipeline);
/* If the pipeline being changed is the same as the last pipeline we
* flushed then we keep a track of the changes so we can try to
* minimize redundant OpenGL calls if the same pipeline is flushed
* again.
*/
if (ctx->current_pipeline == pipeline)
ctx->current_pipeline_changes_since_flush |= change;
}
static void
_cogl_pipeline_add_layer_difference (CoglPipeline *pipeline,
CoglPipelineLayer *layer,
gboolean inc_n_layers)
{
g_return_if_fail (layer->owner == NULL);
layer->owner = pipeline;
cogl_object_ref (layer);
/* - Flush journal primitives referencing the current state.
* - Make sure the pipeline has no dependants so it may be modified.
* - If the pipeline isn't currently an authority for the state being
* changed, then initialize that state from the current authority.
*/
/* Note: the last argument to _cogl_pipeline_pre_change_notify is
* needed to differentiate STATE_LAYER changes which don't affect
* the number of layers from those that do. NB: Layer change
* notifications that don't change the number of layers don't get
* forwarded to the fragend. */
_cogl_pipeline_pre_change_notify (pipeline,
COGL_PIPELINE_STATE_LAYERS,
NULL,
!inc_n_layers);
pipeline->differences |= COGL_PIPELINE_STATE_LAYERS;
pipeline->layer_differences =
g_list_prepend (pipeline->layer_differences, layer);
if (inc_n_layers)
pipeline->n_layers++;
}
static void
_cogl_pipeline_remove_layer_difference (CoglPipeline *pipeline,
CoglPipelineLayer *layer,
gboolean dec_n_layers)
{
g_return_if_fail (layer->owner == pipeline);
/* - Flush journal primitives referencing the current state.
* - Make sure the pipeline has no dependants so it may be modified.
* - If the pipeline isn't currently an authority for the state being
* changed, then initialize that state from the current authority.
*/
/* Note: the last argument to _cogl_pipeline_pre_change_notify is
* needed to differentiate STATE_LAYER changes which don't affect
* the number of layers from those that do. NB: Layer change
* notifications that don't change the number of layers don't get
* forwarded to the fragend. */
_cogl_pipeline_pre_change_notify (pipeline,
COGL_PIPELINE_STATE_LAYERS,
NULL,
!dec_n_layers);
layer->owner = NULL;
cogl_object_unref (layer);
pipeline->differences |= COGL_PIPELINE_STATE_LAYERS;
pipeline->layer_differences =
g_list_remove (pipeline->layer_differences, layer);
if (dec_n_layers)
pipeline->n_layers--;
}
static void
_cogl_pipeline_try_reverting_layers_authority (CoglPipeline *authority,
CoglPipeline *old_authority)
{
if (authority->layer_differences == NULL &&
_cogl_pipeline_get_parent (authority))
{
/* If the previous _STATE_LAYERS authority has the same
* ->n_layers then we can revert to that being the authority
* again. */
if (!old_authority)
{
old_authority =
_cogl_pipeline_get_authority (_cogl_pipeline_get_parent (authority),
COGL_PIPELINE_STATE_LAYERS);
}
if (old_authority->n_layers == authority->n_layers)
authority->differences &= ~COGL_PIPELINE_STATE_LAYERS;
}
}
void
_cogl_pipeline_update_blend_enable (CoglPipeline *pipeline,
CoglPipelineState change)
{
gboolean blend_enable =
_cogl_pipeline_needs_blending_enabled (pipeline, change, NULL);
if (blend_enable != pipeline->real_blend_enable)
{
/* - Flush journal primitives referencing the current state.
* - Make sure the pipeline has no dependants so it may be
* modified.
* - If the pipeline isn't currently an authority for the state
* being changed, then initialize that state from the current
* authority.
*/
_cogl_pipeline_pre_change_notify (pipeline,
COGL_PIPELINE_STATE_REAL_BLEND_ENABLE,
NULL,
FALSE);
pipeline->real_blend_enable = blend_enable;
}
}
typedef struct
{
int keep_n;
int current_pos;
int first_index_to_prune;
} CoglPipelinePruneLayersInfo;
static gboolean
update_prune_layers_info_cb (CoglPipelineLayer *layer, void *user_data)
{
CoglPipelinePruneLayersInfo *state = user_data;
if (state->current_pos == state->keep_n)
{
state->first_index_to_prune = layer->index;
return FALSE;
}
state->current_pos++;
return TRUE;
}
void
_cogl_pipeline_prune_to_n_layers (CoglPipeline *pipeline, int n)
{
CoglPipeline *authority =
_cogl_pipeline_get_authority (pipeline, COGL_PIPELINE_STATE_LAYERS);
CoglPipelinePruneLayersInfo state;
GList *l;
GList *next;
if (authority->n_layers <= n)
return;
_cogl_pipeline_pre_change_notify (pipeline,
COGL_PIPELINE_STATE_LAYERS,
NULL,
FALSE);
state.keep_n = n;
state.current_pos = 0;
_cogl_pipeline_foreach_layer_internal (pipeline,
update_prune_layers_info_cb,
&state);
pipeline->differences |= COGL_PIPELINE_STATE_LAYERS;
pipeline->n_layers = n;
/* It's possible that this pipeline owns some of the layers being
* discarded, so we'll need to unlink them... */
for (l = pipeline->layer_differences; l; l = next)
{
CoglPipelineLayer *layer = l->data;
next = l->next; /* we're modifying the list we're iterating */
if (layer->index > state.first_index_to_prune)
_cogl_pipeline_remove_layer_difference (pipeline, layer, FALSE);
}
pipeline->differences |= COGL_PIPELINE_STATE_LAYERS;
}
static void
_cogl_pipeline_fragend_layer_change_notify (CoglPipeline *owner,
CoglPipelineLayer *layer,
CoglPipelineLayerState change)
{
/* NB: Although layers can have private state associated with them
* by multiple backends we know that a layer can't be *changed* if
* it has multiple dependants so if we reach here we know we only
* have a single owner and can only be associated with a single
* backend that needs to be notified of the layer change...
*/
if (owner->fragend != COGL_PIPELINE_FRAGEND_UNDEFINED &&
_cogl_pipeline_fragends[owner->fragend]->layer_pre_change_notify)
{
const CoglPipelineFragend *fragend =
_cogl_pipeline_fragends[owner->fragend];
fragend->layer_pre_change_notify (owner, layer, change);
}
}
static void
_cogl_pipeline_vertend_layer_change_notify (CoglPipeline *owner,
CoglPipelineLayer *layer,
CoglPipelineLayerState change)
{
/* NB: The comment in fragend_layer_change_notify applies here too */
if (owner->vertend != COGL_PIPELINE_VERTEND_UNDEFINED &&
_cogl_pipeline_vertends[owner->vertend]->layer_pre_change_notify)
{
const CoglPipelineVertend *vertend =
_cogl_pipeline_vertends[owner->vertend];
vertend->layer_pre_change_notify (owner, layer, change);
}
}
static void
_cogl_pipeline_progend_layer_change_notify (CoglPipeline *owner,
CoglPipelineLayer *layer,
CoglPipelineLayerState change)
{
int i;
/* Give all of the progends a chance to notice that the layer has
changed */
for (i = 0; i < COGL_PIPELINE_N_PROGENDS; i++)
if (_cogl_pipeline_progends[i]->layer_pre_change_notify)
_cogl_pipeline_progends[i]->layer_pre_change_notify (owner,
layer,
change);
}
unsigned int
_cogl_get_n_args_for_combine_func (CoglPipelineCombineFunc func)
{
switch (func)
{
case COGL_PIPELINE_COMBINE_FUNC_REPLACE:
return 1;
case COGL_PIPELINE_COMBINE_FUNC_MODULATE:
case COGL_PIPELINE_COMBINE_FUNC_ADD:
case COGL_PIPELINE_COMBINE_FUNC_ADD_SIGNED:
case COGL_PIPELINE_COMBINE_FUNC_SUBTRACT:
case COGL_PIPELINE_COMBINE_FUNC_DOT3_RGB:
case COGL_PIPELINE_COMBINE_FUNC_DOT3_RGBA:
return 2;
case COGL_PIPELINE_COMBINE_FUNC_INTERPOLATE:
return 3;
}
return 0;
}
static void
_cogl_pipeline_layer_init_multi_property_sparse_state (
CoglPipelineLayer *layer,
CoglPipelineLayerState change)
{
CoglPipelineLayer *authority;
/* Nothing to initialize in these cases since they are all comprised
* of one member which we expect to immediately be overwritten. */
if (!(change & COGL_PIPELINE_LAYER_STATE_MULTI_PROPERTY))
return;
authority = _cogl_pipeline_layer_get_authority (layer, change);
switch (change)
{
/* XXX: avoid using a default: label so we get a warning if we
* don't explicitly handle a newly defined state-group here. */
case COGL_PIPELINE_LAYER_STATE_UNIT:
case COGL_PIPELINE_LAYER_STATE_TEXTURE_TARGET:
case COGL_PIPELINE_LAYER_STATE_TEXTURE_DATA:
case COGL_PIPELINE_LAYER_STATE_POINT_SPRITE_COORDS:
case COGL_PIPELINE_LAYER_STATE_USER_MATRIX:
case COGL_PIPELINE_LAYER_STATE_COMBINE_CONSTANT:
g_return_if_reached ();
/* XXX: technically we could probably even consider these as
* single property state-groups from the pov that currently the
* corresponding property setters always update all of the values
* at the same time. */
case COGL_PIPELINE_LAYER_STATE_FILTERS:
layer->min_filter = authority->min_filter;
layer->mag_filter = authority->mag_filter;
break;
case COGL_PIPELINE_LAYER_STATE_WRAP_MODES:
layer->wrap_mode_s = authority->wrap_mode_s;
layer->wrap_mode_t = authority->wrap_mode_t;
layer->wrap_mode_p = authority->wrap_mode_p;
break;
case COGL_PIPELINE_LAYER_STATE_COMBINE:
{
int n_args;
int i;
CoglPipelineLayerBigState *src_big_state = authority->big_state;
CoglPipelineLayerBigState *dest_big_state = layer->big_state;
GLint func = src_big_state->texture_combine_rgb_func;
dest_big_state->texture_combine_rgb_func = func;
n_args = _cogl_get_n_args_for_combine_func (func);
for (i = 0; i < n_args; i++)
{
dest_big_state->texture_combine_rgb_src[i] =
src_big_state->texture_combine_rgb_src[i];
dest_big_state->texture_combine_rgb_op[i] =
src_big_state->texture_combine_rgb_op[i];
}
func = src_big_state->texture_combine_alpha_func;
dest_big_state->texture_combine_alpha_func = func;
n_args = _cogl_get_n_args_for_combine_func (func);
for (i = 0; i < n_args; i++)
{
dest_big_state->texture_combine_alpha_src[i] =
src_big_state->texture_combine_alpha_src[i];
dest_big_state->texture_combine_alpha_op[i] =
src_big_state->texture_combine_alpha_op[i];
}
break;
}
}
}
/* NB: This function will allocate a new derived layer if you are
* trying to change the state of a layer with dependants so you must
* always check the return value.
*
* If a new layer is returned it will be owned by required_owner.
*
* required_owner can only by NULL for new, currently unowned layers
* with no dependants.
*/
static CoglPipelineLayer *
_cogl_pipeline_layer_pre_change_notify (CoglPipeline *required_owner,
CoglPipelineLayer *layer,
CoglPipelineLayerState change)
{
CoglTextureUnit *unit;
/* Identify the case where the layer is new with no owner or
* dependants and so we don't need to do anything. */
if (COGL_LIST_EMPTY (&COGL_PIPELINE_NODE (layer)->children) &&
layer->owner == NULL)
goto init_layer_state;
/* We only allow a NULL required_owner for new layers */
g_return_val_if_fail (required_owner != NULL, layer);
/* Chain up:
* A modification of a layer is indirectly also a modification of
* its owner so first make sure to flush the journal of any
* references to the current owner state and if necessary perform
* a copy-on-write for the required_owner if it has dependants.
*/
_cogl_pipeline_pre_change_notify (required_owner,
COGL_PIPELINE_STATE_LAYERS,
NULL,
TRUE);
/* Unlike pipelines; layers are simply considered immutable once
* they have dependants - either direct children, or another
* pipeline as an owner.
*/
if (!COGL_LIST_EMPTY (&COGL_PIPELINE_NODE (layer)->children) ||
layer->owner != required_owner)
{
CoglPipelineLayer *new = _cogl_pipeline_layer_copy (layer);
if (layer->owner == required_owner)
_cogl_pipeline_remove_layer_difference (required_owner, layer, FALSE);
_cogl_pipeline_add_layer_difference (required_owner, new, FALSE);
cogl_object_unref (new);
layer = new;
goto init_layer_state;
}
/* Note: At this point we know there is only one pipeline dependant on
* this layer (required_owner), and there are no other layers
* dependant on this layer so it's ok to modify it. */
_cogl_pipeline_fragend_layer_change_notify (required_owner, layer, change);
_cogl_pipeline_vertend_layer_change_notify (required_owner, layer, change);
_cogl_pipeline_progend_layer_change_notify (required_owner, layer, change);
/* If the layer being changed is the same as the last layer we
* flushed to the corresponding texture unit then we keep a track of
* the changes so we can try to minimize redundant OpenGL calls if
* the same layer is flushed again.
*/
unit = _cogl_get_texture_unit (_cogl_pipeline_layer_get_unit_index (layer));
if (unit->layer == layer)
unit->layer_changes_since_flush |= change;
init_layer_state:
if (required_owner)
required_owner->age++;
if (change & COGL_PIPELINE_LAYER_STATE_NEEDS_BIG_STATE &&
!layer->has_big_state)
{
layer->big_state = g_slice_new (CoglPipelineLayerBigState);
layer->has_big_state = TRUE;
}
/* Note: conceptually we have just been notified that a single
* property value is about to change, but since some state-groups
* contain multiple properties and 'layer' is about to take over
* being the authority for the property's corresponding state-group
* we need to maintain the integrity of the other property values
* too.
*
* To ensure this we handle multi-property state-groups by copying
* all the values from the old-authority to the new...
*
* We don't have to worry about non-sparse property groups since
* we never take over being an authority for such properties so
* they automatically maintain integrity.
*/
if (change & COGL_PIPELINE_LAYER_STATE_ALL_SPARSE &&
!(layer->differences & change))
{
_cogl_pipeline_layer_init_multi_property_sparse_state (layer, change);
layer->differences |= change;
}
return layer;
}
static void
_cogl_pipeline_layer_unparent (CoglPipelineNode *layer)
{
/* Chain up */
_cogl_pipeline_node_unparent_real (layer);
}
static void
_cogl_pipeline_layer_set_parent (CoglPipelineLayer *layer,
CoglPipelineLayer *parent)
{
/* Chain up */
_cogl_pipeline_node_set_parent_real (COGL_PIPELINE_NODE (layer),
COGL_PIPELINE_NODE (parent),
_cogl_pipeline_layer_unparent,
TRUE);
}
/* XXX: This is duplicated logic; the same as for
* _cogl_pipeline_prune_redundant_ancestry it would be nice to find a
* way to consolidate these functions! */
static void
_cogl_pipeline_layer_prune_redundant_ancestry (CoglPipelineLayer *layer)
{
CoglPipelineLayer *new_parent = _cogl_pipeline_layer_get_parent (layer);
/* walk up past ancestors that are now redundant and potentially
* reparent the layer. */
while (_cogl_pipeline_layer_get_parent (new_parent) &&
(new_parent->differences | layer->differences) ==
layer->differences)
new_parent = _cogl_pipeline_layer_get_parent (new_parent);
_cogl_pipeline_layer_set_parent (layer, new_parent);
}
/*
* XXX: consider special casing layer->unit_index so it's not a sparse
* property so instead we can assume it's valid for all layer
* instances.
* - We would need to initialize ->unit_index in
* _cogl_pipeline_layer_copy ().
*
* XXX: If you use this API you should consider that the given layer
* might not be writeable and so a new derived layer will be allocated
* and modified instead. The layer modified will be returned so you
* can identify when this happens.
*/
static CoglPipelineLayer *
_cogl_pipeline_set_layer_unit (CoglPipeline *required_owner,
CoglPipelineLayer *layer,
int unit_index)
{
CoglPipelineLayerState change = COGL_PIPELINE_LAYER_STATE_UNIT;
CoglPipelineLayer *authority =
_cogl_pipeline_layer_get_authority (layer, change);
CoglPipelineLayer *new;
if (authority->unit_index == unit_index)
return layer;
new =
_cogl_pipeline_layer_pre_change_notify (required_owner,
layer,
change);
if (new != layer)
layer = new;
else
{
/* If the layer we found is currently the authority on the state
* we are changing see if we can revert to one of our ancestors
* being the authority. */
if (layer == authority &&
_cogl_pipeline_layer_get_parent (authority) != NULL)
{
CoglPipelineLayer *parent =
_cogl_pipeline_layer_get_parent (authority);
CoglPipelineLayer *old_authority =
_cogl_pipeline_layer_get_authority (parent, change);
if (old_authority->unit_index == unit_index)
{
layer->differences &= ~change;
return layer;
}
}
}
layer->unit_index = unit_index;
/* If we weren't previously the authority on this state then we need
* to extended our differences mask and so it's possible that some
* of our ancestry will now become redundant, so we aim to reparent
* ourselves if that's true... */
if (layer != authority)
{
layer->differences |= change;
_cogl_pipeline_layer_prune_redundant_ancestry (layer);
}
return layer;
}
typedef struct
{
/* The layer we are trying to find */
int layer_index;
/* The layer we find or untouched if not found */
CoglPipelineLayer *layer;
/* If the layer can't be found then a new layer should be
* inserted after this texture unit index... */
int insert_after;
/* When adding a layer we need the list of layers to shift up
* to a new texture unit. When removing we need the list of
* layers to shift down.
*
* Note: the list isn't sorted */
CoglPipelineLayer **layers_to_shift;
int n_layers_to_shift;
/* When adding a layer we don't need a complete list of
* layers_to_shift if we find a layer already corresponding to the
* layer_index. */
gboolean ignore_shift_layers_if_found;
} CoglPipelineLayerInfo;
/* Returns TRUE once we know there is nothing more to update */
static gboolean
update_layer_info (CoglPipelineLayer *layer,
CoglPipelineLayerInfo *layer_info)
{
if (layer->index == layer_info->layer_index)
{
layer_info->layer = layer;
if (layer_info->ignore_shift_layers_if_found)
return TRUE;
}
else if (layer->index < layer_info->layer_index)
{
int unit_index = _cogl_pipeline_layer_get_unit_index (layer);
layer_info->insert_after = unit_index;
}
else
layer_info->layers_to_shift[layer_info->n_layers_to_shift++] =
layer;
return FALSE;
}
/* Returns FALSE to break out of a _foreach_layer () iteration */
static gboolean
update_layer_info_cb (CoglPipelineLayer *layer,
void *user_data)
{
CoglPipelineLayerInfo *layer_info = user_data;
if (update_layer_info (layer, layer_info))
return FALSE; /* break */
else
return TRUE; /* continue */
}
static void
_cogl_pipeline_get_layer_info (CoglPipeline *pipeline,
CoglPipelineLayerInfo *layer_info)
{
/* Note: we are assuming this pipeline is a _STATE_LAYERS authority */
int n_layers = pipeline->n_layers;
int i;
/* FIXME: _cogl_pipeline_foreach_layer_internal now calls
* _cogl_pipeline_update_layers_cache anyway so this codepath is
* pointless! */
if (layer_info->ignore_shift_layers_if_found &&
pipeline->layers_cache_dirty)
{
/* The expectation is that callers of
* _cogl_pipeline_get_layer_info are likely to be modifying the
* list of layers associated with a pipeline so in this case
* where we don't have a cache of the layers and we don't
* necessarily have to iterate all the layers of the pipeline we
* use a foreach_layer callback instead of updating the cache
* and iterating that as below. */
_cogl_pipeline_foreach_layer_internal (pipeline,
update_layer_info_cb,
layer_info);
return;
}
_cogl_pipeline_update_layers_cache (pipeline);
for (i = 0; i < n_layers; i++)
{
CoglPipelineLayer *layer = pipeline->layers_cache[i];
if (update_layer_info (layer, layer_info))
return;
}
}
static CoglPipelineLayer *
_cogl_pipeline_get_layer (CoglPipeline *pipeline,
int layer_index)
{
CoglPipeline *authority =
_cogl_pipeline_get_authority (pipeline, COGL_PIPELINE_STATE_LAYERS);
CoglPipelineLayerInfo layer_info;
CoglPipelineLayer *layer;
int unit_index;
int i;
_COGL_GET_CONTEXT (ctx, NULL);
/* The layer index of the layer we want info about */
layer_info.layer_index = layer_index;
/* If a layer already exists with the given index this will be
* updated. */
layer_info.layer = NULL;
/* If a layer isn't found for the given index we'll need to know
* where to insert a new layer. */
layer_info.insert_after = -1;
/* If a layer can't be found then we'll need to insert a new layer
* and bump up the texture unit for all layers with an index
* > layer_index. */
layer_info.layers_to_shift =
g_alloca (sizeof (CoglPipelineLayer *) * authority->n_layers);
layer_info.n_layers_to_shift = 0;
/* If an exact match is found though we don't need a complete
* list of layers with indices > layer_index... */
layer_info.ignore_shift_layers_if_found = TRUE;
_cogl_pipeline_get_layer_info (authority, &layer_info);
if (layer_info.layer)
return layer_info.layer;
unit_index = layer_info.insert_after + 1;
if (unit_index == 0)
layer = _cogl_pipeline_layer_copy (ctx->default_layer_0);
else
{
CoglPipelineLayer *new;
layer = _cogl_pipeline_layer_copy (ctx->default_layer_n);
new = _cogl_pipeline_set_layer_unit (NULL, layer, unit_index);
/* Since we passed a newly allocated layer we wouldn't expect
* _set_layer_unit() to have to allocate *another* layer. */
g_assert (new == layer);
}
layer->index = layer_index;
for (i = 0; i < layer_info.n_layers_to_shift; i++)
{
CoglPipelineLayer *shift_layer = layer_info.layers_to_shift[i];
unit_index = _cogl_pipeline_layer_get_unit_index (shift_layer);
_cogl_pipeline_set_layer_unit (pipeline, shift_layer, unit_index + 1);
/* NB: shift_layer may not be writeable so _set_layer_unit()
* will allocate a derived layer internally which will become
* owned by pipeline. Check the return value if we need to do
* anything else with this layer. */
}
_cogl_pipeline_add_layer_difference (pipeline, layer, TRUE);
cogl_object_unref (layer);
return layer;
}
CoglHandle
_cogl_pipeline_layer_get_texture_real (CoglPipelineLayer *layer)
{
CoglPipelineLayer *authority =
_cogl_pipeline_layer_get_authority (layer,
COGL_PIPELINE_LAYER_STATE_TEXTURE_DATA);
return authority->texture;
}
CoglHandle
_cogl_pipeline_get_layer_texture (CoglPipeline *pipeline,
int layer_index)
{
CoglPipelineLayer *layer =
_cogl_pipeline_get_layer (pipeline, layer_index);
return _cogl_pipeline_layer_get_texture (layer);
}
static void
_cogl_pipeline_prune_empty_layer_difference (CoglPipeline *layers_authority,
CoglPipelineLayer *layer)
{
/* Find the GList link that references the empty layer */
GList *link = g_list_find (layers_authority->layer_differences, layer);
/* No pipeline directly owns the root node layer so this is safe... */
CoglPipelineLayer *layer_parent = _cogl_pipeline_layer_get_parent (layer);
CoglPipelineLayerInfo layer_info;
CoglPipeline *old_layers_authority;
g_return_if_fail (link != NULL);
/* If the layer's parent doesn't have an owner then we can simply
* take ownership ourselves and drop our reference on the empty
* layer. We don't want to take ownership of the root node layer so
* we also need to verify that the parent has a parent
*/
if (layer_parent->index == layer->index && layer_parent->owner == NULL &&
_cogl_pipeline_layer_get_parent (layer_parent) != NULL)
{
cogl_object_ref (layer_parent);
layer_parent->owner = layers_authority;
link->data = layer_parent;
cogl_object_unref (layer);
recursively_free_layer_caches (layers_authority);
return;
}
/* Now we want to find the layer that would become the authority for
* layer->index if we were to remove layer from
* layers_authority->layer_differences
*/
/* The layer index of the layer we want info about */
layer_info.layer_index = layer->index;
/* If a layer already exists with the given index this will be
* updated. */
layer_info.layer = NULL;
/* If a layer can't be found then we'll need to insert a new layer
* and bump up the texture unit for all layers with an index
* > layer_index. */
layer_info.layers_to_shift =
g_alloca (sizeof (CoglPipelineLayer *) * layers_authority->n_layers);
layer_info.n_layers_to_shift = 0;
/* If an exact match is found though we don't need a complete
* list of layers with indices > layer_index... */
layer_info.ignore_shift_layers_if_found = TRUE;
/* We know the default/root pipeline isn't a LAYERS authority so it's
* safe to use the result of _cogl_pipeline_get_parent (layers_authority)
* without checking it.
*/
old_layers_authority =
_cogl_pipeline_get_authority (_cogl_pipeline_get_parent (layers_authority),
COGL_PIPELINE_STATE_LAYERS);
_cogl_pipeline_get_layer_info (old_layers_authority, &layer_info);
/* If layer is the defining layer for the corresponding ->index then
* we can't get rid of it. */
if (!layer_info.layer)
return;
/* If the layer that would become the authority for layer->index is
* _cogl_pipeline_layer_get_parent (layer) then we can simply remove the
* layer difference. */
if (layer_info.layer == _cogl_pipeline_layer_get_parent (layer))
{
_cogl_pipeline_remove_layer_difference (layers_authority, layer, FALSE);
_cogl_pipeline_try_reverting_layers_authority (layers_authority,
old_layers_authority);
}
}
static void
_cogl_pipeline_set_layer_texture_target (CoglPipeline *pipeline,
int layer_index,
GLenum target)
{
CoglPipelineLayerState change = COGL_PIPELINE_LAYER_STATE_TEXTURE_TARGET;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
CoglPipelineLayer *new;
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, change);
if (target == authority->target)
return;
new = _cogl_pipeline_layer_pre_change_notify (pipeline, layer, change);
if (new != layer)
layer = new;
else
{
/* If the original layer we found is currently the authority on
* the state we are changing see if we can revert to one of our
* ancestors being the authority. */
if (layer == authority &&
_cogl_pipeline_layer_get_parent (authority) != NULL)
{
CoglPipelineLayer *parent =
_cogl_pipeline_layer_get_parent (authority);
CoglPipelineLayer *old_authority =
_cogl_pipeline_layer_get_authority (parent, change);
if (old_authority->target == target)
{
layer->differences &= ~change;
g_assert (layer->owner == pipeline);
if (layer->differences == 0)
_cogl_pipeline_prune_empty_layer_difference (pipeline,
layer);
goto changed;
}
}
}
layer->target = target;
/* If we weren't previously the authority on this state then we need
* to extended our differences mask and so it's possible that some
* of our ancestry will now become redundant, so we aim to reparent
* ourselves if that's true... */
if (layer != authority)
{
layer->differences |= change;
_cogl_pipeline_layer_prune_redundant_ancestry (layer);
}
changed:
_cogl_pipeline_update_blend_enable (pipeline, COGL_PIPELINE_STATE_LAYERS);
}
static void
_cogl_pipeline_set_layer_texture_data (CoglPipeline *pipeline,
int layer_index,
CoglHandle texture)
{
CoglPipelineLayerState change = COGL_PIPELINE_LAYER_STATE_TEXTURE_DATA;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
CoglPipelineLayer *new;
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, change);
if (authority->texture == texture)
return;
new = _cogl_pipeline_layer_pre_change_notify (pipeline, layer, change);
if (new != layer)
layer = new;
else
{
/* If the original layer we found is currently the authority on
* the state we are changing see if we can revert to one of our
* ancestors being the authority. */
if (layer == authority &&
_cogl_pipeline_layer_get_parent (authority) != NULL)
{
CoglPipelineLayer *parent =
_cogl_pipeline_layer_get_parent (authority);
CoglPipelineLayer *old_authority =
_cogl_pipeline_layer_get_authority (parent, change);
if (old_authority->texture == texture)
{
layer->differences &= ~change;
if (layer->texture != COGL_INVALID_HANDLE)
cogl_handle_unref (layer->texture);
g_assert (layer->owner == pipeline);
if (layer->differences == 0)
_cogl_pipeline_prune_empty_layer_difference (pipeline,
layer);
goto changed;
}
}
}
if (texture != COGL_INVALID_HANDLE)
cogl_handle_ref (texture);
if (layer == authority &&
layer->texture != COGL_INVALID_HANDLE)
cogl_handle_unref (layer->texture);
layer->texture = texture;
/* If we weren't previously the authority on this state then we need
* to extended our differences mask and so it's possible that some
* of our ancestry will now become redundant, so we aim to reparent
* ourselves if that's true... */
if (layer != authority)
{
layer->differences |= change;
_cogl_pipeline_layer_prune_redundant_ancestry (layer);
}
changed:
_cogl_pipeline_update_blend_enable (pipeline, COGL_PIPELINE_STATE_LAYERS);
}
/* A convenience for querying the target of a given texture that
* notably returns 0 for NULL textures - so we can say that a layer
* with no associated CoglTexture will have a texture target of 0.
*/
static GLenum
get_texture_target (CoglHandle texture)
{
GLuint ignore_handle;
GLenum gl_target;
g_return_val_if_fail (texture, 0);
cogl_texture_get_gl_texture (texture, &ignore_handle, &gl_target);
return gl_target;
}
void
cogl_pipeline_set_layer_texture (CoglPipeline *pipeline,
int layer_index,
CoglHandle texture)
{
/* For the convenience of fragend code we separate texture state
* into the "target" and the "data", and setting a layer texture
* updates both of these properties.
*
* One example for why this is helpful is that the fragends may
* cache programs they generate and want to re-use those programs
* with all pipelines having equivalent fragment processing state.
* For the sake of determining if pipelines have equivalent fragment
* processing state we don't need to compare that the same
* underlying texture objects are referenced by the pipelines but we
* do need to see if they use the same texture targets. Making this
* distinction is much simpler if they are in different state
* groups.
*
* Note: if a NULL texture is set then we leave the target unchanged
* so we can avoid needlessly invalidating any associated fragment
* program.
*/
if (texture)
_cogl_pipeline_set_layer_texture_target (pipeline, layer_index,
get_texture_target (texture));
_cogl_pipeline_set_layer_texture_data (pipeline, layer_index, texture);
}
typedef struct
{
int i;
CoglPipeline *pipeline;
unsigned long fallback_layers;
} CoglPipelineFallbackState;
static gboolean
fallback_layer_cb (CoglPipelineLayer *layer, void *user_data)
{
CoglPipelineFallbackState *state = user_data;
CoglPipeline *pipeline = state->pipeline;
CoglHandle texture = _cogl_pipeline_layer_get_texture (layer);
GLenum gl_target;
COGL_STATIC_COUNTER (layer_fallback_counter,
"layer fallback counter",
"Increments each time a layer's texture is "
"forced to a fallback texture",
0 /* no application private data */);
_COGL_GET_CONTEXT (ctx, FALSE);
if (!(state->fallback_layers & 1<<state->i))
return TRUE;
COGL_COUNTER_INC (_cogl_uprof_context, layer_fallback_counter);
if (G_LIKELY (texture != COGL_INVALID_HANDLE))
cogl_texture_get_gl_texture (texture, NULL, &gl_target);
else
gl_target = GL_TEXTURE_2D;
if (gl_target == GL_TEXTURE_2D)
texture = ctx->default_gl_texture_2d_tex;
#ifdef HAVE_COGL_GL
else if (gl_target == GL_TEXTURE_RECTANGLE_ARB)
texture = ctx->default_gl_texture_rect_tex;
#endif
else
{
g_warning ("We don't have a fallback texture we can use to fill "
"in for an invalid pipeline layer, since it was "
"using an unsupported texture target ");
/* might get away with this... */
texture = ctx->default_gl_texture_2d_tex;
}
cogl_pipeline_set_layer_texture (pipeline, layer->index, texture);
state->i++;
return TRUE;
}
void
_cogl_pipeline_set_layer_wrap_modes (CoglPipeline *pipeline,
CoglPipelineLayer *layer,
CoglPipelineLayer *authority,
CoglPipelineWrapModeInternal wrap_mode_s,
CoglPipelineWrapModeInternal wrap_mode_t,
CoglPipelineWrapModeInternal wrap_mode_p)
{
CoglPipelineLayer *new;
CoglPipelineLayerState change = COGL_PIPELINE_LAYER_STATE_WRAP_MODES;
if (authority->wrap_mode_s == wrap_mode_s &&
authority->wrap_mode_t == wrap_mode_t &&
authority->wrap_mode_p == wrap_mode_p)
return;
new = _cogl_pipeline_layer_pre_change_notify (pipeline, layer, change);
if (new != layer)
layer = new;
else
{
/* If the original layer we found is currently the authority on
* the state we are changing see if we can revert to one of our
* ancestors being the authority. */
if (layer == authority &&
_cogl_pipeline_layer_get_parent (authority) != NULL)
{
CoglPipelineLayer *parent =
_cogl_pipeline_layer_get_parent (authority);
CoglPipelineLayer *old_authority =
_cogl_pipeline_layer_get_authority (parent, change);
if (old_authority->wrap_mode_s == wrap_mode_s &&
old_authority->wrap_mode_t == wrap_mode_t &&
old_authority->wrap_mode_p == wrap_mode_p)
{
layer->differences &= ~change;
g_assert (layer->owner == pipeline);
if (layer->differences == 0)
_cogl_pipeline_prune_empty_layer_difference (pipeline,
layer);
return;
}
}
}
layer->wrap_mode_s = wrap_mode_s;
layer->wrap_mode_t = wrap_mode_t;
layer->wrap_mode_p = wrap_mode_p;
/* If we weren't previously the authority on this state then we need
* to extended our differences mask and so it's possible that some
* of our ancestry will now become redundant, so we aim to reparent
* ourselves if that's true... */
if (layer != authority)
{
layer->differences |= change;
_cogl_pipeline_layer_prune_redundant_ancestry (layer);
}
}
static CoglPipelineWrapModeInternal
public_to_internal_wrap_mode (CoglPipelineWrapMode mode)
{
return (CoglPipelineWrapModeInternal)mode;
}
static CoglPipelineWrapMode
internal_to_public_wrap_mode (CoglPipelineWrapModeInternal internal_mode)
{
g_return_val_if_fail (internal_mode !=
COGL_PIPELINE_WRAP_MODE_INTERNAL_CLAMP_TO_BORDER,
COGL_PIPELINE_WRAP_MODE_AUTOMATIC);
return (CoglPipelineWrapMode)internal_mode;
}
void
cogl_pipeline_set_layer_wrap_mode_s (CoglPipeline *pipeline,
int layer_index,
CoglPipelineWrapMode mode)
{
CoglPipelineLayerState change = COGL_PIPELINE_LAYER_STATE_WRAP_MODES;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
CoglPipelineWrapModeInternal internal_mode =
public_to_internal_wrap_mode (mode);
g_return_if_fail (cogl_is_pipeline (pipeline));
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, change);
_cogl_pipeline_set_layer_wrap_modes (pipeline, layer, authority,
internal_mode,
authority->wrap_mode_t,
authority->wrap_mode_p);
}
void
cogl_pipeline_set_layer_wrap_mode_t (CoglPipeline *pipeline,
int layer_index,
CoglPipelineWrapMode mode)
{
CoglPipelineLayerState change = COGL_PIPELINE_LAYER_STATE_WRAP_MODES;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
CoglPipelineWrapModeInternal internal_mode =
public_to_internal_wrap_mode (mode);
g_return_if_fail (cogl_is_pipeline (pipeline));
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, change);
_cogl_pipeline_set_layer_wrap_modes (pipeline, layer, authority,
authority->wrap_mode_s,
internal_mode,
authority->wrap_mode_p);
}
/* The rationale for naming the third texture coordinate 'p' instead
of OpenGL's usual 'r' is that 'r' conflicts with the usual naming
of the 'red' component when treating a vector as a color. Under
GLSL this is awkward because the texture swizzling for a vector
uses a single letter for each component and the names for colors,
textures and positions are synonymous. GLSL works around this by
naming the components of the texture s, t, p and q. Cogl already
effectively already exposes this naming because it exposes GLSL so
it makes sense to use that naming consistently. Another alternative
could be u, v and w. This is what Blender and Direct3D use. However
the w component conflicts with the w component of a position
vertex. */
void
cogl_pipeline_set_layer_wrap_mode_p (CoglPipeline *pipeline,
int layer_index,
CoglPipelineWrapMode mode)
{
CoglPipelineLayerState change = COGL_PIPELINE_LAYER_STATE_WRAP_MODES;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
CoglPipelineWrapModeInternal internal_mode =
public_to_internal_wrap_mode (mode);
g_return_if_fail (cogl_is_pipeline (pipeline));
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, change);
_cogl_pipeline_set_layer_wrap_modes (pipeline, layer, authority,
authority->wrap_mode_s,
authority->wrap_mode_t,
internal_mode);
}
void
cogl_pipeline_set_layer_wrap_mode (CoglPipeline *pipeline,
int layer_index,
CoglPipelineWrapMode mode)
{
CoglPipelineLayerState change = COGL_PIPELINE_LAYER_STATE_WRAP_MODES;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
CoglPipelineWrapModeInternal internal_mode =
public_to_internal_wrap_mode (mode);
g_return_if_fail (cogl_is_pipeline (pipeline));
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, change);
_cogl_pipeline_set_layer_wrap_modes (pipeline, layer, authority,
internal_mode,
internal_mode,
internal_mode);
/* XXX: I wonder if we should really be duplicating the mode into
* the 'r' wrap mode too? */
}
/* FIXME: deprecate this API */
CoglPipelineWrapMode
_cogl_pipeline_layer_get_wrap_mode_s (CoglPipelineLayer *layer)
{
CoglPipelineLayerState change = COGL_PIPELINE_LAYER_STATE_WRAP_MODES;
CoglPipelineLayer *authority;
g_return_val_if_fail (_cogl_is_pipeline_layer (layer), FALSE);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, change);
return internal_to_public_wrap_mode (authority->wrap_mode_s);
}
CoglPipelineWrapMode
cogl_pipeline_get_layer_wrap_mode_s (CoglPipeline *pipeline, int layer_index)
{
CoglPipelineLayer *layer;
g_return_val_if_fail (cogl_is_pipeline (pipeline), FALSE);
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* FIXME: we shouldn't ever construct a layer in a getter function */
return _cogl_pipeline_layer_get_wrap_mode_s (layer);
}
/* FIXME: deprecate this API */
CoglPipelineWrapMode
_cogl_pipeline_layer_get_wrap_mode_t (CoglPipelineLayer *layer)
{
CoglPipelineLayerState change = COGL_PIPELINE_LAYER_STATE_WRAP_MODES;
CoglPipelineLayer *authority;
g_return_val_if_fail (_cogl_is_pipeline_layer (layer), FALSE);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, change);
return internal_to_public_wrap_mode (authority->wrap_mode_t);
}
CoglPipelineWrapMode
cogl_pipeline_get_layer_wrap_mode_t (CoglPipeline *pipeline, int layer_index)
{
CoglPipelineLayer *layer;
g_return_val_if_fail (cogl_is_pipeline (pipeline), FALSE);
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* FIXME: we shouldn't ever construct a layer in a getter function */
return _cogl_pipeline_layer_get_wrap_mode_t (layer);
}
CoglPipelineWrapMode
_cogl_pipeline_layer_get_wrap_mode_p (CoglPipelineLayer *layer)
{
CoglPipelineLayerState change = COGL_PIPELINE_LAYER_STATE_WRAP_MODES;
CoglPipelineLayer *authority =
_cogl_pipeline_layer_get_authority (layer, change);
return internal_to_public_wrap_mode (authority->wrap_mode_p);
}
CoglPipelineWrapMode
cogl_pipeline_get_layer_wrap_mode_p (CoglPipeline *pipeline, int layer_index)
{
CoglPipelineLayer *layer;
g_return_val_if_fail (cogl_is_pipeline (pipeline), FALSE);
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
return _cogl_pipeline_layer_get_wrap_mode_p (layer);
}
void
_cogl_pipeline_layer_get_wrap_modes (CoglPipelineLayer *layer,
CoglPipelineWrapModeInternal *wrap_mode_s,
CoglPipelineWrapModeInternal *wrap_mode_t,
CoglPipelineWrapModeInternal *wrap_mode_p)
{
CoglPipelineLayer *authority =
_cogl_pipeline_layer_get_authority (layer,
COGL_PIPELINE_LAYER_STATE_WRAP_MODES);
*wrap_mode_s = authority->wrap_mode_s;
*wrap_mode_t = authority->wrap_mode_t;
*wrap_mode_p = authority->wrap_mode_p;
}
gboolean
cogl_pipeline_set_layer_point_sprite_coords_enabled (CoglPipeline *pipeline,
int layer_index,
gboolean enable,
GError **error)
{
CoglPipelineLayerState change =
COGL_PIPELINE_LAYER_STATE_POINT_SPRITE_COORDS;
CoglPipelineLayer *layer;
CoglPipelineLayer *new;
CoglPipelineLayer *authority;
g_return_val_if_fail (cogl_is_pipeline (pipeline), FALSE);
/* Don't allow point sprite coordinates to be enabled if the driver
doesn't support it */
if (enable && !cogl_features_available (COGL_FEATURE_POINT_SPRITE))
{
if (error)
{
g_set_error (error, COGL_ERROR, COGL_ERROR_UNSUPPORTED,
"Point sprite texture coordinates are enabled "
"for a layer but the GL driver does not support it.");
}
else
{
static gboolean warning_seen = FALSE;
if (!warning_seen)
g_warning ("Point sprite texture coordinates are enabled "
"for a layer but the GL driver does not support it.");
warning_seen = TRUE;
}
return FALSE;
}
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, change);
if (authority->big_state->point_sprite_coords == enable)
return TRUE;
new = _cogl_pipeline_layer_pre_change_notify (pipeline, layer, change);
if (new != layer)
layer = new;
else
{
/* If the original layer we found is currently the authority on
* the state we are changing see if we can revert to one of our
* ancestors being the authority. */
if (layer == authority &&
_cogl_pipeline_layer_get_parent (authority) != NULL)
{
CoglPipelineLayer *parent =
_cogl_pipeline_layer_get_parent (authority);
CoglPipelineLayer *old_authority =
_cogl_pipeline_layer_get_authority (parent, change);
if (old_authority->big_state->point_sprite_coords == enable)
{
layer->differences &= ~change;
g_assert (layer->owner == pipeline);
if (layer->differences == 0)
_cogl_pipeline_prune_empty_layer_difference (pipeline,
layer);
return TRUE;
}
}
}
layer->big_state->point_sprite_coords = enable;
/* If we weren't previously the authority on this state then we need
* to extended our differences mask and so it's possible that some
* of our ancestry will now become redundant, so we aim to reparent
* ourselves if that's true... */
if (layer != authority)
{
layer->differences |= change;
_cogl_pipeline_layer_prune_redundant_ancestry (layer);
}
return TRUE;
}
gboolean
cogl_pipeline_get_layer_point_sprite_coords_enabled (CoglPipeline *pipeline,
int layer_index)
{
CoglPipelineLayerState change =
COGL_PIPELINE_LAYER_STATE_POINT_SPRITE_COORDS;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
g_return_val_if_fail (cogl_is_pipeline (pipeline), FALSE);
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* FIXME: we shouldn't ever construct a layer in a getter function */
authority = _cogl_pipeline_layer_get_authority (layer, change);
return authority->big_state->point_sprite_coords;
}
typedef struct
{
CoglPipeline *pipeline;
CoglHandle texture;
} CoglPipelineOverrideLayerState;
static gboolean
override_layer_texture_cb (CoglPipelineLayer *layer, void *user_data)
{
CoglPipelineOverrideLayerState *state = user_data;
cogl_pipeline_set_layer_texture (state->pipeline,
layer->index,
state->texture);
return TRUE;
}
void
_cogl_pipeline_apply_overrides (CoglPipeline *pipeline,
CoglPipelineFlushOptions *options)
{
COGL_STATIC_COUNTER (apply_overrides_counter,
"pipeline overrides counter",
"Increments each time we have to apply "
"override options to a pipeline",
0 /* no application private data */);
COGL_COUNTER_INC (_cogl_uprof_context, apply_overrides_counter);
if (options->flags & COGL_PIPELINE_FLUSH_DISABLE_MASK)
{
int i;
/* NB: we can assume that once we see one bit to disable
* a layer, all subsequent layers are also disabled. */
for (i = 0; i < 32 && options->disable_layers & (1<<i); i++)
;
_cogl_pipeline_prune_to_n_layers (pipeline, i);
}
if (options->flags & COGL_PIPELINE_FLUSH_FALLBACK_MASK)
{
CoglPipelineFallbackState state;
state.i = 0;
state.pipeline = pipeline;
state.fallback_layers = options->fallback_layers;
_cogl_pipeline_foreach_layer_internal (pipeline,
fallback_layer_cb,
&state);
}
if (options->flags & COGL_PIPELINE_FLUSH_LAYER0_OVERRIDE)
{
CoglPipelineOverrideLayerState state;
_cogl_pipeline_prune_to_n_layers (pipeline, 1);
/* NB: we are overriding the first layer, but we don't know
* the user's given layer_index, which is why we use
* _cogl_pipeline_foreach_layer_internal() here even though we know
* there's only one layer. */
state.pipeline = pipeline;
state.texture = options->layer0_override_texture;
_cogl_pipeline_foreach_layer_internal (pipeline,
override_layer_texture_cb,
&state);
}
}
static gboolean
_cogl_pipeline_layer_texture_target_equal (CoglPipelineLayer *authority0,
CoglPipelineLayer *authority1,
CoglPipelineEvalFlags flags)
{
return authority0->target == authority1->target;
}
static gboolean
_cogl_pipeline_layer_texture_data_equal (CoglPipelineLayer *authority0,
CoglPipelineLayer *authority1,
CoglPipelineEvalFlags flags)
{
GLuint gl_handle0, gl_handle1;
cogl_texture_get_gl_texture (authority0->texture, &gl_handle0, NULL);
cogl_texture_get_gl_texture (authority1->texture, &gl_handle1, NULL);
return gl_handle0 == gl_handle1;
}
/* Determine the mask of differences between two layers.
*
* XXX: If layers and pipelines could both be cast to a common Tree
* type of some kind then we could have a unified
* compare_differences() function.
*/
unsigned long
_cogl_pipeline_layer_compare_differences (CoglPipelineLayer *layer0,
CoglPipelineLayer *layer1)
{
CoglPipelineLayer *node0;
CoglPipelineLayer *node1;
int len0;
int len1;
int len0_index;
int len1_index;
int count;
int i;
CoglPipelineLayer *common_ancestor = NULL;
unsigned long layers_difference = 0;
_COGL_GET_CONTEXT (ctx, 0);
/* Algorithm:
*
* 1) Walk the ancestors of each layer to the root node, adding a
* pointer to each ancester node to two GArrays:
* ctx->pipeline0_nodes, and ctx->pipeline1_nodes.
*
* 2) Compare the arrays to find the nodes where they stop to
* differ.
*
* 3) For each array now iterate from index 0 to the first node of
* difference ORing that nodes ->difference mask into the final
* pipeline_differences mask.
*/
g_array_set_size (ctx->pipeline0_nodes, 0);
g_array_set_size (ctx->pipeline1_nodes, 0);
for (node0 = layer0; node0; node0 = _cogl_pipeline_layer_get_parent (node0))
g_array_append_vals (ctx->pipeline0_nodes, &node0, 1);
for (node1 = layer1; node1; node1 = _cogl_pipeline_layer_get_parent (node1))
g_array_append_vals (ctx->pipeline1_nodes, &node1, 1);
len0 = ctx->pipeline0_nodes->len;
len1 = ctx->pipeline1_nodes->len;
/* There's no point looking at the last entries since we know both
* layers must have the same default layer as their root node. */
len0_index = len0 - 2;
len1_index = len1 - 2;
count = MIN (len0, len1) - 1;
for (i = 0; i < count; i++)
{
node0 = g_array_index (ctx->pipeline0_nodes,
CoglPipelineLayer *, len0_index--);
node1 = g_array_index (ctx->pipeline1_nodes,
CoglPipelineLayer *, len1_index--);
if (node0 != node1)
{
common_ancestor = _cogl_pipeline_layer_get_parent (node0);
break;
}
}
/* If we didn't already find the first the common_ancestor ancestor
* that's because one pipeline is a direct descendant of the other
* and in this case the first common ancestor is the last node we
* looked at. */
if (!common_ancestor)
common_ancestor = node0;
count = len0 - 1;
for (i = 0; i < count; i++)
{
node0 = g_array_index (ctx->pipeline0_nodes, CoglPipelineLayer *, i);
if (node0 == common_ancestor)
break;
layers_difference |= node0->differences;
}
count = len1 - 1;
for (i = 0; i < count; i++)
{
node1 = g_array_index (ctx->pipeline1_nodes, CoglPipelineLayer *, i);
if (node1 == common_ancestor)
break;
layers_difference |= node1->differences;
}
return layers_difference;
}
static gboolean
_cogl_pipeline_layer_combine_state_equal (CoglPipelineLayer *authority0,
CoglPipelineLayer *authority1)
{
CoglPipelineLayerBigState *big_state0 = authority0->big_state;
CoglPipelineLayerBigState *big_state1 = authority1->big_state;
int n_args;
int i;
if (big_state0->texture_combine_rgb_func !=
big_state1->texture_combine_rgb_func)
return FALSE;
if (big_state0->texture_combine_alpha_func !=
big_state1->texture_combine_alpha_func)
return FALSE;
n_args =
_cogl_get_n_args_for_combine_func (big_state0->texture_combine_rgb_func);
for (i = 0; i < n_args; i++)
{
if ((big_state0->texture_combine_rgb_src[i] !=
big_state1->texture_combine_rgb_src[i]) ||
(big_state0->texture_combine_rgb_op[i] !=
big_state1->texture_combine_rgb_op[i]))
return FALSE;
}
n_args =
_cogl_get_n_args_for_combine_func (big_state0->texture_combine_alpha_func);
for (i = 0; i < n_args; i++)
{
if ((big_state0->texture_combine_alpha_src[i] !=
big_state1->texture_combine_alpha_src[i]) ||
(big_state0->texture_combine_alpha_op[i] !=
big_state1->texture_combine_alpha_op[i]))
return FALSE;
}
return TRUE;
}
static gboolean
_cogl_pipeline_layer_combine_constant_equal (CoglPipelineLayer *authority0,
CoglPipelineLayer *authority1)
{
return memcmp (authority0->big_state->texture_combine_constant,
authority1->big_state->texture_combine_constant,
sizeof (float) * 4) == 0 ? TRUE : FALSE;
}
static gboolean
_cogl_pipeline_layer_filters_equal (CoglPipelineLayer *authority0,
CoglPipelineLayer *authority1)
{
if (authority0->mag_filter != authority1->mag_filter)
return FALSE;
if (authority0->min_filter != authority1->min_filter)
return FALSE;
return TRUE;
}
static gboolean
compare_wrap_mode_equal (CoglPipelineWrapMode wrap_mode0,
CoglPipelineWrapMode wrap_mode1)
{
/* We consider AUTOMATIC to be equivalent to CLAMP_TO_EDGE because
the primitives code is expected to override this to something
else if it wants it to be behave any other way */
if (wrap_mode0 == COGL_PIPELINE_WRAP_MODE_AUTOMATIC)
wrap_mode0 = COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE;
if (wrap_mode1 == COGL_PIPELINE_WRAP_MODE_AUTOMATIC)
wrap_mode1 = COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE;
return wrap_mode0 == wrap_mode1;
}
static gboolean
_cogl_pipeline_layer_wrap_modes_equal (CoglPipelineLayer *authority0,
CoglPipelineLayer *authority1)
{
if (!compare_wrap_mode_equal (authority0->wrap_mode_s,
authority1->wrap_mode_s) ||
!compare_wrap_mode_equal (authority0->wrap_mode_t,
authority1->wrap_mode_t) ||
!compare_wrap_mode_equal (authority0->wrap_mode_p,
authority1->wrap_mode_p))
return FALSE;
return TRUE;
}
static gboolean
_cogl_pipeline_layer_user_matrix_equal (CoglPipelineLayer *authority0,
CoglPipelineLayer *authority1)
{
CoglPipelineLayerBigState *big_state0 = authority0->big_state;
CoglPipelineLayerBigState *big_state1 = authority1->big_state;
if (!cogl_matrix_equal (&big_state0->matrix, &big_state1->matrix))
return FALSE;
return TRUE;
}
static gboolean
_cogl_pipeline_layer_point_sprite_coords_equal (CoglPipelineLayer *authority0,
CoglPipelineLayer *authority1)
{
CoglPipelineLayerBigState *big_state0 = authority0->big_state;
CoglPipelineLayerBigState *big_state1 = authority1->big_state;
return big_state0->point_sprite_coords == big_state1->point_sprite_coords;
}
typedef gboolean
(*CoglPipelineLayerStateComparitor) (CoglPipelineLayer *authority0,
CoglPipelineLayer *authority1);
static gboolean
layer_state_equal (CoglPipelineLayerStateIndex state_index,
CoglPipelineLayer **authorities0,
CoglPipelineLayer **authorities1,
CoglPipelineLayerStateComparitor comparitor)
{
return comparitor (authorities0[state_index], authorities1[state_index]);
}
static void
_cogl_pipeline_layer_resolve_authorities (CoglPipelineLayer *layer,
unsigned long differences,
CoglPipelineLayer **authorities)
{
unsigned long remaining = differences;
CoglPipelineLayer *authority = layer;
do
{
unsigned long found = authority->differences & remaining;
int i;
if (found == 0)
continue;
for (i = 0; TRUE; i++)
{
unsigned long state = (1L<<i);
if (state & found)
authorities[i] = authority;
else if (state > found)
break;
}
remaining &= ~found;
if (remaining == 0)
return;
}
while ((authority = _cogl_pipeline_layer_get_parent (authority)));
g_assert (remaining == 0);
}
static gboolean
_cogl_pipeline_layer_equal (CoglPipelineLayer *layer0,
CoglPipelineLayer *layer1,
unsigned long differences_mask,
CoglPipelineEvalFlags flags)
{
unsigned long layers_difference;
CoglPipelineLayer *authorities0[COGL_PIPELINE_LAYER_STATE_SPARSE_COUNT];
CoglPipelineLayer *authorities1[COGL_PIPELINE_LAYER_STATE_SPARSE_COUNT];
if (layer0 == layer1)
return TRUE;
layers_difference =
_cogl_pipeline_layer_compare_differences (layer0, layer1);
/* Only compare the sparse state groups requested by the caller... */
layers_difference &= differences_mask;
_cogl_pipeline_layer_resolve_authorities (layer0,
layers_difference,
authorities0);
_cogl_pipeline_layer_resolve_authorities (layer1,
layers_difference,
authorities1);
if (layers_difference & COGL_PIPELINE_LAYER_STATE_TEXTURE_TARGET)
{
CoglPipelineLayerStateIndex state_index =
COGL_PIPELINE_LAYER_STATE_TEXTURE_TARGET_INDEX;
if (!_cogl_pipeline_layer_texture_target_equal (authorities0[state_index],
authorities1[state_index],
flags))
return FALSE;
}
if (layers_difference & COGL_PIPELINE_LAYER_STATE_TEXTURE_DATA)
{
CoglPipelineLayerStateIndex state_index =
COGL_PIPELINE_LAYER_STATE_TEXTURE_DATA_INDEX;
if (!_cogl_pipeline_layer_texture_data_equal (authorities0[state_index],
authorities1[state_index],
flags))
return FALSE;
}
if (layers_difference & COGL_PIPELINE_LAYER_STATE_COMBINE &&
!layer_state_equal (COGL_PIPELINE_LAYER_STATE_COMBINE_INDEX,
authorities0, authorities1,
_cogl_pipeline_layer_combine_state_equal))
return FALSE;
if (layers_difference & COGL_PIPELINE_LAYER_STATE_COMBINE_CONSTANT &&
!layer_state_equal (COGL_PIPELINE_LAYER_STATE_COMBINE_CONSTANT_INDEX,
authorities0, authorities1,
_cogl_pipeline_layer_combine_constant_equal))
return FALSE;
if (layers_difference & COGL_PIPELINE_LAYER_STATE_FILTERS &&
!layer_state_equal (COGL_PIPELINE_LAYER_STATE_FILTERS_INDEX,
authorities0, authorities1,
_cogl_pipeline_layer_filters_equal))
return FALSE;
if (layers_difference & COGL_PIPELINE_LAYER_STATE_WRAP_MODES &&
!layer_state_equal (COGL_PIPELINE_LAYER_STATE_WRAP_MODES_INDEX,
authorities0, authorities1,
_cogl_pipeline_layer_wrap_modes_equal))
return FALSE;
if (layers_difference & COGL_PIPELINE_LAYER_STATE_USER_MATRIX &&
!layer_state_equal (COGL_PIPELINE_LAYER_STATE_USER_MATRIX_INDEX,
authorities0, authorities1,
_cogl_pipeline_layer_user_matrix_equal))
return FALSE;
if (layers_difference & COGL_PIPELINE_LAYER_STATE_POINT_SPRITE_COORDS &&
!layer_state_equal (COGL_PIPELINE_LAYER_STATE_POINT_SPRITE_COORDS_INDEX,
authorities0, authorities1,
_cogl_pipeline_layer_point_sprite_coords_equal))
return FALSE;
return TRUE;
}
static gboolean
_cogl_pipeline_layers_equal (CoglPipeline *authority0,
CoglPipeline *authority1,
unsigned long differences,
CoglPipelineEvalFlags flags)
{
int i;
if (authority0->n_layers != authority1->n_layers)
return FALSE;
_cogl_pipeline_update_layers_cache (authority0);
_cogl_pipeline_update_layers_cache (authority1);
for (i = 0; i < authority0->n_layers; i++)
{
if (!_cogl_pipeline_layer_equal (authority0->layers_cache[i],
authority1->layers_cache[i],
differences,
flags))
return FALSE;
}
return TRUE;
}
/* Determine the mask of differences between two pipelines */
unsigned long
_cogl_pipeline_compare_differences (CoglPipeline *pipeline0,
CoglPipeline *pipeline1)
{
CoglPipeline *node0;
CoglPipeline *node1;
int len0;
int len1;
int len0_index;
int len1_index;
int count;
int i;
CoglPipeline *common_ancestor = NULL;
unsigned long pipelines_difference = 0;
_COGL_GET_CONTEXT (ctx, 0);
/* Algorithm:
*
* 1) Walk the ancestors of each layer to the root node, adding a
* pointer to each ancester node to two GArrays:
* ctx->pipeline0_nodes, and ctx->pipeline1_nodes.
*
* 2) Compare the arrays to find the nodes where they stop to
* differ.
*
* 3) For each array now iterate from index 0 to the first node of
* difference ORing that nodes ->difference mask into the final
* pipeline_differences mask.
*/
g_array_set_size (ctx->pipeline0_nodes, 0);
g_array_set_size (ctx->pipeline1_nodes, 0);
for (node0 = pipeline0; node0; node0 = _cogl_pipeline_get_parent (node0))
g_array_append_vals (ctx->pipeline0_nodes, &node0, 1);
for (node1 = pipeline1; node1; node1 = _cogl_pipeline_get_parent (node1))
g_array_append_vals (ctx->pipeline1_nodes, &node1, 1);
len0 = ctx->pipeline0_nodes->len;
len1 = ctx->pipeline1_nodes->len;
/* There's no point looking at the last entries since we know both
* layers must have the same default layer as their root node. */
len0_index = len0 - 2;
len1_index = len1 - 2;
count = MIN (len0, len1) - 1;
for (i = 0; i < count; i++)
{
node0 = g_array_index (ctx->pipeline0_nodes,
CoglPipeline *, len0_index--);
node1 = g_array_index (ctx->pipeline1_nodes,
CoglPipeline *, len1_index--);
if (node0 != node1)
{
common_ancestor = _cogl_pipeline_get_parent (node0);
break;
}
}
/* If we didn't already find the first the common_ancestor ancestor
* that's because one pipeline is a direct descendant of the other
* and in this case the first common ancestor is the last node we
* looked at. */
if (!common_ancestor)
common_ancestor = node0;
count = len0 - 1;
for (i = 0; i < count; i++)
{
node0 = g_array_index (ctx->pipeline0_nodes, CoglPipeline *, i);
if (node0 == common_ancestor)
break;
pipelines_difference |= node0->differences;
}
count = len1 - 1;
for (i = 0; i < count; i++)
{
node1 = g_array_index (ctx->pipeline1_nodes, CoglPipeline *, i);
if (node1 == common_ancestor)
break;
pipelines_difference |= node1->differences;
}
return pipelines_difference;
}
static gboolean
simple_property_equal (CoglPipeline **authorities0,
CoglPipeline **authorities1,
unsigned long pipelines_difference,
CoglPipelineStateIndex state_index,
CoglPipelineStateComparitor comparitor)
{
if (pipelines_difference & (1L<<state_index))
{
if (!comparitor (authorities0[state_index], authorities1[state_index]))
return FALSE;
}
return TRUE;
}
static void
_cogl_pipeline_resolve_authorities (CoglPipeline *pipeline,
unsigned long differences,
CoglPipeline **authorities)
{
unsigned long remaining = differences;
CoglPipeline *authority = pipeline;
do
{
unsigned long found = authority->differences & remaining;
int i;
if (found == 0)
continue;
for (i = 0; TRUE; i++)
{
unsigned long state = (1L<<i);
if (state & found)
authorities[i] = authority;
else if (state > found)
break;
}
remaining &= ~found;
if (remaining == 0)
return;
}
while ((authority = _cogl_pipeline_get_parent (authority)));
g_assert (remaining == 0);
}
/* Comparison of two arbitrary pipelines is done by:
* 1) walking up the parents of each pipeline until a common
* ancestor is found, and at each step ORing together the
* difference masks.
*
* 2) using the final difference mask to determine which state
* groups to compare.
*
* This is used, for example, by the Cogl journal to compare pipelines so that
* it can split up geometry that needs different OpenGL state.
*
* XXX: When comparing texture layers, _cogl_pipeline_equal will actually
* compare the underlying GL texture handle that the Cogl texture uses so that
* atlas textures and sub textures will be considered equal if they point to
* the same texture. This is useful for comparing pipelines in the journal but
* it means that _cogl_pipeline_equal doesn't strictly compare whether the
* pipelines are the same. If we needed those semantics we could perhaps add
* another function or some flags to control the behaviour.
*
* XXX: Similarly when comparing the wrap modes,
* COGL_PIPELINE_WRAP_MODE_AUTOMATIC is considered to be the same as
* COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE because once they get to the
* journal stage they act exactly the same.
*/
gboolean
_cogl_pipeline_equal (CoglPipeline *pipeline0,
CoglPipeline *pipeline1,
unsigned long differences,
unsigned long layer_differences,
CoglPipelineEvalFlags flags)
{
unsigned long pipelines_difference;
CoglPipeline *authorities0[COGL_PIPELINE_STATE_SPARSE_COUNT];
CoglPipeline *authorities1[COGL_PIPELINE_STATE_SPARSE_COUNT];
gboolean ret;
COGL_STATIC_TIMER (pipeline_equal_timer,
"Mainloop", /* parent */
"_cogl_pipeline_equal",
"The time spent comparing cogl pipelines",
0 /* no application private data */);
COGL_TIMER_START (_cogl_uprof_context, pipeline_equal_timer);
if (pipeline0 == pipeline1)
{
ret = TRUE;
goto done;
}
ret = FALSE;
/* First check non-sparse properties */
if (differences & COGL_PIPELINE_STATE_REAL_BLEND_ENABLE &&
pipeline0->real_blend_enable != pipeline1->real_blend_enable)
goto done;
/* Then check sparse properties */
pipelines_difference =
_cogl_pipeline_compare_differences (pipeline0, pipeline1);
/* Only compare the sparse state groups requested by the caller... */
pipelines_difference &= differences;
_cogl_pipeline_resolve_authorities (pipeline0,
pipelines_difference,
authorities0);
_cogl_pipeline_resolve_authorities (pipeline1,
pipelines_difference,
authorities1);
/* FIXME: we should resolve all the required authorities up front since
* that should reduce some repeat ancestor traversals. */
if (pipelines_difference & COGL_PIPELINE_STATE_COLOR)
{
CoglPipeline *authority0 = authorities0[COGL_PIPELINE_STATE_COLOR_INDEX];
CoglPipeline *authority1 = authorities1[COGL_PIPELINE_STATE_COLOR_INDEX];
if (!cogl_color_equal (&authority0->color, &authority1->color))
goto done;
}
if (!simple_property_equal (authorities0, authorities1,
pipelines_difference,
COGL_PIPELINE_STATE_LIGHTING_INDEX,
_cogl_pipeline_lighting_state_equal))
goto done;
if (!simple_property_equal (authorities0, authorities1,
pipelines_difference,
COGL_PIPELINE_STATE_ALPHA_FUNC_INDEX,
_cogl_pipeline_alpha_func_state_equal))
goto done;
if (!simple_property_equal (authorities0, authorities1,
pipelines_difference,
COGL_PIPELINE_STATE_ALPHA_FUNC_REFERENCE_INDEX,
_cogl_pipeline_alpha_func_reference_state_equal))
goto done;
/* We don't need to compare the detailed blending state if we know
* blending is disabled for both pipelines. */
if (pipeline0->real_blend_enable &&
pipelines_difference & COGL_PIPELINE_STATE_BLEND)
{
CoglPipeline *authority0 = authorities0[COGL_PIPELINE_STATE_BLEND_INDEX];
CoglPipeline *authority1 = authorities1[COGL_PIPELINE_STATE_BLEND_INDEX];
if (!_cogl_pipeline_blend_state_equal (authority0, authority1))
goto done;
}
/* XXX: we don't need to compare the BLEND_ENABLE state because it's
* already reflected in ->real_blend_enable */
#if 0
if (!simple_property_equal (authorities0, authorities1,
pipelines_difference,
COGL_PIPELINE_STATE_BLEND_INDEX,
_cogl_pipeline_blend_enable_equal))
return FALSE;
#endif
if (!simple_property_equal (authorities0, authorities1,
pipelines_difference,
COGL_PIPELINE_STATE_DEPTH_INDEX,
_cogl_pipeline_depth_state_equal))
goto done;
if (!simple_property_equal (authorities0, authorities1,
pipelines_difference,
COGL_PIPELINE_STATE_FOG_INDEX,
_cogl_pipeline_fog_state_equal))
goto done;
if (!simple_property_equal (authorities0, authorities1,
pipelines_difference,
COGL_PIPELINE_STATE_POINT_SIZE_INDEX,
_cogl_pipeline_point_size_equal))
goto done;
if (!simple_property_equal (authorities0, authorities1,
pipelines_difference,
COGL_PIPELINE_STATE_LOGIC_OPS_INDEX,
_cogl_pipeline_logic_ops_state_equal))
goto done;
if (!simple_property_equal (authorities0, authorities1,
pipelines_difference,
COGL_PIPELINE_STATE_USER_SHADER_INDEX,
_cogl_pipeline_user_shader_equal))
goto done;
if (pipelines_difference & COGL_PIPELINE_STATE_LAYERS)
{
CoglPipelineStateIndex state_index = COGL_PIPELINE_STATE_LAYERS_INDEX;
if (!_cogl_pipeline_layers_equal (authorities0[state_index],
authorities1[state_index],
layer_differences,
flags))
goto done;
}
ret = TRUE;
done:
COGL_TIMER_STOP (_cogl_uprof_context, pipeline_equal_timer);
return ret;
}
void
_cogl_pipeline_prune_redundant_ancestry (CoglPipeline *pipeline)
{
CoglPipeline *new_parent = _cogl_pipeline_get_parent (pipeline);
/* Before considering pruning redundant ancestry we check if this
* pipeline is an authority for layer state and if so only consider
* reparenting if it *owns* all the layers it depends on. NB: A
* pipeline can be be a STATE_LAYERS authority but it may still
* defer to its ancestors to define the state for some of its
* layers.
*
* For example a pipeline that derives from a parent with 5 layers
* can become a STATE_LAYERS authority by simply changing it's
* ->n_layers count to 4 and in that case it can still defer to its
* ancestors to define the state of those 4 layers.
*
* If a pipeline depends on any ancestors for layer state then we
* immediatly bail out.
*/
if (pipeline->differences & COGL_PIPELINE_STATE_LAYERS)
{
if (pipeline->n_layers != g_list_length (pipeline->layer_differences))
return;
}
/* walk up past ancestors that are now redundant and potentially
* reparent the pipeline. */
while (_cogl_pipeline_get_parent (new_parent) &&
(new_parent->differences | pipeline->differences) ==
pipeline->differences)
new_parent = _cogl_pipeline_get_parent (new_parent);
if (new_parent != _cogl_pipeline_get_parent (pipeline))
{
gboolean is_weak = _cogl_pipeline_is_weak (pipeline);
_cogl_pipeline_set_parent (pipeline, new_parent, is_weak ? FALSE : TRUE);
}
}
void
_cogl_pipeline_update_authority (CoglPipeline *pipeline,
CoglPipeline *authority,
CoglPipelineState state,
CoglPipelineStateComparitor comparitor)
{
/* If we are the current authority see if we can revert to one of
* our ancestors being the authority */
if (pipeline == authority &&
_cogl_pipeline_get_parent (authority) != NULL)
{
CoglPipeline *parent = _cogl_pipeline_get_parent (authority);
CoglPipeline *old_authority =
_cogl_pipeline_get_authority (parent, state);
if (comparitor (authority, old_authority))
pipeline->differences &= ~state;
}
else if (pipeline != authority)
{
/* If we weren't previously the authority on this state then we
* need to extended our differences mask and so it's possible
* that some of our ancestry will now become redundant, so we
* aim to reparent ourselves if that's true... */
pipeline->differences |= state;
_cogl_pipeline_prune_redundant_ancestry (pipeline);
}
}
unsigned long
_cogl_pipeline_get_age (CoglPipeline *pipeline)
{
g_return_val_if_fail (cogl_is_pipeline (pipeline), 0);
return pipeline->age;
}
static CoglPipelineLayer *
_cogl_pipeline_layer_copy (CoglPipelineLayer *src)
{
CoglPipelineLayer *layer = g_slice_new (CoglPipelineLayer);
_cogl_pipeline_node_init (COGL_PIPELINE_NODE (layer));
layer->owner = NULL;
layer->index = src->index;
layer->differences = 0;
layer->has_big_state = FALSE;
_cogl_pipeline_layer_set_parent (layer, src);
return _cogl_pipeline_layer_object_new (layer);
}
static void
_cogl_pipeline_layer_free (CoglPipelineLayer *layer)
{
_cogl_pipeline_layer_unparent (COGL_PIPELINE_NODE (layer));
if (layer->differences & COGL_PIPELINE_LAYER_STATE_TEXTURE_DATA &&
layer->texture != NULL)
cogl_object_unref (layer->texture);
if (layer->differences & COGL_PIPELINE_LAYER_STATE_NEEDS_BIG_STATE)
g_slice_free (CoglPipelineLayerBigState, layer->big_state);
g_slice_free (CoglPipelineLayer, layer);
}
/* If a layer has descendants we can't modify it freely
*
* If the layer is owned and the owner has descendants we can't
* modify it freely.
*
* In both cases when we can't freely modify a layer we can either:
* - create a new layer; splice it in to replace the layer so it can
* be directly modified.
* XXX: disadvantage is that we have to invalidate the layers_cache
* for the owner and its descendants.
* - create a new derived layer and modify that.
*/
/* XXX: how is the caller expected to deal with ref-counting?
*
* If the layer can't be freely modified and we return a new layer
* then that will effectively make the caller own a new reference
* which doesn't happen if we simply modify the given layer.
*
* We could make it consistent by taking a reference on the layer if
* we don't create a new one. At least this way the caller could
* deal with it consistently, though the semantics are a bit
* strange.
*
* Alternatively we could leave it to the caller to check
* ...?
*/
void
_cogl_pipeline_init_default_layers (void)
{
CoglPipelineLayer *layer = g_slice_new0 (CoglPipelineLayer);
CoglPipelineLayerBigState *big_state =
g_slice_new0 (CoglPipelineLayerBigState);
CoglPipelineLayer *new;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
_cogl_pipeline_node_init (COGL_PIPELINE_NODE (layer));
layer->index = 0;
layer->differences = COGL_PIPELINE_LAYER_STATE_ALL_SPARSE;
layer->unit_index = 0;
layer->texture = NULL;
layer->target = 0;
layer->mag_filter = COGL_PIPELINE_FILTER_LINEAR;
layer->min_filter = COGL_PIPELINE_FILTER_LINEAR;
layer->wrap_mode_s = COGL_PIPELINE_WRAP_MODE_AUTOMATIC;
layer->wrap_mode_t = COGL_PIPELINE_WRAP_MODE_AUTOMATIC;
layer->wrap_mode_p = COGL_PIPELINE_WRAP_MODE_AUTOMATIC;
layer->big_state = big_state;
layer->has_big_state = TRUE;
/* Choose the same default combine mode as OpenGL:
* RGBA = MODULATE(PREVIOUS[RGBA],TEXTURE[RGBA]) */
big_state->texture_combine_rgb_func =
COGL_PIPELINE_COMBINE_FUNC_MODULATE;
big_state->texture_combine_rgb_src[0] =
COGL_PIPELINE_COMBINE_SOURCE_PREVIOUS;
big_state->texture_combine_rgb_src[1] =
COGL_PIPELINE_COMBINE_SOURCE_TEXTURE;
big_state->texture_combine_rgb_op[0] =
COGL_PIPELINE_COMBINE_OP_SRC_COLOR;
big_state->texture_combine_rgb_op[1] =
COGL_PIPELINE_COMBINE_OP_SRC_COLOR;
big_state->texture_combine_alpha_func =
COGL_PIPELINE_COMBINE_FUNC_MODULATE;
big_state->texture_combine_alpha_src[0] =
COGL_PIPELINE_COMBINE_SOURCE_PREVIOUS;
big_state->texture_combine_alpha_src[1] =
COGL_PIPELINE_COMBINE_SOURCE_TEXTURE;
big_state->texture_combine_alpha_op[0] =
COGL_PIPELINE_COMBINE_OP_SRC_ALPHA;
big_state->texture_combine_alpha_op[1] =
COGL_PIPELINE_COMBINE_OP_SRC_ALPHA;
big_state->point_sprite_coords = FALSE;
cogl_matrix_init_identity (&big_state->matrix);
ctx->default_layer_0 = _cogl_pipeline_layer_object_new (layer);
/* TODO: we should make default_layer_n comprise of two
* descendants of default_layer_0:
* - the first descendant should change the texture combine
* to what we expect is most commonly used for multitexturing
* - the second should revert the above change.
*
* why? the documentation for how a new layer is initialized
* doesn't say that layers > 0 have different defaults so unless
* we change the documentation we can't use different defaults,
* but if the user does what we expect and changes the
* texture combine then we can revert the authority to the
* first descendant which means we can maximize the number
* of layers with a common ancestor.
*
* The main problem will be that we'll need to disable the
* optimizations for flattening the ancestry when we make
* the second descendant which reverts the state.
*/
ctx->default_layer_n = _cogl_pipeline_layer_copy (layer);
new = _cogl_pipeline_set_layer_unit (NULL, ctx->default_layer_n, 1);
g_assert (new == ctx->default_layer_n);
/* Since we passed a newly allocated layer we don't expect that
* _set_layer_unit() will have to allocate *another* layer. */
/* Finally we create a dummy dependant for ->default_layer_n which
* effectively ensures that ->default_layer_n and ->default_layer_0
* remain immutable.
*/
ctx->dummy_layer_dependant =
_cogl_pipeline_layer_copy (ctx->default_layer_n);
}
static void
setup_texture_combine_state (CoglBlendStringStatement *statement,
CoglPipelineCombineFunc *texture_combine_func,
CoglPipelineCombineSource *texture_combine_src,
CoglPipelineCombineOp *texture_combine_op)
{
int i;
switch (statement->function->type)
{
case COGL_BLEND_STRING_FUNCTION_REPLACE:
*texture_combine_func = COGL_PIPELINE_COMBINE_FUNC_REPLACE;
break;
case COGL_BLEND_STRING_FUNCTION_MODULATE:
*texture_combine_func = COGL_PIPELINE_COMBINE_FUNC_MODULATE;
break;
case COGL_BLEND_STRING_FUNCTION_ADD:
*texture_combine_func = COGL_PIPELINE_COMBINE_FUNC_ADD;
break;
case COGL_BLEND_STRING_FUNCTION_ADD_SIGNED:
*texture_combine_func = COGL_PIPELINE_COMBINE_FUNC_ADD_SIGNED;
break;
case COGL_BLEND_STRING_FUNCTION_INTERPOLATE:
*texture_combine_func = COGL_PIPELINE_COMBINE_FUNC_INTERPOLATE;
break;
case COGL_BLEND_STRING_FUNCTION_SUBTRACT:
*texture_combine_func = COGL_PIPELINE_COMBINE_FUNC_SUBTRACT;
break;
case COGL_BLEND_STRING_FUNCTION_DOT3_RGB:
*texture_combine_func = COGL_PIPELINE_COMBINE_FUNC_DOT3_RGB;
break;
case COGL_BLEND_STRING_FUNCTION_DOT3_RGBA:
*texture_combine_func = COGL_PIPELINE_COMBINE_FUNC_DOT3_RGBA;
break;
}
for (i = 0; i < statement->function->argc; i++)
{
CoglBlendStringArgument *arg = &statement->args[i];
switch (arg->source.info->type)
{
case COGL_BLEND_STRING_COLOR_SOURCE_CONSTANT:
texture_combine_src[i] = COGL_PIPELINE_COMBINE_SOURCE_CONSTANT;
break;
case COGL_BLEND_STRING_COLOR_SOURCE_TEXTURE:
texture_combine_src[i] = COGL_PIPELINE_COMBINE_SOURCE_TEXTURE;
break;
case COGL_BLEND_STRING_COLOR_SOURCE_TEXTURE_N:
texture_combine_src[i] =
COGL_PIPELINE_COMBINE_SOURCE_TEXTURE0 + arg->source.texture;
break;
case COGL_BLEND_STRING_COLOR_SOURCE_PRIMARY:
texture_combine_src[i] = COGL_PIPELINE_COMBINE_SOURCE_PRIMARY_COLOR;
break;
case COGL_BLEND_STRING_COLOR_SOURCE_PREVIOUS:
texture_combine_src[i] = COGL_PIPELINE_COMBINE_SOURCE_PREVIOUS;
break;
default:
g_warning ("Unexpected texture combine source");
texture_combine_src[i] = COGL_PIPELINE_COMBINE_SOURCE_TEXTURE;
}
if (arg->source.mask == COGL_BLEND_STRING_CHANNEL_MASK_RGB)
{
if (statement->args[i].source.one_minus)
texture_combine_op[i] =
COGL_PIPELINE_COMBINE_OP_ONE_MINUS_SRC_COLOR;
else
texture_combine_op[i] = COGL_PIPELINE_COMBINE_OP_SRC_COLOR;
}
else
{
if (statement->args[i].source.one_minus)
texture_combine_op[i] =
COGL_PIPELINE_COMBINE_OP_ONE_MINUS_SRC_ALPHA;
else
texture_combine_op[i] = COGL_PIPELINE_COMBINE_OP_SRC_ALPHA;
}
}
}
gboolean
cogl_pipeline_set_layer_combine (CoglPipeline *pipeline,
int layer_index,
const char *combine_description,
GError **error)
{
CoglPipelineLayerState state = COGL_PIPELINE_LAYER_STATE_COMBINE;
CoglPipelineLayer *authority;
CoglPipelineLayer *layer;
CoglBlendStringStatement statements[2];
CoglBlendStringStatement split[2];
CoglBlendStringStatement *rgb;
CoglBlendStringStatement *a;
GError *internal_error = NULL;
int count;
g_return_val_if_fail (cogl_is_pipeline (pipeline), FALSE);
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, state);
count =
_cogl_blend_string_compile (combine_description,
COGL_BLEND_STRING_CONTEXT_TEXTURE_COMBINE,
statements,
&internal_error);
if (!count)
{
if (error)
g_propagate_error (error, internal_error);
else
{
g_warning ("Cannot compile combine description: %s\n",
internal_error->message);
g_error_free (internal_error);
}
return FALSE;
}
if (statements[0].mask == COGL_BLEND_STRING_CHANNEL_MASK_RGBA)
{
_cogl_blend_string_split_rgba_statement (statements,
&split[0], &split[1]);
rgb = &split[0];
a = &split[1];
}
else
{
rgb = &statements[0];
a = &statements[1];
}
/* FIXME: compare the new state with the current state! */
/* possibly flush primitives referencing the current state... */
layer = _cogl_pipeline_layer_pre_change_notify (pipeline, layer, state);
setup_texture_combine_state (rgb,
&layer->big_state->texture_combine_rgb_func,
layer->big_state->texture_combine_rgb_src,
layer->big_state->texture_combine_rgb_op);
setup_texture_combine_state (a,
&layer->big_state->texture_combine_alpha_func,
layer->big_state->texture_combine_alpha_src,
layer->big_state->texture_combine_alpha_op);
/* If the original layer we found is currently the authority on
* the state we are changing see if we can revert to one of our
* ancestors being the authority. */
if (layer == authority &&
_cogl_pipeline_layer_get_parent (authority) != NULL)
{
CoglPipelineLayer *parent = _cogl_pipeline_layer_get_parent (authority);
CoglPipelineLayer *old_authority =
_cogl_pipeline_layer_get_authority (parent, state);
if (_cogl_pipeline_layer_combine_state_equal (authority,
old_authority))
{
layer->differences &= ~state;
g_assert (layer->owner == pipeline);
if (layer->differences == 0)
_cogl_pipeline_prune_empty_layer_difference (pipeline,
layer);
goto changed;
}
}
/* If we weren't previously the authority on this state then we need
* to extended our differences mask and so it's possible that some
* of our ancestry will now become redundant, so we aim to reparent
* ourselves if that's true... */
if (layer != authority)
{
layer->differences |= state;
_cogl_pipeline_layer_prune_redundant_ancestry (layer);
}
changed:
_cogl_pipeline_update_blend_enable (pipeline, COGL_PIPELINE_STATE_LAYERS);
return TRUE;
}
void
cogl_pipeline_set_layer_combine_constant (CoglPipeline *pipeline,
int layer_index,
const CoglColor *constant_color)
{
CoglPipelineLayerState state = COGL_PIPELINE_LAYER_STATE_COMBINE_CONSTANT;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
CoglPipelineLayer *new;
float color_as_floats[4];
g_return_if_fail (cogl_is_pipeline (pipeline));
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, state);
color_as_floats[0] = cogl_color_get_red_float (constant_color);
color_as_floats[1] = cogl_color_get_green_float (constant_color);
color_as_floats[2] = cogl_color_get_blue_float (constant_color);
color_as_floats[3] = cogl_color_get_alpha_float (constant_color);
if (memcmp (authority->big_state->texture_combine_constant,
color_as_floats, sizeof (float) * 4) == 0)
return;
new = _cogl_pipeline_layer_pre_change_notify (pipeline, layer, state);
if (new != layer)
layer = new;
else
{
/* If the original layer we found is currently the authority on
* the state we are changing see if we can revert to one of our
* ancestors being the authority. */
if (layer == authority &&
_cogl_pipeline_layer_get_parent (authority) != NULL)
{
CoglPipelineLayer *parent =
_cogl_pipeline_layer_get_parent (authority);
CoglPipelineLayer *old_authority =
_cogl_pipeline_layer_get_authority (parent, state);
CoglPipelineLayerBigState *old_big_state = old_authority->big_state;
if (memcmp (old_big_state->texture_combine_constant,
color_as_floats, sizeof (float) * 4) == 0)
{
layer->differences &= ~state;
g_assert (layer->owner == pipeline);
if (layer->differences == 0)
_cogl_pipeline_prune_empty_layer_difference (pipeline,
layer);
goto changed;
}
}
}
memcpy (layer->big_state->texture_combine_constant,
color_as_floats,
sizeof (color_as_floats));
/* If we weren't previously the authority on this state then we need
* to extended our differences mask and so it's possible that some
* of our ancestry will now become redundant, so we aim to reparent
* ourselves if that's true... */
if (layer != authority)
{
layer->differences |= state;
_cogl_pipeline_layer_prune_redundant_ancestry (layer);
}
changed:
_cogl_pipeline_update_blend_enable (pipeline, COGL_PIPELINE_STATE_LAYERS);
}
void
_cogl_pipeline_get_layer_combine_constant (CoglPipeline *pipeline,
int layer_index,
float *constant)
{
CoglPipelineLayerState change =
COGL_PIPELINE_LAYER_STATE_COMBINE_CONSTANT;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
g_return_if_fail (cogl_is_pipeline (pipeline));
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* FIXME: we shouldn't ever construct a layer in a getter function */
authority = _cogl_pipeline_layer_get_authority (layer, change);
memcpy (constant, authority->big_state->texture_combine_constant,
sizeof (float) * 4);
}
/* We should probably make a public API version of this that has a
matrix out-param. For an internal API it's good to be able to avoid
copying the matrix */
const CoglMatrix *
_cogl_pipeline_get_layer_matrix (CoglPipeline *pipeline, int layer_index)
{
CoglPipelineLayerState change =
COGL_PIPELINE_LAYER_STATE_USER_MATRIX;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
g_return_val_if_fail (cogl_is_pipeline (pipeline), NULL);
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
authority = _cogl_pipeline_layer_get_authority (layer, change);
return &authority->big_state->matrix;
}
void
cogl_pipeline_set_layer_matrix (CoglPipeline *pipeline,
int layer_index,
const CoglMatrix *matrix)
{
CoglPipelineLayerState state = COGL_PIPELINE_LAYER_STATE_USER_MATRIX;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
CoglPipelineLayer *new;
g_return_if_fail (cogl_is_pipeline (pipeline));
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, state);
if (cogl_matrix_equal (matrix, &authority->big_state->matrix))
return;
new = _cogl_pipeline_layer_pre_change_notify (pipeline, layer, state);
if (new != layer)
layer = new;
else
{
/* If the original layer we found is currently the authority on
* the state we are changing see if we can revert to one of our
* ancestors being the authority. */
if (layer == authority &&
_cogl_pipeline_layer_get_parent (authority) != NULL)
{
CoglPipelineLayer *parent =
_cogl_pipeline_layer_get_parent (authority);
CoglPipelineLayer *old_authority =
_cogl_pipeline_layer_get_authority (parent, state);
if (cogl_matrix_equal (matrix, &old_authority->big_state->matrix))
{
layer->differences &= ~state;
g_assert (layer->owner == pipeline);
if (layer->differences == 0)
_cogl_pipeline_prune_empty_layer_difference (pipeline,
layer);
return;
}
}
}
layer->big_state->matrix = *matrix;
/* If we weren't previously the authority on this state then we need
* to extended our differences mask and so it's possible that some
* of our ancestry will now become redundant, so we aim to reparent
* ourselves if that's true... */
if (layer != authority)
{
layer->differences |= state;
_cogl_pipeline_layer_prune_redundant_ancestry (layer);
}
}
void
cogl_pipeline_remove_layer (CoglPipeline *pipeline, int layer_index)
{
CoglPipeline *authority;
CoglPipelineLayerInfo layer_info;
int i;
g_return_if_fail (cogl_is_pipeline (pipeline));
authority =
_cogl_pipeline_get_authority (pipeline, COGL_PIPELINE_STATE_LAYERS);
/* The layer index of the layer we want info about */
layer_info.layer_index = layer_index;
/* This will be updated with a reference to the layer being removed
* if it can be found. */
layer_info.layer = NULL;
/* This will be filled in with a list of layers that need to be
* dropped down to a lower texture unit to fill the gap of the
* removed layer. */
layer_info.layers_to_shift =
g_alloca (sizeof (CoglPipelineLayer *) * authority->n_layers);
layer_info.n_layers_to_shift = 0;
/* Unlike when we query layer info when adding a layer we must
* always have a complete layers_to_shift list... */
layer_info.ignore_shift_layers_if_found = FALSE;
_cogl_pipeline_get_layer_info (authority, &layer_info);
if (layer_info.layer == NULL)
return;
for (i = 0; i < layer_info.n_layers_to_shift; i++)
{
CoglPipelineLayer *shift_layer = layer_info.layers_to_shift[i];
int unit_index = _cogl_pipeline_layer_get_unit_index (shift_layer);
_cogl_pipeline_set_layer_unit (pipeline, shift_layer, unit_index - 1);
/* NB: shift_layer may not be writeable so _set_layer_unit()
* will allocate a derived layer internally which will become
* owned by pipeline. Check the return value if we need to do
* anything else with this layer. */
}
_cogl_pipeline_remove_layer_difference (pipeline, layer_info.layer, TRUE);
_cogl_pipeline_try_reverting_layers_authority (pipeline, NULL);
_cogl_pipeline_update_blend_enable (pipeline, COGL_PIPELINE_STATE_LAYERS);
}
static gboolean
prepend_layer_to_list_cb (CoglPipelineLayer *layer,
void *user_data)
{
GList **layers = user_data;
*layers = g_list_prepend (*layers, layer);
return TRUE;
}
/* TODO: deprecate this API and replace it with
* cogl_pipeline_foreach_layer
* TODO: update the docs to note that if the user modifies any layers
* then the list may become invalid.
*/
const GList *
_cogl_pipeline_get_layers (CoglPipeline *pipeline)
{
g_return_val_if_fail (cogl_is_pipeline (pipeline), NULL);
if (!pipeline->deprecated_get_layers_list_dirty)
g_list_free (pipeline->deprecated_get_layers_list);
pipeline->deprecated_get_layers_list = NULL;
_cogl_pipeline_foreach_layer_internal (pipeline,
prepend_layer_to_list_cb,
&pipeline->deprecated_get_layers_list);
pipeline->deprecated_get_layers_list =
g_list_reverse (pipeline->deprecated_get_layers_list);
pipeline->deprecated_get_layers_list_dirty = 0;
return pipeline->deprecated_get_layers_list;
}
int
cogl_pipeline_get_n_layers (CoglPipeline *pipeline)
{
CoglPipeline *authority;
g_return_val_if_fail (cogl_is_pipeline (pipeline), 0);
authority =
_cogl_pipeline_get_authority (pipeline, COGL_PIPELINE_STATE_LAYERS);
return authority->n_layers;
}
/* FIXME: deprecate and replace with
* cogl_pipeline_get_layer_texture() instead. */
CoglHandle
_cogl_pipeline_layer_get_texture (CoglPipelineLayer *layer)
{
g_return_val_if_fail (_cogl_is_pipeline_layer (layer),
COGL_INVALID_HANDLE);
return _cogl_pipeline_layer_get_texture_real (layer);
}
gboolean
_cogl_pipeline_layer_has_user_matrix (CoglPipeline *pipeline,
int layer_index)
{
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
authority =
_cogl_pipeline_layer_get_authority (layer,
COGL_PIPELINE_LAYER_STATE_USER_MATRIX);
/* If the authority is the default pipeline then no, otherwise yes */
return _cogl_pipeline_layer_get_parent (authority) ? TRUE : FALSE;
}
void
_cogl_pipeline_layer_get_filters (CoglPipelineLayer *layer,
CoglPipelineFilter *min_filter,
CoglPipelineFilter *mag_filter)
{
CoglPipelineLayer *authority =
_cogl_pipeline_layer_get_authority (layer,
COGL_PIPELINE_LAYER_STATE_FILTERS);
*min_filter = authority->min_filter;
*mag_filter = authority->mag_filter;
}
void
_cogl_pipeline_get_layer_filters (CoglPipeline *pipeline,
int layer_index,
CoglPipelineFilter *min_filter,
CoglPipelineFilter *mag_filter)
{
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
g_return_if_fail (cogl_is_pipeline (pipeline));
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
authority =
_cogl_pipeline_layer_get_authority (layer,
COGL_PIPELINE_LAYER_STATE_FILTERS);
*min_filter = authority->min_filter;
*mag_filter = authority->mag_filter;
}
CoglPipelineFilter
_cogl_pipeline_get_layer_min_filter (CoglPipeline *pipeline,
int layer_index)
{
CoglPipelineFilter min_filter;
CoglPipelineFilter mag_filter;
_cogl_pipeline_get_layer_filters (pipeline, layer_index,
&min_filter, &mag_filter);
return min_filter;
}
CoglPipelineFilter
_cogl_pipeline_get_layer_mag_filter (CoglPipeline *pipeline,
int layer_index)
{
CoglPipelineFilter min_filter;
CoglPipelineFilter mag_filter;
_cogl_pipeline_get_layer_filters (pipeline, layer_index,
&min_filter, &mag_filter);
return mag_filter;
}
void
_cogl_pipeline_layer_pre_paint (CoglPipelineLayer *layer)
{
CoglPipelineLayer *texture_authority;
texture_authority =
_cogl_pipeline_layer_get_authority (layer,
COGL_PIPELINE_LAYER_STATE_TEXTURE_DATA);
if (texture_authority->texture != COGL_INVALID_HANDLE)
{
CoglTexturePrePaintFlags flags = 0;
CoglPipelineFilter min_filter;
CoglPipelineFilter mag_filter;
_cogl_pipeline_layer_get_filters (layer, &min_filter, &mag_filter);
if (min_filter == COGL_PIPELINE_FILTER_NEAREST_MIPMAP_NEAREST
|| min_filter == COGL_PIPELINE_FILTER_LINEAR_MIPMAP_NEAREST
|| min_filter == COGL_PIPELINE_FILTER_NEAREST_MIPMAP_LINEAR
|| min_filter == COGL_PIPELINE_FILTER_LINEAR_MIPMAP_LINEAR)
flags |= COGL_TEXTURE_NEEDS_MIPMAP;
_cogl_texture_pre_paint (texture_authority->texture, flags);
}
}
void
_cogl_pipeline_pre_paint_for_layer (CoglPipeline *pipeline,
int layer_id)
{
CoglPipelineLayer *layer = _cogl_pipeline_get_layer (pipeline, layer_id);
_cogl_pipeline_layer_pre_paint (layer);
}
CoglPipelineFilter
_cogl_pipeline_layer_get_min_filter (CoglPipelineLayer *layer)
{
CoglPipelineLayer *authority;
g_return_val_if_fail (_cogl_is_pipeline_layer (layer), 0);
authority =
_cogl_pipeline_layer_get_authority (layer,
COGL_PIPELINE_LAYER_STATE_FILTERS);
return authority->min_filter;
}
CoglPipelineFilter
_cogl_pipeline_layer_get_mag_filter (CoglPipelineLayer *layer)
{
CoglPipelineLayer *authority;
g_return_val_if_fail (_cogl_is_pipeline_layer (layer), 0);
authority =
_cogl_pipeline_layer_get_authority (layer,
COGL_PIPELINE_LAYER_STATE_FILTERS);
return authority->mag_filter;
}
void
cogl_pipeline_set_layer_filters (CoglPipeline *pipeline,
int layer_index,
CoglPipelineFilter min_filter,
CoglPipelineFilter mag_filter)
{
CoglPipelineLayerState state = COGL_PIPELINE_LAYER_STATE_FILTERS;
CoglPipelineLayer *layer;
CoglPipelineLayer *authority;
CoglPipelineLayer *new;
g_return_if_fail (cogl_is_pipeline (pipeline));
/* Note: this will ensure that the layer exists, creating one if it
* doesn't already.
*
* Note: If the layer already existed it's possibly owned by another
* pipeline. If the layer is created then it will be owned by
* pipeline. */
layer = _cogl_pipeline_get_layer (pipeline, layer_index);
/* Now find the ancestor of the layer that is the authority for the
* state we want to change */
authority = _cogl_pipeline_layer_get_authority (layer, state);
if (authority->min_filter == min_filter &&
authority->mag_filter == mag_filter)
return;
new = _cogl_pipeline_layer_pre_change_notify (pipeline, layer, state);
if (new != layer)
layer = new;
else
{
/* If the original layer we found is currently the authority on
* the state we are changing see if we can revert to one of our
* ancestors being the authority. */
if (layer == authority &&
_cogl_pipeline_layer_get_parent (authority) != NULL)
{
CoglPipelineLayer *parent =
_cogl_pipeline_layer_get_parent (authority);
CoglPipelineLayer *old_authority =
_cogl_pipeline_layer_get_authority (parent, state);
if (old_authority->min_filter == min_filter &&
old_authority->mag_filter == mag_filter)
{
layer->differences &= ~state;
g_assert (layer->owner == pipeline);
if (layer->differences == 0)
_cogl_pipeline_prune_empty_layer_difference (pipeline,
layer);
return;
}
}
}
layer->min_filter = min_filter;
layer->mag_filter = mag_filter;
/* If we weren't previously the authority on this state then we need
* to extended our differences mask and so it's possible that some
* of our ancestry will now become redundant, so we aim to reparent
* ourselves if that's true... */
if (layer != authority)
{
layer->differences |= state;
_cogl_pipeline_layer_prune_redundant_ancestry (layer);
}
}
/* While a pipeline is referenced by the Cogl journal we can not allow
* modifications, so this gives us a mechanism to track journal
* references separately */
CoglPipeline *
_cogl_pipeline_journal_ref (CoglPipeline *pipeline)
{
pipeline->journal_ref_count++;
return cogl_object_ref (pipeline);
}
void
_cogl_pipeline_journal_unref (CoglPipeline *pipeline)
{
pipeline->journal_ref_count--;
cogl_object_unref (pipeline);
}
void
_cogl_pipeline_apply_legacy_state (CoglPipeline *pipeline)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
/* It was a mistake that we ever copied the OpenGL style API for
* associating these things directly with the context when we
* originally wrote Cogl. Until the corresponding deprecated APIs
* can be removed though we now shoehorn the state changes through
* the cogl_pipeline API instead.
*/
/* A program explicitly set on the pipeline has higher precedence than
* one associated with the context using cogl_program_use() */
if (ctx->current_program &&
cogl_pipeline_get_user_program (pipeline) == COGL_INVALID_HANDLE)
cogl_pipeline_set_user_program (pipeline, ctx->current_program);
if (ctx->legacy_depth_test_enabled)
{
CoglDepthState depth_state;
cogl_depth_state_init (&depth_state);
cogl_depth_state_set_test_enabled (&depth_state, TRUE);
cogl_pipeline_set_depth_state (pipeline, &depth_state, NULL);
}
if (ctx->legacy_fog_state.enabled)
_cogl_pipeline_set_fog_state (pipeline, &ctx->legacy_fog_state);
}
void
_cogl_pipeline_set_static_breadcrumb (CoglPipeline *pipeline,
const char *breadcrumb)
{
pipeline->has_static_breadcrumb = TRUE;
pipeline->static_breadcrumb = breadcrumb;
}
static void
_cogl_pipeline_layer_hash_unit_state (CoglPipelineLayer *authority,
CoglPipelineLayer **authorities,
CoglPipelineHashState *state)
{
int unit = authority->unit_index;
state->hash =
_cogl_util_one_at_a_time_hash (state->hash, &unit, sizeof (unit));
}
static void
_cogl_pipeline_layer_hash_texture_target_state (CoglPipelineLayer *authority,
CoglPipelineLayer **authorities,
CoglPipelineHashState *state)
{
GLenum gl_target = authority->target;
state->hash =
_cogl_util_one_at_a_time_hash (state->hash, &gl_target, sizeof (gl_target));
}
static void
_cogl_pipeline_layer_hash_texture_data_state (CoglPipelineLayer *authority,
CoglPipelineLayer **authorities,
CoglPipelineHashState *state)
{
GLuint gl_handle;
cogl_texture_get_gl_texture (authority->texture, &gl_handle, NULL);
state->hash =
_cogl_util_one_at_a_time_hash (state->hash, &gl_handle, sizeof (gl_handle));
}
static void
_cogl_pipeline_layer_hash_filters_state (CoglPipelineLayer *authority,
CoglPipelineLayer **authorities,
CoglPipelineHashState *state)
{
unsigned int hash = state->hash;
hash = _cogl_util_one_at_a_time_hash (hash, &authority->mag_filter,
sizeof (authority->mag_filter));
hash = _cogl_util_one_at_a_time_hash (hash, &authority->min_filter,
sizeof (authority->min_filter));
state->hash = hash;
}
static void
_cogl_pipeline_layer_hash_wrap_modes_state (CoglPipelineLayer *authority,
CoglPipelineLayer **authorities,
CoglPipelineHashState *state)
{
unsigned int hash = state->hash;
hash = _cogl_util_one_at_a_time_hash (hash, &authority->wrap_mode_s,
sizeof (authority->wrap_mode_s));
hash = _cogl_util_one_at_a_time_hash (hash, &authority->wrap_mode_t,
sizeof (authority->wrap_mode_t));
hash = _cogl_util_one_at_a_time_hash (hash, &authority->wrap_mode_p,
sizeof (authority->wrap_mode_p));
state->hash = hash;
}
static void
_cogl_pipeline_layer_hash_combine_state (CoglPipelineLayer *authority,
CoglPipelineLayer **authorities,
CoglPipelineHashState *state)
{
unsigned int hash = state->hash;
CoglPipelineLayerBigState *b = authority->big_state;
int n_args;
int i;
hash = _cogl_util_one_at_a_time_hash (hash, &b->texture_combine_rgb_func,
sizeof (b->texture_combine_rgb_func));
n_args = _cogl_get_n_args_for_combine_func (b->texture_combine_rgb_func);
for (i = 0; i < n_args; i++)
{
hash =
_cogl_util_one_at_a_time_hash (hash, &b->texture_combine_rgb_src[i],
sizeof (b->texture_combine_rgb_src[i]));
hash =
_cogl_util_one_at_a_time_hash (hash, &b->texture_combine_rgb_op[i],
sizeof (b->texture_combine_rgb_op[i]));
}
hash = _cogl_util_one_at_a_time_hash (hash, &b->texture_combine_alpha_func,
sizeof (b->texture_combine_alpha_func));
n_args = _cogl_get_n_args_for_combine_func (b->texture_combine_alpha_func);
for (i = 0; i < n_args; i++)
{
hash =
_cogl_util_one_at_a_time_hash (hash, &b->texture_combine_alpha_src[i],
sizeof (b->texture_combine_alpha_src[i]));
hash =
_cogl_util_one_at_a_time_hash (hash, &b->texture_combine_alpha_op[i],
sizeof (b->texture_combine_alpha_op[i]));
}
state->hash = hash;
}
static void
_cogl_pipeline_layer_hash_combine_constant_state (CoglPipelineLayer *authority,
CoglPipelineLayer **authorities,
CoglPipelineHashState *state)
{
CoglPipelineLayerBigState *b = authority->big_state;
gboolean need_hash = FALSE;
int n_args;
int i;
/* XXX: If the user also asked to hash the ALPHA_FUNC_STATE then it
* would be nice if we could combine the n_args loops in this
* function and _cogl_pipeline_layer_hash_combine_state.
*/
n_args = _cogl_get_n_args_for_combine_func (b->texture_combine_rgb_func);
for (i = 0; i < n_args; i++)
{
if (b->texture_combine_rgb_src[i] ==
COGL_PIPELINE_COMBINE_SOURCE_CONSTANT)
{
/* XXX: should we be careful to only hash the alpha
* component in the COGL_PIPELINE_COMBINE_OP_SRC_ALPHA case? */
need_hash = TRUE;
goto done;
}
}
n_args = _cogl_get_n_args_for_combine_func (b->texture_combine_alpha_func);
for (i = 0; i < n_args; i++)
{
if (b->texture_combine_alpha_src[i] ==
COGL_PIPELINE_COMBINE_SOURCE_CONSTANT)
{
/* XXX: should we be careful to only hash the alpha
* component in the COGL_PIPELINE_COMBINE_OP_SRC_ALPHA case? */
need_hash = TRUE;
goto done;
}
}
done:
if (need_hash)
{
float *constant = b->texture_combine_constant;
state->hash = _cogl_util_one_at_a_time_hash (state->hash, constant,
sizeof (float) * 4);
}
}
static void
_cogl_pipeline_layer_hash_user_matrix_state (CoglPipelineLayer *authority,
CoglPipelineLayer **authorities,
CoglPipelineHashState *state)
{
CoglPipelineLayerBigState *big_state = authority->big_state;
state->hash = _cogl_util_one_at_a_time_hash (state->hash, &big_state->matrix,
sizeof (float) * 16);
}
static void
_cogl_pipeline_layer_hash_point_sprite_state (CoglPipelineLayer *authority,
CoglPipelineLayer **authorities,
CoglPipelineHashState *state)
{
CoglPipelineLayerBigState *big_state = authority->big_state;
state->hash =
_cogl_util_one_at_a_time_hash (state->hash, &big_state->point_sprite_coords,
sizeof (big_state->point_sprite_coords));
}
typedef void (*LayerStateHashFunction) (CoglPipelineLayer *authority,
CoglPipelineLayer **authorities,
CoglPipelineHashState *state);
static LayerStateHashFunction
layer_state_hash_functions[COGL_PIPELINE_LAYER_STATE_SPARSE_COUNT];
/* XXX: We don't statically initialize the array of hash functions, so
* we won't get caught out by later re-indexing the groups for some
* reason. */
void
_cogl_pipeline_init_layer_state_hash_functions (void)
{
CoglPipelineLayerStateIndex _index;
layer_state_hash_functions[COGL_PIPELINE_LAYER_STATE_UNIT_INDEX] =
_cogl_pipeline_layer_hash_unit_state;
layer_state_hash_functions[COGL_PIPELINE_LAYER_STATE_TEXTURE_TARGET_INDEX] =
_cogl_pipeline_layer_hash_texture_target_state;
layer_state_hash_functions[COGL_PIPELINE_LAYER_STATE_TEXTURE_DATA_INDEX] =
_cogl_pipeline_layer_hash_texture_data_state;
layer_state_hash_functions[COGL_PIPELINE_LAYER_STATE_FILTERS_INDEX] =
_cogl_pipeline_layer_hash_filters_state;
layer_state_hash_functions[COGL_PIPELINE_LAYER_STATE_WRAP_MODES_INDEX] =
_cogl_pipeline_layer_hash_wrap_modes_state;
layer_state_hash_functions[COGL_PIPELINE_LAYER_STATE_COMBINE_INDEX] =
_cogl_pipeline_layer_hash_combine_state;
layer_state_hash_functions[COGL_PIPELINE_LAYER_STATE_COMBINE_CONSTANT_INDEX] =
_cogl_pipeline_layer_hash_combine_constant_state;
layer_state_hash_functions[COGL_PIPELINE_LAYER_STATE_USER_MATRIX_INDEX] =
_cogl_pipeline_layer_hash_user_matrix_state;
_index = COGL_PIPELINE_LAYER_STATE_POINT_SPRITE_COORDS_INDEX;
layer_state_hash_functions[_index] =
_cogl_pipeline_layer_hash_point_sprite_state;
/* So we get a big error if we forget to update this code! */
g_assert (COGL_PIPELINE_LAYER_STATE_SPARSE_COUNT == 9);
}
static gboolean
_cogl_pipeline_hash_layer_cb (CoglPipelineLayer *layer,
void *user_data)
{
CoglPipelineHashState *state = user_data;
unsigned long differences = state->layer_differences;
CoglPipelineLayer *authorities[COGL_PIPELINE_LAYER_STATE_COUNT];
unsigned long mask;
int i;
/* Theoretically we would hash non-sparse layer state here but
* currently layers don't have any. */
/* XXX: we resolve all the authorities here - not just those
* corresponding to hash_state->layer_differences - because
* the hashing of some state groups actually depends on the values
* in other groups. For example we don't hash layer combine
* constants if they are aren't referenced by the current layer
* combine function.
*/
mask = COGL_PIPELINE_LAYER_STATE_ALL_SPARSE;
_cogl_pipeline_layer_resolve_authorities (layer,
mask,
authorities);
/* So we go right ahead and hash the sparse state... */
for (i = 0; i < COGL_PIPELINE_LAYER_STATE_COUNT; i++)
{
unsigned long current_state = (1L<<i);
/* XXX: we are hashing the un-mixed hash values of all the
* individual state groups; we should provide a means to test
* the quality of the final hash values we are getting with this
* approach... */
if (differences & current_state)
{
CoglPipelineLayer *authority = authorities[i];
layer_state_hash_functions[i] (authority, authorities, state);
}
if (current_state > differences)
break;
}
return TRUE;
}
void
_cogl_pipeline_hash_layers_state (CoglPipeline *authority,
CoglPipelineHashState *state)
{
state->hash =
_cogl_util_one_at_a_time_hash (state->hash, &authority->n_layers,
sizeof (authority->n_layers));
_cogl_pipeline_foreach_layer_internal (authority,
_cogl_pipeline_hash_layer_cb,
state);
}
typedef void (*StateHashFunction) (CoglPipeline *authority, CoglPipelineHashState *state);
static StateHashFunction
state_hash_functions[COGL_PIPELINE_STATE_SPARSE_COUNT];
/* We don't statically initialize the array of hash functions
* so we won't get caught out by later re-indexing the groups for
* some reason. */
void
_cogl_pipeline_init_state_hash_functions (void)
{
state_hash_functions[COGL_PIPELINE_STATE_COLOR_INDEX] =
_cogl_pipeline_hash_color_state;
state_hash_functions[COGL_PIPELINE_STATE_BLEND_ENABLE_INDEX] =
_cogl_pipeline_hash_blend_enable_state;
state_hash_functions[COGL_PIPELINE_STATE_LAYERS_INDEX] =
_cogl_pipeline_hash_layers_state;
state_hash_functions[COGL_PIPELINE_STATE_LIGHTING_INDEX] =
_cogl_pipeline_hash_lighting_state;
state_hash_functions[COGL_PIPELINE_STATE_ALPHA_FUNC_INDEX] =
_cogl_pipeline_hash_alpha_func_state;
state_hash_functions[COGL_PIPELINE_STATE_ALPHA_FUNC_REFERENCE_INDEX] =
_cogl_pipeline_hash_alpha_func_reference_state;
state_hash_functions[COGL_PIPELINE_STATE_BLEND_INDEX] =
_cogl_pipeline_hash_blend_state;
state_hash_functions[COGL_PIPELINE_STATE_USER_SHADER_INDEX] =
_cogl_pipeline_hash_user_shader_state;
state_hash_functions[COGL_PIPELINE_STATE_DEPTH_INDEX] =
_cogl_pipeline_hash_depth_state;
state_hash_functions[COGL_PIPELINE_STATE_FOG_INDEX] =
_cogl_pipeline_hash_fog_state;
state_hash_functions[COGL_PIPELINE_STATE_POINT_SIZE_INDEX] =
_cogl_pipeline_hash_point_size_state;
state_hash_functions[COGL_PIPELINE_STATE_LOGIC_OPS_INDEX] =
_cogl_pipeline_hash_logic_ops_state;
/* So we get a big error if we forget to update this code! */
g_assert (COGL_PIPELINE_STATE_SPARSE_COUNT == 12);
}
unsigned int
_cogl_pipeline_hash (CoglPipeline *pipeline,
unsigned long differences,
unsigned long layer_differences,
CoglPipelineEvalFlags flags)
{
CoglPipeline *authorities[COGL_PIPELINE_STATE_SPARSE_COUNT];
unsigned long mask;
int i;
CoglPipelineHashState state;
unsigned int final_hash = 0;
state.hash = 0;
state.layer_differences = layer_differences;
state.flags = flags;
/* hash non-sparse state */
if (differences & COGL_PIPELINE_STATE_REAL_BLEND_ENABLE)
{
gboolean enable = pipeline->real_blend_enable;
state.hash =
_cogl_util_one_at_a_time_hash (state.hash, &enable, sizeof (enable));
}
/* hash sparse state */
mask = differences & COGL_PIPELINE_STATE_ALL_SPARSE;
_cogl_pipeline_resolve_authorities (pipeline, mask, authorities);
for (i = 0; i < COGL_PIPELINE_STATE_SPARSE_COUNT; i++)
{
unsigned long current_state = (1L<<i);
/* XXX: we are hashing the un-mixed hash values of all the
* individual state groups; we should provide a means to test
* the quality of the final hash values we are getting with this
* approach... */
if (differences & current_state)
{
CoglPipeline *authority = authorities[i];
state_hash_functions[i] (authority, &state);
final_hash = _cogl_util_one_at_a_time_hash (final_hash, &state.hash,
sizeof (state.hash));
}
if (current_state > differences)
break;
}
return _cogl_util_one_at_a_time_mix (final_hash);
}
typedef struct
{
int parent_id;
int *node_id_ptr;
GString *graph;
int indent;
} PrintDebugState;
static gboolean
dump_layer_cb (CoglPipelineNode *node, void *user_data)
{
CoglPipelineLayer *layer = COGL_PIPELINE_LAYER (node);
PrintDebugState *state = user_data;
int layer_id = *state->node_id_ptr;
PrintDebugState state_out;
GString *changes_label;
gboolean changes = FALSE;
if (state->parent_id >= 0)
g_string_append_printf (state->graph, "%*slayer%p -> layer%p;\n",
state->indent, "",
layer->_parent.parent,
layer);
g_string_append_printf (state->graph,
"%*slayer%p [label=\"layer=0x%p\\n"
"ref count=%d\" "
"color=\"blue\"];\n",
state->indent, "",
layer,
layer,
COGL_OBJECT (layer)->ref_count);
changes_label = g_string_new ("");
g_string_append_printf (changes_label,
"%*slayer%p -> layer_state%d [weight=100];\n"
"%*slayer_state%d [shape=box label=\"",
state->indent, "",
layer,
layer_id,
state->indent, "",
layer_id);
if (layer->differences & COGL_PIPELINE_LAYER_STATE_UNIT)
{
changes = TRUE;
g_string_append_printf (changes_label,
"\\lunit=%u\\n",
layer->unit_index);
}
if (layer->differences & COGL_PIPELINE_LAYER_STATE_TEXTURE_DATA)
{
changes = TRUE;
g_string_append_printf (changes_label,
"\\ltexture=%p\\n",
layer->texture);
}
if (changes)
{
g_string_append_printf (changes_label, "\"];\n");
g_string_append (state->graph, changes_label->str);
g_string_free (changes_label, TRUE);
}
state_out.parent_id = layer_id;
state_out.node_id_ptr = state->node_id_ptr;
(*state_out.node_id_ptr)++;
state_out.graph = state->graph;
state_out.indent = state->indent + 2;
_cogl_pipeline_node_foreach_child (COGL_PIPELINE_NODE (layer),
dump_layer_cb,
&state_out);
return TRUE;
}
static gboolean
dump_layer_ref_cb (CoglPipelineLayer *layer, void *data)
{
PrintDebugState *state = data;
int pipeline_id = *state->node_id_ptr;
g_string_append_printf (state->graph,
"%*spipeline_state%d -> layer%p;\n",
state->indent, "",
pipeline_id,
layer);
return TRUE;
}
static gboolean
dump_pipeline_cb (CoglPipelineNode *node, void *user_data)
{
CoglPipeline *pipeline = COGL_PIPELINE (node);
PrintDebugState *state = user_data;
int pipeline_id = *state->node_id_ptr;
PrintDebugState state_out;
GString *changes_label;
gboolean changes = FALSE;
gboolean layers = FALSE;
if (state->parent_id >= 0)
g_string_append_printf (state->graph, "%*spipeline%d -> pipeline%d;\n",
state->indent, "",
state->parent_id,
pipeline_id);
g_string_append_printf (state->graph,
"%*spipeline%d [label=\"pipeline=0x%p\\n"
"ref count=%d\\n"
"breadcrumb=\\\"%s\\\"\" color=\"red\"];\n",
state->indent, "",
pipeline_id,
pipeline,
COGL_OBJECT (pipeline)->ref_count,
pipeline->has_static_breadcrumb ?
pipeline->static_breadcrumb : "NULL");
changes_label = g_string_new ("");
g_string_append_printf (changes_label,
"%*spipeline%d -> pipeline_state%d [weight=100];\n"
"%*spipeline_state%d [shape=box label=\"",
state->indent, "",
pipeline_id,
pipeline_id,
state->indent, "",
pipeline_id);
if (pipeline->differences & COGL_PIPELINE_STATE_COLOR)
{
changes = TRUE;
g_string_append_printf (changes_label,
"\\lcolor=0x%02X%02X%02X%02X\\n",
cogl_color_get_red_byte (&pipeline->color),
cogl_color_get_green_byte (&pipeline->color),
cogl_color_get_blue_byte (&pipeline->color),
cogl_color_get_alpha_byte (&pipeline->color));
}
if (pipeline->differences & COGL_PIPELINE_STATE_BLEND)
{
const char *blend_enable_name;
changes = TRUE;
switch (pipeline->blend_enable)
{
case COGL_PIPELINE_BLEND_ENABLE_AUTOMATIC:
blend_enable_name = "AUTO";
break;
case COGL_PIPELINE_BLEND_ENABLE_ENABLED:
blend_enable_name = "ENABLED";
break;
case COGL_PIPELINE_BLEND_ENABLE_DISABLED:
blend_enable_name = "DISABLED";
break;
default:
blend_enable_name = "UNKNOWN";
}
g_string_append_printf (changes_label,
"\\lblend=%s\\n",
blend_enable_name);
}
if (pipeline->differences & COGL_PIPELINE_STATE_LAYERS)
{
changes = TRUE;
layers = TRUE;
g_string_append_printf (changes_label, "\\ln_layers=%d\\n",
pipeline->n_layers);
}
if (changes)
{
g_string_append_printf (changes_label, "\"];\n");
g_string_append (state->graph, changes_label->str);
g_string_free (changes_label, TRUE);
}
if (layers)
{
g_list_foreach (pipeline->layer_differences,
(GFunc)dump_layer_ref_cb,
state);
}
state_out.parent_id = pipeline_id;
state_out.node_id_ptr = state->node_id_ptr;
(*state_out.node_id_ptr)++;
state_out.graph = state->graph;
state_out.indent = state->indent + 2;
_cogl_pipeline_node_foreach_child (COGL_PIPELINE_NODE (pipeline),
dump_pipeline_cb,
&state_out);
return TRUE;
}
void
_cogl_debug_dump_pipelines_dot_file (const char *filename)
{
GString *graph;
PrintDebugState layer_state;
PrintDebugState pipeline_state;
int layer_id = 0;
int pipeline_id = 0;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (!ctx->default_pipeline)
return;
graph = g_string_new ("");
g_string_append_printf (graph, "digraph {\n");
layer_state.graph = graph;
layer_state.parent_id = -1;
layer_state.node_id_ptr = &layer_id;
layer_state.indent = 0;
dump_layer_cb (ctx->default_layer_0, &layer_state);
pipeline_state.graph = graph;
pipeline_state.parent_id = -1;
pipeline_state.node_id_ptr = &pipeline_id;
pipeline_state.indent = 0;
dump_pipeline_cb (ctx->default_pipeline, &pipeline_state);
g_string_append_printf (graph, "}\n");
if (filename)
g_file_set_contents (filename, graph->str, -1, NULL);
else
g_print ("%s", graph->str);
g_string_free (graph, TRUE);
}
typedef struct
{
int i;
CoglPipelineLayer **layers;
} AddLayersToArrayState;
static gboolean
add_layer_to_array_cb (CoglPipelineLayer *layer,
void *user_data)
{
AddLayersToArrayState *state = user_data;
state->layers[state->i++] = layer;
return TRUE;
}
/* Determines if we need to handle the RGB and A texture combining
* separately or is the same function used for both channel masks and
* with the same arguments...
*/
gboolean
_cogl_pipeline_need_texture_combine_separate
(CoglPipelineLayer *combine_authority)
{
CoglPipelineLayerBigState *big_state = combine_authority->big_state;
int n_args;
int i;
if (big_state->texture_combine_rgb_func !=
big_state->texture_combine_alpha_func)
return TRUE;
n_args = _cogl_get_n_args_for_combine_func (big_state->texture_combine_rgb_func);
for (i = 0; i < n_args; i++)
{
if (big_state->texture_combine_rgb_src[i] !=
big_state->texture_combine_alpha_src[i])
return TRUE;
/*
* We can allow some variation of the source operands without
* needing a separation...
*
* "A = REPLACE (CONSTANT[A])" + either of the following...
* "RGB = REPLACE (CONSTANT[RGB])"
* "RGB = REPLACE (CONSTANT[A])"
*
* can be combined as:
* "RGBA = REPLACE (CONSTANT)" or
* "RGBA = REPLACE (CONSTANT[A])" or
*
* And "A = REPLACE (1-CONSTANT[A])" + either of the following...
* "RGB = REPLACE (1-CONSTANT)" or
* "RGB = REPLACE (1-CONSTANT[A])"
*
* can be combined as:
* "RGBA = REPLACE (1-CONSTANT)" or
* "RGBA = REPLACE (1-CONSTANT[A])"
*/
switch (big_state->texture_combine_alpha_op[i])
{
case GL_SRC_ALPHA:
switch (big_state->texture_combine_rgb_op[i])
{
case GL_SRC_COLOR:
case GL_SRC_ALPHA:
break;
default:
return FALSE;
}
break;
case GL_ONE_MINUS_SRC_ALPHA:
switch (big_state->texture_combine_rgb_op[i])
{
case GL_ONE_MINUS_SRC_COLOR:
case GL_ONE_MINUS_SRC_ALPHA:
break;
default:
return FALSE;
}
break;
default:
return FALSE; /* impossible */
}
}
return FALSE;
}
/* This tries to find the oldest ancestor whose pipeline and layer
state matches the given flags. This is mostly used to detect code
gen authorities so that we can reduce the numer of programs
generated */
CoglPipeline *
_cogl_pipeline_find_equivalent_parent (CoglPipeline *pipeline,
CoglPipelineState pipeline_state,
CoglPipelineLayerState layer_state)
{
CoglPipeline *authority0;
CoglPipeline *authority1;
int n_layers;
CoglPipelineLayer **authority0_layers;
CoglPipelineLayer **authority1_layers;
/* Find the first pipeline that modifies state that affects the
* state or any layer state... */
authority0 = _cogl_pipeline_get_authority (pipeline,
pipeline_state |
COGL_PIPELINE_STATE_LAYERS);
/* Find the next ancestor after that, that also modifies the
* state... */
if (_cogl_pipeline_get_parent (authority0))
{
authority1 =
_cogl_pipeline_get_authority (_cogl_pipeline_get_parent (authority0),
pipeline_state |
COGL_PIPELINE_STATE_LAYERS);
}
else
return authority0;
n_layers = cogl_pipeline_get_n_layers (authority0);
for (;;)
{
AddLayersToArrayState state;
int i;
if (n_layers != cogl_pipeline_get_n_layers (authority1))
return authority0;
/* If the programs differ by anything that isn't part of the
layer state then we can't continue */
if (pipeline_state &&
(_cogl_pipeline_compare_differences (authority0, authority1) &
pipeline_state))
return authority0;
authority0_layers =
g_alloca (sizeof (CoglPipelineLayer *) * n_layers);
state.i = 0;
state.layers = authority0_layers;
_cogl_pipeline_foreach_layer_internal (authority0,
add_layer_to_array_cb,
&state);
authority1_layers =
g_alloca (sizeof (CoglPipelineLayer *) * n_layers);
state.i = 0;
state.layers = authority1_layers;
_cogl_pipeline_foreach_layer_internal (authority1,
add_layer_to_array_cb,
&state);
for (i = 0; i < n_layers; i++)
{
unsigned long layer_differences;
if (authority0_layers[i] == authority1_layers[i])
continue;
layer_differences =
_cogl_pipeline_layer_compare_differences (authority0_layers[i],
authority1_layers[i]);
if (layer_differences & layer_state)
return authority0;
}
/* Find the next ancestor after that, that also modifies state
* affecting codegen... */
if (!_cogl_pipeline_get_parent (authority1))
break;
authority0 = authority1;
authority1 =
_cogl_pipeline_get_authority (_cogl_pipeline_get_parent (authority1),
pipeline_state |
COGL_PIPELINE_STATE_LAYERS);
if (authority1 == authority0)
break;
}
return authority1;
}
CoglPipelineLayerState
_cogl_pipeline_get_layer_state_for_fragment_codegen (CoglContext *context)
{
CoglPipelineLayerState state =
(COGL_PIPELINE_LAYER_STATE_COMBINE |
COGL_PIPELINE_LAYER_STATE_TEXTURE_TARGET |
COGL_PIPELINE_LAYER_STATE_POINT_SPRITE_COORDS |
COGL_PIPELINE_LAYER_STATE_UNIT);
if (context->driver == COGL_DRIVER_GLES2)
state |= COGL_PIPELINE_LAYER_STATE_POINT_SPRITE_COORDS;
return state;
}
CoglPipelineState
_cogl_pipeline_get_state_for_fragment_codegen (CoglContext *context)
{
CoglPipelineState state = (COGL_PIPELINE_STATE_LAYERS |
COGL_PIPELINE_STATE_USER_SHADER);
if (context->driver == COGL_DRIVER_GLES2)
state |= COGL_PIPELINE_STATE_ALPHA_FUNC;
return state;
}
|