featureSet.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "platform/platform.h"
  23. #include "shaderGen/featureSet.h"
  24. #include "shaderGen/featureType.h"
  25. #include "platform/profiler.h"
  26. #include "core/util/hashFunction.h"
  27. const FeatureSet FeatureSet::EmptySet;
  28. S32 QSORT_CALLBACK FeatureSet::_typeCmp( const FeatureInfo* a, const FeatureInfo* b )
  29. {
  30. if ( a->type->getGroup() < b->type->getGroup() )
  31. return -1;
  32. else if ( a->type->getGroup() > b->type->getGroup() )
  33. return 1;
  34. else if ( a->index < b->index )
  35. return -1;
  36. else if ( a->index > b->index )
  37. return 1;
  38. else if ( a->type->getOrder() < b->type->getOrder() )
  39. return -1;
  40. else if ( a->type->getOrder() > b->type->getOrder() )
  41. return 1;
  42. else
  43. return 0;
  44. }
  45. void FeatureSet::_rebuildDesc()
  46. {
  47. PROFILE_SCOPE( FeatureSet_RebuildDesc );
  48. // First get the features in the proper order.
  49. mFeatures.sort( _typeCmp );
  50. String desc;
  51. for ( U32 i=0; i < mFeatures.size(); i++ )
  52. desc += String::ToString( "%s,%d\n",
  53. mFeatures[i].type->getName().c_str(),
  54. mFeatures[i].index );
  55. // By interning the description we have only
  56. // one instance in the system and we get fast
  57. // pointer compares for equality.
  58. mDescription = desc.intern();
  59. }
  60. const FeatureType& FeatureSet::getAt( U32 index, S32 *outIndex ) const
  61. {
  62. // We want to make sure we access the features in the
  63. // correct order. By asking for the description we ensure
  64. // the feature set is properly sorted.
  65. getDescription();
  66. if ( outIndex )
  67. *outIndex = mFeatures[index].index;
  68. return *mFeatures[index].type;
  69. }
  70. void FeatureSet::clear()
  71. {
  72. mDescription.clear();
  73. mFeatures.clear();
  74. }
  75. FeatureSet& FeatureSet::operator =( const FeatureSet &h )
  76. {
  77. clear();
  78. merge( h );
  79. return *this;
  80. }
  81. bool FeatureSet::hasFeature( const FeatureType &type, S32 index ) const
  82. {
  83. PROFILE_SCOPE(FeatureSet_hasFeature);
  84. for ( U32 i = 0; i < mFeatures.size(); i++)
  85. {
  86. if ( mFeatures[i].type == &type &&
  87. ( index < 0 || mFeatures[i].index == index ) )
  88. return true;
  89. }
  90. return false;
  91. }
  92. void FeatureSet::setFeature( const FeatureType &type, bool set, S32 index )
  93. {
  94. for ( U32 i=0; i < mFeatures.size(); i++ )
  95. {
  96. const FeatureInfo &info = mFeatures[i];
  97. if ( info.type == &type && info.index == index )
  98. {
  99. if ( set )
  100. return;
  101. else
  102. {
  103. mFeatures.erase_fast( i );
  104. mDescription.clear();
  105. return;
  106. }
  107. }
  108. }
  109. if ( !set )
  110. return;
  111. FeatureInfo info;
  112. info.type = &type;
  113. info.index = index;
  114. mFeatures.push_back( info );
  115. mDescription.clear();
  116. }
  117. void FeatureSet::addFeature( const FeatureType &type, S32 index )
  118. {
  119. for ( U32 i=0; i < mFeatures.size(); i++ )
  120. {
  121. const FeatureInfo &info = mFeatures[i];
  122. if ( info.type == &type &&
  123. info.index == index )
  124. return;
  125. }
  126. FeatureInfo info;
  127. info.type = &type;
  128. info.index = index;
  129. mFeatures.push_back( info );
  130. mDescription.clear();
  131. }
  132. void FeatureSet::removeFeature( const FeatureType &type )
  133. {
  134. for ( U32 i=0; i < mFeatures.size(); i++ )
  135. {
  136. const FeatureInfo &info = mFeatures[i];
  137. if ( info.type == &type )
  138. {
  139. mFeatures.erase_fast( i );
  140. mDescription.clear();
  141. return;
  142. }
  143. }
  144. }
  145. S32 FeatureSet::getNextFeatureIndex( const FeatureType &type, S32 index ) const
  146. {
  147. for ( U32 i=0; i < mFeatures.size(); i++ )
  148. {
  149. const FeatureInfo &info = mFeatures[i];
  150. if ( info.type == &type && info.index > index )
  151. return i;
  152. }
  153. return -1;
  154. }
  155. void FeatureSet::filter( const FeatureSet &features )
  156. {
  157. PROFILE_SCOPE( FeatureSet_Filter );
  158. for ( U32 i=0; i < mFeatures.size(); )
  159. {
  160. if ( !features.hasFeature( *mFeatures[i].type ) )
  161. mFeatures.erase_fast( i );
  162. else
  163. i++;
  164. }
  165. mDescription.clear();
  166. }
  167. void FeatureSet::exclude( const FeatureSet &features )
  168. {
  169. PROFILE_SCOPE( FeatureSet_Exclude );
  170. for ( U32 i=0; i < features.mFeatures.size(); i++ )
  171. removeFeature( *features.mFeatures[i].type );
  172. mDescription.clear();
  173. }
  174. void FeatureSet::merge( const FeatureSet &features )
  175. {
  176. PROFILE_SCOPE( FeatureSet_Merge );
  177. if ( mFeatures.empty() )
  178. {
  179. mFeatures.merge( features.mFeatures );
  180. mDescription = features.mDescription;
  181. return;
  182. }
  183. for ( U32 i=0; i < features.mFeatures.size(); i++ )
  184. addFeature( *features.mFeatures[i].type,
  185. features.mFeatures[i].index );
  186. }