W3DBufferManager.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: W3DBufferManager.h ///////////////////////////////////////////////////////////////////////////
  24. // Author: Mark Wilczynski
  25. // Desc: A system for managing partial vertex buffer allocations.
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #pragma once
  28. #ifndef _W3D_VERTEX_BUFFER_MANAGER
  29. #define _W3D_VERTEX_BUFFER_MANAGER
  30. #include "Lib/BaseType.h"
  31. #include "dx8vertexbuffer.h"
  32. #include "dx8indexbuffer.h"
  33. #define MAX_VB_SIZES 128 //number of different sized VB slots allowed.
  34. #define MIN_SLOT_SIZE 32 //minimum number of vertices allocated per slot (power of 2). See also MIN_SLOT_SIZE_SHIFT.
  35. #define MIN_SLOT_SIZE_SHIFT 5 //used for division by MIN_SLOT_SIZE
  36. #define MAX_VERTEX_BUFFERS_CREATED 32 //maximum number of D3D vertex buffers allowed to create per vertex type.
  37. #define DEFAULT_VERTEX_BUFFER_SIZE 8192 //this size ends up generating VB's of about 256Kbytes
  38. #define MAX_NUMBER_SLOTS 4096 //maximum number of slots that can be allocated.
  39. #define MAX_IB_SIZES 128 //number of different sized IB slots allowed (goes all the way up to 65536)
  40. #define MAX_INDEX_BUFFERS_CREATED 32
  41. #define DEFAULT_INDEX_BUFFER_SIZE 32768
  42. class W3DBufferManager
  43. {
  44. public:
  45. //List of all possible vertex formats available to the game.
  46. enum VBM_FVF_TYPES
  47. {
  48. VBM_FVF_XYZ, //position
  49. VBM_FVF_XYZD, //position, diffuse
  50. VBM_FVF_XYZUV, //position, uv
  51. VBM_FVF_XYZDUV, //position, diffuse, uv
  52. VBM_FVF_XYZUV2, //position, uv1, uv2
  53. VBM_FVF_XYZDUV2, //position, diffuse, uv1, uv2
  54. VBM_FVF_XYZN, //position, normal
  55. VBM_FVF_XYZND, //position, normal, diffuse
  56. VBM_FVF_XYZNUV, //position, normal, uv
  57. VBM_FVF_XYZNDUV, //position, normal, diffuse, uv
  58. VBM_FVF_XYZNUV2, //position, normal, uv1, uv2
  59. VBM_FVF_XYZNDUV2, //position, normal, diffuse, uv1, uv2
  60. VBM_FVF_XYZRHW, //transformed position
  61. VBM_FVF_XYZRHWD, //transformed position, diffuse
  62. VBM_FVF_XYZRHWUV, //transformed position, uv
  63. VBM_FVF_XYZRHWDUV, //transformed position, diffuse, uv
  64. VBM_FVF_XYZRHWUV2, //transformed position, uv1, uv2
  65. VBM_FVF_XYZRHWDUV2, //transformed position, diffuse, uv1, uv2
  66. MAX_FVF
  67. };
  68. struct W3DRenderTask
  69. {
  70. W3DRenderTask *m_nextTask; ///<next rendering task
  71. };
  72. struct W3DVertexBuffer; //forward reference
  73. struct W3DIndexBuffer; //forward reference
  74. struct W3DVertexBufferSlot
  75. {
  76. Int m_size; ///<number of vertices in slot
  77. Int m_start; ///<index of first vertex within VB
  78. W3DVertexBuffer *m_VB; ///<vertex buffer holding this slot.
  79. W3DVertexBufferSlot *m_prevSameSize; //previous slot of equal size.
  80. W3DVertexBufferSlot *m_nextSameSize; //next slot of equal size.
  81. W3DVertexBufferSlot *m_prevSameVB; //previous slot in same VB.
  82. W3DVertexBufferSlot *m_nextSameVB; //next slot in same VB.
  83. };
  84. struct W3DVertexBuffer
  85. {
  86. VBM_FVF_TYPES m_format; ///<format of vertices in this VB.
  87. W3DVertexBufferSlot *m_usedSlots; ///<slots inside this vertex buffer being used.
  88. Int m_startFreeIndex; ///<index of vertex at start of unallocated memory.
  89. Int m_size; ///<number of vertices allowed in VB.
  90. W3DVertexBuffer *m_nextVB; ///<next vertex buffer of same type.
  91. DX8VertexBufferClass *m_DX8VertexBuffer; ///<actual DX8 vertex buffer interface
  92. W3DRenderTask *m_renderTaskList; ///<used to help app sort its D3D access by VB.
  93. };
  94. struct W3DIndexBufferSlot
  95. {
  96. Int m_size; ///<number of vertices in slot
  97. Int m_start; ///<index of first index within VB
  98. W3DIndexBuffer *m_IB; ///<index buffer holding this slot.
  99. W3DIndexBufferSlot *m_prevSameSize; //previous slot of equal size.
  100. W3DIndexBufferSlot *m_nextSameSize; //next slot of equal size.
  101. W3DIndexBufferSlot *m_prevSameIB; //previous slot in same VB.
  102. W3DIndexBufferSlot *m_nextSameIB; //next slot in same VB.
  103. };
  104. struct W3DIndexBuffer
  105. {
  106. W3DIndexBufferSlot *m_usedSlots; ///<slots inside this index buffer being used.
  107. Int m_startFreeIndex; ///<index of index at start of unallocated memory.
  108. Int m_size; ///<number of vertices allowed in VB.
  109. W3DIndexBuffer *m_nextIB; ///<next index buffer of same type.
  110. DX8IndexBufferClass *m_DX8IndexBuffer; ///<actual DX8 index buffer interface
  111. };
  112. W3DBufferManager(void);
  113. ~W3DBufferManager(void);
  114. ///return free vertex buffer memory slot.
  115. W3DVertexBufferSlot *getSlot(VBM_FVF_TYPES fvfType, Int size);
  116. ///return free index buffer memory slot.
  117. W3DIndexBufferSlot *getSlot(Int size);
  118. void releaseSlot(W3DVertexBufferSlot *vbSlot); ///<return slot to pool
  119. void releaseSlot(W3DIndexBufferSlot *vbSlot); ///<return slot to pool
  120. void freeAllSlots(void); ///<release all slots to pool.
  121. void freeAllBuffers(void); ///<release all vertex buffers to pool.
  122. void W3DBufferManager::ReleaseResources(void); ///<release D3D/W3D resources.
  123. Bool W3DBufferManager::ReAcquireResources(void); ///<reaquire D3D/W3D resources.
  124. ///allows iterating over vertex buffers used by manager. Input of NULL to get first.
  125. W3DVertexBuffer *getNextVertexBuffer(W3DVertexBuffer *pVb, VBM_FVF_TYPES type)
  126. { if (pVb == NULL)
  127. return m_W3DVertexBuffers[type];
  128. return pVb->m_nextVB;
  129. };
  130. static Int getDX8Format(VBM_FVF_TYPES format); ///<translates our vertex format into D3D equivalent
  131. protected:
  132. ///holds previously allocated slots that are free to fill again.
  133. W3DVertexBufferSlot *m_W3DVertexBufferSlots[MAX_FVF][MAX_VB_SIZES];
  134. ///holds allocated vertex buffers of each type.
  135. W3DVertexBuffer *m_W3DVertexBuffers[MAX_FVF];
  136. ///holds unallocated slot wrappers which were never initialized
  137. W3DVertexBufferSlot m_W3DVertexBufferEmptySlots[MAX_NUMBER_SLOTS];
  138. Int m_numEmptySlotsAllocated;
  139. ///holds unallocated vertex buffer wrappers which were never initialized
  140. W3DVertexBuffer m_W3DEmptyVertexBuffers[MAX_VERTEX_BUFFERS_CREATED];
  141. Int m_numEmptyVertexBuffersAllocated;
  142. ///holds previously allocated slots that are free to fill again.
  143. W3DIndexBufferSlot *m_W3DIndexBufferSlots[MAX_IB_SIZES];
  144. ///holds allocated index buffers of each type.
  145. W3DIndexBuffer *m_W3DIndexBuffers;
  146. ///holds unallocated slot wrappers which were never initialized
  147. W3DIndexBufferSlot m_W3DIndexBufferEmptySlots[MAX_NUMBER_SLOTS];
  148. Int m_numEmptyIndexSlotsAllocated;
  149. ///holds unallocated index buffer wrappers which were never initialized
  150. W3DIndexBuffer m_W3DEmptyIndexBuffers[MAX_INDEX_BUFFERS_CREATED];
  151. Int m_numEmptyIndexBuffersAllocated;
  152. ///allocate new memory inside a vertex buffer.
  153. W3DVertexBufferSlot *allocateSlotStorage(VBM_FVF_TYPES fvfType, Int size);
  154. W3DIndexBufferSlot *allocateSlotStorage(Int size);
  155. };
  156. extern W3DBufferManager *TheW3DBufferManager; //singleton
  157. #endif //_W3D_VERTEX_BUFFER_MANAGER