CmVertexIndexData.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. UINT32 vertexStart;
  77. /// The number of vertices used in this operation
  78. UINT32 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. /** Additional shadow volume vertex buffer storage.
  86. @remarks
  87. This additional buffer is only used where we have prepared this VertexData for
  88. use in shadow volume construction, and where the current render system supports
  89. vertex programs. This buffer contains the 'w' vertex position component which will
  90. be used by that program to differentiate between extruded and non-extruded vertices.
  91. This 'w' component cannot be included in the original position buffer because
  92. DirectX does not allow 4-component positions in the fixed-function pipeline, and the original
  93. position buffer must still be usable for fixed-function rendering.
  94. @par
  95. Note that we don't store any vertex declaration or vertex buffer binding here because this
  96. can be reused in the shadow algorithm.
  97. */
  98. HardwareVertexBufferPtr hardwareShadowVolWBuffer;
  99. /** Reorganises the data in the vertex buffers according to the
  100. new vertex declaration passed in. Note that new vertex buffers
  101. are created and written to, so if the buffers being referenced
  102. by this vertex data object are also used by others, then the
  103. original buffers will not be damaged by this operation.
  104. Once this operation has completed, the new declaration
  105. passed in will overwrite the current one.
  106. @param newDeclaration The vertex declaration which will be used
  107. for the reorganised buffer state. Note that the new declaration
  108. must not include any elements which do not already exist in the
  109. current declaration; you can drop elements by
  110. excluding them from the declaration if you wish, however.
  111. @param bufferUsages Vector of usage flags which indicate the usage options
  112. for each new vertex buffer created. The indexes of the entries must correspond
  113. to the buffer binding values referenced in the declaration.
  114. @param mgr Optional pointer to the manager to use to create new declarations
  115. and buffers etc. If not supplied, the HardwareBufferManager singleton will be used
  116. */
  117. void reorganiseBuffers(VertexDeclarationPtr newDeclaration, const BufferUsageList& bufferUsage,
  118. HardwareBufferManagerBase* mgr = 0);
  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. This version of the method derives the buffer usages from the existing
  127. buffers, by using the 'most flexible' usage from the equivalent sources.
  128. @param newDeclaration The vertex declaration which will be used
  129. for the reorganised buffer state. Note that the new delcaration
  130. must not include any elements which do not already exist in the
  131. current declaration; you can drop elements by
  132. excluding them from the declaration if you wish, however.
  133. @param mgr Optional pointer to the manager to use to create new declarations
  134. and buffers etc. If not supplied, the HardwareBufferManager singleton will be used
  135. */
  136. void reorganiseBuffers(VertexDeclarationPtr newDeclaration, HardwareBufferManagerBase* mgr = 0);
  137. /** Remove any gaps in the vertex buffer bindings.
  138. @remarks
  139. This is useful if you've removed elements and buffers from this vertex
  140. data and want to remove any gaps in the vertex buffer bindings. This
  141. method is mainly useful when reorganising vertex data manually.
  142. @note
  143. This will cause binding index of the elements in the vertex declaration
  144. to be altered to new binding index.
  145. */
  146. void closeGapsInBindings(void);
  147. /** Remove all vertex buffers that never used by the vertex declaration.
  148. @remarks
  149. This is useful if you've removed elements from the vertex declaration
  150. and want to unreference buffers that never used any more. This method
  151. is mainly useful when reorganising vertex data manually.
  152. @note
  153. This also remove any gaps in the vertex buffer bindings.
  154. */
  155. void removeUnusedBuffers(void);
  156. /** Convert all packed colour values (VET_COLOUR_*) in buffers used to
  157. another type.
  158. @param srcType The source colour type to assume if the ambiguous VET_COLOUR
  159. is encountered.
  160. @param destType The destination colour type, must be VET_COLOUR_ABGR or
  161. VET_COLOUR_ARGB.
  162. */
  163. void convertPackedColour(VertexElementType srcType, VertexElementType destType);
  164. };
  165. /** Summary class collecting together index data source information. */
  166. class CM_EXPORT IndexData
  167. {
  168. protected:
  169. /// Protected copy constructor, to prevent misuse
  170. IndexData(const IndexData& rhs); /* do nothing, should not use */
  171. /// Protected operator=, to prevent misuse
  172. IndexData& operator=(const IndexData& rhs); /* do not use */
  173. public:
  174. IndexData();
  175. ~IndexData();
  176. /// pointer to the HardwareIndexBuffer to use, must be specified if useIndexes = true
  177. HardwareIndexBufferPtr indexBuffer;
  178. /// index in the buffer to start from for this operation
  179. UINT32 indexStart;
  180. /// The number of indexes to use from the buffer
  181. UINT32 indexCount;
  182. /** Clones this index data, potentially including replicating the index buffer.
  183. @param copyData Whether to create new buffers too or just reference the existing ones
  184. @param mgr If supplied, the buffer manager through which copies should be made
  185. @remarks The caller is expected to delete the returned pointer when finished
  186. */
  187. IndexData* clone(bool copyData = true, HardwareBufferManagerBase* mgr = 0) const;
  188. /** Re-order the indexes in this index data structure to be more
  189. vertex cache friendly; that is to re-use the same vertices as close
  190. together as possible.
  191. @remarks
  192. Can only be used for index data which consists of triangle lists.
  193. It would in fact be pointless to use it on triangle strips or fans
  194. in any case.
  195. */
  196. void optimiseVertexCacheTriList(void);
  197. };
  198. /** Vertex cache profiler.
  199. @remarks
  200. Utility class for evaluating the effectiveness of the use of the vertex
  201. cache by a given index buffer.
  202. */
  203. class CM_EXPORT VertexCacheProfiler
  204. {
  205. public:
  206. enum CacheType {
  207. FIFO, LRU
  208. };
  209. VertexCacheProfiler(unsigned int cachesize = 16, CacheType cachetype = FIFO )
  210. : size ( cachesize ), type ( cachetype ), tail (0), buffersize (0), hit (0), miss (0)
  211. {
  212. cache = (UINT32*)malloc(sizeof(UINT32) * size);
  213. }
  214. ~VertexCacheProfiler()
  215. {
  216. free(cache);
  217. }
  218. void profile(const HardwareIndexBufferPtr& indexBuffer);
  219. void reset() { hit = 0; miss = 0; tail = 0; buffersize = 0; }
  220. void flush() { tail = 0; buffersize = 0; }
  221. unsigned int getHits() { return hit; }
  222. unsigned int getMisses() { return miss; }
  223. unsigned int getSize() { return size; }
  224. private:
  225. unsigned int size;
  226. UINT32 *cache;
  227. CacheType type;
  228. unsigned int tail, buffersize;
  229. unsigned int hit, miss;
  230. bool inCache(unsigned int index);
  231. };
  232. /** @} */
  233. /** @} */
  234. }
  235. #endif