ShaderVariation.h 6.4 KB

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