ShaderProgramResource.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/resource/ShaderProgramResource.h>
  6. #include <anki/resource/ResourceManager.h>
  7. #include <anki/gr/ShaderProgram.h>
  8. #include <anki/gr/GrManager.h>
  9. #include <anki/util/Filesystem.h>
  10. #include <anki/util/Functions.h>
  11. namespace anki
  12. {
  13. ShaderProgramResourceVariant::ShaderProgramResourceVariant()
  14. {
  15. }
  16. ShaderProgramResourceVariant::~ShaderProgramResourceVariant()
  17. {
  18. }
  19. ShaderProgramResource::ShaderProgramResource(ResourceManager* manager)
  20. : ResourceObject(manager)
  21. , m_binary(getAllocator())
  22. {
  23. }
  24. ShaderProgramResource::~ShaderProgramResource()
  25. {
  26. m_mutators.destroy(getAllocator());
  27. for(ShaderProgramResourceConstant& c : m_consts)
  28. {
  29. c.m_name.destroy(getAllocator());
  30. }
  31. m_consts.destroy(getAllocator());
  32. m_constBinaryMapping.destroy(getAllocator());
  33. for(auto it : m_variants)
  34. {
  35. ShaderProgramResourceVariant* variant = &(*it);
  36. getAllocator().deleteInstance(variant);
  37. }
  38. m_variants.destroy(getAllocator());
  39. }
  40. Error ShaderProgramResource::load(const ResourceFilename& filename, Bool async)
  41. {
  42. // Load the binary from the cache. It should have been compiled there
  43. StringAuto baseFilename(getTempAllocator());
  44. getFilepathFilename(filename, baseFilename);
  45. StringAuto binaryFilename(getTempAllocator());
  46. binaryFilename.sprintf("%s/%sbin", getManager().getCacheDirectory().cstr(), baseFilename.cstr());
  47. ANKI_CHECK(m_binary.deserializeFromFile(binaryFilename));
  48. const ShaderProgramBinary& binary = m_binary.getBinary();
  49. // Create the mutators
  50. if(binary.m_mutators.getSize() > 0)
  51. {
  52. m_mutators.create(getAllocator(), binary.m_mutators.getSize());
  53. for(U32 i = 0; i < binary.m_mutators.getSize(); ++i)
  54. {
  55. m_mutators[i].m_name = binary.m_mutators[i].m_name.getBegin();
  56. ANKI_ASSERT(m_mutators[i].m_name.getLength() > 0);
  57. m_mutators[i].m_values = binary.m_mutators[i].m_values;
  58. }
  59. }
  60. // Create the constants
  61. for(const ShaderProgramBinaryConstant& c : binary.m_constants)
  62. {
  63. U32 componentIdx;
  64. U32 componentCount;
  65. CString name;
  66. ANKI_CHECK(parseConst(c.m_name.getBegin(), componentIdx, componentCount, name));
  67. // Do the mapping
  68. ConstMapping mapping;
  69. mapping.m_component = componentIdx;
  70. if(componentIdx > 0)
  71. {
  72. const ShaderProgramResourceConstant* other = tryFindConstant(name);
  73. ANKI_ASSERT(other);
  74. mapping.m_constsIdx = U32(other - m_consts.getBegin());
  75. }
  76. else
  77. {
  78. mapping.m_constsIdx = m_consts.getSize();
  79. }
  80. m_constBinaryMapping.emplaceBack(getAllocator(), mapping);
  81. // Skip if const is there
  82. if(componentIdx > 0)
  83. {
  84. continue;
  85. }
  86. // Create new one
  87. ShaderProgramResourceConstant& in = *m_consts.emplaceBack(getAllocator());
  88. in.m_name.create(getAllocator(), name);
  89. in.m_index = m_consts.getSize() - 1;
  90. if(componentCount == 1)
  91. {
  92. in.m_dataType = c.m_type;
  93. }
  94. else if(componentCount == 2)
  95. {
  96. if(c.m_type == ShaderVariableDataType::U32)
  97. {
  98. in.m_dataType = ShaderVariableDataType::UVEC2;
  99. }
  100. else if(c.m_type == ShaderVariableDataType::I32)
  101. {
  102. in.m_dataType = ShaderVariableDataType::IVEC2;
  103. }
  104. else
  105. {
  106. ANKI_ASSERT(c.m_type == ShaderVariableDataType::F32);
  107. in.m_dataType = ShaderVariableDataType::VEC2;
  108. }
  109. }
  110. else if(componentCount == 3)
  111. {
  112. if(c.m_type == ShaderVariableDataType::U32)
  113. {
  114. in.m_dataType = ShaderVariableDataType::UVEC3;
  115. }
  116. else if(c.m_type == ShaderVariableDataType::I32)
  117. {
  118. in.m_dataType = ShaderVariableDataType::IVEC3;
  119. }
  120. else
  121. {
  122. ANKI_ASSERT(c.m_type == ShaderVariableDataType::F32);
  123. in.m_dataType = ShaderVariableDataType::VEC3;
  124. }
  125. }
  126. else if(componentCount == 4)
  127. {
  128. if(c.m_type == ShaderVariableDataType::U32)
  129. {
  130. in.m_dataType = ShaderVariableDataType::UVEC4;
  131. }
  132. else if(c.m_type == ShaderVariableDataType::I32)
  133. {
  134. in.m_dataType = ShaderVariableDataType::IVEC4;
  135. }
  136. else
  137. {
  138. ANKI_ASSERT(c.m_type == ShaderVariableDataType::F32);
  139. in.m_dataType = ShaderVariableDataType::VEC4;
  140. }
  141. }
  142. else
  143. {
  144. ANKI_ASSERT(0);
  145. }
  146. }
  147. m_shaderStages = binary.m_presentShaderTypes;
  148. // Do some RT checks
  149. if(!!(m_shaderStages & ShaderTypeBit::ALL_RAY_TRACING))
  150. {
  151. if((m_shaderStages & ~(ShaderTypeBit::ANY_HIT | ShaderTypeBit::CLOSEST_HIT)) != ShaderTypeBit::NONE)
  152. {
  153. ANKI_RESOURCE_LOGE("Any and closest hit shaders shouldn't coexist with other stages");
  154. return Error::USER_DATA;
  155. }
  156. if((m_shaderStages & ~(ShaderTypeBit::MISS | ShaderTypeBit::RAY_GEN)) != ShaderTypeBit::NONE)
  157. {
  158. ANKI_RESOURCE_LOGE("Miss and ray gen shaders shouldn't coexist with other stages");
  159. return Error::USER_DATA;
  160. }
  161. }
  162. return Error::NONE;
  163. }
  164. Error ShaderProgramResource::parseConst(CString constName, U32& componentIdx, U32& componentCount, CString& name)
  165. {
  166. const CString prefixName = "_anki_const_";
  167. const PtrSize prefix = constName.find(prefixName);
  168. if(prefix != 0)
  169. {
  170. // Simple name
  171. componentIdx = 0;
  172. componentCount = 1;
  173. name = constName;
  174. return Error::NONE;
  175. }
  176. Array<char, 2> number;
  177. number[0] = constName[prefixName.getLength()];
  178. number[1] = '\0';
  179. ANKI_CHECK(CString(number.getBegin()).toNumber(componentIdx));
  180. number[0] = constName[prefixName.getLength() + 2];
  181. ANKI_CHECK(CString(number.getBegin()).toNumber(componentCount));
  182. name = constName.getBegin() + prefixName.getLength() + 4;
  183. return Error::NONE;
  184. }
  185. void ShaderProgramResource::getOrCreateVariant(const ShaderProgramResourceVariantInitInfo& info,
  186. const ShaderProgramResourceVariant*& variant) const
  187. {
  188. // Sanity checks
  189. ANKI_ASSERT(info.m_setMutators.getEnabledBitCount() == m_mutators.getSize());
  190. ANKI_ASSERT(info.m_setConstants.getEnabledBitCount() == m_consts.getSize());
  191. // Compute variant hash
  192. U64 hash = 0;
  193. if(m_mutators.getSize())
  194. {
  195. hash = computeHash(info.m_mutation.getBegin(), m_mutators.getSize() * sizeof(info.m_mutation[0]));
  196. }
  197. if(m_consts.getSize())
  198. {
  199. hash =
  200. appendHash(info.m_constantValues.getBegin(), m_consts.getSize() * sizeof(info.m_constantValues[0]), hash);
  201. }
  202. // Check if the variant is in the cache
  203. {
  204. RLockGuard<RWMutex> lock(m_mtx);
  205. auto it = m_variants.find(hash);
  206. variant = (it != m_variants.getEnd()) ? *it : nullptr;
  207. if(variant != nullptr)
  208. {
  209. // Done
  210. return;
  211. }
  212. }
  213. // Create the variant
  214. WLockGuard<RWMutex> lock(m_mtx);
  215. // Check again
  216. auto it = m_variants.find(hash);
  217. variant = (it != m_variants.getEnd()) ? *it : nullptr;
  218. if(variant != nullptr)
  219. {
  220. // Done
  221. return;
  222. }
  223. // Create
  224. ShaderProgramResourceVariant* v = getAllocator().newInstance<ShaderProgramResourceVariant>();
  225. initVariant(info, *v);
  226. m_variants.emplace(getAllocator(), hash, v);
  227. variant = v;
  228. }
  229. void ShaderProgramResource::initVariant(const ShaderProgramResourceVariantInitInfo& info,
  230. ShaderProgramResourceVariant& variant) const
  231. {
  232. const ShaderProgramBinary& binary = m_binary.getBinary();
  233. // Get the binary program variant
  234. const ShaderProgramBinaryVariant* binaryVariant = nullptr;
  235. if(m_mutators.getSize())
  236. {
  237. // Create the mutation hash
  238. const U64 hash = computeHash(info.m_mutation.getBegin(), m_mutators.getSize() * sizeof(info.m_mutation[0]));
  239. // Search for the mutation in the binary
  240. // TODO optimize the search
  241. for(const ShaderProgramBinaryMutation& mutation : binary.m_mutations)
  242. {
  243. if(mutation.m_hash == hash)
  244. {
  245. binaryVariant = &binary.m_variants[mutation.m_variantIndex];
  246. break;
  247. }
  248. }
  249. }
  250. else
  251. {
  252. ANKI_ASSERT(binary.m_variants.getSize() == 1);
  253. binaryVariant = &binary.m_variants[0];
  254. }
  255. ANKI_ASSERT(binaryVariant);
  256. variant.m_binaryVariant = binaryVariant;
  257. // Set the constant values
  258. Array<ShaderSpecializationConstValue, 64> constValues;
  259. U32 constValueCount = 0;
  260. for(const ShaderProgramBinaryConstantInstance& instance : binaryVariant->m_constants)
  261. {
  262. const ShaderProgramBinaryConstant& c = binary.m_constants[instance.m_index];
  263. const U32 inputIdx = m_constBinaryMapping[instance.m_index].m_constsIdx;
  264. const U32 component = m_constBinaryMapping[instance.m_index].m_component;
  265. // Get value
  266. const ShaderProgramResourceConstantValue* value = nullptr;
  267. for(U32 i = 0; i < m_consts.getSize(); ++i)
  268. {
  269. if(info.m_constantValues[i].m_constantIndex == inputIdx)
  270. {
  271. value = &info.m_constantValues[i];
  272. break;
  273. }
  274. }
  275. ANKI_ASSERT(value && "Forgot to set the value of a constant");
  276. constValues[constValueCount].m_constantId = c.m_constantId;
  277. constValues[constValueCount].m_dataType = c.m_type;
  278. constValues[constValueCount].m_int = value->m_ivec4[component];
  279. ++constValueCount;
  280. }
  281. // Get the workgroup sizes
  282. if(!!(m_shaderStages & ShaderTypeBit::COMPUTE))
  283. {
  284. for(U32 i = 0; i < 3; ++i)
  285. {
  286. if(binaryVariant->m_workgroupSizes[i] != MAX_U32)
  287. {
  288. // Size didn't come from specialization const
  289. variant.m_workgroupSizes[i] = binaryVariant->m_workgroupSizes[i];
  290. }
  291. else
  292. {
  293. // Size is specialization const
  294. ANKI_ASSERT(binaryVariant->m_workgroupSizesConstants[i] != MAX_U32);
  295. const U32 binaryConstIdx = binaryVariant->m_workgroupSizesConstants[i];
  296. const U32 constIdx = m_constBinaryMapping[binaryConstIdx].m_constsIdx;
  297. const U32 component = m_constBinaryMapping[binaryConstIdx].m_component;
  298. const Const& c = m_consts[constIdx];
  299. (void)c;
  300. ANKI_ASSERT(c.m_dataType == ShaderVariableDataType::U32 || c.m_dataType == ShaderVariableDataType::UVEC2
  301. || c.m_dataType == ShaderVariableDataType::UVEC3
  302. || c.m_dataType == ShaderVariableDataType::UVEC4);
  303. // Find the value
  304. for(U32 i = 0; i < m_consts.getSize(); ++i)
  305. {
  306. if(info.m_constantValues[i].m_constantIndex == constIdx)
  307. {
  308. const I32 value = info.m_constantValues[i].m_ivec4[component];
  309. ANKI_ASSERT(value > 0);
  310. variant.m_workgroupSizes[i] = U32(value);
  311. break;
  312. }
  313. }
  314. }
  315. ANKI_ASSERT(variant.m_workgroupSizes[i] != MAX_U32);
  316. }
  317. }
  318. // Time to init the shaders
  319. if(!!(m_shaderStages & (ShaderTypeBit::ALL_GRAPHICS | ShaderTypeBit::COMPUTE)))
  320. {
  321. // Create the program name
  322. StringAuto progName(getTempAllocator());
  323. getFilepathFilename(getFilename(), progName);
  324. char* cprogName = const_cast<char*>(progName.cstr());
  325. if(progName.getLength() > MAX_GR_OBJECT_NAME_LENGTH)
  326. {
  327. cprogName[MAX_GR_OBJECT_NAME_LENGTH] = '\0';
  328. }
  329. ShaderProgramInitInfo progInf(cprogName);
  330. for(ShaderType shaderType : EnumIterable<ShaderType>())
  331. {
  332. if(!(ShaderTypeBit(1 << shaderType) & m_shaderStages))
  333. {
  334. continue;
  335. }
  336. ShaderInitInfo inf(cprogName);
  337. inf.m_shaderType = shaderType;
  338. inf.m_binary = binary.m_codeBlocks[binaryVariant->m_codeBlockIndices[shaderType]].m_binary;
  339. inf.m_constValues.setArray((constValueCount) ? constValues.getBegin() : nullptr, constValueCount);
  340. ShaderPtr shader = getManager().getGrManager().newShader(inf);
  341. const ShaderTypeBit shaderBit = ShaderTypeBit(1 << shaderType);
  342. if(!!(shaderBit & ShaderTypeBit::ALL_GRAPHICS))
  343. {
  344. progInf.m_graphicsShaders[shaderType] = shader;
  345. }
  346. else if(shaderType == ShaderType::COMPUTE)
  347. {
  348. progInf.m_computeShader = shader;
  349. }
  350. else
  351. {
  352. ANKI_ASSERT(0);
  353. }
  354. }
  355. // Create the program
  356. variant.m_prog = getManager().getGrManager().newShaderProgram(progInf);
  357. }
  358. else
  359. {
  360. ANKI_ASSERT(!!(m_shaderStages & ShaderTypeBit::ALL_RAY_TRACING));
  361. ANKI_ASSERT(!"TODO");
  362. }
  363. }
  364. } // end namespace anki