ShaderProgramResource.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Resource/ResourceObject.h>
  7. #include <AnKi/ShaderCompiler/ShaderProgramCompiler.h>
  8. #include <AnKi/Gr/Utils/Functions.h>
  9. #include <AnKi/Gr/ShaderProgram.h>
  10. #include <AnKi/Util/BitSet.h>
  11. #include <AnKi/Util/String.h>
  12. #include <AnKi/Util/HashMap.h>
  13. #include <AnKi/Util/WeakArray.h>
  14. #include <AnKi/Math.h>
  15. namespace anki {
  16. // Forward
  17. class ShaderProgramResourceVariantInitInfo;
  18. /// @addtogroup resource
  19. /// @{
  20. /// The means to mutate a shader program.
  21. /// @memberof ShaderProgramResource
  22. class ShaderProgramResourceMutator
  23. {
  24. public:
  25. CString m_name;
  26. ConstWeakArray<MutatorValue> m_values;
  27. ShaderProgramResourceMutator() = default;
  28. ShaderProgramResourceMutator(const ShaderProgramResourceMutator&) = delete; // Non-copyable
  29. ShaderProgramResourceMutator& operator=(const ShaderProgramResourceMutator&) = delete; // Non-copyable
  30. Bool valueExists(MutatorValue v) const
  31. {
  32. for(MutatorValue x : m_values)
  33. {
  34. if(v == x)
  35. {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. };
  42. /// Shader program resource variant.
  43. class ShaderProgramResourceConstant
  44. {
  45. public:
  46. ResourceString m_name;
  47. ShaderVariableDataType m_dataType = ShaderVariableDataType::kNone;
  48. U32 m_index = kMaxU32;
  49. };
  50. /// Shader program resource variant.
  51. class ShaderProgramResourceVariant
  52. {
  53. friend class ShaderProgramResource;
  54. public:
  55. ShaderProgramResourceVariant();
  56. ~ShaderProgramResourceVariant();
  57. /// @note On ray tracing program resources it points to the actual ray tracing program that contains everything.
  58. ShaderProgram& getProgram() const
  59. {
  60. return *m_prog;
  61. }
  62. /// Return true if the the variable is active in this variant.
  63. Bool isConstantActive(const ShaderProgramResourceConstant& var) const
  64. {
  65. return m_activeConsts.get(var.m_index);
  66. }
  67. const ShaderProgramBinaryVariant& getBinaryVariant() const
  68. {
  69. ANKI_ASSERT(m_binaryVariant);
  70. return *m_binaryVariant;
  71. }
  72. const Array<U32, 3>& getWorkgroupSizes() const
  73. {
  74. ANKI_ASSERT(m_workgroupSizes[0] != kMaxU32 && m_workgroupSizes[0] != 0);
  75. ANKI_ASSERT(m_workgroupSizes[1] != kMaxU32 && m_workgroupSizes[1] != 0);
  76. ANKI_ASSERT(m_workgroupSizes[2] != kMaxU32 && m_workgroupSizes[2] != 0);
  77. return m_workgroupSizes;
  78. }
  79. /// Only for hit ray tracing programs.
  80. U32 getShaderGroupHandleIndex() const
  81. {
  82. ANKI_ASSERT(m_shaderGroupHandleIndex < kMaxU32);
  83. return m_shaderGroupHandleIndex;
  84. }
  85. private:
  86. ShaderProgramPtr m_prog;
  87. const ShaderProgramBinaryVariant* m_binaryVariant = nullptr;
  88. BitSet<128, U64> m_activeConsts = {false};
  89. Array<U32, 3> m_workgroupSizes;
  90. U32 m_shaderGroupHandleIndex = kMaxU32; ///< Cache the index of the handle here.
  91. };
  92. /// The value of a constant.
  93. class ShaderProgramResourceConstantValue
  94. {
  95. public:
  96. union
  97. {
  98. U32 m_uint;
  99. UVec2 m_uvec2;
  100. UVec3 m_uvec3;
  101. UVec4 m_uvec4;
  102. I32 m_int;
  103. IVec2 m_ivec2;
  104. IVec3 m_ivec3;
  105. IVec4 m_ivec4;
  106. F32 m_float;
  107. Vec2 m_vec2;
  108. Vec3 m_vec3;
  109. Vec4 m_vec4;
  110. };
  111. U32 m_constantIndex;
  112. U8 _m_padding[sizeof(Vec4) - sizeof(m_constantIndex)];
  113. ShaderProgramResourceConstantValue()
  114. {
  115. zeroMemory(*this);
  116. }
  117. };
  118. static_assert(sizeof(ShaderProgramResourceConstantValue) == sizeof(Vec4) * 2, "Need it to be packed");
  119. /// Smart initializer of multiple ShaderProgramResourceConstantValue.
  120. class ShaderProgramResourceVariantInitInfo
  121. {
  122. friend class ShaderProgramResource;
  123. public:
  124. ShaderProgramResourceVariantInitInfo()
  125. {
  126. }
  127. ShaderProgramResourceVariantInitInfo(const ShaderProgramResourcePtr& ptr)
  128. : m_ptr(ptr)
  129. {
  130. }
  131. ~ShaderProgramResourceVariantInitInfo()
  132. {
  133. }
  134. template<typename T>
  135. ShaderProgramResourceVariantInitInfo& addConstant(CString name, const T& value);
  136. ShaderProgramResourceVariantInitInfo& addMutation(CString name, MutatorValue t);
  137. private:
  138. static constexpr U32 kMaxConstants = 32;
  139. static constexpr U32 kMaxMutators = 32;
  140. ShaderProgramResourcePtr m_ptr;
  141. Array<ShaderProgramResourceConstantValue, kMaxConstants> m_constantValues;
  142. BitSet<kMaxConstants> m_setConstants = {false};
  143. Array<MutatorValue, kMaxMutators> m_mutation; ///< The order of storing the values is important. It will be hashed.
  144. BitSet<kMaxMutators> m_setMutators = {false};
  145. };
  146. /// Shader program resource. It loads special AnKi programs.
  147. class ShaderProgramResource : public ResourceObject
  148. {
  149. public:
  150. ShaderProgramResource();
  151. ~ShaderProgramResource();
  152. /// Load the resource.
  153. Error load(const ResourceFilename& filename, Bool async);
  154. /// Get the array of constants.
  155. ConstWeakArray<ShaderProgramResourceConstant> getConstants() const
  156. {
  157. return m_consts;
  158. }
  159. /// Try to find a constant.
  160. const ShaderProgramResourceConstant* tryFindConstant(CString name) const
  161. {
  162. for(const ShaderProgramResourceConstant& m : m_consts)
  163. {
  164. if(m.m_name == name)
  165. {
  166. return &m;
  167. }
  168. }
  169. return nullptr;
  170. }
  171. /// Get the array of mutators.
  172. ConstWeakArray<ShaderProgramResourceMutator> getMutators() const
  173. {
  174. return m_mutators;
  175. }
  176. /// Try to find a mutator.
  177. const ShaderProgramResourceMutator* tryFindMutator(CString name) const
  178. {
  179. for(const ShaderProgramResourceMutator& m : m_mutators)
  180. {
  181. if(m.m_name == name)
  182. {
  183. return &m;
  184. }
  185. }
  186. return nullptr;
  187. }
  188. ShaderTypeBit getStages() const
  189. {
  190. return m_shaderStages;
  191. }
  192. const ShaderProgramBinary& getBinary() const
  193. {
  194. return m_binary.getBinary();
  195. }
  196. /// Get or create a graphics shader program variant. If returned variant is nullptr then it means that the mutation
  197. /// is skipped and thus incorrect.
  198. /// @note It's thread-safe.
  199. void getOrCreateVariant(const ShaderProgramResourceVariantInitInfo& info, const ShaderProgramResourceVariant*& variant) const;
  200. /// @copydoc getOrCreateVariant
  201. void getOrCreateVariant(const ShaderProgramResourceVariant*& variant) const
  202. {
  203. getOrCreateVariant(ShaderProgramResourceVariantInitInfo(), variant);
  204. }
  205. private:
  206. using Mutator = ShaderProgramResourceMutator;
  207. using Const = ShaderProgramResourceConstant;
  208. ShaderProgramBinaryWrapper m_binary;
  209. ResourceDynamicArray<Const> m_consts;
  210. ResourceDynamicArray<Mutator> m_mutators;
  211. class ConstMapping
  212. {
  213. public:
  214. U32 m_component = 0;
  215. U32 m_constsIdx = 0; ///< Index in m_consts
  216. };
  217. ResourceDynamicArray<ConstMapping> m_constBinaryMapping;
  218. mutable ResourceHashMap<U64, ShaderProgramResourceVariant*> m_variants;
  219. mutable RWMutex m_mtx;
  220. ShaderTypeBit m_shaderStages = ShaderTypeBit::kNone;
  221. ShaderProgramResourceVariant* createNewVariant(const ShaderProgramResourceVariantInitInfo& info) const;
  222. static Error parseConst(CString constName, U32& componentIdx, U32& componentCount, CString& name);
  223. };
  224. template<typename T>
  225. inline ShaderProgramResourceVariantInitInfo& ShaderProgramResourceVariantInitInfo::addConstant(CString name, const T& value)
  226. {
  227. const ShaderProgramResourceConstant* in = m_ptr->tryFindConstant(name);
  228. if(in != nullptr)
  229. {
  230. ANKI_ASSERT(in->m_dataType == getShaderVariableTypeFromTypename<T>());
  231. const U32 constIdx = U32(in - m_ptr->getConstants().getBegin());
  232. m_constantValues[constIdx].m_constantIndex = constIdx;
  233. memcpy(&m_constantValues[constIdx].m_int, &value, sizeof(T));
  234. m_setConstants.set(constIdx);
  235. }
  236. else
  237. {
  238. ANKI_RESOURCE_LOGW("Constant not found: %s", name.cstr());
  239. }
  240. return *this;
  241. }
  242. inline ShaderProgramResourceVariantInitInfo& ShaderProgramResourceVariantInitInfo::addMutation(CString name, MutatorValue t)
  243. {
  244. const ShaderProgramResourceMutator* m = m_ptr->tryFindMutator(name);
  245. ANKI_ASSERT(m);
  246. ANKI_ASSERT(m->valueExists(t));
  247. const PtrSize mutatorIdx = m - m_ptr->getMutators().getBegin();
  248. m_mutation[mutatorIdx] = t;
  249. m_setMutators.set(mutatorIdx);
  250. return *this;
  251. }
  252. /// @}
  253. } // end namespace anki