CmHardwareVertexBuffer.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 __HardwareVertexBuffer__
  25. #define __HardwareVertexBuffer__
  26. // Precompiler options
  27. #include "CmPrerequisites.h"
  28. #include "CmHardwareBuffer.h"
  29. #include "CmColor.h"
  30. namespace CamelotEngine {
  31. class HardwareBufferManagerBase;
  32. /** \addtogroup Core
  33. * @{
  34. */
  35. /** \addtogroup RenderSystem
  36. * @{
  37. */
  38. /** Specialisation of HardwareBuffer for a vertex buffer. */
  39. class CM_EXPORT HardwareVertexBuffer : public HardwareBuffer
  40. {
  41. protected:
  42. HardwareBufferManagerBase* mMgr;
  43. size_t mNumVertices;
  44. size_t mVertexSize;
  45. public:
  46. /// Should be called by HardwareBufferManager
  47. HardwareVertexBuffer(HardwareBufferManagerBase* mgr, size_t vertexSize, size_t numVertices,
  48. HardwareBuffer::Usage usage, bool useSystemMemory, bool useShadowBuffer);
  49. ~HardwareVertexBuffer();
  50. /// Return the manager of this buffer, if any
  51. HardwareBufferManagerBase* getManager() const { return mMgr; }
  52. /// Gets the size in bytes of a single vertex in this buffer
  53. size_t getVertexSize(void) const { return mVertexSize; }
  54. /// Get the number of vertices in this buffer
  55. size_t getNumVertices(void) const { return mNumVertices; }
  56. // NB subclasses should override lock, unlock, readData, writeData
  57. };
  58. typedef std::shared_ptr<HardwareVertexBuffer> HardwareVertexBufferPtr;
  59. /// Vertex element semantics, used to identify the meaning of vertex buffer contents
  60. enum VertexElementSemantic {
  61. /// Position, 3 reals per vertex
  62. VES_POSITION = 1,
  63. /// Blending weights
  64. VES_BLEND_WEIGHTS = 2,
  65. /// Blending indices
  66. VES_BLEND_INDICES = 3,
  67. /// Normal, 3 reals per vertex
  68. VES_NORMAL = 4,
  69. /// Diffuse colours
  70. VES_DIFFUSE = 5,
  71. /// Specular colours
  72. VES_SPECULAR = 6,
  73. /// Texture coordinates
  74. VES_TEXTURE_COORDINATES = 7,
  75. /// Binormal (Y axis if normal is Z)
  76. VES_BINORMAL = 8,
  77. /// Tangent (X axis if normal is Z)
  78. VES_TANGENT = 9
  79. };
  80. /// Vertex element type, used to identify the base types of the vertex contents
  81. enum VertexElementType
  82. {
  83. VET_FLOAT1 = 0,
  84. VET_FLOAT2 = 1,
  85. VET_FLOAT3 = 2,
  86. VET_FLOAT4 = 3,
  87. /// alias to more specific colour type - use the current rendersystem's colour packing
  88. VET_COLOUR = 4,
  89. VET_SHORT1 = 5,
  90. VET_SHORT2 = 6,
  91. VET_SHORT3 = 7,
  92. VET_SHORT4 = 8,
  93. VET_UBYTE4 = 9,
  94. /// D3D style compact colour
  95. VET_COLOUR_ARGB = 10,
  96. /// GL style compact colour
  97. VET_COLOUR_ABGR = 11
  98. };
  99. /** This class declares the usage of a single vertex buffer as a component
  100. of a complete VertexDeclaration.
  101. @remarks
  102. Several vertex buffers can be used to supply the input geometry for a
  103. rendering operation, and in each case a vertex buffer can be used in
  104. different ways for different operations; the buffer itself does not
  105. define the semantics (position, normal etc), the VertexElement
  106. class does.
  107. */
  108. class CM_EXPORT VertexElement
  109. {
  110. protected:
  111. /// The source vertex buffer, as bound to an index using VertexBufferBinding
  112. unsigned short mSource;
  113. /// The offset in the buffer that this element starts at
  114. size_t mOffset;
  115. /// The type of element
  116. VertexElementType mType;
  117. /// The meaning of the element
  118. VertexElementSemantic mSemantic;
  119. /// Index of the item, only applicable for some elements like texture coords
  120. unsigned short mIndex;
  121. public:
  122. /// Constructor, should not be called directly, only needed because of list
  123. VertexElement() {}
  124. /// Constructor, should not be called directly, call VertexDeclaration::addElement
  125. VertexElement(unsigned short source, size_t offset, VertexElementType theType,
  126. VertexElementSemantic semantic, unsigned short index = 0);
  127. /// Gets the vertex buffer index from where this element draws it's values
  128. unsigned short getSource(void) const { return mSource; }
  129. /// Gets the offset into the buffer where this element starts
  130. size_t getOffset(void) const { return mOffset; }
  131. /// Gets the data format of this element
  132. VertexElementType getType(void) const { return mType; }
  133. /// Gets the meaning of this element
  134. VertexElementSemantic getSemantic(void) const { return mSemantic; }
  135. /// Gets the index of this element, only applicable for repeating elements
  136. unsigned short getIndex(void) const { return mIndex; }
  137. /// Gets the size of this element in bytes
  138. size_t getSize(void) const;
  139. /// Utility method for helping to calculate offsets
  140. static size_t getTypeSize(VertexElementType etype);
  141. /// Utility method which returns the count of values in a given type
  142. static unsigned short getTypeCount(VertexElementType etype);
  143. /** Simple converter function which will turn a single-value type into a
  144. multi-value type based on a parameter.
  145. */
  146. static VertexElementType multiplyTypeCount(VertexElementType baseType, unsigned short count);
  147. /** Simple converter function which will a type into it's single-value
  148. equivalent - makes switches on type easier.
  149. */
  150. static VertexElementType getBaseType(VertexElementType multiType);
  151. /** Utility method for converting colour from
  152. one packed 32-bit colour type to another.
  153. @param srcType The source type
  154. @param dstType The destination type
  155. @param ptr Read / write value to change
  156. */
  157. static void convertColourValue(VertexElementType srcType,
  158. VertexElementType dstType, UINT32* ptr);
  159. /** Utility method for converting colour to
  160. a packed 32-bit colour type.
  161. @param src source colour
  162. @param dst The destination type
  163. */
  164. static UINT32 convertColourValue(const Color& src,
  165. VertexElementType dst);
  166. /** Utility method to get the most appropriate packed colour vertex element format. */
  167. static VertexElementType getBestColourVertexElementType(void);
  168. inline bool operator== (const VertexElement& rhs) const
  169. {
  170. if (mType != rhs.mType ||
  171. mIndex != rhs.mIndex ||
  172. mOffset != rhs.mOffset ||
  173. mSemantic != rhs.mSemantic ||
  174. mSource != rhs.mSource)
  175. return false;
  176. else
  177. return true;
  178. }
  179. /** Adjusts a pointer to the base of a vertex to point at this element.
  180. @remarks
  181. This variant is for void pointers, passed as a parameter because we can't
  182. rely on covariant return types.
  183. @param pBase Pointer to the start of a vertex in this buffer.
  184. @param pElem Pointer to a pointer which will be set to the start of this element.
  185. */
  186. inline void baseVertexPointerToElement(void* pBase, void** pElem) const
  187. {
  188. // The only way we can do this is to cast to char* in order to use byte offset
  189. // then cast back to void*.
  190. *pElem = static_cast<void*>(
  191. static_cast<unsigned char*>(pBase) + mOffset);
  192. }
  193. /** Adjusts a pointer to the base of a vertex to point at this element.
  194. @remarks
  195. This variant is for float pointers, passed as a parameter because we can't
  196. rely on covariant return types.
  197. @param pBase Pointer to the start of a vertex in this buffer.
  198. @param pElem Pointer to a pointer which will be set to the start of this element.
  199. */
  200. inline void baseVertexPointerToElement(void* pBase, float** pElem) const
  201. {
  202. // The only way we can do this is to cast to char* in order to use byte offset
  203. // then cast back to float*. However we have to go via void* because casting
  204. // directly is not allowed
  205. *pElem = static_cast<float*>(
  206. static_cast<void*>(
  207. static_cast<unsigned char*>(pBase) + mOffset));
  208. }
  209. /** Adjusts a pointer to the base of a vertex to point at this element.
  210. @remarks
  211. This variant is for RGBA pointers, passed as a parameter because we can't
  212. rely on covariant return types.
  213. @param pBase Pointer to the start of a vertex in this buffer.
  214. @param pElem Pointer to a pointer which will be set to the start of this element.
  215. */
  216. inline void baseVertexPointerToElement(void* pBase, RGBA** pElem) const
  217. {
  218. *pElem = static_cast<RGBA*>(
  219. static_cast<void*>(
  220. static_cast<unsigned char*>(pBase) + mOffset));
  221. }
  222. /** Adjusts a pointer to the base of a vertex to point at this element.
  223. @remarks
  224. This variant is for char pointers, passed as a parameter because we can't
  225. rely on covariant return types.
  226. @param pBase Pointer to the start of a vertex in this buffer.
  227. @param pElem Pointer to a pointer which will be set to the start of this element.
  228. */
  229. inline void baseVertexPointerToElement(void* pBase, unsigned char** pElem) const
  230. {
  231. *pElem = static_cast<unsigned char*>(pBase) + mOffset;
  232. }
  233. /** Adjusts a pointer to the base of a vertex to point at this element.
  234. @remarks
  235. This variant is for UINT16 pointers, passed as a parameter because we can't
  236. rely on covariant return types.
  237. @param pBase Pointer to the start of a vertex in this buffer.
  238. @param pElem Pointer to a pointer which will be set to the start of this element.
  239. */
  240. inline void baseVertexPointerToElement(void* pBase, unsigned short** pElem) const
  241. {
  242. *pElem = static_cast<unsigned short*>(
  243. static_cast<void*>(
  244. static_cast<unsigned char*>(pBase) + mOffset));
  245. }
  246. };
  247. /** This class declares the format of a set of vertex inputs, which
  248. can be issued to the rendering API through a RenderOperation.
  249. @remarks
  250. You should be aware that the ordering and structure of the
  251. VertexDeclaration can be very important on DirectX with older
  252. cards,so if you want to maintain maximum compatibility with
  253. all render systems and all cards you should be careful to follow these
  254. rules:<ol>
  255. <li>VertexElements should be added in the following order, and the order of the
  256. elements within a shared buffer should be as follows:
  257. position, blending weights, normals, diffuse colours, specular colours,
  258. texture coordinates (in order, with no gaps)</li>
  259. <li>You must not have unused gaps in your buffers which are not referenced
  260. by any VertexElement</li>
  261. <li>You must not cause the buffer & offset settings of 2 VertexElements to overlap</li>
  262. </ol>
  263. Whilst GL and more modern graphics cards in D3D will allow you to defy these rules,
  264. sticking to them will ensure that your buffers have the maximum compatibility.
  265. @par
  266. Like the other classes in this functional area, these declarations should be created and
  267. destroyed using the HardwareBufferManager.
  268. */
  269. class CM_EXPORT VertexDeclaration
  270. {
  271. public:
  272. /// Defines the list of vertex elements that makes up this declaration
  273. typedef list<VertexElement>::type VertexElementList;
  274. /// Sort routine for vertex elements
  275. static bool vertexElementLess(const VertexElement& e1, const VertexElement& e2);
  276. protected:
  277. VertexElementList mElementList;
  278. public:
  279. /// Standard constructor, not you should use HardwareBufferManager::createVertexDeclaration
  280. VertexDeclaration();
  281. virtual ~VertexDeclaration();
  282. /** Get the number of elements in the declaration. */
  283. size_t getElementCount(void) { return mElementList.size(); }
  284. /** Gets read-only access to the list of vertex elements. */
  285. const VertexElementList& getElements(void) const;
  286. /** Get a single element. */
  287. const VertexElement* getElement(unsigned short index);
  288. /** Sorts the elements in this list to be compatible with the maximum
  289. number of rendering APIs / graphics cards.
  290. @remarks
  291. Older graphics cards require vertex data to be presented in a more
  292. rigid way, as defined in the main documentation for this class. As well
  293. as the ordering being important, where shared source buffers are used, the
  294. declaration must list all the elements for each source in turn.
  295. */
  296. void sort(void);
  297. /** Remove any gaps in the source buffer list used by this declaration.
  298. @remarks
  299. This is useful if you've modified a declaration and want to remove
  300. any gaps in the list of buffers being used. Note, however, that if this
  301. declaration is already being used with a VertexBufferBinding, you will
  302. need to alter that too. This method is mainly useful when reorganising
  303. buffers based on an altered declaration.
  304. @note
  305. This will cause the vertex declaration to be re-sorted.
  306. */
  307. void closeGapsInSource(void);
  308. /** Gets the index of the highest source value referenced by this declaration. */
  309. unsigned short getMaxSource(void) const;
  310. /** Adds a new VertexElement to this declaration.
  311. @remarks
  312. This method adds a single element (positions, normals etc) to the end of the
  313. vertex declaration. <b>Please read the information in VertexDeclaration about
  314. the importance of ordering and structure for compatibility with older D3D drivers</b>.
  315. @param source The binding index of HardwareVertexBuffer which will provide the source for this element.
  316. See VertexBufferBindingState for full information.
  317. @param offset The offset in bytes where this element is located in the buffer
  318. @param theType The data format of the element (3 floats, a colour etc)
  319. @param semantic The meaning of the data (position, normal, diffuse colour etc)
  320. @param index Optional index for multi-input elements like texture coordinates
  321. @returns A reference to the VertexElement added.
  322. */
  323. virtual const VertexElement& addElement(unsigned short source, size_t offset, VertexElementType theType,
  324. VertexElementSemantic semantic, unsigned short index = 0);
  325. /** Inserts a new VertexElement at a given position in this declaration.
  326. @remarks
  327. This method adds a single element (positions, normals etc) at a given position in this
  328. vertex declaration. <b>Please read the information in VertexDeclaration about
  329. the importance of ordering and structure for compatibility with older D3D drivers</b>.
  330. @param source The binding index of HardwareVertexBuffer which will provide the source for this element.
  331. See VertexBufferBindingState for full information.
  332. @param offset The offset in bytes where this element is located in the buffer
  333. @param theType The data format of the element (3 floats, a colour etc)
  334. @param semantic The meaning of the data (position, normal, diffuse colour etc)
  335. @param index Optional index for multi-input elements like texture coordinates
  336. @returns A reference to the VertexElement added.
  337. */
  338. virtual const VertexElement& insertElement(unsigned short atPosition,
  339. unsigned short source, size_t offset, VertexElementType theType,
  340. VertexElementSemantic semantic, unsigned short index = 0);
  341. /** Remove the element at the given index from this declaration. */
  342. virtual void removeElement(unsigned short elem_index);
  343. /** Remove the element with the given semantic and usage index.
  344. @remarks
  345. In this case 'index' means the usage index for repeating elements such
  346. as texture coordinates. For other elements this will always be 0 and does
  347. not refer to the index in the vector.
  348. */
  349. virtual void removeElement(VertexElementSemantic semantic, unsigned short index = 0);
  350. /** Remove all elements. */
  351. virtual void removeAllElements(void);
  352. /** Modify an element in-place, params as addElement.
  353. @remarks
  354. <b>Please read the information in VertexDeclaration about
  355. the importance of ordering and structure for compatibility with older D3D drivers</b>.
  356. */
  357. virtual void modifyElement(unsigned short elem_index, unsigned short source, size_t offset, VertexElementType theType,
  358. VertexElementSemantic semantic, unsigned short index = 0);
  359. /** Finds a VertexElement with the given semantic, and index if there is more than
  360. one element with the same semantic.
  361. @remarks
  362. If the element is not found, this method returns null.
  363. */
  364. virtual const VertexElement* findElementBySemantic(VertexElementSemantic sem, unsigned short index = 0);
  365. /** Based on the current elements, gets the size of the vertex for a given buffer source.
  366. @param source The buffer binding index for which to get the vertex size.
  367. */
  368. /** Gets a list of elements which use a given source.
  369. @remarks
  370. Note that the list of elements is returned by value therefore is separate from
  371. the declaration as soon as this method returns.
  372. */
  373. virtual VertexElementList findElementsBySource(unsigned short source);
  374. /** Gets the vertex size defined by this declaration for a given source. */
  375. virtual size_t getVertexSize(unsigned short source);
  376. /** Clones this declaration.
  377. @param mgr Optional HardwareBufferManager to use for creating the clone
  378. (if null, use the current default).
  379. */
  380. virtual VertexDeclaration* clone(HardwareBufferManagerBase* mgr = 0);
  381. inline bool operator== (const VertexDeclaration& rhs) const
  382. {
  383. if (mElementList.size() != rhs.mElementList.size())
  384. return false;
  385. VertexElementList::const_iterator i, iend, rhsi, rhsiend;
  386. iend = mElementList.end();
  387. rhsiend = rhs.mElementList.end();
  388. rhsi = rhs.mElementList.begin();
  389. for (i = mElementList.begin(); i != iend && rhsi != rhsiend; ++i, ++rhsi)
  390. {
  391. if ( !(*i == *rhsi) )
  392. return false;
  393. }
  394. return true;
  395. }
  396. inline bool operator!= (const VertexDeclaration& rhs) const
  397. {
  398. return !(*this == rhs);
  399. }
  400. };
  401. /** Records the state of all the vertex buffer bindings required to provide a vertex declaration
  402. with the input data it needs for the vertex elements.
  403. @remarks
  404. Why do we have this binding list rather than just have VertexElement referring to the
  405. vertex buffers direct? Well, in the underlying APIs, binding the vertex buffers to an
  406. index (or 'stream') is the way that vertex data is linked, so this structure better
  407. reflects the realities of that. In addition, by separating the vertex declaration from
  408. the list of vertex buffer bindings, it becomes possible to reuse bindings between declarations
  409. and vice versa, giving opportunities to reduce the state changes required to perform rendering.
  410. @par
  411. Like the other classes in this functional area, these binding maps should be created and
  412. destroyed using the HardwareBufferManager.
  413. */
  414. class CM_EXPORT VertexBufferBinding
  415. {
  416. public:
  417. /// Defines the vertex buffer bindings used as source for vertex declarations
  418. typedef map<unsigned short, HardwareVertexBufferPtr>::type VertexBufferBindingMap;
  419. protected:
  420. VertexBufferBindingMap mBindingMap;
  421. mutable unsigned short mHighIndex;
  422. public:
  423. /// Constructor, should not be called direct, use HardwareBufferManager::createVertexBufferBinding
  424. VertexBufferBinding();
  425. virtual ~VertexBufferBinding();
  426. /** Set a binding, associating a vertex buffer with a given index.
  427. @remarks
  428. If the index is already associated with a vertex buffer,
  429. the association will be replaced. This may cause the old buffer
  430. to be destroyed if nothing else is referring to it.
  431. You should assign bindings from 0 and not leave gaps, although you can
  432. bind them in any order.
  433. */
  434. virtual void setBinding(unsigned short index, const HardwareVertexBufferPtr& buffer);
  435. /** Removes an existing binding. */
  436. virtual void unsetBinding(unsigned short index);
  437. /** Removes all the bindings. */
  438. virtual void unsetAllBindings(void);
  439. /// Gets a read-only version of the buffer bindings
  440. virtual const VertexBufferBindingMap& getBindings(void) const;
  441. /// Gets the buffer bound to the given source index
  442. virtual const HardwareVertexBufferPtr& getBuffer(unsigned short index) const;
  443. /// Gets whether a buffer is bound to the given source index
  444. virtual bool isBufferBound(unsigned short index) const;
  445. virtual size_t getBufferCount(void) const { return mBindingMap.size(); }
  446. /** Gets the highest index which has already been set, plus 1.
  447. @remarks
  448. This is to assist in binding the vertex buffers such that there are
  449. not gaps in the list.
  450. */
  451. virtual unsigned short getNextIndex(void) const { return mHighIndex++; }
  452. /** Gets the last bound index.
  453. */
  454. virtual unsigned short getLastBoundIndex(void) const;
  455. typedef map<UINT16, UINT16>::type BindingIndexMap;
  456. /** Check whether any gaps in the bindings.
  457. */
  458. virtual bool hasGaps(void) const;
  459. /** Remove any gaps in the bindings.
  460. @remarks
  461. This is useful if you've removed vertex buffer from this vertex buffer
  462. bindings and want to remove any gaps in the bindings. Note, however,
  463. that if this bindings is already being used with a VertexDeclaration,
  464. you will need to alter that too. This method is mainly useful when
  465. reorganising buffers manually.
  466. @param
  467. bindingIndexMap To be retrieve the binding index map that used to
  468. translation old index to new index; will be cleared by this method
  469. before fill-in.
  470. */
  471. virtual void closeGaps(BindingIndexMap& bindingIndexMap);
  472. };
  473. /** @} */
  474. /** @} */
  475. }
  476. #endif