gfxD3D9PrimitiveBuffer.pc.cpp 3.8 KB

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