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