featureSet.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if (!argStruct)
  126. {
  127. for (U32 i = 0; i < mFeatures.size(); i++)
  128. {
  129. const FeatureInfo& info = mFeatures[i];
  130. if (info.type == &type &&
  131. info.index == index)
  132. return;
  133. }
  134. }
  135. FeatureInfo info;
  136. info.type = &type;
  137. info.index = index;
  138. info.argStruct = argStruct;
  139. mFeatures.push_back( info );
  140. mDescription.clear();
  141. }
  142. void FeatureSet::removeFeature( const FeatureType &type )
  143. {
  144. for ( U32 i=0; i < mFeatures.size(); i++ )
  145. {
  146. const FeatureInfo &info = mFeatures[i];
  147. if ( info.type == &type )
  148. {
  149. mFeatures.erase_fast( i );
  150. mDescription.clear();
  151. return;
  152. }
  153. }
  154. }
  155. S32 FeatureSet::getNextFeatureIndex( const FeatureType &type, S32 index ) const
  156. {
  157. for ( U32 i=0; i < mFeatures.size(); i++ )
  158. {
  159. const FeatureInfo &info = mFeatures[i];
  160. if ( info.type == &type && info.index > index )
  161. return i;
  162. }
  163. return -1;
  164. }
  165. void FeatureSet::filter( const FeatureSet &features )
  166. {
  167. PROFILE_SCOPE( FeatureSet_Filter );
  168. for ( U32 i=0; i < mFeatures.size(); )
  169. {
  170. if ( !features.hasFeature( *mFeatures[i].type ) )
  171. mFeatures.erase_fast( i );
  172. else
  173. i++;
  174. }
  175. mDescription.clear();
  176. }
  177. void FeatureSet::exclude( const FeatureSet &features )
  178. {
  179. PROFILE_SCOPE( FeatureSet_Exclude );
  180. for ( U32 i=0; i < features.mFeatures.size(); i++ )
  181. removeFeature( *features.mFeatures[i].type );
  182. mDescription.clear();
  183. }
  184. void FeatureSet::merge( const FeatureSet &features )
  185. {
  186. PROFILE_SCOPE( FeatureSet_Merge );
  187. if ( mFeatures.empty() )
  188. {
  189. mFeatures.merge( features.mFeatures );
  190. mDescription = features.mDescription;
  191. return;
  192. }
  193. for ( U32 i=0; i < features.mFeatures.size(); i++ )
  194. addFeature( *features.mFeatures[i].type,
  195. features.mFeatures[i].index );
  196. }