ShaderParser.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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. class XMLElement;
  27. /// Option definition and combination rules for constructing shader variations.
  28. struct ShaderOption
  29. {
  30. /// Variation name.
  31. String name_;
  32. /// Defines to use in compiling.
  33. Vector<String> defines_;
  34. /// Define values to use in compiling.
  35. Vector<String> defineValues_;
  36. /// Other variations to exclude.
  37. Vector<String> excludes_;
  38. /// Other variations to include.
  39. Vector<String> includes_;
  40. /// Required defines for variation to be compiled.
  41. Vector<String> requires_;
  42. /// Exclude indices.
  43. PODVector<unsigned> excludeIndices_;
  44. /// Include indices.
  45. PODVector<unsigned> includeIndices_;
  46. /// Option(s) which satisfy the requirements.
  47. PODVector<unsigned> requirementBits_;
  48. /// Variation flag. A variation excludes all other variations, unless in a separate group.
  49. bool isVariation_;
  50. };
  51. /// Combination of shader options, used for compiling a shader variation.
  52. struct ShaderCombination
  53. {
  54. /// Shader variation name.
  55. String name_;
  56. /// Defines to use in compiling.
  57. Vector<String> defines_;
  58. /// Define values to use in compiling.
  59. Vector<String> defineValues_;
  60. };
  61. /// %Shader definition parser. Constructs a list of shader variations from the definition.
  62. class ShaderParser
  63. {
  64. public:
  65. /// Parse from an XML element. Return true if successful.
  66. bool Parse(ShaderType type, const XMLElement& element, const Vector<String>& globalDefines = Vector<String>(), const Vector<String>& globalDefineValues = Vector<String>());
  67. /// Return error message if parsing failed.
  68. String GetErrorMessage() const { return errorMessage_; }
  69. /// Return number of combinations.
  70. unsigned GetNumCombinations() const { return combinations_.Size(); }
  71. /// Return all combinations.
  72. const HashMap<String, unsigned>& GetAllCombinations() const { return combinations_; }
  73. /// Return whether a shader combination exists.
  74. bool HasCombination(const String& name) const;
  75. /// Return a combination by name.
  76. bool GetCombination(ShaderCombination& dest, const String& name) const;
  77. private:
  78. /// Parse options for a shader.
  79. bool ParseOptions(const XMLElement& element);
  80. /// Construct shader combinations from the options.
  81. void BuildCombinations();
  82. /// Error message.
  83. String errorMessage_;
  84. /// Global defines.
  85. Vector<String> globalDefines_;
  86. /// Global define values.
  87. Vector<String> globalDefineValues_;
  88. /// Shader options.
  89. Vector<ShaderOption> options_;
  90. /// Available combinations.
  91. HashMap<String, unsigned> combinations_;
  92. };