summaryrefslogtreecommitdiff
path: root/materialsystem/shaderapidx9/colorformatdx8.cpp
blob: daf3e5032f8570c972e3019821e75b81f743bd00 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//===========================================================================//

#define DISABLE_PROTECTED_THINGS
#include "togl/rendermechanism.h"
#include "locald3dtypes.h"
#include "colorformatdx8.h"
#include "shaderapidx8_global.h"
#include "bitmap/imageformat.h"
#include "shaderapi/ishaderutil.h"
#include "tier0/dbg.h"
#include "tier1/strtools.h"
#include "shaderdevicedx8.h"


// Must be last
#include "tier0/memdbgon.h"


//-----------------------------------------------------------------------------
// Figures out what texture formats we support
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// globals
//-----------------------------------------------------------------------------

// Texture formats supported by DX driver[vertextexture][render target][non filterable]
static D3DFORMAT	g_D3DColorFormat[NUM_IMAGE_FORMATS][2][2][2];
static UINT			g_DisplayAdapter;
static D3DDEVTYPE	g_DeviceType;
static ImageFormat	g_DeviceFormat;
static bool			g_bSupportsD24S8;
static bool			g_bSupportsD24X8;
static bool			g_bSupportsD16;
static bool			g_bSupportsD24X4S4;
static bool			g_bSupportsD15S1;

//-----------------------------------------------------------------------------
// Determines what formats we actually *do* support
//-----------------------------------------------------------------------------
static bool TestTextureFormat( D3DFORMAT format, bool bIsRenderTarget, 
							   bool bIsVertexTexture, bool bIsFilterableRequired ) 
{
	int nUsage = bIsRenderTarget ? D3DUSAGE_RENDERTARGET : 0;
	if ( bIsVertexTexture )
	{
		// vertex textures never need filtering
		nUsage |= D3DUSAGE_QUERY_VERTEXTEXTURE;
	}
	if ( bIsFilterableRequired )
	{
		nUsage |= D3DUSAGE_QUERY_FILTER;
	}

	HRESULT hr;

	// IHV depth texture formats require a slightly different check...
	if ( !IsX360() && bIsRenderTarget && ( ( format == NVFMT_RAWZ ) || ( format == NVFMT_INTZ   ) ||
										   ( format == D3DFMT_D16 ) || ( format == D3DFMT_D24S8 ) ||
										   ( format == ATIFMT_D16 ) || ( format == ATIFMT_D24S8 ) ) )
	{
		hr = D3D()->CheckDeviceFormat(
			g_DisplayAdapter, g_DeviceType, ImageLoader::ImageFormatToD3DFormat( g_DeviceFormat ),
			D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, format );
	}
	else if ( !IsX360() || !bIsRenderTarget )
	{
		// See if we can do it!
		hr = D3D()->CheckDeviceFormat( 
			g_DisplayAdapter, g_DeviceType, ImageLoader::ImageFormatToD3DFormat( g_DeviceFormat ),
			nUsage, D3DRTYPE_TEXTURE, format );
	}
	else // 360
	{
		// 360 can only validate render targets as surface display format
		hr = D3D()->CheckDeviceFormat( g_DisplayAdapter, g_DeviceType, format, 0, D3DRTYPE_SURFACE, format );
	}

    return SUCCEEDED( hr );
}

D3DFORMAT GetNearestD3DColorFormat( ImageFormat fmt,
									bool isRenderTarget, bool bIsVertexTexture,
									bool bIsFilterableRequired)
{
	switch(fmt)
	{
	case IMAGE_FORMAT_RGBA8888:
	case IMAGE_FORMAT_ABGR8888:
	case IMAGE_FORMAT_ARGB8888:
	case IMAGE_FORMAT_BGRA8888:
		if (TestTextureFormat(D3DFMT_A8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A8R8G8B8;
		if (TestTextureFormat(D3DFMT_A4R4G4B4, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A4R4G4B4;
		break;

#if defined( _X360 )
	case IMAGE_FORMAT_LINEAR_RGBA8888:
	case IMAGE_FORMAT_LINEAR_ABGR8888:
	case IMAGE_FORMAT_LINEAR_ARGB8888:
	case IMAGE_FORMAT_LINEAR_BGRA8888:
		// same as above - all xxxx8888 RGBA ordering funnels to d3d a8r8g8b8
		if ( TestTextureFormat( D3DFMT_LIN_A8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_LIN_A8R8G8B8;
		break;
#endif

#if defined( _X360 )
	case IMAGE_FORMAT_LINEAR_BGRX8888:
		if ( TestTextureFormat( D3DFMT_LIN_X8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_LIN_X8R8G8B8;
		break;
#endif

	case IMAGE_FORMAT_BGRX8888:
		// We want this format to return exactly it's equivalent so that
		// when we create render targets to blit to from the framebuffer,
		// the CopyRect won't fail due to format mismatches.
		if (TestTextureFormat(D3DFMT_X8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_X8R8G8B8;

		// fall through. . . .
	case IMAGE_FORMAT_RGB888:
	case IMAGE_FORMAT_BGR888:
#if !defined( _X360 )
		if (TestTextureFormat(D3DFMT_R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_R8G8B8;
#endif
		if (TestTextureFormat(D3DFMT_X8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_X8R8G8B8;
		if (TestTextureFormat(D3DFMT_A8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A8R8G8B8;
		if (TestTextureFormat(D3DFMT_R5G6B5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_R5G6B5;
		if (TestTextureFormat(D3DFMT_X1R5G5B5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_X1R5G5B5;
		if (TestTextureFormat(D3DFMT_A1R5G5B5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A1R5G5B5;
		break;

	case IMAGE_FORMAT_BGR565:
	case IMAGE_FORMAT_RGB565:
		if (TestTextureFormat(D3DFMT_R5G6B5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_R5G6B5;
		if (TestTextureFormat(D3DFMT_X1R5G5B5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_X1R5G5B5;
		if (TestTextureFormat(D3DFMT_A1R5G5B5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A1R5G5B5;
#if !defined( _X360 )
		if (TestTextureFormat(D3DFMT_R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_R8G8B8;
#endif
		if (TestTextureFormat(D3DFMT_X8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_X8R8G8B8;
		if (TestTextureFormat(D3DFMT_A8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A8R8G8B8;
		break;

	case IMAGE_FORMAT_BGRX5551:
		if (TestTextureFormat(D3DFMT_X1R5G5B5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_X1R5G5B5;
		if (TestTextureFormat(D3DFMT_A1R5G5B5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A1R5G5B5;
		if (TestTextureFormat(D3DFMT_R5G6B5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_R5G6B5;
#if !defined( _X360 )
		if (TestTextureFormat(D3DFMT_R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_R8G8B8;
#endif
		if (TestTextureFormat(D3DFMT_X8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_X8R8G8B8;
		if (TestTextureFormat(D3DFMT_A8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A8R8G8B8;
		break;

#if defined( _X360 )
	case IMAGE_FORMAT_LINEAR_BGRX5551:
		if ( TestTextureFormat( D3DFMT_LIN_X1R5G5B5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_LIN_X1R5G5B5;
		break;
#endif

	case IMAGE_FORMAT_BGRA5551:
		if (TestTextureFormat(D3DFMT_A1R5G5B5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A1R5G5B5;
		if (TestTextureFormat(D3DFMT_A4R4G4B4, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A4R4G4B4;
		if (TestTextureFormat(D3DFMT_A8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A8R8G8B8;
		break;

	case IMAGE_FORMAT_BGRA4444:
		if (TestTextureFormat(D3DFMT_A4R4G4B4, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A4R4G4B4;
		if (TestTextureFormat(D3DFMT_A8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A8R8G8B8;
		break;

	case IMAGE_FORMAT_I8:
		if (TestTextureFormat(D3DFMT_L8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_L8;
		if (TestTextureFormat(D3DFMT_A8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A8R8G8B8;
		break;

#if defined( _X360 )
	case IMAGE_FORMAT_LINEAR_I8:
		if ( TestTextureFormat( D3DFMT_LIN_L8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_LIN_L8;
		break;
#endif

	case IMAGE_FORMAT_IA88:
		if (TestTextureFormat(D3DFMT_A8L8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A8L8;
		if (TestTextureFormat(D3DFMT_A8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A8R8G8B8;
		break;

	case IMAGE_FORMAT_A8:
		if (TestTextureFormat(D3DFMT_A8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A8;
		if (TestTextureFormat(D3DFMT_A8R8G8B8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_A8R8G8B8;
		break;

	case IMAGE_FORMAT_DXT1:
	case IMAGE_FORMAT_DXT1_ONEBITALPHA:
	case IMAGE_FORMAT_DXT1_RUNTIME:
		if (TestTextureFormat(D3DFMT_DXT1, isRenderTarget, bIsVertexTexture, bIsFilterableRequired))
			return D3DFMT_DXT1;
		break;

	case IMAGE_FORMAT_DXT3:
		if (TestTextureFormat(D3DFMT_DXT3, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ))
			return D3DFMT_DXT3;
		break;

	case IMAGE_FORMAT_DXT5:
	case IMAGE_FORMAT_DXT5_RUNTIME:
		if (TestTextureFormat(D3DFMT_DXT5, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ))
			return D3DFMT_DXT5;
		break;

	case IMAGE_FORMAT_UV88:
		if (TestTextureFormat(D3DFMT_V8U8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ))
			return D3DFMT_V8U8;
		break;

	case IMAGE_FORMAT_UVWQ8888:
		if (TestTextureFormat(D3DFMT_Q8W8V8U8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ))
			return D3DFMT_Q8W8V8U8;
		break;

	case IMAGE_FORMAT_UVLX8888:
		if (TestTextureFormat(D3DFMT_X8L8V8U8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ))
			return D3DFMT_X8L8V8U8;
		break;

	case IMAGE_FORMAT_RGBA16161616F:
		if ( TestTextureFormat( D3DFMT_A16B16G16R16F, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_A16B16G16R16F;
		if ( TestTextureFormat( D3DFMT_A16B16G16R16, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_A16B16G16R16;
		break;

	case IMAGE_FORMAT_RGBA16161616:
		if ( TestTextureFormat( D3DFMT_A16B16G16R16, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_A16B16G16R16;
		if ( TestTextureFormat( D3DFMT_A16B16G16R16F, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_A16B16G16R16F;
		break;

#if defined( _X360 )
	case IMAGE_FORMAT_LINEAR_RGBA16161616:
		if ( TestTextureFormat( D3DFMT_LIN_A16B16G16R16, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_LIN_A16B16G16R16;
		break;
#endif

	case IMAGE_FORMAT_R32F:
		if ( TestTextureFormat( D3DFMT_R32F, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_R32F;
		break;

	case IMAGE_FORMAT_RGBA32323232F:
		if ( TestTextureFormat( D3DFMT_A32B32G32R32F, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_A32B32G32R32F;
		break;

#if defined( _X360 )
	case IMAGE_FORMAT_X360_DST16:
		return D3DFMT_D16;

	case IMAGE_FORMAT_X360_DST24:
		return D3DFMT_D24S8;

	case IMAGE_FORMAT_X360_DST24F:
		return D3DFMT_D24FS8;

	case IMAGE_FORMAT_LE_BGRX8888:
		return D3DFMT_LE_X8R8G8B8;

	case IMAGE_FORMAT_LE_BGRA8888:
		return D3DFMT_LE_A8R8G8B8;
#endif

	// nVidia overloads DST formats as texture formats
	case IMAGE_FORMAT_NV_DST16:
		if ( TestTextureFormat( D3DFMT_D16, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_D16;
		break;

	case IMAGE_FORMAT_NV_DST24:
		if ( TestTextureFormat( D3DFMT_D24S8, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return D3DFMT_D24S8;
		break;
	case IMAGE_FORMAT_NV_INTZ:
		if ( TestTextureFormat( NVFMT_INTZ, isRenderTarget, bIsVertexTexture, false ) )
			return NVFMT_INTZ;
		break;

	case IMAGE_FORMAT_NV_RAWZ:
		if ( TestTextureFormat( NVFMT_RAWZ, isRenderTarget, bIsVertexTexture, false ) )
			return NVFMT_RAWZ;
		break;

	case IMAGE_FORMAT_NV_NULL:
		if ( TestTextureFormat( NVFMT_NULL, isRenderTarget, bIsVertexTexture, false ) )
			return NVFMT_NULL;
		break;

	case IMAGE_FORMAT_ATI_DST16:
		if ( TestTextureFormat( ATIFMT_D16, isRenderTarget, bIsVertexTexture, false ) )
			return ATIFMT_D16;
		break;

	case IMAGE_FORMAT_ATI_DST24:
		if ( TestTextureFormat( ATIFMT_D24S8, isRenderTarget, bIsVertexTexture, false ) )
			return ATIFMT_D24S8;
		break;

	case IMAGE_FORMAT_ATI2N:
		if ( TestTextureFormat( ATIFMT_ATI2N, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return ATIFMT_ATI2N;
		break;

	case IMAGE_FORMAT_ATI1N:
		if ( TestTextureFormat( ATIFMT_ATI1N, isRenderTarget, bIsVertexTexture, bIsFilterableRequired ) )
			return ATIFMT_ATI1N;
		break;
	}

	return D3DFMT_UNKNOWN;
}

void InitializeColorInformation( UINT displayAdapter, D3DDEVTYPE deviceType, 
								 ImageFormat displayFormat )
{
	g_DisplayAdapter = displayAdapter;
	g_DeviceType = deviceType;
	g_DeviceFormat = displayFormat;

	int fmt = 0;
	while ( fmt < NUM_IMAGE_FORMATS )
	{
		for ( int nVertexTexture = 0; nVertexTexture <= 1; ++nVertexTexture )
		{
			for ( int nRenderTarget = 0; nRenderTarget <= 1; ++nRenderTarget )
			{
				for ( int nFilterable = 0; nFilterable <= 1; ++nFilterable )
				{
					g_D3DColorFormat[fmt][nVertexTexture][nRenderTarget][nFilterable] = 
						GetNearestD3DColorFormat( (ImageFormat)fmt, nRenderTarget != 0, nVertexTexture != 0, nFilterable != 0 );
				}
			}
		}
		++fmt;
	}

	// Check the depth formats
    HRESULT hr = D3D()->CheckDeviceFormat( 
		g_DisplayAdapter, g_DeviceType, ImageLoader::ImageFormatToD3DFormat( g_DeviceFormat ),
        D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24S8 );
	g_bSupportsD24S8 = !FAILED(hr);

    hr = D3D()->CheckDeviceFormat( 
		g_DisplayAdapter, g_DeviceType, ImageLoader::ImageFormatToD3DFormat( g_DeviceFormat ),
        D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24X8 );
	g_bSupportsD24X8 = !FAILED(hr);

    hr = D3D()->CheckDeviceFormat( 
		g_DisplayAdapter, g_DeviceType, ImageLoader::ImageFormatToD3DFormat( g_DeviceFormat ),
        D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16 );
	g_bSupportsD16 = !FAILED(hr);

#if !defined( _X360 )
	hr = D3D()->CheckDeviceFormat( 
		g_DisplayAdapter, g_DeviceType, ImageLoader::ImageFormatToD3DFormat( g_DeviceFormat ),
		D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24X4S4 );
	g_bSupportsD24X4S4 = !FAILED(hr);
#else
	g_bSupportsD24X4S4 = false;
#endif

#if !defined( _X360 )
	hr = D3D()->CheckDeviceFormat( 
		g_DisplayAdapter, g_DeviceType, ImageLoader::ImageFormatToD3DFormat( g_DeviceFormat ),
		D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D15S1 );
	g_bSupportsD15S1 = !FAILED(hr);
#else
	g_bSupportsD15S1 = false;
#endif
}


//-----------------------------------------------------------------------------
// Returns true if compressed textures are supported
//-----------------------------------------------------------------------------
bool D3DSupportsCompressedTextures()
{
	return	(g_D3DColorFormat[IMAGE_FORMAT_DXT1][0][0][0] != D3DFMT_UNKNOWN) &&
			(g_D3DColorFormat[IMAGE_FORMAT_DXT3][0][0][0] != D3DFMT_UNKNOWN) &&
			(g_D3DColorFormat[IMAGE_FORMAT_DXT5][0][0][0] != D3DFMT_UNKNOWN);
}


//-----------------------------------------------------------------------------
// Returns closest supported format
//-----------------------------------------------------------------------------
ImageFormat FindNearestSupportedFormat( ImageFormat format, bool bIsVertexTexture, bool bIsRenderTarget, bool bFilterableRequired )
{
	return ImageLoader::D3DFormatToImageFormat( g_D3DColorFormat[format][bIsVertexTexture][bIsRenderTarget][bFilterableRequired] );
}


//-----------------------------------------------------------------------------
// Returns true if compressed textures are supported
//-----------------------------------------------------------------------------
bool D3DSupportsDepthTexture(D3DFORMAT format)
{
	// See if we can do it!
    HRESULT hr = D3D()->CheckDeviceFormat( 
		g_DisplayAdapter, g_DeviceType, ImageLoader::ImageFormatToD3DFormat(g_DeviceFormat),
        D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, format);

	return !FAILED(hr);
}


//-----------------------------------------------------------------------------
// Returns true if the depth format is compatible with the display
//-----------------------------------------------------------------------------
static inline bool IsDepthFormatCompatible( int nAdapter, ImageFormat displayFormat, ImageFormat renderTargetFormat, D3DFORMAT depthFormat )
{
	D3DFORMAT d3dDisplayFormat = ImageLoader::ImageFormatToD3DFormat( displayFormat );
	D3DFORMAT d3dRenderTargetFormat = ImageLoader::ImageFormatToD3DFormat( renderTargetFormat );

	// Verify that the depth format is compatible.
	HRESULT hr = D3D()->CheckDepthStencilMatch(	nAdapter, DX8_DEVTYPE,
		d3dDisplayFormat, d3dRenderTargetFormat, depthFormat);
	return !FAILED(hr);
}

//-----------------------------------------------------------------------------
// Finds the nearest supported depth buffer format
//-----------------------------------------------------------------------------
D3DFORMAT FindNearestSupportedDepthFormat( int nAdapter, ImageFormat displayFormat, ImageFormat renderTargetFormat, D3DFORMAT depthFormat )
{
	// This is the default case, used for rendering to the main render target
	Assert( displayFormat != IMAGE_FORMAT_UNKNOWN && renderTargetFormat != IMAGE_FORMAT_UNKNOWN );

	switch (depthFormat)
	{
#if defined( _X360 )
	case D3DFMT_D24FS8:
		return D3DFMT_D24FS8;

	case D3DFMT_LIN_D24S8:
		if ( g_bSupportsD24S8 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_LIN_D24S8 ) )
			return D3DFMT_LIN_D24S8;
#endif
	case D3DFMT_D24S8:
		if ( g_bSupportsD24S8 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D24S8 ) )
			return D3DFMT_D24S8;
#if !defined( _X360 )
		if ( g_bSupportsD24X4S4 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D24X4S4 ) )
			return D3DFMT_D24X4S4;
		if ( g_bSupportsD15S1 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D15S1 ) )
			return D3DFMT_D15S1;
#endif
		if ( g_bSupportsD24X8 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D24X8 ) )
			return D3DFMT_D24X8;
		if ( g_bSupportsD16 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D16 ) )
			return D3DFMT_D16;
		break;

	case D3DFMT_D24X8:
		if ( g_bSupportsD24X8 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D24X8 ) )
			return D3DFMT_D24X8;
		if ( g_bSupportsD24S8 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D24S8 ) )
			return D3DFMT_D24S8;
#if !defined( _X360 )
		if ( g_bSupportsD24X4S4 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D24X4S4 ) )
			return D3DFMT_D24X4S4;
#endif
        if ( g_bSupportsD16 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D16 ) )
			return D3DFMT_D16;
#if !defined( _X360 )
		if ( g_bSupportsD15S1 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D15S1 ) )
			return D3DFMT_D15S1;
#endif
		break;

	case D3DFMT_D16:
		if ( g_bSupportsD16 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D16 ) )
			return D3DFMT_D16;
#if !defined( _X360 )
		if ( g_bSupportsD15S1 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D15S1 ) )
			return D3DFMT_D15S1;
#endif
		if ( g_bSupportsD24X8 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D24X8 ) )
			return D3DFMT_D24X8;
		if ( g_bSupportsD24S8 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D24S8 ) )
			return D3DFMT_D24S8;
#if !defined( _X360 )
		if ( g_bSupportsD24X4S4 && IsDepthFormatCompatible( nAdapter, displayFormat, renderTargetFormat, D3DFMT_D24X4S4 ) )
			return D3DFMT_D24X4S4;
#endif
		break;
	}

	Assert( 0 );
	return D3DFMT_D16;
}


//-----------------------------------------------------------------------------
// Is a display buffer valid?
//-----------------------------------------------------------------------------
static inline bool IsFrameBufferFormatValid( UINT displayAdapter, D3DDEVTYPE deviceType, 
	D3DFORMAT displayFormat, D3DFORMAT backBufferFormat, bool bIsWindowed )
{
	HRESULT hr = D3D()->CheckDeviceType( displayAdapter, deviceType, displayFormat, 
                                        backBufferFormat, bIsWindowed );
	return !FAILED(hr);
}


//-----------------------------------------------------------------------------
// Finds the nearest supported frame buffer format
//-----------------------------------------------------------------------------
ImageFormat FindNearestSupportedBackBufferFormat( UINT displayAdapter, 
	  D3DDEVTYPE deviceType, ImageFormat displayFormat, ImageFormat backBufferFormat, bool bIsWindowed )
{
	D3DFORMAT d3dDisplayFormat = ImageLoader::ImageFormatToD3DFormat( displayFormat );
	switch (backBufferFormat)
	{
	case IMAGE_FORMAT_RGBA8888:
	case IMAGE_FORMAT_ABGR8888:
	case IMAGE_FORMAT_ARGB8888:
	case IMAGE_FORMAT_BGRA8888:
	case IMAGE_FORMAT_BGRA4444:		// This is not supported ever; bump up to 32 bit
		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_A8R8G8B8, bIsWindowed ))
			return IMAGE_FORMAT_BGRA8888;

		// Bye, bye dest alpha
		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_X8R8G8B8, bIsWindowed ))
			return IMAGE_FORMAT_BGRX8888;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_R5G6B5, bIsWindowed ))
			return IMAGE_FORMAT_BGR565;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_A1R5G5B5, bIsWindowed ))
			return IMAGE_FORMAT_BGRA5551;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_X1R5G5B5, bIsWindowed ))
			return IMAGE_FORMAT_BGRX5551;

		return IMAGE_FORMAT_UNKNOWN;

	case IMAGE_FORMAT_RGB888:
	case IMAGE_FORMAT_BGR888:
	case IMAGE_FORMAT_RGB888_BLUESCREEN:
	case IMAGE_FORMAT_BGRX8888:
		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_X8R8G8B8, bIsWindowed ))
			return IMAGE_FORMAT_BGRX8888;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_A8R8G8B8, bIsWindowed ))
			return IMAGE_FORMAT_BGRA8888;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_R5G6B5, bIsWindowed ))
			return IMAGE_FORMAT_BGR565;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_A1R5G5B5, bIsWindowed ))
			return IMAGE_FORMAT_BGRA5551;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_X1R5G5B5, bIsWindowed ))
			return IMAGE_FORMAT_BGRX5551;

		return IMAGE_FORMAT_UNKNOWN;

	case IMAGE_FORMAT_RGB565:
	case IMAGE_FORMAT_BGR565:
		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_R5G6B5, bIsWindowed ))
			return IMAGE_FORMAT_BGR565;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_A1R5G5B5, bIsWindowed ))
			return IMAGE_FORMAT_BGRA5551;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_X1R5G5B5, bIsWindowed ))
			return IMAGE_FORMAT_BGRX5551;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_X8R8G8B8, bIsWindowed ))
			return IMAGE_FORMAT_BGRX8888;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_A8R8G8B8, bIsWindowed ))
			return IMAGE_FORMAT_BGRA8888;

		return IMAGE_FORMAT_UNKNOWN;

	case IMAGE_FORMAT_BGRX5551:
		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_X1R5G5B5, bIsWindowed ))
			return IMAGE_FORMAT_BGRX5551;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_A1R5G5B5, bIsWindowed ))
			return IMAGE_FORMAT_BGRA5551;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_R5G6B5, bIsWindowed ))
			return IMAGE_FORMAT_BGR565;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_X8R8G8B8, bIsWindowed ))
			return IMAGE_FORMAT_BGRX8888;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_A8R8G8B8, bIsWindowed ))
			return IMAGE_FORMAT_BGRA8888;

		return IMAGE_FORMAT_UNKNOWN;

	case IMAGE_FORMAT_BGRA5551:
		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_A1R5G5B5, bIsWindowed ))
			return IMAGE_FORMAT_BGRA5551;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_X1R5G5B5, bIsWindowed ))
			return IMAGE_FORMAT_BGRX5551;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_R5G6B5, bIsWindowed ))
			return IMAGE_FORMAT_BGR565;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_A8R8G8B8, bIsWindowed ))
			return IMAGE_FORMAT_BGRA8888;

		if (IsFrameBufferFormatValid( displayAdapter, deviceType, d3dDisplayFormat, D3DFMT_X8R8G8B8, bIsWindowed ))
			return IMAGE_FORMAT_BGRX8888;

		return IMAGE_FORMAT_UNKNOWN;
	}

	return IMAGE_FORMAT_UNKNOWN;
}

#if defined( _X360 )
const char *D3DFormatName( D3DFORMAT d3dFormat )
{
	if ( IS_D3DFORMAT_SRGB( d3dFormat ) )
	{
		// sanitize the format from possible sRGB state for comparison purposes
		d3dFormat = MAKE_NON_SRGB_FMT( d3dFormat );
	}

	switch ( d3dFormat )
	{
	case D3DFMT_A8R8G8B8:
		return "D3DFMT_A8R8G8B8";
	case D3DFMT_LIN_A8R8G8B8:
		return "D3DFMT_LIN_A8R8G8B8";
	case D3DFMT_X8R8G8B8:
		return "D3DFMT_X8R8G8B8";
	case D3DFMT_LIN_X8R8G8B8:
		return "D3DFMT_LIN_X8R8G8B8";
	case D3DFMT_R5G6B5:
		return "D3DFMT_R5G6B5";
	case D3DFMT_X1R5G5B5:
		return "D3DFMT_X1R5G5B5";
	case D3DFMT_A1R5G5B5:
		return "D3DFMT_A1R5G5B5";
	case D3DFMT_A4R4G4B4:
		return "D3DFMT_A4R4G4B4";
	case D3DFMT_L8:
		return "D3DFMT_L8";
	case D3DFMT_A8L8:
		return "D3DFMT_A8L8";
	case D3DFMT_A8:
		return "D3DFMT_A8";
	case D3DFMT_DXT1:
		return "D3DFMT_DXT1";
	case D3DFMT_DXT3:
		return "D3DFMT_DXT3";
	case D3DFMT_DXT5:
		return "D3DFMT_DXT5";
	case D3DFMT_V8U8:
		return "D3DFMT_V8U8";
	case D3DFMT_Q8W8V8U8:
		return "D3DFMT_Q8W8V8U8";
	case D3DFMT_D16:
		return "D3DFMT_D16";
	case D3DFMT_D24S8:
		return "D3DFMT_D24S8";
	case D3DFMT_D24FS8:
		return "D3DFMT_D24FS8";
	case D3DFMT_LIN_D24S8:
		return "D3DFMT_LIN_D24S8";
	case D3DFMT_A16B16G16R16:
		return "D3DFMT_A16B16G16R16";
	case D3DFMT_LIN_A16B16G16R16:
		return "D3DFMT_LIN_A16B16G16R16";
	}
	return "???";
}
#endif