ww3dformat.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. ** Command & Conquer Renegade(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:: Jani_p $*
  29. * *
  30. * $Modtime:: 12/10/01 3:16p $*
  31. * *
  32. * $Revision:: 13 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "ww3dformat.h"
  38. #include "vector4.h"
  39. #include "wwdebug.h"
  40. #include "targa.h"
  41. #include "dx8wrapper.h"
  42. #include "dx8caps.h"
  43. #include <d3d8.h>
  44. /*
  45. WW3D_FORMAT_UNKNOWN=0,
  46. WW3D_FORMAT_R8G8B8,
  47. WW3D_FORMAT_A8R8G8B8,
  48. WW3D_FORMAT_X8R8G8B8,
  49. WW3D_FORMAT_R5G6B5,
  50. WW3D_FORMAT_X1R5G5B5,
  51. WW3D_FORMAT_A1R5G5B5,
  52. WW3D_FORMAT_A4R4G4B4,
  53. WW3D_FORMAT_R3G3B2,
  54. WW3D_FORMAT_A8,
  55. WW3D_FORMAT_A8R3G3B2,
  56. WW3D_FORMAT_X4R4G4B4,
  57. WW3D_FORMAT_A8P8,
  58. WW3D_FORMAT_P8,
  59. WW3D_FORMAT_L8,
  60. WW3D_FORMAT_A8L8,
  61. WW3D_FORMAT_A4L4,
  62. WW3D_FORMAT_COUNT // Used only to determine number of surface formats
  63. */
  64. void Get_WW3D_Format_Name(WW3DFormat format, StringClass& name)
  65. {
  66. switch (format) {
  67. default:
  68. case WW3D_FORMAT_UNKNOWN: name="Unknown"; break;
  69. case WW3D_FORMAT_R8G8B8: name="R8G8B8"; break;
  70. case WW3D_FORMAT_A8R8G8B8: name="A8R8G8B8"; break;
  71. case WW3D_FORMAT_X8R8G8B8: name="X8R8G8B8"; break;
  72. case WW3D_FORMAT_R5G6B5: name="R5G6B5"; break;
  73. case WW3D_FORMAT_X1R5G5B5: name="X1R5G5B5"; break;
  74. case WW3D_FORMAT_A1R5G5B5: name="A1R5G5B5"; break;
  75. case WW3D_FORMAT_A4R4G4B4: name="A4R4G4B4"; break;
  76. case WW3D_FORMAT_R3G3B2: name="R3G3B2"; break;
  77. case WW3D_FORMAT_A8: name="A8"; break;
  78. case WW3D_FORMAT_A8R3G3B2: name="A8R3G3B2"; break;
  79. case WW3D_FORMAT_X4R4G4B4: name="X4R4G4B4"; break;
  80. case WW3D_FORMAT_A8P8: name="A8P8"; break;
  81. case WW3D_FORMAT_P8: name="P8"; break;
  82. case WW3D_FORMAT_L8: name="L8"; break;
  83. case WW3D_FORMAT_A8L8: name="A8L8"; break;
  84. case WW3D_FORMAT_A4L4: name="A4L4"; break;
  85. case WW3D_FORMAT_U8V8: name="U8V8"; break; // Bumpmap
  86. case WW3D_FORMAT_L6V5U5: name="L6V5U5"; break; // Bumpmap
  87. case WW3D_FORMAT_X8L8V8U8: name="X8L8V8U8"; break; // Bumpmap
  88. case WW3D_FORMAT_DXT1: name="DXT1"; break;
  89. case WW3D_FORMAT_DXT2: name="DXT2"; break;
  90. case WW3D_FORMAT_DXT3: name="DXT3"; break;
  91. case WW3D_FORMAT_DXT4: name="DXT4"; break;
  92. case WW3D_FORMAT_DXT5: name="DXT5"; break;
  93. }
  94. }
  95. // extract the luminance from the RGB using the CIE 709 standard
  96. unsigned char RGB_to_CIEY(Vector4 color)
  97. {
  98. float lum=0.2126f*color.X + 0.7152f*color.Y + 0.0722f*color.Z;
  99. return (unsigned char) (255.0f*lum);
  100. }
  101. void Vector4_to_Color(unsigned int *outc,const Vector4 &inc,const WW3DFormat format)
  102. {
  103. // convert to ARGB 32-bit
  104. unsigned int color=DX8Wrapper::Convert_Color(inc);
  105. unsigned char *argb=(unsigned char*) &color;
  106. unsigned char r,g,b,a,lum;
  107. switch (format)
  108. {
  109. case WW3D_FORMAT_R8G8B8:
  110. case WW3D_FORMAT_A8R8G8B8:
  111. case WW3D_FORMAT_X8R8G8B8:
  112. *outc=color;
  113. break;
  114. case WW3D_FORMAT_R5G6B5:
  115. r=argb[1] >> 3;
  116. g=argb[2] >> 2;
  117. b=argb[3] >> 3;
  118. *outc=(r << 11) | (g<<5) | b;
  119. break;
  120. case WW3D_FORMAT_X1R5G5B5:
  121. case WW3D_FORMAT_A1R5G5B5:
  122. a=argb[0] >> 7;
  123. r=argb[1] >> 3;
  124. g=argb[2] >> 3;
  125. b=argb[3] >> 3;
  126. *outc=(a<<15) | (r<<10) | (g<<5) | b;
  127. break;
  128. case WW3D_FORMAT_A4R4G4B4:
  129. case WW3D_FORMAT_X4R4G4B4:
  130. a=argb[0] >> 4;
  131. r=argb[1] >> 4;
  132. g=argb[2] >> 4;
  133. b=argb[3] >> 4;
  134. *outc=(a<<12) | (r<<8) | (g<<4) | b;
  135. break;
  136. case WW3D_FORMAT_R3G3B2:
  137. case WW3D_FORMAT_A8R3G3B2:
  138. a=argb[0];
  139. r=argb[1] >> 5;
  140. g=argb[2] >> 5;
  141. b=argb[3] >> 6;
  142. *outc=(a<<8) | (r<<5) | (g<<2) | b;
  143. break;
  144. case WW3D_FORMAT_A8:
  145. *outc=argb[0];
  146. break;
  147. case WW3D_FORMAT_L8:
  148. lum=RGB_to_CIEY(inc);
  149. *outc=lum;
  150. break;
  151. case WW3D_FORMAT_A8L8:
  152. a=argb[0];
  153. lum=RGB_to_CIEY(inc);
  154. *outc=(a<<8) | lum;
  155. break;
  156. case WW3D_FORMAT_A4L4:
  157. a=argb[0] >> 4;
  158. lum=RGB_to_CIEY(inc);
  159. lum=lum>>4;
  160. *outc=(a<<4) | lum;
  161. break;
  162. default:
  163. WWASSERT(0);
  164. }
  165. }
  166. void Color_to_Vector4(Vector4* outc,const unsigned int inc,const WW3DFormat format)
  167. {
  168. WWASSERT(outc);
  169. unsigned char *argb=(unsigned char*) &inc;
  170. unsigned char a,r,g,b;
  171. a=r=g=b=0;
  172. switch (format)
  173. {
  174. case WW3D_FORMAT_R8G8B8:
  175. case WW3D_FORMAT_A8R8G8B8:
  176. case WW3D_FORMAT_X8R8G8B8:
  177. a=argb[0];
  178. r=argb[1];
  179. g=argb[2];
  180. b=argb[3];
  181. break;
  182. case WW3D_FORMAT_R5G6B5:
  183. r=argb[1]<<3;
  184. g=argb[2]<<2;
  185. b=argb[3]<<3;
  186. break;
  187. case WW3D_FORMAT_X1R5G5B5:
  188. case WW3D_FORMAT_A1R5G5B5:
  189. a=argb[0]<<7;
  190. r=argb[1]<<3;
  191. g=argb[2]<<3;
  192. b=argb[3]<<3;
  193. break;
  194. case WW3D_FORMAT_A4R4G4B4:
  195. a=argb[0]<<4;
  196. r=argb[1]<<4;
  197. g=argb[2]<<4;
  198. b=argb[3]<<4;
  199. break;
  200. case WW3D_FORMAT_R3G3B2:
  201. r=argb[1]<<5;
  202. g=argb[2]<<5;
  203. b=argb[3]<<6;
  204. break;
  205. case WW3D_FORMAT_A8:
  206. a=argb[0];
  207. break;
  208. case WW3D_FORMAT_A8R3G3B2:
  209. a=argb[0];
  210. r=argb[1]<<5;
  211. g=argb[2]<<5;
  212. b=argb[3]<<6;
  213. break;
  214. case WW3D_FORMAT_X4R4G4B4:
  215. r=argb[1]<<4;
  216. g=argb[2]<<4;
  217. b=argb[3]<<4;
  218. break;
  219. default:
  220. WWASSERT(0);
  221. }
  222. outc->X=r/255.0f;
  223. outc->Y=g/255.0f;
  224. outc->Z=b/255.0f;
  225. outc->W=a/255.0f;
  226. }
  227. // ----------------------------------------------------------------------------
  228. //
  229. // Utility function for determining WW3D format from TGA file header.
  230. //
  231. // ----------------------------------------------------------------------------
  232. void Get_WW3D_Format(WW3DFormat& dest_format,WW3DFormat& src_format,unsigned& src_bpp,const Targa& targa)
  233. {
  234. Get_WW3D_Format(src_format,src_bpp,targa);
  235. dest_format=src_format;
  236. if ((dest_format==WW3D_FORMAT_P8) || (dest_format==WW3D_FORMAT_L8)) {
  237. dest_format=WW3D_FORMAT_X8R8G8B8;
  238. }
  239. dest_format=Get_Valid_Texture_Format(dest_format,false); // No compressed destination format if reading from targa...
  240. }
  241. void Get_WW3D_Format(WW3DFormat& src_format,unsigned& src_bpp,const Targa& targa)
  242. {
  243. // Guess the format from the TGA Header bits:
  244. src_format = WW3D_FORMAT_UNKNOWN;
  245. src_bpp=0;
  246. switch (targa.Header.PixelDepth) {
  247. case 32: src_format = WW3D_FORMAT_A8R8G8B8; src_bpp=4; break;
  248. case 24: src_format = WW3D_FORMAT_R8G8B8; src_bpp=3; break;
  249. case 16: src_format = WW3D_FORMAT_A1R5G5B5; src_bpp=2; break;
  250. case 8:
  251. src_bpp=1;
  252. if (targa.Header.ColorMapType == 1) src_format = WW3D_FORMAT_P8;
  253. else if (targa.Header.ImageType == TGA_MONO) src_format = WW3D_FORMAT_L8;
  254. else src_format = WW3D_FORMAT_A8;
  255. break;
  256. default:
  257. WWDEBUG_SAY(("TextureClass: Targa has unsupported bitdepth(%i)\n",targa.Header.PixelDepth));
  258. // WWASSERT(0);
  259. break;
  260. }
  261. }
  262. // ----------------------------------------------------------------------------
  263. //
  264. // Utility function for determining valid WW3D format
  265. //
  266. // ----------------------------------------------------------------------------
  267. WW3DFormat Get_Valid_Texture_Format(WW3DFormat format, bool is_compression_allowed)
  268. {
  269. int w,h,bits;
  270. bool windowed;
  271. if (!DX8Wrapper::Get_Current_Caps()->Support_DXTC() ||
  272. !is_compression_allowed) {
  273. switch (format) {
  274. case WW3D_FORMAT_DXT1: format=WW3D_FORMAT_R8G8B8; break;
  275. case WW3D_FORMAT_DXT2:
  276. case WW3D_FORMAT_DXT3:
  277. case WW3D_FORMAT_DXT4:
  278. case WW3D_FORMAT_DXT5: format=WW3D_FORMAT_A8R8G8B8; break;
  279. default: break;
  280. }
  281. }
  282. else {
  283. switch (format) {
  284. case WW3D_FORMAT_DXT1:
  285. // NVidia hack - switch to DXT2 is there is no DXT1 support (which is disabled on NVidia cards)
  286. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(WW3D_FORMAT_DXT1) &&
  287. DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(WW3D_FORMAT_DXT2)) {
  288. format=WW3D_FORMAT_DXT2;
  289. }
  290. break;
  291. case WW3D_FORMAT_DXT2:
  292. case WW3D_FORMAT_DXT3:
  293. case WW3D_FORMAT_DXT4:
  294. case WW3D_FORMAT_DXT5:
  295. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) format=WW3D_FORMAT_A8R8G8B8;
  296. break;
  297. }
  298. }
  299. if (format==WW3D_FORMAT_R8G8B8) {
  300. format=WW3D_FORMAT_X8R8G8B8;
  301. }
  302. WW3D::Get_Device_Resolution(w,h,bits,windowed);
  303. if (WW3D::Get_Texture_Bitdepth()==16) bits=16;
  304. // if the device bitdepth is 16, don't allow 32 bit textures
  305. if (bits<=16) {
  306. switch (format) {
  307. case WW3D_FORMAT_A8R8G8B8: return WW3D_FORMAT_A4R4G4B4;
  308. case WW3D_FORMAT_X8R8G8B8:
  309. case WW3D_FORMAT_R8G8B8: return WW3D_FORMAT_R5G6B5;
  310. case WW3D_FORMAT_A4R4G4B4:
  311. case WW3D_FORMAT_A1R5G5B5:
  312. case WW3D_FORMAT_R5G6B5:
  313. case WW3D_FORMAT_L8:
  314. case WW3D_FORMAT_A8:
  315. case WW3D_FORMAT_P8:
  316. default:
  317. // Basically, anything goes here (just make sure the most common 32 bit formats are converted to 16 bit
  318. break;
  319. }
  320. }
  321. // Fallback if the hardware doesn't support the texture format
  322. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) {
  323. format=WW3D_FORMAT_A8R8G8B8;
  324. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) {
  325. format=WW3D_FORMAT_A4R4G4B4;
  326. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) {
  327. // If still no luck, try non-alpha formats
  328. format=WW3D_FORMAT_X8R8G8B8;
  329. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) {
  330. format=WW3D_FORMAT_R5G6B5;
  331. if (!DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(format)) {
  332. WWASSERT_PRINT(0,("No valid texture format found"));
  333. }
  334. }
  335. }
  336. }
  337. }
  338. return format;
  339. }
  340. unsigned Get_Bytes_Per_Pixel(WW3DFormat format)
  341. {
  342. switch (format) {
  343. case WW3D_FORMAT_X8R8G8B8:
  344. case WW3D_FORMAT_X8L8V8U8:
  345. case WW3D_FORMAT_A8R8G8B8: return 4;
  346. case WW3D_FORMAT_R8G8B8: return 3;
  347. case WW3D_FORMAT_A1R5G5B5:
  348. case WW3D_FORMAT_A4R4G4B4:
  349. case WW3D_FORMAT_U8V8:
  350. case WW3D_FORMAT_L6V5U5:
  351. case WW3D_FORMAT_R5G6B5: return 2;
  352. case WW3D_FORMAT_R3G3B2:
  353. case WW3D_FORMAT_L8:
  354. case WW3D_FORMAT_A8:
  355. case WW3D_FORMAT_P8: return 1;
  356. default: WWASSERT(0); break;
  357. }
  358. return 0;
  359. }