textureloader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 : DX8 Texture Manager *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/textureloader.h $*
  25. * *
  26. * Original Author:: vss_sync *
  27. * *
  28. * Author : Kenny Mitchell *
  29. * *
  30. * $Modtime:: 06/27/02 1:27p $*
  31. * *
  32. * $Revision:: 2 $*
  33. * *
  34. * 06/27/02 KM Texture class abstraction *
  35. *---------------------------------------------------------------------------------------------*
  36. * Functions: *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #ifndef TEXTURELOADER_H
  39. #define TEXTURELOADER_H
  40. #if defined(_MSC_VER)
  41. #pragma once
  42. #endif
  43. #include "always.h"
  44. #include "texture.h"
  45. class StringClass;
  46. struct IDirect3DTexture8;
  47. class TextureLoadTaskClass;
  48. class TextureLoader
  49. {
  50. public:
  51. static void Init(void);
  52. static void Deinit(void);
  53. // Modify given texture size to nearest valid size on current hardware.
  54. static void Validate_Texture_Size(unsigned& width, unsigned& height, unsigned& depth);
  55. static IDirect3DTexture8 * Load_Thumbnail(
  56. const StringClass& filename,const Vector3& hsv_shift);
  57. // WW3DFormat texture_format); // Pass WW3D_FORMAT_UNKNOWN if you don't care
  58. static IDirect3DSurface8 * Load_Surface_Immediate(
  59. const StringClass& filename,
  60. WW3DFormat surface_format, // Pass WW3D_FORMAT_UNKNOWN if you don't care
  61. bool allow_compression);
  62. static void Request_Thumbnail(TextureBaseClass* tc);
  63. // Adds a loading task to the system. The task if processed in a separate
  64. // thread as soon as possible. The task will appear in finished tasks list
  65. // when it's been completed. The texture will be refreshed on the next
  66. // update call after appearing to the finished tasks list.
  67. static void Request_Background_Loading(TextureBaseClass* tc);
  68. // Textures can only be created and locked by the main thread so this function sends a request to the texture
  69. // handling system to load the texture immediatelly next time it enters the main thread. If this function
  70. // is called from the main thread the texture is loaded immediatelly.
  71. static void Request_Foreground_Loading(TextureBaseClass* tc);
  72. static void Flush_Pending_Load_Tasks(void);
  73. static void Update(void(*network_callback)(void) = NULL);
  74. // returns true if current thread of execution is allowed to make DX8 calls.
  75. static bool Is_DX8_Thread(void);
  76. static void Suspend_Texture_Load();
  77. static void Continue_Texture_Load();
  78. static void Set_Texture_Inactive_Override_Time(int time_ms) {TextureInactiveOverrideTime = time_ms;}
  79. private:
  80. static void Process_Foreground_Load (TextureLoadTaskClass *task);
  81. static void Process_Foreground_Thumbnail (TextureLoadTaskClass *task);
  82. static void Begin_Load_And_Queue (TextureLoadTaskClass *task);
  83. static void Load_Thumbnail (TextureBaseClass *tc);
  84. static bool TextureLoadSuspended;
  85. // The time in ms before a texture is thrown out.
  86. // The default is zero. The scripted movies set this to reduce texture stalls in movies.
  87. static int TextureInactiveOverrideTime;
  88. };
  89. class TextureLoadTaskListNodeClass
  90. {
  91. friend class TextureLoadTaskListClass;
  92. public:
  93. TextureLoadTaskListNodeClass(void) : Next(0), Prev(0) { }
  94. TextureLoadTaskListClass *Get_List(void) { return List; }
  95. TextureLoadTaskListNodeClass *Next;
  96. TextureLoadTaskListNodeClass *Prev;
  97. TextureLoadTaskListClass * List;
  98. };
  99. class TextureLoadTaskListClass
  100. {
  101. // This class implements an unsynchronized, double-linked list of TextureLoadTaskClass
  102. // objects, using an embedded list node.
  103. public:
  104. TextureLoadTaskListClass(void);
  105. // Returns true if list is empty, false otherwise.
  106. bool Is_Empty (void) const { return (Root.Next == &Root); }
  107. // Add a task to beginning of list
  108. void Push_Front (TextureLoadTaskClass *task);
  109. // Add a task to end of list
  110. void Push_Back (TextureLoadTaskClass *task);
  111. // Remove and return a task from beginning of list, or NULL if list is empty.
  112. TextureLoadTaskClass * Pop_Front (void);
  113. // Remove and return a task from end of list, or NULL if list is empty
  114. TextureLoadTaskClass * Pop_Back (void);
  115. // Remove specified task from list, if present
  116. void Remove (TextureLoadTaskClass *task);
  117. private:
  118. // This list is implemented using a sentinel node.
  119. TextureLoadTaskListNodeClass Root;
  120. };
  121. class SynchronizedTextureLoadTaskListClass : public TextureLoadTaskListClass
  122. {
  123. // This class added thread-safety to the basic TextureLoadTaskListClass.
  124. public:
  125. SynchronizedTextureLoadTaskListClass(void);
  126. // See comments above for description of member functions.
  127. void Push_Front (TextureLoadTaskClass *task);
  128. void Push_Back (TextureLoadTaskClass *task);
  129. TextureLoadTaskClass * Pop_Front (void);
  130. TextureLoadTaskClass * Pop_Back (void);
  131. void Remove (TextureLoadTaskClass *task);
  132. private:
  133. FastCriticalSectionClass CriticalSection;
  134. };
  135. /*
  136. ** (gth) The allocation system we're using for TextureLoadTaskClass has gotten a little
  137. ** complicated since Kenny added the new task types for Cube and Volume textures. The
  138. ** ::Destroy member is used to return a task to the pool now and must be over-ridden in
  139. ** each derived class to put the task back into the correct free list.
  140. */
  141. class TextureLoadTaskClass : public TextureLoadTaskListNodeClass
  142. {
  143. public:
  144. enum TaskType {
  145. TASK_NONE,
  146. TASK_THUMBNAIL,
  147. TASK_LOAD,
  148. };
  149. enum PriorityType {
  150. PRIORITY_LOW,
  151. PRIORITY_HIGH,
  152. };
  153. enum StateType {
  154. STATE_NONE,
  155. STATE_LOAD_BEGUN,
  156. STATE_LOAD_MIPMAP,
  157. STATE_LOAD_COMPLETE,
  158. STATE_COMPLETE,
  159. };
  160. TextureLoadTaskClass(void);
  161. ~TextureLoadTaskClass(void);
  162. static TextureLoadTaskClass * Create (TextureBaseClass *tc, TaskType type, PriorityType priority);
  163. static void Delete_Free_Pool (void);
  164. virtual void Destroy (void);
  165. virtual void Init (TextureBaseClass *tc, TaskType type, PriorityType priority);
  166. virtual void Deinit (void);
  167. TaskType Get_Type (void) const { return Type; }
  168. PriorityType Get_Priority (void) const { return Priority; }
  169. StateType Get_State (void) const { return State; }
  170. WW3DFormat Get_Format (void) const { return Format; }
  171. unsigned int Get_Width (void) const { return Width; }
  172. unsigned int Get_Height (void) const { return Height; }
  173. unsigned int Get_Mip_Level_Count (void) const { return MipLevelCount; }
  174. unsigned int Get_Reduction (void) const { return Reduction; }
  175. unsigned char * Get_Locked_Surface_Ptr (unsigned int level);
  176. unsigned int Get_Locked_Surface_Pitch(unsigned int level) const;
  177. TextureBaseClass * Peek_Texture (void) { return Texture; }
  178. IDirect3DTexture8 * Peek_D3D_Texture (void) { return (IDirect3DTexture8*)D3DTexture; }
  179. void Set_Type (TaskType t) { Type = t; }
  180. void Set_Priority (PriorityType p) { Priority = p; }
  181. void Set_State (StateType s) { State = s; }
  182. bool Begin_Load (void);
  183. bool Load (void);
  184. void End_Load (void);
  185. void Finish_Load (void);
  186. void Apply_Missing_Texture (void);
  187. protected:
  188. virtual bool Begin_Compressed_Load (void);
  189. virtual bool Begin_Uncompressed_Load (void);
  190. virtual bool Load_Compressed_Mipmap (void);
  191. virtual bool Load_Uncompressed_Mipmap(void);
  192. virtual void Lock_Surfaces (void);
  193. virtual void Unlock_Surfaces (void);
  194. void Apply (bool initialize);
  195. TextureBaseClass* Texture;
  196. IDirect3DBaseTexture8* D3DTexture;
  197. WW3DFormat Format;
  198. unsigned int Width;
  199. unsigned int Height;
  200. unsigned int MipLevelCount;
  201. unsigned int Reduction;
  202. Vector3 HSVShift;
  203. unsigned char * LockedSurfacePtr[MIP_LEVELS_MAX];
  204. unsigned int LockedSurfacePitch[MIP_LEVELS_MAX];
  205. TaskType Type;
  206. PriorityType Priority;
  207. StateType State;
  208. };
  209. class CubeTextureLoadTaskClass : public TextureLoadTaskClass
  210. {
  211. public:
  212. CubeTextureLoadTaskClass();
  213. virtual void Destroy (void);
  214. virtual void Init (TextureBaseClass *tc, TaskType type, PriorityType priority);
  215. virtual void Deinit (void);
  216. protected:
  217. virtual bool Begin_Compressed_Load (void);
  218. virtual bool Begin_Uncompressed_Load (void);
  219. virtual bool Load_Compressed_Mipmap (void);
  220. // virtual bool Load_Uncompressed_Mipmap(void);
  221. virtual void Lock_Surfaces (void);
  222. virtual void Unlock_Surfaces (void);
  223. private:
  224. unsigned char* Get_Locked_CubeMap_Surface_Pointer(unsigned int face, unsigned int level);
  225. unsigned int Get_Locked_CubeMap_Surface_Pitch(unsigned int face, unsigned int level) const;
  226. IDirect3DCubeTexture8* Peek_D3D_Cube_Texture(void) { return (IDirect3DCubeTexture8*)D3DTexture; }
  227. unsigned char* LockedCubeSurfacePtr[6][MIP_LEVELS_MAX];
  228. unsigned int LockedCubeSurfacePitch[6][MIP_LEVELS_MAX];
  229. };
  230. class VolumeTextureLoadTaskClass : public TextureLoadTaskClass
  231. {
  232. public:
  233. VolumeTextureLoadTaskClass();
  234. virtual void Destroy (void);
  235. virtual void Init (TextureBaseClass *tc, TaskType type, PriorityType priority);
  236. protected:
  237. virtual bool Begin_Compressed_Load (void);
  238. virtual bool Begin_Uncompressed_Load (void);
  239. virtual bool Load_Compressed_Mipmap (void);
  240. // virtual bool Load_Uncompressed_Mipmap(void);
  241. virtual void Lock_Surfaces (void);
  242. virtual void Unlock_Surfaces (void);
  243. private:
  244. unsigned char* Get_Locked_Volume_Pointer(unsigned int level);
  245. unsigned int Get_Locked_Volume_Row_Pitch(unsigned int level);
  246. unsigned int Get_Locked_Volume_Slice_Pitch(unsigned int level);
  247. IDirect3DVolumeTexture8* Peek_D3D_Volume_Texture(void) { return (IDirect3DVolumeTexture8*)D3DTexture; }
  248. unsigned int LockedSurfaceSlicePitch[MIP_LEVELS_MAX];
  249. unsigned int Depth;
  250. };
  251. #endif