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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//===========================================================================//
#define DISABLE_PROTECTED_THINGS
#include "togl/rendermechanism.h"
#include "TransitionTable.h"
#include "recording.h"
#include "shaderapidx8.h"
#include "shaderapi/ishaderutil.h"
#include "tier1/convar.h"
#include "materialsystem/imaterialsystemhardwareconfig.h"
#include "vertexshaderdx8.h"
#include "tier0/vprof.h"
#include "shaderdevicedx8.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
enum
{
TEXTURE_STAGE_BIT_COUNT = 4,
TEXTURE_STAGE_MAX_STAGE = 1 << TEXTURE_STAGE_BIT_COUNT,
TEXTURE_STAGE_MASK = TEXTURE_STAGE_MAX_STAGE - 1,
TEXTURE_OP_BIT_COUNT = 7 - TEXTURE_STAGE_BIT_COUNT,
TEXTURE_OP_SHIFT = TEXTURE_STAGE_BIT_COUNT,
TEXTURE_OP_MASK = ((1 << TEXTURE_OP_BIT_COUNT) - 1) << TEXTURE_OP_SHIFT,
};
//-----------------------------------------------------------------------------
// Texture op compressing/uncompressing
//-----------------------------------------------------------------------------
inline unsigned char TextureOp( TextureStateFunc_t func, int stage )
{
// This fails if we've added too many texture stages states to fit in a byte.
COMPILE_TIME_ASSERT( TEXTURE_STATE_COUNT < (1 << TEXTURE_OP_BIT_COUNT) );
Assert( stage < TEXTURE_STAGE_MAX_STAGE );
return ((func << TEXTURE_OP_SHIFT) & TEXTURE_OP_MASK) | (stage & TEXTURE_STAGE_MASK);
}
inline void GetTextureOp( unsigned char nBits, TextureStateFunc_t *pFunc, int *pStage )
{
*pStage = (nBits & TEXTURE_STAGE_MASK);
*pFunc = (TextureStateFunc_t)((nBits & TEXTURE_OP_MASK) >> TEXTURE_OP_SHIFT);
}
//-----------------------------------------------------------------------------
// Stats
//-----------------------------------------------------------------------------
static int s_pRenderTransitions[RENDER_STATE_COUNT];
static int s_pTextureTransitions[TEXTURE_STATE_COUNT][TEXTURE_STAGE_MAX_STAGE];
//-----------------------------------------------------------------------------
// Singleton
//-----------------------------------------------------------------------------
CTransitionTable *g_pTransitionTable = NULL;
#ifdef DEBUG_BOARD_STATE
inline ShadowState_t& BoardState()
{
return g_pTransitionTable->BoardState();
}
#endif
inline CTransitionTable::CurrentState_t& CurrentState()
{
return g_pTransitionTable->CurrentState();
}
//-----------------------------------------------------------------------------
// Less functions
//-----------------------------------------------------------------------------
bool CTransitionTable::ShadowStateDictLessFunc::Less( const CTransitionTable::ShadowStateDictEntry_t &src1, const CTransitionTable::ShadowStateDictEntry_t &src2, void *pCtx )
{
return src1.m_nChecksum < src2.m_nChecksum;
}
bool CTransitionTable::SnapshotDictLessFunc::Less( const CTransitionTable::SnapshotDictEntry_t &src1, const CTransitionTable::SnapshotDictEntry_t &src2, void *pCtx )
{
return src1.m_nChecksum < src2.m_nChecksum;
}
bool CTransitionTable::UniqueSnapshotLessFunc::Less( const CTransitionTable::TransitionList_t &src1, const CTransitionTable::TransitionList_t &src2, void *pCtx )
{
return src1.m_NumOperations > src2.m_NumOperations;
}
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
CTransitionTable::CTransitionTable() : m_DefaultStateSnapshot(-1),
m_CurrentShadowId(-1), m_CurrentSnapshotId(-1), m_TransitionOps( 0, 8192 ), m_ShadowStateList( 0, 256 ),
m_TransitionTable( 0, 256 ), m_SnapshotList( 0, 256 ),
m_ShadowStateDict(0, 256 ),
m_SnapshotDict( 0, 256 ),
m_UniqueTransitions( 0, 4096 )
{
Assert( !g_pTransitionTable );
g_pTransitionTable = this;
#ifdef DEBUG_BOARD_STATE
memset( &m_BoardState, 0, sizeof( m_BoardState ) );
memset( &m_BoardShaderState, 0, sizeof( m_BoardShaderState ) );
#endif
}
CTransitionTable::~CTransitionTable()
{
Assert( g_pTransitionTable == this );
g_pTransitionTable = NULL;
}
//-----------------------------------------------------------------------------
// Initialization, shutdown
//-----------------------------------------------------------------------------
bool CTransitionTable::Init( )
{
return true;
}
void CTransitionTable::Shutdown( )
{
Reset();
}
//-----------------------------------------------------------------------------
// Creates a shadow, adding an entry into the shadow list and transition table
//-----------------------------------------------------------------------------
StateSnapshot_t CTransitionTable::CreateStateSnapshot( ShadowStateId_t shadowStateId, const ShadowShaderState_t& currentShaderState )
{
StateSnapshot_t snapshotId = m_SnapshotList.AddToTail();
// Copy our snapshot into the list
SnapshotShaderState_t &shaderState = m_SnapshotList[snapshotId];
shaderState.m_ShadowStateId = shadowStateId;
memcpy( &shaderState.m_ShaderState, ¤tShaderState, sizeof(ShadowShaderState_t) );
memset( shaderState.m_ShaderState.m_nReserved, 0, sizeof( shaderState.m_ShaderState.m_nReserved ) );
shaderState.m_nReserved = 0; // needed to get a good CRC
shaderState.m_nReserved2 = 0;
// Insert entry into the lookup table
SnapshotDictEntry_t insert;
CRC32_Init( &insert.m_nChecksum );
CRC32_ProcessBuffer( &insert.m_nChecksum, &shaderState, sizeof(SnapshotShaderState_t) );
CRC32_Final( &insert.m_nChecksum );
insert.m_nSnapshot = snapshotId;
m_SnapshotDict.Insert( insert );
return snapshotId;
}
//-----------------------------------------------------------------------------
// Creates a shadow, adding an entry into the shadow list and transition table
//-----------------------------------------------------------------------------
CTransitionTable::ShadowStateId_t CTransitionTable::CreateShadowState( const ShadowState_t ¤tState )
{
int newShaderState = m_ShadowStateList.AddToTail();
// Copy our snapshot into the list
memcpy( &m_ShadowStateList[newShaderState], ¤tState, sizeof(ShadowState_t) );
// all existing states must transition to the new state
int i;
for ( i = 0; i < newShaderState; ++i )
{
// Add a new transition to all existing states
int newElem = m_TransitionTable[i].AddToTail();
m_TransitionTable[i][newElem].m_FirstOperation = INVALID_TRANSITION_OP;
m_TransitionTable[i][newElem].m_NumOperations = 0;
}
// Add a new vector for this transition
int newTransitionElem = m_TransitionTable.AddToTail();
m_TransitionTable[newTransitionElem].EnsureCapacity( 32 );
Assert( newShaderState == newTransitionElem );
for ( i = 0; i <= newShaderState; ++i )
{
// Add a new transition from all existing states
int newElem = m_TransitionTable[newShaderState].AddToTail();
m_TransitionTable[newShaderState][newElem].m_FirstOperation = INVALID_TRANSITION_OP;
m_TransitionTable[newShaderState][newElem].m_NumOperations = 0;
}
// Insert entry into the lookup table
ShadowStateDictEntry_t insert;
CRC32_Init( &insert.m_nChecksum );
CRC32_ProcessBuffer( &insert.m_nChecksum, &m_ShadowStateList[newShaderState], sizeof(ShadowState_t) );
CRC32_Final( &insert.m_nChecksum );
insert.m_nShadowStateId = newShaderState;
m_ShadowStateDict.Insert( insert );
return newShaderState;
}
//-----------------------------------------------------------------------------
// Finds a snapshot, if it exists. Or creates a new one if it doesn't.
//-----------------------------------------------------------------------------
CTransitionTable::ShadowStateId_t CTransitionTable::FindShadowState( const ShadowState_t& currentState ) const
{
ShadowStateDictEntry_t find;
CRC32_Init( &find.m_nChecksum );
CRC32_ProcessBuffer( &find.m_nChecksum, ¤tState, sizeof(ShadowState_t) );
CRC32_Final( &find.m_nChecksum );
int nDictCount = m_ShadowStateDict.Count();
int i = m_ShadowStateDict.FindLessOrEqual( find );
if ( i < 0 )
return (ShadowStateId_t)-1;
for ( ; i < nDictCount; ++i )
{
const ShadowStateDictEntry_t &entry = m_ShadowStateDict[i];
// Didn't find a match
if ( entry.m_nChecksum > find.m_nChecksum )
break;
if ( entry.m_nChecksum != find.m_nChecksum )
continue;
ShadowStateId_t nShadowState = entry.m_nShadowStateId;
if (!memcmp(&m_ShadowStateList[nShadowState], ¤tState, sizeof(ShadowState_t) ))
return nShadowState;
}
// Need to create a new one
return (ShadowStateId_t)-1;
}
//-----------------------------------------------------------------------------
// Finds a snapshot, if it exists. Or creates a new one if it doesn't.
//-----------------------------------------------------------------------------
StateSnapshot_t CTransitionTable::FindStateSnapshot( ShadowStateId_t id, const ShadowShaderState_t& currentState ) const
{
SnapshotShaderState_t temp;
temp.m_ShaderState = currentState;
temp.m_ShadowStateId = id;
memset( temp.m_ShaderState.m_nReserved, 0, sizeof( temp.m_ShaderState.m_nReserved ) );
temp.m_nReserved = 0; // needed to get a good CRC
temp.m_nReserved2 = 0;
SnapshotDictEntry_t find;
CRC32_Init( &find.m_nChecksum );
CRC32_ProcessBuffer( &find.m_nChecksum, &temp, sizeof(temp) );
CRC32_Final( &find.m_nChecksum );
int nDictCount = m_SnapshotDict.Count();
int i = m_SnapshotDict.FindLessOrEqual( find );
if ( i < 0 )
return (StateSnapshot_t)-1;
for ( ; i < nDictCount; ++i )
{
// Didn't find a match
if ( m_SnapshotDict[i].m_nChecksum > find.m_nChecksum )
break;
if ( m_SnapshotDict[i].m_nChecksum != find.m_nChecksum )
continue;
StateSnapshot_t nShapshot = m_SnapshotDict[i].m_nSnapshot;
if ( (id == m_SnapshotList[nShapshot].m_ShadowStateId) &&
!memcmp(&m_SnapshotList[nShapshot].m_ShaderState, ¤tState, sizeof(ShadowShaderState_t)) )
{
return nShapshot;
}
}
// Need to create a new one
return (StateSnapshot_t)-1;
}
//-----------------------------------------------------------------------------
// Used to clear the transition table when we know it's become invalid.
//-----------------------------------------------------------------------------
void CTransitionTable::Reset()
{
m_ShadowStateList.RemoveAll();
m_SnapshotList.RemoveAll();
m_TransitionTable.RemoveAll();
m_TransitionOps.RemoveAll();
m_ShadowStateDict.RemoveAll();
m_SnapshotDict.RemoveAll();
m_UniqueTransitions.RemoveAll();
m_CurrentShadowId = -1;
m_CurrentSnapshotId = -1;
m_DefaultStateSnapshot = -1;
}
//-----------------------------------------------------------------------------
// Sets the texture stage state
//-----------------------------------------------------------------------------
#ifdef _WIN32
#pragma warning( disable : 4189 )
#endif
static inline void SetTextureStageState( int stage, D3DTEXTURESTAGESTATETYPE state, DWORD val )
{
#if !defined( _X360 )
Assert( !g_pShaderDeviceDx8->IsDeactivated() );
Dx9Device()->SetTextureStageState( stage, state, val );
#endif
}
//Moved to a #define so every instance of this skips unsupported render states at compile time
#define SetSamplerState( _stage, _state, _val ) \
{ \
if ( (_state != D3DSAMP_NOTSUPPORTED) ) \
{ \
Assert( !g_pShaderDeviceDx8->IsDeactivated() ); \
Dx9Device()->SetSamplerState( _stage, _state, _val ); \
} \
}
//Moved to a #define so every instance of this skips unsupported render states at compile time
#define SetRenderState( _state, _val ) \
{ \
if ( _state != D3DRS_NOTSUPPORTED ) \
{ \
Assert( !g_pShaderDeviceDx8->IsDeactivated() ); \
Dx9Device()->SetRenderState( _state, _val ); \
} \
}
#ifdef DX_TO_GL_ABSTRACTION
#define SetRenderStateConstMacro( state, val ) { if ( state != D3DRS_NOTSUPPORTED ) Dx9Device()->SetRenderStateConstInline( state, val ); }
#else
#define SetRenderStateConstMacro( state, val ) SetRenderState( state, val )
#endif
#ifdef _WIN32
#pragma warning( default : 4189 )
#endif
//-----------------------------------------------------------------------------
// Methods that actually apply the state
//-----------------------------------------------------------------------------
#ifdef DEBUG_BOARD_STATE
static bool g_SpewTransitions = false;
#define UPDATE_BOARD_RENDER_STATE( _d3dState, _state ) \
{ \
BoardState().m_ ## _state = shaderState.m_ ## _state; \
if (g_SpewTransitions) \
{ \
char buf[128]; \
sprintf( buf, "Apply %s : %d\n", #_d3dState, shaderState.m_ ## _state ); \
Plat_DebugString(buf); \
} \
}
#define UPDATE_BOARD_TEXTURE_STAGE_STATE( _d3dState, _state, _stage ) \
{ \
BoardState().m_TextureStage[_stage].m_ ## _state = shaderState.m_TextureStage[_stage].m_ ## _state; \
if (g_SpewTransitions) \
{ \
char buf[128]; \
sprintf( buf, "Apply Tex %s (%d): %d\n", #_d3dState, _stage, shaderState.m_TextureStage[_stage].m_ ## _state ); \
Plat_DebugString(buf); \
} \
}
#define UPDATE_BOARD_SAMPLER_STATE( _d3dState, _state, _stage ) \
{ \
BoardState().m_SamplerState[_stage].m_ ## _state = shaderState.m_SamplerState[_stage].m_ ## _state; \
if (g_SpewTransitions) \
{ \
char buf[128]; \
sprintf( buf, "Apply SamplerSate %s (%d): %d\n", #_d3dState, stage, shaderState.m_SamplerState[_stage].m_ ## _state ); \
Plat_DebugString(buf); \
} \
}
#else
#define UPDATE_BOARD_RENDER_STATE( _d3dState, _state ) {}
#define UPDATE_BOARD_TEXTURE_STAGE_STATE( _d3dState, _state, _stage ) {}
#define UPDATE_BOARD_SAMPLER_STATE( _d3dState, _state, _stage ) {}
#endif
#define APPLY_RENDER_STATE_FUNC( _d3dState, _state ) \
void Apply ## _state( const ShadowState_t& shaderState, int arg ) \
{ \
SetRenderState( _d3dState, shaderState.m_ ## _state ); \
UPDATE_BOARD_RENDER_STATE( _d3dState, _state ); \
}
#define APPLY_TEXTURE_STAGE_STATE_FUNC( _d3dState, _state ) \
void Apply ## _state( const ShadowState_t& shaderState, int stage ) \
{ \
SetTextureStageState( stage, _d3dState, shaderState.m_TextureStage[stage].m_ ## _state ); \
UPDATE_BOARD_TEXTURE_STAGE_STATE( _d3dState, _state, stage ); \
}
#define APPLY_SAMPLER_STATE_FUNC( _d3dState, _state ) \
void Apply ## _state( const ShadowState_t& shaderState, int stage ) \
{ \
SetSamplerState( stage, _d3dState, shaderState.m_SamplerState[stage].m_ ## _state ); \
UPDATE_BOARD_SAMPLER_STATE( _d3dState, _state, stage ); \
}
// Special overridden sampler state to turn on Fetch4 on ATI hardware (and 360?)
void ApplyFetch4Enable( const ShadowState_t& shaderState, int stage )
{
if ( ShaderAPI()->SupportsFetch4() )
{
SetSamplerState( stage, ATISAMP_FETCH4, shaderState.m_SamplerState[stage].m_Fetch4Enable ? ATI_FETCH4_ENABLE : ATI_FETCH4_DISABLE );
}
UPDATE_BOARD_SAMPLER_STATE( ATISAMP_FETCH4, Fetch4Enable, stage );
}
#ifdef DX_TO_GL_ABSTRACTION
void ApplyShadowFilterEnable( const ShadowState_t& shaderState, int stage )
{
SetSamplerState( stage, D3DSAMP_SHADOWFILTER, shaderState.m_SamplerState[stage].m_ShadowFilterEnable );
UPDATE_BOARD_SAMPLER_STATE( D3DSAMP_SHADOWFILTER, ShadowFilterEnable, stage );
}
#endif
//APPLY_RENDER_STATE_FUNC( D3DRS_ZWRITEENABLE, ZWriteEnable )
//APPLY_RENDER_STATE_FUNC( D3DRS_COLORWRITEENABLE, ColorWriteEnable )
APPLY_RENDER_STATE_FUNC( D3DRS_FILLMODE, FillMode )
APPLY_RENDER_STATE_FUNC( D3DRS_LIGHTING, Lighting )
APPLY_RENDER_STATE_FUNC( D3DRS_SPECULARENABLE, SpecularEnable )
APPLY_RENDER_STATE_FUNC( D3DRS_DIFFUSEMATERIALSOURCE, DiffuseMaterialSource )
APPLY_TEXTURE_STAGE_STATE_FUNC( D3DTSS_TEXCOORDINDEX, TexCoordIndex )
void ApplyZWriteEnable( const ShadowState_t& shaderState, int arg )
{
SetRenderStateConstMacro( D3DRS_ZWRITEENABLE, shaderState.m_ZWriteEnable );
#if defined( _X360 )
//SetRenderStateConstMacro( D3DRS_HIZWRITEENABLE, shaderState.m_ZWriteEnable ? D3DHIZ_AUTOMATIC : D3DHIZ_DISABLE );
#endif
UPDATE_BOARD_RENDER_STATE( D3DRS_ZWRITEENABLE, ZWriteEnable );
}
void ApplyColorWriteEnable( const ShadowState_t& shaderState, int arg )
{
SetRenderState( D3DRS_COLORWRITEENABLE, shaderState.m_ColorWriteEnable );
g_pTransitionTable->CurrentState().m_ColorWriteEnable = shaderState.m_ColorWriteEnable;
UPDATE_BOARD_RENDER_STATE( D3DRS_COLORWRITEENABLE, ColorWriteEnable );
}
void ApplySRGBReadEnable( const ShadowState_t& shaderState, int stage )
{
# if ( !defined( _X360 ) )
{
SetSamplerState( stage, D3DSAMP_SRGBTEXTURE, shaderState.m_SamplerState[stage].m_SRGBReadEnable );
}
# else
{
ShaderAPI()->ApplySRGBReadState( stage, shaderState.m_SamplerState[stage].m_SRGBReadEnable );
}
# endif
UPDATE_BOARD_SAMPLER_STATE( D3DSAMP_SRGBTEXTURE, SRGBReadEnable, stage );
}
void ApplySRGBWriteEnable( const ShadowState_t& shadowState, int stageUnused )
{
g_pTransitionTable->ApplySRGBWriteEnable( shadowState );
}
void CTransitionTable::ApplySRGBWriteEnable( const ShadowState_t& shaderState )
{
// ApplySRGBWriteEnable set to true means that the shader is writing linear values.
if ( CurrentState().m_bLinearColorSpaceFrameBufferEnable )
{
// The shader had better be writing linear values since we can't convert to gamma here.
// Can't leave this assert here since there are cases where the shader is doing the right thing.
// This is good to test occasionally to make sure that the shaders are doing the right thing.
// Assert( shaderState.m_SRGBWriteEnable );
// render target is linear
SetRenderStateConstMacro( D3DRS_SRGBWRITEENABLE, 0 );
ShaderAPI()->EnabledSRGBWrite( false );
// fog isn't fixed-function with linear frame buffers, so don't bother with that here.
}
else
{
// render target is gamma
// SRGBWrite enable can affect the space in which fog color is defined
if ( HardwareConfig()->NeedsShaderSRGBConversion() )
{
if ( HardwareConfig()->SupportsPixelShaders_2_b() ) //in 2b supported devices, we never actually enable SRGB writes, but instead handle the conversion in the pixel shader. But we want all other code to be unaware.
{
SetRenderStateConstMacro( D3DRS_SRGBWRITEENABLE, 0 );
}
else
{
SetRenderStateConstMacro( D3DRS_SRGBWRITEENABLE, shaderState.m_SRGBWriteEnable );
}
}
else
{
SetRenderStateConstMacro( D3DRS_SRGBWRITEENABLE, shaderState.m_SRGBWriteEnable );
}
ShaderAPI()->EnabledSRGBWrite( shaderState.m_SRGBWriteEnable );
if ( HardwareConfig()->SpecifiesFogColorInLinearSpace() )
{
ShaderAPI()->ApplyFogMode( shaderState.m_FogMode, shaderState.m_SRGBWriteEnable, shaderState.m_bDisableFogGammaCorrection );
}
}
#ifdef _DEBUG
BoardState().m_SRGBWriteEnable = shaderState.m_SRGBWriteEnable;
if (g_SpewTransitions)
{
char buf[128];
sprintf( buf, "Apply %s : %d\n", "D3DRS_SRGBWRITEENABLE", shaderState.m_SRGBWriteEnable );
Plat_DebugString(buf);
}
#endif
}
void ApplyDisableFogGammaCorrection( const ShadowState_t& shadowState, int stageUnused )
{
ShaderAPI()->ApplyFogMode( shadowState.m_FogMode, shadowState.m_SRGBWriteEnable, shadowState.m_bDisableFogGammaCorrection );
#ifdef DEBUG_BOARD_STATE
g_pTransitionTable->BoardState().m_bDisableFogGammaCorrection = shadowState.m_bDisableFogGammaCorrection;
#endif
}
void ApplyDepthTest( const ShadowState_t& state, int stage )
{
g_pTransitionTable->ApplyDepthTest( state );
}
void CTransitionTable::SetZEnable( D3DZBUFFERTYPE nEnable )
{
if (m_CurrentState.m_ZEnable != nEnable )
{
SetRenderStateConstMacro( D3DRS_ZENABLE, nEnable );
#if defined( _X360 )
//SetRenderState( D3DRS_HIZENABLE, nEnable ? D3DHIZ_AUTOMATIC : D3DHIZ_DISABLE );
#endif
m_CurrentState.m_ZEnable = nEnable;
}
}
void CTransitionTable::SetZFunc( D3DCMPFUNC nCmpFunc )
{
if (m_CurrentState.m_ZFunc != nCmpFunc )
{
SetRenderStateConstMacro( D3DRS_ZFUNC, nCmpFunc );
m_CurrentState.m_ZFunc = nCmpFunc;
}
}
void CTransitionTable::ApplyDepthTest( const ShadowState_t& state )
{
SetZEnable( state.m_ZEnable );
if (state.m_ZEnable != D3DZB_FALSE)
{
SetZFunc( state.m_ZFunc );
}
if (m_CurrentState.m_ZBias != state.m_ZBias)
{
ShaderAPI()->ApplyZBias( state );
m_CurrentState.m_ZBias = (PolygonOffsetMode_t) state.m_ZBias; // Cast two bits from m_ZBias
}
#ifdef DEBUG_BOARD_STATE
// This isn't quite true, but it's necessary for other error checking to work
BoardState().m_ZEnable = state.m_ZEnable;
BoardState().m_ZFunc = state.m_ZFunc;
BoardState().m_ZBias = state.m_ZBias;
#endif
}
void ApplyAlphaTest( const ShadowState_t& state, int stage )
{
g_pTransitionTable->ApplyAlphaTest( state );
}
void CTransitionTable::ApplyAlphaTest( const ShadowState_t& state )
{
if (m_CurrentState.m_AlphaTestEnable != state.m_AlphaTestEnable)
{
SetRenderStateConstMacro( D3DRS_ALPHATESTENABLE, state.m_AlphaTestEnable );
m_CurrentState.m_AlphaTestEnable = state.m_AlphaTestEnable;
}
if (state.m_AlphaTestEnable)
{
// Set the blend state here...
if (m_CurrentState.m_AlphaFunc != state.m_AlphaFunc)
{
SetRenderStateConstMacro( D3DRS_ALPHAFUNC, state.m_AlphaFunc );
m_CurrentState.m_AlphaFunc = state.m_AlphaFunc;
}
if (m_CurrentState.m_AlphaRef != state.m_AlphaRef)
{
SetRenderStateConstMacro( D3DRS_ALPHAREF, state.m_AlphaRef );
m_CurrentState.m_AlphaRef = state.m_AlphaRef;
}
}
#ifdef DEBUG_BOARD_STATE
// This isn't quite true, but it's necessary for other error checking to work
BoardState().m_AlphaTestEnable = state.m_AlphaTestEnable;
BoardState().m_AlphaFunc = state.m_AlphaFunc;
BoardState().m_AlphaRef = state.m_AlphaRef;
#endif
}
void ApplyAlphaBlend( const ShadowState_t& state, int stage )
{
g_pTransitionTable->ApplyAlphaBlend( state );
}
void CTransitionTable::ApplyAlphaBlend( const ShadowState_t& state )
{
if (m_CurrentState.m_AlphaBlendEnable != state.m_AlphaBlendEnable)
{
SetRenderStateConstMacro( D3DRS_ALPHABLENDENABLE, state.m_AlphaBlendEnable );
m_CurrentState.m_AlphaBlendEnable = state.m_AlphaBlendEnable;
}
if (state.m_AlphaBlendEnable)
{
// Set the blend state here...
if (m_CurrentState.m_SrcBlend != state.m_SrcBlend)
{
SetRenderStateConstMacro( D3DRS_SRCBLEND, state.m_SrcBlend );
m_CurrentState.m_SrcBlend = state.m_SrcBlend;
}
if (m_CurrentState.m_DestBlend != state.m_DestBlend)
{
SetRenderStateConstMacro( D3DRS_DESTBLEND, state.m_DestBlend );
m_CurrentState.m_DestBlend = state.m_DestBlend;
}
if (m_CurrentState.m_BlendOp != state.m_BlendOp )
{
SetRenderStateConstMacro( D3DRS_BLENDOP, state.m_BlendOp );
m_CurrentState.m_BlendOp = state.m_BlendOp;
}
}
#ifdef DEBUG_BOARD_STATE
// This isn't quite true, but it's necessary for other error checking to work
BoardState().m_AlphaBlendEnable = state.m_AlphaBlendEnable;
BoardState().m_SrcBlend = state.m_SrcBlend;
BoardState().m_DestBlend = state.m_DestBlend;
BoardState().m_BlendOp = state.m_BlendOp;
#endif
}
void ApplySeparateAlphaBlend( const ShadowState_t& state, int stage )
{
g_pTransitionTable->ApplySeparateAlphaBlend( state );
}
void CTransitionTable::ApplySeparateAlphaBlend( const ShadowState_t& state )
{
if (m_CurrentState.m_SeparateAlphaBlendEnable != state.m_SeparateAlphaBlendEnable)
{
SetRenderStateConstMacro( D3DRS_SEPARATEALPHABLENDENABLE, state.m_SeparateAlphaBlendEnable );
m_CurrentState.m_SeparateAlphaBlendEnable = state.m_SeparateAlphaBlendEnable;
}
if (state.m_SeparateAlphaBlendEnable)
{
// Set the blend state here...
if (m_CurrentState.m_SrcBlendAlpha != state.m_SrcBlendAlpha)
{
SetRenderStateConstMacro( D3DRS_SRCBLENDALPHA, state.m_SrcBlendAlpha );
m_CurrentState.m_SrcBlendAlpha = state.m_SrcBlendAlpha;
}
if (m_CurrentState.m_DestBlendAlpha != state.m_DestBlendAlpha)
{
SetRenderStateConstMacro( D3DRS_DESTBLENDALPHA, state.m_DestBlendAlpha );
m_CurrentState.m_DestBlendAlpha = state.m_DestBlendAlpha;
}
if (m_CurrentState.m_BlendOpAlpha != state.m_BlendOpAlpha )
{
SetRenderStateConstMacro( D3DRS_BLENDOPALPHA, state.m_BlendOpAlpha );
m_CurrentState.m_BlendOpAlpha = state.m_BlendOpAlpha;
}
}
#ifdef DEBUG_BOARD_STATE
// This isn't quite true, but it's necessary for other error checking to work
BoardState().m_SeparateAlphaBlendEnable = state.m_SeparateAlphaBlendEnable;
BoardState().m_SrcBlendAlpha = state.m_SrcBlendAlpha;
BoardState().m_DestBlendAlpha = state.m_DestBlendAlpha;
BoardState().m_BlendOpAlpha = state.m_BlendOpAlpha;
#endif
}
//-----------------------------------------------------------------------------
// Applies alpha texture op
//-----------------------------------------------------------------------------
void ApplyColorTextureStage( const ShadowState_t& state, int stage )
{
g_pTransitionTable->ApplyColorTextureStage( state, stage );
}
void ApplyAlphaTextureStage( const ShadowState_t& state, int stage )
{
g_pTransitionTable->ApplyAlphaTextureStage( state, stage );
}
void CTransitionTable::ApplyColorTextureStage( const ShadowState_t& state, int stage )
{
D3DTEXTUREOP op = state.m_TextureStage[stage].m_ColorOp;
int arg1 = state.m_TextureStage[stage].m_ColorArg1;
int arg2 = state.m_TextureStage[stage].m_ColorArg2;
if (m_CurrentState.m_TextureStage[stage].m_ColorOp != op)
{
SetTextureStageState( stage, D3DTSS_COLOROP, op );
m_CurrentState.m_TextureStage[stage].m_ColorOp = op;
}
if (op != D3DTOP_DISABLE)
{
if (m_CurrentState.m_TextureStage[stage].m_ColorArg1 != arg1)
{
SetTextureStageState( stage, D3DTSS_COLORARG1, arg1 );
m_CurrentState.m_TextureStage[stage].m_ColorArg1 = arg1;
}
if (m_CurrentState.m_TextureStage[stage].m_ColorArg2 != arg2)
{
SetTextureStageState( stage, D3DTSS_COLORARG2, arg2 );
m_CurrentState.m_TextureStage[stage].m_ColorArg2 = arg2;
}
}
#ifdef DEBUG_BOARD_STATE
// This isn't quite true, but it's necessary for other error checking to work
BoardState().m_TextureStage[stage].m_ColorOp = op;
BoardState().m_TextureStage[stage].m_ColorArg1 = arg1;
BoardState().m_TextureStage[stage].m_ColorArg2 = arg2;
#endif
}
void CTransitionTable::ApplyAlphaTextureStage( const ShadowState_t& state, int stage )
{
D3DTEXTUREOP op = state.m_TextureStage[stage].m_AlphaOp;
int arg1 = state.m_TextureStage[stage].m_AlphaArg1;
int arg2 = state.m_TextureStage[stage].m_AlphaArg2;
if (m_CurrentState.m_TextureStage[stage].m_AlphaOp != op)
{
SetTextureStageState( stage, D3DTSS_ALPHAOP, op );
m_CurrentState.m_TextureStage[stage].m_AlphaOp = op;
}
if (op != D3DTOP_DISABLE)
{
if (m_CurrentState.m_TextureStage[stage].m_AlphaArg1 != arg1)
{
SetTextureStageState( stage, D3DTSS_ALPHAARG1, arg1 );
m_CurrentState.m_TextureStage[stage].m_AlphaArg1 = arg1;
}
if (m_CurrentState.m_TextureStage[stage].m_AlphaArg2 != arg2)
{
SetTextureStageState( stage, D3DTSS_ALPHAARG2, arg2 );
m_CurrentState.m_TextureStage[stage].m_AlphaArg2 = arg2;
}
}
#ifdef DEBUG_BOARD_STATE
// This isn't quite true, but it's necessary for other error checking to work
BoardState().m_TextureStage[stage].m_AlphaOp = op;
BoardState().m_TextureStage[stage].m_AlphaArg1 = arg1;
BoardState().m_TextureStage[stage].m_AlphaArg2 = arg2;
#endif
}
void ApplyActivateFixedFunction( const ShadowState_t& state, int stage )
{
int nStageCount = HardwareConfig()->GetTextureStageCount();
for ( int i = 0; i < nStageCount; ++i )
{
g_pTransitionTable->ApplyColorTextureStage( state, i );
g_pTransitionTable->ApplyAlphaTextureStage( state, i );
}
}
//-----------------------------------------------------------------------------
// Enables textures
//-----------------------------------------------------------------------------
void ApplyTextureEnable( const ShadowState_t& state, int stage )
{
// This may well enable/disable textures that are already enabled/disabled
// but the ShaderAPI will handle that
int i;
int nSamplerCount = HardwareConfig()->GetSamplerCount();
for ( i = 0; i < nSamplerCount; ++i )
{
ShaderAPI()->ApplyTextureEnable( state, i );
#ifdef DEBUG_BOARD_STATE
BoardState().m_SamplerState[i].m_TextureEnable = state.m_SamplerState[i].m_TextureEnable;
#endif
}
// Needed to prevent mat_dxlevel assertions
#ifdef DEBUG_BOARD_STATE
for ( i = nSamplerCount; i < MAX_SAMPLERS; ++i )
{
BoardState().m_SamplerState[i].m_TextureEnable = false;
}
#endif
}
//-----------------------------------------------------------------------------
// All transitions below this point depend on dynamic render state
// FIXME: Eliminate these virtual calls?
//-----------------------------------------------------------------------------
void ApplyCullEnable( const ShadowState_t& state, int arg )
{
ShaderAPI()->ApplyCullEnable( state.m_CullEnable );
#ifdef DEBUG_BOARD_STATE
BoardState().m_CullEnable = state.m_CullEnable;
#endif
}
//-----------------------------------------------------------------------------
void ApplyAlphaToCoverage( const ShadowState_t& state, int arg )
{
ShaderAPI()->ApplyAlphaToCoverage( state.m_EnableAlphaToCoverage );
#ifdef DEBUG_BOARD_STATE
BoardState().m_EnableAlphaToCoverage = state.m_EnableAlphaToCoverage;
#endif
}
//-----------------------------------------------------------------------------
void ApplyVertexBlendEnable( const ShadowState_t& state, int stage )
{
ShaderAPI()->SetVertexBlendState( state.m_VertexBlendEnable ? -1 : 0 );
#ifdef DEBUG_BOARD_STATE
BoardState().m_VertexBlendEnable = state.m_VertexBlendEnable;
#endif
}
//-----------------------------------------------------------------------------
// Outputs the fog mode string
//-----------------------------------------------------------------------------
#ifdef RECORDING
const char *ShaderFogModeToString( ShaderFogMode_t fogMode )
{
switch( fogMode )
{
case SHADER_FOGMODE_DISABLED:
return "SHADER_FOGMODE_DISABLED";
case SHADER_FOGMODE_OO_OVERBRIGHT:
return "SHADER_FOGMODE_OO_OVERBRIGHT";
case SHADER_FOGMODE_BLACK:
return "SHADER_FOGMODE_BLACK";
case SHADER_FOGMODE_GREY:
return "SHADER_FOGMODE_GREY";
case SHADER_FOGMODE_FOGCOLOR:
return "SHADER_FOGMODE_FOGCOLOR";
case SHADER_FOGMODE_WHITE:
return "SHADER_FOGMODE_WHITE";
case SHADER_FOGMODE_NUMFOGMODES:
return "SHADER_FOGMODE_NUMFOGMODES";
default:
return "ERROR";
}
}
#endif
// Uses GetConfig().overbright and GetSceneFogMode, so
// will have to fix up the state manually when those change.
void ApplyFogMode( const ShadowState_t& state, int arg )
{
#ifdef RECORDING
char buf[1024];
sprintf( buf, "ApplyFogMode( %s )", ShaderFogModeToString( state.m_FogMode ) );
RECORD_DEBUG_STRING( buf );
#endif
ShaderAPI()->ApplyFogMode( state.m_FogMode, state.m_SRGBWriteEnable, state.m_bDisableFogGammaCorrection );
#ifdef DEBUG_BOARD_STATE
BoardState().m_FogMode = state.m_FogMode;
#endif
}
//-----------------------------------------------------------------------------
// Function tables mapping enum to function
//-----------------------------------------------------------------------------
ApplyStateFunc_t s_pRenderFunctionTable[] =
{
ApplyDepthTest,
ApplyZWriteEnable,
ApplyColorWriteEnable,
ApplyAlphaTest,
ApplyFillMode,
ApplyLighting,
ApplySpecularEnable,
ApplySRGBWriteEnable,
ApplyAlphaBlend,
ApplySeparateAlphaBlend,
ApplyCullEnable,
ApplyVertexBlendEnable,
ApplyFogMode,
ApplyActivateFixedFunction,
ApplyTextureEnable, // Enables textures on *all* stages
ApplyDiffuseMaterialSource,
ApplyDisableFogGammaCorrection,
ApplyAlphaToCoverage,
};
ApplyStateFunc_t s_pTextureFunctionTable[] =
{
ApplyTexCoordIndex,
ApplySRGBReadEnable,
ApplyFetch4Enable,
#ifdef DX_TO_GL_ABSTRACTION
ApplyShadowFilterEnable,
#endif
// Fixed function states
ApplyColorTextureStage,
ApplyAlphaTextureStage,
};
//-----------------------------------------------------------------------------
// Creates an entry in the state transition table
//-----------------------------------------------------------------------------
inline void CTransitionTable::AddTransition( RenderStateFunc_t func )
{
int nElem = m_TransitionOps.AddToTail();
TransitionOp_t &op = m_TransitionOps[nElem];
op.m_nInfo.m_bIsTextureCode = false;
op.m_nInfo.m_nOpCode = func;
// Stats
// ++s_pRenderTransitions[ func ];
}
inline void CTransitionTable::AddTextureTransition( TextureStateFunc_t func, int stage )
{
int nElem = m_TransitionOps.AddToTail();
TransitionOp_t &op = m_TransitionOps[nElem];
op.m_nInfo.m_bIsTextureCode = true;
op.m_nInfo.m_nOpCode = TextureOp( func, stage );
// Stats
// ++s_pTextureTransitions[ func ][stage];
}
#define ADD_RENDER_STATE_TRANSITION( _state ) \
if (bForce || (toState.m_ ## _state != fromState.m_ ## _state)) \
{ \
AddTransition( RENDER_STATE_ ## _state ); \
++numOps; \
}
#define ADD_TEXTURE_STAGE_STATE_TRANSITION( _stage, _state )\
if (bForce || (toState.m_TextureStage[_stage].m_ ## _state != fromState.m_TextureStage[_stage].m_ ## _state)) \
{ \
Assert( _stage < MAX_TEXTURE_STAGES ); \
AddTextureTransition( TEXTURE_STATE_ ## _state, _stage ); \
++numOps; \
}
#define ADD_SAMPLER_STATE_TRANSITION( _stage, _state )\
if (bForce || (toState.m_SamplerState[_stage].m_ ## _state != fromState.m_SamplerState[_stage].m_ ## _state)) \
{ \
Assert( _stage < MAX_SAMPLERS ); \
AddTextureTransition( TEXTURE_STATE_ ## _state, _stage ); \
++numOps; \
}
int CTransitionTable::CreateNormalTransitions( const ShadowState_t& fromState, const ShadowState_t& toState, bool bForce )
{
int numOps = 0;
// Special case for alpha blending to eliminate extra transitions
bool blendEnableDifferent = (toState.m_AlphaBlendEnable != fromState.m_AlphaBlendEnable);
bool srcBlendDifferent = toState.m_AlphaBlendEnable && (toState.m_SrcBlend != fromState.m_SrcBlend);
bool destBlendDifferent = toState.m_AlphaBlendEnable && (toState.m_DestBlend != fromState.m_DestBlend);
bool blendOpDifferent = toState.m_AlphaBlendEnable && ( toState.m_BlendOp != fromState.m_BlendOp );
if (bForce || blendOpDifferent || blendEnableDifferent || srcBlendDifferent || destBlendDifferent)
{
AddTransition( RENDER_STATE_AlphaBlend );
++numOps;
}
// Shouldn't have m_SeparateAlphaBlendEnable set unless m_AlphaBlendEnable is also set.
Assert ( toState.m_AlphaBlendEnable || !toState.m_SeparateAlphaBlendEnable );
bool blendSeparateAlphaEnableDifferent = (toState.m_SeparateAlphaBlendEnable != fromState.m_SeparateAlphaBlendEnable);
bool srcBlendAlphaDifferent = toState.m_SeparateAlphaBlendEnable && (toState.m_SrcBlendAlpha != fromState.m_SrcBlendAlpha);
bool destBlendAlphaDifferent = toState.m_SeparateAlphaBlendEnable && (toState.m_DestBlendAlpha != fromState.m_DestBlendAlpha);
bool blendOpAlphaDifferent = toState.m_SeparateAlphaBlendEnable && ( toState.m_BlendOpAlpha != fromState.m_BlendOpAlpha );
if (bForce || blendOpAlphaDifferent || blendSeparateAlphaEnableDifferent || srcBlendAlphaDifferent || destBlendAlphaDifferent)
{
AddTransition( RENDER_STATE_SeparateAlphaBlend );
++numOps;
}
bool bAlphaTestEnableDifferent = (toState.m_AlphaTestEnable != fromState.m_AlphaTestEnable);
bool bAlphaFuncDifferent = toState.m_AlphaTestEnable && (toState.m_AlphaFunc != fromState.m_AlphaFunc);
bool bAlphaRefDifferent = toState.m_AlphaTestEnable && (toState.m_AlphaRef != fromState.m_AlphaRef);
if (bForce || bAlphaTestEnableDifferent || bAlphaFuncDifferent || bAlphaRefDifferent)
{
AddTransition( RENDER_STATE_AlphaTest );
++numOps;
}
bool bDepthTestEnableDifferent = (toState.m_ZEnable != fromState.m_ZEnable);
bool bDepthFuncDifferent = (toState.m_ZEnable != D3DZB_FALSE) && (toState.m_ZFunc != fromState.m_ZFunc);
bool bDepthBiasDifferent = (toState.m_ZBias != fromState.m_ZBias);
if (bForce || bDepthTestEnableDifferent || bDepthFuncDifferent || bDepthBiasDifferent)
{
AddTransition( RENDER_STATE_DepthTest );
++numOps;
}
if ( bForce || (toState.m_UsingFixedFunction && !fromState.m_UsingFixedFunction) )
{
AddTransition( RENDER_STATE_ActivateFixedFunction );
++numOps;
}
if ( bForce || (toState.m_bDisableFogGammaCorrection != fromState.m_bDisableFogGammaCorrection) )
{
AddTransition( RENDER_STATE_DisableFogGammaCorrection );
++numOps;
}
int nStageCount = HardwareConfig()->GetTextureStageCount();
int i;
for ( i = 0; i < nStageCount; ++i )
{
// Special case for texture stage ops to eliminate extra transitions
// NOTE: If we're forcing transitions, then ActivateFixedFunction above will take care of all these transitions
if ( !bForce && toState.m_UsingFixedFunction && fromState.m_UsingFixedFunction )
{
const TextureStageShadowState_t& fromTexture = fromState.m_TextureStage[i];
const TextureStageShadowState_t& toTexture = toState.m_TextureStage[i];
bool fromEnabled = (fromTexture.m_ColorOp != D3DTOP_DISABLE);
bool toEnabled = (toTexture.m_ColorOp != D3DTOP_DISABLE);
if ( fromEnabled || toEnabled )
{
bool opDifferent = (toTexture.m_ColorOp != fromTexture.m_ColorOp);
bool arg1Different = (toTexture.m_ColorArg1 != fromTexture.m_ColorArg1);
bool arg2Different = (toTexture.m_ColorArg2 != fromTexture.m_ColorArg2);
if (opDifferent || arg1Different || arg2Different )
{
AddTextureTransition( TEXTURE_STATE_ColorTextureStage, i );
++numOps;
}
}
fromEnabled = (fromTexture.m_AlphaOp != D3DTOP_DISABLE);
toEnabled = (toTexture.m_AlphaOp != D3DTOP_DISABLE);
if ( fromEnabled || toEnabled )
{
bool opDifferent = (toTexture.m_AlphaOp != fromTexture.m_AlphaOp);
bool arg1Different = (toTexture.m_AlphaArg1 != fromTexture.m_AlphaArg1);
bool arg2Different = (toTexture.m_AlphaArg2 != fromTexture.m_AlphaArg2);
if (opDifferent || arg1Different || arg2Different )
{
AddTextureTransition( TEXTURE_STATE_AlphaTextureStage, i );
++numOps;
}
}
}
ADD_TEXTURE_STAGE_STATE_TRANSITION( i, TexCoordIndex );
}
int nSamplerCount = HardwareConfig()->GetSamplerCount();
for ( int i = 0; i < nSamplerCount; ++i )
{
ADD_SAMPLER_STATE_TRANSITION( i, SRGBReadEnable );
ADD_SAMPLER_STATE_TRANSITION( i, Fetch4Enable );
#ifdef DX_TO_GL_ABSTRACTION
ADD_SAMPLER_STATE_TRANSITION( i, ShadowFilterEnable );
#endif
}
return numOps;
}
void CTransitionTable::CreateTransitionTableEntry( int to, int from )
{
// You added or removed a state to the enums but not to the function table lists!
COMPILE_TIME_ASSERT( sizeof(s_pRenderFunctionTable) == sizeof(ApplyStateFunc_t) * RENDER_STATE_COUNT );
COMPILE_TIME_ASSERT( sizeof(s_pTextureFunctionTable) == sizeof(ApplyStateFunc_t) * TEXTURE_STATE_COUNT );
// If from < 0, that means add *all* transitions into it.
unsigned int firstElem = m_TransitionOps.Count();
unsigned short numOps = 0;
const ShadowState_t& toState = m_ShadowStateList[to];
const ShadowState_t& fromState = (from >= 0) ? m_ShadowStateList[from] : m_ShadowStateList[to];
bool bForce = (from < 0);
ADD_RENDER_STATE_TRANSITION( ZWriteEnable )
ADD_RENDER_STATE_TRANSITION( ColorWriteEnable )
ADD_RENDER_STATE_TRANSITION( FillMode )
ADD_RENDER_STATE_TRANSITION( Lighting )
ADD_RENDER_STATE_TRANSITION( SpecularEnable )
ADD_RENDER_STATE_TRANSITION( SRGBWriteEnable )
ADD_RENDER_STATE_TRANSITION( DiffuseMaterialSource )
// Some code for the non-trivial transitions
numOps += CreateNormalTransitions( fromState, toState, bForce );
// NOTE: From here on down are transitions that depend on dynamic state
// and which can therefore not appear in the state block
ADD_RENDER_STATE_TRANSITION( CullEnable )
ADD_RENDER_STATE_TRANSITION( EnableAlphaToCoverage )
ADD_RENDER_STATE_TRANSITION( VertexBlendEnable )
// NOTE! : Have to do the extra check for changes in m_UsingFixedFunction
// since d3d fog state is different if you are using fixed function vs.
// using a vsh/psh.
// This code is derived from: ADD_RENDER_STATE_TRANSITION( FogMode )
// If ADD_RENDER_STATE_TRANSITION ever changes, this needs to be updated!
// This is another reason to try to have very little fixed function in the dx8/dx9 path.
if( bForce || (toState.m_FogMode != fromState.m_FogMode ) ||
( toState.m_UsingFixedFunction != fromState.m_UsingFixedFunction ) )
{
AddTransition( RENDER_STATE_FogMode );
++numOps;
}
bool bDifferentTexturesEnabled = false;
int nSamplerCount = HardwareConfig()->GetSamplerCount();
for ( int i = 0; i < nSamplerCount; ++i )
{
if ( toState.m_SamplerState[i].m_TextureEnable != fromState.m_SamplerState[i].m_TextureEnable )
{
bDifferentTexturesEnabled = true;
break;
}
}
if ( bForce || bDifferentTexturesEnabled )
{
AddTransition( RENDER_STATE_TextureEnable );
++numOps;
}
// Look for identical transition lists, and use those instead...
TransitionList_t& transition = (from >= 0) ?
m_TransitionTable[to][from] : m_DefaultTransition;
Assert( numOps <= 255 );
transition.m_NumOperations = numOps;
// This condition can happen, and is valid. It occurs when we snapshot
// state but do not generate a transition function for that state
if (numOps == 0)
{
transition.m_FirstOperation = INVALID_TRANSITION_OP;
return;
}
// An optimization to try to early out of the identical transition check
// taking advantage of the fact that the matrix is usually diagonal.
unsigned int nFirstTest = INVALID_TRANSITION_OP;
if (from >= 0)
{
TransitionList_t &diagonalList = m_TransitionTable[from][to];
if ( diagonalList.m_NumOperations == numOps )
{
nFirstTest = diagonalList.m_FirstOperation;
}
}
unsigned int identicalListFirstElem = FindIdenticalTransitionList( firstElem, numOps, nFirstTest );
if (identicalListFirstElem == INVALID_TRANSITION_OP)
{
transition.m_FirstOperation = firstElem;
m_UniqueTransitions.Insert( transition );
Assert( (int)firstElem + (int)numOps < 16777215 );
if( (int)firstElem + (int)numOps >= 16777215 )
{
Warning("**** WARNING: Transition table overflow. Grab Brian\n");
}
}
else
{
// Remove the transitions ops we made; use the duplicate copy
transition.m_FirstOperation = identicalListFirstElem;
m_TransitionOps.RemoveMultiple( firstElem, numOps );
}
}
//-----------------------------------------------------------------------------
// Tests a snapshot to see if it can be used
//-----------------------------------------------------------------------------
#define PERFORM_RENDER_STATE_TRANSITION( _state, _func ) \
::Apply ## _func( _state, 0 );
#define PERFORM_TEXTURE_STAGE_STATE_TRANSITION( _state, _stage, _func ) \
::Apply ## _func( _state, _stage );
#define PERFORM_SAMPLER_STATE_TRANSITION( _state, _stage, _func ) \
::Apply ## _func( _state, _stage );
bool CTransitionTable::TestShadowState( const ShadowState_t& state, const ShadowShaderState_t &shaderState )
{
PERFORM_RENDER_STATE_TRANSITION( state, DepthTest )
PERFORM_RENDER_STATE_TRANSITION( state, ZWriteEnable )
PERFORM_RENDER_STATE_TRANSITION( state, ColorWriteEnable )
PERFORM_RENDER_STATE_TRANSITION( state, AlphaTest )
PERFORM_RENDER_STATE_TRANSITION( state, FillMode )
PERFORM_RENDER_STATE_TRANSITION( state, Lighting )
PERFORM_RENDER_STATE_TRANSITION( state, SpecularEnable )
PERFORM_RENDER_STATE_TRANSITION( state, SRGBWriteEnable )
PERFORM_RENDER_STATE_TRANSITION( state, AlphaBlend )
PERFORM_RENDER_STATE_TRANSITION( state, SeparateAlphaBlend )
PERFORM_RENDER_STATE_TRANSITION( state, CullEnable )
PERFORM_RENDER_STATE_TRANSITION( state, AlphaToCoverage )
PERFORM_RENDER_STATE_TRANSITION( state, VertexBlendEnable )
PERFORM_RENDER_STATE_TRANSITION( state, FogMode )
PERFORM_RENDER_STATE_TRANSITION( state, ActivateFixedFunction )
PERFORM_RENDER_STATE_TRANSITION( state, TextureEnable )
PERFORM_RENDER_STATE_TRANSITION( state, DiffuseMaterialSource )
int i;
int nStageCount = HardwareConfig()->GetTextureStageCount();
for ( i = 0; i < nStageCount; ++i )
{
PERFORM_TEXTURE_STAGE_STATE_TRANSITION( state, i, ColorTextureStage );
PERFORM_TEXTURE_STAGE_STATE_TRANSITION( state, i, AlphaTextureStage );
PERFORM_TEXTURE_STAGE_STATE_TRANSITION( state, i, TexCoordIndex );
}
int nSamplerCount = HardwareConfig()->GetSamplerCount();
for ( i = 0; i < nSamplerCount; ++i )
{
PERFORM_SAMPLER_STATE_TRANSITION( state, i, SRGBReadEnable );
PERFORM_SAMPLER_STATE_TRANSITION( state, i, Fetch4Enable );
#ifdef DX_TO_GL_ABSTRACTION
PERFORM_SAMPLER_STATE_TRANSITION( state, i, ShadowFilterEnable );
#endif
}
// Just make sure we've got a good snapshot
RECORD_COMMAND( DX8_VALIDATE_DEVICE, 0 );
#if !defined( _X360 )
DWORD numPasses;
HRESULT hr = Dx9Device()->ValidateDevice( &numPasses );
bool ok = !FAILED(hr);
#else
bool ok = true;
#endif
// Now set the board state to match the default state
ApplyTransition( m_DefaultTransition, m_DefaultStateSnapshot );
ShaderManager()->SetVertexShader( shaderState.m_VertexShader );
ShaderManager()->SetPixelShader( shaderState.m_PixelShader );
return ok;
}
//-----------------------------------------------------------------------------
// Finds identical transition lists and shares them
//-----------------------------------------------------------------------------
unsigned int CTransitionTable::FindIdenticalTransitionList( unsigned int firstElem,
unsigned short numOps, unsigned int nFirstTest ) const
{
VPROF("CTransitionTable::FindIdenticalTransitionList");
// As it turns out, this works most of the time
if ( nFirstTest != INVALID_TRANSITION_OP )
{
const TransitionOp_t *pCurrOp = &m_TransitionOps[firstElem];
const TransitionOp_t *pTestOp = &m_TransitionOps[nFirstTest];
if ( !memcmp( pCurrOp, pTestOp, numOps * sizeof(TransitionOp_t) ) )
return nFirstTest;
}
// Look for a common list
const TransitionOp_t &op = m_TransitionOps[firstElem];
int nCount = m_UniqueTransitions.Count();
for ( int i = 0; i < nCount; ++i )
{
const TransitionList_t &list = m_UniqueTransitions[i];
// We can early out here because we've sorted the unique transitions
// descending by count
if ( list.m_NumOperations < numOps )
return INVALID_TRANSITION_OP;
// If we don't find a match in the first
int nPotentialMatch;
int nLastTest = list.m_FirstOperation + list.m_NumOperations - numOps;
for ( nPotentialMatch = list.m_FirstOperation; nPotentialMatch <= nLastTest; ++nPotentialMatch )
{
// Find the first match
const TransitionOp_t &testOp = m_TransitionOps[nPotentialMatch];
if ( testOp.m_nBits == op.m_nBits )
break;
}
// No matches found, continue
if ( nPotentialMatch > nLastTest )
continue;
// Ok, found a match of the first op, lets see if they all match
if ( numOps == 1 )
return nPotentialMatch;
const TransitionOp_t *pCurrOp = &m_TransitionOps[firstElem + 1];
const TransitionOp_t *pTestOp = &m_TransitionOps[nPotentialMatch + 1];
if ( !memcmp( pCurrOp, pTestOp, (numOps - 1) * sizeof(TransitionOp_t) ) )
return nPotentialMatch;
}
return INVALID_TRANSITION_OP;
}
//-----------------------------------------------------------------------------
// Create startup snapshot
//-----------------------------------------------------------------------------
void CTransitionTable::TakeDefaultStateSnapshot( )
{
if (m_DefaultStateSnapshot == -1)
{
m_DefaultStateSnapshot = TakeSnapshot();
// This will create a transition which sets *all* shadowed state
CreateTransitionTableEntry( m_DefaultStateSnapshot, -1 );
}
}
//-----------------------------------------------------------------------------
// Applies the transition list
//-----------------------------------------------------------------------------
void CTransitionTable::ApplyTransitionList( int snapshot, int nFirstOp, int nOpCount )
{
VPROF("CTransitionTable::ApplyTransitionList");
// Don't bother if there's nothing to do
if (nOpCount > 0)
{
// Trying to avoid function overhead here
ShadowState_t& shadowState = m_ShadowStateList[snapshot];
TransitionOp_t* pTransitionOp = &m_TransitionOps[nFirstOp];
for (int i = 0; i < nOpCount; ++i )
{
// invoke the transition method
if ( pTransitionOp->m_nInfo.m_bIsTextureCode )
{
TextureStateFunc_t code;
int nStage;
GetTextureOp( pTransitionOp->m_nInfo.m_nOpCode, &code, &nStage );
(*s_pTextureFunctionTable[code])( shadowState, nStage );
}
else
{
(*s_pRenderFunctionTable[pTransitionOp->m_nInfo.m_nOpCode])( shadowState, 0 );
}
++pTransitionOp;
}
}
}
//-----------------------------------------------------------------------------
// Apply startup snapshot
//-----------------------------------------------------------------------------
#ifdef _WIN32
#pragma warning( disable : 4189 )
#endif
void CTransitionTable::ApplyTransition( TransitionList_t& list, int snapshot )
{
VPROF("CTransitionTable::ApplyTransition");
if ( g_pShaderDeviceDx8->IsDeactivated() )
return;
// Transition lists when using state blocks have 2 parts: the first
// is the stateblock part, which is states that are not related to
// dynamic state at all; followed by states that *are* affected by dynamic state
int nFirstOp = list.m_FirstOperation;
int nOpCount = list.m_NumOperations;
ApplyTransitionList( snapshot, nFirstOp, nOpCount );
// Semi-hacky code to override what the transitions are doing
PerformShadowStateOverrides();
// Set the current snapshot id
m_CurrentShadowId = snapshot;
#ifdef DEBUG_BOARD_STATE
// Copy over the board states that aren't explicitly in the transition table
// so the assertion works...
int i;
int nSamplerCount = HardwareConfig()->GetSamplerCount();
for ( i = nSamplerCount; i < MAX_SAMPLERS; ++i )
{
m_BoardState.m_SamplerState[i].m_TextureEnable =
CurrentShadowState()->m_SamplerState[i].m_TextureEnable;
}
int nTextureStageCount = HardwareConfig()->GetTextureStageCount();
for ( i = nTextureStageCount; i < MAX_TEXTURE_STAGES; ++i )
{
memcpy( &m_BoardState.m_TextureStage[i], &CurrentShadowState()->m_TextureStage[i], sizeof(TextureStageShadowState_t) );
}
m_BoardState.m_UsingFixedFunction = CurrentShadowState()->m_UsingFixedFunction;
// State blocks bypass the code that sets the board state
#ifdef _DEBUG
// NOTE: A memcmp here isn't enough since we don't set alpha args in cases where the op is nothing.
// Assert( !memcmp( &m_BoardState, &CurrentShadowState(), sizeof(m_BoardState) ) );
const ShadowState_t &testState1 = *CurrentShadowState();
ShadowState_t testState2 = m_BoardState;
if ( testState1.m_ZEnable == D3DZB_FALSE )
{
testState2.m_ZBias = testState1.m_ZBias;
testState2.m_ZFunc = testState1.m_ZFunc;
}
if ( !testState1.m_AlphaTestEnable )
{
testState2.m_AlphaRef = testState1.m_AlphaRef;
testState2.m_AlphaFunc = testState1.m_AlphaFunc;
}
for( i = 0; i < nTextureStageCount; i++ )
{
if ( !testState1.m_UsingFixedFunction )
{
testState2.m_TextureStage[i].m_ColorOp = testState1.m_TextureStage[i].m_ColorOp;
testState2.m_TextureStage[i].m_ColorArg1 = testState1.m_TextureStage[i].m_ColorArg1;
testState2.m_TextureStage[i].m_ColorArg2 = testState1.m_TextureStage[i].m_ColorArg2;
testState2.m_TextureStage[i].m_AlphaOp = testState1.m_TextureStage[i].m_AlphaOp;
testState2.m_TextureStage[i].m_AlphaArg1 = testState1.m_TextureStage[i].m_AlphaArg1;
testState2.m_TextureStage[i].m_AlphaArg2 = testState1.m_TextureStage[i].m_AlphaArg2;
}
else
{
if ( testState1.m_TextureStage[i].m_ColorOp == D3DTOP_DISABLE )
{
testState2.m_TextureStage[i].m_ColorArg1 = testState1.m_TextureStage[i].m_ColorArg1;
testState2.m_TextureStage[i].m_ColorArg2 = testState1.m_TextureStage[i].m_ColorArg2;
}
if ( testState1.m_TextureStage[i].m_AlphaOp == D3DTOP_DISABLE )
{
testState2.m_TextureStage[i].m_AlphaArg1 = testState1.m_TextureStage[i].m_AlphaArg1;
testState2.m_TextureStage[i].m_AlphaArg2 = testState1.m_TextureStage[i].m_AlphaArg2;
}
}
}
Assert( !memcmp( &testState1, &testState2, sizeof( testState1 ) ) );
#endif
#endif
}
#ifdef _WIN32
#pragma warning( default : 4189 )
#endif
//-----------------------------------------------------------------------------
// Takes a snapshot, hooks it into the material
//-----------------------------------------------------------------------------
StateSnapshot_t CTransitionTable::TakeSnapshot( )
{
// Do any final computation of the shadow state
ShaderShadow()->ComputeAggregateShadowState();
// Get the current snapshot
const ShadowState_t& currentState = ShaderShadow()->GetShadowState();
// Create a new snapshot
ShadowStateId_t shadowStateId = FindShadowState( currentState );
if (shadowStateId == -1)
{
// Create entry in state transition table
shadowStateId = CreateShadowState( currentState );
// Now create new transition entries
for (int to = 0; to < shadowStateId; ++to)
{
CreateTransitionTableEntry( to, shadowStateId );
}
for (int from = 0; from < shadowStateId; ++from)
{
CreateTransitionTableEntry( shadowStateId, from );
}
}
const ShadowShaderState_t& currentShaderState = ShaderShadow()->GetShadowShaderState();
StateSnapshot_t snapshotId = FindStateSnapshot( shadowStateId, currentShaderState );
if (snapshotId == -1)
{
// Create entry in state transition table
snapshotId = CreateStateSnapshot( shadowStateId, currentShaderState );
}
return snapshotId;
}
//-----------------------------------------------------------------------------
// Apply shader state (stuff that doesn't lie in the transition table)
//-----------------------------------------------------------------------------
void CTransitionTable::ApplyShaderState( const ShadowState_t &shadowState, const ShadowShaderState_t &shaderState )
{
VPROF("CTransitionTable::ApplyShaderState");
// Don't bother testing against the current state because there
// could well be dynamic state modifiers affecting this too....
if ( !shadowState.m_UsingFixedFunction )
{
// FIXME: Improve early-binding of vertex shader index
ShaderManager()->SetVertexShader( shaderState.m_VertexShader );
ShaderManager()->SetPixelShader( shaderState.m_PixelShader );
#ifdef DEBUG_BOARD_STATE
BoardShaderState().m_VertexShader = shaderState.m_VertexShader;
BoardShaderState().m_PixelShader = shaderState.m_PixelShader;
BoardShaderState().m_nStaticVshIndex = shaderState.m_nStaticVshIndex;
BoardShaderState().m_nStaticPshIndex = shaderState.m_nStaticPshIndex;
#endif
}
else
{
ShaderManager()->SetVertexShader( INVALID_SHADER );
ShaderManager()->SetPixelShader( INVALID_SHADER );
#if defined( _X360 )
// no fixed function support
Assert( 0 );
#endif
#ifdef DEBUG_BOARD_STATE
BoardShaderState().m_VertexShader = INVALID_SHADER;
BoardShaderState().m_PixelShader = INVALID_SHADER;
BoardShaderState().m_nStaticVshIndex = 0;
BoardShaderState().m_nStaticPshIndex = 0;
#endif
}
}
//-----------------------------------------------------------------------------
// Makes the board state match the snapshot
//-----------------------------------------------------------------------------
void CTransitionTable::UseSnapshot( StateSnapshot_t snapshotId )
{
VPROF("CTransitionTable::UseSnapshot");
ShadowStateId_t id = m_SnapshotList[snapshotId].m_ShadowStateId;
if (m_CurrentSnapshotId != snapshotId)
{
// First apply things that are in the transition table
if ( m_CurrentShadowId != id )
{
TransitionList_t& transition = m_TransitionTable[id][m_CurrentShadowId];
ApplyTransition( transition, id );
}
// NOTE: There is an opportunity here to set non-dynamic state that we don't
// store in the transition list if we ever need it.
m_CurrentSnapshotId = snapshotId;
}
// NOTE: This occurs regardless of whether the snapshot changed because it depends
// on dynamic state (namely, the dynamic vertex + pixel shader index)
// Followed by things that are not
ApplyShaderState( m_ShadowStateList[id], m_SnapshotList[snapshotId].m_ShaderState );
#ifdef _DEBUG
// NOTE: We can't ship with this active because mod makers may well violate this rule
// We don't want no stinking fixed-function on hardware that has vertex and pixel shaders. .
// This could cause a serious perf hit.
if( HardwareConfig()->SupportsVertexAndPixelShaders() )
{
// Assert( !CurrentShadowState().m_UsingFixedFunction );
}
#endif
}
//-----------------------------------------------------------------------------
// Cause the board to match the default state snapshot
//-----------------------------------------------------------------------------
void CTransitionTable::UseDefaultState( )
{
VPROF("CTransitionTable::UseDefaultState");
// Need to blat these out because they are tested during transitions
m_CurrentState.m_AlphaBlendEnable = false;
m_CurrentState.m_SrcBlend = D3DBLEND_ONE;
m_CurrentState.m_DestBlend = D3DBLEND_ZERO;
m_CurrentState.m_BlendOp = D3DBLENDOP_ADD;
SetRenderStateConstMacro( D3DRS_ALPHABLENDENABLE, m_CurrentState.m_AlphaBlendEnable );
SetRenderStateConstMacro( D3DRS_SRCBLEND, m_CurrentState.m_SrcBlend );
SetRenderStateConstMacro( D3DRS_DESTBLEND, m_CurrentState.m_DestBlend );
SetRenderStateConstMacro( D3DRS_BLENDOP, m_CurrentState.m_BlendOp );
m_CurrentState.m_SeparateAlphaBlendEnable = false;
m_CurrentState.m_SrcBlendAlpha = D3DBLEND_ONE;
m_CurrentState.m_DestBlendAlpha = D3DBLEND_ZERO;
m_CurrentState.m_BlendOpAlpha = D3DBLENDOP_ADD;
SetRenderStateConstMacro( D3DRS_SEPARATEALPHABLENDENABLE, m_CurrentState.m_SeparateAlphaBlendEnable );
SetRenderStateConstMacro( D3DRS_SRCBLENDALPHA, m_CurrentState.m_SrcBlendAlpha );
SetRenderStateConstMacro( D3DRS_DESTBLENDALPHA, m_CurrentState.m_DestBlendAlpha );
SetRenderStateConstMacro( D3DRS_BLENDOPALPHA, m_CurrentState.m_BlendOpAlpha );
m_CurrentState.m_ZEnable = D3DZB_TRUE;
m_CurrentState.m_ZFunc = D3DCMP_LESSEQUAL;
m_CurrentState.m_ZBias = SHADER_POLYOFFSET_DISABLE;
SetRenderStateConstMacro( D3DRS_ZENABLE, m_CurrentState.m_ZEnable );
#if defined( _X360 )
//SetRenderStateConstMacro( D3DRS_HIZENABLE, m_CurrentState.m_ZEnable ? D3DHIZ_AUTOMATIC : D3DHIZ_DISABLE );
#endif
SetRenderStateConstMacro( D3DRS_ZFUNC, m_CurrentState.m_ZFunc );
m_CurrentState.m_AlphaTestEnable = false;
m_CurrentState.m_AlphaFunc = D3DCMP_GREATEREQUAL;
m_CurrentState.m_AlphaRef = 0;
SetRenderStateConstMacro( D3DRS_ALPHATESTENABLE, m_CurrentState.m_AlphaTestEnable );
SetRenderStateConstMacro( D3DRS_ALPHAFUNC, m_CurrentState.m_AlphaFunc );
SetRenderStateConstMacro( D3DRS_ALPHAREF, m_CurrentState.m_AlphaRef );
int nTextureStages = ShaderAPI()->GetActualTextureStageCount();
for ( int i = 0; i < nTextureStages; ++i)
{
TextureStage(i).m_ColorOp = D3DTOP_DISABLE;
TextureStage(i).m_ColorArg1 = D3DTA_TEXTURE;
TextureStage(i).m_ColorArg2 = (i == 0) ? D3DTA_DIFFUSE : D3DTA_CURRENT;
TextureStage(i).m_AlphaOp = D3DTOP_DISABLE;
TextureStage(i).m_AlphaArg1 = D3DTA_TEXTURE;
TextureStage(i).m_AlphaArg2 = (i == 0) ? D3DTA_DIFFUSE : D3DTA_CURRENT;
SetTextureStageState( i, D3DTSS_COLOROP, TextureStage(i).m_ColorOp );
SetTextureStageState( i, D3DTSS_COLORARG1, TextureStage(i).m_ColorArg1 );
SetTextureStageState( i, D3DTSS_COLORARG2, TextureStage(i).m_ColorArg2 );
SetTextureStageState( i, D3DTSS_ALPHAOP, TextureStage(i).m_AlphaOp );
SetTextureStageState( i, D3DTSS_ALPHAARG1, TextureStage(i).m_AlphaArg1 );
SetTextureStageState( i, D3DTSS_ALPHAARG2, TextureStage(i).m_AlphaArg2 );
}
int nSamplerCount = ShaderAPI()->GetActualSamplerCount();
for ( int i = 0; i < nSamplerCount; ++i)
{
SetSamplerState( i, D3DSAMP_SRGBTEXTURE, SamplerState(i).m_SRGBReadEnable );
// Set default Fetch4 state on parts which support it
if ( ShaderAPI()->SupportsFetch4() )
{
SetSamplerState( i, ATISAMP_FETCH4, SamplerState(i).m_Fetch4Enable ? ATI_FETCH4_ENABLE : ATI_FETCH4_DISABLE );
}
#ifdef DX_TO_GL_ABSTRACTION
SetSamplerState( i, D3DSAMP_SHADOWFILTER, SamplerState(i).m_ShadowFilterEnable );
#endif
}
// Disable z overrides...
m_CurrentState.m_bOverrideDepthEnable = false;
m_CurrentState.m_bOverrideAlphaWriteEnable = false;
m_CurrentState.m_bOverrideColorWriteEnable = false;
m_CurrentState.m_ForceDepthFuncEquals = false;
m_CurrentState.m_bLinearColorSpaceFrameBufferEnable = false;
ApplyTransition( m_DefaultTransition, m_DefaultStateSnapshot );
ShaderManager()->SetVertexShader( INVALID_SHADER );
ShaderManager()->SetPixelShader( INVALID_SHADER );
m_CurrentSnapshotId = -1;
}
//-----------------------------------------------------------------------------
// Snapshotted state overrides
//-----------------------------------------------------------------------------
void CTransitionTable::ForceDepthFuncEquals( bool bEnable )
{
if( bEnable != m_CurrentState.m_ForceDepthFuncEquals )
{
// Do this so that we can call this from within the rendering code
// See OverrideDepthEnable + PerformShadowStateOverrides for a version
// that isn't expected to be called from within rendering code
if( !ShaderAPI()->IsRenderingMesh() )
{
ShaderAPI()->FlushBufferedPrimitives();
}
m_CurrentState.m_ForceDepthFuncEquals = bEnable;
if( bEnable )
{
SetZFunc( D3DCMP_EQUAL );
}
else
{
if ( CurrentShadowState() )
{
SetZFunc( CurrentShadowState()->m_ZFunc );
}
}
}
}
void CTransitionTable::OverrideDepthEnable( bool bEnable, bool bDepthEnable )
{
if ( bEnable != m_CurrentState.m_bOverrideDepthEnable )
{
ShaderAPI()->FlushBufferedPrimitives();
m_CurrentState.m_bOverrideDepthEnable = bEnable;
m_CurrentState.m_OverrideZWriteEnable = bDepthEnable ? D3DZB_TRUE : D3DZB_FALSE;
if ( m_CurrentState.m_bOverrideDepthEnable )
{
SetZEnable( D3DZB_TRUE );
SetRenderStateConstMacro( D3DRS_ZWRITEENABLE, m_CurrentState.m_OverrideZWriteEnable );
#if defined( _X360 )
//SetRenderStateConstMacro( D3DRS_HIZWRITEENABLE, m_CurrentState.m_OverrideZWriteEnable ? D3DHIZ_AUTOMATIC : D3DHIZ_DISABLE );
#endif
}
else
{
if ( CurrentShadowState() )
{
SetZEnable( CurrentShadowState()->m_ZEnable );
SetRenderStateConstMacro( D3DRS_ZWRITEENABLE, CurrentShadowState()->m_ZWriteEnable );
#if defined( _X360 )
//SetRenderStateConstMacro( D3DRS_HIZWRITEENABLE, CurrentShadowState()->m_ZWriteEnable ? D3DHIZ_AUTOMATIC : D3DHIZ_DISABLE );
#endif
}
}
}
}
void CTransitionTable::OverrideAlphaWriteEnable( bool bOverrideEnable, bool bAlphaWriteEnable )
{
if ( bOverrideEnable != m_CurrentState.m_bOverrideAlphaWriteEnable )
{
ShaderAPI()->FlushBufferedPrimitives();
m_CurrentState.m_bOverrideAlphaWriteEnable = bOverrideEnable;
m_CurrentState.m_bOverriddenAlphaWriteValue = bAlphaWriteEnable;
DWORD dwSetValue = m_CurrentState.m_ColorWriteEnable;
if ( m_CurrentState.m_bOverrideAlphaWriteEnable )
{
if( m_CurrentState.m_bOverriddenAlphaWriteValue )
{
dwSetValue |= D3DCOLORWRITEENABLE_ALPHA;
}
else
{
dwSetValue &= ~D3DCOLORWRITEENABLE_ALPHA;
}
}
else
{
if ( CurrentShadowState() )
{
//probably being paranoid, but only copy the alpha flag from the shadow state
dwSetValue &= ~D3DCOLORWRITEENABLE_ALPHA;
dwSetValue |= CurrentShadowState()->m_ColorWriteEnable & D3DCOLORWRITEENABLE_ALPHA;
}
}
if( dwSetValue != m_CurrentState.m_ColorWriteEnable )
{
m_CurrentState.m_ColorWriteEnable = dwSetValue;
SetRenderState( D3DRS_COLORWRITEENABLE, m_CurrentState.m_ColorWriteEnable );
}
}
}
void CTransitionTable::OverrideColorWriteEnable( bool bOverrideEnable, bool bColorWriteEnable )
{
if ( bOverrideEnable != m_CurrentState.m_bOverrideColorWriteEnable )
{
ShaderAPI()->FlushBufferedPrimitives();
m_CurrentState.m_bOverrideColorWriteEnable = bOverrideEnable;
m_CurrentState.m_bOverriddenColorWriteValue = bColorWriteEnable;
DWORD dwSetValue = m_CurrentState.m_ColorWriteEnable;
if ( m_CurrentState.m_bOverrideColorWriteEnable )
{
if( m_CurrentState.m_bOverriddenColorWriteValue )
{
dwSetValue |= (D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
}
else
{
dwSetValue &= ~(D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
}
}
else
{
if ( CurrentShadowState() )
{
//probably being paranoid, but only copy the alpha flag from the shadow state
dwSetValue &= ~(D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
dwSetValue |= CurrentShadowState()->m_ColorWriteEnable & (D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
}
}
if( dwSetValue != m_CurrentState.m_ColorWriteEnable )
{
m_CurrentState.m_ColorWriteEnable = dwSetValue;
SetRenderState( D3DRS_COLORWRITEENABLE, m_CurrentState.m_ColorWriteEnable );
}
}
}
void CTransitionTable::EnableLinearColorSpaceFrameBuffer( bool bEnable )
{
if ( m_CurrentState.m_bLinearColorSpaceFrameBufferEnable != bEnable && CurrentShadowState() )
{
ShaderAPI()->FlushBufferedPrimitives();
m_CurrentState.m_bLinearColorSpaceFrameBufferEnable = bEnable;
ApplySRGBWriteEnable( *CurrentShadowState() );
}
}
//-----------------------------------------------------------------------------
// Perform state block overrides
//-----------------------------------------------------------------------------
void CTransitionTable::PerformShadowStateOverrides( )
{
VPROF("CTransitionTable::PerformShadowStateOverrides");
// Deal with funky overrides here, because the state blocks can't...
if ( m_CurrentState.m_ForceDepthFuncEquals )
{
SetZFunc( D3DCMP_EQUAL );
}
if ( m_CurrentState.m_bOverrideDepthEnable )
{
SetZEnable( D3DZB_TRUE );
SetRenderStateConstMacro( D3DRS_ZWRITEENABLE, m_CurrentState.m_OverrideZWriteEnable );
#if defined( _X360 )
//SetRenderStateConstMacro( D3DRS_HIZWRITEENABLE, m_CurrentState.m_OverrideZWriteEnable ? D3DHIZ_AUTOMATIC : D3DHIZ_DISABLE );
#endif
}
if ( m_CurrentState.m_bOverrideAlphaWriteEnable )
{
DWORD dwSetValue = m_CurrentState.m_ColorWriteEnable & ~D3DCOLORWRITEENABLE_ALPHA;
dwSetValue |= m_CurrentState.m_bOverriddenAlphaWriteValue ? D3DCOLORWRITEENABLE_ALPHA : 0;
if ( dwSetValue != m_CurrentState.m_ColorWriteEnable )
{
m_CurrentState.m_ColorWriteEnable = dwSetValue;
SetRenderState( D3DRS_COLORWRITEENABLE, m_CurrentState.m_ColorWriteEnable );
}
}
if ( m_CurrentState.m_bOverrideColorWriteEnable )
{
DWORD dwSetValue = m_CurrentState.m_ColorWriteEnable & ~(D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
dwSetValue |= m_CurrentState.m_bOverriddenColorWriteValue ? (D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE) : 0;
if ( dwSetValue != m_CurrentState.m_ColorWriteEnable )
{
m_CurrentState.m_ColorWriteEnable = dwSetValue;
SetRenderState( D3DRS_COLORWRITEENABLE, m_CurrentState.m_ColorWriteEnable );
}
}
}
|