CmVertexIndexData.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __VertexIndexData_H__
  25. #define __VertexIndexData_H__
  26. #include "CmPrerequisites.h"
  27. #include "CmHardwareVertexBuffer.h"
  28. #include "CmHardwareIndexBuffer.h"
  29. namespace CamelotEngine {
  30. /** \addtogroup Core
  31. * @{
  32. */
  33. /** \addtogroup RenderSystem
  34. * @{
  35. */
  36. /// Define a list of usage flags
  37. typedef vector<HardwareBuffer::Usage>::type BufferUsageList;
  38. /** Summary class collecting together vertex source information. */
  39. class CM_EXPORT VertexData
  40. {
  41. private:
  42. /// Protected copy constructor, to prevent misuse
  43. VertexData(const VertexData& rhs); /* do nothing, should not use */
  44. /// Protected operator=, to prevent misuse
  45. VertexData& operator=(const VertexData& rhs); /* do not use */
  46. HardwareBufferManagerBase* mMgr;
  47. public:
  48. /** Constructor.
  49. @note
  50. This constructor creates the VertexDeclaration and VertexBufferBinding
  51. automatically, and arranges for their deletion afterwards.
  52. @param mgr Optional HardwareBufferManager from which to create resources
  53. */
  54. VertexData(HardwareBufferManagerBase* mgr = 0);
  55. /** Constructor.
  56. @note
  57. This constructor receives the VertexDeclaration and VertexBufferBinding
  58. from the caller, and as such does not arrange for their deletion afterwards,
  59. the caller remains responsible for that.
  60. @param dcl The VertexDeclaration to use
  61. @param bind The VertexBufferBinding to use
  62. */
  63. VertexData(VertexDeclarationPtr dcl, VertexBufferBinding* bind);
  64. ~VertexData();
  65. /** Declaration of the vertex to be used in this operation.
  66. @remarks Note that this is created for you on construction.
  67. */
  68. VertexDeclarationPtr vertexDeclaration;
  69. /** The vertex buffer bindings to be used.
  70. @remarks Note that this is created for you on construction.
  71. */
  72. VertexBufferBinding* vertexBufferBinding;
  73. /// Whether this class should delete the declaration and binding
  74. bool mDeleteDclBinding;
  75. /// The base vertex index to start from
  76. size_t vertexStart;
  77. /// The number of vertices used in this operation
  78. size_t vertexCount;
  79. /** Clones this vertex data, potentially including replicating any vertex buffers.
  80. @param copyData Whether to create new vertex buffers too or just reference the existing ones
  81. @param mgr If supplied, the buffer manager through which copies should be made
  82. @remarks The caller is expected to delete the returned pointer when ready
  83. */
  84. VertexData* clone(bool copyData = true, HardwareBufferManagerBase* mgr = 0) const;
  85. /** Modifies the vertex data to be suitable for use for rendering shadow geometry.
  86. @remarks
  87. Preparing vertex data to generate a shadow volume involves firstly ensuring that the
  88. vertex buffer containing the positions is a standalone vertex buffer,
  89. with no other components in it. This method will therefore break apart any existing
  90. vertex buffers if position is sharing a vertex buffer.
  91. Secondly, it will double the size of this vertex buffer so that there are 2 copies of
  92. the position data for the mesh. The first half is used for the original, and the second
  93. half is used for the 'extruded' version. The vertex count used to render will remain
  94. the same though, so as not to add any overhead to regular rendering of the object.
  95. Both copies of the position are required in one buffer because shadow volumes stretch
  96. from the original mesh to the extruded version.
  97. @par
  98. It's important to appreciate that this method can fundamentally change the structure of your
  99. vertex buffers, although in reality they will be new buffers. As it happens, if other
  100. objects are using the original buffers then they will be unaffected because the reference
  101. counting will keep them intact. However, if you have made any assumptions about the
  102. structure of the vertex data in the buffers of this object, you may have to rethink them.
  103. */
  104. void prepareForShadowVolume(void);
  105. /** Additional shadow volume vertex buffer storage.
  106. @remarks
  107. This additional buffer is only used where we have prepared this VertexData for
  108. use in shadow volume construction, and where the current render system supports
  109. vertex programs. This buffer contains the 'w' vertex position component which will
  110. be used by that program to differentiate between extruded and non-extruded vertices.
  111. This 'w' component cannot be included in the original position buffer because
  112. DirectX does not allow 4-component positions in the fixed-function pipeline, and the original
  113. position buffer must still be usable for fixed-function rendering.
  114. @par
  115. Note that we don't store any vertex declaration or vertex buffer binding here because this
  116. can be reused in the shadow algorithm.
  117. */
  118. HardwareVertexBufferPtr hardwareShadowVolWBuffer;
  119. /** Reorganises the data in the vertex buffers according to the
  120. new vertex declaration passed in. Note that new vertex buffers
  121. are created and written to, so if the buffers being referenced
  122. by this vertex data object are also used by others, then the
  123. original buffers will not be damaged by this operation.
  124. Once this operation has completed, the new declaration
  125. passed in will overwrite the current one.
  126. @param newDeclaration The vertex declaration which will be used
  127. for the reorganised buffer state. Note that the new declaration
  128. must not include any elements which do not already exist in the
  129. current declaration; you can drop elements by
  130. excluding them from the declaration if you wish, however.
  131. @param bufferUsages Vector of usage flags which indicate the usage options
  132. for each new vertex buffer created. The indexes of the entries must correspond
  133. to the buffer binding values referenced in the declaration.
  134. @param mgr Optional pointer to the manager to use to create new declarations
  135. and buffers etc. If not supplied, the HardwareBufferManager singleton will be used
  136. */
  137. void reorganiseBuffers(VertexDeclarationPtr newDeclaration, const BufferUsageList& bufferUsage,
  138. HardwareBufferManagerBase* mgr = 0);
  139. /** Reorganises the data in the vertex buffers according to the
  140. new vertex declaration passed in. Note that new vertex buffers
  141. are created and written to, so if the buffers being referenced
  142. by this vertex data object are also used by others, then the
  143. original buffers will not be damaged by this operation.
  144. Once this operation has completed, the new declaration
  145. passed in will overwrite the current one.
  146. This version of the method derives the buffer usages from the existing
  147. buffers, by using the 'most flexible' usage from the equivalent sources.
  148. @param newDeclaration The vertex declaration which will be used
  149. for the reorganised buffer state. Note that the new delcaration
  150. must not include any elements which do not already exist in the
  151. current declaration; you can drop elements by
  152. excluding them from the declaration if you wish, however.
  153. @param mgr Optional pointer to the manager to use to create new declarations
  154. and buffers etc. If not supplied, the HardwareBufferManager singleton will be used
  155. */
  156. void reorganiseBuffers(VertexDeclarationPtr newDeclaration, HardwareBufferManagerBase* mgr = 0);
  157. /** Remove any gaps in the vertex buffer bindings.
  158. @remarks
  159. This is useful if you've removed elements and buffers from this vertex
  160. data and want to remove any gaps in the vertex buffer bindings. This
  161. method is mainly useful when reorganising vertex data manually.
  162. @note
  163. This will cause binding index of the elements in the vertex declaration
  164. to be altered to new binding index.
  165. */
  166. void closeGapsInBindings(void);
  167. /** Remove all vertex buffers that never used by the vertex declaration.
  168. @remarks
  169. This is useful if you've removed elements from the vertex declaration
  170. and want to unreference buffers that never used any more. This method
  171. is mainly useful when reorganising vertex data manually.
  172. @note
  173. This also remove any gaps in the vertex buffer bindings.
  174. */
  175. void removeUnusedBuffers(void);
  176. /** Convert all packed colour values (VET_COLOUR_*) in buffers used to
  177. another type.
  178. @param srcType The source colour type to assume if the ambiguous VET_COLOUR
  179. is encountered.
  180. @param destType The destination colour type, must be VET_COLOUR_ABGR or
  181. VET_COLOUR_ARGB.
  182. */
  183. void convertPackedColour(VertexElementType srcType, VertexElementType destType);
  184. };
  185. /** Summary class collecting together index data source information. */
  186. class CM_EXPORT IndexData
  187. {
  188. protected:
  189. /// Protected copy constructor, to prevent misuse
  190. IndexData(const IndexData& rhs); /* do nothing, should not use */
  191. /// Protected operator=, to prevent misuse
  192. IndexData& operator=(const IndexData& rhs); /* do not use */
  193. public:
  194. IndexData();
  195. ~IndexData();
  196. /// pointer to the HardwareIndexBuffer to use, must be specified if useIndexes = true
  197. HardwareIndexBufferPtr indexBuffer;
  198. /// index in the buffer to start from for this operation
  199. size_t indexStart;
  200. /// The number of indexes to use from the buffer
  201. size_t indexCount;
  202. /** Clones this index data, potentially including replicating the index buffer.
  203. @param copyData Whether to create new buffers too or just reference the existing ones
  204. @param mgr If supplied, the buffer manager through which copies should be made
  205. @remarks The caller is expected to delete the returned pointer when finished
  206. */
  207. IndexData* clone(bool copyData = true, HardwareBufferManagerBase* mgr = 0) const;
  208. /** Re-order the indexes in this index data structure to be more
  209. vertex cache friendly; that is to re-use the same vertices as close
  210. together as possible.
  211. @remarks
  212. Can only be used for index data which consists of triangle lists.
  213. It would in fact be pointless to use it on triangle strips or fans
  214. in any case.
  215. */
  216. void optimiseVertexCacheTriList(void);
  217. };
  218. /** Vertex cache profiler.
  219. @remarks
  220. Utility class for evaluating the effectiveness of the use of the vertex
  221. cache by a given index buffer.
  222. */
  223. class CM_EXPORT VertexCacheProfiler
  224. {
  225. public:
  226. enum CacheType {
  227. FIFO, LRU
  228. };
  229. VertexCacheProfiler(unsigned int cachesize = 16, CacheType cachetype = FIFO )
  230. : size ( cachesize ), type ( cachetype ), tail (0), buffersize (0), hit (0), miss (0)
  231. {
  232. cache = (UINT32*)malloc(sizeof(UINT32) * size);
  233. }
  234. ~VertexCacheProfiler()
  235. {
  236. free(cache);
  237. }
  238. void profile(const HardwareIndexBufferPtr& indexBuffer);
  239. void reset() { hit = 0; miss = 0; tail = 0; buffersize = 0; }
  240. void flush() { tail = 0; buffersize = 0; }
  241. unsigned int getHits() { return hit; }
  242. unsigned int getMisses() { return miss; }
  243. unsigned int getSize() { return size; }
  244. private:
  245. unsigned int size;
  246. UINT32 *cache;
  247. CacheType type;
  248. unsigned int tail, buffersize;
  249. unsigned int hit, miss;
  250. bool inCache(unsigned int index);
  251. };
  252. /** @} */
  253. /** @} */
  254. }
  255. #endif