texture.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. ** Command & Conquer Generals(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:: 8/17/01 9:41a $*
  29. * *
  30. * $Revision:: 35 $*
  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 W3DMPO, public RefCountClass
  61. {
  62. W3DMPO_GLUE(TextureClass)
  63. friend DX8Wrapper;
  64. friend TextureLoader;
  65. friend LoaderThreadClass;
  66. friend DX8TextureManagerClass;
  67. public:
  68. enum PoolType {
  69. POOL_DEFAULT=0,
  70. POOL_MANAGED,
  71. POOL_SYSTEMMEM
  72. };
  73. enum FilterType {
  74. FILTER_TYPE_NONE,
  75. FILTER_TYPE_FAST,
  76. FILTER_TYPE_BEST,
  77. FILTER_TYPE_DEFAULT,
  78. FILTER_TYPE_COUNT
  79. };
  80. enum TxtAddrMode {
  81. TEXTURE_ADDRESS_REPEAT=0,
  82. TEXTURE_ADDRESS_CLAMP
  83. };
  84. enum MipCountType {
  85. MIP_LEVELS_ALL=0, // generate all mipmap levels down to 1x1 size
  86. MIP_LEVELS_1, // no mipmapping at all (just one mip level)
  87. MIP_LEVELS_2,
  88. MIP_LEVELS_3,
  89. MIP_LEVELS_4,
  90. MIP_LEVELS_5,
  91. MIP_LEVELS_6,
  92. MIP_LEVELS_7,
  93. MIP_LEVELS_8,
  94. MIP_LEVELS_10,
  95. MIP_LEVELS_11,
  96. MIP_LEVELS_12,
  97. MIP_LEVELS_MAX // This isn't to be used (use MIP_LEVELS_ALL instead), it is just an enum for creating static tables etc.
  98. };
  99. // Create texture with desired height, width and format.
  100. TextureClass(
  101. unsigned width,
  102. unsigned height,
  103. WW3DFormat format,
  104. MipCountType mip_level_count=MIP_LEVELS_ALL,
  105. PoolType pool=POOL_MANAGED,
  106. bool rendertarget=false);
  107. // Create texture from a file. If format is specified the texture is converted to that format.
  108. // Note that the format must be supported by the current device and that a texture can't exist
  109. // in the system with the same name in multiple formats.
  110. TextureClass(
  111. const char *name,
  112. const char *full_path=NULL,
  113. MipCountType mip_level_count=MIP_LEVELS_ALL,
  114. WW3DFormat texture_format=WW3D_FORMAT_UNKNOWN,
  115. bool allow_compression=true);
  116. // Create texture from a surface.
  117. TextureClass(
  118. SurfaceClass *surface,
  119. MipCountType mip_level_count=MIP_LEVELS_ALL);
  120. TextureClass(IDirect3DTexture8* d3d_texture);
  121. virtual ~TextureClass(void);
  122. // Names
  123. void Set_Texture_Name(const char * name);
  124. void Set_Full_Path(const char * path) { FullPath = path; }
  125. const char * Get_Texture_Name(void) const { return Name; }
  126. const char * Get_Full_Path(void) const { if (FullPath.Is_Empty ()) return Name; return FullPath; }
  127. unsigned Get_ID() const { return texture_id; } // Each textrure has a unique id
  128. // The number of Mip levels in the texture
  129. unsigned int Get_Mip_Level_Count(void);
  130. // Get surface description of a Mip level (defaults to the highest-resolution one)
  131. void Get_Level_Description(SurfaceClass::SurfaceDescription &surface_desc, unsigned int level = 0);
  132. // Get the surface of one of the mipmap levels (defaults to highest-resolution one)
  133. SurfaceClass *Get_Surface_Level(unsigned int level = 0);
  134. // Texture priority affects texture management and caching.
  135. unsigned int Get_Priority(void);
  136. unsigned int Set_Priority(unsigned int priority); // Returns previous priority
  137. // Filter and MIPmap settings:
  138. FilterType Get_Min_Filter(void) const { return TextureMinFilter; }
  139. FilterType Get_Mag_Filter(void) const { return TextureMagFilter; }
  140. FilterType Get_Mip_Mapping(void) const { return MipMapFilter; }
  141. void Set_Min_Filter(FilterType filter) { TextureMinFilter=filter; }
  142. void Set_Mag_Filter(FilterType filter) { TextureMagFilter=filter; }
  143. void Set_Mip_Mapping(FilterType mipmap);
  144. // Texture address mode
  145. TxtAddrMode Get_U_Addr_Mode(void) const { return UAddressMode; }
  146. TxtAddrMode Get_V_Addr_Mode(void) const { return VAddressMode; }
  147. void Set_U_Addr_Mode(TxtAddrMode mode) { UAddressMode=mode; }
  148. void Set_V_Addr_Mode(TxtAddrMode mode) { VAddressMode=mode; }
  149. // Debug utility functions for returning the texture memory usage
  150. unsigned Get_Texture_Memory_Usage() const;
  151. bool Is_Initialized() const { return Initialized; }
  152. bool Is_Lightmap() const { return IsLightmap; }
  153. bool Is_Procedural() const { return IsProcedural; }
  154. static int _Get_Total_Locked_Surface_Size();
  155. static int _Get_Total_Texture_Size();
  156. static int _Get_Total_Lightmap_Texture_Size();
  157. static int _Get_Total_Procedural_Texture_Size();
  158. static int _Get_Total_Locked_Surface_Count();
  159. static int _Get_Total_Texture_Count();
  160. static int _Get_Total_Lightmap_Texture_Count();
  161. static int _Get_Total_Procedural_Texture_Count();
  162. // This needs to be called after device has been created
  163. static void _Init_Filters();
  164. static void _Set_Default_Min_Filter(FilterType filter);
  165. static void _Set_Default_Mag_Filter(FilterType filter);
  166. static void _Set_Default_Mip_Filter(FilterType filter);
  167. // This utility function processes the texture reduction (used during rendering)
  168. void Invalidate();
  169. IDirect3DTexture8 *Peek_DX8_Texture()
  170. {
  171. return D3DTexture;
  172. }
  173. bool Is_Missing_Texture();
  174. // Support for self managed textures
  175. bool Is_Dirty() { WWASSERT(Pool==POOL_DEFAULT); return Dirty; };
  176. void Clean() { Dirty=false; };
  177. unsigned Get_Reduction() const;
  178. WW3DFormat Get_Texture_Format() const { return TextureFormat; }
  179. bool Is_Compression_Allowed() const { return IsCompressionAllowed; }
  180. protected:
  181. // Apply this texture's settings into D3D
  182. virtual void Apply(unsigned int stage);
  183. void Load_Locked_Surface();
  184. void Init();
  185. // Apply a Null texture's settings into D3D
  186. static void Apply_Null(unsigned int stage);
  187. // State not contained in the Direct3D texture object:
  188. FilterType TextureMinFilter;
  189. FilterType TextureMagFilter;
  190. FilterType MipMapFilter;
  191. TxtAddrMode UAddressMode;
  192. TxtAddrMode VAddressMode;
  193. // Direct3D texture object
  194. IDirect3DTexture8 *D3DTexture;
  195. bool Initialized;
  196. // Name
  197. StringClass Name;
  198. StringClass FullPath;
  199. // Unique id
  200. unsigned texture_id;
  201. // NOTE: Since "texture wrapping" (NOT TEXTURE WRAP MODE - THIS IS
  202. // SOMETHING ELSE) is a global state that affects all texture stages,
  203. // and this class only affects its own stage, we will not worry about
  204. // it for now. Later (probably when we implement world-oriented
  205. // environment maps) we will consider where to put it.
  206. // For debug purposes the texture sets this true if it is a lightmap texture
  207. bool IsLightmap;
  208. bool IsProcedural;
  209. bool IsCompressionAllowed;
  210. unsigned LastAccessed;
  211. WW3DFormat TextureFormat;
  212. // Support for self-managed textures
  213. PoolType Pool;
  214. bool Dirty;
  215. public:
  216. MipCountType MipLevelCount;
  217. TextureLoadTaskClass* TextureLoadTask;
  218. // Background texture loader will call this when texture has been loaded
  219. void Apply_New_Surface(bool initialized); // If the parameter is true, the texture will be flagged as initialised
  220. };
  221. class BumpmapTextureClass : public TextureClass
  222. {
  223. public:
  224. // Generate bumpmap texture procedurally from the source texture
  225. BumpmapTextureClass(TextureClass* texture);
  226. virtual ~BumpmapTextureClass();
  227. };
  228. // Utility functions for loading and saving texture descriptions from/to W3D files
  229. TextureClass *Load_Texture(ChunkLoadClass & cload);
  230. void Save_Texture(TextureClass * texture, ChunkSaveClass & csave);
  231. #endif //TEXTURE_H