2
0

dx8vertexbuffer.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/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 RefCountClass
  73. {
  74. protected:
  75. VertexBufferClass(unsigned type, unsigned FVF, unsigned short VertexCount);
  76. virtual ~VertexBufferClass();
  77. public:
  78. inline const FVFInfoClass& FVF_Info() const { return *fvf_info; }
  79. inline unsigned short Get_Vertex_Count() const { return VertexCount; }
  80. inline unsigned Type() const { return type; }
  81. void Add_Engine_Ref() const;
  82. void Release_Engine_Ref() const;
  83. inline unsigned Engine_Refs() const { return engine_refs; }
  84. class WriteLockClass : public VertexBufferLockClass
  85. {
  86. public:
  87. WriteLockClass(VertexBufferClass* vertex_buffer);
  88. ~WriteLockClass();
  89. };
  90. class AppendLockClass : public VertexBufferLockClass
  91. {
  92. public:
  93. AppendLockClass(VertexBufferClass* vertex_buffer,unsigned start_index, unsigned index_range);
  94. ~AppendLockClass();
  95. };
  96. static unsigned Get_Total_Buffer_Count();
  97. static unsigned Get_Total_Allocated_Vertices();
  98. static unsigned Get_Total_Allocated_Memory();
  99. protected:
  100. unsigned type;
  101. unsigned short VertexCount;
  102. mutable int engine_refs;
  103. FVFInfoClass* fvf_info;
  104. };
  105. /**
  106. ** Dynamic vertex buffer access is a wrapper to a single cycled dynamic vertex
  107. ** buffer.
  108. ** DynamicVBAccess gains an access to the dynamic vertex buffer and only
  109. ** only of these are allowed at any one time.
  110. **
  111. ** The dynamic fvf buffers are always of the same type.
  112. **
  113. ** NOTE: Dynamic vertex buffers accessors should only be used locally!
  114. **
  115. */
  116. class DynamicVBAccessClass
  117. {
  118. friend DX8Wrapper;
  119. friend SortingRendererClass;
  120. const FVFInfoClass& FVFInfo;
  121. unsigned Type;
  122. unsigned short VertexCount;
  123. unsigned short VertexBufferOffset;
  124. VertexBufferClass* VertexBuffer;
  125. // static VertexFormatXYZNDUV2* _Get_Sorting_Vertex_Array();
  126. void Allocate_Sorting_Dynamic_Buffer();
  127. void Allocate_DX8_Dynamic_Buffer();
  128. public:
  129. // Type parameter can be either BUFFER_TYPE_DYNAMIC_DX8 or BUFFER_TYPE_DYNAMIC_SORTING.
  130. // Note: Even though the constructor takes fvf as a parameter, currently the
  131. // only acceptable parameter is "dynamic_fvf_type". Any other type will
  132. // result to an assert.
  133. DynamicVBAccessClass(unsigned type,unsigned fvf,unsigned short vertex_count);
  134. ~DynamicVBAccessClass();
  135. // Access fvf
  136. const FVFInfoClass& FVF_Info() const { return FVFInfo; }
  137. unsigned Get_Type() const { return Type; }
  138. unsigned short Get_Vertex_Count() const { return VertexCount; }
  139. // Call at the end of the execution, or at whatever time you wish to release
  140. // the recycled dynamic vertex buffer.
  141. static void _Deinit();
  142. static void _Reset(bool frame_changed);
  143. // To lock the vertex buffer, create instance of this write class locally.
  144. // The buffer is automatically unlocked when you exit the scope.
  145. class WriteLockClass// : public VertexBufferLockClass
  146. {
  147. DynamicVBAccessClass* DynamicVBAccess;
  148. VertexFormatXYZNDUV2 * Vertices;
  149. public:
  150. WriteLockClass(DynamicVBAccessClass* vb_access);
  151. ~WriteLockClass();
  152. // Use this function to get a pointer to the first vertex you can write into.
  153. // If we ever change the format used by DynamicVBAccessClass, then the
  154. // return type of this function will change and we'll easily find all code
  155. // using it.
  156. VertexFormatXYZNDUV2 * Get_Formatted_Vertex_Array();
  157. };
  158. friend WriteLockClass;
  159. };
  160. // ----------------------------------------------------------------------------
  161. inline VertexFormatXYZNDUV2 * DynamicVBAccessClass::WriteLockClass::Get_Formatted_Vertex_Array()
  162. {
  163. // assert that the format of the dynamic vertex buffer is still what we think it is.
  164. WWASSERT(DynamicVBAccess->VertexBuffer->FVF_Info().Get_FVF() == (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX2|D3DFVF_DIFFUSE));
  165. return Vertices;
  166. }
  167. // ----------------------------------------------------------------------------
  168. /**
  169. ** DX8VertexBufferClass
  170. ** This class wraps a DX8 vertex buffer. Use the lock objects to modify or append to the vertex buffer.
  171. */
  172. class DX8VertexBufferClass : public VertexBufferClass
  173. {
  174. protected:
  175. ~DX8VertexBufferClass();
  176. public:
  177. enum UsageType {
  178. USAGE_DEFAULT=0,
  179. USAGE_DYNAMIC=1,
  180. USAGE_SOFTWAREPROCESSING=2,
  181. USAGE_NPATCHES=4
  182. };
  183. DX8VertexBufferClass(unsigned FVF, unsigned short VertexCount, UsageType usage=USAGE_DEFAULT);
  184. DX8VertexBufferClass(const Vector3* vertices, const Vector3* normals, const Vector2* tex_coords, unsigned short VertexCount,UsageType usage=USAGE_DEFAULT);
  185. DX8VertexBufferClass(const Vector3* vertices, const Vector3* normals, const Vector4* diffuse, const Vector2* tex_coords, unsigned short VertexCount,UsageType usage=USAGE_DEFAULT);
  186. DX8VertexBufferClass(const Vector3* vertices, const Vector4* diffuse, const Vector2* tex_coords, unsigned short VertexCount,UsageType usage=USAGE_DEFAULT);
  187. DX8VertexBufferClass(const Vector3* vertices, const Vector2* tex_coords, unsigned short VertexCount,UsageType usage=USAGE_DEFAULT);
  188. IDirect3DVertexBuffer8* Get_DX8_Vertex_Buffer() { return VertexBuffer; }
  189. void Copy(const Vector3* loc, unsigned first_vertex, unsigned count);
  190. void Copy(const Vector3* loc, const Vector2* uv, unsigned first_vertex, unsigned count);
  191. void Copy(const Vector3* loc, const Vector3* norm, unsigned first_vertex, unsigned count);
  192. void Copy(const Vector3* loc, const Vector3* norm, const Vector2* uv, unsigned first_vertex, unsigned count);
  193. void Copy(const Vector3* loc, const Vector3* norm, const Vector2* uv, const Vector4* diffuse, unsigned first_vertex, unsigned count);
  194. void Copy(const Vector3* loc, const Vector2* uv, const Vector4* diffuse, unsigned first_vertex, unsigned count);
  195. protected:
  196. IDirect3DVertexBuffer8* VertexBuffer;
  197. void Create_Vertex_Buffer(UsageType usage);
  198. };
  199. /**
  200. ** SortingVertexBufferClass
  201. ** This class acts as a vertex buffer for the vertices that need to be passed to alpha renderer.
  202. */
  203. class SortingVertexBufferClass : public VertexBufferClass
  204. {
  205. friend DX8Wrapper;
  206. friend SortingRendererClass;
  207. friend VertexBufferClass::WriteLockClass;
  208. friend VertexBufferClass::AppendLockClass;
  209. friend DynamicVBAccessClass::WriteLockClass;
  210. VertexFormatXYZNDUV2* VertexBuffer;
  211. protected:
  212. ~SortingVertexBufferClass();
  213. public:
  214. SortingVertexBufferClass(unsigned short VertexCount);
  215. };
  216. #endif //DX8VERTEXBUFFER_H