CmD3D9HardwareBufferManager.cpp 4.9 KB

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