dx8renderer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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/dx8renderer.h $*
  25. * *
  26. * Original Author:: Jani Penttinen *
  27. * *
  28. * $Author:: Jani_p $*
  29. * *
  30. * $Modtime:: 7/10/01 5:49p $*
  31. * *
  32. * $Revision:: 28 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #ifndef DX8_RENDERER_H
  41. #define DX8_RENDERER_H
  42. #include "always.h"
  43. #include "wwstring.h"
  44. #include "simplevec.h"
  45. #include "refcount.h"
  46. #include "vector.h"
  47. #include "dx8list.h"
  48. #include "shader.h"
  49. #include "dx8wrapper.h"
  50. class IndexBufferClass;
  51. class VertexBufferClass;
  52. class DX8RenderTypeArrayClass;
  53. class MeshClass;
  54. class MeshModelClass;
  55. class DX8PolygonRendererClass;
  56. class Vertex_Split_Table;
  57. class DX8FVFCategoryContainer;
  58. class DecalMeshClass;
  59. class MaterialPassClass;
  60. class MatPassTaskClass;
  61. class PolyRenderTaskClass;
  62. class TextureClass;
  63. class VertexMaterialClass;
  64. class CameraClass;
  65. #define RECORD_RENDER(I,P) render_stats[I].count++; render_stats[I].polys+=P;
  66. /**
  67. ** DX8TextureCategoryClass
  68. ** This class is used for each Material-Texture-Shader combination that is encountered during rendering.
  69. ** Each polygon_renderer that uses the same 'TextureCategory' will be linked to the 'TextureCategory' object.
  70. ** Then, all polygons will be rendered in 'TextureCategory' batches to reduce the number of stage changes
  71. ** (and most importantly, texture changes) that we cause in DX8.
  72. */
  73. class DX8TextureCategoryClass : public MultiListObjectClass
  74. {
  75. int pass;
  76. TextureClass * textures[MAX_TEXTURE_STAGES];
  77. ShaderClass shader;
  78. VertexMaterialClass * material;
  79. DX8PolygonRendererList PolygonRendererList;
  80. DX8FVFCategoryContainer* container;
  81. PolyRenderTaskClass * render_task_head; // polygon renderers queued for rendering
  82. static bool m_gForceMultiply; // Forces opaque materials to use the multiply blend - pseudo transparent effect. jba.
  83. public:
  84. DX8TextureCategoryClass(DX8FVFCategoryContainer* container,TextureClass** textures, ShaderClass shd, VertexMaterialClass* mat,int pass);
  85. ~DX8TextureCategoryClass();
  86. void Add_Render_Task(DX8PolygonRendererClass * p_renderer,MeshClass * p_mesh);
  87. void Render(void);
  88. bool Anything_To_Render() { return (render_task_head != NULL); }
  89. void Clear_Render_List() { render_task_head = NULL; }
  90. TextureClass * Peek_Texture(int stage) { return textures[stage]; }
  91. const VertexMaterialClass * Peek_Material() { return material; }
  92. ShaderClass Get_Shader() { return shader; }
  93. DX8PolygonRendererList& Get_Polygon_Renderer_List() { return PolygonRendererList; }
  94. unsigned Add_Mesh(
  95. Vertex_Split_Table& split_buffer,
  96. unsigned vertex_offset,
  97. unsigned index_offset,
  98. IndexBufferClass* index_buffer,
  99. unsigned pass);
  100. void Log(bool only_visible);
  101. void Remove_Polygon_Renderer(DX8PolygonRendererClass* p_renderer);
  102. void Add_Polygon_Renderer(DX8PolygonRendererClass* p_renderer,DX8PolygonRendererClass* add_after_this=NULL);
  103. DX8FVFCategoryContainer * Get_Container(void) { return container; }
  104. // Force multiply blend on all objects inserted from now on. (Doesn't affect the objects that are already in the lists)
  105. static void SetForceMultiply(bool multiply) { m_gForceMultiply=multiply; }
  106. };
  107. // ----------------------------------------------------------------------------
  108. /**
  109. ** DX8FVFCategoryContainer
  110. */
  111. class DX8FVFCategoryContainer : public MultiListObjectClass
  112. {
  113. public:
  114. enum {
  115. MAX_PASSES=4
  116. };
  117. protected:
  118. TextureCategoryList texture_category_list[MAX_PASSES];
  119. TextureCategoryList visible_texture_category_list[MAX_PASSES];
  120. MatPassTaskClass * visible_matpass_head;
  121. MatPassTaskClass * visible_matpass_tail;
  122. IndexBufferClass * index_buffer;
  123. int used_indices;
  124. unsigned FVF;
  125. unsigned passes;
  126. unsigned uv_coordinate_channels;
  127. bool sorting;
  128. bool AnythingToRender;
  129. void Generate_Texture_Categories(Vertex_Split_Table& split_table,unsigned vertex_offset);
  130. void DX8FVFCategoryContainer::Insert_To_Texture_Category(
  131. Vertex_Split_Table& split_table,
  132. TextureClass** textures,
  133. VertexMaterialClass* mat,
  134. ShaderClass shader,
  135. int pass,
  136. unsigned vertex_offset);
  137. inline bool Anything_To_Render();
  138. void Render_Procedural_Material_Passes(void);
  139. DX8TextureCategoryClass* Find_Matching_Texture_Category(
  140. TextureClass* texture,
  141. unsigned pass,
  142. unsigned stage,
  143. DX8TextureCategoryClass* ref_category);
  144. DX8TextureCategoryClass* Find_Matching_Texture_Category(
  145. VertexMaterialClass* vmat,
  146. unsigned pass,
  147. DX8TextureCategoryClass* ref_category);
  148. public:
  149. DX8FVFCategoryContainer(unsigned FVF,bool sorting);
  150. virtual ~DX8FVFCategoryContainer();
  151. static unsigned Define_FVF(MeshModelClass* mmc,bool enable_lighting);
  152. bool Is_Sorting() const { return sorting; }
  153. void Change_Polygon_Renderer_Texture(
  154. DX8PolygonRendererList& polygon_renderer_list,
  155. TextureClass* texture,
  156. TextureClass* new_texture,
  157. unsigned pass,
  158. unsigned stage);
  159. void Change_Polygon_Renderer_Material(
  160. DX8PolygonRendererList& polygon_renderer_list,
  161. VertexMaterialClass* vmat,
  162. VertexMaterialClass* new_vmat,
  163. unsigned pass);
  164. void Remove_Texture_Category(DX8TextureCategoryClass* tex_category);
  165. virtual void Render(void)=0;
  166. virtual void Add_Mesh(MeshModelClass* mmc)=0;
  167. virtual void Log(bool only_visible)=0;
  168. virtual bool Check_If_Mesh_Fits(MeshModelClass* mmc)=0;
  169. inline unsigned Get_FVF() const { return FVF; }
  170. inline void Add_Visible_Texture_Category(DX8TextureCategoryClass * tex_category,int pass)
  171. {
  172. WWASSERT(pass<MAX_PASSES);
  173. WWASSERT(tex_category != NULL);
  174. WWASSERT(texture_category_list[pass].Contains(tex_category));
  175. visible_texture_category_list[pass].Add(tex_category);
  176. AnythingToRender=true;
  177. }
  178. void Add_Visible_Material_Pass(MaterialPassClass * pass,MeshClass * mesh);
  179. };
  180. bool DX8FVFCategoryContainer::Anything_To_Render()
  181. {
  182. /* for (unsigned p=0;p<passes;++p) {
  183. TextureCategoryListIterator it(&texture_category_list[p]);
  184. while (!it.Is_Done()) {
  185. if (it.Peek_Obj()->Anything_To_Render()) return true;
  186. it.Next();
  187. }
  188. }
  189. return false;
  190. */
  191. return AnythingToRender;
  192. }
  193. /**
  194. ** DX8RigidFVFCategoryContainer
  195. ** This is an FVFCategoryContainer for rigid (non-skin) meshes
  196. */
  197. class DX8RigidFVFCategoryContainer : public DX8FVFCategoryContainer
  198. {
  199. public:
  200. DX8RigidFVFCategoryContainer(unsigned FVF,bool sorting);
  201. ~DX8RigidFVFCategoryContainer();
  202. void Add_Mesh(MeshModelClass* mmc);
  203. void Log(bool only_visible);
  204. bool Check_If_Mesh_Fits(MeshModelClass* mmc);
  205. void Render(void); // Generic render function
  206. protected:
  207. VertexBufferClass * vertex_buffer;
  208. int used_vertices;
  209. };
  210. /**
  211. ** DX8SkinFVFCategoryContainer
  212. ** This is an FVFCategoryContainer for skin meshes
  213. */
  214. class DX8SkinFVFCategoryContainer: public DX8FVFCategoryContainer
  215. {
  216. public:
  217. DX8SkinFVFCategoryContainer(bool sorting);
  218. ~DX8SkinFVFCategoryContainer();
  219. void Render(void);
  220. void Add_Mesh(MeshModelClass* mmc);
  221. void Log(bool only_visible);
  222. bool Check_If_Mesh_Fits(MeshModelClass* mmc);
  223. void Add_Visible_Skin(MeshClass * mesh);
  224. private:
  225. void Reset();
  226. void clearVisibleSkinList();
  227. unsigned int VisibleVertexCount;
  228. MeshClass * VisibleSkinHead;
  229. MeshClass * VisibleSkinTail;
  230. };
  231. /**
  232. ** DX8MeshRendererClass
  233. ** This object is controller for the entire DX8 mesh rendering system. It organizes mesh
  234. ** fragments into groups based on FVF, texture, and material. During rendering, a list of
  235. ** the visible mesh fragments is composed and rendered. There is a global instance of this
  236. ** class called TheDX8MeshRenderer that should be used for all mesh rendering.
  237. */
  238. class DX8MeshRendererClass
  239. {
  240. public:
  241. DX8MeshRendererClass();
  242. ~DX8MeshRendererClass();
  243. void Init();
  244. void Shutdown();
  245. void Flush();
  246. void Clear_Pending_Delete_Lists();
  247. void Log_Statistics_String(bool only_visible);
  248. static void Request_Log_Statistics();
  249. void Register_Mesh_Type(MeshModelClass* mmc);
  250. void Unregister_Mesh_Type(MeshModelClass* mmc);
  251. void Set_Camera(CameraClass* cam) { camera=cam; }
  252. CameraClass * Peek_Camera(void) { return camera; }
  253. void Add_To_Render_List(DecalMeshClass * decalmesh);
  254. // Enable or disable lighting on all objects inserted from now on. (Doesn't affect the objects that are already in the lists)
  255. void Enable_Lighting(bool enable) { enable_lighting=enable; }
  256. // This should be called at the beginning of a game or menu or after a major modifications to the scene...
  257. void Invalidate(bool shutdown=false); // Added flag so it doesn't allocate more mem when shutting down. -MW
  258. protected:
  259. void Render_Decal_Meshes(void);
  260. bool enable_lighting;
  261. CameraClass * camera;
  262. SimpleDynVecClass<FVFCategoryList *> texture_category_container_lists_rigid;
  263. FVFCategoryList * texture_category_container_list_skin;
  264. DecalMeshClass * visible_decal_meshes;
  265. };
  266. extern DX8MeshRendererClass TheDX8MeshRenderer;
  267. #endif