CmDefaultHardwareBufferManager.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "CmDefaultHardwareBufferManager.h"
  25. namespace CamelotEngine {
  26. DefaultHardwareVertexBuffer::DefaultHardwareVertexBuffer(UINT32 vertexSize, UINT32 numVertices,
  27. HardwareBuffer::Usage usage)
  28. : HardwareVertexBuffer(0, vertexSize, numVertices, usage, true)
  29. {
  30. // Allocate aligned memory for better SIMD processing friendly.
  31. mpData = static_cast<unsigned char*>(_aligned_malloc(mSizeInBytes, CM_SIMD_ALIGNMENT));
  32. }
  33. //-----------------------------------------------------------------------
  34. DefaultHardwareVertexBuffer::DefaultHardwareVertexBuffer(HardwareBufferManagerBase* mgr, UINT32 vertexSize, UINT32 numVertices,
  35. HardwareBuffer::Usage usage)
  36. : HardwareVertexBuffer(mgr, vertexSize, numVertices, usage, true)
  37. {
  38. // Allocate aligned memory for better SIMD processing friendly.
  39. mpData = static_cast<unsigned char*>(_aligned_malloc(mSizeInBytes, CM_SIMD_ALIGNMENT));
  40. }
  41. //-----------------------------------------------------------------------
  42. DefaultHardwareVertexBuffer::~DefaultHardwareVertexBuffer()
  43. {
  44. _aligned_free(mpData);
  45. }
  46. //-----------------------------------------------------------------------
  47. void* DefaultHardwareVertexBuffer::lockImpl(UINT32 offset, UINT32 length, LockOptions options)
  48. {
  49. // Only for use internally, no 'locking' as such
  50. return mpData + offset;
  51. }
  52. //-----------------------------------------------------------------------
  53. void DefaultHardwareVertexBuffer::unlockImpl(void)
  54. {
  55. // Nothing to do
  56. }
  57. //-----------------------------------------------------------------------
  58. void* DefaultHardwareVertexBuffer::lock(UINT32 offset, UINT32 length, LockOptions options)
  59. {
  60. mIsLocked = true;
  61. return mpData + offset;
  62. }
  63. //-----------------------------------------------------------------------
  64. void DefaultHardwareVertexBuffer::unlock(void)
  65. {
  66. mIsLocked = false;
  67. // Nothing to do
  68. }
  69. //-----------------------------------------------------------------------
  70. void DefaultHardwareVertexBuffer::readData(UINT32 offset, UINT32 length, void* pDest)
  71. {
  72. assert((offset + length) <= mSizeInBytes);
  73. memcpy(pDest, mpData + offset, length);
  74. }
  75. //-----------------------------------------------------------------------
  76. void DefaultHardwareVertexBuffer::writeData(UINT32 offset, UINT32 length, const void* pSource,
  77. bool discardWholeBuffer)
  78. {
  79. assert((offset + length) <= mSizeInBytes);
  80. // ignore discard, memory is not guaranteed to be zeroised
  81. memcpy(mpData + offset, pSource, length);
  82. }
  83. //-----------------------------------------------------------------------
  84. DefaultHardwareIndexBuffer::DefaultHardwareIndexBuffer(IndexType idxType,
  85. UINT32 numIndexes, HardwareBuffer::Usage usage)
  86. : HardwareIndexBuffer(0, idxType, numIndexes, usage, true)
  87. {
  88. mpData = (unsigned char*)malloc(sizeof(unsigned char) * mSizeInBytes);
  89. }
  90. //-----------------------------------------------------------------------
  91. DefaultHardwareIndexBuffer::~DefaultHardwareIndexBuffer()
  92. {
  93. free(mpData);
  94. }
  95. //-----------------------------------------------------------------------
  96. void* DefaultHardwareIndexBuffer::lockImpl(UINT32 offset, UINT32 length, LockOptions options)
  97. {
  98. // Only for use internally, no 'locking' as such
  99. return mpData + offset;
  100. }
  101. //-----------------------------------------------------------------------
  102. void DefaultHardwareIndexBuffer::unlockImpl(void)
  103. {
  104. // Nothing to do
  105. }
  106. //-----------------------------------------------------------------------
  107. void* DefaultHardwareIndexBuffer::lock(UINT32 offset, UINT32 length, LockOptions options)
  108. {
  109. mIsLocked = true;
  110. return mpData + offset;
  111. }
  112. //-----------------------------------------------------------------------
  113. void DefaultHardwareIndexBuffer::unlock(void)
  114. {
  115. mIsLocked = false;
  116. // Nothing to do
  117. }
  118. //-----------------------------------------------------------------------
  119. void DefaultHardwareIndexBuffer::readData(UINT32 offset, UINT32 length, void* pDest)
  120. {
  121. assert((offset + length) <= mSizeInBytes);
  122. memcpy(pDest, mpData + offset, length);
  123. }
  124. //-----------------------------------------------------------------------
  125. void DefaultHardwareIndexBuffer::writeData(UINT32 offset, UINT32 length, const void* pSource,
  126. bool discardWholeBuffer)
  127. {
  128. assert((offset + length) <= mSizeInBytes);
  129. // ignore discard, memory is not guaranteed to be zeroised
  130. memcpy(mpData + offset, pSource, length);
  131. }
  132. //-----------------------------------------------------------------------
  133. DefaultHardwareConstantBuffer::DefaultHardwareConstantBuffer(HardwareBufferManagerBase* mgr, UINT32 sizeBytes, HardwareBuffer::Usage usage)
  134. : HardwareConstantBuffer(mgr, sizeBytes, usage, true)
  135. {
  136. // Allocate aligned memory for better SIMD processing friendly.
  137. mpData = static_cast<unsigned char*>(_aligned_malloc(mSizeInBytes, CM_SIMD_ALIGNMENT));
  138. }
  139. //-----------------------------------------------------------------------
  140. DefaultHardwareConstantBuffer::~DefaultHardwareConstantBuffer()
  141. {
  142. _aligned_free(mpData);
  143. }
  144. //-----------------------------------------------------------------------
  145. void* DefaultHardwareConstantBuffer::lockImpl(UINT32 offset, UINT32 length, LockOptions options)
  146. {
  147. // Only for use internally, no 'locking' as such
  148. return mpData + offset;
  149. }
  150. //-----------------------------------------------------------------------
  151. void DefaultHardwareConstantBuffer::unlockImpl(void)
  152. {
  153. // Nothing to do
  154. }
  155. //-----------------------------------------------------------------------
  156. void* DefaultHardwareConstantBuffer::lock(UINT32 offset, UINT32 length, LockOptions options)
  157. {
  158. mIsLocked = true;
  159. return mpData + offset;
  160. }
  161. //-----------------------------------------------------------------------
  162. void DefaultHardwareConstantBuffer::unlock(void)
  163. {
  164. mIsLocked = false;
  165. // Nothing to do
  166. }
  167. //-----------------------------------------------------------------------
  168. void DefaultHardwareConstantBuffer::readData(UINT32 offset, UINT32 length, void* pDest)
  169. {
  170. assert((offset + length) <= mSizeInBytes);
  171. memcpy(pDest, mpData + offset, length);
  172. }
  173. //-----------------------------------------------------------------------
  174. void DefaultHardwareConstantBuffer::writeData(UINT32 offset, UINT32 length, const void* pSource,
  175. bool discardWholeBuffer)
  176. {
  177. assert((offset + length) <= mSizeInBytes);
  178. // ignore discard, memory is not guaranteed to be zeroised
  179. memcpy(mpData + offset, pSource, length);
  180. }
  181. //-----------------------------------------------------------------------
  182. DefaultHardwareBufferManagerBase::DefaultHardwareBufferManagerBase()
  183. {
  184. }
  185. //-----------------------------------------------------------------------
  186. DefaultHardwareBufferManagerBase::~DefaultHardwareBufferManagerBase()
  187. {
  188. destroyAllBindings();
  189. }
  190. //-----------------------------------------------------------------------
  191. HardwareVertexBufferPtr
  192. DefaultHardwareBufferManagerBase::createVertexBuffer(UINT32 vertexSize,
  193. UINT32 numVerts, HardwareBuffer::Usage usage, bool streamOut)
  194. {
  195. DefaultHardwareVertexBuffer* vb = new DefaultHardwareVertexBuffer(this, vertexSize, numVerts, usage);
  196. return HardwareVertexBufferPtr(vb);
  197. }
  198. //-----------------------------------------------------------------------
  199. HardwareIndexBufferPtr
  200. DefaultHardwareBufferManagerBase::createIndexBuffer(HardwareIndexBuffer::IndexType itype,
  201. UINT32 numIndexes, HardwareBuffer::Usage usage)
  202. {
  203. DefaultHardwareIndexBuffer* ib = new DefaultHardwareIndexBuffer(itype, numIndexes, usage);
  204. return HardwareIndexBufferPtr(ib);
  205. }
  206. //-----------------------------------------------------------------------
  207. GpuParamBlockPtr DefaultHardwareBufferManagerBase::createGpuParamBlock(const GpuParamBlockDesc& paramDesc)
  208. {
  209. return GpuParamBlockPtr(new GpuParamBlock(paramDesc));
  210. }
  211. }