gfxD3D9PrimitiveBuffer.pc.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/gfxD3D9PrimitiveBuffer.h"
  24. #include "core/util/safeRelease.h"
  25. void GFXD3D9PrimitiveBuffer::lock(U32 indexStart, U32 indexEnd, void **indexPtr)
  26. {
  27. AssertFatal(!mLocked, "GFXD3D9PrimitiveBuffer::lock - Can't lock a primitive buffer more than once!");
  28. mLocked = true;
  29. U32 flags=0;
  30. switch(mBufferType)
  31. {
  32. case GFXBufferTypeStatic:
  33. // flags |= D3DLOCK_DISCARD;
  34. break;
  35. case GFXBufferTypeDynamic:
  36. // Always discard the content within a locked region.
  37. flags |= D3DLOCK_DISCARD;
  38. break;
  39. case GFXBufferTypeVolatile:
  40. // Get our range now...
  41. AssertFatal(indexStart == 0, "Cannot get a subrange on a volatile buffer.");
  42. AssertFatal(indexEnd < MAX_DYNAMIC_INDICES, "Cannot get more than MAX_DYNAMIC_INDICES in a volatile buffer. Up the constant!");
  43. // Get the primtive buffer
  44. mVolatileBuffer = ((GFXD3D9Device*)mDevice)->mDynamicPB;
  45. AssertFatal( mVolatileBuffer, "GFXD3D9PrimitiveBuffer::lock - No dynamic primitive buffer was available!");
  46. // We created the pool when we requested this volatile buffer, so assume it exists...
  47. if( mVolatileBuffer->mIndexCount + indexEnd > MAX_DYNAMIC_INDICES )
  48. {
  49. flags |= D3DLOCK_DISCARD;
  50. mVolatileStart = indexStart = 0;
  51. indexEnd = indexEnd;
  52. }
  53. else
  54. {
  55. flags |= D3DLOCK_NOOVERWRITE;
  56. mVolatileStart = indexStart = mVolatileBuffer->mIndexCount;
  57. indexEnd += mVolatileBuffer->mIndexCount;
  58. }
  59. mVolatileBuffer->mIndexCount = indexEnd + 1;
  60. ib = mVolatileBuffer->ib;
  61. break;
  62. }
  63. D3D9Assert( ib->Lock(indexStart * sizeof(U16), (indexEnd - indexStart) * sizeof(U16), indexPtr, flags),
  64. "GFXD3D9PrimitiveBuffer::lock - Could not lock primitive buffer.");
  65. #ifdef TORQUE_DEBUG
  66. // Allocate a debug buffer large enough for the lock
  67. // plus space for over and under run guard strings.
  68. mLockedSize = (indexEnd - indexStart) * sizeof(U16);
  69. const U32 guardSize = sizeof( _PBGuardString );
  70. mDebugGuardBuffer = new U8[mLockedSize+(guardSize*2)];
  71. // Setup the guard strings.
  72. dMemcpy( mDebugGuardBuffer, _PBGuardString, guardSize );
  73. dMemcpy( mDebugGuardBuffer + mLockedSize + guardSize, _PBGuardString, guardSize );
  74. // Store the real lock pointer and return our debug pointer.
  75. mLockedBuffer = *indexPtr;
  76. *indexPtr = (U16*)( mDebugGuardBuffer + guardSize );
  77. #endif // TORQUE_DEBUG
  78. }