ShaderVariation.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // Copyright (c) 2008-2020 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/HashMap.h"
  24. #include "../Container/RefCounted.h"
  25. #include "../Container/ArrayPtr.h"
  26. #include "../Graphics/GPUObject.h"
  27. #include "../Graphics/GraphicsDefs.h"
  28. namespace Urho3D
  29. {
  30. class ConstantBuffer;
  31. class Shader;
  32. /// %Shader parameter definition.
  33. struct ShaderParameter
  34. {
  35. /// Construct with defaults.
  36. ShaderParameter() = default;
  37. /// Construct with name, glType and location, leaving the remaining attributes zero-initialized (used only in OpenGL).
  38. ShaderParameter(const String& name, unsigned glType, int location);
  39. /// Construct with type, name, offset, size, and buffer, leaving the remaining attributes zero-initialized (used only in Direct3D11).
  40. ShaderParameter(ShaderType type, const String& name, unsigned offset, unsigned size, unsigned buffer);
  41. /// Construct with type, name, register, and register count, leaving the remaining attributes zero-initialized (used only in Direct3D9).
  42. ShaderParameter(ShaderType type, const String& name, unsigned reg, unsigned regCount);
  43. /// %Shader type.
  44. ShaderType type_{};
  45. /// Name of the parameter.
  46. String name_{};
  47. union
  48. {
  49. /// Offset in constant buffer.
  50. unsigned offset_;
  51. /// OpenGL uniform location.
  52. int location_;
  53. /// Direct3D9 register index.
  54. unsigned register_;
  55. };
  56. union
  57. {
  58. /// Parameter size. Used only on Direct3D11 to calculate constant buffer size.
  59. unsigned size_;
  60. /// Parameter OpenGL type.
  61. unsigned glType_;
  62. /// Number of registers on Direct3D9.
  63. unsigned regCount_;
  64. };
  65. /// Constant buffer index. Only used on Direct3D11.
  66. unsigned buffer_{};
  67. /// Constant buffer pointer. Defined only in shader programs.
  68. ConstantBuffer* bufferPtr_{};
  69. };
  70. /// Vertex or pixel shader on the GPU.
  71. class URHO3D_API ShaderVariation : public RefCounted, public GPUObject
  72. {
  73. public:
  74. /// Construct.
  75. ShaderVariation(Shader* owner, ShaderType type);
  76. /// Destruct.
  77. ~ShaderVariation() override;
  78. /// Mark the GPU resource destroyed on graphics context destruction.
  79. void OnDeviceLost() override;
  80. /// Release the shader.
  81. void Release() override;
  82. /// Compile the shader. Return true if successful.
  83. bool Create();
  84. /// Set name.
  85. void SetName(const String& name);
  86. /// Set defines.
  87. void SetDefines(const String& defines);
  88. /// Return the owner resource.
  89. Shader* GetOwner() const;
  90. /// Return shader type.
  91. ShaderType GetShaderType() const { return type_; }
  92. /// Return shader name.
  93. const String& GetName() const { return name_; }
  94. /// Return full shader name.
  95. String GetFullName() const { return name_ + "(" + defines_ + ")"; }
  96. /// Return whether uses a parameter. Not applicable on OpenGL, where this information is contained in ShaderProgram instead.
  97. bool HasParameter(StringHash param) const { return parameters_.Contains(param); }
  98. /// Return whether uses a texture unit (only for pixel shaders). Not applicable on OpenGL, where this information is contained in ShaderProgram instead.
  99. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnits_[unit]; }
  100. /// Return all parameter definitions. Not applicable on OpenGL, where this information is contained in ShaderProgram instead.
  101. const HashMap<StringHash, ShaderParameter>& GetParameters() const { return parameters_; }
  102. /// Return vertex element hash.
  103. unsigned long long GetElementHash() const { return elementHash_; }
  104. /// Return shader bytecode. Stored persistently on Direct3D11 only.
  105. const PODVector<unsigned char>& GetByteCode() const { return byteCode_; }
  106. /// Return defines.
  107. const String& GetDefines() const { return defines_; }
  108. /// Return compile error/warning string.
  109. const String& GetCompilerOutput() const { return compilerOutput_; }
  110. /// Return constant buffer data sizes.
  111. const unsigned* GetConstantBufferSizes() const { return &constantBufferSizes_[0]; }
  112. /// Return defines with the CLIPPLANE define appended. Used internally on Direct3D11 only, will be empty on other APIs.
  113. const String& GetDefinesClipPlane() { return definesClipPlane_; }
  114. /// D3D11 vertex semantic names. Used internally.
  115. static const char* elementSemanticNames[];
  116. private:
  117. /// Load bytecode from a file. Return true if successful.
  118. bool LoadByteCode(const String& binaryShaderName);
  119. /// Compile from source. Return true if successful.
  120. bool Compile();
  121. /// Inspect the constant parameters and input layout (if applicable) from the shader bytecode.
  122. void ParseParameters(unsigned char* bufData, unsigned bufSize);
  123. /// Save bytecode to a file.
  124. void SaveByteCode(const String& binaryShaderName);
  125. /// Calculate constant buffer sizes from parameters.
  126. void CalculateConstantBufferSizes();
  127. /// Shader this variation belongs to.
  128. WeakPtr<Shader> owner_;
  129. /// Shader type.
  130. ShaderType type_;
  131. /// Vertex element hash for vertex shaders. Zero for pixel shaders. Note that hashing is different than vertex buffers.
  132. unsigned long long elementHash_{};
  133. /// Shader parameters.
  134. HashMap<StringHash, ShaderParameter> parameters_;
  135. /// Texture unit use flags.
  136. bool useTextureUnits_[MAX_TEXTURE_UNITS]{};
  137. /// Constant buffer sizes. 0 if a constant buffer slot is not in use.
  138. unsigned constantBufferSizes_[MAX_SHADER_PARAMETER_GROUPS]{};
  139. /// Shader bytecode. Needed for inspecting the input signature and parameters. Not used on OpenGL.
  140. PODVector<unsigned char> byteCode_;
  141. /// Shader name.
  142. String name_;
  143. /// Defines to use in compiling.
  144. String defines_;
  145. /// Defines to use in compiling + CLIPPLANE define appended. Used only on Direct3D11.
  146. String definesClipPlane_;
  147. /// Shader compile error string.
  148. String compilerOutput_;
  149. };
  150. }