summaryrefslogtreecommitdiff
path: root/materialsystem/cmatlightmaps.cpp
blob: fe2df86f80f1ad43c3fbba48c6c4466006a49065 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================

#include "pch_materialsystem.h"

#define MATSYS_INTERNAL

#include "cmatlightmaps.h"

#include "colorspace.h"
#include "IHardwareConfigInternal.h"

#include "cmaterialsystem.h"

// NOTE: This must be the last file included!!!
#include "tier0/memdbgon.h"
#include "bitmap/float_bm.h"

static ConVar mat_lightmap_pfms( "mat_lightmap_pfms", "0", FCVAR_MATERIAL_SYSTEM_THREAD, "Outputs .pfm files containing lightmap data for each lightmap page when a level exits." ); // Write PFM files for each lightmap page in the game directory when exiting a level 

#define USE_32BIT_LIGHTMAPS_ON_360 //uncomment to use 32bit lightmaps, be sure to keep this in sync with the same #define in stdshaders/lightmappedgeneric_ps2_3_x.h

#ifdef _X360
#define X360_USE_SIMD_LIGHTMAP
#endif

//-----------------------------------------------------------------------------

inline IMaterialInternal* CMatLightmaps::GetCurrentMaterialInternal() const
{
	return GetMaterialSystem()->GetRenderContextInternal()->GetCurrentMaterialInternal();
}

inline void CMatLightmaps::SetCurrentMaterialInternal(IMaterialInternal* pCurrentMaterial)
{
	return GetMaterialSystem()->GetRenderContextInternal()->SetCurrentMaterialInternal( pCurrentMaterial );
}

inline IMaterialInternal *CMatLightmaps::GetMaterialInternal( MaterialHandle_t idx ) const
{
	return GetMaterialSystem()->GetMaterialInternal( idx );
}

inline const IMatRenderContextInternal *CMatLightmaps::GetRenderContextInternal() const
{
	return GetMaterialSystem()->GetRenderContextInternal();
}

inline IMatRenderContextInternal *CMatLightmaps::GetRenderContextInternal()
{
	return GetMaterialSystem()->GetRenderContextInternal();
}

inline const CMaterialDict *CMatLightmaps::GetMaterialDict() const
{
	return GetMaterialSystem()->GetMaterialDict();
}

inline CMaterialDict *CMatLightmaps::GetMaterialDict()
{
	return GetMaterialSystem()->GetMaterialDict();
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
CMatLightmaps::CMatLightmaps()
{
	m_currentWhiteLightmapMaterial = NULL;
	m_pLightmapPages = NULL;
	m_NumLightmapPages = 0;
	m_numSortIDs = 0;
	m_nUpdatingLightmapsStackDepth = 0;
	m_nLockedLightmap = -1;
	m_pLightmapDataPtrArray = NULL;
	m_eLightmapsState = STATE_DEFAULT;
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatLightmaps::Shutdown( )
{
	// Clean up all lightmaps
	CleanupLightmaps();
}

//-----------------------------------------------------------------------------
// Assign enumeration IDs to all materials
//-----------------------------------------------------------------------------
void CMatLightmaps::EnumerateMaterials( void )
{
	// iterate in sorted order
	int id = 0;
	for (MaterialHandle_t i = GetMaterialDict()->FirstMaterial(); i != GetMaterialDict()->InvalidMaterial(); i = GetMaterialDict()->NextMaterial(i) )
	{
		GetMaterialInternal(i)->SetEnumerationID( id );
		++id;
	}
}


//-----------------------------------------------------------------------------
// Gets the maximum lightmap page size...
//-----------------------------------------------------------------------------
int CMatLightmaps::GetMaxLightmapPageWidth() const
{
	// FIXME: It's unclear which we want here.
	// It doesn't drastically increase primitives per DrawIndexedPrimitive
	// call at the moment to increase it, so let's not for now.
	
	// If we're using dynamic textures though, we want bigger that's for sure.
	// The tradeoff here is how much memory we waste if we don't fill the lightmap

	// We need to go to 512x256 textures because that's the only way bumped
	// lighting on displacements can work given the 128x128 allowance..
	int nWidth = 512;
	if ( nWidth > HardwareConfig()->MaxTextureWidth() )
		nWidth = HardwareConfig()->MaxTextureWidth();

	return nWidth;
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int CMatLightmaps::GetMaxLightmapPageHeight() const
{
	int nHeight = 256;

	if ( nHeight > HardwareConfig()->MaxTextureHeight() )
		nHeight = HardwareConfig()->MaxTextureHeight();

	return nHeight;
}


//-----------------------------------------------------------------------------
// Returns the lightmap page size
//-----------------------------------------------------------------------------
void CMatLightmaps::GetLightmapPageSize( int lightmapPageID, int *pWidth, int *pHeight ) const
{
	switch( lightmapPageID )
	{
	default:
 		Assert( lightmapPageID >= 0 && lightmapPageID < GetNumLightmapPages() );
		*pWidth = m_pLightmapPages[lightmapPageID].m_Width;
		*pHeight = m_pLightmapPages[lightmapPageID].m_Height;
		break;

	case MATERIAL_SYSTEM_LIGHTMAP_PAGE_USER_DEFINED:
		*pWidth = *pHeight = 1;
		AssertOnce( !"Can't use CMatLightmaps to get properties of MATERIAL_SYSTEM_LIGHTMAP_PAGE_USER_DEFINED" );
		break;
	
	case MATERIAL_SYSTEM_LIGHTMAP_PAGE_WHITE:
	case MATERIAL_SYSTEM_LIGHTMAP_PAGE_WHITE_BUMP:
		*pWidth = *pHeight = 1;
		break;
	}
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int CMatLightmaps::GetLightmapWidth( int lightmapPageID ) const
{
	switch( lightmapPageID )
	{
	default:
 		Assert( lightmapPageID >= 0 && lightmapPageID < GetNumLightmapPages() );
		return m_pLightmapPages[lightmapPageID].m_Width;

	case MATERIAL_SYSTEM_LIGHTMAP_PAGE_USER_DEFINED:
		AssertOnce( !"Can't use CMatLightmaps to get properties of MATERIAL_SYSTEM_LIGHTMAP_PAGE_USER_DEFINED" );
		return 1;
	
	case MATERIAL_SYSTEM_LIGHTMAP_PAGE_WHITE:
	case MATERIAL_SYSTEM_LIGHTMAP_PAGE_WHITE_BUMP:
		return 1;
	}
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int CMatLightmaps::GetLightmapHeight( int lightmapPageID ) const
{
	switch( lightmapPageID )
	{
	default:
 		Assert( lightmapPageID >= 0 && lightmapPageID < GetNumLightmapPages() );
		return m_pLightmapPages[lightmapPageID].m_Height;

	case MATERIAL_SYSTEM_LIGHTMAP_PAGE_USER_DEFINED:
		AssertOnce( !"Can't use CMatLightmaps to get properties of MATERIAL_SYSTEM_LIGHTMAP_PAGE_USER_DEFINED" );
		return 1;
	
	case MATERIAL_SYSTEM_LIGHTMAP_PAGE_WHITE:
	case MATERIAL_SYSTEM_LIGHTMAP_PAGE_WHITE_BUMP:
		return 1;
	}
}


//-----------------------------------------------------------------------------
// Clean up lightmap pages.
//-----------------------------------------------------------------------------
void CMatLightmaps::CleanupLightmaps()
{
   if ( mat_lightmap_pfms.GetBool())
   {
      // Write PFM files containing lightmap data for this page
      for (int lightmap = 0; lightmap < GetNumLightmapPages(); lightmap++)
      {
         if ((NULL != m_pLightmapDataPtrArray) && (NULL != m_pLightmapDataPtrArray[lightmap]))
         {
            char szPFMFileName[MAX_PATH];

            sprintf(szPFMFileName, "Lightmap-Page-%d.pfm", lightmap);
            m_pLightmapDataPtrArray[lightmap]->WritePFM(szPFMFileName);
         }
      }
   }

   // Remove the lightmap data bitmap representations
   if (m_pLightmapDataPtrArray)
   {
      int i;
      for( i = 0; i < GetNumLightmapPages(); i++ )
      {
         delete m_pLightmapDataPtrArray[i];
      }

      delete [] m_pLightmapDataPtrArray;
      m_pLightmapDataPtrArray = NULL;
   }
   
   // delete old lightmap pages
	if( m_pLightmapPages )
	{
		int i;
		for( i = 0; i < GetNumLightmapPages(); i++ )
		{
			g_pShaderAPI->DeleteTexture( m_LightmapPageTextureHandles[i] );
		}
		delete [] m_pLightmapPages;
		m_pLightmapPages = 0;
	}

	m_NumLightmapPages = 0;
}

//-----------------------------------------------------------------------------
// Resets the lightmap page info for each material
//-----------------------------------------------------------------------------
void CMatLightmaps::ResetMaterialLightmapPageInfo( void )
{
	for (MaterialHandle_t i = GetMaterialDict()->FirstMaterial(); i != GetMaterialDict()->InvalidMaterial(); i = GetMaterialDict()->NextMaterial(i) )
	{
		IMaterialInternal *pMaterial = GetMaterialInternal(i);
		pMaterial->SetMinLightmapPageID( 9999 );
		pMaterial->SetMaxLightmapPageID( -9999 );
		pMaterial->SetNeedsWhiteLightmap( false );
	}
}

//-----------------------------------------------------------------------------
// This is called before any lightmap allocations take place
//-----------------------------------------------------------------------------
void CMatLightmaps::BeginLightmapAllocation()
{	
	// delete old lightmap pages
	CleanupLightmaps();

	m_ImagePackers.RemoveAll();
	int i = m_ImagePackers.AddToTail();
	m_ImagePackers[i].Reset( 0, GetMaxLightmapPageWidth(), GetMaxLightmapPageHeight() );

	SetCurrentMaterialInternal(0);
	m_currentWhiteLightmapMaterial = 0;
	m_numSortIDs = 0;

	// need to set the min and max sorting id number for each material to 
	// a default value that basically means that it hasn't been used yet.
	ResetMaterialLightmapPageInfo();

	EnumerateMaterials();
}


//-----------------------------------------------------------------------------
// Allocates space in the lightmaps; must be called after BeginLightmapAllocation
//-----------------------------------------------------------------------------
int CMatLightmaps::AllocateLightmap( int width, int height, 
		                               int offsetIntoLightmapPage[2],
									   IMaterial *iMaterial )
{
	IMaterialInternal *pMaterial = static_cast<IMaterialInternal *>( iMaterial );
	if ( !pMaterial )
	{
		Warning( "Programming error: CMatRenderContext::AllocateLightmap: NULL material\n" );
		return m_numSortIDs;
	}
	pMaterial = pMaterial->GetRealTimeVersion(); //always work with the real time versions of materials internally
	
	// material change
	int i;
	int nPackCount = m_ImagePackers.Count();
	if ( GetCurrentMaterialInternal() != pMaterial )
	{
		// If this happens, then we need to close out all image packers other than
		// the last one so as to produce as few sort IDs as possible
		for ( i = nPackCount - 1; --i >= 0; )
		{
			// NOTE: We *must* use the order preserving one here so the remaining one
			// is the last lightmap
			m_ImagePackers.Remove( i );
			--nPackCount;
		}

		// If it's not the first material, increment the sort id
		if (GetCurrentMaterialInternal())
		{
			m_ImagePackers[0].IncrementSortId( );
			++m_numSortIDs;
		}

		SetCurrentMaterialInternal(pMaterial);

		// This assertion guarantees we don't see the same material twice in this loop.
		Assert( pMaterial->GetMinLightmapPageID( ) > pMaterial->GetMaxLightmapPageID() );

		// NOTE: We may not use this lightmap page, but we might
		// we won't know for sure until the next material is passed in.
		// So, for now, we're going to forcibly add the current lightmap
		// page to this material so the sort IDs work out correctly.
		GetCurrentMaterialInternal()->SetMinLightmapPageID( GetNumLightmapPages() );
		GetCurrentMaterialInternal()->SetMaxLightmapPageID( GetNumLightmapPages() );
	}

	// Try to add it to any of the current images...
	bool bAdded = false;
	for ( i = 0; i < nPackCount; ++i )
	{
		bAdded = m_ImagePackers[i].AddBlock( width, height, &offsetIntoLightmapPage[0], &offsetIntoLightmapPage[1] );
		if ( bAdded )
			break;
	}

	if ( !bAdded )
	{
		++m_numSortIDs;
		i = m_ImagePackers.AddToTail();
		m_ImagePackers[i].Reset( m_numSortIDs, GetMaxLightmapPageWidth(), GetMaxLightmapPageHeight() );
		++m_NumLightmapPages;
		if ( !m_ImagePackers[i].AddBlock( width, height, &offsetIntoLightmapPage[0], &offsetIntoLightmapPage[1] ) )
		{
			Error( "MaterialSystem_Interface_t::AllocateLightmap: lightmap (%dx%d) too big to fit in page (%dx%d)\n", 
				width, height, GetMaxLightmapPageWidth(), GetMaxLightmapPageHeight() );
		}

		// Add this lightmap to the material...
		GetCurrentMaterialInternal()->SetMaxLightmapPageID( GetNumLightmapPages() );
	}

	return m_ImagePackers[i].GetSortId();
}

// UNDONE: This needs testing, but it appears as though creating these textures managed
// results in huge stalls whenever they are locked for modify.
// That makes sense given the d3d docs, but these have been flagged as managed for quite some time.
#define DYNAMIC_TEXTURES_NO_BACKING 1

void CMatLightmaps::EndLightmapAllocation()
{
	// count the last page that we were on.if it wasn't 
	// and count the last sortID that we were on
	m_NumLightmapPages++; 
	m_numSortIDs++;

	m_firstDynamicLightmap = m_NumLightmapPages;
	// UNDONE: Until we start using the separate dynamic lighting textures don't allocate them
	// NOTE: Enable this if we want to stop locking the base lightmaps and instead only lock update
	// these completely dynamic pages
//	m_NumLightmapPages += COUNT_DYNAMIC_LIGHTMAP_PAGES;
	m_dynamic.Init();

	// Compute the dimensions of the last lightmap 
	int lastLightmapPageWidth, lastLightmapPageHeight;
	int nLastIdx = m_ImagePackers.Count();
	m_ImagePackers[nLastIdx - 1].GetMinimumDimensions( &lastLightmapPageWidth, &lastLightmapPageHeight );
	m_ImagePackers.Purge();

	m_pLightmapPages = new LightmapPageInfo_t[GetNumLightmapPages()];
	Assert( m_pLightmapPages );

   if ( mat_lightmap_pfms.GetBool())
   {
      // This array will be used to write PFM files full of lightmap data
      m_pLightmapDataPtrArray = new FloatBitMap_t*[GetNumLightmapPages()];
   }

	int i;
	m_LightmapPageTextureHandles.EnsureCapacity( GetNumLightmapPages() );
	for ( i = 0; i < GetNumLightmapPages(); i++ )
	{
		// Compute lightmap dimensions
		bool lastStaticLightmap = ( i == (m_firstDynamicLightmap-1));
		m_pLightmapPages[i].m_Width = (unsigned short)(lastStaticLightmap ? lastLightmapPageWidth : GetMaxLightmapPageWidth());
		m_pLightmapPages[i].m_Height = (unsigned short)(lastStaticLightmap ? lastLightmapPageHeight : GetMaxLightmapPageHeight());
		m_pLightmapPages[i].m_Flags = 0;

		AllocateLightmapTexture( i );

        if ( mat_lightmap_pfms.GetBool())
        {
           // Initialize the pointers to lightmap data
           m_pLightmapDataPtrArray[i] = NULL;
        }
	}
}

//-----------------------------------------------------------------------------
// Allocate lightmap textures
//-----------------------------------------------------------------------------
void CMatLightmaps::AllocateLightmapTexture( int lightmap )
{
	bool bUseDynamicTextures = HardwareConfig()->PreferDynamicTextures();

	int flags = bUseDynamicTextures ? TEXTURE_CREATE_DYNAMIC : TEXTURE_CREATE_MANAGED;

	m_LightmapPageTextureHandles.EnsureCount( lightmap + 1 );

	char debugName[256];
	Q_snprintf( debugName, sizeof( debugName ), "[lightmap %d]", lightmap );
	
	ImageFormat imageFormat;
	switch ( HardwareConfig()->GetHDRType() )
	{
	default:
		Assert( 0 );
		// fall through.

	case HDR_TYPE_NONE:
#if !defined( _X360 )
		imageFormat = IMAGE_FORMAT_RGBA8888;
		flags |= TEXTURE_CREATE_SRGB;
#else
		imageFormat = IMAGE_FORMAT_LINEAR_RGBA8888;
#endif
		break;

	case HDR_TYPE_INTEGER:
#if !defined( _X360 )
		imageFormat = IMAGE_FORMAT_RGBA16161616;
#else
#		if ( defined( USE_32BIT_LIGHTMAPS_ON_360 ) )
			imageFormat = IMAGE_FORMAT_LINEAR_RGBA8888;
#		else
			imageFormat = IMAGE_FORMAT_LINEAR_RGBA16161616;
#		endif
#endif
		break;

	case HDR_TYPE_FLOAT:
		imageFormat = IMAGE_FORMAT_RGBA16161616F;
		break;
	}

	switch ( m_eLightmapsState )
	{
	case STATE_DEFAULT:
		// Allow allocations in default state
		{
			m_LightmapPageTextureHandles[lightmap] = g_pShaderAPI->CreateTexture( 
				GetLightmapWidth(lightmap), GetLightmapHeight(lightmap), 1,
				imageFormat, 
				1, 1, flags, debugName, TEXTURE_GROUP_LIGHTMAP );	// don't mipmap lightmaps

			// Load up the texture data
			g_pShaderAPI->ModifyTexture( m_LightmapPageTextureHandles[lightmap] );
			g_pShaderAPI->TexMinFilter( SHADER_TEXFILTERMODE_LINEAR );
			g_pShaderAPI->TexMagFilter( SHADER_TEXFILTERMODE_LINEAR );

			if ( !bUseDynamicTextures )
			{
				g_pShaderAPI->TexSetPriority( 1 );
			}

			// Blat out the lightmap bits
			InitLightmapBits( lightmap );
		}
		break;

	case STATE_RELEASED:
		// Not assigned m_LightmapPageTextureHandles[lightmap];
		DevMsg( "AllocateLightmapTexture(%d) in released lightmap state (STATE_RELEASED), delayed till \"Restore\".\n", lightmap );
		return;

	default:
		// Not assigned m_LightmapPageTextureHandles[lightmap];
		Warning( "AllocateLightmapTexture(%d) in unknown lightmap state (%d), skipped.\n", lightmap, m_eLightmapsState );
		Assert( !"AllocateLightmapTexture(?) in unknown lightmap state (?)" );
		return;
	}
}


int	CMatLightmaps::AllocateWhiteLightmap( IMaterial *iMaterial )
{
	IMaterialInternal *pMaterial = static_cast<IMaterialInternal *>( iMaterial );
	if( !pMaterial )
	{
		Warning( "Programming error: CMatRenderContext::AllocateWhiteLightmap: NULL material\n" );
		return m_numSortIDs;
	}
	pMaterial = pMaterial->GetRealTimeVersion(); //always work with the real time versions of materials internally

	if ( !m_currentWhiteLightmapMaterial || ( m_currentWhiteLightmapMaterial != pMaterial ) )
	{
		if ( !GetCurrentMaterialInternal() && !m_currentWhiteLightmapMaterial )
		{
			// don't increment if this is the very first material (ie. no lightmaps
			// allocated with AllocateLightmap
			// Assert( 0 );
		}
		else
		{
			// material change
			m_numSortIDs++;
#if 0
			char buf[128];
			Q_snprintf( buf, sizeof( buf ), "AllocateWhiteLightmap: m_numSortIDs = %d %s\n", m_numSortIDs, pMaterial->GetName() );
			OutputDebugString( buf );
#endif
		}
//		Warning( "%d material: \"%s\" lightmapPageID: -1\n", m_numSortIDs, pMaterial->GetName() );
		m_currentWhiteLightmapMaterial = pMaterial;
		pMaterial->SetNeedsWhiteLightmap( true );
	}

	return m_numSortIDs;
}

//-----------------------------------------------------------------------------
// Releases/restores lightmap pages
//-----------------------------------------------------------------------------
void CMatLightmaps::ReleaseLightmapPages()
{
	switch ( m_eLightmapsState )
	{
	case STATE_DEFAULT:
		// Allow release in default state only
		break;
	
	default:
		Warning( "ReleaseLightmapPages is expected in STATE_DEFAULT, current state = %d, discarded.\n", m_eLightmapsState );
		Assert( !"ReleaseLightmapPages is expected in STATE_DEFAULT" );
		return;
	}

	for( int i = 0; i < GetNumLightmapPages(); i++ )
	{
		g_pShaderAPI->DeleteTexture( m_LightmapPageTextureHandles[i] );
	}
	
	// We are now in released state
	m_eLightmapsState = STATE_RELEASED;
}

void CMatLightmaps::RestoreLightmapPages()
{
	switch ( m_eLightmapsState )
	{
	case STATE_RELEASED:
		// Allow restore in released state only
		break;

	default:
		Warning( "RestoreLightmapPages is expected in STATE_RELEASED, current state = %d, discarded.\n", m_eLightmapsState );
		Assert( !"RestoreLightmapPages is expected in STATE_RELEASED" );
		return;
	}

	// Switch to default state to allow allocations
	m_eLightmapsState = STATE_DEFAULT;

	for( int i = 0; i < GetNumLightmapPages(); i++ )
	{
		AllocateLightmapTexture( i );
	}
}


//-----------------------------------------------------------------------------
// This initializes the lightmap bits
//-----------------------------------------------------------------------------
void CMatLightmaps::InitLightmapBits( int lightmap )
{
	VPROF_( "CMatLightmaps::InitLightmapBits", 1, VPROF_BUDGETGROUP_DLIGHT_RENDERING, false, 0 );
	int width = GetLightmapWidth(lightmap);
	int height = GetLightmapHeight(lightmap);

	CPixelWriter writer;

	g_pShaderAPI->ModifyTexture( m_LightmapPageTextureHandles[lightmap] );
	if ( !g_pShaderAPI->TexLock( 0, 0, 0, 0, width, height, writer ) )
		return;

	// Debug mode, make em green checkerboard
	if ( writer.IsUsingFloatFormat() )
	{
		for ( int j = 0; j < height; ++j )
		{
			writer.Seek( 0, j );
			for ( int k = 0; k < width; ++k )
			{
#ifndef _DEBUG
				writer.WritePixel( 1.0f, 1.0f, 1.0f );
#else // _DEBUG
				if( ( j + k ) & 1 )
				{
					writer.WritePixelF( 0.0f, 1.0f, 0.0f );
				}
				else
				{
					writer.WritePixelF( 0.0f, 0.0f, 0.0f );
				}
#endif // _DEBUG
			}
		}
	}
	else
	{
		for ( int j = 0; j < height; ++j )
		{
			writer.Seek( 0, j );
			for ( int k = 0; k < width; ++k )
			{
#ifndef _DEBUG
				// note: make this white to find multisample centroid sampling problems.
				//				writer.WritePixel( 255, 255, 255 );
				writer.WritePixel( 0, 0, 0 );
#else // _DEBUG
				if ( ( j + k ) & 1 )
				{
					writer.WritePixel( 0, 255, 0 );
				}
				else
				{
					writer.WritePixel( 0, 0, 0 );
				}
#endif // _DEBUG
			}
		}
	}

	g_pShaderAPI->TexUnlock();
}

bool CMatLightmaps::LockLightmap( int lightmap )
{
//	Warning( "locking lightmap page: %d\n", lightmap );
	VPROF_INCREMENT_COUNTER( "lightmap fullpage texlock", 1 );
	if( m_nLockedLightmap != -1 )
	{
		g_pShaderAPI->TexUnlock();
	}
	g_pShaderAPI->ModifyTexture( m_LightmapPageTextureHandles[lightmap] );
	int pageWidth  = m_pLightmapPages[lightmap].m_Width;
	int pageHeight = m_pLightmapPages[lightmap].m_Height;
	if (!g_pShaderAPI->TexLock( 0, 0, 0, 0,	pageWidth, pageHeight, m_LightmapPixelWriter ))
	{
		Assert( 0 );
		return false;
	}
	m_nLockedLightmap = lightmap;
	return true;
}

Vector4D ConvertLightmapColorToRGBScale( const float *lightmapColor )
{
	Vector4D result;

	float fScale = lightmapColor[0];
	for( int i = 1; i != 3; ++i )
	{
		if( lightmapColor[i] > fScale )
			fScale = lightmapColor[i];
	}

	fScale = ceil( fScale * (255.0f/16.0f) ) * (16.0f/255.0f);
	fScale = min( fScale, 16.0f );

	float fInvScale = 1.0f / fScale;

	for( int i = 0; i != 3; ++i )
	{
		result[i] = lightmapColor[i] * fInvScale;
		result[i] = ceil( result[i] * 255.0f ) * (1.0f/255.0f);
		result[i] = min( result[i], 1.0f );
	}

	fScale /= 16.0f;

	result.w = fScale;

	return result;
}

#ifdef _X360
// SIMD version of above
// input numbers from pSrc are on the domain [0..16]
// output is RGBA 
// ignores contents of w channel of input
// the shader does this: rOut = Rin * Ain * 16.0f 
// where Rin is [0..1], a float computed from a byte value [0..255]
// Ain is therefore the brightest channel (say R) divided by 16 and quantized
// Rin is computed from pSrc->r by dividing by Ain
// this outputs RGBa where RGB are [0..255] and a is the shader's scaling factor (also 0..255)
//
// WARNING - this code appears to be vulnerable to a compiler bug. Be very careful modifying and be
// sure to test
fltx4 ConvertLightmapColorToRGBScale( FLTX4 lightmapColor )
{
	
	static const fltx4 vTwoFiftyFive = {255.0f, 255.0f, 255.0f, 255.0f};
	static const fltx4 FourPoint1s = { 0.1, 0.1, 0.1, 0.1 };
	static const fltx4 vTwoFiftyFiveOverSixteen = {255.0f / 16.0f, 255.0f / 16.0f, 255.0f / 16.0f, 255.0f / 16.0f};
	// static const fltx4 vSixteenOverTwoFiftyFive = { 16.0f / 255.0f, 16.0f / 255.0f, 16.0f / 255.0f, 16.0f / 255.0f };


	// find the highest color value in lightmapColor and replicate it
	fltx4 scale = FindHighestSIMD3( lightmapColor );
	fltx4 minscale = FindLowestSIMD3( lightmapColor );
	fltx4 fl4OutofRange = OrSIMD( CmpGeSIMD( scale, Four_Ones ), CmpLeSIMD( scale, FourPoint1s ) );
	fl4OutofRange = OrSIMD( fl4OutofRange, CmpGtSIMD( minscale, MulSIMD( Four_PointFives, scale ) ) );

	// scale needs to be divided by 16 (because the shader multiplies it by 16)
	// then mapped to 0..255 and quantized. 
	scale = __vrfip(MulSIMD(scale, vTwoFiftyFiveOverSixteen)); // scale = ceil(scale * 255/16)
		
	fltx4 result = MulSIMD(vTwoFiftyFive, lightmapColor); // start the scale cooking on the final result
		
	fltx4 invScale = ReciprocalEstSIMD(scale); // invScale = (16/255)(1/scale). may be +inf
	invScale = MulSIMD(invScale, vTwoFiftyFiveOverSixteen); // take the quantizing factor back out
															// of the inverse scale (one less
															// dependent op if you do it this way)
		
	// scale the input channels
	// compute so the numbers are all 0..255 ints. (if one happens to 
	// be 256 due to numerical error in the reciprocation, the unsigned-saturate
	// store we'll use later on will bake it back down to 255)
	result = MulSIMD(result, invScale);
		
	// now, output --
	// if the input color was nonzero, slip the scale into return value's w
	// component and return. If the input was zero, return zero.

	result = MaskedAssign( 
		fl4OutofRange,
		SetWSIMD( result, scale ),
		SetWSIMD( MulSIMD( lightmapColor, vTwoFiftyFive ), vTwoFiftyFiveOverSixteen ) );
	return result;
}
#endif


// write bumped lightmap update to LDR 8-bit lightmap
void CMatLightmaps::BumpedLightmapBitsToPixelWriter_LDR( float* pFloatImage, float *pFloatImageBump1, float *pFloatImageBump2, 
	float *pFloatImageBump3, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut )
{
	const int nLightmapSize0 = pLightmapSize[0];
	const int nLightmap0WriterSizeBytes = nLightmapSize0 * m_LightmapPixelWriter.GetPixelSize();
	const int nRewindToNextPixel = -( ( nLightmap0WriterSizeBytes * 3 ) - m_LightmapPixelWriter.GetPixelSize() );

	for( int t = 0; t < pLightmapSize[1]; t++ )
	{
		int srcTexelOffset = ( sizeof( Vector4D ) / sizeof( float ) ) * ( 0 + t * nLightmapSize0 );
		m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );

		for( int s = 0; s < nLightmapSize0; 
			s++, m_LightmapPixelWriter.SkipBytes(nRewindToNextPixel),srcTexelOffset += (sizeof(Vector4D)/sizeof(float)))
		{
			unsigned char color[4][3];

			ColorSpace::LinearToBumpedLightmap( &pFloatImage[srcTexelOffset],
				&pFloatImageBump1[srcTexelOffset], &pFloatImageBump2[srcTexelOffset],
				&pFloatImageBump3[srcTexelOffset],
				color[0], color[1], color[2], color[3] );

			unsigned char alpha =  RoundFloatToByte( pFloatImage[srcTexelOffset+3] * 255.0f );
			m_LightmapPixelWriter.WritePixelNoAdvance( color[0][0], color[0][1], color[0][2], alpha );

			m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
			m_LightmapPixelWriter.WritePixelNoAdvance( color[1][0], color[1][1], color[1][2], alpha );

			m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
			m_LightmapPixelWriter.WritePixelNoAdvance( color[2][0], color[2][1], color[2][2], alpha );

			m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
			m_LightmapPixelWriter.WritePixelNoAdvance( color[3][0], color[3][1], color[3][2], alpha );
		}
	}
	if ( pfmOut )
	{
		for( int t = 0; t < pLightmapSize[1]; t++ )
		{
			int srcTexelOffset = ( sizeof( Vector4D ) / sizeof( float ) ) * ( 0 + t * nLightmapSize0 );
			for( int s = 0;  s < nLightmapSize0; s++,srcTexelOffset += (sizeof(Vector4D)/sizeof(float)))
			{
				unsigned char color[4][3];

				ColorSpace::LinearToBumpedLightmap( &pFloatImage[srcTexelOffset],
					&pFloatImageBump1[srcTexelOffset], &pFloatImageBump2[srcTexelOffset],
					&pFloatImageBump3[srcTexelOffset],
					color[0], color[1], color[2], color[3] );

				unsigned char alpha =  RoundFloatToByte( pFloatImage[srcTexelOffset+3] * 255.0f );
				// Write data to the bitmapped represenations so that PFM files can be written
				PixRGBAF pixelData;
				pixelData.Red = color[0][0];                  
				pixelData.Green = color[0][1];                  
				pixelData.Blue = color[0][2];
				pixelData.Alpha = alpha;
				pfmOut->WritePixelRGBAF( pOffsetIntoLightmapPage[0] + s, pOffsetIntoLightmapPage[1] + t, pixelData);
			}
		}

	}
}

// write bumped lightmap update to HDR float lightmap
void CMatLightmaps::BumpedLightmapBitsToPixelWriter_HDRF( float* pFloatImage, float *pFloatImageBump1, float *pFloatImageBump2, 
												 float *pFloatImageBump3, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut )
{
	if ( IsX360() )
	{
		// 360 does not support HDR float mode 
		Assert( 0 );
		return;
	}

	Assert( !pfmOut );		// unsupported in this mode

	const int nLightmapSize0 = pLightmapSize[0];
	const int nLightmap0WriterSizeBytes = nLightmapSize0 * m_LightmapPixelWriter.GetPixelSize();
	const int nRewindToNextPixel = -( ( nLightmap0WriterSizeBytes * 3 ) - m_LightmapPixelWriter.GetPixelSize() );

	for( int t = 0; t < pLightmapSize[1]; t++ )
	{
		int srcTexelOffset = ( sizeof( Vector4D ) / sizeof( float ) ) * ( 0 + t * nLightmapSize0 );
		m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );

		for( int s = 0; 
			s < nLightmapSize0; 
			s++, m_LightmapPixelWriter.SkipBytes(nRewindToNextPixel),srcTexelOffset += (sizeof(Vector4D)/sizeof(float)))
		{
			m_LightmapPixelWriter.WritePixelNoAdvanceF( pFloatImage[srcTexelOffset], pFloatImage[srcTexelOffset+1],
				pFloatImage[srcTexelOffset+2], pFloatImage[srcTexelOffset+3] );

			m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
			m_LightmapPixelWriter.WritePixelNoAdvanceF( pFloatImageBump1[srcTexelOffset], pFloatImageBump1[srcTexelOffset+1],
				pFloatImageBump1[srcTexelOffset+2], pFloatImage[srcTexelOffset+3] );

			m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
			m_LightmapPixelWriter.WritePixelNoAdvanceF( pFloatImageBump2[srcTexelOffset], pFloatImageBump2[srcTexelOffset+1],
				pFloatImageBump2[srcTexelOffset+2], pFloatImage[srcTexelOffset+3] );

			m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
			m_LightmapPixelWriter.WritePixelNoAdvanceF( pFloatImageBump3[srcTexelOffset], pFloatImageBump3[srcTexelOffset+1],
				pFloatImageBump3[srcTexelOffset+2], pFloatImage[srcTexelOffset+3] );
		}
	}
}

#ifdef _X360
#pragma optimize("u", on)
#endif


#ifdef _X360

namespace {
	// pack a pixel into BGRA8888 and return it with the data packed into the w component
FORCEINLINE fltx4 PackPixel_BGRA8888( FLTX4 rgba ) 
{
	// this happens to be in an order such that we can use the handy builtin packing op
	// clamp to 0..255 (coz it might have leaked over)
	static const fltx4 vTwoFiftyFive = {255.0f, 255.0f, 255.0f, 255.0f};

	// the magic number such that when mul-accummulated against rbga,
	// gets us a representation 3.0 + (r)*2^-22 -- puts the bits at
	// the bottom of the float
	static const XMVECTOR   PackScale = { (1.0f / (FLOAT)(1 << 22)), (1.0f / (FLOAT)(1 << 22)), (1.0f / (FLOAT)(1 << 22)), (1.0f / (FLOAT)(1 << 22))}; // 255.0f / (FLOAT)(1 << 22)
	static const XMVECTOR   Three = {3.0f, 3.0f, 3.0f, 3.0f};

	fltx4 N = MinSIMD(vTwoFiftyFive, rgba); 

	N = __vmaddfp(N, PackScale, Three);
	N = __vpkd3d(N, N, VPACK_D3DCOLOR, VPACK_32, 0);  // pack into w word
	return N;
}

// A small store-gather buffer used in the 
// BumpedLightmapBitsToPixelWriter_HDRI_BGRA_X360().
// The store-gather buffers. Hopefully these will live in the L1
// cache, which will make writing to them, then to memory, faster
// than just using __stvewx to write directly into WC memory
// one noncontiguous float at a time. (If there weren't a huge
// compiler bug with __stvewx in the Apr07 XDK, that might not
// be the case.)
struct ALIGN128 CPixelWriterStoreGather
{
	enum {
		kRows = 4,
		kWordsPerRow = 32,
	};

	ALIGN128 uint32 m_data[kRows][kWordsPerRow]; // four rows of bgra data, aligned to 4 cache lines. dwords so memcpy works better.
	int m_wordsGathered;
	int m_bytesBetweenWriterRows; // the number of bytes spacing the maps inside the writer from each other
								// if we weren't gathering, we'd SkipBytes this many between the base map, bump1, etc.

	// write four rows, as SIMD registers, into the buffers
	inline void write( CPixelWriter * RESTRICT pLightmapPixelWriter, FLTX4 row0,  FLTX4 row1,  FLTX4 row2,  FLTX4 row3 ) RESTRICT
	{
		// if full, commit
		Assert(m_wordsGathered <= kWordsPerRow);
		AssertMsg((m_wordsGathered & 3) == 0, "Don't call CPixelWriterStoreGather::write after ::writeJustX"); // single-word writes have misaligned me
		if (m_wordsGathered >= kWordsPerRow)
		{
			commitWhenFull(pLightmapPixelWriter);
		}

		XMStoreVector4A( &m_data[0][m_wordsGathered], row0 );
		XMStoreVector4A( &m_data[1][m_wordsGathered], row1 );
		XMStoreVector4A( &m_data[2][m_wordsGathered], row2 );
		XMStoreVector4A( &m_data[3][m_wordsGathered], row3 );

		m_wordsGathered += 4 ; // four words per simd vec
	}

	// pluck the w component out of each of the rows, and store it into the gather buffer. Don't
	// call the other write function after calling this.
	inline void writeJustW( CPixelWriter * RESTRICT pLightmapPixelWriter, FLTX4 row0,  FLTX4 row1,  FLTX4 row2,  FLTX4 row3 ) RESTRICT
	{
		// if full, commit
		Assert(m_wordsGathered <= kWordsPerRow);
		if (m_wordsGathered >= kWordsPerRow)
		{
			commitWhenFull(pLightmapPixelWriter);
		}

		// for each fltx4, splat out x and then use the __stvewx to store
		// whichever word happens to align with the float pointer through
		// that pointer.

		__stvewx(__vspltw(row0, 3), &m_data[0][m_wordsGathered], 0 );
		__stvewx(__vspltw(row1, 3), &m_data[1][m_wordsGathered], 0 );
		__stvewx(__vspltw(row2, 3), &m_data[2][m_wordsGathered], 0 );
		__stvewx(__vspltw(row3, 3), &m_data[3][m_wordsGathered], 0 );

		m_wordsGathered += 1 ; // only stored one word
	}

	// Commit my buffers to the pixelwriter's memory, and advance its
	// pointer.
	void commit(CPixelWriter * RESTRICT pLightmapPixelWriter) RESTRICT
	{
		if (m_wordsGathered > 0)
		{
			unsigned char* RESTRICT pWriteInto = pLightmapPixelWriter->GetCurrentPixel();
			// we have to use memcpy because we're writing to non-cacheable memory,
			// but we can't even assume that the addresses we're writing to are
			// vector-aligned.
#ifdef memcpy // if someone's overriden the intrinsic, complain
#pragma error("You have overridden memcpy(), which is an XBOX360 intrinsic. This function will not behave optimally.")
#endif

			memcpy(pWriteInto, m_data[0], m_wordsGathered * sizeof(uint32));
			pWriteInto += m_bytesBetweenWriterRows;
			memcpy(pWriteInto, m_data[1], m_wordsGathered * sizeof(uint32));
			pWriteInto += m_bytesBetweenWriterRows;
			memcpy(pWriteInto, m_data[2], m_wordsGathered * sizeof(uint32));
			pWriteInto += m_bytesBetweenWriterRows;
			memcpy(pWriteInto, m_data[3], m_wordsGathered * sizeof(uint32));

			pLightmapPixelWriter->SkipBytes(m_wordsGathered * sizeof(uint32));
			m_wordsGathered = 0;
		}
	}

	// like commit, but the version we use when we know we're full.
	// Takes advantage of better compile-time generation for 
	// memcpy.
	void commitWhenFull(CPixelWriter * RESTRICT pLightmapPixelWriter) RESTRICT
	{
		unsigned char* RESTRICT pWriteInto = pLightmapPixelWriter->GetCurrentPixel();
		// we have to use memcpy because we're writing to non-cacheable memory,
		// but we can't even assume that the addresses we're writing to are
		// vector-aligned.
#ifdef memcpy // if someone's overriden the intrinsic, complain
#pragma error("You have overridden memcpy(), which is an XBOX360 intrinsic. This function will not behave optimally.")
#endif

		// if we're full, use compile-time known version of 
		// mempcy to take advantage of its ability to generate
		// inline code. In fact, use the dword-aligned
		// version so that we use the 64-bit writing funcs.
		Assert( m_wordsGathered == kWordsPerRow );
		COMPILE_TIME_ASSERT((kWordsPerRow & 3) == 0); // the number of words per row has to be a multiple of four
		
		memcpy(pWriteInto, reinterpret_cast<uint64* RESTRICT>(m_data[0]), kWordsPerRow * sizeof(uint32));
		pWriteInto += m_bytesBetweenWriterRows;
		memcpy(pWriteInto, reinterpret_cast<uint64* RESTRICT>(m_data[1]), kWordsPerRow * sizeof(uint32));
		pWriteInto += m_bytesBetweenWriterRows;
		memcpy(pWriteInto, reinterpret_cast<uint64* RESTRICT>(m_data[2]), kWordsPerRow * sizeof(uint32));
		pWriteInto += m_bytesBetweenWriterRows;
		memcpy(pWriteInto, reinterpret_cast<uint64* RESTRICT>(m_data[3]), kWordsPerRow * sizeof(uint32));
		
		pLightmapPixelWriter->SkipBytes(m_wordsGathered * sizeof(uint32));
		m_wordsGathered = 0;
	}

	// parameter: space between bump pages in the pixelwriter
	CPixelWriterStoreGather(int writerSizeBytes) : m_wordsGathered(0), m_bytesBetweenWriterRows(writerSizeBytes) {};

};
}


// this is a function for specifically writing bumped BGRA lightmaps -- in order for it
// to be properly scheduled, I needed to break out the inline functions. Also,
// to make the write-combined memory more efficient (and work around a bug in the
// April 2007 XDK), we need to store-gather our writes on the cache before blasting
// them out to write-combined memory. We can't simply write from the SIMD registers
// into the pixelwriter's data, because the difference between the output rows,
// eg nLightmap0WriterSizeBytes[0], might not be a multiple of 16. Unaligned stores
// to non-cacheable memory cause an alignment exception.
static void BumpedLightmapBitsToPixelWriter_HDRI_BGRA_X360( float* RESTRICT pFloatImage, float * RESTRICT pFloatImageBump1, float * RESTRICT pFloatImageBump2, 
													  float * RESTRICT pFloatImageBump3, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut,
													  CPixelWriter * RESTRICT m_LightmapPixelWriter)
{
	AssertMsg(m_LightmapPixelWriter->GetPixelSize() == 4, "BGRA format is no longer four bytes long? This is unsupported on 360, and probably immoral as well.");
	const int nLightmap0WriterSizeBytes = pLightmapSize[0] * 4 /*m_LightmapPixelWriter->GetPixelSize()*/;
	// const int nRewindToNextPixel = -( ( nLightmap0WriterSizeBytes * 3 ) - 4 );

	// assert that 1 * 4 = 4 
	COMPILE_TIME_ASSERT(sizeof( Vector4D ) == sizeof(float) * 4); 

	AssertMsg(!pfmOut, "Runtime conversion of lightmaps to files is no longer supported on 360.\n");

	
	// The store-gather buffers. Hopefully these will live in the L1
	// cache, which will make writing to them, then to memory, faster
	// than just using __stvewx to write directly into WC memory
	// one noncontiguous float at a time. (If there weren't a huge
	// compiler bug with __stvewx in the Apr07 XDK, that might not
	// be the case.)
	CPixelWriterStoreGather storeGather(nLightmap0WriterSizeBytes);

	for( int t = 0; t < pLightmapSize[1]; t++ )
	{
#define	FOUR (sizeof( Vector4D ) / sizeof( float ))  //  make explicit when we're incrementing by length of a 4dvec
		int srcTexelOffset = ( FOUR ) * ( 0 + t * pLightmapSize[0] );
		m_LightmapPixelWriter->Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );

		// Our code works best when we can process luxels in groups of four. So,
		// figure out how many four-luxel groups we can process,
		// then do them in groups, then process the remainder.
		unsigned int groupsOfFourLimit = (((unsigned int)pLightmapSize[0]) & ~3);
		
		// we want to hang on to this index when we're done with groups so we can do the remainder.
		unsigned int s; // counts the number of luxels processed
		for( s = 0; 
			s < groupsOfFourLimit; 
			s += 4, srcTexelOffset += 4 * ( FOUR ))
		{				
			static const fltx4 vSixteen = {16.0f, 16.0f, 16.0f, 16.0f};
			// the store-gather simds
			fltx4 outBaseMap = Four_Zeros, outBump1 = Four_Zeros, outBump2 = Four_Zeros, outBump3 = Four_Zeros;
			// we'll read four at a time
			fltx4 vFloatImage[4], vFloatImageBump1[4], vFloatImageBump2[4], vFloatImageBump3[4];


			// stripe these loads to cause less ERAT thrashing
			vFloatImage[0]	  = LoadUnalignedSIMD(pFloatImage	   + srcTexelOffset );
			vFloatImage[1]	  = LoadUnalignedSIMD(pFloatImage	   + srcTexelOffset + 4 );
			vFloatImage[2]	  = LoadUnalignedSIMD(pFloatImage	   + srcTexelOffset + 8 );
			vFloatImage[3]	  = LoadUnalignedSIMD(pFloatImage	   + srcTexelOffset + 12 );

			vFloatImageBump1[0] = LoadUnalignedSIMD(pFloatImageBump1 + srcTexelOffset );
			vFloatImageBump1[1] = LoadUnalignedSIMD(pFloatImageBump1 + srcTexelOffset + 4 );
			vFloatImageBump1[2] = LoadUnalignedSIMD(pFloatImageBump1 + srcTexelOffset + 8 );
			vFloatImageBump1[3] = LoadUnalignedSIMD(pFloatImageBump1 + srcTexelOffset + 12 );

			vFloatImageBump2[0] = LoadUnalignedSIMD(pFloatImageBump2 + srcTexelOffset );
			vFloatImageBump2[1] = LoadUnalignedSIMD(pFloatImageBump2 + srcTexelOffset + 4 );
			vFloatImageBump2[2] = LoadUnalignedSIMD(pFloatImageBump2 + srcTexelOffset + 8 );
			vFloatImageBump2[3] = LoadUnalignedSIMD(pFloatImageBump2 + srcTexelOffset + 12 );

			vFloatImageBump3[0] = LoadUnalignedSIMD(pFloatImageBump3 + srcTexelOffset );
			vFloatImageBump3[1] = LoadUnalignedSIMD(pFloatImageBump3 + srcTexelOffset + 4 );
			vFloatImageBump3[2] = LoadUnalignedSIMD(pFloatImageBump3 + srcTexelOffset + 8 );
			vFloatImageBump3[3] = LoadUnalignedSIMD(pFloatImageBump3 + srcTexelOffset + 12 );

			// perform an arcane averaging operation upon the bump map values
			// (todo: make this not an inline so it will schedule better -- inlining is 
			//  done by the linker, which is too late for operation scheduling)
			ColorSpace::LinearToBumpedLightmap( vFloatImage[0],	vFloatImageBump1[0],
												vFloatImageBump2[0], vFloatImageBump3[0],
												// transform "in place":
												vFloatImage[0], vFloatImageBump1[0], 
												vFloatImageBump2[0], vFloatImageBump3[0] );
			ColorSpace::LinearToBumpedLightmap( vFloatImage[1],	vFloatImageBump1[1],
												vFloatImageBump2[1], vFloatImageBump3[1],
												// transform "in place":
												vFloatImage[1], vFloatImageBump1[1], 
												vFloatImageBump2[1], vFloatImageBump3[1] );
			ColorSpace::LinearToBumpedLightmap( vFloatImage[2],	vFloatImageBump1[2],
												vFloatImageBump2[2], vFloatImageBump3[2],
												// transform "in place":
												vFloatImage[2], vFloatImageBump1[2], 
												vFloatImageBump2[2], vFloatImageBump3[2] );
			ColorSpace::LinearToBumpedLightmap( vFloatImage[3],	vFloatImageBump1[3],
												vFloatImageBump2[3], vFloatImageBump3[3],
												// transform "in place":
												vFloatImage[3], vFloatImageBump1[3], 
												vFloatImageBump2[3], vFloatImageBump3[3] );
	

			// convert each color to RGB scaled.
			// DO NOT! make this into a for loop. The (April07 XDK) compiler
			// in fact DOES NOT unroll them, and will perform very naive
			// scheduling if you try. 

			// clamp to 0..16 float
			vFloatImage[0]		= MinSIMD(vFloatImage[0], vSixteen);
			vFloatImageBump1[0] = MinSIMD(vFloatImageBump1[0], vSixteen);
			vFloatImageBump2[0] = MinSIMD(vFloatImageBump2[0], vSixteen);
			vFloatImageBump3[0] = MinSIMD(vFloatImageBump3[0], vSixteen);

			vFloatImage[1]		= MinSIMD(vFloatImage[1], vSixteen);
			vFloatImageBump1[1] = MinSIMD(vFloatImageBump1[1], vSixteen);
			vFloatImageBump2[1] = MinSIMD(vFloatImageBump2[1], vSixteen);
			vFloatImageBump3[1] = MinSIMD(vFloatImageBump3[1], vSixteen);

			vFloatImage[2]		= MinSIMD(vFloatImage[2], vSixteen);
			vFloatImageBump1[2] = MinSIMD(vFloatImageBump1[2], vSixteen);
			vFloatImageBump2[2] = MinSIMD(vFloatImageBump2[2], vSixteen);
			vFloatImageBump3[2] = MinSIMD(vFloatImageBump3[2], vSixteen);

			vFloatImage[3]		= MinSIMD(vFloatImage[3], vSixteen);
			vFloatImageBump1[3] = MinSIMD(vFloatImageBump1[3], vSixteen);
			vFloatImageBump2[3] = MinSIMD(vFloatImageBump2[3], vSixteen);
			vFloatImageBump3[3] = MinSIMD(vFloatImageBump3[3], vSixteen);


			// compute the scaling factor, place it in w, and 
			// scale the rest by it. Obliterates whatever was
			// already in alpha.
			// This code is why it is important to not use a for
			// loop: you need to let the compiler keep the value
			// on registers (which it can't do if you use a
			// variable indexed array) and interleave the
			// inlined instructions.

			vFloatImage[0]		= PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImage[0]) );
			vFloatImageBump1[0] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump1[0]) );
			vFloatImageBump2[0] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump2[0]) );
			vFloatImageBump3[0] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump3[0]) );

			vFloatImage[1]		= PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImage[1]) );
			vFloatImageBump1[1] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump1[1]) );
			vFloatImageBump2[1] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump2[1]) );
			vFloatImageBump3[1] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump3[1]) );

			vFloatImage[2]		= PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImage[2]) );
			vFloatImageBump1[2] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump1[2]) );
			vFloatImageBump2[2] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump2[2]) );
			vFloatImageBump3[2] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump3[2]) );

			vFloatImage[3]		= PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImage[3]) );
			vFloatImageBump1[3] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump1[3]) );
			vFloatImageBump2[3] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump2[3]) );
			vFloatImageBump3[3] = PackPixel_BGRA8888( ConvertLightmapColorToRGBScale(vFloatImageBump3[3]) );

			// Each of the registers above contains one RGBA 32-bit struct
			// in their w word. So, combine them such that each of the assignees
			// below contains four RGBAs, in xyzw order (big-endian).

			outBaseMap = __vrlimi(outBaseMap, vFloatImage[0], 8, 3 ); // insert into x
			outBump1 =	 __vrlimi(outBump1, vFloatImageBump1[0], 8, 3 ); // insert into x
			outBump2 =	 __vrlimi(outBump2, vFloatImageBump2[0], 8, 3 ); // insert into x
			outBump3 =	 __vrlimi(outBump3, vFloatImageBump3[0], 8, 3 ); // insert into x

			outBaseMap = __vrlimi(outBaseMap, vFloatImage[1], 4, 2 ); // insert into y
			outBump1 =	 __vrlimi(outBump1, vFloatImageBump1[1], 4, 2 ); // insert into y
			outBump2 =	 __vrlimi(outBump2, vFloatImageBump2[1], 4, 2 ); // insert into y
			outBump3 =	 __vrlimi(outBump3, vFloatImageBump3[1], 4, 2 ); // insert into y

			outBaseMap = __vrlimi(outBaseMap, vFloatImage[2], 2, 1 ); // insert into z
			outBump1 =	 __vrlimi(outBump1, vFloatImageBump1[2], 2, 1 ); // insert into z
			outBump2 =	 __vrlimi(outBump2, vFloatImageBump2[2], 2, 1 ); // insert into z
			outBump3 =	 __vrlimi(outBump3, vFloatImageBump3[2], 2, 1 ); // insert into z

			outBaseMap = __vrlimi(outBaseMap, vFloatImage[3], 1, 0 ); // insert into w
			outBump1 =	 __vrlimi(outBump1, vFloatImageBump1[3], 1, 0 ); // insert into w
			outBump2 =	 __vrlimi(outBump2, vFloatImageBump2[3], 1, 0 ); // insert into w
			outBump3 =	 __vrlimi(outBump3, vFloatImageBump3[3], 1, 0 ); // insert into w

			// push the data through the store-gather buffer.
			storeGather.write(m_LightmapPixelWriter, outBaseMap, outBump1, outBump2, outBump3);

		}

		// Once here, make sure we've committed any leftover changes, then process
		// the remainders singly.
		storeGather.commit(m_LightmapPixelWriter);

		for( ;  // s is where it should be from the loop above
			s < (unsigned int) pLightmapSize[0]; 
			s++, 
				// m_LightmapPixelWriter->SkipBytes(nRewindToNextPixel), // now handled by store-gather
				srcTexelOffset += ( FOUR ))
		{				

			static const fltx4 vSixteen = {16.0f, 16.0f, 16.0f, 16.0f};
			fltx4 vColor[4];
			fltx4 vFloatImage = LoadUnalignedSIMD(&pFloatImage[srcTexelOffset]);
			fltx4 vFloatImageBump1 = LoadUnalignedSIMD(&pFloatImageBump1[srcTexelOffset]);
			fltx4 vFloatImageBump2 = LoadUnalignedSIMD(&pFloatImageBump2[srcTexelOffset]);
			fltx4 vFloatImageBump3 = LoadUnalignedSIMD(&pFloatImageBump3[srcTexelOffset]);

			// perform an arcane averaging operation upon the bump map values
			ColorSpace::LinearToBumpedLightmap( vFloatImage,
				vFloatImageBump1, vFloatImageBump2,
				vFloatImageBump3,
				vColor[0], vColor[1], vColor[2], vColor[3] );		

			// convert each color to RGB scaled.
			// DO NOT! make this into a for loop. The (April07 XDK) compiler
			// in fact DOES NOT unroll them, and will perform very naive
			// scheduling if you try. 

			// clamp to 0..16 float
			vColor[0] = MinSIMD(vColor[0], vSixteen);
			vColor[1] = MinSIMD(vColor[1], vSixteen);
			vColor[2] = MinSIMD(vColor[2], vSixteen);
			vColor[3] = MinSIMD(vColor[3], vSixteen);

			// compute the scaling factor, place it in w, and 
			// scale the rest by it. Obliterates whatever was
			// already in alpha.
			// This code is why it is important to not use a for
			// loop: you need to let the compiler interleave the
			// inlined instructions.
			vColor[0] = ConvertLightmapColorToRGBScale( vColor[0] );
			vColor[1] = ConvertLightmapColorToRGBScale( vColor[1] );
			vColor[2] = ConvertLightmapColorToRGBScale( vColor[2] );
			vColor[3] = ConvertLightmapColorToRGBScale( vColor[3] );


#ifdef X360_DOUBLECHECK_LIGHTMAPS
			unsigned short color[4][4];

			ColorSpace::LinearToBumpedLightmap( &pFloatImage[srcTexelOffset],
				&pFloatImageBump1[srcTexelOffset], &pFloatImageBump2[srcTexelOffset],
				&pFloatImageBump3[srcTexelOffset],
				color[0], color[1], color[2], color[3] );
			unsigned short alpha = ColorSpace::LinearToUnsignedShort( pFloatImage[srcTexelOffset+3], 16 );
			color[0][3] = color[1][3] = color[2][3] = color[3][3] = alpha;

			if( IsX360() )
			{
				for( int i = 0; i != 4; ++i )
				{
					Vector4D vRGBScale;

					vRGBScale.x = color[i][0] * (16.0f / 65535.0f);
					vRGBScale.y = color[i][1] * (16.0f / 65535.0f);
					vRGBScale.z = color[i][2] * (16.0f / 65535.0f);
					vRGBScale = ConvertLightmapColorToRGBScale( &vRGBScale.x );
					color[i][0] = RoundFloatToByte( vRGBScale.x * 255.0f );
					color[i][1] = RoundFloatToByte( vRGBScale.y * 255.0f );
					color[i][2] = RoundFloatToByte( vRGBScale.z * 255.0f );
					color[i][3] = RoundFloatToByte( vRGBScale.w * 255.0f );
				}						
			}

			/*
			for (int ii = 0; ii < 4; ++ii)
			{
				uint32 pack = (PackPixel_BGRA8888( vColor[ii] ).u[3]);
				if (color[ii][3] != 0)
				Assert(	color[ii][0] == (pack & 0xFF0000) >> 16	&& 
						color[ii][1] == (pack & 0xFF00) >> 8		&& 
						color[ii][2] == (pack & 0xFF)				&& 
						color[ii][3] == (pack & 0xFF000000) >> 24 );
			}
			*/

#endif


				vColor[0] = PackPixel_BGRA8888( vColor[0] );
				vColor[1] = PackPixel_BGRA8888( vColor[1] );
				vColor[2] = PackPixel_BGRA8888( vColor[2] );
				vColor[3] = PackPixel_BGRA8888( vColor[3] );

				storeGather.writeJustW(m_LightmapPixelWriter, vColor[0], vColor[1], vColor[2], vColor[3] );

				/* // here is the old way of writing pixels:
				// now we store-gather this
				m_LightmapPixelWriter->WritePixelNoAdvance_BGRA8888( vColor[0] );
				Assert(*reinterpret_cast<unsigned int *>(m_LightmapPixelWriter->GetCurrentPixel()) == PackPixel_BGRA8888( vColor[0] ).u[3] );
				void * RESTRICT pBits = m_LightmapPixelWriter->SkipBytes( nLightmap0WriterSizeBytes );
				m_LightmapPixelWriter->WritePixelNoAdvance_BGRA8888( vColor[1], pBits );
				Assert(*reinterpret_cast<unsigned int *>(m_LightmapPixelWriter->GetCurrentPixel()) == PackPixel_BGRA8888( vColor[1] ).u[3] );
				pBits = m_LightmapPixelWriter->SkipBytes( nLightmap0WriterSizeBytes );
				m_LightmapPixelWriter->WritePixelNoAdvance_BGRA8888( vColor[2], pBits );
				Assert(*reinterpret_cast<unsigned int *>(m_LightmapPixelWriter->GetCurrentPixel()) == PackPixel_BGRA8888( vColor[2] ).u[3] );
				pBits = m_LightmapPixelWriter->SkipBytes( nLightmap0WriterSizeBytes );
				m_LightmapPixelWriter->WritePixelNoAdvance_BGRA8888( vColor[3], pBits );
				Assert(*reinterpret_cast<unsigned int *>(m_LightmapPixelWriter->GetCurrentPixel()) == PackPixel_BGRA8888( vColor[3] ).u[3] );

				m_LightmapPixelWriter->SkipBytes(nRewindToNextPixel);
				*/
		}

		storeGather.commit(m_LightmapPixelWriter);

	}
}

#endif // _X360

// write bumped lightmap update to HDR integer lightmap
void CMatLightmaps::BumpedLightmapBitsToPixelWriter_HDRI( float* RESTRICT pFloatImage, float * RESTRICT pFloatImageBump1, float * RESTRICT pFloatImageBump2, 
												 float * RESTRICT pFloatImageBump3, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut ) RESTRICT
{
	const int nLightmapSize0 = pLightmapSize[0];
	const int nLightmap0WriterSizeBytes = nLightmapSize0 * m_LightmapPixelWriter.GetPixelSize();
	const int nRewindToNextPixel = -( ( nLightmap0WriterSizeBytes * 3 ) - m_LightmapPixelWriter.GetPixelSize() );

	if( m_LightmapPixelWriter.IsUsingFloatFormat() )
	{
		AssertMsg(!IsX360(), "Tried to use a floating-point pixel format for lightmaps on 360, which is not supported.");
		if (!IsX360())
		{
			for( int t = 0; t < pLightmapSize[1]; t++ )
			{
				int srcTexelOffset = ( sizeof( Vector4D ) / sizeof( float ) ) * ( 0 + t * nLightmapSize0 );
				m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );

				for( int s = 0; 
					s < nLightmapSize0; 
					s++, m_LightmapPixelWriter.SkipBytes(nRewindToNextPixel),srcTexelOffset += (sizeof(Vector4D)/sizeof(float)))
				{
					unsigned short color[4][4];

					ColorSpace::LinearToBumpedLightmap( &pFloatImage[srcTexelOffset],
						&pFloatImageBump1[srcTexelOffset], &pFloatImageBump2[srcTexelOffset],
						&pFloatImageBump3[srcTexelOffset],
						color[0], color[1], color[2], color[3] );
					float alpha = pFloatImage[srcTexelOffset+3];
					Assert( alpha >= 0.0f && alpha <= 1.0f );
					color[0][3] = color[1][3] = color[2][3] = color[3][3] = alpha;

					float toFloat = ( 1.0f / ( float )( 1 << 16 ) );

					/* // This code is now a can't-happen, because we do not allow float formats on 360.
#if ( defined( USE_32BIT_LIGHTMAPS_ON_360 ) )
					if( IsX360() )
					{
						for( int i = 0; i != 4; ++i )
						{
							Vector4D vRGBScale;

							vRGBScale.x = color[i][0] * (16.0f / 65535.0f);
							vRGBScale.y = color[i][1] * (16.0f / 65535.0f);
							vRGBScale.z = color[i][2] * (16.0f / 65535.0f);
							vRGBScale = ConvertLightmapColorToRGBScale( &vRGBScale.x );
							color[i][0] = RoundFloatToByte( vRGBScale.x * 255.0f );
							color[i][1] = RoundFloatToByte( vRGBScale.y * 255.0f );
							color[i][2] = RoundFloatToByte( vRGBScale.z * 255.0f );
							color[i][3] = RoundFloatToByte( vRGBScale.w * 255.0f );
						}

						toFloat = ( 1.0f / ( float )( 1 << 8 ) );
					}
#endif
					*/

					m_LightmapPixelWriter.WritePixelNoAdvanceF( toFloat * color[0][0], toFloat * color[0][1], toFloat * color[0][2], toFloat * color[0][3] );

					m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
					m_LightmapPixelWriter.WritePixelNoAdvanceF( toFloat * color[1][0], toFloat * color[1][1], toFloat * color[1][2], toFloat * color[1][3] );

					m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
					m_LightmapPixelWriter.WritePixelNoAdvanceF( toFloat * color[2][0], toFloat * color[2][1], toFloat * color[2][2], toFloat * color[2][3] );

					m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
					m_LightmapPixelWriter.WritePixelNoAdvanceF( toFloat * color[3][0], toFloat * color[3][1], toFloat * color[3][2], toFloat * color[3][3] );
				}
			}
		}
	}
	else
	{
#ifndef X360_USE_SIMD_LIGHTMAP
		for( int t = 0; t < pLightmapSize[1]; t++ )
		{
			int srcTexelOffset = ( sizeof( Vector4D ) / sizeof( float ) ) * ( 0 + t * nLightmapSize0 );
			m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );

			for( int s = 0; 
				s < nLightmapSize0; 
				s++, m_LightmapPixelWriter.SkipBytes(nRewindToNextPixel),srcTexelOffset += (sizeof(Vector4D)/sizeof(float)))
			{					
				unsigned short color[4][4];

				ColorSpace::LinearToBumpedLightmap( &pFloatImage[srcTexelOffset],
					&pFloatImageBump1[srcTexelOffset], &pFloatImageBump2[srcTexelOffset],
					&pFloatImageBump3[srcTexelOffset],
					color[0], color[1], color[2], color[3] );
				unsigned short alpha = ColorSpace::LinearToUnsignedShort( pFloatImage[srcTexelOffset+3], 16 );
				color[0][3] = color[1][3] = color[2][3] = color[3][3] = alpha;

#if ( defined( USE_32BIT_LIGHTMAPS_ON_360 ) )
				if( IsX360() )
				{
					for( int i = 0; i != 4; ++i )
					{
						Vector4D vRGBScale;

						vRGBScale.x = color[i][0] * (16.0f / 65535.0f);
						vRGBScale.y = color[i][1] * (16.0f / 65535.0f);
						vRGBScale.z = color[i][2] * (16.0f / 65535.0f);
						vRGBScale = ConvertLightmapColorToRGBScale( &vRGBScale.x );
						color[i][0] = RoundFloatToByte( vRGBScale.x * 255.0f );
						color[i][1] = RoundFloatToByte( vRGBScale.y * 255.0f );
						color[i][2] = RoundFloatToByte( vRGBScale.z * 255.0f );
						color[i][3] = RoundFloatToByte( vRGBScale.w * 255.0f );
					}						
				}
#endif
				m_LightmapPixelWriter.WritePixelNoAdvance( color[0][0], color[0][1], color[0][2], color[0][3] );

				m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
				m_LightmapPixelWriter.WritePixelNoAdvance( color[1][0], color[1][1], color[1][2], color[1][3] );

				m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
				m_LightmapPixelWriter.WritePixelNoAdvance( color[2][0], color[2][1], color[2][2], color[2][3] );

				m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
				m_LightmapPixelWriter.WritePixelNoAdvance( color[3][0], color[3][1], color[3][2], color[3][3] );

				// Write data to the bitmapped represenations so that PFM files can be written
				if ( pfmOut )
				{
					PixRGBAF pixelData;
					pixelData.Red = color[0][0];                  
					pixelData.Green = color[0][1];                  
					pixelData.Blue = color[0][2];
					pixelData.Alpha = alpha;
					pfmOut->WritePixelRGBAF(pOffsetIntoLightmapPage[0] + s, pOffsetIntoLightmapPage[1] + t, pixelData);
				}
			}
		}
#else
		// this is an optimized XBOX implementation. For a clearer
		// presentation of the algorithm, see the PC implementation
		// above.
		// First check for the most common case, using an efficient
		// branch rather than a switch:
		if (m_LightmapPixelWriter.GetFormat() == IMAGE_FORMAT_LINEAR_BGRA8888)
		{
			// broken out into a static to make things more readable
			// and be nicer to the instruction cache
			BumpedLightmapBitsToPixelWriter_HDRI_BGRA_X360( pFloatImage, pFloatImageBump1, pFloatImageBump2, 
				pFloatImageBump3, pLightmapSize, pOffsetIntoLightmapPage, pfmOut, &m_LightmapPixelWriter );
		}
		else
		{	// This case should actually never be hit -- we do not use RGBA.
			for( int t = 0; t < pLightmapSize[1]; t++ )
			{
				// assert that 1 * 4 = 4 
				COMPILE_TIME_ASSERT(sizeof( Vector4D ) == sizeof(float) * 4); 
#define	FOUR (sizeof( Vector4D ) / sizeof( float ))  // in case this ever changes
				int srcTexelOffset = ( FOUR ) * ( 0 + t * nLightmapSize0 );
				m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );

				for( int s = 0; 
					s < nLightmapSize0; 
					s++, m_LightmapPixelWriter.SkipBytes(nRewindToNextPixel),srcTexelOffset += ( FOUR ))
				{				

					static const fltx4 vSixteen = {16.0f, 16.0f, 16.0f, 16.0f};
					fltx4 vColor[4];
					fltx4 vFloatImage = LoadUnalignedSIMD(&pFloatImage[srcTexelOffset]);
					fltx4 vFloatImageBump1 = LoadUnalignedSIMD(&pFloatImageBump1[srcTexelOffset]);
					fltx4 vFloatImageBump2 = LoadUnalignedSIMD(&pFloatImageBump2[srcTexelOffset]);
					fltx4 vFloatImageBump3 = LoadUnalignedSIMD(&pFloatImageBump3[srcTexelOffset]);
					
					// perform an arcane averaging operation upon the bump map values
					ColorSpace::LinearToBumpedLightmap( vFloatImage,
						vFloatImageBump1, vFloatImageBump2,
						vFloatImageBump3,
						vColor[0], vColor[1], vColor[2], vColor[3] );		

					// convert each color to RGB scaled.
					// DO NOT! make this into a for loop. The (April07 XDK) compiler
					// in fact DOES NOT unroll them, and will perform very naive
					// scheduling if you try. 

					// clamp to 0..16 float
					vColor[0] = MinSIMD(vColor[0], vSixteen);
					vColor[1] = MinSIMD(vColor[1], vSixteen);
					vColor[2] = MinSIMD(vColor[2], vSixteen);
					vColor[3] = MinSIMD(vColor[3], vSixteen);

					// compute the scaling factor, transform the RGB,
					// and place the scale in w. Obliterates whatever was
					// already in alpha.
					// This code is why it is important to not use a for
					// loop: you need to let the compiler interleave the
					// inlined instructions.
					vColor[0] = ConvertLightmapColorToRGBScale( vColor[0] );
					vColor[1] = ConvertLightmapColorToRGBScale( vColor[1] );
					vColor[2] = ConvertLightmapColorToRGBScale( vColor[2] );
					vColor[3] = ConvertLightmapColorToRGBScale( vColor[3] );


					m_LightmapPixelWriter.WritePixelNoAdvance( vColor[0] );
					m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
					m_LightmapPixelWriter.WritePixelNoAdvance( vColor[1] );
					m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
					m_LightmapPixelWriter.WritePixelNoAdvance( vColor[2] );
					m_LightmapPixelWriter.SkipBytes( nLightmap0WriterSizeBytes );
					m_LightmapPixelWriter.WritePixelNoAdvance( vColor[3] );

					AssertMsg(!pfmOut, "Runtime conversion of lightmaps to files is no longer supported on 360.\n");

					// Write data to the bitmapped represenations so that PFM files can be written
					if ( pfmOut )
					{
						Warning("**************************************************\n"
								"Lightmap output to files on 360 HAS BEEN DISABLED.\n"
								"A grave error has just occurred.\n"
								"**************************************************\n");
						DebuggerBreakIfDebugging();
						/*
						PixRGBAF pixelData;
						pixelData.Red = color[0][0];                  
						pixelData.Green = color[0][1];                  
						pixelData.Blue = color[0][2];
						pixelData.Alpha = alpha;
						pfmOut->WritePixelRGBAF(pOffsetIntoLightmapPage[0] + s, pOffsetIntoLightmapPage[1] + t, pixelData);
						*/
					}
				}
			}
		}
#endif
	}
}


void CMatLightmaps::LightmapBitsToPixelWriter_LDR( float* pFloatImage, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut )
{
	// non-HDR lightmap processing
	float *pSrc = pFloatImage;
	for( int t = 0; t < pLightmapSize[1]; ++t )
	{
		m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );
		for( int s = 0; s < pLightmapSize[0]; ++s, pSrc += (sizeof(Vector4D)/sizeof(*pSrc)) )
		{
			unsigned char color[4];
			ColorSpace::LinearToLightmap( color, pSrc );
			color[3] =  RoundFloatToByte( pSrc[3] * 255.0f );
			m_LightmapPixelWriter.WritePixel( color[0], color[1], color[2], color[3] );

			if ( pfmOut )
			{
				// Write data to the bitmapped represenations so that PFM files can be written
				PixRGBAF pixelData;
				pixelData.Red = color[0];                  
				pixelData.Green = color[1];                  
				pixelData.Blue = color[2];
				pixelData.Alpha = color[3];
				pfmOut->WritePixelRGBAF( pOffsetIntoLightmapPage[0] + s, pOffsetIntoLightmapPage[1] + t, pixelData );
			}
		}
	}
}


void CMatLightmaps::LightmapBitsToPixelWriter_HDRF( float* pFloatImage, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut )
{
	if ( IsX360() )
	{
		// 360 does not support HDR float 
		Assert( 0 );
		return;
	}

	// float HDR lightmap processing
	float *pSrc = pFloatImage;
	for ( int t = 0; t < pLightmapSize[1]; ++t )
	{
		m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );
		for ( int s = 0; s < pLightmapSize[0]; ++s, pSrc += (sizeof(Vector4D)/sizeof(*pSrc)) )
		{
			m_LightmapPixelWriter.WritePixelF( pSrc[0], pSrc[1], pSrc[2], pSrc[3] );
		}
	}
}

// numbers come in on the domain [0..16]
void CMatLightmaps::LightmapBitsToPixelWriter_HDRI( float* RESTRICT pFloatImage, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t * RESTRICT pfmOut )
{
#ifndef X360_USE_SIMD_LIGHTMAP
	// PC code (and old, pre-SIMD xbox version -- unshippably slow)
	if ( m_LightmapPixelWriter.IsUsingFloatFormat() )
	{
		// integer HDR lightmap processing
		float *pSrc = pFloatImage;
		for ( int t = 0; t < pLightmapSize[1]; ++t )
		{
			m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );
			for ( int s = 0; s < pLightmapSize[0]; ++s, pSrc += (sizeof(Vector4D)/sizeof(*pSrc)) )
			{
				int r, g, b, a;

				r = ColorSpace::LinearFloatToCorrectedShort( pSrc[0] );
				g = ColorSpace::LinearFloatToCorrectedShort( pSrc[1] );
				b = ColorSpace::LinearFloatToCorrectedShort( pSrc[2] );
				a = ColorSpace::LinearToUnsignedShort( pSrc[3], 16 );

				float toFloat = ( 1.0f / ( float )( 1 << 16 ) );

#if ( defined( USE_32BIT_LIGHTMAPS_ON_360 ) )
				if( IsX360() )
				{
					Vector4D vRGBScale;

					vRGBScale.x = r * (16.0f / 65535.0f);
					vRGBScale.y = g * (16.0f / 65535.0f);
					vRGBScale.z = b * (16.0f / 65535.0f);
					vRGBScale = ConvertLightmapColorToRGBScale( &vRGBScale.x );

					r = RoundFloatToByte( vRGBScale.x * 255.0f );
					g = RoundFloatToByte( vRGBScale.y * 255.0f );
					b = RoundFloatToByte( vRGBScale.z * 255.0f );
					a = RoundFloatToByte( vRGBScale.w * 255.0f );

					toFloat = ( 1.0f / ( float )( 1 << 8 ) );
				}

#endif
				Assert( pSrc[3] >= 0.0f && pSrc[3] <= 1.0f );
				m_LightmapPixelWriter.WritePixelF( r * toFloat, g * toFloat, b * toFloat, pSrc[3] );
			}
		}
	}
	else
	{
		// integer HDR lightmap processing
		float *pSrc = pFloatImage;
		for ( int t = 0; t < pLightmapSize[1]; ++t )
		{
			m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );
			for ( int s = 0; s < pLightmapSize[0]; ++s, pSrc += (sizeof(Vector4D)/sizeof(*pSrc)) )
			{
				int r, g, b, a;

				r = ColorSpace::LinearFloatToCorrectedShort( pSrc[0] );
				g = ColorSpace::LinearFloatToCorrectedShort( pSrc[1] );
				b = ColorSpace::LinearFloatToCorrectedShort( pSrc[2] );
				a = ColorSpace::LinearToUnsignedShort( pSrc[3], 16 );

#if ( defined( USE_32BIT_LIGHTMAPS_ON_360 ) )
				if( IsX360() )
				{
					Vector4D vRGBScale;

					vRGBScale.x = r * (16.0f / 65535.0f);
					vRGBScale.y = g * (16.0f / 65535.0f);
					vRGBScale.z = b * (16.0f / 65535.0f);
					vRGBScale = ConvertLightmapColorToRGBScale( &vRGBScale.x );

					r = RoundFloatToByte( vRGBScale.x * 255.0f );
					g = RoundFloatToByte( vRGBScale.y * 255.0f );
					b = RoundFloatToByte( vRGBScale.z * 255.0f );
					a = RoundFloatToByte( vRGBScale.w * 255.0f );
				}
#endif
				m_LightmapPixelWriter.WritePixel( r, g, b, a );

				if ( pfmOut )
				{
					// Write data to the bitmapped represenations so that PFM files can be written
					PixRGBAF pixelData;
					pixelData.Red = pSrc[0];                  
					pixelData.Green = pSrc[1];                  
					pixelData.Blue = pSrc[2];
					pixelData.Alpha = pSrc[3];
					pfmOut->WritePixelRGBAF( pOffsetIntoLightmapPage[0] + s, pOffsetIntoLightmapPage[1] + t, pixelData );
				}				
			}
		}
	}
#else
	// XBOX360 code
	if ( m_LightmapPixelWriter.IsUsingFloatFormat() )
	{
		if( IsX360() )
		{
			AssertMsg( false, "Float-format pixel writers do not exist on x360." );
		}
		else
		{	// This code is here as an example only, in case floating point
			// format is restored to 360.

			// integer HDR lightmap processing
			float * RESTRICT pSrc = pFloatImage;
			for ( int t = 0; t < pLightmapSize[1]; ++t )
			{
				m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );
				for ( int s = 0; s < pLightmapSize[0]; ++s, pSrc += (sizeof(Vector4D)/sizeof(*pSrc)) )
				{
					int r, g, b, a;

					r = ColorSpace::LinearFloatToCorrectedShort( pSrc[0] );
					g = ColorSpace::LinearFloatToCorrectedShort( pSrc[1] );
					b = ColorSpace::LinearFloatToCorrectedShort( pSrc[2] );
					a = ColorSpace::LinearToUnsignedShort( pSrc[3], 16 );

					float toFloat = ( 1.0f / ( float )( 1 << 16 ) );

#if ( defined( USE_32BIT_LIGHTMAPS_ON_360 ) )
					if( IsX360() )
					{
						Vector4D vRGBScale;

						vRGBScale.x = r * (16.0f / 65535.0f);
						vRGBScale.y = g * (16.0f / 65535.0f);
						vRGBScale.z = b * (16.0f / 65535.0f);
						vRGBScale = ConvertLightmapColorToRGBScale( &vRGBScale.x );

						r = RoundFloatToByte( vRGBScale.x * 255.0f );
						g = RoundFloatToByte( vRGBScale.y * 255.0f );
						b = RoundFloatToByte( vRGBScale.z * 255.0f );
						a = RoundFloatToByte( vRGBScale.w * 255.0f );

						toFloat = ( 1.0f / ( float )( 1 << 8 ) );
					}

#endif
					Assert( pSrc[3] >= 0.0f && pSrc[3] <= 1.0f );
					m_LightmapPixelWriter.WritePixelF( r * toFloat, g * toFloat, b * toFloat, pSrc[3] );
				}
			}
		}
	}
	else
	{
		// This is the fast X360 pathway.

		// integer HDR lightmap processing
		float * RESTRICT pSrc = pFloatImage;
		// Assert((reinterpret_cast<unsigned int>(pSrc) & 15) == 0); // 16-byte aligned?
		COMPILE_TIME_ASSERT(sizeof(Vector4D)/sizeof(*pSrc) == 4); // assert that 1 * 4 = 4
#ifndef USE_32BIT_LIGHTMAPS_ON_360 
#pragma error("This function only supports 32 bit lightmaps.")
#endif

		// input numbers from pSrc are on the domain [0..+inf]
		// we clamp them to the range [0..16]
		// output is RGBA 
		// the shader does this: rOut = Rin * Ain * 16.0f 
		// where Rin is [0..1], a float computed from a byte value [0..255]
		// Ain is therefore the brightest channel (say R) divided by 16 and quantized
		// Rin is computed from pSrc->r by dividing by Ain
		
		// rather than switching inside WritePixel for each different format,
		// thus causing a 23-cycle pipeline clear for every pixel, we'll
		// branch on the format here. That will allow us to unroll the inline
		// pixel write functions differently depending on their different 
		// latencies. 

		Assert(!pfmOut); // should never happen on 360.
#ifndef ALLOW_PFM_OUTPUT_ON_360
		if ( pfmOut )
		{
			Warning("*****************************************\n"
					"Lightmap output on 360 HAS BEEN DISABLED.\n"
					"A grave error has just occurred.\n"
					"*****************************************\n");
		}
#endif

		// switch once, here, outside the loop, rather than
		// switching inside each pixel. Switches are not fast
		// on x360: they are usually implemented as jumps 
		// through function tables, which have a 24-cycle
		// stall. 
		switch (m_LightmapPixelWriter.GetFormat())
		{
			// note: format names are low-order-byte first. 
		case IMAGE_FORMAT_RGBA8888:
		case IMAGE_FORMAT_LINEAR_RGBA8888:
		{
			for ( int t = 0; t < pLightmapSize[1]; ++t )
			{
				m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );
				for ( int s = 0; s < pLightmapSize[0]; ++s, pSrc += 4 )
				{	
					static const fltx4 vSixteen = {16.0f, 16.0f, 16.0f, 16.0f};
					fltx4 rgba = LoadUnalignedSIMD(pSrc);

					// clamp to 0..16 float
					rgba = MinSIMD(rgba, vSixteen);
					// compute the scaling factor, place it in w, and 
					// scale the rest by it.
					rgba = ConvertLightmapColorToRGBScale( rgba );
					// rgba is now  float 0..255 in each component
					m_LightmapPixelWriter.WritePixelNoAdvance_RGBA8888(rgba);


					/*  // not supported on X360
					if ( pfmOut )
					{
						// Write data to the bitmapped represenations so that PFM files can be written
						PixRGBAF pixelData;
						XMStoreVector4(&pixelData,rgba);
						pfmOut->WritePixelRGBAF( pOffsetIntoLightmapPage[0] + s, pOffsetIntoLightmapPage[1] + t, pixelData );
					}			
					*/
				}
			}
			break;
		}

		case IMAGE_FORMAT_BGRA8888: // NOTE! : the low order bits are first in this naming convention.
		case IMAGE_FORMAT_LINEAR_BGRA8888:
		{			
			for ( int t = 0; t < pLightmapSize[1]; ++t )
			{
				m_LightmapPixelWriter.Seek( pOffsetIntoLightmapPage[0], pOffsetIntoLightmapPage[1] + t );
				for ( int s = 0; s < pLightmapSize[0]; ++s, pSrc += 4 )
				{	
					static const fltx4 vSixteen = {16.0f, 16.0f, 16.0f, 16.0f};
					fltx4 rgba = LoadUnalignedSIMD(pSrc);

					// clamp to 0..16 float
					rgba = MinSIMD(rgba, vSixteen);
					// compute the scaling factor, place it in w, and 
					// scale the rest by it.
					rgba = ConvertLightmapColorToRGBScale( rgba );
					// rgba is now  float 0..255 in each component
					m_LightmapPixelWriter.WritePixelNoAdvance_BGRA8888(rgba);
					// forcibly advance
					m_LightmapPixelWriter.SkipBytes(4);

					/* // not supported on X360
					if ( pfmOut )
					{
						// Write data to the bitmapped represenations so that PFM files can be written
						PixRGBAF pixelData;
						XMStoreVector4(&pixelData,rgba);
						pfmOut->WritePixelRGBAF( pOffsetIntoLightmapPage[0] + s, pOffsetIntoLightmapPage[1] + t, pixelData );
					}			
					*/
				}
			}
			break;
		}

		default:
			AssertMsg1(false,"Unsupported pixel format %d while writing lightmaps!", m_LightmapPixelWriter.GetFormat() );
			Warning("Unsupported pixel format used in lightmap. Lightmaps could not be downloaded.\n");
			break;
		}
	}
#endif
}

void CMatLightmaps::BeginUpdateLightmaps( void )
{
	CMatCallQueue *pCallQueue = GetMaterialSystem()->GetRenderContextInternal()->GetCallQueueInternal();
	if ( pCallQueue )
	{
		pCallQueue->QueueCall( this, &CMatLightmaps::BeginUpdateLightmaps );
		return;
	}

	m_nUpdatingLightmapsStackDepth++;
}

void CMatLightmaps::EndUpdateLightmaps( void )
{
	CMatCallQueue *pCallQueue = GetMaterialSystem()->GetRenderContextInternal()->GetCallQueueInternal();
	if ( pCallQueue )
	{
		pCallQueue->QueueCall( this, &CMatLightmaps::EndUpdateLightmaps );
		return;
	}

	m_nUpdatingLightmapsStackDepth--;
	Assert( m_nUpdatingLightmapsStackDepth >= 0 );
	if( m_nUpdatingLightmapsStackDepth <= 0 && m_nLockedLightmap != -1 )
	{
		g_pShaderAPI->TexUnlock();
		m_nLockedLightmap = -1;
	}
}

int CMatLightmaps::AllocateDynamicLightmap( int lightmapSize[2], int *pOutOffsetIntoPage, int frameID )
{
	// check frameID, fail if current
	for ( int i = 0; i < COUNT_DYNAMIC_LIGHTMAP_PAGES; i++ )
	{
		int dynamicIndex = (m_dynamic.currentDynamicIndex + i) % COUNT_DYNAMIC_LIGHTMAP_PAGES;
		int lightmapPageIndex = m_firstDynamicLightmap + dynamicIndex;
		if ( m_dynamic.lightmapLockFrame[dynamicIndex] != frameID )
		{
			m_dynamic.lightmapLockFrame[dynamicIndex] = frameID;
			m_dynamic.imagePackers[dynamicIndex].Reset( 0, m_pLightmapPages[lightmapPageIndex].m_Width, m_pLightmapPages[lightmapPageIndex].m_Height );
		}

		if ( m_dynamic.imagePackers[dynamicIndex].AddBlock( lightmapSize[0], lightmapSize[1], &pOutOffsetIntoPage[0], &pOutOffsetIntoPage[1] ) )
		{
			return lightmapPageIndex;
		}
	}
	
	return -1;
}

//-----------------------------------------------------------------------------
// Updates the lightmap
//-----------------------------------------------------------------------------
void CMatLightmaps::UpdateLightmap( int lightmapPageID, int lightmapSize[2],
									  int offsetIntoLightmapPage[2], 
									  float *pFloatImage, float *pFloatImageBump1,
									  float *pFloatImageBump2, float *pFloatImageBump3 )
{
	VPROF( "CMatRenderContext::UpdateLightmap" );

	bool hasBump = false;
	int uSize = 1;
	FloatBitMap_t *pfmOut = NULL;
	if ( pFloatImageBump1 && pFloatImageBump2 && pFloatImageBump3 )
	{
		hasBump = true;
		uSize = 4;
	}

	if ( lightmapPageID >= GetNumLightmapPages() || lightmapPageID < 0 )
	{
		Error( "MaterialSystem_Interface_t::UpdateLightmap lightmapPageID=%d out of range\n", lightmapPageID );
		return;
	}
	bool bDynamic = IsDynamicLightmap(lightmapPageID);

	if ( bDynamic )
	{
		int dynamicIndex = lightmapPageID-m_firstDynamicLightmap;
		Assert(dynamicIndex < COUNT_DYNAMIC_LIGHTMAP_PAGES);
		m_dynamic.currentDynamicIndex = (dynamicIndex + 1) % COUNT_DYNAMIC_LIGHTMAP_PAGES;
	}

	if ( mat_lightmap_pfms.GetBool())
	{
		// Allocate and initialize lightmap data that will be written to a PFM file
		if (NULL == m_pLightmapDataPtrArray[lightmapPageID])
		{
			m_pLightmapDataPtrArray[lightmapPageID] = new FloatBitMap_t(m_pLightmapPages[lightmapPageID].m_Width, m_pLightmapPages[lightmapPageID].m_Height);
			m_pLightmapDataPtrArray[lightmapPageID]->Clear(0, 0, 0, 1);
		}
		pfmOut = m_pLightmapDataPtrArray[lightmapPageID];
	}

	// NOTE: Change how the lock is taking place if you ever change how bumped
	// lightmaps are put into the page. Right now, we assume that they're all
	// added to the right of the original lightmap.
	bool bLockSubRect;
	{
		VPROF_( "Locking lightmaps", 2, VPROF_BUDGETGROUP_DLIGHT_RENDERING, false, 0 ); // vprof scope

		bLockSubRect = m_nUpdatingLightmapsStackDepth <= 0 && !bDynamic;
		if( bLockSubRect )
		{
			VPROF_INCREMENT_COUNTER( "lightmap subrect texlock", 1 );
			g_pShaderAPI->ModifyTexture( m_LightmapPageTextureHandles[lightmapPageID] );
			if (!g_pShaderAPI->TexLock( 0, 0, offsetIntoLightmapPage[0], offsetIntoLightmapPage[1],
				lightmapSize[0] * uSize, lightmapSize[1], m_LightmapPixelWriter ))
			{
				return;
			}
		}
		else if( lightmapPageID != m_nLockedLightmap )
		{
			if ( !LockLightmap( lightmapPageID ) )
			{
				ExecuteNTimes( 10, Warning( "Failed to lock lightmap\n" ) );
				return;
			}
		}
	}

	int subRectOffset[2] = {0,0};

	{
		// account for the part spent in math:
		VPROF_( "LightmapBitsToPixelWriter", 2, VPROF_BUDGETGROUP_DLIGHT_RENDERING, false, 0 );
		if ( hasBump )
		{
			switch( HardwareConfig()->GetHDRType() )
			{
			case HDR_TYPE_NONE:
				BumpedLightmapBitsToPixelWriter_LDR( pFloatImage, pFloatImageBump1, pFloatImageBump2, pFloatImageBump3, 
					lightmapSize, bLockSubRect ? subRectOffset : offsetIntoLightmapPage, pfmOut );
				break;
			case HDR_TYPE_INTEGER:
				BumpedLightmapBitsToPixelWriter_HDRI( pFloatImage, pFloatImageBump1, pFloatImageBump2, pFloatImageBump3, 
					lightmapSize, bLockSubRect ? subRectOffset : offsetIntoLightmapPage, pfmOut );
				break;
			case HDR_TYPE_FLOAT:
				BumpedLightmapBitsToPixelWriter_HDRF( pFloatImage, pFloatImageBump1, pFloatImageBump2, pFloatImageBump3, 
					lightmapSize, bLockSubRect ? subRectOffset : offsetIntoLightmapPage, pfmOut );
				break;
			}
		}
		else
		{
			switch ( HardwareConfig()->GetHDRType() )
			{
			case HDR_TYPE_NONE:
				LightmapBitsToPixelWriter_LDR( pFloatImage, lightmapSize, bLockSubRect ? subRectOffset : offsetIntoLightmapPage, pfmOut );
				break;

			case HDR_TYPE_INTEGER:
				LightmapBitsToPixelWriter_HDRI( pFloatImage, lightmapSize, bLockSubRect ? subRectOffset : offsetIntoLightmapPage, pfmOut );
				break;

			case HDR_TYPE_FLOAT:
				LightmapBitsToPixelWriter_HDRF( pFloatImage, lightmapSize, bLockSubRect ? subRectOffset : offsetIntoLightmapPage, pfmOut );
				break;

			default:
				Assert( 0 );
				break;
			}
		}
	}

	if( bLockSubRect )
	{
		VPROF_( "Unlocking Lightmaps", 2, VPROF_BUDGETGROUP_DLIGHT_RENDERING, false, 0 );
		g_pShaderAPI->TexUnlock();
	}
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int	CMatLightmaps::GetNumSortIDs( void )
{
	return m_numSortIDs;
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatLightmaps::ComputeSortInfo( MaterialSystem_SortInfo_t* pInfo, int& sortId, bool alpha )
{
	int lightmapPageID;

	for ( MaterialHandle_t i = GetMaterialDict()->FirstMaterial(); i != GetMaterialDict()->InvalidMaterial(); i = GetMaterialDict()->NextMaterial(i) )
	{
		IMaterialInternal* pMaterial = GetMaterialInternal(i);

		if ( pMaterial->GetMinLightmapPageID() > pMaterial->GetMaxLightmapPageID() )
		{
			continue;
		}
		
		//	const IMaterialVar *pTransVar = pMaterial->GetMaterialProperty( MATERIAL_PROPERTY_OPACITY );
		//	if( ( !alpha && ( pTransVar->GetIntValue() == MATERIAL_TRANSLUCENT ) ) ||
		//		( alpha && !( pTransVar->GetIntValue() == MATERIAL_TRANSLUCENT ) ) )
		//	{
		//		return true;
		//	}

	
//		Warning( "sort stuff: %s %s\n", material->GetName(), bAlpha ? "alpha" : "not alpha" );
		
		// fill in the lightmapped materials
		for ( lightmapPageID = pMaterial->GetMinLightmapPageID(); 
			 lightmapPageID <= pMaterial->GetMaxLightmapPageID(); ++lightmapPageID )
		{
			pInfo[sortId].material = pMaterial->GetQueueFriendlyVersion();
			pInfo[sortId].lightmapPageID = lightmapPageID;
#if 0
			char buf[128];
			Q_snprintf( buf, sizeof( buf ), "ComputeSortInfo: %s lightmapPageID: %d sortID: %d\n", pMaterial->GetName(), lightmapPageID, sortId );
			OutputDebugString( buf );
#endif
			++sortId;
		}
	}
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatLightmaps::ComputeWhiteLightmappedSortInfo( MaterialSystem_SortInfo_t* pInfo, int& sortId, bool alpha )
{
	for (MaterialHandle_t i = GetMaterialDict()->FirstMaterial(); i != GetMaterialDict()->InvalidMaterial(); i = GetMaterialDict()->NextMaterial(i) )
	{
		IMaterialInternal* pMaterial = GetMaterialInternal(i);

		// fill in the lightmapped materials that are actually used by this level
		if( pMaterial->GetNeedsWhiteLightmap() && 
			( pMaterial->GetReferenceCount() > 0 ) )
		{
			// const IMaterialVar *pTransVar = pMaterial->GetMaterialProperty( MATERIAL_PROPERTY_OPACITY );
			//		if( ( !alpha && ( pTransVar->GetIntValue() == MATERIAL_TRANSLUCENT ) ) ||
			//			( alpha && !( pTransVar->GetIntValue() == MATERIAL_TRANSLUCENT ) ) )
			//		{
			//			return true;
			//		}

			pInfo[sortId].material = pMaterial->GetQueueFriendlyVersion();
			if( pMaterial->GetPropertyFlag( MATERIAL_PROPERTY_NEEDS_BUMPED_LIGHTMAPS ) )
			{
				pInfo[sortId].lightmapPageID = MATERIAL_SYSTEM_LIGHTMAP_PAGE_WHITE_BUMP;
			}
			else
			{
				pInfo[sortId].lightmapPageID = MATERIAL_SYSTEM_LIGHTMAP_PAGE_WHITE;
			}

			sortId++;
		}
	}
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatLightmaps::GetSortInfo( MaterialSystem_SortInfo_t *pSortInfoArray )
{
	// sort non-alpha blended materials first
	int sortId = 0;
	ComputeSortInfo( pSortInfoArray, sortId, false );
	ComputeWhiteLightmappedSortInfo( pSortInfoArray, sortId, false );
	Assert( m_numSortIDs == sortId );
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatLightmaps::EnableLightmapFiltering( bool enabled )
{
	int i;
	for( i = 0; i < GetNumLightmapPages(); i++ )
	{
		g_pShaderAPI->ModifyTexture( m_LightmapPageTextureHandles[i] );
		if( enabled )
		{
			g_pShaderAPI->TexMinFilter( SHADER_TEXFILTERMODE_LINEAR );
			g_pShaderAPI->TexMagFilter( SHADER_TEXFILTERMODE_LINEAR );
		}
		else
		{
			g_pShaderAPI->TexMinFilter( SHADER_TEXFILTERMODE_NEAREST );
			g_pShaderAPI->TexMagFilter( SHADER_TEXFILTERMODE_NEAREST );
		}
	}
}