dx8renderer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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/dx8renderer.h $*
  25. * *
  26. * Original Author:: Jani Penttinen *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 2/28/02 5:34p $*
  31. * *
  32. * $Revision:: 30 $*
  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. #include "hashtemplate.h"
  51. class IndexBufferClass;
  52. class VertexBufferClass;
  53. class DX8RenderTypeArrayClass;
  54. class MeshClass;
  55. class MeshModelClass;
  56. class DX8PolygonRendererClass;
  57. class Vertex_Split_Table;
  58. class DX8FVFCategoryContainer;
  59. class DecalMeshClass;
  60. class MaterialPassClass;
  61. class MatPassTaskClass;
  62. class PolyRenderTaskClass;
  63. class TextureClass;
  64. class VertexMaterialClass;
  65. class CameraClass;
  66. #define RECORD_RENDER(I,P) render_stats[I].count++; render_stats[I].polys+=P;
  67. /**
  68. ** DX8TextureCategoryClass
  69. ** This class is used for each Material-Texture-Shader combination that is encountered during rendering.
  70. ** Each polygon_renderer that uses the same 'TextureCategory' will be linked to the 'TextureCategory' object.
  71. ** Then, all polygons will be rendered in 'TextureCategory' batches to reduce the number of stage changes
  72. ** (and most importantly, texture changes) that we cause in DX8.
  73. */
  74. class DX8TextureCategoryClass : public MultiListObjectClass
  75. {
  76. int pass;
  77. TextureClass * textures[MAX_TEXTURE_STAGES];
  78. ShaderClass shader;
  79. VertexMaterialClass * material;
  80. DX8PolygonRendererList PolygonRendererList;
  81. DX8FVFCategoryContainer* container;
  82. PolyRenderTaskClass * render_task_head; // polygon renderers queued for rendering
  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. };
  105. // ----------------------------------------------------------------------------
  106. /**
  107. ** DX8FVFCategoryContainer
  108. */
  109. class DX8FVFCategoryContainer : public MultiListObjectClass
  110. {
  111. public:
  112. enum {
  113. MAX_PASSES=4
  114. };
  115. protected:
  116. TextureCategoryList texture_category_list[MAX_PASSES];
  117. TextureCategoryList visible_texture_category_list[MAX_PASSES];
  118. MatPassTaskClass * visible_matpass_head;
  119. MatPassTaskClass * visible_matpass_tail;
  120. IndexBufferClass * index_buffer;
  121. int used_indices;
  122. unsigned FVF;
  123. unsigned passes;
  124. unsigned uv_coordinate_channels;
  125. bool sorting;
  126. bool AnythingToRender;
  127. bool AnyDelayedPassesToRender;
  128. void Generate_Texture_Categories(Vertex_Split_Table& split_table,unsigned vertex_offset);
  129. void DX8FVFCategoryContainer::Insert_To_Texture_Category(
  130. Vertex_Split_Table& split_table,
  131. TextureClass** textures,
  132. VertexMaterialClass* mat,
  133. ShaderClass shader,
  134. int pass,
  135. unsigned vertex_offset);
  136. inline bool Anything_To_Render() { return AnythingToRender; }
  137. inline bool Any_Delayed_Passes_To_Render() { return AnyDelayedPassesToRender; }
  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,unsigned int * user_lighting,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(MeshClass* 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. /*
  179. ** Material pass rendering. The following two functions allow procedural material passes
  180. ** to be applied to meshes in this FVF category. In certain cases, the game will *only* render
  181. ** the procedural pass and not the base materials for the mesh. When this happens there can
  182. ** be rendering errors unless these procedural passes are rendered after all of the meshes in
  183. ** the scene. The virtual method Add_Delayed_Material_Pass is used in this case.
  184. */
  185. void Add_Visible_Material_Pass(MaterialPassClass * pass,MeshClass * mesh);
  186. virtual void Add_Delayed_Visible_Material_Pass(MaterialPassClass * pass, MeshClass * mesh) = 0;
  187. virtual void Render_Delayed_Procedural_Material_Passes(void) = 0;
  188. };
  189. /**
  190. ** DX8RigidFVFCategoryContainer
  191. ** This is an FVFCategoryContainer for rigid (non-skin) meshes
  192. */
  193. class DX8RigidFVFCategoryContainer : public DX8FVFCategoryContainer
  194. {
  195. public:
  196. DX8RigidFVFCategoryContainer(unsigned FVF,bool sorting);
  197. ~DX8RigidFVFCategoryContainer();
  198. void Add_Mesh(MeshClass* mmc);
  199. void Log(bool only_visible);
  200. bool Check_If_Mesh_Fits(MeshModelClass* mmc);
  201. void Render(void); // Generic render function
  202. /*
  203. ** This method adds a material pass which must be rendered after all of the other rendering is complete.
  204. ** This is needed whenever a mesh turns off its base passes and renders a translucent pass on its geometry.
  205. */
  206. virtual void Add_Delayed_Visible_Material_Pass(MaterialPassClass * pass, MeshClass * mesh);
  207. virtual void Render_Delayed_Procedural_Material_Passes(void);
  208. protected:
  209. VertexBufferClass * vertex_buffer;
  210. int used_vertices;
  211. MatPassTaskClass * delayed_matpass_head;
  212. MatPassTaskClass * delayed_matpass_tail;
  213. };
  214. /**
  215. ** DX8SkinFVFCategoryContainer
  216. ** This is an FVFCategoryContainer for skin meshes
  217. */
  218. class DX8SkinFVFCategoryContainer: public DX8FVFCategoryContainer
  219. {
  220. public:
  221. DX8SkinFVFCategoryContainer(bool sorting);
  222. ~DX8SkinFVFCategoryContainer();
  223. void Render(void);
  224. void Add_Mesh(MeshClass* mmc);
  225. void Log(bool only_visible);
  226. bool Check_If_Mesh_Fits(MeshModelClass* mmc);
  227. void Add_Visible_Skin(MeshClass * mesh);
  228. /*
  229. ** Since skins are already rendered after the rigid meshes, the Add_Delayed_Material_Pass function simply
  230. ** routes into the Add_Visible_Material_Pass method and no extra overhead is added.
  231. */
  232. virtual void Add_Delayed_Visible_Material_Pass(MaterialPassClass * pass, MeshClass * mesh) { Add_Visible_Material_Pass(pass,mesh); }
  233. virtual void Render_Delayed_Procedural_Material_Passes(void) { }
  234. private:
  235. void Reset();
  236. unsigned int VisibleVertexCount;
  237. MeshClass * VisibleSkinHead;
  238. };
  239. /**
  240. ** MeshRegKeyStruct, _RegisteredMeshTable
  241. ** This hash table is used to map model+lighting pointer pairs back to meshes
  242. ** which are registered with the mesh rendering system. Whenever a mesh is using
  243. ** the same model and lighting array, it can simply clone the polygon renderers
  244. ** and re-use the vb and ib data.
  245. */
  246. struct MeshRegKeyStruct
  247. {
  248. MeshRegKeyStruct(void) : Model(NULL), UserLighting(NULL) {}
  249. MeshRegKeyStruct(MeshModelClass * mdl,unsigned int * lighting) : Model(mdl), UserLighting(lighting) {}
  250. bool operator == (const MeshRegKeyStruct & that) { return ((Model == that.Model) && (UserLighting == that.UserLighting)); }
  251. MeshModelClass * Model;
  252. unsigned int * UserLighting;
  253. };
  254. inline unsigned int HashTemplateKeyClass<MeshRegKeyStruct>::Get_Hash_Value(const MeshRegKeyStruct& key)
  255. {
  256. unsigned int hval = (unsigned int)(key.Model) + (unsigned int)(key.UserLighting);
  257. hval = hval + (hval>>5) + (hval>>10) + (hval >> 20);
  258. return hval;
  259. }
  260. /**
  261. ** DX8MeshRendererClass
  262. ** This object is controller for the entire DX8 mesh rendering system. It organizes mesh
  263. ** fragments into groups based on FVF, texture, and material. During rendering, a list of
  264. ** the visible mesh fragments is composed and rendered. There is a global instance of this
  265. ** class called TheDX8MeshRenderer that should be used for all mesh rendering.
  266. */
  267. class DX8MeshRendererClass
  268. {
  269. public:
  270. DX8MeshRendererClass();
  271. ~DX8MeshRendererClass();
  272. void Init();
  273. void Shutdown();
  274. void Flush();
  275. void Clear_Pending_Delete_Lists();
  276. void Log_Statistics_String(bool only_visible);
  277. static void Request_Log_Statistics();
  278. void Register_Mesh_Type(MeshClass* mesh);
  279. void Unregister_Mesh_Type(MeshClass* mesh);
  280. void Set_Camera(CameraClass* cam) { camera=cam; }
  281. CameraClass * Peek_Camera(void) { return camera; }
  282. void Add_To_Render_List(DecalMeshClass * decalmesh);
  283. // Enable or disable lighting on all objects inserted from now on. (Doesn't affect the objects that are already in the lists)
  284. void Enable_Lighting(bool enable) { enable_lighting=enable; }
  285. // This should be called at the beginning of a game or menu or after a major modifications to the scene...
  286. void Invalidate();
  287. protected:
  288. void Render_Decal_Meshes(void);
  289. bool enable_lighting;
  290. CameraClass * camera;
  291. SimpleDynVecClass<FVFCategoryList *> texture_category_container_lists_rigid;
  292. FVFCategoryList * texture_category_container_list_skin;
  293. DecalMeshClass * visible_decal_meshes;
  294. HashTemplateClass<MeshRegKeyStruct,MeshClass*> _RegisteredMeshTable;
  295. };
  296. extern DX8MeshRendererClass TheDX8MeshRenderer;
  297. #endif