texture.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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.h $*
  25. * *
  26. * $Org Author:: Jani_p $*
  27. * *
  28. * Author : Kenny Mitchell *
  29. * *
  30. * $Modtime:: 08/05/02 1:27p $*
  31. * *
  32. * $Revision:: 46 $*
  33. * *
  34. * 05/16/02 KM Base texture class to abstract major texture types, e.g. 3d, z, cube, etc.
  35. * 06/27/02 KM Texture class abstraction *
  36. * 08/05/02 KM Texture class redesign (revisited)
  37. *---------------------------------------------------------------------------------------------*
  38. * Functions: *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #if defined(_MSC_VER)
  41. #pragma once
  42. #endif
  43. #ifndef TEXTURE_H
  44. #define TEXTURE_H
  45. #include "always.h"
  46. #include "refcount.h"
  47. #include "chunkio.h"
  48. #include "surfaceclass.h"
  49. #include "ww3dformat.h"
  50. #include "wwstring.h"
  51. #include "vector3.h"
  52. #include "texturefilter.h"
  53. struct IDirect3DBaseTexture8;
  54. struct IDirect3DTexture8;
  55. struct IDirect3DCubeTexture8;
  56. struct IDirect3DVolumeTexture8;
  57. class DX8Wrapper;
  58. class TextureLoader;
  59. class LoaderThreadClass;
  60. class TextureLoadTaskClass;
  61. class CubeTextureClass;
  62. class VolumeTextureClass;
  63. class TextureBaseClass : public RefCountClass
  64. {
  65. friend class TextureLoader;
  66. friend class LoaderThreadClass;
  67. friend class DX8TextureTrackerClass; //(gth) so it can call Poke_Texture,
  68. friend class DX8ZTextureTrackerClass;
  69. public:
  70. enum PoolType
  71. {
  72. POOL_DEFAULT=0,
  73. POOL_MANAGED,
  74. POOL_SYSTEMMEM
  75. };
  76. enum TexAssetType
  77. {
  78. TEX_REGULAR,
  79. TEX_CUBEMAP,
  80. TEX_VOLUME
  81. };
  82. // base constructor for derived classes
  83. TextureBaseClass
  84. (
  85. unsigned width,
  86. unsigned height,
  87. MipCountType mip_level_count=MIP_LEVELS_ALL,
  88. PoolType pool=POOL_MANAGED,
  89. bool rendertarget=false,
  90. bool reducible=true
  91. );
  92. virtual ~TextureBaseClass();
  93. virtual TexAssetType Get_Asset_Type() const=0;
  94. // Names
  95. void Set_Texture_Name(const char * name);
  96. void Set_Full_Path(const char * path) { FullPath = path; }
  97. const StringClass& Get_Texture_Name(void) const { return Name; }
  98. const StringClass& Get_Full_Path(void) const { if (FullPath.Is_Empty ()) return Name; return FullPath; }
  99. unsigned Get_ID() const { return texture_id; } // Each textrure has a unique id
  100. // The number of Mip levels in the texture
  101. unsigned int Get_Mip_Level_Count(void) const
  102. {
  103. return MipLevelCount;
  104. }
  105. // Note! Width and Height may be zero and may change if texture uses mipmaps
  106. int Get_Width() const
  107. {
  108. return Width;
  109. }
  110. int Get_Height() const
  111. {
  112. return Height;
  113. }
  114. // Time, after which the texture is invalidated if not used. Set to zero to indicate infinite.
  115. // Time is in milliseconds.
  116. void Set_Inactivation_Time(unsigned time) { InactivationTime=time; }
  117. int Get_Inactivation_Time() const { return InactivationTime; }
  118. // Texture priority affects texture management and caching.
  119. unsigned int Get_Priority(void);
  120. unsigned int Set_Priority(unsigned int priority); // Returns previous priority
  121. // Debug utility functions for returning the texture memory usage
  122. virtual unsigned Get_Texture_Memory_Usage() const=0;
  123. bool Is_Initialized() const { return Initialized; }
  124. bool Is_Lightmap() const { return IsLightmap; }
  125. bool Is_Procedural() const { return IsProcedural; }
  126. bool Is_Reducible() const { return IsReducible; } //can texture be reduced in resolution for LOD purposes?
  127. static int _Get_Total_Locked_Surface_Size();
  128. static int _Get_Total_Texture_Size();
  129. static int _Get_Total_Lightmap_Texture_Size();
  130. static int _Get_Total_Procedural_Texture_Size();
  131. static int _Get_Total_Locked_Surface_Count();
  132. static int _Get_Total_Texture_Count();
  133. static int _Get_Total_Lightmap_Texture_Count();
  134. static int _Get_Total_Procedural_Texture_Count();
  135. virtual void Init()=0;
  136. // This utility function processes the texture reduction (used during rendering)
  137. void Invalidate();
  138. // texture accessors (dx8)
  139. IDirect3DBaseTexture8 *Peek_D3D_Base_Texture() const;
  140. void Set_D3D_Base_Texture(IDirect3DBaseTexture8* tex);
  141. PoolType Get_Pool() const { return Pool; }
  142. bool Is_Missing_Texture();
  143. // Support for self managed textures
  144. bool Is_Dirty() { WWASSERT(Pool==POOL_DEFAULT); return Dirty; };
  145. void Set_Dirty() { WWASSERT(Pool==POOL_DEFAULT); Dirty=true; }
  146. void Clean() { Dirty=false; };
  147. void Set_HSV_Shift(const Vector3 &hsv_shift);
  148. const Vector3& Get_HSV_Shift() { return HSVShift; }
  149. bool Is_Compression_Allowed() const { return IsCompressionAllowed; }
  150. unsigned Get_Reduction() const;
  151. // Background texture loader will call this when texture has been loaded
  152. virtual void Apply_New_Surface(IDirect3DBaseTexture8* tex, bool initialized, bool disable_auto_invalidation = false)=0; // If the parameter is true, the texture will be flagged as initialised
  153. MipCountType MipLevelCount;
  154. // Inactivate textures that haven't been used in a while. Pass zero to use textures'
  155. // own inactive times (default). In urgent need to free up texture memory, try
  156. // calling with relatively small (just few seconds) time override to free up everything
  157. // but the currently used textures.
  158. static void Invalidate_Old_Unused_Textures(unsigned inactive_time_override);
  159. // Apply this texture's settings into D3D
  160. virtual void Apply(unsigned int stage)=0;
  161. // Apply a Null texture's settings into D3D
  162. static void Apply_Null(unsigned int stage);
  163. virtual TextureClass* As_TextureClass() { return NULL; }
  164. virtual CubeTextureClass* As_CubeTextureClass() { return NULL; }
  165. virtual VolumeTextureClass* As_VolumeTextureClass() { return NULL; }
  166. IDirect3DTexture8* Peek_D3D_Texture() const { return (IDirect3DTexture8*)Peek_D3D_Base_Texture(); }
  167. IDirect3DVolumeTexture8* Peek_D3D_VolumeTexture() const { return (IDirect3DVolumeTexture8*)Peek_D3D_Base_Texture(); }
  168. IDirect3DCubeTexture8* Peek_D3D_CubeTexture() const { return (IDirect3DCubeTexture8*)Peek_D3D_Base_Texture(); }
  169. protected:
  170. void Load_Locked_Surface();
  171. void Poke_Texture(IDirect3DBaseTexture8* tex) { D3DTexture = tex; }
  172. bool Initialized;
  173. // For debug purposes the texture sets this true if it is a lightmap texture
  174. bool IsLightmap;
  175. bool IsCompressionAllowed;
  176. bool IsProcedural;
  177. bool IsReducible;
  178. unsigned InactivationTime; // In milliseconds
  179. unsigned ExtendedInactivationTime; // This is set by the engine, if needed
  180. unsigned LastInactivationSyncTime;
  181. mutable unsigned LastAccessed;
  182. // If this is non-zero, the texture will have a hue shift done at the next init (this
  183. // value should only be changed by Set_HSV_Shift() function, which also invalidates the
  184. // texture).
  185. Vector3 HSVShift;
  186. int Width;
  187. int Height;
  188. private:
  189. // Direct3D texture object
  190. IDirect3DBaseTexture8 *D3DTexture;
  191. // Name
  192. StringClass Name;
  193. StringClass FullPath;
  194. // Unique id
  195. unsigned texture_id;
  196. // Support for self-managed textures
  197. PoolType Pool;
  198. bool Dirty;
  199. friend class TextureLoadTaskClass;
  200. friend class CubeTextureLoadTaskClass;
  201. friend class VolumeTextureLoadTaskClass;
  202. TextureLoadTaskClass* TextureLoadTask;
  203. TextureLoadTaskClass* ThumbnailLoadTask;
  204. };
  205. /*************************************************************************
  206. ** TextureClass
  207. **
  208. ** This is our regular texture class. For legacy reasons it contains some
  209. ** information beyond the D3D texture itself, such as texture addressing
  210. ** modes.
  211. **
  212. *************************************************************************/
  213. class TextureClass : public TextureBaseClass
  214. {
  215. W3DMPO_GLUE(TextureClass)
  216. // friend DX8Wrapper;
  217. public:
  218. // Create texture with desired height, width and format.
  219. TextureClass
  220. (
  221. unsigned width,
  222. unsigned height,
  223. WW3DFormat format,
  224. MipCountType mip_level_count=MIP_LEVELS_ALL,
  225. PoolType pool=POOL_MANAGED,
  226. bool rendertarget=false,
  227. bool allow_reduction=true
  228. );
  229. // Create texture from a file. If format is specified the texture is converted to that format.
  230. // Note that the format must be supported by the current device and that a texture can't exist
  231. // in the system with the same name in multiple formats.
  232. TextureClass
  233. (
  234. const char *name,
  235. const char *full_path=NULL,
  236. MipCountType mip_level_count=MIP_LEVELS_ALL,
  237. WW3DFormat texture_format=WW3D_FORMAT_UNKNOWN,
  238. bool allow_compression=true,
  239. bool allow_reduction=true
  240. );
  241. // Create texture from a surface.
  242. TextureClass
  243. (
  244. SurfaceClass *surface,
  245. MipCountType mip_level_count=MIP_LEVELS_ALL
  246. );
  247. TextureClass(IDirect3DBaseTexture8* d3d_texture);
  248. // defualt constructors for derived classes (cube & vol)
  249. TextureClass
  250. (
  251. unsigned width,
  252. unsigned height,
  253. MipCountType mip_level_count=MIP_LEVELS_ALL,
  254. PoolType pool=POOL_MANAGED,
  255. bool rendertarget=false,
  256. WW3DFormat format=WW3D_FORMAT_UNKNOWN,
  257. bool allow_reduction=true
  258. )
  259. : TextureBaseClass(width,height,mip_level_count,pool,rendertarget,allow_reduction), TextureFormat(format), Filter(mip_level_count) { }
  260. virtual TexAssetType Get_Asset_Type() const { return TEX_REGULAR; }
  261. virtual void Init();
  262. // Background texture loader will call this when texture has been loaded
  263. virtual void Apply_New_Surface(IDirect3DBaseTexture8* tex, bool initialized, bool disable_auto_invalidation = false); // If the parameter is true, the texture will be flagged as initialised
  264. // Get the surface of one of the mipmap levels (defaults to highest-resolution one)
  265. SurfaceClass *Get_Surface_Level(unsigned int level = 0);
  266. IDirect3DSurface8 *Get_D3D_Surface_Level(unsigned int level = 0);
  267. void Get_Level_Description( SurfaceClass::SurfaceDescription & desc, unsigned int level = 0 );
  268. TextureFilterClass& Get_Filter() { return Filter; }
  269. WW3DFormat Get_Texture_Format() const { return TextureFormat; }
  270. virtual void Apply(unsigned int stage);
  271. virtual unsigned Get_Texture_Memory_Usage() const;
  272. virtual TextureClass* As_TextureClass() { return this; }
  273. protected:
  274. WW3DFormat TextureFormat;
  275. // legacy
  276. TextureFilterClass Filter;
  277. };
  278. class ZTextureClass : public TextureBaseClass
  279. {
  280. public:
  281. // Create a z texture with desired height, width and format
  282. ZTextureClass
  283. (
  284. unsigned width,
  285. unsigned height,
  286. WW3DZFormat zformat,
  287. MipCountType mip_level_count=MIP_LEVELS_ALL,
  288. PoolType pool=POOL_MANAGED
  289. );
  290. WW3DZFormat Get_Texture_Format() const { return DepthStencilTextureFormat; }
  291. virtual TexAssetType Get_Asset_Type() const { return TEX_REGULAR; }
  292. virtual void Init() {}
  293. // Background texture loader will call this when texture has been loaded
  294. virtual void Apply_New_Surface(IDirect3DBaseTexture8* tex, bool initialized, bool disable_auto_invalidation = false); // If the parameter is true, the texture will be flagged as initialised
  295. virtual void Apply(unsigned int stage);
  296. IDirect3DSurface8 *Get_D3D_Surface_Level(unsigned int level = 0);
  297. virtual unsigned Get_Texture_Memory_Usage() const;
  298. private:
  299. WW3DZFormat DepthStencilTextureFormat;
  300. };
  301. class CubeTextureClass : public TextureClass
  302. {
  303. public:
  304. // Create texture with desired height, width and format.
  305. CubeTextureClass
  306. (
  307. unsigned width,
  308. unsigned height,
  309. WW3DFormat format,
  310. MipCountType mip_level_count=MIP_LEVELS_ALL,
  311. PoolType pool=POOL_MANAGED,
  312. bool rendertarget=false,
  313. bool allow_reduction=true
  314. );
  315. // Create texture from a file. If format is specified the texture is converted to that format.
  316. // Note that the format must be supported by the current device and that a texture can't exist
  317. // in the system with the same name in multiple formats.
  318. CubeTextureClass
  319. (
  320. const char *name,
  321. const char *full_path=NULL,
  322. MipCountType mip_level_count=MIP_LEVELS_ALL,
  323. WW3DFormat texture_format=WW3D_FORMAT_UNKNOWN,
  324. bool allow_compression=true,
  325. bool allow_reduction=true
  326. );
  327. // Create texture from a surface.
  328. CubeTextureClass
  329. (
  330. SurfaceClass *surface,
  331. MipCountType mip_level_count=MIP_LEVELS_ALL
  332. );
  333. CubeTextureClass(IDirect3DBaseTexture8* d3d_texture);
  334. virtual void Apply_New_Surface(IDirect3DBaseTexture8* tex, bool initialized, bool disable_auto_invalidation = false); // If the parameter is true, the texture will be flagged as initialised
  335. virtual TexAssetType Get_Asset_Type() const { return TEX_CUBEMAP; }
  336. virtual CubeTextureClass* As_CubeTextureClass() { return this; }
  337. };
  338. class VolumeTextureClass : public TextureClass
  339. {
  340. public:
  341. // Create texture with desired height, width and format.
  342. VolumeTextureClass
  343. (
  344. unsigned width,
  345. unsigned height,
  346. unsigned depth,
  347. WW3DFormat format,
  348. MipCountType mip_level_count=MIP_LEVELS_ALL,
  349. PoolType pool=POOL_MANAGED,
  350. bool rendertarget=false,
  351. bool allow_reduction=true
  352. );
  353. // Create texture from a file. If format is specified the texture is converted to that format.
  354. // Note that the format must be supported by the current device and that a texture can't exist
  355. // in the system with the same name in multiple formats.
  356. VolumeTextureClass
  357. (
  358. const char *name,
  359. const char *full_path=NULL,
  360. MipCountType mip_level_count=MIP_LEVELS_ALL,
  361. WW3DFormat texture_format=WW3D_FORMAT_UNKNOWN,
  362. bool allow_compression=true,
  363. bool allow_reduction=true
  364. );
  365. // Create texture from a surface.
  366. VolumeTextureClass
  367. (
  368. SurfaceClass *surface,
  369. MipCountType mip_level_count=MIP_LEVELS_ALL
  370. );
  371. VolumeTextureClass(IDirect3DBaseTexture8* d3d_texture);
  372. virtual void Apply_New_Surface(IDirect3DBaseTexture8* tex, bool initialized, bool disable_auto_invalidation = false); // If the parameter is true, the texture will be flagged as initialised
  373. virtual TexAssetType Get_Asset_Type() const { return TEX_VOLUME; }
  374. virtual VolumeTextureClass* As_VolumeTextureClass() { return this; }
  375. protected:
  376. int Depth;
  377. };
  378. // Utility functions for loading and saving texture descriptions from/to W3D files
  379. TextureClass *Load_Texture(ChunkLoadClass & cload);
  380. void Save_Texture(TextureClass * texture, ChunkSaveClass & csave);
  381. #endif //TEXTURE_H