ShaderParser.h 3.8 KB

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