CmD3D9HardwareBufferManager.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "CmD3D9HardwareBufferManager.h"
  25. #include "CmD3D9HardwareVertexBuffer.h"
  26. #include "CmD3D9HardwareIndexBuffer.h"
  27. #include "CmD3D9VertexDeclaration.h"
  28. #include "CmException.h"
  29. namespace CamelotEngine {
  30. //-----------------------------------------------------------------------
  31. D3D9HardwareBufferManagerBase::D3D9HardwareBufferManagerBase()
  32. {
  33. }
  34. //-----------------------------------------------------------------------
  35. D3D9HardwareBufferManagerBase::~D3D9HardwareBufferManagerBase()
  36. {
  37. destroyAllBindings();
  38. }
  39. //-----------------------------------------------------------------------
  40. HardwareVertexBufferPtr
  41. D3D9HardwareBufferManagerBase::
  42. createVertexBuffer(UINT32 vertexSize, UINT32 numVerts, HardwareBuffer::Usage usage,
  43. bool useShadowBuffer)
  44. {
  45. assert (numVerts > 0);
  46. #if CM_D3D_MANAGE_BUFFERS
  47. // Override shadow buffer setting; managed buffers are automatically
  48. // backed by system memory
  49. // Don't override shadow buffer if discardable, since then we use
  50. // unmanaged buffers for speed (avoids write-through overhead)
  51. if (useShadowBuffer && !(usage & HardwareBuffer::HBU_DISCARDABLE))
  52. {
  53. useShadowBuffer = false;
  54. // Also drop any WRITE_ONLY so we can read direct
  55. if (usage == HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY)
  56. {
  57. usage = HardwareBuffer::HBU_DYNAMIC;
  58. }
  59. else if (usage == HardwareBuffer::HBU_STATIC_WRITE_ONLY)
  60. {
  61. usage = HardwareBuffer::HBU_STATIC;
  62. }
  63. }
  64. #endif
  65. D3D9HardwareVertexBuffer* vbuf = new D3D9HardwareVertexBuffer(
  66. this, vertexSize, numVerts, usage, false, useShadowBuffer);
  67. {
  68. CM_LOCK_MUTEX(mVertexBuffersMutex)
  69. mVertexBuffers.insert(vbuf);
  70. }
  71. return HardwareVertexBufferPtr(vbuf);
  72. }
  73. //-----------------------------------------------------------------------
  74. HardwareIndexBufferPtr
  75. D3D9HardwareBufferManagerBase::
  76. createIndexBuffer(HardwareIndexBuffer::IndexType itype, UINT32 numIndexes,
  77. HardwareBuffer::Usage usage, bool useShadowBuffer)
  78. {
  79. assert (numIndexes > 0);
  80. #if CM_D3D_MANAGE_BUFFERS
  81. // Override shadow buffer setting; managed buffers are automatically
  82. // backed by system memory
  83. if (useShadowBuffer)
  84. {
  85. useShadowBuffer = false;
  86. // Also drop any WRITE_ONLY so we can read direct
  87. if (usage == HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY)
  88. {
  89. usage = HardwareBuffer::HBU_DYNAMIC;
  90. }
  91. else if (usage == HardwareBuffer::HBU_STATIC_WRITE_ONLY)
  92. {
  93. usage = HardwareBuffer::HBU_STATIC;
  94. }
  95. }
  96. #endif
  97. D3D9HardwareIndexBuffer* idx = new D3D9HardwareIndexBuffer(
  98. this, itype, numIndexes, usage, false, useShadowBuffer);
  99. {
  100. CM_LOCK_MUTEX(mIndexBuffersMutex)
  101. mIndexBuffers.insert(idx);
  102. }
  103. return HardwareIndexBufferPtr(idx);
  104. }
  105. //-----------------------------------------------------------------------
  106. VertexDeclarationPtr D3D9HardwareBufferManagerBase::createVertexDeclarationImpl(void)
  107. {
  108. return VertexDeclarationPtr(new D3D9VertexDeclaration());
  109. }
  110. //-----------------------------------------------------------------------
  111. void D3D9HardwareBufferManagerBase::destroyVertexDeclarationImpl(VertexDeclaration* decl)
  112. {
  113. delete decl;
  114. }
  115. }