featureSet.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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::getArguments(U32 index) const
  71. {
  72. if (mFeatures[index].argStruct)
  73. return mFeatures[index].argStruct;
  74. return nullptr;
  75. }
  76. void FeatureSet::clear()
  77. {
  78. mDescription.clear();
  79. mFeatures.clear();
  80. }
  81. FeatureSet& FeatureSet::operator =( const FeatureSet &h )
  82. {
  83. clear();
  84. merge( h );
  85. return *this;
  86. }
  87. bool FeatureSet::hasFeature( const FeatureType &type, S32 index ) const
  88. {
  89. PROFILE_SCOPE(FeatureSet_hasFeature);
  90. for ( U32 i = 0; i < mFeatures.size(); i++)
  91. {
  92. if ( mFeatures[i].type == &type &&
  93. ( index < 0 || mFeatures[i].index == index ) )
  94. return true;
  95. }
  96. return false;
  97. }
  98. void FeatureSet::setFeature( const FeatureType &type, bool set, S32 index )
  99. {
  100. for ( U32 i=0; i < mFeatures.size(); i++ )
  101. {
  102. const FeatureInfo &info = mFeatures[i];
  103. if ( info.type == &type && info.index == index )
  104. {
  105. if ( set )
  106. return;
  107. else
  108. {
  109. mFeatures.erase_fast( i );
  110. mDescription.clear();
  111. return;
  112. }
  113. }
  114. }
  115. if ( !set )
  116. return;
  117. FeatureInfo info;
  118. info.type = &type;
  119. info.index = index;
  120. mFeatures.push_back( info );
  121. mDescription.clear();
  122. }
  123. void FeatureSet::addFeature( const FeatureType &type, S32 index, void* argStruct )
  124. {
  125. for ( U32 i=0; i < mFeatures.size(); i++ )
  126. {
  127. const FeatureInfo &info = mFeatures[i];
  128. if ( info.type == &type &&
  129. info.index == index )
  130. return;
  131. }
  132. FeatureInfo info;
  133. info.type = &type;
  134. info.index = index;
  135. info.argStruct = argStruct;
  136. mFeatures.push_back( info );
  137. mDescription.clear();
  138. }
  139. void FeatureSet::removeFeature( const FeatureType &type )
  140. {
  141. for ( U32 i=0; i < mFeatures.size(); i++ )
  142. {
  143. const FeatureInfo &info = mFeatures[i];
  144. if ( info.type == &type )
  145. {
  146. mFeatures.erase_fast( i );
  147. mDescription.clear();
  148. return;
  149. }
  150. }
  151. }
  152. S32 FeatureSet::getNextFeatureIndex( const FeatureType &type, S32 index ) const
  153. {
  154. for ( U32 i=0; i < mFeatures.size(); i++ )
  155. {
  156. const FeatureInfo &info = mFeatures[i];
  157. if ( info.type == &type && info.index > index )
  158. return i;
  159. }
  160. return -1;
  161. }
  162. void FeatureSet::filter( const FeatureSet &features )
  163. {
  164. PROFILE_SCOPE( FeatureSet_Filter );
  165. for ( U32 i=0; i < mFeatures.size(); )
  166. {
  167. if ( !features.hasFeature( *mFeatures[i].type ) )
  168. mFeatures.erase_fast( i );
  169. else
  170. i++;
  171. }
  172. mDescription.clear();
  173. }
  174. void FeatureSet::exclude( const FeatureSet &features )
  175. {
  176. PROFILE_SCOPE( FeatureSet_Exclude );
  177. for ( U32 i=0; i < features.mFeatures.size(); i++ )
  178. removeFeature( *features.mFeatures[i].type );
  179. mDescription.clear();
  180. }
  181. void FeatureSet::merge( const FeatureSet &features )
  182. {
  183. PROFILE_SCOPE( FeatureSet_Merge );
  184. if ( mFeatures.empty() )
  185. {
  186. mFeatures.merge( features.mFeatures );
  187. mDescription = features.mDescription;
  188. return;
  189. }
  190. for ( U32 i=0; i < features.mFeatures.size(); i++ )
  191. addFeature( *features.mFeatures[i].type,
  192. features.mFeatures[i].index );
  193. }