ShaderParser.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "GraphicsDefs.h"
  24. #include "HashMap.h"
  25. #include "HashSet.h"
  26. namespace Urho3D
  27. {
  28. class XMLElement;
  29. /// Option definition and combination rules for constructing shader variations.
  30. struct ShaderOption
  31. {
  32. /// Variation name.
  33. String name_;
  34. /// Defines to use in compiling.
  35. Vector<String> defines_;
  36. /// Define values to use in compiling.
  37. Vector<String> defineValues_;
  38. /// Other variations to exclude.
  39. Vector<String> excludes_;
  40. /// Other variations to include.
  41. Vector<String> includes_;
  42. /// Required defines for variation to be compiled.
  43. Vector<String> requires_;
  44. /// Excluded options as a bitmask.
  45. unsigned excludeBits_;
  46. /// Included options as a bitmask.
  47. unsigned includeBits_;
  48. /// Exclude bits for variations of the same group.
  49. unsigned variationGroupBits_;
  50. /// Option(s) which satisfy the requirements.
  51. PODVector<unsigned> requirementBits_;
  52. /// Variation flag. A variation excludes all other variations, unless in a separate group.
  53. bool isVariation_;
  54. /// Dummy separator flag.
  55. bool isDummySeparator_;
  56. };
  57. /// Combination of shader options, used for compiling a shader variation.
  58. struct ShaderCombination
  59. {
  60. /// Shader variation name.
  61. String name_;
  62. /// Defines to use in compiling.
  63. Vector<String> defines_;
  64. /// Define values to use in compiling.
  65. Vector<String> defineValues_;
  66. };
  67. /// %Shader definition parser. Constructs a list of shader variations from the definition.
  68. class URHO3D_API ShaderParser
  69. {
  70. public:
  71. /// Construct with defaults.
  72. ShaderParser();
  73. /// Parse from an XML element. Return true if successful.
  74. bool Parse(ShaderType type, const XMLElement& element, bool buildAll = false, const Vector<String>& globalDefines = Vector<String>(), const Vector<String>& globalDefineValues = Vector<String>());
  75. /// Return error message if parsing failed.
  76. String GetErrorMessage() const { return errorMessage_; }
  77. /// Return all combinations. Should only be called after building all combinations, because otherwise it only returns combinations built so far.
  78. const HashMap<String, unsigned>& GetAllCombinations() const { return combinations_; }
  79. /// Return whether a shader combination exists. Will be built on demand if all combinations not yet built.
  80. bool HasCombination(const String& name);
  81. /// Return a combination by name. Will be built on demand if all combinations not yet built.
  82. ShaderCombination GetCombination(const String& name);
  83. private:
  84. /// Parse options for a shader, then preprocess them for building combinations.
  85. bool ParseOptions(const XMLElement& element);
  86. /// Construct all shader combinations from the options. This is potentially slow
  87. void BuildCombinations();
  88. /// Construct one shader combination on demand based on active option bitmask. Return true if was valid.
  89. bool BuildCombination(unsigned active);
  90. /// Construct one shader combination on demand based on the name. Return true if was valid.
  91. bool BuildCombination(const String& name);
  92. /// Error message.
  93. String errorMessage_;
  94. /// Global defines.
  95. Vector<String> globalDefines_;
  96. /// Global define values.
  97. Vector<String> globalDefineValues_;
  98. /// Shader options.
  99. Vector<ShaderOption> options_;
  100. /// Option name to index mapping.
  101. HashMap<String, unsigned> nameToIndex_;
  102. /// Available combinations.
  103. HashMap<String, unsigned> combinations_;
  104. /// Combinations that were attempted to be built, but were not found.
  105. HashSet<String> failedCombinations_;
  106. /// Bitmasks of already built valid combinations.
  107. HashSet<unsigned> usedCombinations_;
  108. /// Total number of possible combinations, including invalid ones.
  109. unsigned maxCombinations_;
  110. /// Number of variation groups.
  111. unsigned numVariationGroups_;
  112. /// All combinations built flag.
  113. bool builtAll_;
  114. };
  115. }