ShaderParser.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "GraphicsDefs.h"
  25. #include "HashMap.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. /// Exclude indices.
  45. PODVector<unsigned> excludeIndices_;
  46. /// Include indices.
  47. PODVector<unsigned> includeIndices_;
  48. /// Option(s) which satisfy the requirements.
  49. PODVector<unsigned> requirementBits_;
  50. /// Variation flag. A variation excludes all other variations, unless in a separate group.
  51. bool isVariation_;
  52. };
  53. /// Combination of shader options, used for compiling a shader variation.
  54. struct ShaderCombination
  55. {
  56. /// Shader variation name.
  57. String name_;
  58. /// Defines to use in compiling.
  59. Vector<String> defines_;
  60. /// Define values to use in compiling.
  61. Vector<String> defineValues_;
  62. };
  63. /// %Shader definition parser. Constructs a list of shader variations from the definition.
  64. class ShaderParser
  65. {
  66. public:
  67. /// Parse from an XML element. Return true if successful.
  68. bool Parse(ShaderType type, const XMLElement& element, const Vector<String>& globalDefines = Vector<String>(), const Vector<String>& globalDefineValues = Vector<String>());
  69. /// Return error message if parsing failed.
  70. String GetErrorMessage() const { return errorMessage_; }
  71. /// Return number of combinations.
  72. unsigned GetNumCombinations() const { return combinations_.Size(); }
  73. /// Return all combinations.
  74. const HashMap<String, unsigned>& GetAllCombinations() const { return combinations_; }
  75. /// Return whether a shader combination exists.
  76. bool HasCombination(const String& name) const;
  77. /// Return a combination by name.
  78. bool GetCombination(ShaderCombination& dest, const String& name) const;
  79. private:
  80. /// Parse options for a shader.
  81. bool ParseOptions(const XMLElement& element);
  82. /// Construct shader combinations from the options.
  83. void BuildCombinations();
  84. /// Error message.
  85. String errorMessage_;
  86. /// Global defines.
  87. Vector<String> globalDefines_;
  88. /// Global define values.
  89. Vector<String> globalDefineValues_;
  90. /// Shader options.
  91. Vector<ShaderOption> options_;
  92. /// Available combinations.
  93. HashMap<String, unsigned> combinations_;
  94. };
  95. }