2
0

CmHardwareBufferManager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 __HardwareBufferManager__
  25. #define __HardwareBufferManager__
  26. // Precompiler options
  27. #include "CmPrerequisites.h"
  28. #include "CmModule.h"
  29. #include "CmHardwareVertexBuffer.h"
  30. #include "CmHardwareIndexBuffer.h"
  31. namespace CamelotEngine {
  32. /** \addtogroup Core
  33. * @{
  34. */
  35. /** \addtogroup RenderSystem
  36. * @{
  37. */
  38. /** Base definition of a hardware buffer manager.
  39. @remarks
  40. This class is deliberately not a Singleton, so that multiple types can
  41. exist at once. The Singleton is wrapped via the Decorator pattern
  42. in HardwareBufferManager, below. Each concrete implementation should
  43. provide a subclass of HardwareBufferManagerBase, which does the actual
  44. work, and also a very simple subclass of HardwareBufferManager which
  45. simply constructs the instance of the HardwareBufferManagerBase subclass
  46. and passes it to the HardwareBufferManager superclass as a delegate.
  47. This subclass must also delete the implementation instance it creates.
  48. */
  49. class CM_EXPORT HardwareBufferManagerBase
  50. {
  51. protected:
  52. /** WARNING: The following two members should place before all other members.
  53. Members destruct order is very important here, because destructing other
  54. members will cause notify back to this class, and then will access to this
  55. two members.
  56. */
  57. typedef set<HardwareVertexBuffer*>::type VertexBufferList;
  58. typedef set<HardwareIndexBuffer*>::type IndexBufferList;
  59. VertexBufferList mVertexBuffers;
  60. IndexBufferList mIndexBuffers;
  61. typedef set<VertexBufferBinding*>::type VertexBufferBindingList;
  62. VertexBufferBindingList mVertexBufferBindings;
  63. // Mutexes
  64. CM_MUTEX(mVertexBuffersMutex)
  65. CM_MUTEX(mIndexBuffersMutex)
  66. CM_MUTEX(mVertexBufferBindingsMutex)
  67. /// Internal method for destroys all vertex buffer bindings
  68. virtual void destroyAllBindings(void);
  69. /// Internal method for creates a new vertex declaration, may be overridden by certain rendering APIs
  70. virtual VertexDeclarationPtr createVertexDeclarationImpl(void);
  71. /// Internal method for creates a new VertexBufferBinding, may be overridden by certain rendering APIs
  72. virtual VertexBufferBinding* createVertexBufferBindingImpl(void);
  73. /// Internal method for destroys a VertexBufferBinding, may be overridden by certain rendering APIs
  74. virtual void destroyVertexBufferBindingImpl(VertexBufferBinding* binding);
  75. protected:
  76. /// Creates a new buffer as a copy of the source, does not copy data
  77. virtual HardwareVertexBufferPtr makeBufferCopy(
  78. const HardwareVertexBufferPtr& source,
  79. HardwareBuffer::Usage usage, bool useShadowBuffer);
  80. public:
  81. HardwareBufferManagerBase();
  82. virtual ~HardwareBufferManagerBase();
  83. /** Create a hardware vertex buffer.
  84. @remarks
  85. This method creates a new vertex buffer; this will act as a source of geometry
  86. data for rendering objects. Note that because the meaning of the contents of
  87. the vertex buffer depends on the usage, this method does not specify a
  88. vertex format; the user of this buffer can actually insert whatever data
  89. they wish, in any format. However, in order to use this with a RenderOperation,
  90. the data in this vertex buffer will have to be associated with a semantic element
  91. of the rendering pipeline, e.g. a position, or texture coordinates. This is done
  92. using the VertexDeclaration class, which itself contains VertexElement structures
  93. referring to the source data.
  94. @remarks Note that because vertex buffers can be shared, they are reference
  95. counted so you do not need to worry about destroying themm this will be done
  96. automatically.
  97. @param vertexSize The size in bytes of each vertex in this buffer; you must calculate
  98. this based on the kind of data you expect to populate this buffer with.
  99. @param numVerts The number of vertices in this buffer.
  100. @param usage One or more members of the HardwareBuffer::Usage enumeration; you are
  101. strongly advised to use HBU_STATIC_WRITE_ONLY wherever possible, if you need to
  102. update regularly, consider HBU_DYNAMIC_WRITE_ONLY and useShadowBuffer=true.
  103. @param useShadowBuffer If set to true, this buffer will be 'shadowed' by one stored in
  104. system memory rather than GPU or AGP memory. You should set this flag if you intend
  105. to read data back from the vertex buffer, because reading data from a buffer
  106. in the GPU or AGP memory is very expensive, and is in fact impossible if you
  107. specify HBU_WRITE_ONLY for the main buffer. If you use this option, all
  108. reads and writes will be done to the shadow buffer, and the shadow buffer will
  109. be synchronised with the real buffer at an appropriate time.
  110. */
  111. virtual HardwareVertexBufferPtr
  112. createVertexBuffer(size_t vertexSize, size_t numVerts, HardwareBuffer::Usage usage,
  113. bool useShadowBuffer = false) = 0;
  114. /** Create a hardware index buffer.
  115. @remarks Note that because buffers can be shared, they are reference
  116. counted so you do not need to worry about destroying them this will be done
  117. automatically.
  118. @param itype The type in index, either 16- or 32-bit, depending on how many vertices
  119. you need to be able to address
  120. @param numIndexes The number of indexes in the buffer
  121. @param usage One or more members of the HardwareBuffer::Usage enumeration.
  122. @param useShadowBuffer If set to true, this buffer will be 'shadowed' by one stored in
  123. system memory rather than GPU or AGP memory. You should set this flag if you intend
  124. to read data back from the index buffer, because reading data from a buffer
  125. in the GPU or AGP memory is very expensive, and is in fact impossible if you
  126. specify HBU_WRITE_ONLY for the main buffer. If you use this option, all
  127. reads and writes will be done to the shadow buffer, and the shadow buffer will
  128. be synchronised with the real buffer at an appropriate time.
  129. */
  130. virtual HardwareIndexBufferPtr
  131. createIndexBuffer(HardwareIndexBuffer::IndexType itype, size_t numIndexes,
  132. HardwareBuffer::Usage usage, bool useShadowBuffer = false) = 0;
  133. /** Creates a new vertex declaration. */
  134. virtual VertexDeclarationPtr createVertexDeclaration(void);
  135. /** Creates a new VertexBufferBinding. */
  136. virtual VertexBufferBinding* createVertexBufferBinding(void);
  137. /** Destroys a VertexBufferBinding. */
  138. virtual void destroyVertexBufferBinding(VertexBufferBinding* binding);
  139. /** Allocates a copy of a given vertex buffer.
  140. @remarks
  141. This method allocates a temporary copy of an existing vertex buffer.
  142. This buffer is subsequently stored and can be made available for
  143. other purposes later without incurring the cost of construction /
  144. destruction.
  145. @param sourceBuffer The source buffer to use as a copy
  146. @param licenseType The type of license required on this buffer - automatic
  147. release causes this class to release licenses every frame so that
  148. they can be reallocated anew.
  149. @param licensee Pointer back to the class requesting the copy, which must
  150. implement HardwareBufferLicense in order to be notified when the license
  151. expires.
  152. @param copyData If true, the current data is copied as well as the
  153. structure of the buffer
  154. */
  155. virtual HardwareVertexBufferPtr allocateVertexBufferCopy(
  156. const HardwareVertexBufferPtr& sourceBuffer,
  157. bool copyData = false);
  158. /// Notification that a hardware vertex buffer has been destroyed
  159. void _notifyVertexBufferDestroyed(HardwareVertexBuffer* buf);
  160. /// Notification that a hardware index buffer has been destroyed
  161. void _notifyIndexBufferDestroyed(HardwareIndexBuffer* buf);
  162. };
  163. /** Singleton wrapper for hardware buffer manager. */
  164. class CM_EXPORT HardwareBufferManager : public HardwareBufferManagerBase, public Module<HardwareBufferManager>
  165. {
  166. protected:
  167. HardwareBufferManagerBase* mImpl;
  168. public:
  169. HardwareBufferManager(HardwareBufferManagerBase* imp);
  170. ~HardwareBufferManager();
  171. /** @copydoc HardwareBufferManagerInterface::createVertexBuffer */
  172. HardwareVertexBufferPtr
  173. createVertexBuffer(size_t vertexSize, size_t numVerts, HardwareBuffer::Usage usage,
  174. bool useShadowBuffer = false)
  175. {
  176. return mImpl->createVertexBuffer(vertexSize, numVerts, usage, useShadowBuffer);
  177. }
  178. /** @copydoc HardwareBufferManagerInterface::createIndexBuffer */
  179. HardwareIndexBufferPtr
  180. createIndexBuffer(HardwareIndexBuffer::IndexType itype, size_t numIndexes,
  181. HardwareBuffer::Usage usage, bool useShadowBuffer = false)
  182. {
  183. return mImpl->createIndexBuffer(itype, numIndexes, usage, useShadowBuffer);
  184. }
  185. /** @copydoc HardwareBufferManagerInterface::createVertexDeclaration */
  186. virtual VertexDeclarationPtr createVertexDeclaration(void)
  187. {
  188. return mImpl->createVertexDeclaration();
  189. }
  190. /** @copydoc HardwareBufferManagerInterface::createVertexBufferBinding */
  191. virtual VertexBufferBinding* createVertexBufferBinding(void)
  192. {
  193. return mImpl->createVertexBufferBinding();
  194. }
  195. /** @copydoc HardwareBufferManagerInterface::destroyVertexBufferBinding */
  196. virtual void destroyVertexBufferBinding(VertexBufferBinding* binding)
  197. {
  198. mImpl->destroyVertexBufferBinding(binding);
  199. }
  200. /** @copydoc HardwareBufferManagerInterface::allocateVertexBufferCopy */
  201. virtual HardwareVertexBufferPtr allocateVertexBufferCopy(
  202. const HardwareVertexBufferPtr& sourceBuffer,
  203. bool copyData = false)
  204. {
  205. return mImpl->allocateVertexBufferCopy(sourceBuffer, copyData);
  206. }
  207. /** @copydoc HardwareBufferManagerInterface::_notifyVertexBufferDestroyed */
  208. void _notifyVertexBufferDestroyed(HardwareVertexBuffer* buf)
  209. {
  210. mImpl->_notifyVertexBufferDestroyed(buf);
  211. }
  212. /** @copydoc HardwareBufferManagerInterface::_notifyIndexBufferDestroyed */
  213. void _notifyIndexBufferDestroyed(HardwareIndexBuffer* buf)
  214. {
  215. mImpl->_notifyIndexBufferDestroyed(buf);
  216. }
  217. };
  218. /** @} */
  219. /** @} */
  220. }
  221. #endif