D3D11ShaderVariation.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Copyright (c) 2008-2015 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 "../../Container/HashSet.h"
  24. #include "../../Container/RefCounted.h"
  25. #include "../../Container/ArrayPtr.h"
  26. #include "../../Graphics/GPUObject.h"
  27. #include "../../Graphics/GraphicsDefs.h"
  28. namespace Atomic
  29. {
  30. class ConstantBuffer;
  31. class Shader;
  32. /// %Shader parameter definition.
  33. struct ShaderParameter
  34. {
  35. /// Construct with defaults.
  36. ShaderParameter() :
  37. type_(VS),
  38. buffer_(0),
  39. offset_(0),
  40. size_(0),
  41. bufferPtr_(0)
  42. {
  43. }
  44. /// Construct with parameters.
  45. ShaderParameter(ShaderType type, const String& name, unsigned buffer, unsigned offset, unsigned size, ConstantBuffer* ptr = 0) :
  46. type_(type),
  47. name_(name),
  48. buffer_(buffer),
  49. offset_(offset),
  50. size_(size),
  51. bufferPtr_(ptr)
  52. {
  53. }
  54. /// %Shader type.
  55. ShaderType type_;
  56. /// Name of the parameter.
  57. String name_;
  58. /// Constant buffer index.
  59. unsigned buffer_;
  60. /// Offset in constant buffer.
  61. unsigned offset_;
  62. /// Size of parameter in bytes.
  63. unsigned size_;
  64. /// Constant buffer pointer. Defined only in shader programs.
  65. ConstantBuffer* bufferPtr_;
  66. };
  67. /// Vertex or pixel shader on the GPU.
  68. class ATOMIC_API ShaderVariation : public RefCounted, public GPUObject
  69. {
  70. REFCOUNTED(ShaderVariation)
  71. public:
  72. /// Construct.
  73. ShaderVariation(Shader* owner, ShaderType type);
  74. /// Destruct.
  75. virtual ~ShaderVariation();
  76. /// Release the shader.
  77. virtual void Release();
  78. /// Compile the shader. Return true if successful.
  79. bool Create();
  80. /// Set name.
  81. void SetName(const String& name);
  82. /// Set defines.
  83. void SetDefines(const String& defines);
  84. /// Return the owner resource.
  85. Shader* GetOwner() const;
  86. /// Return shader type.
  87. ShaderType GetShaderType() const { return type_; }
  88. /// Return shader name.
  89. const String& GetName() const { return name_; }
  90. /// Return full shader name.
  91. String GetFullName() const { return name_ + "(" + defines_ + ")"; }
  92. /// Return whether uses a parameter.
  93. bool HasParameter(StringHash param) const { return parameters_.Contains(param); }
  94. /// Return whether uses a texture unit (only for pixel shaders.)
  95. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnit_[unit]; }
  96. /// Return all parameter definitions.
  97. const HashMap<StringHash, ShaderParameter>& GetParameters() const { return parameters_; }
  98. /// Return vertex element mask.
  99. unsigned GetElementMask() const { return elementMask_; }
  100. /// Return shader bytecode.
  101. const PODVector<unsigned char>& GetByteCode() const { return byteCode_; }
  102. /// Return defines.
  103. const String& GetDefines() const { return defines_; }
  104. /// Return compile error/warning string.
  105. const String& GetCompilerOutput() const { return compilerOutput_; }
  106. /// Return constant buffer data sizes.
  107. const unsigned* GetConstantBufferSizes() const { return &constantBufferSizes_[0]; }
  108. private:
  109. /// Load bytecode from a file. Return true if successful.
  110. bool LoadByteCode(const String& binaryShaderName);
  111. /// Compile from source. Return true if successful.
  112. bool Compile();
  113. /// Inspect the constant parameters and input layout (if applicable) from the shader bytecode.
  114. void ParseParameters(unsigned char* bufData, unsigned bufSize);
  115. /// Save bytecode to a file.
  116. void SaveByteCode(const String& binaryShaderName);
  117. /// Calculate constant buffer sizes from parameters.
  118. void CalculateConstantBufferSizes();
  119. /// Shader this variation belongs to.
  120. WeakPtr<Shader> owner_;
  121. /// Shader type.
  122. ShaderType type_;
  123. /// Vertex element mask for vertex shaders. Zero for pixel shaders.
  124. unsigned elementMask_;
  125. /// Shader parameters.
  126. HashMap<StringHash, ShaderParameter> parameters_;
  127. /// Texture unit use flags.
  128. bool useTextureUnit_[MAX_TEXTURE_UNITS];
  129. /// Constant buffer sizes. 0 if a constant buffer slot is not in use.
  130. unsigned constantBufferSizes_[MAX_SHADER_PARAMETER_GROUPS];
  131. /// Bytecode. Needed for inspecting the input signature and parameters.
  132. PODVector<unsigned char> byteCode_;
  133. /// Shader name.
  134. String name_;
  135. /// Defines to use in compiling.
  136. String defines_;
  137. /// Shader compile error string.
  138. String compilerOutput_;
  139. };
  140. }