gfxPrimitiveBuffer.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifndef _GFXPRIMITIVEBUFFER_H_
  23. #define _GFXPRIMITIVEBUFFER_H_
  24. #ifndef _GFXSTRUCTS_H_
  25. #include "gfx/gfxStructs.h"
  26. #endif
  27. #ifdef TORQUE_ENABLE_PROFILER
  28. #include "platform/profiler.h"
  29. #endif
  30. class GFXPrimitiveBuffer : public StrongRefBase, public GFXResource
  31. {
  32. friend class GFXPrimitiveBufferHandle;
  33. friend class GFXDevice;
  34. public: //protected:
  35. U32 mIndexCount;
  36. U32 mPrimitiveCount;
  37. GFXBufferType mBufferType;
  38. GFXPrimitive *mPrimitiveArray;
  39. GFXDevice *mDevice;
  40. #ifdef TORQUE_DEBUG
  41. // In debug builds we provide a TOC leak tracking system.
  42. static U32 smActivePBCount;
  43. static GFXPrimitiveBuffer *smHead;
  44. static void dumpActivePBs();
  45. String mDebugCreationPath;
  46. GFXPrimitiveBuffer *mDebugNext;
  47. GFXPrimitiveBuffer *mDebugPrev;
  48. #endif
  49. GFXPrimitiveBuffer( GFXDevice *device,
  50. U32 indexCount,
  51. U32 primitiveCount,
  52. GFXBufferType bufferType )
  53. {
  54. mDevice = device;
  55. mIndexCount = indexCount;
  56. mPrimitiveCount = primitiveCount;
  57. mBufferType = bufferType;
  58. if(primitiveCount)
  59. {
  60. mPrimitiveArray = new GFXPrimitive[primitiveCount];
  61. dMemset((void *) mPrimitiveArray, 0, primitiveCount * sizeof(GFXPrimitive));
  62. }
  63. else
  64. mPrimitiveArray = NULL;
  65. #if defined(TORQUE_DEBUG)
  66. // Active copy tracking.
  67. smActivePBCount++;
  68. #if defined(TORQUE_ENABLE_PROFILE_PATH)
  69. mDebugCreationPath = gProfiler->getProfilePath();
  70. #endif
  71. mDebugNext = smHead;
  72. mDebugPrev = NULL;
  73. if(smHead)
  74. {
  75. AssertFatal(smHead->mDebugPrev == NULL, "GFXPrimitiveBuffer::GFXPrimitiveBuffer - found unexpected previous in current head!");
  76. smHead->mDebugPrev = this;
  77. }
  78. smHead = this;
  79. #endif
  80. }
  81. virtual ~GFXPrimitiveBuffer()
  82. {
  83. if( mPrimitiveArray != NULL )
  84. {
  85. delete [] mPrimitiveArray;
  86. mPrimitiveArray = NULL;
  87. }
  88. #ifdef TORQUE_DEBUG
  89. if(smHead == this)
  90. smHead = this->mDebugNext;
  91. if(mDebugNext)
  92. mDebugNext->mDebugPrev = mDebugPrev;
  93. if(mDebugPrev)
  94. mDebugPrev->mDebugNext = mDebugNext;
  95. mDebugPrev = mDebugNext = NULL;
  96. smActivePBCount--;
  97. #endif
  98. }
  99. virtual void lock(U32 indexStart, U32 indexEnd, void **indexPtr)=0; ///< locks this primitive buffer for writing into
  100. virtual void unlock()=0; ///< unlocks this primitive buffer.
  101. virtual void prepare()=0; ///< prepares this primitive buffer for use on the device it was allocated on
  102. // GFXResource interface
  103. /// The resource should put a description of itself (number of vertices, size/width of texture, etc.) in buffer
  104. virtual const String describeSelf() const;
  105. };
  106. class GFXPrimitiveBufferHandle : public StrongRefPtr<GFXPrimitiveBuffer>
  107. {
  108. typedef StrongRefPtr<GFXPrimitiveBuffer> Parent;
  109. public:
  110. enum Constants {
  111. MaxIndexCount = 65535,
  112. };
  113. GFXPrimitiveBufferHandle() {};
  114. GFXPrimitiveBufferHandle(GFXDevice *theDevice, U32 indexCount, U32 primitiveCount, GFXBufferType bufferType, String desc = String::EmptyString )
  115. {
  116. set(theDevice, indexCount, primitiveCount, bufferType, desc);
  117. }
  118. void set(GFXDevice *theDevice, U32 indexCount, U32 primitiveCount, GFXBufferType bufferType, String desc = String::EmptyString );
  119. void lock(U16 **indexBuffer, GFXPrimitive **primitiveBuffer = NULL, U32 indexStart = 0, U32 indexEnd = 0)
  120. {
  121. if(indexEnd == 0)
  122. indexEnd = getPointer()->mIndexCount;
  123. AssertFatal(indexStart < indexEnd && indexEnd <= getPointer()->mIndexCount, "Out of range index lock!");
  124. getPointer()->lock(indexStart, indexEnd, (void**)indexBuffer);
  125. if(primitiveBuffer)
  126. *primitiveBuffer = getPointer()->mPrimitiveArray;
  127. }
  128. void unlock()
  129. {
  130. getPointer()->unlock();
  131. }
  132. void prepare()
  133. {
  134. getPointer()->prepare();
  135. }
  136. bool operator==(const GFXPrimitiveBufferHandle &buffer) const {
  137. return getPointer() == buffer.getPointer();
  138. }
  139. GFXPrimitiveBufferHandle& operator=(GFXPrimitiveBuffer *ptr)
  140. {
  141. StrongObjectRef::set(ptr);
  142. return *this;
  143. }
  144. };
  145. #endif // _GFXPRIMITIVEBUFFER_H_