dx8vertexbuffer.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/dx8vertexbuffer.h $*
  25. * *
  26. * Original Author:: Jani Penttinen *
  27. * *
  28. * $Author:: Jani_p $*
  29. * *
  30. * $Modtime:: 7/09/01 8:15p $*
  31. * *
  32. * $Revision:: 25 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #ifndef DX8VERTEXBUFFER_H
  41. #define DX8VERTEXBUFFER_H
  42. #include "always.h"
  43. #include "wwdebug.h"
  44. #include "refcount.h"
  45. #include "dx8fvf.h"
  46. const unsigned dynamic_fvf_type=D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX2|D3DFVF_DIFFUSE;
  47. class DX8Wrapper;
  48. class SortingRendererClass;
  49. class Vector2;
  50. class Vector3;
  51. class Vector4;
  52. class StringClass;
  53. class DX8VertexBufferClass;
  54. class FVFInfoClass;
  55. struct IDirect3DVertexBuffer8;
  56. class VertexBufferClass;
  57. struct VertexFormatXYZNDUV2;
  58. class VertexBufferLockClass
  59. {
  60. protected:
  61. VertexBufferClass* VertexBuffer;
  62. void* Vertices;
  63. // This class can't be used directly, so constructor as to be protected
  64. VertexBufferLockClass(VertexBufferClass* vertex_buffer_) : VertexBuffer(vertex_buffer_) {}
  65. public:
  66. void* Get_Vertex_Array() { return Vertices; }
  67. };
  68. /**
  69. ** DX8VertexBufferClass
  70. ** This class wraps a DX8 vertex buffer. Use the lock objects to modify or append to the vertex buffer.
  71. */
  72. class VertexBufferClass : public W3DMPO, public RefCountClass
  73. {
  74. // nope, an ABC
  75. //W3DMPO_GLUE(VertexBufferClass)
  76. protected:
  77. VertexBufferClass(unsigned type, unsigned FVF, unsigned short VertexCount);
  78. virtual ~VertexBufferClass();
  79. public:
  80. inline const FVFInfoClass& FVF_Info() const { return *fvf_info; }
  81. inline unsigned short Get_Vertex_Count() const { return VertexCount; }
  82. inline unsigned Type() const { return type; }
  83. void Add_Engine_Ref() const;
  84. void Release_Engine_Ref() const;
  85. inline unsigned Engine_Refs() const { return engine_refs; }
  86. class WriteLockClass : public VertexBufferLockClass
  87. {
  88. public:
  89. WriteLockClass(VertexBufferClass* vertex_buffer);
  90. ~WriteLockClass();
  91. };
  92. class AppendLockClass : public VertexBufferLockClass
  93. {
  94. public:
  95. AppendLockClass(VertexBufferClass* vertex_buffer,unsigned start_index, unsigned index_range);
  96. ~AppendLockClass();
  97. };
  98. static unsigned Get_Total_Buffer_Count();
  99. static unsigned Get_Total_Allocated_Vertices();
  100. static unsigned Get_Total_Allocated_Memory();
  101. protected:
  102. unsigned type;
  103. unsigned short VertexCount;
  104. mutable int engine_refs;
  105. FVFInfoClass* fvf_info;
  106. };
  107. /**
  108. ** Dynamic vertex buffer access is a wrapper to a single cycled dynamic vertex
  109. ** buffer.
  110. ** DynamicVBAccess gains an access to the dynamic vertex buffer and only
  111. ** only of these are allowed at any one time.
  112. **
  113. ** The dynamic fvf buffers are always of the same type.
  114. **
  115. ** NOTE: Dynamic vertex buffers accessors should only be used locally!
  116. **
  117. */
  118. class DynamicVBAccessClass
  119. {
  120. friend DX8Wrapper;
  121. friend SortingRendererClass;
  122. const FVFInfoClass& FVFInfo;
  123. unsigned Type;
  124. unsigned short VertexCount;
  125. unsigned short VertexBufferOffset;
  126. VertexBufferClass* VertexBuffer;
  127. // static VertexFormatXYZNDUV2* _Get_Sorting_Vertex_Array();
  128. void Allocate_Sorting_Dynamic_Buffer();
  129. void Allocate_DX8_Dynamic_Buffer();
  130. public:
  131. // Type parameter can be either BUFFER_TYPE_DYNAMIC_DX8 or BUFFER_TYPE_DYNAMIC_SORTING.
  132. // Note: Even though the constructor takes fvf as a parameter, currently the
  133. // only acceptable parameter is "dynamic_fvf_type". Any other type will
  134. // result to an assert.
  135. DynamicVBAccessClass(unsigned type,unsigned fvf,unsigned short vertex_count);
  136. ~DynamicVBAccessClass();
  137. // Access fvf
  138. const FVFInfoClass& FVF_Info() const { return FVFInfo; }
  139. unsigned Get_Type() const { return Type; }
  140. unsigned short Get_Vertex_Count() const { return VertexCount; }
  141. // Call at the end of the execution, or at whatever time you wish to release
  142. // the recycled dynamic vertex buffer.
  143. static void _Deinit();
  144. static void _Reset(bool frame_changed);
  145. static unsigned short Get_Default_Vertex_Count(void); ///<current size of dynamic vertex buffer
  146. // To lock the vertex buffer, create instance of this write class locally.
  147. // The buffer is automatically unlocked when you exit the scope.
  148. class WriteLockClass// : public VertexBufferLockClass
  149. {
  150. DynamicVBAccessClass* DynamicVBAccess;
  151. VertexFormatXYZNDUV2 * Vertices;
  152. public:
  153. WriteLockClass(DynamicVBAccessClass* vb_access);
  154. ~WriteLockClass();
  155. // Use this function to get a pointer to the first vertex you can write into.
  156. // If we ever change the format used by DynamicVBAccessClass, then the
  157. // return type of this function will change and we'll easily find all code
  158. // using it.
  159. VertexFormatXYZNDUV2 * Get_Formatted_Vertex_Array();
  160. };
  161. friend WriteLockClass;
  162. };
  163. // ----------------------------------------------------------------------------
  164. inline VertexFormatXYZNDUV2 * DynamicVBAccessClass::WriteLockClass::Get_Formatted_Vertex_Array()
  165. {
  166. // assert that the format of the dynamic vertex buffer is still what we think it is.
  167. WWASSERT(DynamicVBAccess->VertexBuffer->FVF_Info().Get_FVF() == (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX2|D3DFVF_DIFFUSE));
  168. return Vertices;
  169. }
  170. // ----------------------------------------------------------------------------
  171. /**
  172. ** DX8VertexBufferClass
  173. ** This class wraps a DX8 vertex buffer. Use the lock objects to modify or append to the vertex buffer.
  174. */
  175. class DX8VertexBufferClass : public VertexBufferClass
  176. {
  177. W3DMPO_GLUE(DX8VertexBufferClass)
  178. protected:
  179. ~DX8VertexBufferClass();
  180. public:
  181. enum UsageType {
  182. USAGE_DEFAULT=0,
  183. USAGE_DYNAMIC=1,
  184. USAGE_SOFTWAREPROCESSING=2,
  185. USAGE_NPATCHES=4
  186. };
  187. DX8VertexBufferClass(unsigned FVF, unsigned short VertexCount, UsageType usage=USAGE_DEFAULT);
  188. DX8VertexBufferClass(const Vector3* vertices, const Vector3* normals, const Vector2* tex_coords, unsigned short VertexCount,UsageType usage=USAGE_DEFAULT);
  189. DX8VertexBufferClass(const Vector3* vertices, const Vector3* normals, const Vector4* diffuse, const Vector2* tex_coords, unsigned short VertexCount,UsageType usage=USAGE_DEFAULT);
  190. DX8VertexBufferClass(const Vector3* vertices, const Vector4* diffuse, const Vector2* tex_coords, unsigned short VertexCount,UsageType usage=USAGE_DEFAULT);
  191. DX8VertexBufferClass(const Vector3* vertices, const Vector2* tex_coords, unsigned short VertexCount,UsageType usage=USAGE_DEFAULT);
  192. IDirect3DVertexBuffer8* Get_DX8_Vertex_Buffer() { return VertexBuffer; }
  193. void Copy(const Vector3* loc, unsigned first_vertex, unsigned count);
  194. void Copy(const Vector3* loc, const Vector2* uv, unsigned first_vertex, unsigned count);
  195. void Copy(const Vector3* loc, const Vector3* norm, unsigned first_vertex, unsigned count);
  196. void Copy(const Vector3* loc, const Vector3* norm, const Vector2* uv, unsigned first_vertex, unsigned count);
  197. void Copy(const Vector3* loc, const Vector3* norm, const Vector2* uv, const Vector4* diffuse, unsigned first_vertex, unsigned count);
  198. void Copy(const Vector3* loc, const Vector2* uv, const Vector4* diffuse, unsigned first_vertex, unsigned count);
  199. protected:
  200. IDirect3DVertexBuffer8* VertexBuffer;
  201. void Create_Vertex_Buffer(UsageType usage);
  202. };
  203. /**
  204. ** SortingVertexBufferClass
  205. ** This class acts as a vertex buffer for the vertices that need to be passed to alpha renderer.
  206. */
  207. class SortingVertexBufferClass : public VertexBufferClass
  208. {
  209. W3DMPO_GLUE(SortingVertexBufferClass)
  210. friend DX8Wrapper;
  211. friend SortingRendererClass;
  212. friend VertexBufferClass::WriteLockClass;
  213. friend VertexBufferClass::AppendLockClass;
  214. friend DynamicVBAccessClass::WriteLockClass;
  215. VertexFormatXYZNDUV2* VertexBuffer;
  216. protected:
  217. ~SortingVertexBufferClass();
  218. public:
  219. SortingVertexBufferClass(unsigned short VertexCount);
  220. };
  221. #endif //DX8VERTEXBUFFER_H