textureloader.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #ifndef TEXTURELOADER_H
  19. #define TEXTURELOADER_H
  20. #if defined(_MSC_VER)
  21. #pragma once
  22. #endif
  23. #include "always.h"
  24. #include "texture.h"
  25. class StringClass;
  26. struct IDirect3DTexture8;
  27. class TextureLoadTaskClass;
  28. class TextureLoader
  29. {
  30. public:
  31. static void Init(void);
  32. static void Deinit(void);
  33. // Modify given texture size to nearest valid size on current hardware.
  34. static void Validate_Texture_Size(unsigned& width, unsigned& height);
  35. static IDirect3DTexture8 * Load_Thumbnail(
  36. const StringClass& filename);
  37. // WW3DFormat texture_format); // Pass WW3D_FORMAT_UNKNOWN if you don't care
  38. static IDirect3DSurface8 * Load_Surface_Immediate(
  39. const StringClass& filename,
  40. WW3DFormat surface_format, // Pass WW3D_FORMAT_UNKNOWN if you don't care
  41. bool allow_compression);
  42. static void Request_Thumbnail(TextureClass* tc);
  43. // Adds a loading task to the system. The task if processed in a separate
  44. // thread as soon as possible. The task will appear in finished tasks list
  45. // when it's been completed. The texture will be refreshed on the next
  46. // update call after appearing to the finished tasks list.
  47. static void Request_Background_Loading(TextureClass* tc);
  48. // Textures can only be created and locked by the main thread so this function sends a request to the texture
  49. // handling system to load the texture immediatelly next time it enters the main thread. If this function
  50. // is called from the main thread the texture is loaded immediatelly.
  51. static void Request_Foreground_Loading(TextureClass* tc);
  52. static void Flush_Pending_Load_Tasks(void);
  53. static void Update(void(*network_callback)(void) = NULL);
  54. // returns true if current thread of execution is allowed to make DX8 calls.
  55. static bool Is_DX8_Thread(void);
  56. static void Suspend_Texture_Load();
  57. static void Continue_Texture_Load();
  58. private:
  59. static void Process_Foreground_Load (TextureLoadTaskClass *task);
  60. static void Process_Foreground_Thumbnail (TextureLoadTaskClass *task);
  61. static void Begin_Load_And_Queue (TextureLoadTaskClass *task);
  62. static void Load_Thumbnail (TextureClass *tc);
  63. static bool TextureLoadSuspended;
  64. };
  65. class TextureLoadTaskListNodeClass
  66. {
  67. friend class TextureLoadTaskListClass;
  68. public:
  69. TextureLoadTaskListNodeClass(void) : Next(0), Prev(0) { }
  70. TextureLoadTaskListClass *Get_List(void) { return List; }
  71. TextureLoadTaskListNodeClass *Next;
  72. TextureLoadTaskListNodeClass *Prev;
  73. TextureLoadTaskListClass * List;
  74. };
  75. class TextureLoadTaskListClass
  76. {
  77. // This class implements an unsynchronized, double-linked list of TextureLoadTaskClass
  78. // objects, using an embedded list node.
  79. public:
  80. TextureLoadTaskListClass(void);
  81. // Returns true if list is empty, false otherwise.
  82. bool Is_Empty (void) const { return (Root.Next == &Root); }
  83. // Add a task to beginning of list
  84. void Push_Front (TextureLoadTaskClass *task);
  85. // Add a task to end of list
  86. void Push_Back (TextureLoadTaskClass *task);
  87. // Remove and return a task from beginning of list, or NULL if list is empty.
  88. TextureLoadTaskClass * Pop_Front (void);
  89. // Remove and return a task from end of list, or NULL if list is empty
  90. TextureLoadTaskClass * Pop_Back (void);
  91. // Remove specified task from list, if present
  92. void Remove (TextureLoadTaskClass *task);
  93. private:
  94. // This list is implemented using a sentinel node.
  95. TextureLoadTaskListNodeClass Root;
  96. };
  97. class SynchronizedTextureLoadTaskListClass : public TextureLoadTaskListClass
  98. {
  99. // This class added thread-safety to the basic TextureLoadTaskListClass.
  100. public:
  101. SynchronizedTextureLoadTaskListClass(void);
  102. // See comments above for description of member functions.
  103. void Push_Front (TextureLoadTaskClass *task);
  104. void Push_Back (TextureLoadTaskClass *task);
  105. TextureLoadTaskClass * Pop_Front (void);
  106. TextureLoadTaskClass * Pop_Back (void);
  107. void Remove (TextureLoadTaskClass *task);
  108. private:
  109. FastCriticalSectionClass CriticalSection;
  110. };
  111. class TextureLoadTaskClass : public TextureLoadTaskListNodeClass
  112. {
  113. public:
  114. enum TaskType {
  115. TASK_NONE,
  116. TASK_THUMBNAIL,
  117. TASK_LOAD,
  118. };
  119. enum PriorityType {
  120. PRIORITY_LOW,
  121. PRIORITY_HIGH,
  122. };
  123. enum StateType {
  124. STATE_NONE,
  125. STATE_LOAD_BEGUN,
  126. STATE_LOAD_MIPMAP,
  127. STATE_LOAD_COMPLETE,
  128. STATE_COMPLETE,
  129. };
  130. TextureLoadTaskClass(void);
  131. ~TextureLoadTaskClass(void);
  132. static TextureLoadTaskClass * Create (TextureClass *tc, TaskType type, PriorityType priority);
  133. void Destroy (void);
  134. static void Delete_Free_Pool (void);
  135. void Init (TextureClass *tc, TaskType type, PriorityType priority);
  136. void Deinit (void);
  137. TaskType Get_Type (void) const { return Type; }
  138. PriorityType Get_Priority (void) const { return Priority; }
  139. StateType Get_State (void) const { return State; }
  140. WW3DFormat Get_Format (void) const { return Format; }
  141. unsigned int Get_Width (void) const { return Width; }
  142. unsigned int Get_Height (void) const { return Height; }
  143. unsigned int Get_Mip_Level_Count (void) const { return MipLevelCount; }
  144. unsigned int Get_Reduction (void) const { return Reduction; }
  145. unsigned char * Get_Locked_Surface_Ptr (unsigned int level);
  146. unsigned int Get_Locked_Surface_Pitch(unsigned int level) const;
  147. TextureClass * Peek_Texture (void) { return Texture; }
  148. IDirect3DTexture8 * Peek_D3D_Texture (void) { return D3DTexture; }
  149. void Set_Type (TaskType t) { Type = t; }
  150. void Set_Priority (PriorityType p) { Priority = p; }
  151. void Set_State (StateType s) { State = s; }
  152. bool Begin_Load (void);
  153. bool Load (void);
  154. void End_Load (void);
  155. void Finish_Load (void);
  156. void Apply_Missing_Texture (void);
  157. private:
  158. bool Begin_Compressed_Load (void);
  159. bool Begin_Uncompressed_Load (void);
  160. bool Load_Compressed_Mipmap (void);
  161. bool Load_Uncompressed_Mipmap(void);
  162. void Lock_Surfaces (void);
  163. void Unlock_Surfaces (void);
  164. void Apply (bool initialize);
  165. TextureClass* Texture;
  166. IDirect3DTexture8 * D3DTexture;
  167. WW3DFormat Format;
  168. unsigned int Width;
  169. unsigned int Height;
  170. unsigned int MipLevelCount;
  171. unsigned int Reduction;
  172. unsigned char * LockedSurfacePtr[TextureClass::MIP_LEVELS_MAX];
  173. unsigned int LockedSurfacePitch[TextureClass::MIP_LEVELS_MAX];
  174. TaskType Type;
  175. PriorityType Priority;
  176. StateType State;
  177. };
  178. #endif