dx8renderer.h 13 KB

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