gfxD3D9VertexBuffer.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "platform/platform.h"
  23. #include "gfx/D3D9/gfxD3D9VertexBuffer.h"
  24. GFXD3D9VertexBuffer::~GFXD3D9VertexBuffer()
  25. {
  26. #ifdef TORQUE_DEBUG
  27. SAFE_DELETE( name );
  28. #endif
  29. if (getOwningDevice() != NULL)
  30. {
  31. if (mBufferType == GFXBufferTypeDynamic)
  32. static_cast<GFXD3D9Device *>(getOwningDevice())->deallocVertexBuffer( this );
  33. else if (mBufferType != GFXBufferTypeVolatile)
  34. {
  35. static_cast<GFXD3D9Device *>(getOwningDevice())->destroyD3DResource( vb );
  36. }
  37. }
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Lock
  41. //-----------------------------------------------------------------------------
  42. void GFXD3D9VertexBuffer::lock(U32 vertexStart, U32 vertexEnd, void **vertexPtr)
  43. {
  44. PROFILE_SCOPE(GFXD3D9VertexBuffer_lock);
  45. AssertFatal(lockedVertexStart == 0 && lockedVertexEnd == 0, "Cannot lock a buffer more than once!");
  46. U32 flags = 0;
  47. GFXD3D9Device *d = static_cast<GFXD3D9Device *>( mDevice );
  48. switch( mBufferType )
  49. {
  50. case GFXBufferTypeImmutable:
  51. case GFXBufferTypeStatic:
  52. break;
  53. case GFXBufferTypeDynamic:
  54. #ifndef TORQUE_OS_XENON
  55. flags |= D3DLOCK_DISCARD;
  56. #endif
  57. break;
  58. case GFXBufferTypeVolatile:
  59. // Get or create the volatile buffer...
  60. mVolatileBuffer = d->findVBPool( &mVertexFormat, vertexEnd );
  61. if( !mVolatileBuffer )
  62. mVolatileBuffer = d->createVBPool( &mVertexFormat, mVertexSize );
  63. vb = mVolatileBuffer->vb;
  64. // Get our range now...
  65. AssertFatal(vertexStart == 0, "Cannot get a subrange on a volatile buffer.");
  66. AssertFatal(vertexEnd <= MAX_DYNAMIC_VERTS, "Cannot get more than MAX_DYNAMIC_VERTS in a volatile buffer. Up the constant!");
  67. AssertFatal(mVolatileBuffer->lockedVertexStart == 0 && mVolatileBuffer->lockedVertexEnd == 0, "Got more than one lock on the volatile pool.");
  68. // We created the pool when we requested this volatile buffer, so assume it exists...
  69. if( mVolatileBuffer->mNumVerts + vertexEnd > MAX_DYNAMIC_VERTS )
  70. {
  71. #ifdef TORQUE_OS_XENON
  72. AssertFatal( false, "This should never, ever happen. findVBPool should have returned NULL" );
  73. #else
  74. flags |= D3DLOCK_DISCARD;
  75. #endif
  76. mVolatileStart = vertexStart = 0;
  77. vertexEnd = vertexEnd;
  78. }
  79. else
  80. {
  81. flags |= D3DLOCK_NOOVERWRITE;
  82. mVolatileStart = vertexStart = mVolatileBuffer->mNumVerts;
  83. vertexEnd += mVolatileBuffer->mNumVerts;
  84. }
  85. mVolatileBuffer->mNumVerts = vertexEnd+1;
  86. mVolatileBuffer->lockedVertexStart = vertexStart;
  87. mVolatileBuffer->lockedVertexEnd = vertexEnd;
  88. break;
  89. }
  90. lockedVertexStart = vertexStart;
  91. lockedVertexEnd = vertexEnd;
  92. // Con::printf("%x: Locking %s range (%d, %d)", this, (mBufferType == GFXBufferTypeVolatile ? "volatile" : "static"), lockedVertexStart, lockedVertexEnd);
  93. #ifdef TORQUE_OS_XENON
  94. // If the vertex buffer which we are trying to lock is held by the D3D device
  95. // on Xenon it will bomb. So if that is the case, then SetStreamSource to NULL
  96. // and also call setVertexBuffer because otherwise the state-cache will be hosed
  97. if( d->mCurrentVB != NULL && d->mCurrentVB->vb == vb )
  98. {
  99. d->setVertexBuffer( NULL );
  100. d->mD3DDevice->SetStreamSource( 0, NULL, 0, 0 );
  101. }
  102. // As of October 2006 XDK, range locking is no longer supported. Lock the whole buffer
  103. // and then manually offset the pointer to simulate the subrange. -patw
  104. D3D9Assert( vb->Lock( 0, 0, vertexPtr, flags),
  105. "Unable to lock vertex buffer.");
  106. U8 *tmp = (U8 *)(*vertexPtr);
  107. tmp += ( vertexStart * mVertexSize );
  108. *vertexPtr = tmp;
  109. #else
  110. U32 sizeToLock = (vertexEnd - vertexStart) * mVertexSize;
  111. D3D9Assert( vb->Lock(vertexStart * mVertexSize, sizeToLock, vertexPtr, flags),
  112. "Unable to lock vertex buffer.");
  113. #ifdef TORQUE_DEBUG
  114. // Allocate a debug buffer large enough for the lock
  115. // plus space for over and under run guard strings.
  116. const U32 guardSize = sizeof( _VBGuardString );
  117. mDebugGuardBuffer = new U8[sizeToLock+(guardSize*2)];
  118. // Setup the guard strings.
  119. dMemcpy( mDebugGuardBuffer, _VBGuardString, guardSize );
  120. dMemcpy( mDebugGuardBuffer + sizeToLock + guardSize, _VBGuardString, guardSize );
  121. // Store the real lock pointer and return our debug pointer.
  122. mLockedBuffer = *vertexPtr;
  123. *vertexPtr = mDebugGuardBuffer + guardSize;
  124. #endif // TORQUE_DEBUG
  125. #endif
  126. }
  127. void GFXD3D9VertexBuffer::unlock()
  128. {
  129. PROFILE_SCOPE(GFXD3D9VertexBuffer_unlock);
  130. #ifdef TORQUE_DEBUG
  131. if ( mDebugGuardBuffer )
  132. {
  133. const U32 guardSize = sizeof( _VBGuardString );
  134. const U32 sizeLocked = (lockedVertexEnd - lockedVertexStart) * mVertexSize;
  135. // First check the guard areas for overwrites.
  136. AssertFatal( dMemcmp( mDebugGuardBuffer, _VBGuardString, guardSize ) == 0,
  137. "GFXD3D9VertexBuffer::unlock - Caught lock memory underrun!" );
  138. AssertFatal( dMemcmp( mDebugGuardBuffer + sizeLocked + guardSize, _VBGuardString, guardSize ) == 0,
  139. "GFXD3D9VertexBuffer::unlock - Caught lock memory overrun!" );
  140. // Copy the debug content down to the real VB.
  141. dMemcpy( mLockedBuffer, mDebugGuardBuffer + guardSize, sizeLocked );
  142. // Cleanup.
  143. delete [] mDebugGuardBuffer;
  144. mDebugGuardBuffer = NULL;
  145. mLockedBuffer = NULL;
  146. }
  147. #endif // TORQUE_DEBUG
  148. D3D9Assert( vb->Unlock(),
  149. "Unable to unlock vertex buffer.");
  150. mIsFirstLock = false;
  151. // Con::printf("%x: Unlocking %s range (%d, %d)", this, (mBufferType == GFXBufferTypeVolatile ? "volatile" : "static"), lockedVertexStart, lockedVertexEnd);
  152. lockedVertexEnd = lockedVertexStart = 0;
  153. if(mVolatileBuffer.isValid())
  154. {
  155. mVolatileBuffer->lockedVertexStart = 0;
  156. mVolatileBuffer->lockedVertexEnd = 0;
  157. mVolatileBuffer = NULL;
  158. //vb->Release();
  159. //vb = NULL;
  160. }
  161. }
  162. void GFXD3D9VertexBuffer::zombify()
  163. {
  164. AssertFatal(lockedVertexStart == 0 && lockedVertexEnd == 0, "GFXD3D9VertexBuffer::zombify - Cannot zombify a locked buffer!");
  165. // Static buffers are managed by D3D9 so we don't deal with them.
  166. if(mBufferType == GFXBufferTypeDynamic || mBufferType == GFXBufferTypeImmutable)
  167. {
  168. SAFE_RELEASE(vb);
  169. }
  170. }
  171. void GFXD3D9VertexBuffer::resurrect()
  172. {
  173. // Static buffers are managed by D3D9 so we don't deal with them.
  174. if(mBufferType == GFXBufferTypeDynamic)
  175. {
  176. D3D9Assert(static_cast<GFXD3D9Device*>(mDevice)->mD3DDevice->CreateVertexBuffer( mVertexSize * mNumVerts,
  177. #ifdef TORQUE_OS_XENON
  178. D3DUSAGE_WRITEONLY,
  179. #else
  180. D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
  181. #endif
  182. 0,
  183. D3DPOOL_DEFAULT,
  184. &vb,
  185. NULL ),
  186. "GFXD3D9VertexBuffer::resurrect - Failed to allocate VB" );
  187. }
  188. }