Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: This is a helper class for the situation where you want to build lists of
  4. // things that fall into buckets, and effeciently keep up with which buckets have
  5. // been used since flush.
  6. //
  7. //=============================================================================//
  8. #ifndef MATERIALBUCKETS_H
  9. #define MATERIALBUCKETS_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. // FLASHLIGHTFIXME: Make all of the buckets share the same m_Elements (ie. make m_Elements static)
  14. template <class Element_t>
  15. class CMaterialsBuckets
  16. {
  17. public:
  18. typedef unsigned short SortIDHandle_t;
  19. typedef unsigned short ElementHandle_t;
  20. CMaterialsBuckets()
  21. {
  22. m_FlushCount = -1;
  23. }
  24. // Set the number of buckets that are needed. This should get called every time
  25. // a level is loaded.
  26. void SetNumMaterialSortIDs( int n )
  27. {
  28. m_MaterialSortInfoArray.Purge();
  29. m_MaterialSortInfoArray.SetCount( n );
  30. m_Elements.Purge();
  31. m_UsedSortIDs.Purge();
  32. }
  33. // Clear out all buckets. This should get called once a frame.
  34. void Flush( void )
  35. {
  36. m_FlushCount++;
  37. m_Elements.RemoveAll();
  38. m_UsedSortIDs.RemoveAll();
  39. }
  40. //
  41. // These functions are used to get at the list of used buckets.
  42. //
  43. SortIDHandle_t GetFirstUsedSortID()
  44. {
  45. return m_UsedSortIDs.Head();
  46. }
  47. SortIDHandle_t GetNextUsedSortID( SortIDHandle_t prevSortID )
  48. {
  49. return m_UsedSortIDs.Next( prevSortID );
  50. }
  51. int GetSortID( SortIDHandle_t handle )
  52. {
  53. return m_UsedSortIDs[handle];
  54. }
  55. SortIDHandle_t InvalidSortIDHandle()
  56. {
  57. return m_UsedSortIDs.InvalidIndex();
  58. }
  59. //
  60. // These functions are used to get at the list of elements for each sortID.
  61. //
  62. ElementHandle_t GetElementListHead( int sortID )
  63. {
  64. return m_MaterialSortInfoArray[sortID].m_Head;
  65. }
  66. ElementHandle_t GetElementListNext( ElementHandle_t h )
  67. {
  68. return m_Elements.Next( h );
  69. }
  70. Element_t GetElement( ElementHandle_t h )
  71. {
  72. return m_Elements[h];
  73. }
  74. ElementHandle_t InvalidElementHandle()
  75. {
  76. return m_Elements.InvalidIndex();
  77. }
  78. // Add an element to the the bucket specified by sortID
  79. void AddElement( int sortID, Element_t elem )
  80. {
  81. // Allocate an element to stick this in.
  82. unsigned short elemID = m_Elements.Alloc( true );
  83. m_Elements[elemID] = elem;
  84. if( m_MaterialSortInfoArray[sortID].m_FlushCount != m_FlushCount )
  85. {
  86. // This is the first element that has used this sort id since flush.
  87. // FLASHLIGHTFIXME: need to sort these by vertex format when shoving
  88. // them into this list!
  89. // Mark this sortID as having been used since the last flush.
  90. m_MaterialSortInfoArray[sortID].m_FlushCount = m_FlushCount;
  91. // Add this sortID to the list of sortIDs used since flush.
  92. m_UsedSortIDs.AddToTail( sortID );
  93. // Set the head pointer for this sort id to this element.
  94. m_MaterialSortInfoArray[sortID].m_Head = elemID;
  95. }
  96. else
  97. {
  98. // We already have an element in this sort id since flush, so chain
  99. // into thelist of elements for this sort id.
  100. m_Elements.LinkBefore( m_MaterialSortInfoArray[sortID].m_Head, elemID );
  101. m_MaterialSortInfoArray[sortID].m_Head = elemID;
  102. }
  103. }
  104. private:
  105. struct MaterialSortInfo_t
  106. {
  107. MaterialSortInfo_t() :
  108. m_FlushCount( -1 ),
  109. m_Head( (unsigned short)-1 ) // i.e., InvalidIndex()
  110. {
  111. }
  112. int m_FlushCount;
  113. unsigned short m_Head;
  114. };
  115. // This is a list of material sort info ids that have been used since flush.
  116. CUtlLinkedList<unsigned short> m_UsedSortIDs;
  117. // This is m_NumMaterialSortIDs big.
  118. CUtlVector<MaterialSortInfo_t> m_MaterialSortInfoArray;
  119. // This is used in multilist mode to make elements that belong in the multiple lists of
  120. CUtlLinkedList<Element_t, unsigned short, true> m_Elements;
  121. int m_FlushCount;
  122. };
  123. #endif // MATERIALBUCKETS_H