Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

651 lines
22 KiB

  1. //======= Copyright 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #if defined( _WIN32 ) && !defined( _X360 ) && !defined( DX_TO_GL_ABSTRACTION )
  7. #include <windows.h>
  8. #include "../dx9sdk/include/d3d9types.h"
  9. #include "dx11sdk/d3d11.h"
  10. #endif
  11. #include "bitmap/imageformat.h"
  12. #include "basetypes.h"
  13. #include "tier0/dbg.h"
  14. #ifndef _PS3
  15. #include <malloc.h>
  16. #include <memory.h>
  17. #else
  18. #include <stdlib.h>
  19. #endif
  20. #include "nvtc.h"
  21. #include "mathlib/mathlib.h"
  22. #include "mathlib/vector.h"
  23. #include "tier1/utlmemory.h"
  24. #include "tier1/strtools.h"
  25. #include "mathlib/compressed_vector.h"
  26. // Should be last include
  27. #include "tier0/memdbgon.h"
  28. //-----------------------------------------------------------------------------
  29. // Various important function types for each color format
  30. //-----------------------------------------------------------------------------
  31. static ImageFormatInfo_t g_ImageFormatInfo[] =
  32. {
  33. { "UNKNOWN", 0, 0, 0, 0, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_UNKNOWN,
  34. { "RGBA8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_RGBA8888,
  35. { "ABGR8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_ABGR8888,
  36. { "RGB888", 3, 8, 8, 8, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_RGB888,
  37. { "BGR888", 3, 8, 8, 8, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_BGR888,
  38. { "RGB565", 2, 5, 6, 5, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_RGB565,
  39. { "I8", 1, 0, 0, 0, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_I8,
  40. { "IA88", 2, 0, 0, 0, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_IA88
  41. { "P8", 1, 0, 0, 0, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_P8
  42. { "A8", 1, 0, 0, 0, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_A8
  43. { "RGB888_BLUESCREEN", 3, 8, 8, 8, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_RGB888_BLUESCREEN
  44. { "BGR888_BLUESCREEN", 3, 8, 8, 8, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_BGR888_BLUESCREEN
  45. { "ARGB8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_ARGB8888
  46. { "BGRA8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_BGRA8888
  47. { "DXT1", 0, 0, 0, 0, 0, 0, 0, true, false, false }, // IMAGE_FORMAT_DXT1
  48. { "DXT3", 0, 0, 0, 0, 8, 0, 0, true, false, false }, // IMAGE_FORMAT_DXT3
  49. { "DXT5", 0, 0, 0, 0, 8, 0, 0, true, false, false }, // IMAGE_FORMAT_DXT5
  50. { "BGRX8888", 4, 8, 8, 8, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_BGRX8888
  51. { "BGR565", 2, 5, 6, 5, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_BGR565
  52. { "BGRX5551", 2, 5, 5, 5, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_BGRX5551
  53. { "BGRA4444", 2, 4, 4, 4, 4, 0, 0, false, false, false }, // IMAGE_FORMAT_BGRA4444
  54. { "DXT1_ONEBITALPHA", 0, 0, 0, 0, 0, 0, 0, true, false, false }, // IMAGE_FORMAT_DXT1_ONEBITALPHA
  55. { "BGRA5551", 2, 5, 5, 5, 1, 0, 0, false, false, false }, // IMAGE_FORMAT_BGRA5551
  56. { "UV88", 2, 8, 8, 0, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_UV88
  57. { "UVWQ8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_UVWQ8888
  58. { "RGBA16161616F", 8, 16, 16, 16, 16, 0, 0, false, true, false }, // IMAGE_FORMAT_RGBA16161616F
  59. { "RGBA16161616", 8, 16, 16, 16, 16, 0, 0, false, false, false }, // IMAGE_FORMAT_RGBA16161616
  60. { "UVLX8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_UVLX8888
  61. { "R32F", 4, 32, 0, 0, 0, 0, 0, false, true, false }, // IMAGE_FORMAT_R32F
  62. { "RGB323232F", 12, 32, 32, 32, 0, 0, 0, false, true, false }, // IMAGE_FORMAT_RGB323232F
  63. { "RGBA32323232F", 16, 32, 32, 32, 32, 0, 0, false, true, false }, // IMAGE_FORMAT_RGBA32323232F
  64. { "RG1616F", 4, 16, 16, 0, 0, 0, 0, false, true, false }, // IMAGE_FORMAT_RG1616F
  65. { "RG3232F", 8, 32, 32, 0, 0, 0, 0, false, true, false }, // IMAGE_FORMAT_RG3232F
  66. { "RGBX8888", 4, 8, 8, 8, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_RGBX8888
  67. { "NV_NULL", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_NV_NULL
  68. // Vendor-dependent compressed formats typically used for normal map compression
  69. { "ATI1N", 0, 0, 0, 0, 0, 0, 0, true, false, false }, // IMAGE_FORMAT_ATI1N
  70. { "ATI2N", 0, 0, 0, 0, 0, 0, 0, true, false, false }, // IMAGE_FORMAT_ATI2N
  71. { "RGBA1010102", 4, 10, 10, 10, 2, 0, 0, false, false, false }, // IMAGE_FORMAT_RGBA1010102
  72. { "BGRA1010102", 4, 10, 10, 10, 2, 0, 0, false, false, false }, // IMAGE_FORMAT_BGRA1010102
  73. { "R16F", 2, 16, 0, 0, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_R16F
  74. // Vendor-dependent depth formats used for shadow depth mapping
  75. { "D16", 2, 0, 0, 0, 0, 16, 0, false, false, true }, // IMAGE_FORMAT_D16
  76. { "D15S1", 2, 0, 0, 0, 0, 15, 1, false, false, true }, // IMAGE_FORMAT_D15S1
  77. { "D32", 4, 0, 0, 0, 0, 32, 0, false, false, true }, // IMAGE_FORMAT_D32
  78. { "D24S8", 4, 0, 0, 0, 0, 24, 8, false, false, true }, // IMAGE_FORMAT_D24S8
  79. { "LINEAR_D24S8", 4, 0, 0, 0, 0, 24, 8, false, false, true }, // IMAGE_FORMAT_LINEAR_D24S8
  80. { "D24X8", 4, 0, 0, 0, 0, 24, 0, false, false, true }, // IMAGE_FORMAT_D24X8
  81. { "D24X4S4", 4, 0, 0, 0, 0, 24, 4, false, false, true }, // IMAGE_FORMAT_D24X4S4
  82. { "D24FS8", 4, 0, 0, 0, 0, 24, 8, false, false, true }, // IMAGE_FORMAT_D24FS8
  83. { "D16_SHADOW", 2, 0, 0, 0, 0, 16, 0, false, false, true }, // IMAGE_FORMAT_D16_SHADOW
  84. { "D24X8_SHADOW", 4, 0, 0, 0, 0, 24, 0, false, false, true }, // IMAGE_FORMAT_D24X8_SHADOW
  85. { "LINEAR_BGRX8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_LINEAR_BGRX8888
  86. { "LINEAR_RGBA8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_LINEAR_RGBA8888
  87. { "LINEAR_ABGR8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_LINEAR_ABGR8888
  88. { "LINEAR_ARGB8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_LINEAR_ARGB8888
  89. { "LINEAR_BGRA8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_LINEAR_BGRA8888
  90. { "LINEAR_RGB888", 3, 8, 8, 8, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_LINEAR_RGB888
  91. { "LINEAR_BGR888", 3, 8, 8, 8, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_LINEAR_BGR888
  92. { "LINEAR_BGRX5551", 2, 5, 5, 5, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_LINEAR_BGRX5551
  93. { "LINEAR_I8", 1, 0, 0, 0, 0, 0, 0, false, false, false }, // IMAGE_FORMAT_LINEAR_I8
  94. { "LINEAR_RGBA16161616", 8, 16, 16, 16, 16, 0, 0, false, false, false }, // IMAGE_FORMAT_LINEAR_RGBA16161616
  95. { "LINEAR_A8", 1, 0, 0, 0, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_LINEAR_A8
  96. { "LINEAR_DXT1", 0, 0, 0, 0, 0, 0, 0, true, false, false }, // IMAGE_FORMAT_LINEAR_DXT1
  97. { "LINEAR_DXT3", 0, 0, 0, 0, 8, 0, 0, true, false, false }, // IMAGE_FORMAT_LINEAR_DXT3
  98. { "LINEAR_DXT5", 0, 0, 0, 0, 8, 0, 0, true, false, false }, // IMAGE_FORMAT_LINEAR_DXT5
  99. { "LE_BGRX8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_LE_BGRX8888
  100. { "LE_BGRA8888", 4, 8, 8, 8, 8, 0, 0, false, false, false }, // IMAGE_FORMAT_LE_BGRA8888
  101. { "DXT1_RUNTIME", 0, 0, 0, 0, 0, 0, 0, true, false, false }, // IMAGE_FORMAT_DXT1_RUNTIME
  102. { "DXT5_RUNTIME", 0, 0, 0, 0, 8, 0, 0, true, false, false }, // IMAGE_FORMAT_DXT5_RUNTIME
  103. // Vendor-dependent depth formats used for resolving
  104. { "INTZ", 4, 0, 0, 0, 0, 24, 8, false, false, true}, // IMAGE_FORMAT_INTZ
  105. };
  106. namespace ImageLoader
  107. {
  108. //-----------------------------------------------------------------------------
  109. // Returns info about each image format
  110. //-----------------------------------------------------------------------------
  111. const ImageFormatInfo_t& ImageFormatInfo( ImageFormat fmt )
  112. {
  113. COMPILE_TIME_ASSERT( ( NUM_IMAGE_FORMATS + 1 ) == ARRAYSIZE( g_ImageFormatInfo ) );
  114. Assert( unsigned( fmt + 1 ) <= ( NUM_IMAGE_FORMATS ) );
  115. return g_ImageFormatInfo[ fmt + 1 ];
  116. }
  117. int GetMemRequired( int width, int height, int depth, int nMipmapCount, ImageFormat imageFormat, int *pAdjustedHeight )
  118. {
  119. depth = MAX( 1, depth );
  120. int nRet = 0;
  121. if ( nMipmapCount == 1 )
  122. {
  123. // Block compressed formats
  124. const ImageFormatInfo_t &fmt = ImageFormatInfo( imageFormat );
  125. if ( fmt.m_bIsCompressed )
  126. {
  127. Assert( ( width < 4 ) || !( width % 4 ) );
  128. Assert( ( height < 4 ) || !( height % 4 ) );
  129. Assert( ( depth < 4 ) || !( depth % 4 ) );
  130. if ( width < 4 && width > 0 )
  131. {
  132. width = 4;
  133. }
  134. if ( height < 4 && height > 0 )
  135. {
  136. height = 4;
  137. }
  138. if ( depth < 4 && depth > 1 )
  139. {
  140. depth = 4;
  141. }
  142. width >>= 2;
  143. height >>= 2;
  144. int numBlocks = width * height * depth;
  145. switch ( imageFormat )
  146. {
  147. case IMAGE_FORMAT_DXT1:
  148. case IMAGE_FORMAT_DXT1_RUNTIME:
  149. case IMAGE_FORMAT_LINEAR_DXT1:
  150. case IMAGE_FORMAT_ATI1N:
  151. nRet = numBlocks * 8;
  152. break;
  153. case IMAGE_FORMAT_DXT3:
  154. case IMAGE_FORMAT_DXT5:
  155. case IMAGE_FORMAT_DXT5_RUNTIME:
  156. case IMAGE_FORMAT_LINEAR_DXT3:
  157. case IMAGE_FORMAT_LINEAR_DXT5:
  158. case IMAGE_FORMAT_ATI2N:
  159. nRet = numBlocks * 16;
  160. break;
  161. }
  162. }
  163. else
  164. {
  165. nRet = width * height * depth * fmt.m_nNumBytes;
  166. }
  167. if ( pAdjustedHeight )
  168. {
  169. *pAdjustedHeight = height;
  170. }
  171. return nRet;
  172. }
  173. // Mipmap version
  174. int memSize = 0;
  175. // Not sensical for mip chains
  176. if ( pAdjustedHeight )
  177. {
  178. *pAdjustedHeight = 0;
  179. }
  180. while ( true )
  181. {
  182. memSize += GetMemRequired( width, height, depth, imageFormat, false );
  183. if ( width == 1 && height == 1 && depth == 1 )
  184. break;
  185. width >>= 1;
  186. height >>= 1;
  187. depth >>= 1;
  188. if ( width < 1 )
  189. {
  190. width = 1;
  191. }
  192. if ( height < 1 )
  193. {
  194. height = 1;
  195. }
  196. if ( depth < 1 )
  197. {
  198. depth = 1;
  199. }
  200. if ( nMipmapCount )
  201. {
  202. if ( --nMipmapCount == 0 )
  203. break;
  204. }
  205. }
  206. return memSize;
  207. }
  208. int GetMemRequired( int width, int height, int depth, ImageFormat imageFormat, bool mipmap, int *pAdjustedHeight )
  209. {
  210. return GetMemRequired( width, height, depth, mipmap ? 0 : 1, imageFormat, pAdjustedHeight );
  211. }
  212. int GetMipMapLevelByteOffset( int width, int height, ImageFormat imageFormat, int skipMipLevels, int nDepth )
  213. {
  214. int offset = 0;
  215. while( skipMipLevels > 0 )
  216. {
  217. offset += GetMemRequired( width, height, nDepth, 1, imageFormat );
  218. if( width == 1 && height == 1 && nDepth == 1 )
  219. {
  220. break;
  221. }
  222. width = MAX( 1, width >> 1 );
  223. height = MAX( 1, height >> 1 );
  224. nDepth = MAX( 1, nDepth >> 1 );
  225. skipMipLevels--;
  226. }
  227. return offset;
  228. }
  229. //-----------------------------------------------------------------------------
  230. // This version is for mipmaps which are stored smallest level to largest level in memory
  231. //-----------------------------------------------------------------------------
  232. int GetMipMapLevelByteOffsetReverse( int nWidth, int nHeight, int nDepth, int nTotalMipCount, ImageFormat imageFormat, int nMipLevel )
  233. {
  234. if ( nTotalMipCount == 1 )
  235. return 0;
  236. int nSkipSize = 0;
  237. for ( int i = 0; i < nTotalMipCount; ++i )
  238. {
  239. int nMipSize = GetMemRequired( nWidth, nHeight, nDepth, 1, imageFormat );
  240. if ( i > nMipLevel )
  241. {
  242. nSkipSize += nMipSize;
  243. }
  244. if( nWidth == 1 && nHeight == 1 && nDepth == 1 )
  245. break;
  246. nWidth = MAX( 1, nWidth >> 1 );
  247. nHeight = MAX( 1, nHeight >> 1 );
  248. nDepth = MAX( 1, nDepth >> 1 );
  249. }
  250. return nSkipSize;
  251. }
  252. void GetMipMapLevelDimensions( int *width, int *height, int skipMipLevels )
  253. {
  254. while( skipMipLevels > 0 )
  255. {
  256. if( *width == 1 && *height == 1 )
  257. {
  258. break;
  259. }
  260. *width >>= 1;
  261. *height >>= 1;
  262. if( *width < 1 )
  263. {
  264. *width = 1;
  265. }
  266. if( *height < 1 )
  267. {
  268. *height = 1;
  269. }
  270. skipMipLevels--;
  271. }
  272. }
  273. void GetMipMapLevelDimensions( int &nWidth, int &nHeight, int &nDepth, int nMipLevel )
  274. {
  275. for( ; nMipLevel > 0; --nMipLevel )
  276. {
  277. if( nWidth <= 1 && nHeight <= 1 && nDepth <= 1 )
  278. break;
  279. nWidth >>= 1;
  280. nHeight >>= 1;
  281. nDepth >>= 1;
  282. }
  283. nWidth = MAX( nWidth, 1 );
  284. nHeight = MAX( nHeight, 1 );
  285. nDepth = MAX( nDepth, 1 );
  286. }
  287. int GetNumMipMapLevels( int width, int height, int depth )
  288. {
  289. if ( depth <= 0 )
  290. {
  291. depth = 1;
  292. }
  293. if( width < 1 || height < 1 || depth < 1 )
  294. return 0;
  295. int numMipLevels = 1;
  296. while( 1 )
  297. {
  298. if( width == 1 && height == 1 && depth == 1 )
  299. break;
  300. width >>= 1;
  301. height >>= 1;
  302. depth >>= 1;
  303. if( width < 1 )
  304. {
  305. width = 1;
  306. }
  307. if( height < 1 )
  308. {
  309. height = 1;
  310. }
  311. if( depth < 1 )
  312. {
  313. depth = 1;
  314. }
  315. numMipLevels++;
  316. }
  317. return numMipLevels;
  318. }
  319. // Turn off warning about FOURCC formats below...
  320. #pragma warning (disable:4063)
  321. #ifdef POSIX
  322. #ifndef MAKEFOURCC
  323. #define MAKEFOURCC(ch0, ch1, ch2, ch3) \
  324. ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
  325. ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24 ))
  326. #endif //defined(MAKEFOURCC)
  327. #endif
  328. //-----------------------------------------------------------------------------
  329. // convert back and forth from D3D format to ImageFormat, regardless of
  330. // whether it's supported or not
  331. //-----------------------------------------------------------------------------
  332. ImageFormat D3DFormatToImageFormat( D3DFORMAT format )
  333. {
  334. #if defined( PLATFORM_X360 )
  335. if ( IS_D3DFORMAT_SRGB( format ) )
  336. {
  337. // sanitize the format from possible sRGB state for comparison purposes
  338. format = MAKE_NON_SRGB_FMT( format );
  339. }
  340. #endif
  341. switch ( format )
  342. {
  343. #if !defined( PLATFORM_X360 )
  344. case D3DFMT_R8G8B8:
  345. return IMAGE_FORMAT_BGR888;
  346. #endif
  347. #ifndef POSIX
  348. case D3DFMT_A8B8G8R8:
  349. return IMAGE_FORMAT_RGBA8888;
  350. case D3DFMT_X8B8G8R8:
  351. return IMAGE_FORMAT_RGBX8888;
  352. #endif // !POSIX
  353. case D3DFMT_A8R8G8B8:
  354. return IMAGE_FORMAT_BGRA8888;
  355. case D3DFMT_X8R8G8B8:
  356. return IMAGE_FORMAT_BGRX8888;
  357. case D3DFMT_R5G6B5:
  358. return IMAGE_FORMAT_BGR565;
  359. case D3DFMT_X1R5G5B5:
  360. return IMAGE_FORMAT_BGRX5551;
  361. case D3DFMT_A1R5G5B5:
  362. return IMAGE_FORMAT_BGRA5551;
  363. case D3DFMT_A4R4G4B4:
  364. return IMAGE_FORMAT_BGRA4444;
  365. #if !defined( PLATFORM_X360 ) && !defined( POSIX )
  366. case D3DFMT_P8:
  367. return IMAGE_FORMAT_P8;
  368. #endif
  369. case D3DFMT_L8:
  370. return IMAGE_FORMAT_I8;
  371. case D3DFMT_A8L8:
  372. return IMAGE_FORMAT_IA88;
  373. case D3DFMT_A8:
  374. return IMAGE_FORMAT_A8;
  375. case D3DFMT_DXT1:
  376. return IMAGE_FORMAT_DXT1;
  377. case D3DFMT_DXT3:
  378. return IMAGE_FORMAT_DXT3;
  379. case D3DFMT_DXT5:
  380. return IMAGE_FORMAT_DXT5;
  381. case D3DFMT_V8U8:
  382. return IMAGE_FORMAT_UV88;
  383. case D3DFMT_Q8W8V8U8:
  384. return IMAGE_FORMAT_UVWQ8888;
  385. case D3DFMT_X8L8V8U8:
  386. return IMAGE_FORMAT_UVLX8888;
  387. case D3DFMT_A16B16G16R16F:
  388. return IMAGE_FORMAT_RGBA16161616F;
  389. case D3DFMT_A16B16G16R16:
  390. return IMAGE_FORMAT_RGBA16161616;
  391. case D3DFMT_R32F:
  392. return IMAGE_FORMAT_R32F;
  393. case D3DFMT_A32B32G32R32F:
  394. return IMAGE_FORMAT_RGBA32323232F;
  395. case (D3DFORMAT)(MAKEFOURCC('N','U','L','L')):
  396. return IMAGE_FORMAT_NULL;
  397. case D3DFMT_D16:
  398. return IMAGE_FORMAT_D16;
  399. #ifndef POSIX
  400. case D3DFMT_G16R16F:
  401. return IMAGE_FORMAT_RG1616F;
  402. case D3DFMT_G32R32F:
  403. return IMAGE_FORMAT_RG3232F;
  404. #endif // !POSIX
  405. case D3DFMT_D24S8:
  406. return IMAGE_FORMAT_D24S8;
  407. case (D3DFORMAT)(MAKEFOURCC('A','T','I','1')):
  408. return IMAGE_FORMAT_ATI1N;
  409. case (D3DFORMAT)(MAKEFOURCC('A','T','I','2')):
  410. return IMAGE_FORMAT_ATI2N;
  411. #ifndef POSIX
  412. case D3DFMT_A2B10G10R10:
  413. return IMAGE_FORMAT_RGBA1010102;
  414. case D3DFMT_A2R10G10B10:
  415. return IMAGE_FORMAT_BGRA1010102;
  416. case D3DFMT_R16F:
  417. return IMAGE_FORMAT_R16F;
  418. case D3DFMT_D32:
  419. return IMAGE_FORMAT_D32;
  420. #endif // !POSIX
  421. case D3DFMT_D24X8:
  422. return IMAGE_FORMAT_D24X8;
  423. #ifndef PLATFORM_X360
  424. case D3DFMT_D15S1:
  425. return IMAGE_FORMAT_D15S1;
  426. case D3DFMT_D24X4S4:
  427. return IMAGE_FORMAT_D24X4S4;
  428. #endif
  429. case D3DFMT_UNKNOWN:
  430. return IMAGE_FORMAT_UNKNOWN;
  431. #ifdef PLATFORM_X360
  432. case D3DFMT_LIN_A8R8G8B8:
  433. return IMAGE_FORMAT_LINEAR_BGRA8888;
  434. case D3DFMT_LIN_A8B8G8R8:
  435. return IMAGE_FORMAT_LINEAR_RGBA8888;
  436. case D3DFMT_LIN_X8R8G8B8:
  437. return IMAGE_FORMAT_LINEAR_BGRX8888;
  438. case D3DFMT_LIN_X1R5G5B5:
  439. return IMAGE_FORMAT_LINEAR_BGRX5551;
  440. case D3DFMT_LIN_L8:
  441. return IMAGE_FORMAT_LINEAR_I8;
  442. case D3DFMT_LIN_A16B16G16R16:
  443. return IMAGE_FORMAT_LINEAR_RGBA16161616;
  444. case D3DFMT_LE_X8R8G8B8:
  445. return IMAGE_FORMAT_LE_BGRX8888;
  446. case D3DFMT_LE_A8R8G8B8:
  447. return IMAGE_FORMAT_LE_BGRA8888;
  448. case D3DFMT_LIN_D24S8:
  449. return IMAGE_FORMAT_LINEAR_D24S8;
  450. case D3DFMT_LIN_A8:
  451. return IMAGE_FORMAT_LINEAR_A8;
  452. case D3DFMT_LIN_DXT1:
  453. return IMAGE_FORMAT_LINEAR_DXT1;
  454. case D3DFMT_LIN_DXT3:
  455. return IMAGE_FORMAT_LINEAR_DXT3;
  456. case D3DFMT_LIN_DXT5:
  457. return IMAGE_FORMAT_LINEAR_DXT5;
  458. #endif
  459. #if !defined( _PS3 )
  460. case D3DFMT_D24FS8:
  461. return IMAGE_FORMAT_D24FS8;
  462. #endif // !_PS3
  463. }
  464. return IMAGE_FORMAT_UNKNOWN;
  465. }
  466. #ifdef _PS3
  467. // Stub out some formats that don't have direct analgoues on PS3 or that we haven't yet mapped
  468. #define D3DFMT_A8B8G8R8 D3DFMT_UNKNOWN
  469. #define D3DFMT_P8 D3DFMT_UNKNOWN
  470. #define D3DFMT_G16R16F D3DFMT_UNKNOWN
  471. #define D3DFMT_G32R32F D3DFMT_UNKNOWN
  472. #define D3DFMT_X8B8G8R8 D3DFMT_UNKNOWN
  473. #define D3DFMT_A2B10G10R10 D3DFMT_UNKNOWN
  474. #define D3DFMT_A2R10G10B10 D3DFMT_UNKNOWN
  475. #define D3DFMT_R16F D3DFMT_UNKNOWN
  476. #define D3DFMT_D32 D3DFMT_UNKNOWN
  477. #define D3DFMT_D24FS8 D3DFMT_UNKNOWN
  478. #endif // _PS3
  479. // A format exists in here only if there is a direct mapping
  480. static D3DFORMAT s_pD3DFormats[] =
  481. {
  482. D3DFMT_UNKNOWN, // IMAGE_FORMAT_UNKNOWN,
  483. D3DFMT_A8B8G8R8, // IMAGE_FORMAT_RGBA8888,
  484. D3DFMT_UNKNOWN, // IMAGE_FORMAT_ABGR8888,
  485. D3DFMT_UNKNOWN, // IMAGE_FORMAT_RGB888,
  486. #if !defined( PLATFORM_X360 )
  487. D3DFMT_R8G8B8, // IMAGE_FORMAT_BGR888
  488. #else
  489. D3DFMT_UNKNOWN, // IMAGE_FORMAT_BGR888
  490. #endif
  491. D3DFMT_UNKNOWN, // IMAGE_FORMAT_RGB565,
  492. D3DFMT_L8, // IMAGE_FORMAT_I8,
  493. D3DFMT_A8L8, // IMAGE_FORMAT_IA88,
  494. #ifndef PLATFORM_X360
  495. D3DFMT_P8, // IMAGE_FORMAT_P8,
  496. #else
  497. D3DFMT_UNKNOWN, // IMAGE_FORMAT_P8,
  498. #endif
  499. D3DFMT_A8, // IMAGE_FORMAT_A8,
  500. D3DFMT_UNKNOWN, // IMAGE_FORMAT_RGB888_BLUESCREEN,
  501. D3DFMT_UNKNOWN, // IMAGE_FORMAT_BGR888_BLUESCREEN,
  502. D3DFMT_UNKNOWN, // IMAGE_FORMAT_ARGB8888,
  503. D3DFMT_A8R8G8B8, // IMAGE_FORMAT_BGRA8888,
  504. D3DFMT_DXT1, // IMAGE_FORMAT_DXT1,
  505. D3DFMT_DXT3, // IMAGE_FORMAT_DXT3,
  506. D3DFMT_DXT5, // IMAGE_FORMAT_DXT5,
  507. D3DFMT_X8R8G8B8, // IMAGE_FORMAT_BGRX8888,
  508. D3DFMT_R5G6B5, // IMAGE_FORMAT_BGR565,
  509. D3DFMT_X1R5G5B5, // IMAGE_FORMAT_BGRX5551,
  510. D3DFMT_A4R4G4B4, // IMAGE_FORMAT_BGRA4444,
  511. D3DFMT_UNKNOWN, // IMAGE_FORMAT_DXT1_ONEBITALPHA,
  512. D3DFMT_A1R5G5B5, // IMAGE_FORMAT_BGRA5551,
  513. D3DFMT_V8U8, // IMAGE_FORMAT_UV88,
  514. D3DFMT_Q8W8V8U8, // IMAGE_FORMAT_UVWQ8888,
  515. D3DFMT_A16B16G16R16F, // IMAGE_FORMAT_RGBA16161616F,
  516. D3DFMT_A16B16G16R16, // IMAGE_FORMAT_RGBA16161616,
  517. D3DFMT_X8L8V8U8, // IMAGE_FORMAT_UVLX8888,
  518. D3DFMT_R32F, // IMAGE_FORMAT_R32F,
  519. D3DFMT_UNKNOWN, // IMAGE_FORMAT_RGB323232F,
  520. D3DFMT_A32B32G32R32F, // IMAGE_FORMAT_RGBA32323232F,
  521. D3DFMT_G16R16F, // IMAGE_FORMAT_RG1616F,
  522. D3DFMT_G32R32F, // IMAGE_FORMAT_RG3232F,
  523. D3DFMT_X8B8G8R8, // IMAGE_FORMAT_RGBX8888,
  524. (D3DFORMAT)(MAKEFOURCC('N','U','L','L')), // IMAGE_FORMAT_NULL,
  525. (D3DFORMAT)(MAKEFOURCC('A','T','I','2')), // IMAGE_FORMAT_ATI2N,
  526. (D3DFORMAT)(MAKEFOURCC('A','T','I','1')), // IMAGE_FORMAT_ATI1N,
  527. D3DFMT_A2B10G10R10, // IMAGE_FORMAT_RGBA1010102,
  528. D3DFMT_A2R10G10B10, // IMAGE_FORMAT_BGRA1010102,
  529. D3DFMT_R16F, // IMAGE_FORMAT_R16F,
  530. D3DFMT_D16, // IMAGE_FORMAT_D16,
  531. #ifndef PLATFORM_X360
  532. D3DFMT_D15S1, // IMAGE_FORMAT_D15S1,
  533. D3DFMT_D32, // IMAGE_FORMAT_D32,
  534. D3DFMT_D24S8, // IMAGE_FORMAT_D24S8,
  535. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_D24S8,
  536. D3DFMT_D24X8, // IMAGE_FORMAT_D24X8,
  537. D3DFMT_D24X4S4, // IMAGE_FORMAT_D24X4S4,
  538. D3DFMT_D24FS8, // IMAGE_FORMAT_D24FS8,
  539. D3DFMT_UNKNOWN, // IMAGE_FORMAT_D16_SHADOW,
  540. D3DFMT_UNKNOWN, // IMAGE_FORMAT_D24S8_SHADOW,
  541. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_BGRX8888,
  542. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_RGBA8888,
  543. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_ABGR8888,
  544. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_ARGB8888,
  545. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_BGRA8888,
  546. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_RGB888,
  547. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_BGR888,
  548. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_BGRX5551,
  549. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_I8,
  550. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_RGBA16161616,
  551. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_A8,
  552. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_DXT1,
  553. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_DXT3,
  554. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_DXT5,
  555. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LE_BGRX8888,
  556. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LE_BGRA8888,
  557. D3DFMT_DXT1, // IMAGE_FORMAT_DXT5_RUNTIME,
  558. D3DFMT_DXT5, // IMAGE_FORMAT_DXT5_RUNTIME,
  559. (D3DFORMAT)(MAKEFOURCC('I','N','T','Z')), // IMAGE_FORMAT_INTZ,
  560. #else
  561. D3DFMT_UNKNOWN, // IMAGE_FORMAT_D15S1,
  562. D3DFMT_D32, // IMAGE_FORMAT_D32,
  563. D3DFMT_D24S8, // IMAGE_FORMAT_D24S8,
  564. D3DFMT_LIN_D24S8, // IMAGE_FORMAT_LINEAR_D24S8,
  565. D3DFMT_D24X8, // IMAGE_FORMAT_D24X8,
  566. D3DFMT_UNKNOWN, // IMAGE_FORMAT_D24X4S4,
  567. D3DFMT_D24FS8, // IMAGE_FORMAT_D24FS8,
  568. D3DFMT_UNKNOWN, // IMAGE_FORMAT_D16_SHADOW,
  569. D3DFMT_UNKNOWN, // IMAGE_FORMAT_D24S8_SHADOW,
  570. D3DFMT_LIN_X8R8G8B8, // IMAGE_FORMAT_LINEAR_BGRX8888,
  571. D3DFMT_LIN_A8B8G8R8, // IMAGE_FORMAT_LINEAR_RGBA8888,
  572. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_ABGR8888,
  573. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_ARGB8888,
  574. D3DFMT_LIN_A8R8G8B8, // IMAGE_FORMAT_LINEAR_BGRA8888,
  575. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_RGB888,
  576. D3DFMT_UNKNOWN, // IMAGE_FORMAT_LINEAR_BGR888,
  577. D3DFMT_LIN_X1R5G5B5, // IMAGE_FORMAT_LINEAR_BGRX5551,
  578. D3DFMT_LIN_L8, // IMAGE_FORMAT_LINEAR_I8,
  579. D3DFMT_LIN_A16B16G16R16, // IMAGE_FORMAT_LINEAR_RGBA16161616,
  580. D3DFMT_LIN_A8, // IMAGE_FORMAT_LINEAR_A8
  581. D3DFMT_LIN_DXT1, // IMAGE_FORMAT_LINEAR_DXT1,
  582. D3DFMT_LIN_DXT3, // IMAGE_FORMAT_LINEAR_DXT3,
  583. D3DFMT_LIN_DXT5, // IMAGE_FORMAT_LINEAR_DXT5,
  584. D3DFMT_LE_X8R8G8B8, // IMAGE_FORMAT_LE_BGRX8888,
  585. D3DFMT_LE_A8R8G8B8, // IMAGE_FORMAT_LE_BGRA8888,
  586. D3DFMT_DXT1, // IMAGE_FORMAT_DXT5_RUNTIME,
  587. D3DFMT_DXT5, // IMAGE_FORMAT_DXT5_RUNTIME,
  588. #endif
  589. };
  590. D3DFORMAT ImageFormatToD3DFormat( ImageFormat format )
  591. {
  592. COMPILE_TIME_ASSERT( ARRAYSIZE( s_pD3DFormats ) == NUM_IMAGE_FORMATS + 1 );
  593. return s_pD3DFormats[ format + 1 ];
  594. }
  595. #pragma warning (default:4063)
  596. } // ImageLoader namespace ends