gfxD3D9PrimitiveBuffer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gfx/D3D9/gfxD3D9Device.h"
  23. #include "gfx/D3D9/gfxD3D9EnumTranslate.h"
  24. #include "gfx/D3D9/gfxD3D9PrimitiveBuffer.h"
  25. #include "core/util/safeRelease.h"
  26. void GFXD3D9PrimitiveBuffer::prepare()
  27. {
  28. static_cast<GFXD3D9Device *>( mDevice )->_setPrimitiveBuffer(this);
  29. }
  30. void GFXD3D9PrimitiveBuffer::unlock()
  31. {
  32. #ifdef TORQUE_DEBUG
  33. if ( mDebugGuardBuffer )
  34. {
  35. const U32 guardSize = sizeof( _PBGuardString );
  36. // First check the guard areas for overwrites.
  37. AssertFatal( dMemcmp( mDebugGuardBuffer, _PBGuardString, guardSize ) == 0,
  38. "GFXD3D9PrimitiveBuffer::unlock - Caught lock memory underrun!" );
  39. AssertFatal( dMemcmp( mDebugGuardBuffer + mLockedSize + guardSize, _PBGuardString, guardSize ) == 0,
  40. "GFXD3D9PrimitiveBuffer::unlock - Caught lock memory overrun!" );
  41. // Copy the debug content down to the real PB.
  42. dMemcpy( mLockedBuffer, mDebugGuardBuffer + guardSize, mLockedSize );
  43. // Cleanup.
  44. delete [] mDebugGuardBuffer;
  45. mDebugGuardBuffer = NULL;
  46. mLockedBuffer = NULL;
  47. mLockedSize = 0;
  48. }
  49. #endif // TORQUE_DEBUG
  50. ib->Unlock();
  51. mLocked = false;
  52. mIsFirstLock = false;
  53. mVolatileBuffer = NULL;
  54. }
  55. GFXD3D9PrimitiveBuffer::~GFXD3D9PrimitiveBuffer()
  56. {
  57. if( mBufferType != GFXBufferTypeVolatile )
  58. {
  59. #if defined(TORQUE_OS_XENON)
  60. if(ib->IsSet(reinterpret_cast<GFXD3D9Device *>(mDevice)->mD3DDevice))
  61. {
  62. reinterpret_cast<GFXD3D9Device *>(mDevice)->mD3DDevice->SetIndices(NULL);
  63. }
  64. #endif
  65. SAFE_RELEASE( ib );
  66. }
  67. }
  68. void GFXD3D9PrimitiveBuffer::zombify()
  69. {
  70. if(mBufferType == GFXBufferTypeStatic)
  71. return;
  72. AssertFatal(!mLocked, "GFXD3D9PrimitiveBuffer::zombify - Cannot zombify a locked buffer!");
  73. if (mBufferType == GFXBufferTypeVolatile)
  74. {
  75. // We must null the volatile buffer else we're holding
  76. // a dead pointer which can be set on the device.
  77. ib = NULL;
  78. return;
  79. }
  80. // Dynamic buffers get released.
  81. SAFE_RELEASE(ib);
  82. }
  83. void GFXD3D9PrimitiveBuffer::resurrect()
  84. {
  85. if ( mBufferType != GFXBufferTypeDynamic )
  86. return;
  87. U32 usage = D3DUSAGE_WRITEONLY;
  88. #ifndef TORQUE_OS_XENON
  89. usage |= D3DUSAGE_DYNAMIC;
  90. #endif
  91. D3DPOOL pool = D3DPOOL_DEFAULT;
  92. D3D9Assert(static_cast<GFXD3D9Device*>(mDevice)->mD3DDevice->CreateIndexBuffer( sizeof(U16) * mIndexCount ,
  93. usage , GFXD3D9IndexFormat[GFXIndexFormat16], pool, &ib, 0),
  94. "GFXD3D9PrimitiveBuffer::resurrect - Failed to allocate an index buffer.");
  95. }