texture.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/texture.h $*
  25. * *
  26. * $Author:: Jani_p $*
  27. * *
  28. * $Modtime:: 11/09/01 5:16p $*
  29. * *
  30. * $Revision:: 44 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef TEXTURE_H
  39. #define TEXTURE_H
  40. #include "always.h"
  41. #include "refcount.h"
  42. #include "chunkio.h"
  43. #include "surfaceclass.h"
  44. #include "ww3dformat.h"
  45. #include "wwstring.h"
  46. class DX8Wrapper;
  47. struct IDirect3DTexture8;
  48. class TextureLoader;
  49. class LoaderThreadClass;
  50. class DX8TextureManagerClass;
  51. class TextureLoadTaskClass;
  52. /*************************************************************************
  53. ** TextureClass
  54. **
  55. ** This is our texture class. For legacy reasons it contains some
  56. ** information beyond the D3D texture itself, such as texture addressing
  57. ** modes.
  58. **
  59. *************************************************************************/
  60. class TextureClass : public RefCountClass
  61. {
  62. friend DX8Wrapper;
  63. friend TextureLoader;
  64. friend LoaderThreadClass;
  65. friend DX8TextureManagerClass;
  66. public:
  67. enum PoolType {
  68. POOL_DEFAULT=0,
  69. POOL_MANAGED,
  70. POOL_SYSTEMMEM
  71. };
  72. enum FilterType {
  73. FILTER_TYPE_NONE,
  74. FILTER_TYPE_FAST,
  75. FILTER_TYPE_BEST,
  76. FILTER_TYPE_DEFAULT,
  77. FILTER_TYPE_COUNT
  78. };
  79. enum TextureFilterMode {
  80. TEXTURE_FILTER_BILINEAR,
  81. TEXTURE_FILTER_TRILINEAR,
  82. TEXTURE_FILTER_ANISOTROPIC
  83. };
  84. enum TxtAddrMode {
  85. TEXTURE_ADDRESS_REPEAT=0,
  86. TEXTURE_ADDRESS_CLAMP
  87. };
  88. enum MipCountType {
  89. MIP_LEVELS_ALL=0, // generate all mipmap levels down to 1x1 size
  90. MIP_LEVELS_1, // no mipmapping at all (just one mip level)
  91. MIP_LEVELS_2,
  92. MIP_LEVELS_3,
  93. MIP_LEVELS_4,
  94. MIP_LEVELS_5,
  95. MIP_LEVELS_6,
  96. MIP_LEVELS_7,
  97. MIP_LEVELS_8,
  98. MIP_LEVELS_10,
  99. MIP_LEVELS_11,
  100. MIP_LEVELS_12,
  101. MIP_LEVELS_MAX // This isn't to be used (use MIP_LEVELS_ALL instead), it is just an enum for creating static tables etc.
  102. };
  103. // Create texture with desired height, width and format.
  104. TextureClass(
  105. unsigned width,
  106. unsigned height,
  107. WW3DFormat format,
  108. MipCountType mip_level_count=MIP_LEVELS_ALL,
  109. PoolType pool=POOL_MANAGED,
  110. bool rendertarget=false);
  111. // Create texture from a file. If format is specified the texture is converted to that format.
  112. // Note that the format must be supported by the current device and that a texture can't exist
  113. // in the system with the same name in multiple formats.
  114. TextureClass(
  115. const char *name,
  116. const char *full_path=NULL,
  117. MipCountType mip_level_count=MIP_LEVELS_ALL,
  118. WW3DFormat texture_format=WW3D_FORMAT_UNKNOWN,
  119. bool allow_compression=true);
  120. // Create texture from a surface.
  121. TextureClass(
  122. SurfaceClass *surface,
  123. MipCountType mip_level_count=MIP_LEVELS_ALL);
  124. TextureClass(IDirect3DTexture8* d3d_texture);
  125. virtual ~TextureClass(void);
  126. // Names
  127. void Set_Texture_Name(const char * name);
  128. void Set_Full_Path(const char * path) { FullPath = path; }
  129. const StringClass& Get_Texture_Name(void) const { return Name; }
  130. const StringClass& Get_Full_Path(void) const { if (FullPath.Is_Empty ()) return Name; return FullPath; }
  131. unsigned Get_ID() const { return texture_id; } // Each textrure has a unique id
  132. // The number of Mip levels in the texture
  133. unsigned int Get_Mip_Level_Count(void);
  134. // Note! Width and Height may be zero and may change if texture uses mipmaps
  135. int Get_Width()
  136. {
  137. return Width;
  138. }
  139. int Get_Height()
  140. {
  141. return Height;
  142. }
  143. void Init();
  144. // Time, after which the texture is invalidated if not used. Set to zero to indicate infinite.
  145. // Time is in milliseconds.
  146. void Set_Inactivation_Time(unsigned time) { InactivationTime=time; }
  147. int Get_Inactivation_Time() const { return InactivationTime; }
  148. // Get the surface of one of the mipmap levels (defaults to highest-resolution one)
  149. SurfaceClass *Get_Surface_Level(unsigned int level = 0);
  150. IDirect3DSurface8 *Get_D3D_Surface_Level(unsigned int level = 0);
  151. // Texture priority affects texture management and caching.
  152. unsigned int Get_Priority(void);
  153. unsigned int Set_Priority(unsigned int priority); // Returns previous priority
  154. // Filter and MIPmap settings:
  155. FilterType Get_Min_Filter(void) const { return TextureMinFilter; }
  156. FilterType Get_Mag_Filter(void) const { return TextureMagFilter; }
  157. FilterType Get_Mip_Mapping(void) const { return MipMapFilter; }
  158. void Set_Min_Filter(FilterType filter) { TextureMinFilter=filter; }
  159. void Set_Mag_Filter(FilterType filter) { TextureMagFilter=filter; }
  160. void Set_Mip_Mapping(FilterType mipmap);
  161. // Texture address mode
  162. TxtAddrMode Get_U_Addr_Mode(void) const { return UAddressMode; }
  163. TxtAddrMode Get_V_Addr_Mode(void) const { return VAddressMode; }
  164. void Set_U_Addr_Mode(TxtAddrMode mode) { UAddressMode=mode; }
  165. void Set_V_Addr_Mode(TxtAddrMode mode) { VAddressMode=mode; }
  166. // Debug utility functions for returning the texture memory usage
  167. unsigned Get_Texture_Memory_Usage() const;
  168. bool Is_Initialized() const { return Initialized; }
  169. bool Is_Lightmap() const { return IsLightmap; }
  170. bool Is_Procedural() const { return IsProcedural; }
  171. static int _Get_Total_Locked_Surface_Size();
  172. static int _Get_Total_Texture_Size();
  173. static int _Get_Total_Lightmap_Texture_Size();
  174. static int _Get_Total_Procedural_Texture_Size();
  175. static int _Get_Total_Locked_Surface_Count();
  176. static int _Get_Total_Texture_Count();
  177. static int _Get_Total_Lightmap_Texture_Count();
  178. static int _Get_Total_Procedural_Texture_Count();
  179. // This needs to be called after device has been created
  180. static void _Init_Filters(TextureFilterMode texture_filter);
  181. static void _Set_Default_Min_Filter(FilterType filter);
  182. static void _Set_Default_Mag_Filter(FilterType filter);
  183. static void _Set_Default_Mip_Filter(FilterType filter);
  184. // This utility function processes the texture reduction (used during rendering)
  185. void Invalidate();
  186. IDirect3DTexture8 *Peek_DX8_Texture()
  187. {
  188. return D3DTexture;
  189. }
  190. bool Is_Missing_Texture();
  191. // Support for self managed textures
  192. bool Is_Dirty() { WWASSERT(Pool==POOL_DEFAULT); return Dirty; };
  193. void Clean() { Dirty=false; };
  194. unsigned Get_Reduction() const;
  195. WW3DFormat Get_Texture_Format() const { return TextureFormat; }
  196. bool Is_Compression_Allowed() const { return IsCompressionAllowed; }
  197. // Inactivate textures that haven't been used in a while. Pass zero to use textures'
  198. // own inactive times (default). In urgent need to free up texture memory, try
  199. // calling with relatively small (just few seconds) time override to free up everything
  200. // but the currently used textures.
  201. static void Invalidate_Old_Unused_Textures(unsigned inactive_time_override);
  202. private:
  203. // Apply this texture's settings into D3D
  204. void Apply(unsigned int stage);
  205. void Load_Locked_Surface();
  206. // Apply a Null texture's settings into D3D
  207. static void Apply_Null(unsigned int stage);
  208. // State not contained in the Direct3D texture object:
  209. FilterType TextureMinFilter;
  210. FilterType TextureMagFilter;
  211. FilterType MipMapFilter;
  212. TxtAddrMode UAddressMode;
  213. TxtAddrMode VAddressMode;
  214. // Direct3D texture object
  215. IDirect3DTexture8 *D3DTexture;
  216. bool Initialized;
  217. // Name
  218. StringClass Name;
  219. StringClass FullPath;
  220. // Unique id
  221. unsigned texture_id;
  222. // NOTE: Since "texture wrapping" (NOT TEXTURE WRAP MODE - THIS IS
  223. // SOMETHING ELSE) is a global state that affects all texture stages,
  224. // and this class only affects its own stage, we will not worry about
  225. // it for now. Later (probably when we implement world-oriented
  226. // environment maps) we will consider where to put it.
  227. // For debug purposes the texture sets this true if it is a lightmap texture
  228. bool IsLightmap;
  229. bool IsProcedural;
  230. bool IsCompressionAllowed;
  231. unsigned InactivationTime; // In milliseconds
  232. unsigned ExtendedInactivationTime; // This is set by the engine, if needed
  233. unsigned LastInactivationSyncTime;
  234. unsigned LastAccessed;
  235. WW3DFormat TextureFormat;
  236. int Width;
  237. int Height;
  238. // Support for self-managed textures
  239. PoolType Pool;
  240. bool Dirty;
  241. public:
  242. MipCountType MipLevelCount;
  243. private:
  244. friend class TextureLoadTaskClass;
  245. TextureLoadTaskClass* TextureLoadTask;
  246. // kind of a crazy merge point not sure if the public should have
  247. // been above the ThumbnailLoadTask or not.
  248. TextureLoadTaskClass* ThumbnailLoadTask;
  249. public:
  250. // Background texture loader will call this when texture has been loaded
  251. void Apply_New_Surface(IDirect3DTexture8* tex, bool initialized); // If the parameter is true, the texture will be flagged as initialised
  252. };
  253. // Utility functions for loading and saving texture descriptions from/to W3D files
  254. TextureClass *Load_Texture(ChunkLoadClass & cload);
  255. void Save_Texture(TextureClass * texture, ChunkSaveClass & csave);
  256. #endif //TEXTURE_H