CmHardwareBufferManager.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 three 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. typedef set<HardwareConstantBuffer*>::type ConstantBufferList;
  60. VertexBufferList mVertexBuffers;
  61. IndexBufferList mIndexBuffers;
  62. ConstantBufferList mConstantBuffers;
  63. typedef set<VertexBufferBinding*>::type VertexBufferBindingList;
  64. VertexBufferBindingList mVertexBufferBindings;
  65. /// Internal method for destroys all vertex buffer bindings
  66. virtual void destroyAllBindings(void);
  67. /// Internal method for creates a new vertex declaration, may be overridden by certain rendering APIs
  68. virtual VertexDeclarationPtr createVertexDeclarationImpl(void);
  69. /// Internal method for creates a new VertexBufferBinding, may be overridden by certain rendering APIs
  70. virtual VertexBufferBinding* createVertexBufferBindingImpl(void);
  71. /// Internal method for destroys a VertexBufferBinding, may be overridden by certain rendering APIs
  72. virtual void destroyVertexBufferBindingImpl(VertexBufferBinding* binding);
  73. public:
  74. HardwareBufferManagerBase();
  75. virtual ~HardwareBufferManagerBase();
  76. /** Create a hardware vertex buffer.
  77. @remarks
  78. This method creates a new vertex buffer; this will act as a source of geometry
  79. data for rendering objects. Note that because the meaning of the contents of
  80. the vertex buffer depends on the usage, this method does not specify a
  81. vertex format; the user of this buffer can actually insert whatever data
  82. they wish, in any format. However, in order to use this with a RenderOperation,
  83. the data in this vertex buffer will have to be associated with a semantic element
  84. of the rendering pipeline, e.g. a position, or texture coordinates. This is done
  85. using the VertexDeclaration class, which itself contains VertexElement structures
  86. referring to the source data.
  87. @remarks Note that because vertex buffers can be shared, they are reference
  88. counted so you do not need to worry about destroying themm this will be done
  89. automatically.
  90. @param vertexSize The size in bytes of each vertex in this buffer; you must calculate
  91. this based on the kind of data you expect to populate this buffer with.
  92. @param numVerts The number of vertices in this buffer.
  93. @param usage One or more members of the HardwareBuffer::Usage enumeration; you are
  94. strongly advised to use HBU_STATIC_WRITE_ONLY wherever possible, if you need to
  95. update regularly, consider HBU_DYNAMIC_WRITE_ONLY and useShadowBuffer=true.
  96. @param streamOut Whether the vertex buffer will be used for steam out operations of the
  97. geometry shader.
  98. */
  99. virtual HardwareVertexBufferPtr
  100. createVertexBuffer(UINT32 vertexSize, UINT32 numVerts, HardwareBuffer::Usage usage, bool streamOut = false) = 0;
  101. /** Create a hardware index buffer.
  102. @remarks Note that because buffers can be shared, they are reference
  103. counted so you do not need to worry about destroying them this will be done
  104. automatically.
  105. @param itype The type in index, either 16- or 32-bit, depending on how many vertices
  106. you need to be able to address
  107. @param numIndexes The number of indexes in the buffer
  108. @param usage One or more members of the HardwareBuffer::Usage enumeration.
  109. */
  110. virtual HardwareIndexBufferPtr
  111. createIndexBuffer(HardwareIndexBuffer::IndexType itype, UINT32 numIndexes,
  112. HardwareBuffer::Usage usage) = 0;
  113. /** Create a hardware constant buffer.
  114. @param sizeBytes Size of the buffer, in bytes.
  115. @param usage One or more members of the HardwareBuffer::Usage enumeration.
  116. */
  117. virtual HardwareConstantBufferPtr
  118. createConstantBuffer(UINT32 sizeBytes, HardwareBuffer::Usage usage) = 0;
  119. /** Creates a new vertex declaration. */
  120. virtual VertexDeclarationPtr createVertexDeclaration(void);
  121. /** Creates a new VertexBufferBinding. */
  122. virtual VertexBufferBinding* createVertexBufferBinding(void);
  123. /** Destroys a VertexBufferBinding. */
  124. virtual void destroyVertexBufferBinding(VertexBufferBinding* binding);
  125. /// Notification that a hardware vertex buffer has been destroyed
  126. void _notifyVertexBufferDestroyed(HardwareVertexBuffer* buf);
  127. /// Notification that a hardware index buffer has been destroyed
  128. void _notifyIndexBufferDestroyed(HardwareIndexBuffer* buf);
  129. /// Notification that a hardware constant buffer has been destroyed
  130. void _notifyConstantBufferDestroyed(HardwareConstantBuffer* buf);
  131. };
  132. /** Singleton wrapper for hardware buffer manager. */
  133. class CM_EXPORT HardwareBufferManager : public HardwareBufferManagerBase, public Module<HardwareBufferManager>
  134. {
  135. protected:
  136. HardwareBufferManagerBase* mImpl;
  137. public:
  138. HardwareBufferManager(HardwareBufferManagerBase* imp);
  139. ~HardwareBufferManager();
  140. /** @copydoc HardwareBufferManagerInterface::createVertexBuffer */
  141. HardwareVertexBufferPtr createVertexBuffer(UINT32 vertexSize, UINT32 numVerts, HardwareBuffer::Usage usage, bool streamOut = false)
  142. {
  143. return mImpl->createVertexBuffer(vertexSize, numVerts, usage, streamOut);
  144. }
  145. /** @copydoc HardwareBufferManagerInterface::createIndexBuffer */
  146. HardwareIndexBufferPtr createIndexBuffer(HardwareIndexBuffer::IndexType itype, UINT32 numIndexes, HardwareBuffer::Usage usage)
  147. {
  148. return mImpl->createIndexBuffer(itype, numIndexes, usage);
  149. }
  150. /** @copydoc HardwareBufferManagerInterface::createConstantBuffer */
  151. HardwareConstantBufferPtr createConstantBuffer(UINT32 sizeBytes, HardwareBuffer::Usage usage)
  152. {
  153. return mImpl->createConstantBuffer(sizeBytes, usage);
  154. }
  155. /** @copydoc HardwareBufferManagerInterface::createVertexDeclaration */
  156. virtual VertexDeclarationPtr createVertexDeclaration(void)
  157. {
  158. return mImpl->createVertexDeclaration();
  159. }
  160. /** @copydoc HardwareBufferManagerInterface::createVertexBufferBinding */
  161. virtual VertexBufferBinding* createVertexBufferBinding(void)
  162. {
  163. return mImpl->createVertexBufferBinding();
  164. }
  165. /** @copydoc HardwareBufferManagerInterface::destroyVertexBufferBinding */
  166. virtual void destroyVertexBufferBinding(VertexBufferBinding* binding)
  167. {
  168. mImpl->destroyVertexBufferBinding(binding);
  169. }
  170. /** @copydoc HardwareBufferManagerInterface::_notifyVertexBufferDestroyed */
  171. void _notifyVertexBufferDestroyed(HardwareVertexBuffer* buf)
  172. {
  173. mImpl->_notifyVertexBufferDestroyed(buf);
  174. }
  175. /** @copydoc HardwareBufferManagerInterface::_notifyIndexBufferDestroyed */
  176. void _notifyIndexBufferDestroyed(HardwareIndexBuffer* buf)
  177. {
  178. mImpl->_notifyIndexBufferDestroyed(buf);
  179. }
  180. };
  181. /** @} */
  182. /** @} */
  183. }
  184. #endif