CmHardwareBufferManager.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include "CmHardwareBufferManager.h"
  25. #include "CmVertexIndexData.h"
  26. namespace CamelotEngine {
  27. //---------------------------------------------------------------------
  28. HardwareBufferManager::HardwareBufferManager(HardwareBufferManagerBase* imp)
  29. : HardwareBufferManagerBase(), mImpl(imp)
  30. {
  31. }
  32. //---------------------------------------------------------------------
  33. HardwareBufferManager::~HardwareBufferManager()
  34. {
  35. // mImpl must be deleted by the creator
  36. }
  37. //-----------------------------------------------------------------------
  38. HardwareBufferManagerBase::HardwareBufferManagerBase()
  39. {
  40. }
  41. //-----------------------------------------------------------------------
  42. HardwareBufferManagerBase::~HardwareBufferManagerBase()
  43. {
  44. // Clear vertex/index buffer list first, avoid destroyed notify do
  45. // unnecessary work, and we'll destroy everything here.
  46. mVertexBuffers.clear();
  47. mIndexBuffers.clear();
  48. // Destroy everything
  49. destroyAllBindings();
  50. // No need to destroy main buffers - they will be destroyed by removal of bindings
  51. // No need to destroy temp buffers - they will be destroyed automatically.
  52. }
  53. //-----------------------------------------------------------------------
  54. VertexDeclarationPtr HardwareBufferManagerBase::createVertexDeclaration(void)
  55. {
  56. VertexDeclarationPtr decl = createVertexDeclarationImpl();
  57. return decl;
  58. }
  59. //-----------------------------------------------------------------------
  60. VertexBufferBinding* HardwareBufferManagerBase::createVertexBufferBinding(void)
  61. {
  62. VertexBufferBinding* ret = createVertexBufferBindingImpl();
  63. CM_LOCK_MUTEX(mVertexBufferBindingsMutex)
  64. mVertexBufferBindings.insert(ret);
  65. return ret;
  66. }
  67. //-----------------------------------------------------------------------
  68. void HardwareBufferManagerBase::destroyVertexBufferBinding(VertexBufferBinding* binding)
  69. {
  70. CM_LOCK_MUTEX(mVertexBufferBindingsMutex)
  71. mVertexBufferBindings.erase(binding);
  72. destroyVertexBufferBindingImpl(binding);
  73. }
  74. //-----------------------------------------------------------------------
  75. VertexDeclarationPtr HardwareBufferManagerBase::createVertexDeclarationImpl(void)
  76. {
  77. return VertexDeclarationPtr(new VertexDeclaration());
  78. }
  79. //-----------------------------------------------------------------------
  80. VertexBufferBinding* HardwareBufferManagerBase::createVertexBufferBindingImpl(void)
  81. {
  82. return new VertexBufferBinding();
  83. }
  84. //-----------------------------------------------------------------------
  85. void HardwareBufferManagerBase::destroyVertexBufferBindingImpl(VertexBufferBinding* binding)
  86. {
  87. delete binding;
  88. }
  89. //-----------------------------------------------------------------------
  90. void HardwareBufferManagerBase::destroyAllBindings(void)
  91. {
  92. CM_LOCK_MUTEX(mVertexBufferBindingsMutex)
  93. VertexBufferBindingList::iterator bind;
  94. for (bind = mVertexBufferBindings.begin(); bind != mVertexBufferBindings.end(); ++bind)
  95. {
  96. destroyVertexBufferBindingImpl(*bind);
  97. }
  98. mVertexBufferBindings.clear();
  99. }
  100. //-----------------------------------------------------------------------
  101. HardwareVertexBufferPtr
  102. HardwareBufferManagerBase::allocateVertexBufferCopy(
  103. const HardwareVertexBufferPtr& sourceBuffer,
  104. bool copyData)
  105. {
  106. // pre-lock the mVertexBuffers mutex, which would usually get locked in
  107. // makeBufferCopy / createVertexBuffer
  108. // this prevents a deadlock in _notifyVertexBufferDestroyed
  109. // which locks the same mutexes (via other methods) but in reverse order
  110. CM_LOCK_MUTEX(mVertexBuffersMutex)
  111. {
  112. CM_LOCK_MUTEX(mTempBuffersMutex)
  113. HardwareVertexBufferPtr vbuf;
  114. // Locate existing buffer copy in temporary vertex buffers
  115. // copy buffer, use shadow buffer and make dynamic
  116. vbuf = makeBufferCopy(
  117. sourceBuffer,
  118. HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE,
  119. true);
  120. // Copy data?
  121. if (copyData)
  122. {
  123. vbuf->copyData(*(sourceBuffer.get()), 0, 0, sourceBuffer->getSizeInBytes(), true);
  124. }
  125. return vbuf;
  126. }
  127. }
  128. //-----------------------------------------------------------------------
  129. void HardwareBufferManagerBase::_notifyVertexBufferDestroyed(HardwareVertexBuffer* buf)
  130. {
  131. CM_LOCK_MUTEX(mVertexBuffersMutex)
  132. VertexBufferList::iterator i = mVertexBuffers.find(buf);
  133. if (i != mVertexBuffers.end())
  134. {
  135. // release vertex buffer copies
  136. mVertexBuffers.erase(i);
  137. }
  138. }
  139. //-----------------------------------------------------------------------
  140. void HardwareBufferManagerBase::_notifyIndexBufferDestroyed(HardwareIndexBuffer* buf)
  141. {
  142. CM_LOCK_MUTEX(mIndexBuffersMutex)
  143. IndexBufferList::iterator i = mIndexBuffers.find(buf);
  144. if (i != mIndexBuffers.end())
  145. {
  146. mIndexBuffers.erase(i);
  147. }
  148. }
  149. //-----------------------------------------------------------------------
  150. HardwareVertexBufferPtr
  151. HardwareBufferManagerBase::makeBufferCopy(
  152. const HardwareVertexBufferPtr& source,
  153. HardwareBuffer::Usage usage, bool useShadowBuffer)
  154. {
  155. return this->createVertexBuffer(
  156. source->getVertexSize(),
  157. source->getNumVertices(),
  158. usage, useShadowBuffer);
  159. }
  160. }