D3D9ShaderVariation.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "GPUObject.h"
  24. #include "GraphicsDefs.h"
  25. #include "HashSet.h"
  26. #include "RefCounted.h"
  27. #include "ArrayPtr.h"
  28. namespace Urho3D
  29. {
  30. class Shader;
  31. /// %Shader parameter definition.
  32. struct ShaderParameter
  33. {
  34. /// Construct with defaults.
  35. ShaderParameter() :
  36. type_(VS),
  37. register_(M_MAX_UNSIGNED),
  38. regCount_(0)
  39. {
  40. }
  41. /// Construct with parameters.
  42. ShaderParameter(ShaderType type, const String& name, unsigned reg, unsigned regCount) :
  43. type_(type),
  44. name_(name),
  45. register_(reg),
  46. regCount_(regCount)
  47. {
  48. }
  49. /// %Shader type.
  50. ShaderType type_;
  51. /// Name of the parameter.
  52. String name_;
  53. /// Hardware register.
  54. unsigned register_;
  55. /// Number of registers.
  56. unsigned regCount_;
  57. };
  58. /// Vertex or pixel shader on the GPU.
  59. class URHO3D_API ShaderVariation : public RefCounted, public GPUObject
  60. {
  61. public:
  62. /// Construct.
  63. ShaderVariation(Shader* owner, ShaderType type);
  64. /// Destruct.
  65. virtual ~ShaderVariation();
  66. /// Release the shader.
  67. virtual void Release();
  68. /// Compile the shader. Return true if successful.
  69. bool Create();
  70. /// Set name.
  71. void SetName(const String& name);
  72. /// Set defines.
  73. void SetDefines(const String& defines);
  74. /// Return the owner resource.
  75. Shader* GetOwner() const;
  76. /// Return shader type.
  77. ShaderType GetShaderType() const { return type_; }
  78. /// Return shader name.
  79. const String& GetName() const { return name_; }
  80. /// Return defines.
  81. const String& GetDefines() const { return defines_; }
  82. /// Return full shader name.
  83. String GetFullName() const { return name_ + "(" + defines_ + ")"; }
  84. /// Return compile error/warning string.
  85. const String& GetCompilerOutput() const { return compilerOutput_; }
  86. /// Return whether uses a parameter.
  87. bool HasParameter(StringHash param) const { return parameters_.Contains(param); }
  88. /// Return whether uses a texture unit (only for pixel shaders.)
  89. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnit_[unit]; }
  90. /// Return all parameter definitions.
  91. const HashMap<StringHash, ShaderParameter>& GetParameters() const { return parameters_; }
  92. private:
  93. /// Load bytecode from a file. Return true if successful.
  94. bool LoadByteCode(PODVector<unsigned>& byteCode, const String& binaryShaderName);
  95. /// Compile from source. Return true if successful.
  96. bool Compile(PODVector<unsigned>& byteCode);
  97. /// Inspect the constant parameters of the shader bytecode using MojoShader.
  98. void ParseParameters(unsigned char* bufData, unsigned bufSize);
  99. /// Strip comments from shader bytecode and store it.
  100. void CopyStrippedCode(PODVector<unsigned>& byteCode, unsigned char* bufData, unsigned bufSize);
  101. /// Save bytecode to a file.
  102. void SaveByteCode(const PODVector<unsigned>& byteCode, const String& binaryShaderName);
  103. /// Shader this variation belongs to.
  104. WeakPtr<Shader> owner_;
  105. /// Shader type.
  106. ShaderType type_;
  107. /// Shader name.
  108. String name_;
  109. /// Defines to use in compiling.
  110. String defines_;
  111. /// Shader compile error string.
  112. String compilerOutput_;
  113. /// Shader parameters.
  114. HashMap<StringHash, ShaderParameter> parameters_;
  115. /// Texture unit use flags.
  116. bool useTextureUnit_[MAX_TEXTURE_UNITS];
  117. };
  118. }