gfxPrimitiveBuffer.h 5.5 KB

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