gfxD3D11VertexBuffer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2015 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/D3D11/gfxD3D11VertexBuffer.h"
  24. #include "console/console.h"
  25. GFXD3D11VertexBuffer::~GFXD3D11VertexBuffer()
  26. {
  27. if(getOwningDevice() != NULL)
  28. {
  29. if(mBufferType != GFXBufferTypeVolatile)
  30. {
  31. SAFE_RELEASE(vb);
  32. }
  33. }
  34. }
  35. void GFXD3D11VertexBuffer::lock(U32 vertexStart, U32 vertexEnd, void **vertexPtr)
  36. {
  37. PROFILE_SCOPE(GFXD3D11VertexBuffer_lock);
  38. AssertFatal(lockedVertexStart == 0 && lockedVertexEnd == 0, "Cannot lock a buffer more than once!");
  39. D3D11_MAP flags = D3D11_MAP_WRITE_DISCARD;
  40. switch(mBufferType)
  41. {
  42. case GFXBufferTypeStatic:
  43. case GFXBufferTypeDynamic:
  44. flags = D3D11_MAP_WRITE_DISCARD;
  45. break;
  46. case GFXBufferTypeVolatile:
  47. // Get or create the volatile buffer...
  48. mVolatileBuffer = D3D11->findVBPool( &mVertexFormat, vertexEnd );
  49. if( !mVolatileBuffer )
  50. mVolatileBuffer = D3D11->createVBPool( &mVertexFormat, mVertexSize );
  51. vb = mVolatileBuffer->vb;
  52. // Get our range now...
  53. AssertFatal(vertexStart == 0, "Cannot get a subrange on a volatile buffer.");
  54. AssertFatal(vertexEnd <= GFX_MAX_DYNAMIC_VERTS, "Cannot get more than GFX_MAX_DYNAMIC_VERTS in a volatile buffer. Up the constant!");
  55. AssertFatal(mVolatileBuffer->lockedVertexStart == 0 && mVolatileBuffer->lockedVertexEnd == 0, "Got more than one lock on the volatile pool.");
  56. // We created the pool when we requested this volatile buffer, so assume it exists...
  57. if( mVolatileBuffer->mNumVerts + vertexEnd > GFX_MAX_DYNAMIC_VERTS )
  58. {
  59. flags = D3D11_MAP_WRITE_DISCARD;
  60. mVolatileStart = vertexStart = 0;
  61. vertexEnd = vertexEnd;
  62. }
  63. else
  64. {
  65. flags = D3D11_MAP_WRITE_NO_OVERWRITE;
  66. mVolatileStart = vertexStart = mVolatileBuffer->mNumVerts;
  67. vertexEnd += mVolatileBuffer->mNumVerts;
  68. }
  69. mVolatileBuffer->mNumVerts = vertexEnd+1;
  70. mVolatileBuffer->lockedVertexStart = vertexStart;
  71. mVolatileBuffer->lockedVertexEnd = vertexEnd;
  72. break;
  73. }
  74. lockedVertexStart = vertexStart;
  75. lockedVertexEnd = vertexEnd;
  76. // uncomment it for debugging purpose. called many times per frame... spammy!
  77. //Con::printf("%x: Locking %s range (%d, %d)", this, (mBufferType == GFXBufferTypeVolatile ? "volatile" : "static"), lockedVertexStart, lockedVertexEnd);
  78. U32 sizeToLock = (vertexEnd - vertexStart) * mVertexSize;
  79. if(mBufferType == GFXBufferTypeStatic)
  80. {
  81. *vertexPtr = new U8[sizeToLock];
  82. mLockedBuffer = *vertexPtr;
  83. }
  84. else
  85. {
  86. D3D11_MAPPED_SUBRESOURCE pVertexData;
  87. ZeroMemory(&pVertexData, sizeof(D3D11_MAPPED_SUBRESOURCE));
  88. HRESULT hr = D3D11DEVICECONTEXT->Map(vb, 0, flags, 0, &pVertexData);
  89. if(FAILED(hr))
  90. {
  91. AssertFatal(false, "Unable to lock vertex buffer.");
  92. }
  93. *vertexPtr = (U8*)pVertexData.pData + (vertexStart * mVertexSize);
  94. }
  95. #ifdef TORQUE_DEBUG
  96. // Allocate a debug buffer large enough for the lock
  97. // plus space for over and under run guard strings.
  98. const U32 guardSize = sizeof( _VBGuardString );
  99. mDebugGuardBuffer = new U8[sizeToLock+(guardSize*2)];
  100. // Setup the guard strings.
  101. dMemcpy( mDebugGuardBuffer, _VBGuardString, guardSize );
  102. dMemcpy( mDebugGuardBuffer + sizeToLock + guardSize, _VBGuardString, guardSize );
  103. // Store the real lock pointer and return our debug pointer.
  104. mLockedBuffer = *vertexPtr;
  105. *vertexPtr = mDebugGuardBuffer + guardSize;
  106. #endif // TORQUE_DEBUG
  107. }
  108. void GFXD3D11VertexBuffer::unlock()
  109. {
  110. PROFILE_SCOPE(GFXD3D11VertexBuffer_unlock);
  111. #ifdef TORQUE_DEBUG
  112. if ( mDebugGuardBuffer )
  113. {
  114. const U32 guardSize = sizeof( _VBGuardString );
  115. const U32 sizeLocked = (lockedVertexEnd - lockedVertexStart) * mVertexSize;
  116. // First check the guard areas for overwrites.
  117. AssertFatal(dMemcmp( mDebugGuardBuffer, _VBGuardString, guardSize) == 0,
  118. "GFXD3D11VertexBuffer::unlock - Caught lock memory underrun!" );
  119. AssertFatal(dMemcmp( mDebugGuardBuffer + sizeLocked + guardSize, _VBGuardString, guardSize) == 0,
  120. "GFXD3D11VertexBuffer::unlock - Caught lock memory overrun!" );
  121. // Copy the debug content down to the real VB.
  122. dMemcpy(mLockedBuffer, mDebugGuardBuffer + guardSize, sizeLocked);
  123. // Cleanup.
  124. delete [] mDebugGuardBuffer;
  125. mDebugGuardBuffer = NULL;
  126. //mLockedBuffer = NULL;
  127. }
  128. #endif // TORQUE_DEBUG
  129. if(mBufferType == GFXBufferTypeStatic)
  130. {
  131. const U32 sizeLocked = (lockedVertexEnd - lockedVertexStart) * mVertexSize;
  132. //set up the update region of the buffer
  133. D3D11_BOX box;
  134. box.back = 1;
  135. box.front = 0;
  136. box.top = 0;
  137. box.bottom = 1;
  138. box.left = lockedVertexStart * mVertexSize;
  139. box.right = lockedVertexEnd * mVertexSize;
  140. //update the real vb buffer
  141. D3D11DEVICECONTEXT->UpdateSubresource(vb, 0, &box,mLockedBuffer,sizeLocked, 0);
  142. //clean up the old buffer
  143. delete[] mLockedBuffer;
  144. mLockedBuffer = NULL;
  145. }
  146. else
  147. {
  148. D3D11DEVICECONTEXT->Unmap(vb,0);
  149. }
  150. mIsFirstLock = false;
  151. //uncomment it for debugging purpose. called many times per frame... spammy!
  152. //Con::printf("%x: Unlocking %s range (%d, %d)", this, (mBufferType == GFXBufferTypeVolatile ? "volatile" : "static"), lockedVertexStart, lockedVertexEnd);
  153. lockedVertexEnd = lockedVertexStart = 0;
  154. if(mVolatileBuffer.isValid())
  155. {
  156. mVolatileBuffer->lockedVertexStart = 0;
  157. mVolatileBuffer->lockedVertexEnd = 0;
  158. mVolatileBuffer = NULL;
  159. }
  160. }
  161. void GFXD3D11VertexBuffer::zombify()
  162. {
  163. AssertFatal(lockedVertexStart == 0 && lockedVertexEnd == 0, "GFXD3D11VertexBuffer::zombify - Cannot zombify a locked buffer!");
  164. // Static buffers are managed by D3D11 so we don't deal with them.
  165. if(mBufferType == GFXBufferTypeDynamic)
  166. {
  167. SAFE_RELEASE(vb);
  168. }
  169. }
  170. void GFXD3D11VertexBuffer::resurrect()
  171. {
  172. // Static buffers are managed by D3D11 so we don't deal with them.
  173. if(mBufferType == GFXBufferTypeDynamic)
  174. {
  175. D3D11_BUFFER_DESC desc;
  176. desc.ByteWidth = mVertexSize * mNumVerts;
  177. desc.Usage = D3D11_USAGE_DYNAMIC;
  178. desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  179. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  180. desc.MiscFlags = 0;
  181. desc.StructureByteStride = 0;
  182. HRESULT hr = D3D11DEVICE->CreateBuffer(&desc, NULL, &vb);
  183. if(FAILED(hr))
  184. {
  185. AssertFatal(false, "GFXD3D11VertexBuffer::resurrect - Failed to allocate VB");
  186. }
  187. }
  188. }