ww3dformat.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/ww3dformat.cpp $*
  25. * *
  26. * Original Author:: Hector Yee *
  27. * *
  28. * Author : Kenny Mitchell *
  29. * *
  30. * $Modtime:: 06/27/02 1:27p $*
  31. * *
  32. * $Revision:: 14 $*
  33. * *
  34. * 06/27/02 KM Z Format support *
  35. *---------------------------------------------------------------------------------------------*
  36. * Functions: *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "ww3dformat.h"
  39. #include "vector4.h"
  40. #include "wwdebug.h"
  41. #include "targa.h"
  42. #include "dx8wrapper.h"
  43. #include "dx8caps.h"
  44. #include <d3d8.h>
  45. /*
  46. WW3D_FORMAT_UNKNOWN=0,
  47. WW3D_FORMAT_R8G8B8,
  48. WW3D_FORMAT_A8R8G8B8,
  49. WW3D_FORMAT_X8R8G8B8,
  50. WW3D_FORMAT_R5G6B5,
  51. WW3D_FORMAT_X1R5G5B5,
  52. WW3D_FORMAT_A1R5G5B5,
  53. WW3D_FORMAT_A4R4G4B4,
  54. WW3D_FORMAT_R3G3B2,
  55. WW3D_FORMAT_A8,
  56. WW3D_FORMAT_A8R3G3B2,
  57. WW3D_FORMAT_X4R4G4B4,
  58. WW3D_FORMAT_A8P8,
  59. WW3D_FORMAT_P8,
  60. WW3D_FORMAT_L8,
  61. WW3D_FORMAT_A8L8,
  62. WW3D_FORMAT_A4L4,
  63. WW3D_FORMAT_COUNT // Used only to determine number of surface formats
  64. */
  65. void Get_WW3D_Format_Name(WW3DFormat format, StringClass& name)
  66. {
  67. switch (format) {
  68. default:
  69. case WW3D_FORMAT_UNKNOWN: name="Unknown"; break;
  70. case WW3D_FORMAT_R8G8B8: name="R8G8B8"; break;
  71. case WW3D_FORMAT_A8R8G8B8: name="A8R8G8B8"; break;
  72. case WW3D_FORMAT_X8R8G8B8: name="X8R8G8B8"; break;
  73. case WW3D_FORMAT_R5G6B5: name="R5G6B5"; break;
  74. case WW3D_FORMAT_X1R5G5B5: name="X1R5G5B5"; break;
  75. case WW3D_FORMAT_A1R5G5B5: name="A1R5G5B5"; break;
  76. case WW3D_FORMAT_A4R4G4B4: name="A4R4G4B4"; break;
  77. case WW3D_FORMAT_R3G3B2: name="R3G3B2"; break;
  78. case WW3D_FORMAT_A8: name="A8"; break;
  79. case WW3D_FORMAT_A8R3G3B2: name="A8R3G3B2"; break;
  80. case WW3D_FORMAT_X4R4G4B4: name="X4R4G4B4"; break;
  81. case WW3D_FORMAT_A8P8: name="A8P8"; break;
  82. case WW3D_FORMAT_P8: name="P8"; break;
  83. case WW3D_FORMAT_L8: name="L8"; break;
  84. case WW3D_FORMAT_A8L8: name="A8L8"; break;
  85. case WW3D_FORMAT_A4L4: name="A4L4"; break;
  86. case WW3D_FORMAT_U8V8: name="U8V8"; break; // Bumpmap
  87. case WW3D_FORMAT_L6V5U5: name="L6V5U5"; break; // Bumpmap
  88. case WW3D_FORMAT_X8L8V8U8: name="X8L8V8U8"; break; // Bumpmap
  89. case WW3D_FORMAT_DXT1: name="DXT1"; break;
  90. case WW3D_FORMAT_DXT2: name="DXT2"; break;
  91. case WW3D_FORMAT_DXT3: name="DXT3"; break;
  92. case WW3D_FORMAT_DXT4: name="DXT4"; break;
  93. case WW3D_FORMAT_DXT5: name="DXT5"; break;
  94. }
  95. }
  96. //**********************************************************************************************
  97. //! Get W3D depth stencil format string name
  98. /*! 06/27/02 KJM
  99. */
  100. void Get_WW3D_ZFormat_Name(WW3DZFormat format, StringClass& name)
  101. {
  102. switch (format)
  103. {
  104. default:
  105. case WW3D_FORMAT_UNKNOWN : name="Unknown"; break;
  106. case WW3D_ZFORMAT_D16_LOCKABLE: name="D16Lockable"; break; // 16-bit z-buffer bit depth. This is an application-lockable surface format.
  107. case WW3D_ZFORMAT_D32 : name="D32"; break; // 32-bit z-buffer bit depth.
  108. case WW3D_ZFORMAT_D15S1 : name="D15S1"; break; // 16-bit z-buffer bit depth where 15 bits are reserved for the depth channel and 1 bit is reserved for the stencil channel.
  109. case WW3D_ZFORMAT_D24S8 : name="D24S8"; break; // 32-bit z-buffer bit depth using 24 bits for the depth channel and 8 bits for the stencil channel.
  110. case WW3D_ZFORMAT_D16 : name="D16"; break; // 16-bit z-buffer bit depth.
  111. case WW3D_ZFORMAT_D24X8 : name="D24X8"; break; // 32-bit z-buffer bit depth using 24 bits for the depth channel.
  112. case WW3D_ZFORMAT_D24X4S4 : name="D24X4S4"; break; // 32-bit z-buffer bit depth using 24 bits for the depth channel and 4 bits for the stencil channel.
  113. #ifdef _XBOX
  114. case WW3D_ZFORMAT_LIN_D24S8 : name="D24S8LIN"; break;
  115. case WW3D_ZFORMAT_LIN_F24S8 : name="F24S8LIN"; break;
  116. case WW3D_ZFORMAT_LIN_D16 : name="D16LIN"; break;
  117. case WW3D_ZFORMAT_LIN_F16 : name="F16LIN"; break;
  118. #endif
  119. }
  120. }
  121. // extract the luminance from the RGB using the CIE 709 standard
  122. unsigned char RGB_to_CIEY(Vector4 color)
  123. {
  124. float lum=0.2126f*color.X + 0.7152f*color.Y + 0.0722f*color.Z;
  125. return (unsigned char) (255.0f*lum);
  126. }
  127. void Vector4_to_Color(unsigned int *outc,const Vector4 &inc,const WW3DFormat format)
  128. {
  129. // convert to ARGB 32-bit
  130. unsigned int color=DX8Wrapper::Convert_Color(inc);
  131. unsigned char *argb=(unsigned char*) &color;
  132. unsigned char r,g,b,a,lum;
  133. switch (format)
  134. {
  135. case WW3D_FORMAT_R8G8B8:
  136. case WW3D_FORMAT_A8R8G8B8:
  137. case WW3D_FORMAT_X8R8G8B8:
  138. *outc=color;
  139. break;
  140. case WW3D_FORMAT_R5G6B5:
  141. r=argb[1] >> 3;
  142. g=argb[2] >> 2;
  143. b=argb[3] >> 3;
  144. *outc=(r << 11) | (g<<5) | b;
  145. break;
  146. case WW3D_FORMAT_X1R5G5B5:
  147. case WW3D_FORMAT_A1R5G5B5:
  148. a=argb[0] >> 7;
  149. r=argb[1] >> 3;
  150. g=argb[2] >> 3;
  151. b=argb[3] >> 3;
  152. *outc=(a<<15) | (r<<10) | (g<<5) | b;
  153. break;
  154. case WW3D_FORMAT_A4R4G4B4:
  155. case WW3D_FORMAT_X4R4G4B4:
  156. a=argb[0] >> 4;
  157. r=argb[1] >> 4;
  158. g=argb[2] >> 4;
  159. b=argb[3] >> 4;
  160. *outc=(a<<12) | (r<<8) | (g<<4) | b;
  161. break;
  162. case WW3D_FORMAT_R3G3B2:
  163. case WW3D_FORMAT_A8R3G3B2:
  164. a=argb[0];
  165. r=argb[1] >> 5;
  166. g=argb[2] >> 5;
  167. b=argb[3] >> 6;
  168. *outc=(a<<8) | (r<<5) | (g<<2) | b;
  169. break;
  170. case WW3D_FORMAT_A8:
  171. *outc=argb[0];
  172. break;
  173. case WW3D_FORMAT_L8:
  174. lum=RGB_to_CIEY(inc);
  175. *outc=lum;
  176. break;
  177. case WW3D_FORMAT_A8L8:
  178. a=argb[0];
  179. lum=RGB_to_CIEY(inc);
  180. *outc=(a<<8) | lum;
  181. break;
  182. case WW3D_FORMAT_A4L4:
  183. a=argb[0] >> 4;
  184. lum=RGB_to_CIEY(inc);
  185. lum=lum>>4;
  186. *outc=(a<<4) | lum;
  187. break;
  188. default:
  189. WWASSERT(0);
  190. }
  191. }
  192. void Color_to_Vector4(Vector4* outc,const unsigned int inc,const WW3DFormat format)
  193. {
  194. WWASSERT(outc);
  195. unsigned char *argb=(unsigned char*) &inc;
  196. unsigned char a,r,g,b;
  197. a=r=g=b=0;
  198. switch (format)
  199. {
  200. case WW3D_FORMAT_R8G8B8:
  201. case WW3D_FORMAT_A8R8G8B8:
  202. case WW3D_FORMAT_X8R8G8B8:
  203. a=argb[0];
  204. r=argb[1];
  205. g=argb[2];
  206. b=argb[3];
  207. break;
  208. case WW3D_FORMAT_R5G6B5:
  209. r=argb[1]<<3;
  210. g=argb[2]<<2;
  211. b=argb[3]<<3;
  212. break;
  213. case WW3D_FORMAT_X1R5G5B5:
  214. case WW3D_FORMAT_A1R5G5B5:
  215. a=argb[0]<<7;
  216. r=argb[1]<<3;
  217. g=argb[2]<<3;
  218. b=argb[3]<<3;
  219. break;
  220. case WW3D_FORMAT_A4R4G4B4:
  221. a=argb[0]<<4;
  222. r=argb[1]<<4;
  223. g=argb[2]<<4;
  224. b=argb[3]<<4;
  225. break;
  226. case WW3D_FORMAT_R3G3B2:
  227. r=argb[1]<<5;
  228. g=argb[2]<<5;
  229. b=argb[3]<<6;
  230. break;
  231. case WW3D_FORMAT_A8:
  232. a=argb[0];
  233. break;
  234. case WW3D_FORMAT_A8R3G3B2:
  235. a=argb[0];
  236. r=argb[1]<<5;
  237. g=argb[2]<<5;
  238. b=argb[3]<<6;
  239. break;
  240. case WW3D_FORMAT_X4R4G4B4:
  241. r=argb[1]<<4;
  242. g=argb[2]<<4;
  243. b=argb[3]<<4;
  244. break;
  245. default:
  246. WWASSERT(0);
  247. }
  248. outc->X=r/255.0f;
  249. outc->Y=g/255.0f;
  250. outc->Z=b/255.0f;
  251. outc->W=a/255.0f;
  252. }
  253. // ----------------------------------------------------------------------------
  254. //
  255. // Utility function for determining WW3D format from TGA file header.
  256. //
  257. // ----------------------------------------------------------------------------
  258. void Get_WW3D_Format(WW3DFormat& dest_format,WW3DFormat& src_format,unsigned& src_bpp,const Targa& targa)
  259. {
  260. Get_WW3D_Format(src_format,src_bpp,targa);
  261. dest_format=src_format;
  262. if ((dest_format==WW3D_FORMAT_P8) || (dest_format==WW3D_FORMAT_L8)) {
  263. dest_format=WW3D_FORMAT_X8R8G8B8;
  264. }
  265. dest_format=Get_Valid_Texture_Format(dest_format,false); // No compressed destination format if reading from targa...
  266. }
  267. void Get_WW3D_Format(WW3DFormat& src_format,unsigned& src_bpp,const Targa& targa)
  268. {
  269. // Guess the format from the TGA Header bits:
  270. src_format = WW3D_FORMAT_UNKNOWN;
  271. src_bpp=0;
  272. switch (targa.Header.PixelDepth) {
  273. case 32: src_format = WW3D_FORMAT_A8R8G8B8; src_bpp=4; break;
  274. case 24: src_format = WW3D_FORMAT_R8G8B8; src_bpp=3; break;
  275. case 16: src_format = WW3D_FORMAT_A1R5G5B5; src_bpp=2; break;
  276. case 8:
  277. src_bpp=1;
  278. if (targa.Header.ColorMapType == 1) src_format = WW3D_FORMAT_P8;
  279. else if (targa.Header.ImageType == TGA_MONO) src_format = WW3D_FORMAT_L8;
  280. else src_format = WW3D_FORMAT_A8;
  281. break;
  282. default:
  283. WWDEBUG_SAY(("TextureClass: Targa has unsupported bitdepth(%i)\n",targa.Header.PixelDepth));
  284. // WWASSERT(0);
  285. break;
  286. }
  287. }
  288. // ----------------------------------------------------------------------------
  289. //
  290. // Utility function for determining valid WW3D format
  291. //
  292. // ----------------------------------------------------------------------------
  293. WW3DFormat Get_Valid_Texture_Format(WW3DFormat format, bool is_compression_allowed)
  294. {
  295. int w,h,bits;
  296. bool windowed;
  297. if (!DX8Wrapper::Get_Current_Caps()->Support_DXTC() ||
  298. !is_compression_allowed) {
  299. switch (format) {
  300. case WW3D_FORMAT_DXT1: format=WW3D_FORMAT_R8G8B8; break;
  301. case WW3D_FORMAT_DXT2:
  302. case WW3D_FORMAT_DXT3:
  303. case WW3D_FORMAT_DXT4:
  304. case WW3D_FORMAT_DXT5: format=WW3D_FORMAT_A8R8G8B8; break;
  305. default: break;
  306. }
  307. }
  308. else {
  309. switch (format) {
  310. case WW3D_FORMAT_DXT1:
  311. // NVidia hack - switch to DXT2 is there is no DXT1 support (which is disabled on NVidia cards)
  312. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(WW3D_FORMAT_DXT1) &&
  313. DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(WW3D_FORMAT_DXT2)) {
  314. format=WW3D_FORMAT_DXT2;
  315. }
  316. break;
  317. case WW3D_FORMAT_DXT2:
  318. case WW3D_FORMAT_DXT3:
  319. case WW3D_FORMAT_DXT4:
  320. case WW3D_FORMAT_DXT5:
  321. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) format=WW3D_FORMAT_A8R8G8B8;
  322. break;
  323. }
  324. }
  325. if (format==WW3D_FORMAT_R8G8B8) {
  326. format=WW3D_FORMAT_X8R8G8B8;
  327. }
  328. WW3D::Get_Device_Resolution(w,h,bits,windowed);
  329. if (WW3D::Get_Texture_Bitdepth()==16) bits=16;
  330. // if the device bitdepth is 16, don't allow 32 bit textures
  331. if (bits<=16) {
  332. switch (format) {
  333. case WW3D_FORMAT_A8R8G8B8: return WW3D_FORMAT_A4R4G4B4;
  334. case WW3D_FORMAT_X8R8G8B8:
  335. case WW3D_FORMAT_R8G8B8: return WW3D_FORMAT_R5G6B5;
  336. case WW3D_FORMAT_A4R4G4B4:
  337. case WW3D_FORMAT_A1R5G5B5:
  338. case WW3D_FORMAT_R5G6B5:
  339. case WW3D_FORMAT_L8:
  340. case WW3D_FORMAT_A8:
  341. case WW3D_FORMAT_P8:
  342. default:
  343. // Basically, anything goes here (just make sure the most common 32 bit formats are converted to 16 bit
  344. break;
  345. }
  346. }
  347. // Fallback if the hardware doesn't support the texture format
  348. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) {
  349. format=WW3D_FORMAT_A8R8G8B8;
  350. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) {
  351. format=WW3D_FORMAT_A4R4G4B4;
  352. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) {
  353. // If still no luck, try non-alpha formats
  354. format=WW3D_FORMAT_X8R8G8B8;
  355. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) {
  356. format=WW3D_FORMAT_R5G6B5;
  357. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) {
  358. WWASSERT_PRINT(0,("No valid texture format found"));
  359. }
  360. }
  361. }
  362. }
  363. }
  364. return format;
  365. }
  366. unsigned Get_Bytes_Per_Pixel(WW3DFormat format)
  367. {
  368. switch (format) {
  369. case WW3D_FORMAT_X8R8G8B8:
  370. case WW3D_FORMAT_X8L8V8U8:
  371. case WW3D_FORMAT_A8R8G8B8: return 4;
  372. case WW3D_FORMAT_R8G8B8: return 3;
  373. case WW3D_FORMAT_A1R5G5B5:
  374. case WW3D_FORMAT_A4R4G4B4:
  375. case WW3D_FORMAT_U8V8:
  376. case WW3D_FORMAT_L6V5U5:
  377. case WW3D_FORMAT_R5G6B5: return 2;
  378. case WW3D_FORMAT_R3G3B2:
  379. case WW3D_FORMAT_L8:
  380. case WW3D_FORMAT_A8:
  381. case WW3D_FORMAT_P8: return 1;
  382. default: WWASSERT(0); break;
  383. }
  384. return 0;
  385. }
  386. unsigned Get_Num_Depth_Bits(WW3DZFormat zformat)
  387. {
  388. switch (zformat)
  389. {
  390. case WW3D_ZFORMAT_D16_LOCKABLE: return 16; break;
  391. case WW3D_ZFORMAT_D32 : return 32; break;
  392. case WW3D_ZFORMAT_D15S1 : return 15; break;
  393. case WW3D_ZFORMAT_D24S8 : return 24; break;
  394. case WW3D_ZFORMAT_D16 : return 16; break;
  395. case WW3D_ZFORMAT_D24X8 : return 24; break;
  396. case WW3D_ZFORMAT_D24X4S4 : return 24; break;
  397. #ifdef _XBOX
  398. case WW3D_ZFORMAT_LIN_D24S8 : return 24; break;
  399. case WW3D_ZFORMAT_LIN_F24S8 : return 24; break;
  400. case WW3D_ZFORMAT_LIN_D16 : return 16; break;
  401. case WW3D_ZFORMAT_LIN_F16 : return 16; break;
  402. #endif
  403. };
  404. return 0;
  405. };
  406. unsigned Get_Num_Stencil_Bits(WW3DZFormat zformat)
  407. {
  408. switch (zformat)
  409. {
  410. case WW3D_ZFORMAT_D16_LOCKABLE: return 0; break;
  411. case WW3D_ZFORMAT_D32 : return 0; break;
  412. case WW3D_ZFORMAT_D15S1 : return 1; break;
  413. case WW3D_ZFORMAT_D24S8 : return 8; break;
  414. case WW3D_ZFORMAT_D16 : return 0; break;
  415. case WW3D_ZFORMAT_D24X8 : return 0; break;
  416. case WW3D_ZFORMAT_D24X4S4 : return 4; break;
  417. #ifdef _XBOX
  418. case WW3D_ZFORMAT_LIN_D24S8 : return 8; break;
  419. case WW3D_ZFORMAT_LIN_F24S8 : return 8; break;
  420. case WW3D_ZFORMAT_LIN_D16 : return 0; break;
  421. case WW3D_ZFORMAT_LIN_F16 : return 0; break;
  422. #endif
  423. };
  424. return 0;
  425. };