texture.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  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/texture.cpp $*
  25. * *
  26. * $Org Author:: Steve_t $*
  27. * *
  28. * Author : Kenny Mitchell *
  29. * *
  30. * $Modtime:: 08/05/02 1:27p $*
  31. * *
  32. * $Revision:: 85 $*
  33. * *
  34. * 06/27/02 KM Texture class abstraction *
  35. * 08/05/02 KM Texture class redesign (revisited)
  36. *---------------------------------------------------------------------------------------------*
  37. * Functions: *
  38. * FileListTextureClass::Load_Frame_Surface -- Load source texture *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "texture.h"
  41. #include <d3d8.h>
  42. #include <stdio.h>
  43. #include <D3dx8core.h>
  44. #include "dx8wrapper.h"
  45. #include "targa.h"
  46. #include <nstrdup.h>
  47. #include "w3d_file.h"
  48. #include "assetmgr.h"
  49. #include "formconv.h"
  50. #include "textureloader.h"
  51. #include "missingtexture.h"
  52. #include "ffactory.h"
  53. #include "dx8caps.h"
  54. #include "dx8texman.h"
  55. #include "meshmatdesc.h"
  56. #include "texturethumbnail.h"
  57. #include "wwprofile.h"
  58. //#pragma optimize("", off)
  59. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  60. const unsigned DEFAULT_INACTIVATION_TIME=20000;
  61. /*
  62. ** Definitions of static members:
  63. */
  64. static unsigned unused_texture_id;
  65. // This throttles submissions to the background texture loading queue.
  66. static unsigned TexturesAppliedPerFrame;
  67. const unsigned MAX_TEXTURES_APPLIED_PER_FRAME=2;
  68. /*!
  69. * KM General base constructor for texture classes
  70. */
  71. TextureBaseClass::TextureBaseClass
  72. (
  73. unsigned int width,
  74. unsigned int height,
  75. enum MipCountType mip_level_count,
  76. enum PoolType pool,
  77. bool rendertarget,
  78. bool reducible
  79. )
  80. : MipLevelCount(mip_level_count),
  81. D3DTexture(NULL),
  82. Initialized(false),
  83. Name(""),
  84. FullPath(""),
  85. texture_id(unused_texture_id++),
  86. IsLightmap(false),
  87. IsProcedural(false),
  88. IsReducible(reducible),
  89. IsCompressionAllowed(false),
  90. InactivationTime(0),
  91. ExtendedInactivationTime(0),
  92. LastInactivationSyncTime(0),
  93. LastAccessed(0),
  94. Width(width),
  95. Height(height),
  96. Pool(pool),
  97. Dirty(false),
  98. TextureLoadTask(NULL),
  99. ThumbnailLoadTask(NULL),
  100. HSVShift(0.0f,0.0f,0.0f)
  101. {
  102. }
  103. //**********************************************************************************************
  104. //! Base texture class destructor
  105. /*! KJM
  106. */
  107. TextureBaseClass::~TextureBaseClass(void)
  108. {
  109. delete TextureLoadTask;
  110. TextureLoadTask=NULL;
  111. delete ThumbnailLoadTask;
  112. ThumbnailLoadTask=NULL;
  113. if (D3DTexture)
  114. {
  115. D3DTexture->Release();
  116. D3DTexture = NULL;
  117. }
  118. DX8TextureManagerClass::Remove(this);
  119. }
  120. //**********************************************************************************************
  121. //! Invalidate old unused textures
  122. /*!
  123. */
  124. void TextureBaseClass::Invalidate_Old_Unused_Textures(unsigned invalidation_time_override)
  125. {
  126. // (gth) If thumbnails are not enabled, then we don't run this code.
  127. if (WW3D::Get_Thumbnail_Enabled() == false) {
  128. return;
  129. }
  130. // Zero the texture apply count in this function because this is called every frame...(this wasn't in E&B main branch KJM)
  131. TexturesAppliedPerFrame=0;
  132. unsigned synctime=WW3D::Get_Sync_Time();
  133. HashTemplateIterator<StringClass,TextureClass*> ite(WW3DAssetManager::Get_Instance()->Texture_Hash());
  134. // Loop through all the textures in the manager
  135. for (ite.First ();!ite.Is_Done();ite.Next ())
  136. {
  137. TextureClass* tex=ite.Peek_Value();
  138. // Consider invalidating if texture has been initialized and defines inactivation time
  139. if (tex->Initialized && tex->InactivationTime)
  140. {
  141. unsigned age=synctime-tex->LastAccessed;
  142. if (invalidation_time_override)
  143. {
  144. if (age>invalidation_time_override)
  145. {
  146. tex->Invalidate();
  147. tex->LastInactivationSyncTime=synctime;
  148. }
  149. }
  150. else
  151. {
  152. // Not used in the last n milliseconds?
  153. if (age>(tex->InactivationTime+tex->ExtendedInactivationTime))
  154. {
  155. tex->Invalidate();
  156. tex->LastInactivationSyncTime=synctime;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. //**********************************************************************************************
  163. //! Invalidate this texture
  164. /*!
  165. */
  166. void TextureBaseClass::Invalidate()
  167. {
  168. if (TextureLoadTask) {
  169. return;
  170. }
  171. if (ThumbnailLoadTask) {
  172. return;
  173. }
  174. // Don't invalidate procedural textures
  175. if (IsProcedural) {
  176. return;
  177. }
  178. if (D3DTexture)
  179. {
  180. D3DTexture->Release();
  181. D3DTexture = NULL;
  182. }
  183. Initialized=false;
  184. LastAccessed=WW3D::Get_Sync_Time();
  185. /* was battlefield version// If the texture has already been initialised we should exit now
  186. if (Initialized) return;
  187. WWPROFILE(("TextureClass::Init()"));
  188. // If the texture has recently been inactivated, increase the inactivation time (this texture obviously
  189. // should not have been inactivated yet).
  190. if (InactivationTime && LastInactivationSyncTime) {
  191. if ((WW3D::Get_Sync_Time()-LastInactivationSyncTime)<InactivationTime) {
  192. ExtendedInactivationTime=3*InactivationTime;
  193. }
  194. LastInactivationSyncTime=0;
  195. }
  196. if (ThumbnailLoadTask)
  197. {
  198. return;
  199. }
  200. // Don't invalidate procedural textures
  201. if (IsProcedural)
  202. {
  203. return;
  204. }
  205. if (D3DTexture)
  206. {
  207. D3DTexture->Release();
  208. D3DTexture = NULL;
  209. }
  210. Initialized=false;
  211. LastAccessed=WW3D::Get_Sync_Time();*/
  212. }
  213. //**********************************************************************************************
  214. //! Returns a pointer to the d3d texture
  215. /*!
  216. */
  217. IDirect3DBaseTexture8 * TextureBaseClass::Peek_D3D_Base_Texture() const
  218. {
  219. LastAccessed=WW3D::Get_Sync_Time();
  220. return D3DTexture;
  221. }
  222. //**********************************************************************************************
  223. //! Set the d3d texture pointer. Handles ref counts properly.
  224. /*!
  225. */
  226. void TextureBaseClass::Set_D3D_Base_Texture(IDirect3DBaseTexture8* tex)
  227. {
  228. // (gth) Generals does stuff directly with the D3DTexture pointer so lets
  229. // reset the access timer whenever someon messes with this pointer.
  230. LastAccessed=WW3D::Get_Sync_Time();
  231. if (D3DTexture != NULL) {
  232. D3DTexture->Release();
  233. }
  234. D3DTexture = tex;
  235. if (D3DTexture != NULL) {
  236. D3DTexture->AddRef();
  237. }
  238. }
  239. //**********************************************************************************************
  240. //! Load locked surface
  241. /*!
  242. */
  243. void TextureBaseClass::Load_Locked_Surface()
  244. {
  245. WWPROFILE(("TextureClass::Load_Locked_Surface()"));
  246. if (D3DTexture) D3DTexture->Release();
  247. D3DTexture=0;
  248. TextureLoader::Request_Thumbnail(this);
  249. Initialized=false;
  250. }
  251. //**********************************************************************************************
  252. //! Is missing texture
  253. /*!
  254. */
  255. bool TextureBaseClass::Is_Missing_Texture()
  256. {
  257. bool flag = false;
  258. IDirect3DBaseTexture8 *missing_texture = MissingTexture::_Get_Missing_Texture();
  259. if (D3DTexture == missing_texture)
  260. flag = true;
  261. if (missing_texture)
  262. {
  263. missing_texture->Release();
  264. }
  265. return flag;
  266. }
  267. //**********************************************************************************************
  268. //! Set texture name
  269. /*!
  270. */
  271. void TextureBaseClass::Set_Texture_Name(const char * name)
  272. {
  273. Name=name;
  274. }
  275. //**********************************************************************************************
  276. //! Get priority
  277. /*!
  278. */
  279. unsigned int TextureBaseClass::Get_Priority(void)
  280. {
  281. if (!D3DTexture)
  282. {
  283. WWASSERT_PRINT(0, "Get_Priority: D3DTexture is NULL!\n");
  284. return 0;
  285. }
  286. #ifndef _XBOX
  287. return D3DTexture->GetPriority();
  288. #else
  289. return 0;
  290. #endif
  291. }
  292. //**********************************************************************************************
  293. //! Set priority
  294. /*!
  295. */
  296. unsigned int TextureBaseClass::Set_Priority(unsigned int priority)
  297. {
  298. if (!D3DTexture)
  299. {
  300. WWASSERT_PRINT(0, "Set_Priority: D3DTexture is NULL!\n");
  301. return 0;
  302. }
  303. #ifndef _XBOX
  304. return D3DTexture->SetPriority(priority);
  305. #else
  306. return 0;
  307. #endif
  308. }
  309. //**********************************************************************************************
  310. //! Get reduction mip levels
  311. /*!
  312. */
  313. unsigned TextureBaseClass::Get_Reduction() const
  314. {
  315. // don't reduce if the texture is too small already or
  316. // has no mip map levels
  317. if (MipLevelCount==MIP_LEVELS_1) return 0;
  318. if (Width <= 32 || Height <= 32) return 0;
  319. int reduction=WW3D::Get_Texture_Reduction();
  320. // 'large texture extra reduction' causes textures above 256x256 to be reduced one more step.
  321. if (WW3D::Is_Large_Texture_Extra_Reduction_Enabled() && (Width > 256 || Height > 256)) {
  322. reduction++;
  323. }
  324. if (MipLevelCount && reduction>MipLevelCount) {
  325. reduction=MipLevelCount;
  326. }
  327. return reduction;
  328. }
  329. //**********************************************************************************************
  330. //! Apply NULL texture state
  331. /*!
  332. */
  333. void TextureBaseClass::Apply_Null(unsigned int stage)
  334. {
  335. // This function sets the render states for a "NULL" texture
  336. DX8Wrapper::Set_DX8_Texture(stage, NULL);
  337. }
  338. // ----------------------------------------------------------------------------
  339. // Setting HSV_Shift value is always relative to the original texture. This function invalidates the
  340. // texture surface and causes the texture to be reloaded. For thumbnailable textures, the hue shifting
  341. // is done in the background loading thread.
  342. // ----------------------------------------------------------------------------
  343. void TextureBaseClass::Set_HSV_Shift(const Vector3 &hsv_shift)
  344. {
  345. Invalidate();
  346. HSVShift=hsv_shift;
  347. }
  348. //**********************************************************************************************
  349. //! Get total locked surface size
  350. /*! KM
  351. */
  352. int TextureBaseClass::_Get_Total_Locked_Surface_Size()
  353. {
  354. int total_locked_surface_size=0;
  355. HashTemplateIterator<StringClass,TextureClass*> ite(WW3DAssetManager::Get_Instance()->Texture_Hash());
  356. // Loop through all the textures in the manager
  357. for (ite.First ();!ite.Is_Done();ite.Next ())
  358. {
  359. // Get the current texture
  360. TextureBaseClass* tex=ite.Peek_Value();
  361. if (!tex->Initialized)
  362. {
  363. total_locked_surface_size+=tex->Get_Texture_Memory_Usage();
  364. }
  365. }
  366. return total_locked_surface_size;
  367. }
  368. //**********************************************************************************************
  369. //! Get total texture size
  370. /*! KM
  371. */
  372. int TextureBaseClass::_Get_Total_Texture_Size()
  373. {
  374. int total_texture_size=0;
  375. HashTemplateIterator<StringClass,TextureClass*> ite(WW3DAssetManager::Get_Instance()->Texture_Hash());
  376. // Loop through all the textures in the manager
  377. for (ite.First ();!ite.Is_Done();ite.Next ())
  378. {
  379. // Get the current texture
  380. TextureBaseClass* tex=ite.Peek_Value();
  381. total_texture_size+=tex->Get_Texture_Memory_Usage();
  382. }
  383. return total_texture_size;
  384. }
  385. // ----------------------------------------------------------------------------
  386. //**********************************************************************************************
  387. //! Get total lightmap texture size
  388. /*!
  389. */
  390. int TextureBaseClass::_Get_Total_Lightmap_Texture_Size()
  391. {
  392. int total_texture_size=0;
  393. HashTemplateIterator<StringClass,TextureClass*> ite(WW3DAssetManager::Get_Instance()->Texture_Hash());
  394. // Loop through all the textures in the manager
  395. for (ite.First ();!ite.Is_Done();ite.Next ())
  396. {
  397. // Get the current texture
  398. TextureBaseClass* tex=ite.Peek_Value();
  399. if (tex->Is_Lightmap())
  400. {
  401. total_texture_size+=tex->Get_Texture_Memory_Usage();
  402. }
  403. }
  404. return total_texture_size;
  405. }
  406. //**********************************************************************************************
  407. //! Get total procedural texture size
  408. /*!
  409. */
  410. int TextureBaseClass::_Get_Total_Procedural_Texture_Size()
  411. {
  412. int total_texture_size=0;
  413. HashTemplateIterator<StringClass,TextureClass*> ite(WW3DAssetManager::Get_Instance()->Texture_Hash());
  414. // Loop through all the textures in the manager
  415. for (ite.First ();!ite.Is_Done();ite.Next ())
  416. {
  417. // Get the current texture
  418. TextureBaseClass* tex=ite.Peek_Value();
  419. if (tex->Is_Procedural())
  420. {
  421. total_texture_size+=tex->Get_Texture_Memory_Usage();
  422. }
  423. }
  424. return total_texture_size;
  425. }
  426. //**********************************************************************************************
  427. //! Get total texture count
  428. /*!
  429. */
  430. int TextureBaseClass::_Get_Total_Texture_Count()
  431. {
  432. int texture_count=0;
  433. HashTemplateIterator<StringClass,TextureClass*> ite(WW3DAssetManager::Get_Instance()->Texture_Hash());
  434. // Loop through all the textures in the manager
  435. for (ite.First ();!ite.Is_Done();ite.Next ())
  436. {
  437. texture_count++;
  438. }
  439. return texture_count;
  440. }
  441. // ----------------------------------------------------------------------------
  442. //**********************************************************************************************
  443. //! Get total light map texture count
  444. /*!
  445. */
  446. int TextureBaseClass::_Get_Total_Lightmap_Texture_Count()
  447. {
  448. int texture_count=0;
  449. HashTemplateIterator<StringClass,TextureClass*> ite(WW3DAssetManager::Get_Instance()->Texture_Hash());
  450. // Loop through all the textures in the manager
  451. for (ite.First ();!ite.Is_Done();ite.Next ())
  452. {
  453. if (ite.Peek_Value()->Is_Lightmap())
  454. {
  455. texture_count++;
  456. }
  457. }
  458. return texture_count;
  459. }
  460. //**********************************************************************************************
  461. //! Get total procedural texture count
  462. /*!
  463. */
  464. int TextureBaseClass::_Get_Total_Procedural_Texture_Count()
  465. {
  466. int texture_count=0;
  467. HashTemplateIterator<StringClass,TextureClass*> ite(WW3DAssetManager::Get_Instance()->Texture_Hash());
  468. // Loop through all the textures in the manager
  469. for (ite.First ();!ite.Is_Done();ite.Next ())
  470. {
  471. if (ite.Peek_Value()->Is_Procedural())
  472. {
  473. texture_count++;
  474. }
  475. }
  476. return texture_count;
  477. }
  478. //**********************************************************************************************
  479. //! Get total locked surface count
  480. /*!
  481. */
  482. int TextureBaseClass::_Get_Total_Locked_Surface_Count()
  483. {
  484. int texture_count=0;
  485. HashTemplateIterator<StringClass,TextureClass*> ite(WW3DAssetManager::Get_Instance()->Texture_Hash());
  486. // Loop through all the textures in the manager
  487. for (ite.First ();!ite.Is_Done();ite.Next ())
  488. {
  489. // Get the current texture
  490. TextureBaseClass* tex=ite.Peek_Value();
  491. if (!tex->Initialized)
  492. {
  493. texture_count++;
  494. }
  495. }
  496. return texture_count;
  497. }
  498. /*************************************************************************
  499. ** TextureClass
  500. *************************************************************************/
  501. TextureClass::TextureClass
  502. (
  503. unsigned width,
  504. unsigned height,
  505. WW3DFormat format,
  506. MipCountType mip_level_count,
  507. PoolType pool,
  508. bool rendertarget,
  509. bool allow_reduction
  510. )
  511. : TextureBaseClass(width, height, mip_level_count, pool, rendertarget,allow_reduction),
  512. Filter(mip_level_count),
  513. TextureFormat(format)
  514. {
  515. Initialized=true;
  516. IsProcedural=true;
  517. IsReducible=false;
  518. switch (format)
  519. {
  520. case WW3D_FORMAT_DXT1:
  521. case WW3D_FORMAT_DXT2:
  522. case WW3D_FORMAT_DXT3:
  523. case WW3D_FORMAT_DXT4:
  524. case WW3D_FORMAT_DXT5:
  525. IsCompressionAllowed=true;
  526. break;
  527. default : break;
  528. }
  529. D3DPOOL d3dpool=(D3DPOOL)0;
  530. switch(pool)
  531. {
  532. case POOL_DEFAULT : d3dpool=D3DPOOL_DEFAULT; break;
  533. case POOL_MANAGED : d3dpool=D3DPOOL_MANAGED; break;
  534. case POOL_SYSTEMMEM : d3dpool=D3DPOOL_SYSTEMMEM; break;
  535. default: WWASSERT(0);
  536. }
  537. Poke_Texture
  538. (
  539. DX8Wrapper::_Create_DX8_Texture
  540. (
  541. width,
  542. height,
  543. format,
  544. mip_level_count,
  545. d3dpool,
  546. rendertarget
  547. )
  548. );
  549. if (pool==POOL_DEFAULT)
  550. {
  551. Set_Dirty();
  552. DX8TextureTrackerClass *track=new DX8TextureTrackerClass
  553. (
  554. width,
  555. height,
  556. format,
  557. mip_level_count,
  558. this,
  559. rendertarget
  560. );
  561. DX8TextureManagerClass::Add(track);
  562. }
  563. LastAccessed=WW3D::Get_Sync_Time();
  564. }
  565. // ----------------------------------------------------------------------------
  566. TextureClass::TextureClass
  567. (
  568. const char *name,
  569. const char *full_path,
  570. MipCountType mip_level_count,
  571. WW3DFormat texture_format,
  572. bool allow_compression,
  573. bool allow_reduction
  574. )
  575. : TextureBaseClass(0, 0, mip_level_count),
  576. Filter(mip_level_count),
  577. TextureFormat(texture_format)
  578. {
  579. IsCompressionAllowed=allow_compression;
  580. InactivationTime=DEFAULT_INACTIVATION_TIME; // Default inactivation time 30 seconds
  581. IsReducible=allow_reduction;
  582. switch (TextureFormat)
  583. {
  584. case WW3D_FORMAT_DXT1:
  585. case WW3D_FORMAT_DXT2:
  586. case WW3D_FORMAT_DXT3:
  587. case WW3D_FORMAT_DXT4:
  588. case WW3D_FORMAT_DXT5:
  589. IsCompressionAllowed=true;
  590. break;
  591. case WW3D_FORMAT_U8V8: // Bumpmap
  592. case WW3D_FORMAT_L6V5U5: // Bumpmap
  593. case WW3D_FORMAT_X8L8V8U8: // Bumpmap
  594. // If requesting bumpmap format that isn't available we'll just return the surface in whatever color
  595. // format the texture file is in. (This is illegal case, the format support should always be queried
  596. // before creating a bump texture!)
  597. if (!DX8Wrapper::Is_Initted() || !DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(TextureFormat))
  598. {
  599. TextureFormat=WW3D_FORMAT_UNKNOWN;
  600. }
  601. // If bump format is valid, make sure compression is not allowed so that we don't even attempt to load
  602. // from a compressed file (quality isn't good enough for bump map). Also disable mipmapping.
  603. else
  604. {
  605. IsCompressionAllowed=false;
  606. MipLevelCount=MIP_LEVELS_1;
  607. Filter.Set_Mip_Mapping(TextureFilterClass::FILTER_TYPE_NONE);
  608. }
  609. break;
  610. default: break;
  611. }
  612. WWASSERT_PRINT(name && name[0], "TextureClass CTor: NULL or empty texture name\n");
  613. int len=strlen(name);
  614. for (int i=0;i<len;++i)
  615. {
  616. if (name[i]=='+')
  617. {
  618. IsLightmap=true;
  619. // Set bilinear filtering for lightmaps (they are very stretched and
  620. // low detail so we don't care for anisotropic or trilinear filtering...)
  621. Filter.Set_Min_Filter(TextureFilterClass::FILTER_TYPE_FAST);
  622. Filter.Set_Mag_Filter(TextureFilterClass::FILTER_TYPE_FAST);
  623. if (mip_level_count!=MIP_LEVELS_1) Filter.Set_Mip_Mapping(TextureFilterClass::FILTER_TYPE_FAST);
  624. break;
  625. }
  626. }
  627. Set_Texture_Name(name);
  628. Set_Full_Path(full_path);
  629. WWASSERT(name[0]!='\0');
  630. if (!WW3D::Is_Texturing_Enabled())
  631. {
  632. Initialized=true;
  633. Poke_Texture(NULL);
  634. }
  635. // Find original size from the thumbnail (but don't create thumbnail texture yet!)
  636. ThumbnailClass* thumb=ThumbnailManagerClass::Peek_Thumbnail_Instance_From_Any_Manager(Get_Full_Path());
  637. if (thumb)
  638. {
  639. Width=thumb->Get_Original_Texture_Width();
  640. Height=thumb->Get_Original_Texture_Height();
  641. if (MipLevelCount!=MIP_LEVELS_1) {
  642. MipLevelCount=(MipCountType)thumb->Get_Original_Texture_Mip_Level_Count();
  643. }
  644. }
  645. LastAccessed=WW3D::Get_Sync_Time();
  646. // If the thumbnails are not enabled, init the texture at this point to avoid stalling when the
  647. // mesh is rendered.
  648. if (!WW3D::Get_Thumbnail_Enabled())
  649. {
  650. if (TextureLoader::Is_DX8_Thread())
  651. {
  652. Init();
  653. }
  654. }
  655. }
  656. // ----------------------------------------------------------------------------
  657. TextureClass::TextureClass
  658. (
  659. SurfaceClass *surface,
  660. MipCountType mip_level_count
  661. )
  662. : TextureBaseClass(0,0,mip_level_count),
  663. Filter(mip_level_count),
  664. TextureFormat(surface->Get_Surface_Format())
  665. {
  666. IsProcedural=true;
  667. Initialized=true;
  668. IsReducible=false;
  669. SurfaceClass::SurfaceDescription sd;
  670. surface->Get_Description(sd);
  671. Width=sd.Width;
  672. Height=sd.Height;
  673. switch (sd.Format)
  674. {
  675. case WW3D_FORMAT_DXT1:
  676. case WW3D_FORMAT_DXT2:
  677. case WW3D_FORMAT_DXT3:
  678. case WW3D_FORMAT_DXT4:
  679. case WW3D_FORMAT_DXT5:
  680. IsCompressionAllowed=true;
  681. break;
  682. default: break;
  683. }
  684. Poke_Texture
  685. (
  686. DX8Wrapper::_Create_DX8_Texture
  687. (
  688. surface->Peek_D3D_Surface(),
  689. mip_level_count
  690. )
  691. );
  692. LastAccessed=WW3D::Get_Sync_Time();
  693. }
  694. // ----------------------------------------------------------------------------
  695. TextureClass::TextureClass(IDirect3DBaseTexture8* d3d_texture)
  696. : TextureBaseClass
  697. (
  698. 0,
  699. 0,
  700. ((MipCountType)d3d_texture->GetLevelCount())
  701. ),
  702. Filter((MipCountType)d3d_texture->GetLevelCount())
  703. {
  704. Initialized=true;
  705. IsProcedural=true;
  706. IsReducible=false;
  707. Set_D3D_Base_Texture(d3d_texture);
  708. IDirect3DSurface8* surface;
  709. DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(0,&surface));
  710. D3DSURFACE_DESC d3d_desc;
  711. ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC));
  712. DX8_ErrorCode(surface->GetDesc(&d3d_desc));
  713. Width=d3d_desc.Width;
  714. Height=d3d_desc.Height;
  715. TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
  716. switch (TextureFormat)
  717. {
  718. case WW3D_FORMAT_DXT1:
  719. case WW3D_FORMAT_DXT2:
  720. case WW3D_FORMAT_DXT3:
  721. case WW3D_FORMAT_DXT4:
  722. case WW3D_FORMAT_DXT5:
  723. IsCompressionAllowed=true;
  724. break;
  725. default: break;
  726. }
  727. LastAccessed=WW3D::Get_Sync_Time();
  728. }
  729. //**********************************************************************************************
  730. //! Initialise the texture
  731. /*!
  732. */
  733. void TextureClass::Init()
  734. {
  735. // If the texture has already been initialised we should exit now
  736. if (Initialized) return;
  737. WWPROFILE("TextureClass::Init");
  738. // If the texture has recently been inactivated, increase the inactivation time (this texture obviously
  739. // should not have been inactivated yet).
  740. if (InactivationTime && LastInactivationSyncTime)
  741. {
  742. if ((WW3D::Get_Sync_Time()-LastInactivationSyncTime)<InactivationTime)
  743. {
  744. ExtendedInactivationTime=3*InactivationTime;
  745. }
  746. LastInactivationSyncTime=0;
  747. }
  748. if (!Peek_D3D_Base_Texture())
  749. {
  750. if (!WW3D::Get_Thumbnail_Enabled() || MipLevelCount==MIP_LEVELS_1)
  751. {
  752. // if (MipLevelCount==MIP_LEVELS_1) {
  753. TextureLoader::Request_Foreground_Loading(this);
  754. }
  755. else
  756. {
  757. WW3DFormat format=TextureFormat;
  758. Load_Locked_Surface();
  759. TextureFormat=format;
  760. }
  761. }
  762. if (!Initialized)
  763. {
  764. TextureLoader::Request_Background_Loading(this);
  765. }
  766. LastAccessed=WW3D::Get_Sync_Time();
  767. }
  768. //**********************************************************************************************
  769. //! Apply new surface to texture
  770. /*!
  771. */
  772. void TextureClass::Apply_New_Surface
  773. (
  774. IDirect3DBaseTexture8* d3d_texture,
  775. bool initialized,
  776. bool disable_auto_invalidation
  777. )
  778. {
  779. IDirect3DBaseTexture8* d3d_tex=Peek_D3D_Base_Texture();
  780. if (d3d_tex) d3d_tex->Release();
  781. Poke_Texture(d3d_texture);//TextureLoadTask->Peek_D3D_Texture();
  782. d3d_texture->AddRef();
  783. if (initialized) Initialized=true;
  784. if (disable_auto_invalidation) InactivationTime = 0;
  785. WWASSERT(d3d_texture);
  786. IDirect3DSurface8* surface;
  787. DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(0,&surface));
  788. D3DSURFACE_DESC d3d_desc;
  789. ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC));
  790. DX8_ErrorCode(surface->GetDesc(&d3d_desc));
  791. if (initialized)
  792. {
  793. TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
  794. Width=d3d_desc.Width;
  795. Height=d3d_desc.Height;
  796. }
  797. surface->Release();
  798. }
  799. //**********************************************************************************************
  800. //! Apply texture states
  801. /*!
  802. */
  803. void TextureClass::Apply(unsigned int stage)
  804. {
  805. // Initialization needs to be done when texture is used if it hasn't been done before.
  806. // XBOX always initializes textures at creation time.
  807. if (!Initialized)
  808. {
  809. Init();
  810. /* was in battlefield// Non-thumbnailed textures are always initialized when used
  811. if (MipLevelCount==MIP_LEVELS_1)
  812. {
  813. }
  814. // Thumbnailed textures have delayed initialization and a background loading system
  815. else
  816. {
  817. // Limit the number of texture initializations per frame to reduce stuttering
  818. if (TexturesAppliedPerFrame<MAX_TEXTURES_APPLIED_PER_FRAME)
  819. {
  820. TexturesAppliedPerFrame++;
  821. Init();
  822. }
  823. else
  824. {
  825. // If texture can't be initialized in this frame, at least make sure we have the thumbnail.
  826. if (!Peek_Texture())
  827. {
  828. WW3DFormat format=TextureFormat;
  829. Load_Locked_Surface();
  830. TextureFormat=format;
  831. }
  832. }
  833. }*/
  834. }
  835. LastAccessed=WW3D::Get_Sync_Time();
  836. DX8_RECORD_TEXTURE(this);
  837. // Set texture itself
  838. if (WW3D::Is_Texturing_Enabled())
  839. {
  840. DX8Wrapper::Set_DX8_Texture(stage, Peek_D3D_Base_Texture());
  841. }
  842. else
  843. {
  844. DX8Wrapper::Set_DX8_Texture(stage, NULL);
  845. }
  846. Filter.Apply(stage);
  847. }
  848. //**********************************************************************************************
  849. //! Get surface from mip level
  850. /*!
  851. */
  852. SurfaceClass *TextureClass::Get_Surface_Level(unsigned int level)
  853. {
  854. if (!Peek_D3D_Texture())
  855. {
  856. WWASSERT_PRINT(0, "Get_Surface_Level: D3DTexture is NULL!\n");
  857. return 0;
  858. }
  859. IDirect3DSurface8 *d3d_surface = NULL;
  860. DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(level, &d3d_surface));
  861. SurfaceClass *surface = new SurfaceClass(d3d_surface);
  862. d3d_surface->Release();
  863. return surface;
  864. }
  865. //**********************************************************************************************
  866. //! Get surface description for a mip level
  867. /*!
  868. */
  869. void TextureClass::Get_Level_Description( SurfaceClass::SurfaceDescription & desc, unsigned int level )
  870. {
  871. SurfaceClass * surf = Get_Surface_Level(level);
  872. if (surf != NULL) {
  873. surf->Get_Description(desc);
  874. }
  875. REF_PTR_RELEASE(surf);
  876. }
  877. //**********************************************************************************************
  878. //! Get D3D surface from mip level
  879. /*!
  880. */
  881. IDirect3DSurface8 *TextureClass::Get_D3D_Surface_Level(unsigned int level)
  882. {
  883. if (!Peek_D3D_Texture())
  884. {
  885. WWASSERT_PRINT(0, "Get_D3D_Surface_Level: D3DTexture is NULL!\n");
  886. return 0;
  887. }
  888. IDirect3DSurface8 *d3d_surface = NULL;
  889. DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(level, &d3d_surface));
  890. return d3d_surface;
  891. }
  892. //**********************************************************************************************
  893. //! Get texture memory usage
  894. /*!
  895. */
  896. unsigned TextureClass::Get_Texture_Memory_Usage() const
  897. {
  898. int size=0;
  899. if (!Peek_D3D_Texture()) return 0;
  900. for (unsigned i=0;i<Peek_D3D_Texture()->GetLevelCount();++i)
  901. {
  902. D3DSURFACE_DESC desc;
  903. DX8_ErrorCode(Peek_D3D_Texture()->GetLevelDesc(i,&desc));
  904. size+=desc.Size;
  905. }
  906. return size;
  907. }
  908. // Utility functions
  909. TextureClass* Load_Texture(ChunkLoadClass & cload)
  910. {
  911. // Assume failure
  912. TextureClass *newtex = NULL;
  913. char name[256];
  914. if (cload.Open_Chunk () && (cload.Cur_Chunk_ID () == W3D_CHUNK_TEXTURE))
  915. {
  916. W3dTextureInfoStruct texinfo;
  917. bool hastexinfo = false;
  918. /*
  919. ** Read in the texture filename, and a possible texture info structure.
  920. */
  921. while (cload.Open_Chunk()) {
  922. switch (cload.Cur_Chunk_ID()) {
  923. case W3D_CHUNK_TEXTURE_NAME:
  924. cload.Read(&name,cload.Cur_Chunk_Length());
  925. break;
  926. case W3D_CHUNK_TEXTURE_INFO:
  927. cload.Read(&texinfo,sizeof(W3dTextureInfoStruct));
  928. hastexinfo = true;
  929. break;
  930. };
  931. cload.Close_Chunk();
  932. }
  933. cload.Close_Chunk();
  934. /*
  935. ** Get the texture from the asset manager
  936. */
  937. if (hastexinfo)
  938. {
  939. MipCountType mipcount;
  940. bool no_lod = ((texinfo.Attributes & W3DTEXTURE_NO_LOD) == W3DTEXTURE_NO_LOD);
  941. if (no_lod)
  942. {
  943. mipcount = MIP_LEVELS_1;
  944. }
  945. else
  946. {
  947. switch (texinfo.Attributes & W3DTEXTURE_MIP_LEVELS_MASK) {
  948. case W3DTEXTURE_MIP_LEVELS_ALL:
  949. mipcount = MIP_LEVELS_ALL;
  950. break;
  951. case W3DTEXTURE_MIP_LEVELS_2:
  952. mipcount = MIP_LEVELS_2;
  953. break;
  954. case W3DTEXTURE_MIP_LEVELS_3:
  955. mipcount = MIP_LEVELS_3;
  956. break;
  957. case W3DTEXTURE_MIP_LEVELS_4:
  958. mipcount = MIP_LEVELS_4;
  959. break;
  960. default:
  961. WWASSERT (false);
  962. mipcount = MIP_LEVELS_ALL;
  963. break;
  964. }
  965. }
  966. WW3DFormat format=WW3D_FORMAT_UNKNOWN;
  967. switch (texinfo.Attributes & W3DTEXTURE_TYPE_MASK)
  968. {
  969. case W3DTEXTURE_TYPE_COLORMAP:
  970. // Do nothing.
  971. break;
  972. case W3DTEXTURE_TYPE_BUMPMAP:
  973. {
  974. if (DX8Wrapper::Is_Initted() && DX8Wrapper::Get_Current_Caps()->Support_Bump_Envmap())
  975. {
  976. // No mipmaps to bumpmap for now
  977. mipcount=MIP_LEVELS_1;
  978. if (DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(WW3D_FORMAT_U8V8)) format=WW3D_FORMAT_U8V8;
  979. else if (DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(WW3D_FORMAT_X8L8V8U8)) format=WW3D_FORMAT_X8L8V8U8;
  980. else if (DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(WW3D_FORMAT_L6V5U5)) format=WW3D_FORMAT_L6V5U5;
  981. }
  982. break;
  983. }
  984. default:
  985. WWASSERT (false);
  986. break;
  987. }
  988. newtex = WW3DAssetManager::Get_Instance()->Get_Texture (name, mipcount, format);
  989. if (no_lod)
  990. {
  991. newtex->Get_Filter().Set_Mip_Mapping(TextureFilterClass::FILTER_TYPE_NONE);
  992. }
  993. bool u_clamp = ((texinfo.Attributes & W3DTEXTURE_CLAMP_U) != 0);
  994. newtex->Get_Filter().Set_U_Addr_Mode(u_clamp ? TextureFilterClass::TEXTURE_ADDRESS_CLAMP : TextureFilterClass::TEXTURE_ADDRESS_REPEAT);
  995. bool v_clamp = ((texinfo.Attributes & W3DTEXTURE_CLAMP_V) != 0);
  996. newtex->Get_Filter().Set_V_Addr_Mode(v_clamp ? TextureFilterClass::TEXTURE_ADDRESS_CLAMP : TextureFilterClass::TEXTURE_ADDRESS_REPEAT);
  997. } else
  998. {
  999. newtex = WW3DAssetManager::Get_Instance()->Get_Texture(name);
  1000. }
  1001. WWASSERT(newtex);
  1002. }
  1003. // Return a pointer to the new texture
  1004. return newtex;
  1005. }
  1006. // Utility function used by Save_Texture
  1007. void setup_texture_attributes(TextureClass * tex, W3dTextureInfoStruct * texinfo)
  1008. {
  1009. texinfo->Attributes = 0;
  1010. if (tex->Get_Filter().Get_Mip_Mapping() == TextureFilterClass::FILTER_TYPE_NONE) texinfo->Attributes |= W3DTEXTURE_NO_LOD;
  1011. if (tex->Get_Filter().Get_U_Addr_Mode() == TextureFilterClass::TEXTURE_ADDRESS_CLAMP) texinfo->Attributes |= W3DTEXTURE_CLAMP_U;
  1012. if (tex->Get_Filter().Get_V_Addr_Mode() == TextureFilterClass::TEXTURE_ADDRESS_CLAMP) texinfo->Attributes |= W3DTEXTURE_CLAMP_V;
  1013. }
  1014. void Save_Texture(TextureClass * texture,ChunkSaveClass & csave)
  1015. {
  1016. const char * filename;
  1017. W3dTextureInfoStruct texinfo;
  1018. memset(&texinfo,0,sizeof(texinfo));
  1019. filename = texture->Get_Full_Path();
  1020. setup_texture_attributes(texture, &texinfo);
  1021. csave.Begin_Chunk(W3D_CHUNK_TEXTURE_NAME);
  1022. csave.Write(filename,strlen(filename)+1);
  1023. csave.End_Chunk();
  1024. if ((texinfo.Attributes != 0) || (texinfo.AnimType != 0) || (texinfo.FrameCount != 0)) {
  1025. csave.Begin_Chunk(W3D_CHUNK_TEXTURE_INFO);
  1026. csave.Write(&texinfo, sizeof(texinfo));
  1027. csave.End_Chunk();
  1028. }
  1029. }
  1030. /*!
  1031. * KJM depth stencil texture constructor
  1032. */
  1033. ZTextureClass::ZTextureClass
  1034. (
  1035. unsigned width,
  1036. unsigned height,
  1037. WW3DZFormat zformat,
  1038. MipCountType mip_level_count,
  1039. PoolType pool
  1040. )
  1041. : TextureBaseClass(width,height, mip_level_count, pool),
  1042. DepthStencilTextureFormat(zformat)
  1043. {
  1044. D3DPOOL d3dpool=(D3DPOOL)0;
  1045. switch (pool)
  1046. {
  1047. case POOL_DEFAULT: d3dpool=D3DPOOL_DEFAULT; break;
  1048. case POOL_MANAGED: d3dpool=D3DPOOL_MANAGED; break;
  1049. case POOL_SYSTEMMEM: d3dpool=D3DPOOL_SYSTEMMEM; break;
  1050. default: WWASSERT(0);
  1051. }
  1052. Poke_Texture
  1053. (
  1054. DX8Wrapper::_Create_DX8_ZTexture
  1055. (
  1056. width,
  1057. height,
  1058. zformat,
  1059. mip_level_count,
  1060. d3dpool
  1061. )
  1062. );
  1063. if (pool==POOL_DEFAULT)
  1064. {
  1065. Set_Dirty();
  1066. DX8ZTextureTrackerClass *track=new DX8ZTextureTrackerClass
  1067. (
  1068. width,
  1069. height,
  1070. zformat,
  1071. mip_level_count,
  1072. this
  1073. );
  1074. DX8TextureManagerClass::Add(track);
  1075. }
  1076. Initialized=true;
  1077. IsProcedural=true;
  1078. IsReducible=false;
  1079. LastAccessed=WW3D::Get_Sync_Time();
  1080. }
  1081. //**********************************************************************************************
  1082. //! Apply depth stencil texture
  1083. /*! KM
  1084. */
  1085. void ZTextureClass::Apply(unsigned int stage)
  1086. {
  1087. DX8Wrapper::Set_DX8_Texture(stage, Peek_D3D_Base_Texture());
  1088. }
  1089. //**********************************************************************************************
  1090. //! Apply new surface to texture
  1091. /*! KM
  1092. */
  1093. void ZTextureClass::Apply_New_Surface
  1094. (
  1095. IDirect3DBaseTexture8* d3d_texture,
  1096. bool initialized,
  1097. bool disable_auto_invalidation
  1098. )
  1099. {
  1100. IDirect3DBaseTexture8* d3d_tex=Peek_D3D_Base_Texture();
  1101. if (d3d_tex) d3d_tex->Release();
  1102. Poke_Texture(d3d_texture);//TextureLoadTask->Peek_D3D_Texture();
  1103. d3d_texture->AddRef();
  1104. if (initialized) Initialized=true;
  1105. if (disable_auto_invalidation) InactivationTime = 0;
  1106. WWASSERT(Peek_D3D_Texture());
  1107. IDirect3DSurface8* surface;
  1108. DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(0,&surface));
  1109. D3DSURFACE_DESC d3d_desc;
  1110. ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC));
  1111. DX8_ErrorCode(surface->GetDesc(&d3d_desc));
  1112. if (initialized)
  1113. {
  1114. DepthStencilTextureFormat=D3DFormat_To_WW3DZFormat(d3d_desc.Format);
  1115. Width=d3d_desc.Width;
  1116. Height=d3d_desc.Height;
  1117. }
  1118. surface->Release();
  1119. }
  1120. //**********************************************************************************************
  1121. //! Get D3D surface from mip level
  1122. /*!
  1123. */
  1124. IDirect3DSurface8* ZTextureClass::Get_D3D_Surface_Level(unsigned int level)
  1125. {
  1126. if (!Peek_D3D_Texture())
  1127. {
  1128. WWASSERT_PRINT(0, "Get_D3D_Surface_Level: D3DTexture is NULL!\n");
  1129. return 0;
  1130. }
  1131. IDirect3DSurface8 *d3d_surface = NULL;
  1132. DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(level, &d3d_surface));
  1133. return d3d_surface;
  1134. }
  1135. //**********************************************************************************************
  1136. //! Get texture memory usage
  1137. /*!
  1138. */
  1139. unsigned ZTextureClass::Get_Texture_Memory_Usage() const
  1140. {
  1141. int size=0;
  1142. if (!Peek_D3D_Texture()) return 0;
  1143. for (unsigned i=0;i<Peek_D3D_Texture()->GetLevelCount();++i)
  1144. {
  1145. D3DSURFACE_DESC desc;
  1146. DX8_ErrorCode(Peek_D3D_Texture()->GetLevelDesc(i,&desc));
  1147. size+=desc.Size;
  1148. }
  1149. return size;
  1150. }
  1151. /*************************************************************************
  1152. ** CubeTextureClass
  1153. *************************************************************************/
  1154. CubeTextureClass::CubeTextureClass
  1155. (
  1156. unsigned width,
  1157. unsigned height,
  1158. WW3DFormat format,
  1159. MipCountType mip_level_count,
  1160. PoolType pool,
  1161. bool rendertarget,
  1162. bool allow_reduction
  1163. )
  1164. : TextureClass(width, height, format, mip_level_count, pool, rendertarget)
  1165. {
  1166. Initialized=true;
  1167. IsProcedural=true;
  1168. IsReducible=false;
  1169. switch (format)
  1170. {
  1171. case WW3D_FORMAT_DXT1:
  1172. case WW3D_FORMAT_DXT2:
  1173. case WW3D_FORMAT_DXT3:
  1174. case WW3D_FORMAT_DXT4:
  1175. case WW3D_FORMAT_DXT5:
  1176. IsCompressionAllowed=true;
  1177. break;
  1178. default : break;
  1179. }
  1180. D3DPOOL d3dpool=(D3DPOOL)0;
  1181. switch(pool)
  1182. {
  1183. case POOL_DEFAULT : d3dpool=D3DPOOL_DEFAULT; break;
  1184. case POOL_MANAGED : d3dpool=D3DPOOL_MANAGED; break;
  1185. case POOL_SYSTEMMEM : d3dpool=D3DPOOL_SYSTEMMEM; break;
  1186. default: WWASSERT(0);
  1187. }
  1188. Poke_Texture
  1189. (
  1190. DX8Wrapper::_Create_DX8_Cube_Texture
  1191. (
  1192. width,
  1193. height,
  1194. format,
  1195. mip_level_count,
  1196. d3dpool,
  1197. rendertarget
  1198. )
  1199. );
  1200. if (pool==POOL_DEFAULT)
  1201. {
  1202. Set_Dirty();
  1203. DX8TextureTrackerClass *track=new DX8TextureTrackerClass
  1204. (
  1205. width,
  1206. height,
  1207. format,
  1208. mip_level_count,
  1209. this,
  1210. rendertarget
  1211. );
  1212. DX8TextureManagerClass::Add(track);
  1213. }
  1214. LastAccessed=WW3D::Get_Sync_Time();
  1215. }
  1216. // ----------------------------------------------------------------------------
  1217. CubeTextureClass::CubeTextureClass
  1218. (
  1219. const char *name,
  1220. const char *full_path,
  1221. MipCountType mip_level_count,
  1222. WW3DFormat texture_format,
  1223. bool allow_compression,
  1224. bool allow_reduction
  1225. )
  1226. : TextureClass(0,0,mip_level_count, POOL_MANAGED, false, texture_format)
  1227. {
  1228. IsCompressionAllowed=allow_compression;
  1229. InactivationTime=DEFAULT_INACTIVATION_TIME; // Default inactivation time 30 seconds
  1230. switch (TextureFormat)
  1231. {
  1232. case WW3D_FORMAT_DXT1:
  1233. case WW3D_FORMAT_DXT2:
  1234. case WW3D_FORMAT_DXT3:
  1235. case WW3D_FORMAT_DXT4:
  1236. case WW3D_FORMAT_DXT5:
  1237. IsCompressionAllowed=true;
  1238. break;
  1239. case WW3D_FORMAT_U8V8: // Bumpmap
  1240. case WW3D_FORMAT_L6V5U5: // Bumpmap
  1241. case WW3D_FORMAT_X8L8V8U8: // Bumpmap
  1242. // If requesting bumpmap format that isn't available we'll just return the surface in whatever color
  1243. // format the texture file is in. (This is illegal case, the format support should always be queried
  1244. // before creating a bump texture!)
  1245. if (!DX8Wrapper::Is_Initted() || !DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(TextureFormat))
  1246. {
  1247. TextureFormat=WW3D_FORMAT_UNKNOWN;
  1248. }
  1249. // If bump format is valid, make sure compression is not allowed so that we don't even attempt to load
  1250. // from a compressed file (quality isn't good enough for bump map). Also disable mipmapping.
  1251. else
  1252. {
  1253. IsCompressionAllowed=false;
  1254. MipLevelCount=MIP_LEVELS_1;
  1255. Filter.Set_Mip_Mapping(TextureFilterClass::FILTER_TYPE_NONE);
  1256. }
  1257. break;
  1258. default: break;
  1259. }
  1260. WWASSERT_PRINT(name && name[0], "TextureClass CTor: NULL or empty texture name\n");
  1261. int len=strlen(name);
  1262. for (int i=0;i<len;++i)
  1263. {
  1264. if (name[i]=='+')
  1265. {
  1266. IsLightmap=true;
  1267. // Set bilinear filtering for lightmaps (they are very stretched and
  1268. // low detail so we don't care for anisotropic or trilinear filtering...)
  1269. Filter.Set_Min_Filter(TextureFilterClass::FILTER_TYPE_FAST);
  1270. Filter.Set_Mag_Filter(TextureFilterClass::FILTER_TYPE_FAST);
  1271. if (mip_level_count!=MIP_LEVELS_1) Filter.Set_Mip_Mapping(TextureFilterClass::FILTER_TYPE_FAST);
  1272. break;
  1273. }
  1274. }
  1275. Set_Texture_Name(name);
  1276. Set_Full_Path(full_path);
  1277. WWASSERT(name[0]!='\0');
  1278. if (!WW3D::Is_Texturing_Enabled())
  1279. {
  1280. Initialized=true;
  1281. Poke_Texture(NULL);
  1282. }
  1283. // Find original size from the thumbnail (but don't create thumbnail texture yet!)
  1284. ThumbnailClass* thumb=ThumbnailManagerClass::Peek_Thumbnail_Instance_From_Any_Manager(Get_Full_Path());
  1285. if (thumb)
  1286. {
  1287. Width=thumb->Get_Original_Texture_Width();
  1288. Height=thumb->Get_Original_Texture_Height();
  1289. if (MipLevelCount!=MIP_LEVELS_1) {
  1290. MipLevelCount=(MipCountType)thumb->Get_Original_Texture_Mip_Level_Count();
  1291. }
  1292. }
  1293. LastAccessed=WW3D::Get_Sync_Time();
  1294. // If the thumbnails are not enabled, init the texture at this point to avoid stalling when the
  1295. // mesh is rendered.
  1296. if (!WW3D::Get_Thumbnail_Enabled())
  1297. {
  1298. if (TextureLoader::Is_DX8_Thread())
  1299. {
  1300. Init();
  1301. }
  1302. }
  1303. }
  1304. // don't know if these are needed
  1305. #if 0
  1306. // ----------------------------------------------------------------------------
  1307. CubeTextureClass::CubeTextureClass
  1308. (
  1309. SurfaceClass *surface,
  1310. MipCountType mip_level_count
  1311. )
  1312. : TextureClass(0,0,mip_level_count, POOL_MANAGED, false, surface->Get_Surface_Format())
  1313. {
  1314. IsProcedural=true;
  1315. Initialized=true;
  1316. IsReducible=false;
  1317. SurfaceClass::SurfaceDescription sd;
  1318. surface->Get_Description(sd);
  1319. Width=sd.Width;
  1320. Height=sd.Height;
  1321. switch (sd.Format)
  1322. {
  1323. case WW3D_FORMAT_DXT1:
  1324. case WW3D_FORMAT_DXT2:
  1325. case WW3D_FORMAT_DXT3:
  1326. case WW3D_FORMAT_DXT4:
  1327. case WW3D_FORMAT_DXT5:
  1328. IsCompressionAllowed=true;
  1329. break;
  1330. default: break;
  1331. }
  1332. Poke_Texture
  1333. (
  1334. DX8Wrapper::_Create_DX8_Cube_Texture
  1335. (
  1336. surface->Peek_D3D_Surface(),
  1337. mip_level_count
  1338. )
  1339. );
  1340. LastAccessed=WW3D::Get_Sync_Time();
  1341. }
  1342. // ----------------------------------------------------------------------------
  1343. CubeTextureClass::CubeTextureClass(IDirect3DBaseTexture8* d3d_texture)
  1344. : TextureBaseClass
  1345. (
  1346. 0,
  1347. 0,
  1348. ((MipCountType)d3d_texture->GetLevelCount())
  1349. ),
  1350. Filter((MipCountType)d3d_texture->GetLevelCount())
  1351. {
  1352. Initialized=true;
  1353. IsProcedural=true;
  1354. IsReducible=false;
  1355. Peek_Texture()->AddRef();
  1356. IDirect3DSurface8* surface;
  1357. DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(0,&surface));
  1358. D3DSURFACE_DESC d3d_desc;
  1359. ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC));
  1360. DX8_ErrorCode(surface->GetDesc(&d3d_desc));
  1361. Width=d3d_desc.Width;
  1362. Height=d3d_desc.Height;
  1363. TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
  1364. switch (TextureFormat)
  1365. {
  1366. case WW3D_FORMAT_DXT1:
  1367. case WW3D_FORMAT_DXT2:
  1368. case WW3D_FORMAT_DXT3:
  1369. case WW3D_FORMAT_DXT4:
  1370. case WW3D_FORMAT_DXT5:
  1371. IsCompressionAllowed=true;
  1372. break;
  1373. default: break;
  1374. }
  1375. LastAccessed=WW3D::Get_Sync_Time();
  1376. }
  1377. #endif
  1378. //**********************************************************************************************
  1379. //! Apply new surface to texture
  1380. /*!
  1381. */
  1382. void CubeTextureClass::Apply_New_Surface
  1383. (
  1384. IDirect3DBaseTexture8* d3d_texture,
  1385. bool initialized,
  1386. bool disable_auto_invalidation
  1387. )
  1388. {
  1389. IDirect3DBaseTexture8* d3d_tex=Peek_D3D_Base_Texture();
  1390. if (d3d_tex) d3d_tex->Release();
  1391. Poke_Texture(d3d_texture);//TextureLoadTask->Peek_D3D_Texture();
  1392. d3d_texture->AddRef();
  1393. if (initialized) Initialized=true;
  1394. if (disable_auto_invalidation) InactivationTime = 0;
  1395. WWASSERT(d3d_texture);
  1396. D3DSURFACE_DESC d3d_desc;
  1397. ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC));
  1398. DX8_ErrorCode(Peek_D3D_CubeTexture()->GetLevelDesc(0,&d3d_desc));
  1399. if (initialized)
  1400. {
  1401. TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
  1402. Width=d3d_desc.Width;
  1403. Height=d3d_desc.Height;
  1404. }
  1405. }
  1406. /*************************************************************************
  1407. ** VolumeTextureClass
  1408. *************************************************************************/
  1409. VolumeTextureClass::VolumeTextureClass
  1410. (
  1411. unsigned width,
  1412. unsigned height,
  1413. unsigned depth,
  1414. WW3DFormat format,
  1415. MipCountType mip_level_count,
  1416. PoolType pool,
  1417. bool rendertarget,
  1418. bool allow_reduction
  1419. )
  1420. : TextureClass(width, height, format, mip_level_count, pool, rendertarget),
  1421. Depth(depth)
  1422. {
  1423. Initialized=true;
  1424. IsProcedural=true;
  1425. IsReducible=false;
  1426. switch (format)
  1427. {
  1428. case WW3D_FORMAT_DXT1:
  1429. case WW3D_FORMAT_DXT2:
  1430. case WW3D_FORMAT_DXT3:
  1431. case WW3D_FORMAT_DXT4:
  1432. case WW3D_FORMAT_DXT5:
  1433. IsCompressionAllowed=true;
  1434. break;
  1435. default : break;
  1436. }
  1437. D3DPOOL d3dpool=(D3DPOOL)0;
  1438. switch(pool)
  1439. {
  1440. case POOL_DEFAULT : d3dpool=D3DPOOL_DEFAULT; break;
  1441. case POOL_MANAGED : d3dpool=D3DPOOL_MANAGED; break;
  1442. case POOL_SYSTEMMEM : d3dpool=D3DPOOL_SYSTEMMEM; break;
  1443. default: WWASSERT(0);
  1444. }
  1445. Poke_Texture
  1446. (
  1447. DX8Wrapper::_Create_DX8_Volume_Texture
  1448. (
  1449. width,
  1450. height,
  1451. depth,
  1452. format,
  1453. mip_level_count,
  1454. d3dpool
  1455. )
  1456. );
  1457. if (pool==POOL_DEFAULT)
  1458. {
  1459. Set_Dirty();
  1460. DX8TextureTrackerClass *track=new DX8TextureTrackerClass
  1461. (
  1462. width,
  1463. height,
  1464. format,
  1465. mip_level_count,
  1466. this,
  1467. rendertarget
  1468. );
  1469. DX8TextureManagerClass::Add(track);
  1470. }
  1471. LastAccessed=WW3D::Get_Sync_Time();
  1472. }
  1473. // ----------------------------------------------------------------------------
  1474. VolumeTextureClass::VolumeTextureClass
  1475. (
  1476. const char *name,
  1477. const char *full_path,
  1478. MipCountType mip_level_count,
  1479. WW3DFormat texture_format,
  1480. bool allow_compression,
  1481. bool allow_reduction
  1482. )
  1483. : TextureClass(0,0,mip_level_count, POOL_MANAGED, false, texture_format),
  1484. Depth(0)
  1485. {
  1486. IsCompressionAllowed=allow_compression;
  1487. InactivationTime=DEFAULT_INACTIVATION_TIME; // Default inactivation time 30 seconds
  1488. switch (TextureFormat)
  1489. {
  1490. case WW3D_FORMAT_DXT1:
  1491. case WW3D_FORMAT_DXT2:
  1492. case WW3D_FORMAT_DXT3:
  1493. case WW3D_FORMAT_DXT4:
  1494. case WW3D_FORMAT_DXT5:
  1495. IsCompressionAllowed=true;
  1496. break;
  1497. case WW3D_FORMAT_U8V8: // Bumpmap
  1498. case WW3D_FORMAT_L6V5U5: // Bumpmap
  1499. case WW3D_FORMAT_X8L8V8U8: // Bumpmap
  1500. // If requesting bumpmap format that isn't available we'll just return the surface in whatever color
  1501. // format the texture file is in. (This is illegal case, the format support should always be queried
  1502. // before creating a bump texture!)
  1503. if (!DX8Wrapper::Is_Initted() || !DX8Wrapper::Get_Current_Caps()->Support_Texture_Format(TextureFormat))
  1504. {
  1505. TextureFormat=WW3D_FORMAT_UNKNOWN;
  1506. }
  1507. // If bump format is valid, make sure compression is not allowed so that we don't even attempt to load
  1508. // from a compressed file (quality isn't good enough for bump map). Also disable mipmapping.
  1509. else
  1510. {
  1511. IsCompressionAllowed=false;
  1512. MipLevelCount=MIP_LEVELS_1;
  1513. Filter.Set_Mip_Mapping(TextureFilterClass::FILTER_TYPE_NONE);
  1514. }
  1515. break;
  1516. default: break;
  1517. }
  1518. WWASSERT_PRINT(name && name[0], "TextureClass CTor: NULL or empty texture name\n");
  1519. int len=strlen(name);
  1520. for (int i=0;i<len;++i)
  1521. {
  1522. if (name[i]=='+')
  1523. {
  1524. IsLightmap=true;
  1525. // Set bilinear filtering for lightmaps (they are very stretched and
  1526. // low detail so we don't care for anisotropic or trilinear filtering...)
  1527. Filter.Set_Min_Filter(TextureFilterClass::FILTER_TYPE_FAST);
  1528. Filter.Set_Mag_Filter(TextureFilterClass::FILTER_TYPE_FAST);
  1529. if (mip_level_count!=MIP_LEVELS_1) Filter.Set_Mip_Mapping(TextureFilterClass::FILTER_TYPE_FAST);
  1530. break;
  1531. }
  1532. }
  1533. Set_Texture_Name(name);
  1534. Set_Full_Path(full_path);
  1535. WWASSERT(name[0]!='\0');
  1536. if (!WW3D::Is_Texturing_Enabled())
  1537. {
  1538. Initialized=true;
  1539. Poke_Texture(NULL);
  1540. }
  1541. // Find original size from the thumbnail (but don't create thumbnail texture yet!)
  1542. ThumbnailClass* thumb=ThumbnailManagerClass::Peek_Thumbnail_Instance_From_Any_Manager(Get_Full_Path());
  1543. if (thumb)
  1544. {
  1545. Width=thumb->Get_Original_Texture_Width();
  1546. Height=thumb->Get_Original_Texture_Height();
  1547. if (MipLevelCount!=MIP_LEVELS_1) {
  1548. MipLevelCount=(MipCountType)thumb->Get_Original_Texture_Mip_Level_Count();
  1549. }
  1550. }
  1551. LastAccessed=WW3D::Get_Sync_Time();
  1552. // If the thumbnails are not enabled, init the texture at this point to avoid stalling when the
  1553. // mesh is rendered.
  1554. if (!WW3D::Get_Thumbnail_Enabled())
  1555. {
  1556. if (TextureLoader::Is_DX8_Thread())
  1557. {
  1558. Init();
  1559. }
  1560. }
  1561. }
  1562. // don't know if these are needed
  1563. #if 0
  1564. // ----------------------------------------------------------------------------
  1565. CubeTextureClass::CubeTextureClass
  1566. (
  1567. SurfaceClass *surface,
  1568. MipCountType mip_level_count
  1569. )
  1570. : TextureClass(0,0,mip_level_count, POOL_MANAGED, false, surface->Get_Surface_Format())
  1571. {
  1572. IsProcedural=true;
  1573. Initialized=true;
  1574. IsReducible=false;
  1575. SurfaceClass::SurfaceDescription sd;
  1576. surface->Get_Description(sd);
  1577. Width=sd.Width;
  1578. Height=sd.Height;
  1579. switch (sd.Format)
  1580. {
  1581. case WW3D_FORMAT_DXT1:
  1582. case WW3D_FORMAT_DXT2:
  1583. case WW3D_FORMAT_DXT3:
  1584. case WW3D_FORMAT_DXT4:
  1585. case WW3D_FORMAT_DXT5:
  1586. IsCompressionAllowed=true;
  1587. break;
  1588. default: break;
  1589. }
  1590. Poke_Texture
  1591. (
  1592. DX8Wrapper::_Create_DX8_Cube_Texture
  1593. (
  1594. surface->Peek_D3D_Surface(),
  1595. mip_level_count
  1596. )
  1597. );
  1598. LastAccessed=WW3D::Get_Sync_Time();
  1599. }
  1600. // ----------------------------------------------------------------------------
  1601. CubeTextureClass::CubeTextureClass(IDirect3DBaseTexture8* d3d_texture)
  1602. : TextureBaseClass
  1603. (
  1604. 0,
  1605. 0,
  1606. ((MipCountType)d3d_texture->GetLevelCount())
  1607. ),
  1608. Filter((MipCountType)d3d_texture->GetLevelCount())
  1609. {
  1610. Initialized=true;
  1611. IsProcedural=true;
  1612. IsReducible=false;
  1613. Peek_Texture()->AddRef();
  1614. IDirect3DSurface8* surface;
  1615. DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(0,&surface));
  1616. D3DSURFACE_DESC d3d_desc;
  1617. ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC));
  1618. DX8_ErrorCode(surface->GetDesc(&d3d_desc));
  1619. Width=d3d_desc.Width;
  1620. Height=d3d_desc.Height;
  1621. TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
  1622. switch (TextureFormat)
  1623. {
  1624. case WW3D_FORMAT_DXT1:
  1625. case WW3D_FORMAT_DXT2:
  1626. case WW3D_FORMAT_DXT3:
  1627. case WW3D_FORMAT_DXT4:
  1628. case WW3D_FORMAT_DXT5:
  1629. IsCompressionAllowed=true;
  1630. break;
  1631. default: break;
  1632. }
  1633. LastAccessed=WW3D::Get_Sync_Time();
  1634. }
  1635. #endif
  1636. //**********************************************************************************************
  1637. //! Apply new surface to texture
  1638. /*!
  1639. */
  1640. void VolumeTextureClass::Apply_New_Surface
  1641. (
  1642. IDirect3DBaseTexture8* d3d_texture,
  1643. bool initialized,
  1644. bool disable_auto_invalidation
  1645. )
  1646. {
  1647. IDirect3DBaseTexture8* d3d_tex=Peek_D3D_Base_Texture();
  1648. if (d3d_tex) d3d_tex->Release();
  1649. Poke_Texture(d3d_texture);//TextureLoadTask->Peek_D3D_Texture();
  1650. d3d_texture->AddRef();
  1651. if (initialized) Initialized=true;
  1652. if (disable_auto_invalidation) InactivationTime = 0;
  1653. WWASSERT(d3d_texture);
  1654. D3DVOLUME_DESC d3d_desc;
  1655. ::ZeroMemory(&d3d_desc, sizeof(D3DVOLUME_DESC));
  1656. DX8_ErrorCode(Peek_D3D_VolumeTexture()->GetLevelDesc(0,&d3d_desc));
  1657. if (initialized)
  1658. {
  1659. TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
  1660. Width=d3d_desc.Width;
  1661. Height=d3d_desc.Height;
  1662. Depth=d3d_desc.Depth;
  1663. }
  1664. }