ShaderVariation.h 6.4 KB

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